0

I wrote a kernel code which is compiled successfully. Now I start writing the host code. But in the host when try to create memory objects (create buffers for the inputs and output), I got confused about what are my input are and how to specify the size of the input and output arrays. I try the following host code.

cl_mem dev_X_train = clCreateBuffer(context, CL_MEM_READ_ONLY, sizeof(int) * [4344][20], NULL, NULL);// size
cl_mem dev_Y_train = clCreateBuffer(context, CL_MEM_READ_ONLY, sizeof(int) * [4344], NULL, NULL);// size 

Kernel code :

    inline float distance(__global int* restrict array_point_A, __global int* restrict array_point_B) {
    float sum = 0.0;
    float  w[20] = { 0.0847282, 0.0408621, 0.105036, 0.0619821, 0.0595455, 0.0416739, 0.0181147, 0.00592921,
     0.040049, 0.0766054, 0.0441091, 0.0376111, 0.0124285, 0.0733558, 0.0587338, 0.0303001, 0.0579207, 0.0449221,
          0.0530462, 0.0530462 };
    for (int i = 0; i < 20; ++i) {
        float a = array_point_A[i] - array_point_B[i];
        float wieghted_distance = w[i] * (a * a);
        sum += wieghted_distance;

    }
    return sqrt(sum);
}
__kernel void classifier(__global int * restrict X_train,__global int * restrict Y_train,__global int * restrict data_point, int k)
{
     
    float array_dist[4344] = {};
    int index_arr[4344] = {};
    for (int i = 0; i < 4344; ++i)
    { 
       array_dist[i] = distance(X_train,data_point);
       index_arr[i] = i;
        }
     .......................
..............................
 int  class_label = min_index; } 

1 Answers1

0
int X_train[4344][20];
int Y_train[4344];
int data_point[10000];
int k;

cl_mem dev_X_train = clCreateBuffer(context, CL_MEM_READ_ONLY, sizeof(int) * 4344*20, NULL, NULL);// size
cl_mem dev_Y_train = clCreateBuffer(context, CL_MEM_READ_ONLY, sizeof(int) * 4344, NULL, NULL);// size 
cl_mem dev_data_point = clCreateBuffer(context, CL_MEM_READ_ONLY, sizeof(int) * 10000, NULL, NULL);// size 

clSetKernelArg(kernel, 0, sizeof(cl_mem), &dev_X_train);
clSetKernelArg(kernel, 1, sizeof(cl_mem), &dev_Y_train);
clSetKernelArg(kernel, 2, sizeof(cl_mem), &dev_data_point);
clSetKernelArg(kernel, 3, sizeof(int), &k);


clEnqueueWriteBuffer(cmdqId, dev_X_train, 1, 0,4344*20*sizeof(int), &X_train, 0, NULL, NULL);
clEnqueueWriteBuffer(cmdqId, dev_Y_train, 1, 0,4344*sizeof(int), &Y_train, 0, NULL, NULL);
clEnqueueWriteBuffer(cmdqId, dev_data_point, 1, 0,10000*sizeof(int), &data_point, 0, NULL, NULL);

You need to initialize the data on the host-side first and send it to the device using clCreateBuffer with host pointer and write to device using clEnqueueReadBuffer. Later using clSetKernelArg map mem pointers to kernel arguments.

Note: I didn't get the size of data_point in your code, so assuming a random value.

For Reading output:

int output[10000];
cl_mem dev_output = clCreateBuffer(context, CL_MEM_WRITE_ONLY, sizeof(int) * 10000, NULL, NULL);// size 
clSetKernelArg(kernel, 4, sizeof(cl_mem), &dev_output);

//clEnqueueNDRangeKernel()
clEnqueueReadBuffer(cmdqId, dev_output, 1, 0, 10000*sizeof(float), output, 0, NULL, NULL);

In your kernel point the last argument to your output and write data to that. Finally read using clEnqueueReadBuffer().

harry
  • 970
  • 6
  • 25
  • Thank you very much for your reply. But what about the output ? what I understand is that also I need to allocate memory for output – user16767585 Aug 31 '21 at 10:06
  • You said "You need to `initialize` the data on the host-side first " But you just identify the data without initialization. Right ? – user16767585 Aug 31 '21 at 10:13
  • yes, you need to initialize the data. since I don't the data, I left it to you. – harry Aug 31 '21 at 10:18
  • I've updated the answer for reading output. – harry Aug 31 '21 at 10:19
  • Actually the output of my kernel is `int class_label = min_index` where `min_index` is either `0` or `1`. So it is not an array. . – user16767585 Aug 31 '21 at 10:58
  • you can change the output to an array of 1 element and use the above code. – harry Aug 31 '21 at 11:05
  • I have a question. You said "You need to `initialize` the data on the host-side first". Actually the data must be read from .csv file. I need you help in this part – user16767585 Sep 01 '21 at 12:17
  • https://stackoverflow.com/questions/12911299/read-csv-file-in-c – harry Sep 01 '21 at 14:18
  • thank you for reply. But I need to know how to read .csv file in OpenCl not in C. Also, where to put the code of reading data from file (in the main or before main ) – user16767585 Sep 01 '21 at 14:29
  • please check my question here : [https://stackoverflow.com/questions/69014171/how-to-read-a-csv-file-in-opencl] – user16767585 Sep 01 '21 at 14:42