| 1 | #include <civlc.h>
|
|---|
| 2 | #include <stdio.h>
|
|---|
| 3 | #include "setup/openCL.cvl"
|
|---|
| 4 |
|
|---|
| 5 | //this is the kernel that gets loaded into the program
|
|---|
| 6 | // Simple compute kernel which computes the square of an input array
|
|---|
| 7 | //CIVL can't turn a string into a function at this time
|
|---|
| 8 |
|
|---|
| 9 | //const char *KernelSource = "__kernel void square( __global float* input, __global float* output, const unsigned int count){ int i = get_global_id(0); if(i < count) output[i] = input[i] * input[i];}";
|
|---|
| 10 |
|
|---|
| 11 | // Simple compute kernel which computes the square of an input array
|
|---|
| 12 | //
|
|---|
| 13 | const char *KernelSource = "\n" \
|
|---|
| 14 | "__kernel void square( \n" \
|
|---|
| 15 | " __global float* input, \n" \
|
|---|
| 16 | " __global float* output, \n" \
|
|---|
| 17 | " const unsigned int count) \n" \
|
|---|
| 18 | "{ \n" \
|
|---|
| 19 | " int i = get_global_id(0); \n" \
|
|---|
| 20 | " if(i < count) \n" \
|
|---|
| 21 | " output[i] = input[i] * input[i]; \n" \
|
|---|
| 22 | "} \n" \
|
|---|
| 23 | "\n";
|
|---|
| 24 | /*
|
|---|
| 25 | void square(float* input, float* output, int count)
|
|---|
| 26 | {
|
|---|
| 27 | int i = get_global_id(0);
|
|---|
| 28 | if(i < count)
|
|---|
| 29 | {
|
|---|
| 30 | output[i] = input[i] * input[i];
|
|---|
| 31 | }
|
|---|
| 32 | }
|
|---|
| 33 | */
|
|---|
| 34 | /*
|
|---|
| 35 | void square(float* input, float* output, const unsigned int count)
|
|---|
| 36 | {
|
|---|
| 37 | int i = get_global_id(0);
|
|---|
| 38 | if (i < count)
|
|---|
| 39 | {
|
|---|
| 40 | output[i] = input[i] * input[i];
|
|---|
| 41 | }
|
|---|
| 42 | }
|
|---|
| 43 | */
|
|---|
| 44 | void main()
|
|---|
| 45 | {
|
|---|
| 46 | int err;
|
|---|
| 47 | int num_entries = 1;
|
|---|
| 48 |
|
|---|
| 49 | cl_device_id device_id;
|
|---|
| 50 | cl_context context;
|
|---|
| 51 | cl_command_queue commands;
|
|---|
| 52 | cl_program program;
|
|---|
| 53 | cl_kernel kernel;
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 | //assumes you have hardware that works, does not check that part
|
|---|
| 57 | //fills the device_id with device ids, returns CL_SUCCESS if it works
|
|---|
| 58 |
|
|---|
| 59 | err = clGetDeviceIDs(NULL,
|
|---|
| 60 | CL_DEVICE_TYPE_GPU,
|
|---|
| 61 | num_entries,
|
|---|
| 62 | &device_id,
|
|---|
| 63 | NULL);
|
|---|
| 64 | //for non arrays, print using device_id[0], otherwise use *device_id[0];
|
|---|
| 65 | printf("devices is %d", device_id[0]);
|
|---|
| 66 | $assert (err == CL_SUCCESS);
|
|---|
| 67 |
|
|---|
| 68 | //if not using an array, use &device_id
|
|---|
| 69 | //Returns a context, which has device information
|
|---|
| 70 | context = clCreateContext(0, 1, &device_id, NULL, NULL, &err);
|
|---|
| 71 | $assert (context != NULL);
|
|---|
| 72 |
|
|---|
| 73 | commands = clCreateCommandQueue(context, device_id, 0, &err);
|
|---|
| 74 | $assert (commands != NULL);
|
|---|
| 75 | //if not using an array, use device_id, no []
|
|---|
| 76 | program = clCreateProgramWithSource(context, 1, (const char **) & KernelSource, NULL, &err);
|
|---|
| 77 | $assert (program != NULL);
|
|---|
| 78 |
|
|---|
| 79 | err = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
|
|---|
| 80 | $assert (err == CL_SUCCESS);
|
|---|
| 81 |
|
|---|
| 82 | kernel = clCreateKernel(program, "square", &err);
|
|---|
| 83 | $assert (kernel != NULL || err == CL_SUCCESS);
|
|---|
| 84 | }
|
|---|