OpenCL 程序仅适用于项目大小的倍数



我是openCL程序的新手,这是我在执行简单的向量加法时面临的问题。

我有以下内核代码

#include <CL/cl.hpp>
#include<iostream>
#include <stdio.h>
#include <stdlib.h>
#define MAX_SOURCE_SIZE (0x100000)
int main() {
__kernel void vector_add(__global const int *A, __global const int *B, __global int *C) {
    int i = get_global_id(0);
    C[i] = A[i] + B[i];
}

我已经在我的系统上集成了 GPU 和 amd gpu。我正在尝试在我的英特尔 GPU 上执行矢量加法,并且我已经为此安装了英特尔 opencl 驱动程序(带有高清显卡的 i7 第三代处理器(。

我有以下 openCL 代码

std::vector<cl::Platform> platforms;
    cl::Platform::get(&platforms);
    std::cout << "Total platforms including cpu: " << platforms.size() << std::endl;
    if (platforms.size() == 0) {
        std::cout << " No platforms found. Check OpenCL installation!n";
        exit(1);
    }
    int i;
    const int LIST_SIZE = 50;
    int *A = (int*)malloc(sizeof(int)*LIST_SIZE);
    int *B = (int*)malloc(sizeof(int)*LIST_SIZE);
    for(i = 0; i < LIST_SIZE; i++) {
        A[i] = i;
        B[i] = LIST_SIZE - i;
    }
    FILE *fp;
    char *source_str;
    size_t source_size;
    fp = fopen("vector_add_kernel.cl", "r");
    if (!fp) {
        fprintf(stderr, "Failed to load kernel.n");
        exit(1);
    }
    source_str = (char*)malloc(MAX_SOURCE_SIZE);
    source_size = fread( source_str, 1, MAX_SOURCE_SIZE, fp);
    fclose( fp );
    //std::cout<<source_str<<std::endl;
    // Get platform and device information
    cl_platform_id* platforms1 = NULL;
    cl_device_id device_id = NULL;   
    cl_uint ret_num_devices;
    cl_uint ret_num_platforms;
    cl_int ret = clGetPlatformIDs(1, platforms1, &ret_num_platforms);
    platforms1= (cl_platform_id*) malloc(sizeof(cl_platform_id) * ret_num_platforms);
    clGetPlatformIDs(ret_num_platforms, platforms1, NULL);
    /*
    *   Platform 0: Intel Graphics
    *   Platform 1 :  AMD Graphics
    */  
    //CHANGE THE PLATFORM ACCORDING TO YOUR SYSTEM!!!!
    ret = clGetDeviceIDs( platforms1[0], CL_DEVICE_TYPE_GPU, 1, 
            &device_id, &ret_num_devices);
    // Create an OpenCL context
    cl_context context = clCreateContext( NULL, 1, &device_id, NULL, NULL, &ret);
    // Create a command queue
    cl_command_queue command_queue = clCreateCommandQueue(context, device_id, 0, &ret);
    // Create memory buffers on the device for each vector 
    cl_mem a_mem_obj = clCreateBuffer(context, CL_MEM_READ_ONLY, 
            LIST_SIZE * sizeof(int), NULL, &ret);
    cl_mem b_mem_obj = clCreateBuffer(context, CL_MEM_READ_ONLY,
            LIST_SIZE * sizeof(int), NULL, &ret);
    cl_mem c_mem_obj = clCreateBuffer(context, CL_MEM_WRITE_ONLY, 
            LIST_SIZE * sizeof(int), NULL, &ret);
    // Copy the lists A and B to their respective memory buffers
    ret = clEnqueueWriteBuffer(command_queue, a_mem_obj, CL_TRUE, 0,
            LIST_SIZE * sizeof(int), A, 0, NULL, NULL);
    ret = clEnqueueWriteBuffer(command_queue, b_mem_obj, CL_TRUE, 0, 
            LIST_SIZE * sizeof(int), B, 0, NULL, NULL);
    // Create a program from the kernel source
    cl_program program = clCreateProgramWithSource(context, 1, 
            (const char **)&source_str, (const size_t *)&source_size, &ret);
    // Build the program
    ret = clBuildProgram(program, 1, &device_id, NULL, NULL, NULL);
    // Create the OpenCL kernel
    cl_kernel kernel = clCreateKernel(program, "vector_add", &ret);
    // Set the arguments of the kernel
    ret = clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *)&a_mem_obj);
    ret = clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *)&b_mem_obj);
    ret = clSetKernelArg(kernel, 2, sizeof(cl_mem), (void *)&c_mem_obj);
    // Execute the OpenCL kernel on the list
    size_t global_item_size = LIST_SIZE; // Process the entire lists
    size_t local_item_size = 16; // Divide work items into groups of 64
    ret = clEnqueueNDRangeKernel(command_queue, kernel, 1, NULL, 
            &global_item_size, &local_item_size, 0, NULL, NULL);
    // Read the memory buffer C on the device to the local variable C
    int *C = (int*)malloc(sizeof(int)*LIST_SIZE);
    ret = clEnqueueReadBuffer(command_queue, c_mem_obj, CL_TRUE, 0, 
            LIST_SIZE * sizeof(int), C, 0, NULL, NULL);
    // Display the result to the screen
    for(i = 0; i < LIST_SIZE; i++)
        printf("%d + %d = %dn", A[i], B[i], C[i]);
    //FREE
   return 0;
}

如果 LISTSIZE 为 50,则它只打印到 48,即 16*3。它只打印 LISTSIZE 的倍数,我无法弄清楚为什么?

OpenCL 内核仅针对本地线程块大小的倍数(本地范围,在您的代码local_item_size中(执行,该大小不应小于 32,并且必须是 2 的倍数(因此它可以是 (32, 64, 128, 256, ...(。如果将其设置为 16,则 GPU 的一半随时将处于空闲状态。 global_item_size必须是 local_item_size 的倍数。至少需要 32 个数据项才能使内核运行,并且需要更多数据项才能产生良好的性能。

也是部分

#include <CL/cl.hpp>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#define MAX_SOURCE_SIZE (0x100000)
int main() {

不是 OpenCL C 代码,不属于 .cl 源文件。如果不是太长,可以直接将 OpenCL C 代码作为原始字符串写入.cpp文件中:

const string kernel_code = R"(
__kernel void vector_add(__global const int *A, __global const int *B, __global int *C) {
    int i = get_global_id(0);
    C[i] = A[i] + B[i];
}
)";
char* source_str = kernel_code.c_str();

最新更新