? 上篇文章【點云處理】點云法向量估計及其加速(3)介紹了如何使用pcl提供的gpu版本法向量計算接口對點云發(fā)向量計算進行加速。不足之處在于點云k近鄰查找依然比較耗時,成為影響整體計算性能的瓶頸。這篇文章就如何優(yōu)化點云K近鄰查找效率進行實驗。上一篇文章的示例代碼中knn算法實際上主要是分為兩個部分:1).KDTree建樹;2).根據(jù)建好的KDTree做k近鄰搜索,從而建立k近鄰表。
創(chuàng)新新互聯(lián),憑借十余年的成都做網(wǎng)站、成都網(wǎng)站建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)經(jīng)驗,本著真心·誠心服務(wù)的企業(yè)理念服務(wù)于成都中小企業(yè)設(shè)計網(wǎng)站有1000多家案例。做網(wǎng)站建設(shè),選成都創(chuàng)新互聯(lián)。for (auto i=0; inearestKSearch(cloud->points[i], 10, neighbors_all[i], dists);
sizes[i] = neighbors_all[i].size();
}
我們應(yīng)該注意到,k近鄰搜索這一步我們只是簡簡單單使用了一個for循環(huán)進行逐個點云的搜索,完全沒有考慮到這一查找過程的可并行性,所以這里我們首先嘗試使用多線程對這一過程進行提速。當我們考慮多線程時可以使用boost::thread庫或者自從C++ 11開始支持的std::thread,也可以使用操作系統(tǒng)相關(guān)的線程API,如在Linux上,可以使用pthread庫。除此之外,還可以使用omp來使用多線程。它的好處是跨平臺,使用簡單。這里直接使用OpenMP提供的parallel編譯指示命令"#pragma omp parallel for"來告訴編譯器對后面的for并行執(zhí)行。編譯器會創(chuàng)建一個包含N(在運行時決定,通常為服務(wù)器的邏輯核數(shù))個線程的先成組,由他們來并行地運行后面的for語句塊。
# pragma omp parallel for
for (auto i=0; idists;
kdtree->nearestKSearch(cloud->points[i], 10, neighbors_all[i], dists);
sizes[i] = neighbors_all[i].size();
}
ok,再次編譯運行。
提速明顯,從處理器的資源利用來看每個核都分擔了壓力。
? ? ? ? ? ? ? ? ? ?圖:無omp加速
? ? ? ? ? ? ? ? ? ? 圖:有omp加速?
通過簡單調(diào)試也可以證實是用上了,因為結(jié)果是亂序的。
?
28 # pragma omp parallel for
29 for (auto i=0; idists;
31 kdtree->nearestKSearch(cloud->points[i], 10, neighbors_all[i], dists);
32 sizes[i] = neighbors_all[i].size();
33 printf("point[%d] is processing...\n", i);
34 }
?
point[53295] is processing...
point[21318] is processing...
point[42636] is processing...
point[31977] is processing...
point[53296] is processing...
point[21319] is processing...
point[53297] is processing...
point[31978] is processing...
point[21320] is processing...
point[53298] is processing...
【補充:我項目中用到的CMakeLists.txt】
cmake_minimum_required(VERSION 3.0.2)
project(calc_normal)
### Compile as C++14, supported in ROS Kinetic and newer
add_compile_options(-std=c++14)
SET(CMAKE_BUILD_TYPE RELEASE CACHE STRING "Set build type release" FORCE)
option(USE_DEBUG "Debug Code" OFF)
option(USE_OPENMP "Use OpenMP" ON)
if(USE_DEBUG)
add_definitions(-DUSE_DEBUG)
endif()
if(USE_OPENMP)
find_package(OpenMP REQUIRED)
if(${OPENMP_FOUND})
message("Find OpenMP")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
endif()
endif(USE_OPENMP)
find_package(CUDA REQUIRED)
find_package(PCL 1.11 REQUIRED)
if(NOT ${PCL_FOUND})
message("PCL 1.11 Not Found ...Searching 1.12")
find_package(PCL 1.12 REQUIRED)
endif(NOT ${PCL_FOUND})
message("PCL_INCLUDE_DIRS" ${PCL_INCLUDE_DIRS})
include_directories(${PCL_INCLUDE_DIRS})
message("PCL_LIBRARY_DIRS" ${PCL_LIBRARY_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
find_package(catkin REQUIRED COMPONENTS
roscpp
std_msgs
message_generation
sensor_msgs
)
catkin_package(
CATKIN_DEPENDS
message_runtime
)
if(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64")
message("embed_platform on")
include_directories(${CUDA_INCLUDE_DIRS})
link_directories(/usr/local/cuda/targets/aarch64-linux/lib)
link_directories(/usr/local/cuda/lib64)
set(CUDA_GEN_CODE "-gencode=arch=compute_72,code=sm_72")
set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} ${CUDA_GEN_CODE}")
else()
message("embed_platform off")
include_directories(${CUDA_INCLUDE_DIRS})
link_directories(/usr/local/cuda/lib64)
set(CUDA_GEN_CODE "-gencode=arch=compute_75,code=sm_75")
set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} ${CUDA_GEN_CODE}")
endif()
include_directories(
${catkin_INCLUDE_DIRS}
)
file(GLOB_RECURSE PROJECT_SRC
./src/*.cpp
)
cuda_add_executable(${PROJECT_NAME} src/main.cpp ${PROJECT_SRC})
## Specify libraries to link a library or executable target against
target_link_libraries(${PROJECT_NAME} cudart)
target_link_libraries(${PROJECT_NAME} ${catkin_LIBRARIES})
target_link_libraries(${PROJECT_NAME} ${PCL_LIBRARIES})
if(USE_OPENMP)
if(${OpenMP_FOUND})
target_link_libraries(${PROJECT_NAME} OpenMP::OpenMP_CXX)
endif()
endif(USE_OPENMP)
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧