| 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 | /*
|
|---|
| 12 | void square(float* input, float* output, const unsigned int count)
|
|---|
| 13 | {
|
|---|
| 14 | int i = get_global_id(0);
|
|---|
| 15 | if (i < count)
|
|---|
| 16 | {
|
|---|
| 17 | output[i] = input[i] * input[i];
|
|---|
| 18 | }
|
|---|
| 19 | }
|
|---|
| 20 | */
|
|---|
| 21 | void main()
|
|---|
| 22 | {
|
|---|
| 23 | int err;
|
|---|
| 24 | int num_entries = 3;
|
|---|
| 25 |
|
|---|
| 26 | cl_device_id device_id;
|
|---|
| 27 | cl_context context;
|
|---|
| 28 | cl_command_queue commands;
|
|---|
| 29 | cl_program program;
|
|---|
| 30 | cl_kernel kernel;
|
|---|
| 31 |
|
|---|
| 32 | //assumes you have hardware that works, does not check that part
|
|---|
| 33 | //fills the device_id with device ids, returns CL_SUCCESS if it works
|
|---|
| 34 | err = clGetDeviceIDs(NULL,
|
|---|
| 35 | CL_DEVICE_TYPE_GPU,
|
|---|
| 36 | num_entries,
|
|---|
| 37 | &device_id,
|
|---|
| 38 | NULL);
|
|---|
| 39 | printf("devices is %d", device_id[0]);
|
|---|
| 40 | $assert (err == CL_SUCCESS);
|
|---|
| 41 |
|
|---|
| 42 | //Returns a context, which has device information
|
|---|
| 43 | context = clCreateContext(0, 1, &device_id, NULL, NULL, &err);
|
|---|
| 44 | $assert (context != NULL);
|
|---|
| 45 |
|
|---|
| 46 | commands = clCreateCommandQueue(context, device_id, 0, &err);
|
|---|
| 47 | $assert (commands != NULL);
|
|---|
| 48 |
|
|---|
| 49 | program = clCreateProgramWithSource(context, 1, (const char **) & KernelSource, NULL, &err);
|
|---|
| 50 | $assert (program != NULL);
|
|---|
| 51 |
|
|---|
| 52 | err = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
|
|---|
| 53 | }
|
|---|