ArrayFire统一后端的使用

Usage of unified backend in ArrayFire

我正在尝试 select 我的 arrayfire 后端基于可用后端及其功能(如果后端支持双打,则使用它,否则不使用)。因此,我创建了一个 CMakeLists.txt-file:

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_CPU_LIBRARIES} ${ArrayFire_OpenCL_LIBRARIES} 替换 ${ArrayFire_Unified_LIBRARIES} 时,我得到输出

1
Backend 1 supports double
Backend 1 is in use

当切换库的顺序时,我得到

4
Backend 4 is in use

因此表明只能看到一个后端,即使我有两个可能的后端。 那么,是我的代码有bug,还是我的方法有误?

首先,我们建议在基于 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

您不需要 link 对抗 forge,它将在运行时由 ArrayFire 加载,图形功能将自动启用。

为了使 ArrayFire 和统一后端加载的任何运行时库(forge、freeimage)正常工作,这些库所在位置的路径(通常 /opt/arrayfire/lib)对于运行时加载应该是可见的.请完成 installing tutorial 以验证您是否已完成所需的所有步骤。