clEnqueueNDRangeKernel
cl_int clEnqueueNDRangeKernel ( cl_command_queue command_queue, //命令队列 cl_kernel kernel, //核 cl_uint work_dim, //数据的维度 const size_t *global_work_offset, // 各维度上的全局ID偏移量 const size_t *global_work_size, //各维度上的工作项数量 const size_t *local_work_size, // 各维度上一个工作组中工作项的数量 cl_uint num_events_in_wait_list, const cl_event *event_wait_list, cl_event *event )
clEnqueueTask
cl_int clEnqueueTask ( cl_command_queue command_queue, //命令队列 cl_kernel kernel, //核 cl_uint num_events_in_wait_list, const cl_event *event_wait_list, cl_event *event )
clEnqueueTask 和 clEnqueueNDRangeKernel的功能都是将核执行命令加入命令队列。而clEnqueueNDRangeKernel可以更好的划分数据,充分利用设备的资源
工作组和工作项的特点:
1、工作组中的工作项可以访问局部内存的同一块地址
2、工作组中的工作项可以进行同步
工作项相关函数
工作组相关函数
demo:
// 核函数 __kernel void id_check(__global float *output) { /* Access work-item/work-group information */ size_t global_id_0 = get_global_id(0); size_t global_id_1 = get_global_id(1); size_t global_size_0 = get_global_size(0); size_t offset_0 = get_global_offset(0); size_t offset_1 = get_global_offset(1); size_t local_id_0 = get_local_id(0); size_t local_id_1 = get_local_id(1); /* Determine array index */ int index_0 = global_id_0 - offset_0; int index_1 = global_id_1 - offset_1; int index = index_1 * global_size_0 + index_0; /* Set float data */ float f = global_id_0 * 10.0f + global_id_1 * 1.0f; f += local_id_0 * 0.1f + local_id_1 * 0.01f; output[index] = f; }
#define _CRT_SECURE_NO_WARNINGS #define PROGRAM_FILE "id_check.cl" #define KERNEL_FUNC "id_check" #include <stdio.h> #include <stdlib.h> #include <string.h> #ifdef MAC #include <OpenCL/cl.h> #else #include <CL/cl.h> #endif /* Find a GPU or CPU associated with the first available platform */ // 获取平台 获取设备 cl_device_id create_device() { cl_platform_id platform; cl_device_id dev; int err; /* Identify a platform */ err = clGetPlatformIDs(1, &platform, NULL); if(err < 0) { perror("Couldn't identify a platform"); exit(1); } /* Access a device */ err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &dev, NULL); if(err == CL_DEVICE_NOT_FOUND) { err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_CPU, 1, &dev, NULL); } if(err < 0) { perror("Couldn't access any devices"); exit(1); } return dev; } /* Create program from a file and compile it */ // 创建 cl_program 编译cl_program cl_program build_program(cl_context ctx, cl_device_id dev, const char* filename) { cl_program program; FILE *program_handle; char *program_buffer, *program_log; size_t program_size, log_size; int err; /* Read program file and place content into buffer */ program_handle = fopen(filename, "r"); if(program_handle == NULL) { perror("Couldn't find the program file"); exit(1); } fseek(program_handle, 0, SEEK_END); program_size = ftell(program_handle); rewind(program_handle); program_buffer = (char*)malloc(program_size + 1); program_buffer[program_size] = '