Playing with ArrayFire: 03 The first ArrayFire program


Preface

In "Playing with ArrayFire: 02 Good Things, Sharp Tools", we have learned about the installation and environment configuration of ArrayFire under Windows systems. In this article, we will continue to learn how ArrayFire works with Visual Studio 2019 software Use and create the first ArrayFire program.


1. Add ArrayFire to existing solutions

  1. Open Visual Studio 2019, create a new project -> empty project, and name it "HelloWorld";
  2. Choose x64 as the solution platform and add the new item "HelloWorld.cpp" in the source file;
  1. Modify "HelloWorld" project properties:

    01 Select the path "Project -> Properties -> Configuration Properties -> VC++ Directory -> Include Directory": add the include path in ArrayFire, namely "$(AF_PATH)/include ";

    02 Select the path "Project -> Properties -> Configuration Properties -> Linker -> General -> Additional Library Directory": add the lib path in ArrayFire, namely "$(AF_PATH)/lib ";

    03 Select the path "Project -> Properties -> Configuration Properties -> Linker -> Input -> Additional Dependencies": add "afcpu.lib" (here, because the first ArrayFire program only runs on the CPU, there is no Add "afcuda.lib" and "afopencl.lib").

Two, test case

1. Sample code

After we install ArrayFire, we will find many classic examples officially given by ArrayFire in the installation path "C:\Program Files\ArrayFire\v3\examples", so first, we can run one of the "HelloWorld" examples to verify the above Whether the configuration is successful.

  1. Use a text file to open "HelloWorld.cpp" under the path "C:\Program Files\ArrayFire\v3\examples\helloworld", and copy all the contents to the HelloWorld.cpp you created;

2. "Ctrl+F5" to run the code, if there are no bugs in the program and the debugging console is as shown in the figure below, congratulations on your smooth entry into ArrayFire!

2. The first ArrayFire program

Next, we will use ArrayFire to write a piece of our own code. Here, only a few precautions that may be encountered are briefly explained:

    01 code needs to include the header file "arrayfire.h";
    02 code needs to use the namespace af of ArrayFire;

The following is a sample code for generating constant matrix and (0-1) random number matrix:

#include <arrayfire.h>

using namespace af;

int main(void)
{
    
    
	//生成常量矩阵
	array array0;
	array0 = constant(1, 3, 2);
	af_print(array0);
	
	//生成(0-1)随机数矩阵
	array rand0;
	rand0 = randu(2, 4);
	af_print(rand0);

	system("pause");
	return 0;
}

The following is the output of the debug console:
Insert picture description here

3. "Fanwai" description

I believe that all the friends here are already cheering. But what the blogger wants to tell you is whether Visual Studio configures the ArrayFire matrix library or other matrix libraries, the method is very similar: you need to let your solution find the file you call, so we need to let it know The location of your include directory header file (.h), the location of the additional library directory library file (.lib) and which lib file to include. Similarly, the ArrayFire official website also gives the corresponding relationship between the path and the file, as follows.

    AF_PATH/include: ArrayFire header files (including directories);
    AF_PATH/lib: all ArrayFire back-end libraries, dlls and dependent dlls (library directories);
    AF_PATH/examples: starting examples. Some examples also have pre-built executable files;
    AF_PATH/cmake: cmake configuration file for automatic configuration of external projects;
    AF_PATH / uninstall.exe: uninstall program. Note: The AF_PATH environment variable points to the installation location. The default installation location is C:\Program Files\ArrayFire\v3
    


Guess you like

Origin blog.csdn.net/weixin_42467801/article/details/113565963