| 1 |
|
|---|
| 2 | #include <civlc.h>
|
|---|
| 3 | #include <stdlib.h>
|
|---|
| 4 |
|
|---|
| 5 | typedef struct
|
|---|
| 6 | {
|
|---|
| 7 | void * arguments;
|
|---|
| 8 | int global_id;
|
|---|
| 9 | int local_id;
|
|---|
| 10 | int workgroup;
|
|---|
| 11 | } cl_kernel;
|
|---|
| 12 |
|
|---|
| 13 | typedef struct
|
|---|
| 14 | {
|
|---|
| 15 | //Variables for kernels
|
|---|
| 16 |
|
|---|
| 17 | float * input;
|
|---|
| 18 | float * output;
|
|---|
| 19 | int count;
|
|---|
| 20 |
|
|---|
| 21 | }args;
|
|---|
| 22 |
|
|---|
| 23 | cl_kernel clCreateKernel(args * argument)
|
|---|
| 24 | {
|
|---|
| 25 | cl_kernel kernel;
|
|---|
| 26 | kernel.arguments = argument;
|
|---|
| 27 |
|
|---|
| 28 | return kernel;
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | //kernel
|
|---|
| 32 | void square(int workgroup, int global_id, int local_id, float* input, float* output, int count)
|
|---|
| 33 | {
|
|---|
| 34 | //int i = get_global_id(0);
|
|---|
| 35 | int i = global_id;
|
|---|
| 36 | if (i < count)
|
|---|
| 37 | {
|
|---|
| 38 | output[i] = input[i] * input[i];
|
|---|
| 39 | //printf("output[%d] is %d\n", i, output[i]);
|
|---|
| 40 | }
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | void workfunc(size_t local, size_t global, cl_kernel param)
|
|---|
| 44 | {
|
|---|
| 45 | for(int i = local * param.workgroup; i < local * param.workgroup + local; i++)
|
|---|
| 46 | {
|
|---|
| 47 | param.local_id = i % local;
|
|---|
| 48 | param.global_id = i;
|
|---|
| 49 | square(param.workgroup, param.global_id, param.local_id, ((args*)param.arguments)->input, ((args*)param.arguments)->output, ((args*)param.arguments)->count);
|
|---|
| 50 | }
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | int clEnqueueNDRangeKernel(cl_kernel kernel, int global, int local)
|
|---|
| 54 | {
|
|---|
| 55 | $assert(global % local == 0);
|
|---|
| 56 | cl_kernel param[global/local];
|
|---|
| 57 | $proc procs[global/local];
|
|---|
| 58 | for(int i = 0; i < global/local; i++)
|
|---|
| 59 | {
|
|---|
| 60 | param[i] = kernel;
|
|---|
| 61 | param[i].workgroup = i;
|
|---|
| 62 | procs[i] = $spawn workfunc(local, global, param[i]);
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | //this part here is the new clFinish(commands);
|
|---|
| 66 | for(int i = 0; i < global/local; i++)
|
|---|
| 67 | {
|
|---|
| 68 | $wait(procs[i]);
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | return 1;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | int main(int argc, char** argv)
|
|---|
| 75 | {
|
|---|
| 76 | args * arguments;
|
|---|
| 77 | arguments = (args*)malloc(sizeof(args));
|
|---|
| 78 |
|
|---|
| 79 | int global = 8;
|
|---|
| 80 | int local = 2;
|
|---|
| 81 |
|
|---|
| 82 | float data[global]; // original data set given to device
|
|---|
| 83 |
|
|---|
| 84 | cl_kernel kernel;
|
|---|
| 85 | int err;
|
|---|
| 86 |
|
|---|
| 87 | float * input; // device memory used for the input array
|
|---|
| 88 | float * output; // device memory used for the output array
|
|---|
| 89 |
|
|---|
| 90 | unsigned int count = global;
|
|---|
| 91 | for(int i = 0; i < count; i++)
|
|---|
| 92 | {
|
|---|
| 93 | data[i] = i;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | input = (float *) malloc(sizeof(float) * count);
|
|---|
| 97 | output = (float *) malloc(sizeof(float) * count);
|
|---|
| 98 |
|
|---|
| 99 | //memcpy(input, data, sizeof(float) * count);
|
|---|
| 100 |
|
|---|
| 101 | kernel = clCreateKernel(arguments);
|
|---|
| 102 |
|
|---|
| 103 | ((args*)kernel.arguments)->input = data;
|
|---|
| 104 |
|
|---|
| 105 | ((args*)kernel.arguments)->output = output;
|
|---|
| 106 |
|
|---|
| 107 | ((args*)kernel.arguments)->count = count;
|
|---|
| 108 |
|
|---|
| 109 | err = clEnqueueNDRangeKernel(kernel, global, local);
|
|---|
| 110 |
|
|---|
| 111 | free(((args*)kernel.arguments)->input);
|
|---|
| 112 | free(((args*)kernel.arguments)->output);
|
|---|
| 113 |
|
|---|
| 114 | free(input);
|
|---|
| 115 | free(output);
|
|---|
| 116 | free(arguments);
|
|---|
| 117 |
|
|---|
| 118 | }
|
|---|