我正试图根据可用的后端及其功能来选择我的arrayfire后端(如果后端支持doubles,则使用它,否则不使用(。因此,我创建了一个CMakeLists.txt文件:
cmake_minimum_required(VERSION 3.10)
project(arrayfire_test)
if(USE_EXTERNAL_PATHS)
set(CMAKE_C_COMPILER ${C_COMPILER})# CACHE PATH "" FORCE)
set(CMAKE_CXX_COMPILER ${CXX_COMPILER})#CACHE PATH "" FORCE)
else()
set(CMAKE_C_COMPILER mpicc)
set(CMAKE_CXX_COMPILER mpic++)
endif()
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
set(CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native -fopenmp-simd")
elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
set(CXX_FLAGS "${CMAKE_CXX_FLAGS} -xHost")
endif()
set(C_FLAGS "${CMAKE_C_FLAGS} ${CXX_FLAGS} -fPIC -flto -march=native -fopenmp -O2 -funroll-loops -funroll-all-loops -fstrict-aliasing -std=gnu++14")
set(CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CXX_FLAGS} -fPIC -flto -march=native -fopenmp -O2 -funroll-loops -funroll-all-loops -fstrict-aliasing -std=gnu++14")
string(REPLACE " " ";" REPLACED_CXX_FLAGS ${CXX_FLAGS})
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/lib)
set(ArrayFire_DIR "/opt/arrayfire/share/ArrayFire/cmake")
FIND_PACKAGE(ArrayFire)
find_package(Armadillo REQUIRED PATHS "/opt/armadillo")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp -std=c++11 -O0 -g3 -flto -march=native")
set(FORGE_LIBRARIES "/opt/arrayfire/lib/libforge.so")
INCLUDE_DIRECTORIES(${ArrayFire_INCLUDE_DIRS}
${ARMADILLO_INCLUDE_DIRS})
add_executable(${PROJECT_NAME} "source/main.cpp")
target_compile_options(${PROJECT_NAME} PRIVATE ${REPLACED_CXX_FLAGS})
TARGET_LINK_LIBRARIES(${PROJECT_NAME}
${ArrayFire_Unified_LIBRARIES}
${ARMADILLO_LIBRARIES})# ${FORGE_LIBRARIES})
主文件:
int main(void){
int backends = af::getAvailableBackends ();
std::cout << backends << "n";
std::vector<af_backend> possible_backends;
if(backends & AF_BACKEND_CUDA)
possible_backends.push_back(AF_BACKEND_CUDA);
if(backends & AF_BACKEND_OPENCL)
possible_backends.push_back(AF_BACKEND_OPENCL);
if(backends & AF_BACKEND_CPU)
possible_backends.push_back(AF_BACKEND_CPU);
if(possible_backends.size() == 0){
std::cerr << "No backends availablen";
return -1;
}
for(size_t i = 0; i < possible_backends.size(); ++i){
af::setBackend (possible_backends[i]);
af::setDevice (0);
if(af::isDoubleAvailable (0)){
std::cout << "Backend " << af::getActiveBackend () << " supports doublen";
break;
}
}
std::cout << "Backend " << af::getActiveBackend () << " is in usen";
return 0;
}
根据http://arrayfire.org/docs/unifiedbackend.htm,在使用此后端时,我必须使用${ArrayFire_Unified_LIBRARIES}
。但是当运行代码时,我得到
0
No backends available
尽管我同时拥有CPU和OpenCL作为后端。当用${ArrayFire_CPU_LIBRARIES} ${ArrayFire_OpenCL_LIBRARIES}
替换${ArrayFire_Unified_LIBRARIES}
时,我得到了输出
1
Backend 1 supports double
Backend 1 is in use
当切换库的顺序时,我得到
4
Backend 4 is in use
从而指示只能看到一个后端,即使我有两个可能的后端。因此,我的代码中是否存在错误,或者我的方法是否错误?
首先,我们建议在基于cmake的项目中集成ArrayFire时使用导入目标而不是原始cmake变量。由于您的最低cmake版本是3.10,因此使用导入目标应该不会有任何问题。
find_package(ArrayFire)
add_executable(<my_executable> [list your source files here])
# To use Unified backend, do the following.
# Unified backend lets you choose the backend at runtime
target_link_libraries(<my_executable> ArrayFire::af)
# for directly using cpu/cuda/opencl backend
# use `ArrayFire::[afcpu | afcuda | afopencl]` respectively
你不需要链接伪造,它将在运行时由ArrayFire加载,图形功能将自动启用。
对于任何由ArrayFire和统一后端加载的运行时库(forge、freeimage(,为了正常工作,这些库所在位置的路径(通常为/opt/arrayfire/lib
(对于运行时加载应该是可见的。请仔细阅读安装教程,以验证您是否已完成所需的所有步骤。