gpt4 book ai didi

c++ - ArrayFire中统一后端的使用

转载 作者:行者123 更新时间:2023-12-01 14:53:28 27 4
gpt4 key购买 nike

我试图根据可用的后端及其功能选择我的arrayfire后端(如果后端支持double,则使用它,否则不使用)。因此,我创建了一个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 available\n";
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 double\n";
break;
}
}
std::cout << "Backend " << af::getActiveBackend () << " is in use\n";
return 0;
}

根据 http://arrayfire.org/docs/unifiedbackend.htm,使用此后端时必须使用 ${ArrayFire_Unified_LIBRARIES}。但是当运行代码时,我得到
0
No backends available

即使我同时拥有CPU和OpenCL作为后端。当用 ${ArrayFire_Unified_LIBRARIES}替换 ${ArrayFire_CPU_LIBRARIES} ${ArrayFire_OpenCL_LIBRARIES}时,我得到了输出
1
Backend 1 supports double
Backend 1 is in use

当切换库的顺序时,我得到
4
Backend 4 is in use

从而表明即使我有两个可能的后端,也只能看到一个后端。
因此,我的代码中是否存在错误,或者我的方法是否错误?

最佳答案

首先,我们建议在将ArrayFire集成到基于cmake的项目中时,使用导入目标而不是原始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)的路径对于运行时加载应该是可见的。请检查 installing tutorial以确认您是否已完成所需的所有步骤。

关于c++ - ArrayFire中统一后端的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60378848/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com