| 1 | //Forget the program
|
|---|
| 2 |
|
|---|
| 3 | #include "openCLshared.cvl"
|
|---|
| 4 | #include <stdio.h>
|
|---|
| 5 | #include <stdlib.h>
|
|---|
| 6 | #include <string.h>
|
|---|
| 7 | #include <civlc.h>
|
|---|
| 8 |
|
|---|
| 9 | $input int NUM_DEVICES;
|
|---|
| 10 | $input int MAX_NUM_DEVICES;
|
|---|
| 11 | $assume 0 < NUM_DEVICES && NUM_DEVICES < MAX_NUM_DEVICES;
|
|---|
| 12 |
|
|---|
| 13 | $input int DATA_SIZE;
|
|---|
| 14 | $input int MAX_DATA_SIZE;
|
|---|
| 15 | $assume 0 < DATA_SIZE && DATA_SIZE < MAX_NUM_DEVICES;
|
|---|
| 16 |
|
|---|
| 17 | $input int LOCAL;
|
|---|
| 18 | $input int MAX_LOCAL;
|
|---|
| 19 | $assume 0 < LOCAL && LOCAL < MAX_LOCAL;
|
|---|
| 20 | //this args struct will hold all the parameters of for the kernel function
|
|---|
| 21 | typedef struct
|
|---|
| 22 | {
|
|---|
| 23 | //Variables for kernels
|
|---|
| 24 |
|
|---|
| 25 | float * input;
|
|---|
| 26 | float * output;
|
|---|
| 27 | int count;
|
|---|
| 28 |
|
|---|
| 29 | }args;
|
|---|
| 30 |
|
|---|
| 31 | /*
|
|---|
| 32 | args * argument - Takes in the struct, which is changed for every program using a different kernel
|
|---|
| 33 | */
|
|---|
| 34 | cl_kernel clCreateKernel(args * argument)
|
|---|
| 35 | {
|
|---|
| 36 | cl_kernel kernel;
|
|---|
| 37 | kernel.arguments = argument;
|
|---|
| 38 |
|
|---|
| 39 | return kernel;
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | /*
|
|---|
| 43 | This is the kernel that processes compute with
|
|---|
| 44 | int workgroup - Gives the workgroup that a particular process came from, made by clEnqueueNDRangeKernel
|
|---|
| 45 | int global_id - Gives the global_id that a particular process has, given by workfunc
|
|---|
| 46 | int local_id - Gives the local_id that a particular process has, given by workfunc
|
|---|
| 47 | float* input - Kernel argument
|
|---|
| 48 | float* output - Kernel argument
|
|---|
| 49 | int count - Kernel argument
|
|---|
| 50 | */
|
|---|
| 51 | void square(int workgroup, int global_id, int local_id, float* input, float* output, int count)
|
|---|
| 52 | {
|
|---|
| 53 | //int i = get_global_id(0);
|
|---|
| 54 | int i = global_id;
|
|---|
| 55 | if (i < count)
|
|---|
| 56 | {
|
|---|
| 57 | output[i] = input[i] * input[i];
|
|---|
| 58 | //printf("output[%d] is %d\n", i, output[i]);
|
|---|
| 59 | }
|
|---|
| 60 | }
|
|---|
| 61 | /*
|
|---|
| 62 | workfunc assigns local and global ids, before calling the kernel.
|
|---|
| 63 | Note: The function should be identical in all transformations except the calling of the kernel, which means that it cannot be in openCLshared.cvl
|
|---|
| 64 | size_t local - The size of the workgroups, used to calculate blocks
|
|---|
| 65 | size_t global - The total amount of work to be done
|
|---|
| 66 | cl_kernel param - Holds the data for local_id, global_id, and the workgroup
|
|---|
| 67 | Use the print statement to get a better idea of what it means to split workgroups, local_ids, and global_ids
|
|---|
| 68 | */
|
|---|
| 69 | void workfunc(size_t local, size_t global, cl_kernel param)
|
|---|
| 70 | {
|
|---|
| 71 | for(int i = local * param.workgroup; i < local * param.workgroup + local; i++)
|
|---|
| 72 | {
|
|---|
| 73 | param.local_id = i % local;
|
|---|
| 74 | param.global_id = i;
|
|---|
| 75 | 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);
|
|---|
| 76 | square(param.workgroup, param.global_id, param.local_id, ((args*)param.arguments)->input, ((args*)param.arguments)->output, ((args*)param.arguments)->count);
|
|---|
| 77 | }
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | /*
|
|---|
| 81 | Splits up and spawns processes based on global and local, using block
|
|---|
| 82 | TODO: remove cl_command_queue completely and put into a "just in case" file, currently not needed
|
|---|
| 83 | cl_command_queue commands - Holds a queue of the order that devices are to be executed
|
|---|
| 84 | cl_kernel kernel - Holds all the arguments for the kernel, as well as local_id, global_id, and the workgroup
|
|---|
| 85 | size_t global - The total amount of work to be done
|
|---|
| 86 | size_t local - Number to split into workgroups by
|
|---|
| 87 | */
|
|---|
| 88 | int clEnqueueNDRangeKernel(cl_command_queue commands, cl_kernel kernel, size_t global, size_t local)
|
|---|
| 89 | {
|
|---|
| 90 | $assert(global % local == 0);
|
|---|
| 91 | cl_kernel param[global/local];
|
|---|
| 92 | $proc procs[global/local];
|
|---|
| 93 | for(int i = 0; i < global/local; i++)
|
|---|
| 94 | {
|
|---|
| 95 | param[i] = kernel;
|
|---|
| 96 | param[i].workgroup = i;
|
|---|
| 97 | procs[i] = $spawn workfunc(local, global, param[i]);
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | //this part here is the new clFinish(commands);
|
|---|
| 101 | for(int i = 0; i < global/local; i++)
|
|---|
| 102 | {
|
|---|
| 103 | $wait(procs[i]);
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | return CL_SUCCESS;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 | int main(int argc, char** argv)
|
|---|
| 111 | {
|
|---|
| 112 | args * arguments;
|
|---|
| 113 | arguments = (args*)malloc(sizeof(args));
|
|---|
| 114 |
|
|---|
| 115 | float data[DATA_SIZE]; // original data set given to device
|
|---|
| 116 | float results[DATA_SIZE]; // results returned from device
|
|---|
| 117 | unsigned int correct; // number of correct results returned
|
|---|
| 118 |
|
|---|
| 119 | size_t global; // global domain size for our calculation
|
|---|
| 120 | size_t local; // local domain size for our calculation
|
|---|
| 121 |
|
|---|
| 122 | cl_device_id device_id; // compute device id
|
|---|
| 123 | cl_context context; // compute context
|
|---|
| 124 | cl_command_queue commands; // compute command queue
|
|---|
| 125 | //cl_program program; // compute program
|
|---|
| 126 | cl_kernel kernel; // compute kernel
|
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 | float * input; // device memory used for the input array
|
|---|
| 130 | float * output; // device memory used for the output array
|
|---|
| 131 |
|
|---|
| 132 | //Puts in data for input
|
|---|
| 133 | unsigned int count = DATA_SIZE;
|
|---|
| 134 | for(int i = 0; i < count; i++)
|
|---|
| 135 | {
|
|---|
| 136 | data[i] = i;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | int err = clGetDeviceIDs(1, &device_id);
|
|---|
| 140 |
|
|---|
| 141 | //ignore clCreateContext for now, until we get an example that uses multiple ones
|
|---|
| 142 |
|
|---|
| 143 | //clCreateCommandQueue, could use context later
|
|---|
| 144 | commands = clCreateCommandQueue(device_id);
|
|---|
| 145 |
|
|---|
| 146 | //clCreateProgram is far different from the real version, this just stores parameters for the kernel
|
|---|
| 147 | //In order to make this clear, it is clCreateProgram and not something like clCreateProgramFromSource, which actually exists in openCL code
|
|---|
| 148 | //program = clCreateProgram(arguments);
|
|---|
| 149 |
|
|---|
| 150 | kernel = clCreateKernel(arguments);
|
|---|
| 151 |
|
|---|
| 152 | //replaces clCreateBuffer
|
|---|
| 153 | input = (float *) malloc(sizeof(float) * count);
|
|---|
| 154 | output = (float *) malloc(sizeof(float) * count);
|
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 | //replaces clEnqueueWriteBuffer, puts data into the input to be put into the kernel arguments
|
|---|
| 158 | memcpy(input, data, sizeof(float) * count);
|
|---|
| 159 |
|
|---|
| 160 | /*
|
|---|
| 161 | err = 0;
|
|---|
| 162 | err = clSetKernelArg(kernel, 0, sizeof(cl_mem), &input);
|
|---|
| 163 | err = clSetKernelArg(kernel, 1, sizeof(cl_mem), &output);
|
|---|
| 164 | err = clSetKernelArg(kernel, 2, sizeof(unsigned int), &count);
|
|---|
| 165 | */
|
|---|
| 166 |
|
|---|
| 167 | //use pointer instead of malloc + memcpy for global variables
|
|---|
| 168 | //((args*)kernel.arguments)->input = (float*)malloc(sizeof(float) * count);
|
|---|
| 169 | //memcpy(((args *)kernel.arguments)->input, input, sizeof(float) * count);
|
|---|
| 170 | ((args*)kernel.arguments)->input = input;
|
|---|
| 171 |
|
|---|
| 172 | //((args*)kernel.arguments)->output = (float*)malloc(sizeof(float) * count);
|
|---|
| 173 | //memcpy(((args*)kernel.arguments)->output, output, sizeof(float));
|
|---|
| 174 | ((args*)kernel.arguments)->output = output;
|
|---|
| 175 |
|
|---|
| 176 | ((args*)kernel.arguments)->count = count;
|
|---|
| 177 | //no malloc needed for non pointers
|
|---|
| 178 |
|
|---|
| 179 | //clGetKernelWorkGroupInfo would get a local size optimal for a device, but is not needed here
|
|---|
| 180 | local = LOCAL;
|
|---|
| 181 |
|
|---|
| 182 | global = count;
|
|---|
| 183 | /*
|
|---|
| 184 | commands holds the "order" of devices
|
|---|
| 185 | kernel holds program, which holds variables
|
|---|
| 186 | offset not implemented
|
|---|
| 187 | */
|
|---|
| 188 | err = clEnqueueNDRangeKernel(commands, kernel, global, local);
|
|---|
| 189 |
|
|---|
| 190 | //Replaces clEnqueueReadBuffer, which takes one of the saved variables and puts it out to another one
|
|---|
| 191 | memcpy(results, output, sizeof(float) * count);
|
|---|
| 192 |
|
|---|
| 193 | //verifies that all values in results are actually squared
|
|---|
| 194 | correct = 0;
|
|---|
| 195 | for(int i = 0; i < count; i++)
|
|---|
| 196 | {
|
|---|
| 197 | if(results[i] == data[i] * data[i])
|
|---|
| 198 | {
|
|---|
| 199 | correct++;
|
|---|
| 200 | }
|
|---|
| 201 | }
|
|---|
| 202 | printf("Computed '%d/%d' correct values!\n", correct, count);
|
|---|
| 203 |
|
|---|
| 204 | //TODO: Think of using void * array instead of regular arguments to make freeing easier
|
|---|
| 205 | free(((args*)kernel.arguments)->input);
|
|---|
| 206 | free(((args*)kernel.arguments)->output);
|
|---|
| 207 |
|
|---|
| 208 | free(input);
|
|---|
| 209 | free(output);
|
|---|
| 210 | free(arguments);
|
|---|
| 211 |
|
|---|
| 212 | return 0;
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 |
|
|---|
| 216 |
|
|---|