Using ArrayFire with Microsoft Windows and Visual Studio (original) (raw)
If you have not already done so, please make sure you have installed, configured, and tested ArrayFire following the installation instructions.
The big picture
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 backend libraries, dlls, and dependency dlls (library directory)
- AF_PATH/examples : Examples to get started
- AF_PATH/cmake : CMake config files
- AF_PATH/uninstall.exe : Uninstaller
Build and Run Helloworld
This can be done in two ways either by using CMake build tool or using Visual Studio directly.
Using CMake
- Download and install CMake, preferably the latest version.
- Open CMake-GUI and set the field Where is the source code to the root directory of examples.
- Set the field Where to build the binaries to path_to_examples_root_dir/build and click the
Configurebutton. - CMake will prompt you to create the
builddirectory if not already present. Click "yes" to create the build directory. - Before the configuration begins, CMake will show you a list (drop-down menu) of available Visual Studio versions. Select one and check the radio button that says Use default native compilers and click finish.
- CMake will show you errors in red text, if any, once configuration is finished. Sometimes a second configuration is necessary.
- Click Generate button to generate the Visual Studio solution files for the examples.
- Click Open Project button that is right next to Generate button to open the solution file.
- You will see the examples segregated into four sets named after the compute backends of ArrayFire: cpu, cuda, oneapi, & opencl, if you installed all backends. Select the helloworld project from any of the installed backends, mark it as startup project, and hit
F5. - Once the helloworld example builds, you will see a console window with the output from helloworld program.
- Open Visual Studio and create an empty C++ project.
- Right-click the project and add an existing source file
examples/helloworld/helloworld.cppto this project. - Add
"$(AF_PATH)/include;"to Project Properties -> C/C++ -> General -> Additional Include Directories. - Add
"$(AF_PATH)/lib;"to Project Properties -> Linker -> General -> Additional Library Directories. - Add
afcpu.lib,afcuda.lib,afoneapi.lib, orafopencl.libto Project Properties -> Linker -> Input -> Additional Dependencies. based on your preferred backend. - (Optional) You may choose to define
NOMINMAX,AF_<CPU/CUDA/ONEAPI/OPENCL>, orAF_<DEBUG/RELEASE>in your projects. This can be added to Project Properties -> C/C++ -> General -> Preprocessor-> Preprocessory definitions. - Build and run the project. You will see a console window with the output from helloworld program.
Using ArrayFire within Existing Visual Studio Projects
This is divided into three parts:
- Part A: Adding ArrayFire to an existing solution (Single Backend)
- Part B: Adding ArrayFire CUDA to a new/existing CUDA project
- Part C: Project with all ArrayFire backends
Part A: Adding ArrayFire to an existing solution (Single Backend)
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:
- Add
"$(AF_PATH)/include;"to Project Properties -> C/C++ -> General -> Additional Include Directories. - Add
"$(AF_PATH)/lib;"to Project Properties -> Linker -> General -> Additional Library Directories. - Add
afcpu.lib,afcuda.lib,afopencl.lib, oraf.libto Project Properties -> Linker -> Input -> Additional Dependencies. based on your preferred backend.
Part B: Adding ArrayFire CUDA to a new/existing CUDA project
Lastly, if your project contains custom CUDA code, the instructions are slightly different as it requires using a CUDA NVCC Project:
- Create a custom "CUDA NVCC project" in Visual Studio
- Add
"$(AF_PATH)/include;"to Project Properties -> CUDA C/C++ -> General -> Additional Include Directories. - Add
"$(AF_PATH)/lib;"to Project Properties -> Linker -> General -> Additional Library Directories. - Add
afcpu.lib,afcuda.lib,afopencl.lib, oraf.libto Project Properties -> Linker -> Input -> Additional Dependencies. based on your preferred backend.
Part C: Project with all ArrayFire backends
If you wish to create a project that allows you to use all the ArrayFire backends with ease, you should use af.lib in step 3 from Part A.
You can alternately download the template project from ArrayFire Template Projects
Using ArrayFire with CMake
ArrayFire ships with a series of CMake scripts to make finding and using the library easy.
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) add_executable( [list your source files here])
The Unified backend lets you choose the backend at runtime.
To use the Unified backend, do the following:
target_link_libraries( ArrayFire::af)
, where <my_executable> is the name of the executable to create. See the CMake documentation for more information on how to use CMake. To link with a specific backend directly, replace the ArrayFire::af with the following for their respective backends.
ArrayFire::afcpufor CPU backend.ArrayFire::afcudafor CUDA backend.ArrayFire::afoneapifor oneAPI backend.ArrayFire::afopenclfor OpenCL backend.
Next, instruct CMake to create build instructions and compile them. 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.
- Under "source directory", add the path to your project.
- Under "build directory", add the path to your project and append /build.
- Click "configure" and choose a 64-bit Visual Studio generator.
- If the configuration was successful, click "generate". This will create a my-project.sln file under build. Click
Open Projectin CMake-GUI to open the solution and compile the ALL_BUILD project.