|
If you have not already done so, please make sure you have installed, configured, and tested ArrayFire following the installation instructions.
The ArrayFire Windows installer creates the following:
AF_PATH
environment variable to point to the installation location. The default install location is C:\Program Files\ArrayFire\v3
AF_PATH/include
: Header files for ArrayFire (include directory)AF_PATH/lib
: All ArrayFire backends libraries, dlls and dependency dlls (library directory)AF_PATH/examples
: Examples to get started. Some examples also have pre-built exectuablesAF_PATH/cmake
: CMake config files for automatic configuration by external projectsAF_PATH/uninstall.exe
: UninstallerAF_PATH/*
: Other miscellenous files including licenses, logos, copyrightsThe installer also appends %AF_PATH%/lib
to the User PATH variable.
To add %AF_PATH%/lib
to PATH for all users see the windows section in installation instructions.
When using CUDA with ArrayFire you may encounter a linker error indicating the NVVM DLLs are missing. This is because the NVVM DLLs are not part of the standard CUDA_PATH\bin
installation directory that is added to your PATH
when the CUDA installer runs. Thus, NVVM will not be found during runtime. There are a few ways to deal with this issue:
CUDA_PATH\nvvm\bin\nvvm64_30_0.dll
to CUDA_PATH\bin
. This is a one time copy such that the NVVM DLL is now with all the other CUDA dlls and in a directory that is a part of PATH and hence the DLL can be detected automatically.%CUDA_PATH%\nvvm\bin
to the system PATH environment variable. This will allow automatic detection by the system and No further copying will be required. ArrayFire does not add this to PATH since the CUDA installer doesn't add it to PATH.The ArrayFire installer ships with a few pre-built executables with the examples. These should run out of the box when double clicked.
Some prebuilt examples are:
Note: For the CUDA executables, you will need to copy CUDA_PATH\nvvm\bin\nvvm64_30_0.dll
to the location of the executables.
AF_PATH/examples/helloworld/helloworld.sln
.helloworld
example. Be sure to, select the platform/configuration of your choice using the platform drop-down (the options are CPU, CUDA, OpenCL, and Unified) and Solution Configuration drop down (options of Release and Debug) menus.helloworld
example.This is divided into 4 parts:
Note: If you plan on using Native CUDA code in the project, use the steps under Part B.
Adding a single backend to an existing project is quite simple.
"$(AF_PATH)/include;"
to Project Properties -> C/C++ -> General -> Additional Include Directories."$(AF_PATH)/lib;"
to Project Properties -> Linker -> General -> Additional Library Directories.afcpu.lib
or afcuda.lib
or afopencl.lib
to Project Properties -> Linker -> Input -> Additional Dependencies. based on your preferred backend.NOMINMAX
, AF_<CPU/CUDA/OPENCL>
and/or AF_<DEBUG/RELEASE>
in your projects. This can be added to Project Properties -> C/C++ -> General -> Preprocessor-> Preprocessory definitions.If you are using the CUDA backend, it is important to ensure that the CUDA NVVM DLLs are copied to the exectuable directory. This can be done by adding a post build event.
Open the Project Properties -> Build Events -> Post Build Events dialog and add the following lines to it.
Lastly, if your project contains custom CUDA code, the instructions are slightly different as it requires using a CUDA NVCC Project:
"$(AF_PATH)/include;"
to Project Properties -> CUDA C/C++ -> General -> Additional Include Directories."$(AF_PATH)/lib;"
to Project Properties -> Linker -> General -> Additional Library Directories.afcpu.lib
or afcuda.lib
or afopencl.lib
to Project Properties -> Linker -> Input -> Additional Dependencies. based on your preferred backend.NOMINMAX
, AF_CUDA
and/or AF_<DEBUG/RELEASE>
in your projects. This can be added to Project Properties -> C/C++ -> General -> Preprocessor-> Preprocessory definitions.If you wish to create a project that allows you to use all the ArrayFire backends with ease, the best way to go is to copy the HelloWorld sln/vcxproj/cpp file trio and rename them to suit your project.
All the ArrayFire examples are pre-configured for all ArrayFire backends as well as the Unified API. These can be chosen from the Solution/Platform configuration drop down boxes.
You can alternately download the template project from ArrayFire Template Projects
NOTE: The ArrayFire installer sets up CMake file and registry so that it can be found by CMake by simply using the Find_PACKAGE(ArrayFire)
command.
If you are writing a new ArrayFire project in C/C++ from scratch, we suggest you grab a copy of our CMake Project Example; however, it is useful to read the documentation below in case you need to add ArrayFire to an existing project.
As discussed above, ArrayFire ships with a series of CMake scripts to make finding and using our library easy. The scripts will automatically find all versions of the ArrayFire library and pick the most powerful of the installed backends (typically CUDA).
First create a file called CMakeLists.txt
in your project directory:
cd your-project-directory touch CMakeLists.txt
and populate it with the following code:
FIND_PACKAGE(ArrayFire) INCLUDE_DIRECTORIES(${ArrayFire_INCLUDE_DIRS}) ... [gather source files, etc.] # If you intend to use OpenCL, you need to find it FIND_PACKAGE(OpenCL) SET(EXTRA_LIBS ${CMAKE_THREAD_LIBS_INIT} ${OpenCL_LIBRARIES}) # Or if you intend to use CUDA, you need it as well as NVVM: FIND_PACKAGE(CUDA) FIND_PACKAGE(NVVM) # this FIND script can be found in the ArrayFire CMake example repository SET(EXTRA_LIBS ${CMAKE_THREAD_LIBS_INIT} ${CUDA_LIBRARIES} ${NVVM_LIB}) ADD_EXECUTABLE(my_executable [list your source files here]) TARGET_LINK_LIBRARIES(my_executable ${ArrayFire_LIBRARIES} ${EXTRA_LIBS})
where my_executable
is the name of the executable you wish to create. See the CMake documentation for more information on how to use CMake. Clearly the above code snippet precludes the use of both CUDA and OpenCL, see the ArrayFire CMake Example for an example of how to build executables for both backends from the same CMake script.
In the above code listing, the FIND_PACKAGE
will find the ArrayFire include files, libraries, and define several variables including:
ArrayFire_INCLUDE_DIRS - Location of ArrayFire's include directory. ArrayFire_LIBRARIES - Location of ArrayFire's libraries. This will default to a GPU backend if one is found ArrayFire_FOUND - True if ArrayFire has been located
If you wish to use a specific backend, the find script also defines these variables:
ArrayFire_CPU_FOUND - True of the ArrayFire CPU library has been found. ArrayFire_CPU_LIBRARIES - Location of ArrayFire's CPU library, if found ArrayFire_CUDA_FOUND - True of the ArrayFire CUDA library has been found. ArrayFire_CUDA_LIBRARIES - Location of ArrayFire's CUDA library, if found ArrayFire_OpenCL_FOUND - True of the ArrayFire OpenCL library has been found. ArrayFire_OpenCL_LIBRARIES - Location of ArrayFire's OpenCL library, if found ArrayFire_Unified_FOUND - True of the ArrayFire Unified library has been found. ArrayFire_Unified_LIBRARIES - Location of ArrayFire's Unified library, if found
Therefore, if you wish to target a specific specific backend, simply replace ${ArrayFire_LIBRARIES}
with ${ArrayFire_CPU}
, ${ArrayFire_OPENCL}
, ${ArrayFire_CUDA}
, or ${ArrayFire_Unified}
in the TARGET_LINK_LIBRARIES
command above.
Next we need to instruct CMake to create build instructions and then compile. We suggest using CMake's out-of-source build functionality to keep your build and source files cleanly separated. To do this open the CMake GUI.
The ArrayFire CMake Example is a CMake project used to demo how ArrayFire can be using with a CMake project.
Note: The CMake project does not add the post build event to copy the NVVM DLLs in case of CUDA backend. You will need to either copy it manually to the exectuable directory, or pick another solution for it.