简单的OpenCL程序无法正常工作

时间:2015-03-26 11:29:27

标签: parallel-processing opencl gpu

这个程序是一个简单的并行程序,它添加了2个向量的元素。 该程序没有错误,并且编译成功,但结果不正确

#include <stdio.h>
#include <stdlib.h>
#include <iostream> 
#include <iomanip>
#include <array>
#include <fstream>
#include <sstream>
#include <string>
#include <algorithm>
#include <iterator>

#ifdef __APPLE__
#include <OpenCL/opencl.h>
#else
#include <CL/cl.h>
#include <time.h>
#endif



#define MAX_SOURCE_SIZE (0x100000)

// number of points in Both  A and B files (number of rows)
const int number_of_points = 11; 
// number of points axis in Both  A and B files (number of Columns)
const int number_of_axis = 3;       

using namespace std;

int main(int argc, char *argv[]) {
    clock_t tStart = clock();
    // Create the two input vectors
    // working variables
    int i;
    ifstream input_fileA, input_fileB;  // input files
    string line;    // transfer row from file to array
    float x;        // transfer word from file to array
    int row = 0;    // number of rows of file A,B (= array)
    int col = 0;    // number of rows of file A,B (= array)

    // working arrays
    // array contains file A data
    float arrayA[number_of_points][number_of_axis]={{0}};
    // array contains file B data
    float arrayB[number_of_points][number_of_axis]={{0}};


//  float X1[number_of_points]; // X values of file A points
    float Y1[number_of_points]; // Y values of file A points
//  float X2[number_of_points]; // X values of file B points
    float Y2[number_of_points]; // Y values of file B points
    float *X1 = (float*)malloc(sizeof(float)*number_of_points);
    float *X2 = (float*)malloc(sizeof(float)*number_of_points);

    // import input files
    input_fileA.open(argv[1]);
    input_fileB.open(argv[2]);  

    // transfer input files data to array
    // input file A to arrayA
    row = 0;
    while (getline(input_fileA, line))
    {

        istringstream streamA(line);
        col = 0;
        while(streamA >> x){
            arrayA[row][col] = x;
            col++;
        }
        row++;
    }
    // input file B to arrayB
    row = 0;
    while (getline(input_fileB, line))
    {

        istringstream streamB(line);
        col = 0;
        while(streamB >> x){
            arrayB[row][col] = x;
            col++;
        }
        row++;
    }

    // put Xs of points in X vectors and Ys of points in Y vectors
    // input file A
    for (int i = 0; i<number_of_points; i++){
        X1[i] = arrayA[i][1];
        Y1[i] = arrayA[i][2];
    }

    // input file B
    for (int i = 0; i<number_of_points; i++){
        X2[i] = arrayB[i][1];
        Y2[i] = arrayB[i][2];
    }       

//    int i;
//    const int LIST_SIZE = 50;
//    int *A = (int*)malloc(sizeof(int)*number_of_points);
//    int *B = (int*)malloc(sizeof(int)*number_of_points);
//    for(i = 0; i < number_of_points; i++) {
//        A[i] = X1[i];
//        B[i] = X2[i];
//    }

    // Load the kernel source code into the array source_str
    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 );

    // Get platform and device information
    cl_platform_id platform_id = NULL;
    cl_device_id device_id = NULL;   
    cl_uint ret_num_devices;
    cl_uint ret_num_platforms;
    cl_int ret = clGetPlatformIDs(1, &platform_id, &ret_num_platforms);
    ret = clGetDeviceIDs( platform_id, CL_DEVICE_TYPE_ALL, 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 x1_mem_obj = clCreateBuffer(context, CL_MEM_READ_ONLY, 
            number_of_points * sizeof(float), NULL, &ret);
    cl_mem x2_mem_obj = clCreateBuffer(context, CL_MEM_READ_ONLY,
            number_of_points * sizeof(float), NULL, &ret);
    cl_mem c_mem_obj = clCreateBuffer(context, CL_MEM_WRITE_ONLY, 
            number_of_points * sizeof(float), NULL, &ret);

    // Copy the lists A and B to their respective memory buffers
    ret = clEnqueueWriteBuffer(command_queue, x1_mem_obj, CL_TRUE, 0,
            number_of_points * sizeof(float), X1, 0, NULL, NULL);
    ret = clEnqueueWriteBuffer(command_queue, x2_mem_obj, CL_TRUE, 0, 
            number_of_points * sizeof(float), X2, 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 *)&x1_mem_obj);
    ret = clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *)&x2_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 = number_of_points; // Process the entire lists
    size_t local_item_size = 64; // Process in 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)*number_of_points);
    float *C = (float*)malloc(sizeof(float)*number_of_points);
    ret = clEnqueueReadBuffer(command_queue, c_mem_obj, CL_TRUE, 0, 
            number_of_points * sizeof(float), C, 0, NULL, NULL);

    // Display the result to the screen
    for(i = 0; i < number_of_points; i++)
        printf("%f + %f = %f\n", X1[i], X2[i], C[i]);

    // Clean up
    ret = clFlush(command_queue);
    ret = clFinish(command_queue);
    ret = clReleaseKernel(kernel);
    ret = clReleaseProgram(program);
    ret = clReleaseMemObject(x1_mem_obj);
    ret = clReleaseMemObject(x2_mem_obj);
    ret = clReleaseMemObject(c_mem_obj);
    ret = clReleaseCommandQueue(command_queue);
    ret = clReleaseContext(context);
    free(X1);
    free(X2);
    free(C);

printf("ALL Time taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
    return 0;
}

和内核文件

__kernel void vector_add(__global float *X1,
                         __global float *X2,
                         __global float *C) {

    // Get the index of the current element
    int i = get_global_id(0);

    // Do the operation
    C[i] = X1[i] + X2[i];

}

结果是

0.000000 + 0.000000 = 0.000000
1.000000 + 1.000000 = 0.000000
2.000000 + 2.000000 = 0.000000
3.000000 + 3.000000 = 0.000000
4.000000 + 4.000000 = 0.000000
5.000000 + 5.000000 = 0.000000
6.000000 + 6.000000 = 0.000000
7.000000 + 7.000000 = 0.000000
8.000000 + 8.000000 = 0.000000
9.000000 + 9.000000 = 0.000000
1.000000 + 1.000000 = 0.000000
ALL Time taken: 0.07s

1 个答案:

答案 0 :(得分:1)

您已经承诺了OpenCL编程的一个主要罪行,因为您没有检查任何OpenCL API调用的错误代码!您应始终检查每个 OpenCL API调用的返回码。如果你这样做,它会很快指出你的问题。

问题出在你的内核入队调用中。如果您检查错误代码,则会看到您返回-54,这与CL_INVALID_WORK_GROUP_SIZE相对应。具体而言,内核调用要求工作组大小(本地大小)完全划分全局大小。您要求的工作组大小为64,全局大小为11,这不符合此要求。

您还可以将NULL作为工作组大小参数传递,OpenCL实现将选择一个肯定适合您的工作组大小。