【OpenCL】Course Notes

Course link: https://www.bilibili.com/video/BV1zC4y1s7Ax/?spm_id_from=333.337.search-card.all.click&vd_source=23174149881549c431809ba9bd3acade

Introduction to OpenCL

OpenCL computing architecture

Insert image description here

Multi-platform support
Insert image description here

Environment configuration

Reference:
[1] https://blog.csdn.net/Ciellee/article/details/122379217
You can also use vcpkg to install opencl environment. vcpkg is a c++ package manager, which can easily install and manage c++ third-party libraries.

vcpkg install opencv:x64-windows

Output platform information supported by local OpenCL

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cassert>

#include <CL/cl.h>

using namespace std;

// 检查返回值错误
#define CHECK_ERRORS(ERR) \
	if(ERR != CL_SUCCESS){
      
       \
		cerr << "OpenCL error code" << ERR \
			 << "file: " << __FILE__ \
			 << "line: " << __LINE__ \
			 << ".\nExiting..." << endl; \
		exit(1); \
	}

int main(int argc, const char** argv)
{
    
    
	cl_int err = CL_SUCCESS;

	// 1. 获取当前设备所有支持OpenCL的平台的数量
	cl_uint num_of_platforms = 0;
	err = clGetPlatformIDs(0, 0, &num_of_platforms);
	CHECK_ERRORS(err);

	// 2. 获取当前设备所有支持OpenCL的平台的信息
	cl_platform_id* platforms = new cl_platform_id[num_of_platforms];
	err = clGetPlatformIDs(num_of_platforms, platforms, 0);
	CHECK_ERRORS(err);

	cout << "Platform info: \n";
	// 3. 打印平台信息
	for (cl_uint i = 0; i < num_of_platforms; i++)
	{
    
    
		// 获取平台字符串的长度
		size_t platform_name_length = 0;
		err = clGetPlatformInfo(platforms[i], CL_PLATFORM_NAME, 0, 0, &platform_name_length);
		CHECK_ERRORS(err);

		// 获取平台字符串
		char* platform_name = new char[platform_name_length];
		err = clGetPlatformInfo(platforms[i], CL_PLATFORM_NAME, platform_name_length, platform_name, 0);
		CHECK_ERRORS(err);

		cout << "    [" << i << "] " << platform_name << endl;
	}
	return 0;
}

This is what my PC outputs:

Platform info:
    [0] NVIDIA CUDA
    [1] Intel(R) OpenCL HD Graphics

Chapter 2 Host Programming: Basic Data Structures

2.1 Basic data types

  • OpenCL is a cross-platform toolkit, so it requires a series of agreed basic data types as support.
  • The declaration of data types is in CL/cl_platform.h. These data are just redefinitions of c/c++ data types.
    Insert image description here

2.2 Obtain platform information

Examples of application scenarios:

  • There are multiple devices available, you need to specify the computing device
  • When publishing a program, specify the device according to the user's hardware platform.
  • Use cl_platform_id to correspond to the above two situations

2.2.1 Create platform structure

Each cl_platform_id corresponds to a different specific implementation (platform) of OpenCL installed on the host.
Writing a platform program is divided into two steps:

  • Allocate memory space for one or more cl_platform_id structures
  • Call clGetPlatformID to initialize these data structures. Its function signature is as follows:
clGetPlatformIDs(
	cl_uint          num_entries,
    cl_platform_id * platforms,
    cl_uint *        num_platforms);

There are three points to note about this function:

  • This function does not return cl_platform_id . It returns 0 when successful. Returning a negative number indicates that the detection failed.
  • The actual function of the function is to put cl_platform_id into the memory space pointed to by platforms
  • Save the number of available platforms using num_platforms , which reflects the upper limit of the number of platforms available.
  • num_entries indicates the upper limit of the number of platforms you want to detect.
  • platforms object memory address

scenes to be used

// 1. 获取当前设备所有支持OpenCL的平台的数量
	cl_uint num_of_platforms = 0;
	err = clGetPlatformIDs(0, 0, &num_of_platforms);
	CHECK_ERRORS(err);

	// 2. 获取当前设备所有支持OpenCL的平台的信息
	cl_platform_id* platforms = new cl_platform_id[num_of_platforms];
	err = clGetPlatformIDs(num_of_platforms, platforms, 0);
	CHECK_ERRORS(err);

clGetPlatformIDs is called twice here

  • The first call obtains the number of supported platforms num_of_platforms
  • The second call obtains the memory address of the platform, similar to handle

2.2.2 Obtain platform information

api:

clGetPlatformInfo(cl_platform_id   platform,
                  cl_platform_info param_name,
                  size_t           param_value_size,
                  void *           param_value,
                  size_t *         param_value_size_ret)
  • platform device memory address
  • param_name platform information related parameters
    Insert image description here

2.3 Access to installation equipment

After accessing the manufacturer's platform, you can access each device connected to the platform. The device complex accepts tasks and data distributed from the host. The device in the program is represented by the cl_device_id structure. This section focuses on examining two OpenCL device functions.

  • clGetDeviceIDS
  • clGetDeviceInfo

2.3.1 Create device structure

Before sending the kernel to the device, we need to create a cl_device_id structure to represent a device. This can be achieved through
clGetDeviceIDs . It will save the structure corresponding to the OpenCL device in the cl_device_id type array, and its function signature is as follows:


Guess you like

Origin blog.csdn.net/qq_30340349/article/details/131370312