OpenCL heterogeneous Parallel Computing Programming notes (1): Platform, device and context __opencl

Source: Internet
Author: User
Tags nvidia support intel core i5

OpenCL (full name open Computing Language, open Computing language) is the first open, free standard for the common purpose of heterogeneous systems, and also a unified programming environment, which makes it easy for software developers to provide High-performance computing servers, desktop computing systems, Handheld devices write efficient and lightweight code and are widely used in multiple core processors (CPUs), graphics processors (GPU), cell type architectures, and other parallel processors such as digital signal processor (DSP) [1].

It should be noted that unlike other Third-party libraries, OpenCL only provides a specification of an open API, defines only the interfaces, but does not implement them, and the implementation of each interface is done by the processor manufacturer itself. So, while vendors including Intel, AMD, and Nvidia support OpenCL, the approach is different, making it possible to use a specific SDK when using OpenCL on a branded processor. At the same time there will be a slight difference in the characteristics (but the basic will not have much impact). Next, a brief introduction of OPENCL based on the heterogeneous programming approach, before OPENCL programming, it is best to understand the OpenCL architecture model, the specific information can be referred to: from the beginning to learn OPENCL development structure, this will not repeat. (Instance environment: WINDOWS10, Visual Studio 2015, OpenCL 1.1, Intel Core i5 3437U, Nvidia GeForce 840M)


First, Access platform (Platform):

Different vendors for OpenCL specific implementation of different, a manufacturer for the implementation of OpenCL formed a platform. Processors from different vendors can only run on different platforms, in other words, only the processors that implement the platform vendor are running on a single platform. For example, Intel, AMD, and Nvidia all have their own implementations of OPENCL, which can use Intel's CPUs and GPU simultaneously on Intel's platforms, but cannot use AMD and NVIDIA processors , as well as the inability to use Intel and Nvidia devices on AMD platforms. In OpenCL programming, you need to first determine which platform you choose.

The query platform ID can be used:

Clgetplatformids (Cl_uint num_entries, cl_platform_id*platforms, Cl_uint *num_platforms)

The first parameter of the function is the number of platform IDs that platforms can save;
The second parameter is a pointer that is used to save the found platform;
The third parameter holds the number of platforms that are queried.
When the return value is 0 o'clock, the flag program executes, not 0 o'clock, the error code.

You can usually use this function two times to get all the platform IDs:

CL_INT ret;     Used to save the function return value
cl_uint num_platform; Use to save platform number
cl_platform_id *platform//For saving platform ID

/////////////////////////////////////////////////////////
//Get the number
of functions ret = clgetplatformids (null, NULL, &num_platform);
//For platform allocation space
platform = New Cl_platform_id[num_platform];

Get all platforms
ret = Clgetplatformids (num_platform, Platform, NULL);

Query platform information can be used:

Cl_int Clgetdeviceids (cl_platform_id platform,
                  &NBSP ;                   Cl_platform_info Param_name,
      &NBSP ;                               size_t Param_valu E_size,
                              &NBSP ;       void *param_value,
                      & nbsp               size_t *param_value_size_ret)

The first parameter of the function is the platform ID obtained before;
The second parameter is an enumeration body that identifies the information to be queried;
The third parameter is a pointer, which is used to store the queried information;
The fourth parameter represents the size of the memory that the pointer refers to;
The fifth parameter holds the size of the information data that is queried.

Similarly, you typically use this function two times to get platform information:

CL_INT ret;         //For saving function return value
size_t ext_size;     //For saving platform information size
char *ext_data;    //For saving platform information
cl_platform_info platform_info = cl_platform_name;   Set the information you want to query

//////////////////////////////////////////////////////////////////////////
//Get platform information size
ret = Clgetplatforminfo (Platform[order], platform_info, 0, NULL, &ext_size);
//For Ext_data allocation space
Ext_data = New Char[ext_size];

Get platform information
ret = Clgetplatforminfo (Platform[order], Platform_info, Ext_size, Ext_data, NULL);

Print platform Information
std::cout << ext_data << Std::endl;


Ii. Acquisition Equipment (Device):

A device is a heterogeneous computing unit that runs on a platform and typically corresponds to a processor on the same platform. The method to get the device ID and information is similar to the Access platform for the function:

Cl_int Clgetdeviceids (cl_platform_id platform,
Cl_device_type Device_type,
Cl_uint Num_entries,
cl_device_id *devices,
Cl_uint *num_devices)

The first parameter identifies the platform to query, the second parameter holds the type of the query to the device, and the remaining three parameters have the same meaning as clgetplatformids.

Cl_int Clgetdeviceinfo (cl_device_id device,
Cl_device_info Param_name,
size_t Param_value_size,
void *param_value,
size_t *param_value_size_ret)

The first parameter identifies the device to query, and the rest of the parameters have the same meaning as clgetplatforminfo.

The use of the device query function and platform, the general through two times to obtain the number of equipment and the corresponding information, specific reference to obtain platform-related functions of the use of.


Iii. Establishing context:

        contexts (context) is a link that connects devices of different architectures on the same platform, and devices in the same context can communicate, read and write, and share memory. When the program is running, use context to manage objects such as command queues, memory, kernels, and so on. The context can be established using:

Cl_context Clcreatecontext (cl_context_properties *properties,
              & nbsp                       Cl_uint num_devices,
                                      Const CL_ device_id *devices,
                                      void *pfn_notify (const char *errinfo, const void *private_info, size_t cb, void *user_ Data),
                                      void *user_data,
                    &NBS P                 cl_int *errcode_ret)

The first parameter of the function points to a list, which indicates the property of the context being established and the corresponding value;
The second parameter indicates the number of devices in the context;
The third parameter is a pointer to the device ID that indicates the device that created the context;
The fourth parameter is a callback function that reports errors occurring in the context of the build;
The fifth parameter is used to return the error code, which returns 0 when the context is successfully established, i.e. cl_success.
If the context is established, the function returns a non-0 context.

In actual use, the context is based on the previously acquired device ID:

CL_INT ret;     //For saving function return value
cl_context_properties context_props[] 
= {cl_context_platform, (cl_context_properties) Platform[1], 0};;   Set Context Property

//////////////////////////////////////////////////////////////////////////
//create context
Cl_ Context context = Clcreatecontext (Context_props, num_device, devices, NULL, NULL, &ret);

After the context is established, you can then build the command queue, set the kernel, and so on. Our OpenCL programming Long March is also the first step to complete.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.