| 1 | //Forget the program
|
|---|
| 2 |
|
|---|
| 3 | #include "openCLshared.cvl"
|
|---|
| 4 | #include <stdio.h>
|
|---|
| 5 | #include <stdlib.h>
|
|---|
| 6 | #include <string.h>
|
|---|
| 7 |
|
|---|
| 8 | $input int NUM_DEVICES;
|
|---|
| 9 | $input int MAX_NUM_DEVICES;
|
|---|
| 10 | $assume 0 < NUM_DEVICES && NUM_DEVICES < MAX_NUM_DEVICES;
|
|---|
| 11 |
|
|---|
| 12 | $input int DATA_SIZE;
|
|---|
| 13 | $input int MAX_DATA_SIZE;
|
|---|
| 14 | $assume 0 < DATA_SIZE && DATA_SIZE < MAX_NUM_DEVICES;
|
|---|
| 15 |
|
|---|
| 16 | $input int LOCAL;
|
|---|
| 17 | $input int MAX_LOCAL;
|
|---|
| 18 | $assume 0 < LOCAL && LOCAL < MAX_LOCAL;
|
|---|
| 19 | //Didn't initialize variables here
|
|---|
| 20 | typedef struct
|
|---|
| 21 | {
|
|---|
| 22 | //Variables for kernels
|
|---|
| 23 |
|
|---|
| 24 | float * input;
|
|---|
| 25 | float * output;
|
|---|
| 26 | int count;
|
|---|
| 27 |
|
|---|
| 28 | }args;
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 | cl_kernel clCreateKernel(args * argument)
|
|---|
| 32 | {
|
|---|
| 33 | cl_kernel kernel;
|
|---|
| 34 | kernel.arguments = argument;
|
|---|
| 35 |
|
|---|
| 36 | return kernel;
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | int clEnqueueNDRangeKernel(cl_command_queue commands, cl_kernel kernel, int global, int local)
|
|---|
| 40 | {
|
|---|
| 41 | cl_kernel param[global/local];
|
|---|
| 42 | $proc procs[global/local];
|
|---|
| 43 | for(int i = 0; i < global/local; i++)
|
|---|
| 44 | {
|
|---|
| 45 | param[i].workgroup = i;
|
|---|
| 46 | param[i] = kernel;
|
|---|
| 47 | //procs[i] = $spawn workfunc(local, global, param[i]);
|
|---|
| 48 | }
|
|---|
| 49 | /*
|
|---|
| 50 | for(int i = 0; i < global/local; i++)
|
|---|
| 51 | {
|
|---|
| 52 | $wait(procs[i]);
|
|---|
| 53 | }
|
|---|
| 54 | */
|
|---|
| 55 | return CL_SUCCESS;
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | //kernel
|
|---|
| 59 | void square(int workgroup, int global_id, int local_id, float* input, float* output, int count)
|
|---|
| 60 | {
|
|---|
| 61 | //int i = get_global_id(0);
|
|---|
| 62 | int i = global_id;
|
|---|
| 63 | if (i < count)
|
|---|
| 64 | {
|
|---|
| 65 | output[i] = input[i] * input[i];
|
|---|
| 66 | //printf("output[%d] is %d\n", i, output[i]);
|
|---|
| 67 | }
|
|---|
| 68 | }
|
|---|
| 69 | /*
|
|---|
| 70 | void workfunc(size_t local, size_t global, kernel param)
|
|---|
| 71 | {
|
|---|
| 72 | for(int i = local * param.workgroup; i < local * param.workgroup + local; i++)
|
|---|
| 73 | {
|
|---|
| 74 | param.local_id = i % local;
|
|---|
| 75 | param.global_id = i;
|
|---|
| 76 | //printf("My workgroup id is %d, my global id is %d, my local id is %d\n", param.workgroup, param.global_id, param.local_id);
|
|---|
| 77 | square(param.workgroup, param.global_id, param.local_id, param.input, param.output, param.count);
|
|---|
| 78 | }
|
|---|
| 79 | }
|
|---|
| 80 | */
|
|---|
| 81 | int main(int argc, char** argv)
|
|---|
| 82 | {
|
|---|
| 83 | args * arguments;
|
|---|
| 84 | arguments = (args*)malloc(sizeof(args));
|
|---|
| 85 |
|
|---|
| 86 | float data[DATA_SIZE]; // original data set given to device
|
|---|
| 87 | float results[DATA_SIZE]; // results returned from device
|
|---|
| 88 | unsigned int correct; // number of correct results returned
|
|---|
| 89 |
|
|---|
| 90 | size_t global; // global domain size for our calculation
|
|---|
| 91 | size_t local; // local domain size for our calculation
|
|---|
| 92 |
|
|---|
| 93 | cl_device_id device_id; // compute device id
|
|---|
| 94 | cl_context context; // compute context
|
|---|
| 95 | cl_command_queue commands; // compute command queue
|
|---|
| 96 | //cl_program program; // compute program
|
|---|
| 97 | cl_kernel kernel; // compute kernel
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 | float * input; // device memory used for the input array
|
|---|
| 101 | float * output; // device memory used for the output array
|
|---|
| 102 |
|
|---|
| 103 | unsigned int count = DATA_SIZE;
|
|---|
| 104 | for(int i = 0; i < count; i++)
|
|---|
| 105 | {
|
|---|
| 106 | data[i] = i;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | int err = clGetDeviceIDs(1, &device_id);
|
|---|
| 110 |
|
|---|
| 111 | //ignore clCreateContext for now, until we get an example that uses multiple ones
|
|---|
| 112 |
|
|---|
| 113 | //clCreateCommandQueue, could use context later
|
|---|
| 114 | commands = clCreateCommandQueue(device_id);
|
|---|
| 115 |
|
|---|
| 116 | //clCreateProgram is far different from the real version, this just stores parameters for the kernel
|
|---|
| 117 | //In order to make this clear, it is clCreateProgram and not something like clCreateProgramFromSource, which actually exists in openCL code
|
|---|
| 118 | //program = clCreateProgram(arguments);
|
|---|
| 119 |
|
|---|
| 120 | kernel = clCreateKernel(arguments);
|
|---|
| 121 | //printf("%s", kernel);
|
|---|
| 122 |
|
|---|
| 123 | //comes from clCreateBuffer
|
|---|
| 124 | input = (float *) malloc(sizeof(float) * count);
|
|---|
| 125 | output = (float *) malloc(sizeof(float) * count);
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 | memcpy(input, data, sizeof(float) * count);
|
|---|
| 130 | //clEnqueueWriteBuffer
|
|---|
| 131 |
|
|---|
| 132 | /*
|
|---|
| 133 | err = 0;
|
|---|
| 134 | err = clSetKernelArg(kernel, 0, sizeof(cl_mem), &input);
|
|---|
| 135 | err = clSetKernelArg(kernel, 1, sizeof(cl_mem), &output);
|
|---|
| 136 | err = clSetKernelArg(kernel, 2, sizeof(unsigned int), &count);
|
|---|
| 137 | */
|
|---|
| 138 | //kernel.arguments.input = input;
|
|---|
| 139 |
|
|---|
| 140 | //kernel.arguments->input;
|
|---|
| 141 |
|
|---|
| 142 | ((args*)kernel.arguments)->input = (float*)malloc(sizeof(float) * count);
|
|---|
| 143 | memcpy(((args *)kernel.arguments)->input, input, sizeof(float) * count);
|
|---|
| 144 |
|
|---|
| 145 | ((args*)kernel.arguments)->output = (float*)malloc(sizeof(float) * count);
|
|---|
| 146 | memcpy(((args*)kernel.arguments)->output, output, sizeof(float));
|
|---|
| 147 |
|
|---|
| 148 | ((args*)kernel.arguments)->count = count;
|
|---|
| 149 | //no malloc needed for non pointers
|
|---|
| 150 |
|
|---|
| 151 | //clGetKernelWorkGroupInfo
|
|---|
| 152 | local = LOCAL;
|
|---|
| 153 |
|
|---|
| 154 | global = count;
|
|---|
| 155 | /*
|
|---|
| 156 | commands holds the "order" of devices
|
|---|
| 157 | kernel holds program, which holds variables
|
|---|
| 158 | offset not implemented
|
|---|
| 159 | */
|
|---|
| 160 | err = clEnqueueNDRangeKernel(commands, kernel, global, local);
|
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 | free(((args*)kernel.arguments)->input);
|
|---|
| 165 | free(((args*)kernel.arguments)->output);
|
|---|
| 166 |
|
|---|
| 167 | free(input);
|
|---|
| 168 | free(output);
|
|---|
| 169 | free(arguments);
|
|---|
| 170 | return 0;
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|