source: CIVL/examples/translation/openclversion2.1/square.cvl@ c5b89ea

1.23 2.0 acw/focus-triggers main test-branch
Last change on this file since c5b89ea was 50088c4, checked in by Jacob Trieu <fuufusuu@…>, 12 years ago

nothing, actually

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@1271 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 7.3 KB
RevLine 
[d508f18]1//Forget the program
2
[863d90b]3#include "openCLshared.cvl"
[4686c50]4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
[da70bc2]7#include <civlc.h>
[863d90b]8
9$input int NUM_DEVICES;
[4686c50]10$input int MAX_NUM_DEVICES;
[863d90b]11$assume 0 < NUM_DEVICES && NUM_DEVICES < MAX_NUM_DEVICES;
12
[141e866]13$input int DATA_SIZE;
14$input int MAX_DATA_SIZE;
15$assume 0 < DATA_SIZE && DATA_SIZE < MAX_NUM_DEVICES;
16
[d508f18]17$input int LOCAL;
[823d00e]18$input int MAX_LOCAL;
19$assume 0 < LOCAL && LOCAL < MAX_LOCAL;
[b93a874]20//this args struct will hold all the parameters of for the kernel function
[141e866]21typedef struct
22{
23 //Variables for kernels
[d508f18]24
[823d00e]25 float * input;
26 float * output;
[ad33b8b]27 int count;
[d508f18]28
[141e866]29}args;
30
[b93a874]31/*
32 args * argument - Takes in the struct, which is changed for every program using a different kernel
33*/
[d508f18]34cl_kernel clCreateKernel(args * argument)
35{
36 cl_kernel kernel;
37 kernel.arguments = argument;
38
39 return kernel;
40}
[823d00e]41
[b93a874]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*/
[ad33b8b]51void square(int workgroup, int global_id, int local_id, float* input, float* output, int count)
[863d90b]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}
[b93a874]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*/
[da70bc2]69void workfunc(size_t local, size_t global, cl_kernel param)
[863d90b]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;
[da70bc2]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);
[863d90b]77 }
78}
[da70bc2]79
[b93a874]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*/
88int clEnqueueNDRangeKernel(cl_command_queue commands, cl_kernel kernel, size_t global, size_t local)
[da70bc2]89{
90 $assert(global % local == 0);
[6f403c8]91 int numworkgroups = global/local;
92 cl_kernel param[numworkgroups];
93 $proc procs[numworkgroups];
94 //consider $parfor
95 $domain(1) dom = {0 .. numworkgroups - 1};
96
97 $for(int i: dom)
[da70bc2]98 {
99 param[i] = kernel;
100 param[i].workgroup = i;
101 }
[6f403c8]102 $parfor(int i: dom)
103 {
104 workfunc(local, global, param[i]);
105 }
[50088c4]106
107 /*
[da70bc2]108 for(int i = 0; i < global/local; i++)
109 {
[6f403c8]110 param[i] = kernel;
111 param[i].workgroup = i;
112 procs[i] = $spawn workfunc(local, global, param[i]);
[da70bc2]113 }
[50088c4]114
[6f403c8]115 //this part here is the new clFinish(commands);
[50088c4]116 for(int i = 0; i < global/local; i++)
117 {
118 $wait(procs[i]);
119 }
120 */
[da70bc2]121 return CL_SUCCESS;
122}
123
124
[863d90b]125int main(int argc, char** argv)
126{
[d508f18]127 args * arguments;
128 arguments = (args*)malloc(sizeof(args));
[141e866]129
[85120bb]130 float data[DATA_SIZE]; // original data set given to device
131 float results[DATA_SIZE]; // results returned from device
132 unsigned int correct; // number of correct results returned
[863d90b]133
[85120bb]134 size_t global; // global domain size for our calculation
135 size_t local; // local domain size for our calculation
[863d90b]136
[85120bb]137 cl_device_id device_id; // compute device id
138 cl_context context; // compute context
139 cl_command_queue commands; // compute command queue
[d508f18]140 //cl_program program; // compute program
[85120bb]141 cl_kernel kernel; // compute kernel
[d508f18]142
143
144 float * input; // device memory used for the input array
145 float * output; // device memory used for the output array
[85120bb]146
[b93a874]147 //Puts in data for input
[d508f18]148 unsigned int count = DATA_SIZE;
149 for(int i = 0; i < count; i++)
150 {
151 data[i] = i;
152 }
[85120bb]153
[1e4e5da]154 int err = clGetDeviceIDs(1, &device_id);
[4686c50]155
[141e866]156 //ignore clCreateContext for now, until we get an example that uses multiple ones
157
158 //clCreateCommandQueue, could use context later
159 commands = clCreateCommandQueue(device_id);
160
161 //clCreateProgram is far different from the real version, this just stores parameters for the kernel
162 //In order to make this clear, it is clCreateProgram and not something like clCreateProgramFromSource, which actually exists in openCL code
[d508f18]163 //program = clCreateProgram(arguments);
164
165 kernel = clCreateKernel(arguments);
166
[b93a874]167 //replaces clCreateBuffer
[d508f18]168 input = (float *) malloc(sizeof(float) * count);
169 output = (float *) malloc(sizeof(float) * count);
170
171
[b93a874]172 //replaces clEnqueueWriteBuffer, puts data into the input to be put into the kernel arguments
[d508f18]173 memcpy(input, data, sizeof(float) * count);
174
175 /*
176 err = 0;
177 err = clSetKernelArg(kernel, 0, sizeof(cl_mem), &input);
178 err = clSetKernelArg(kernel, 1, sizeof(cl_mem), &output);
179 err = clSetKernelArg(kernel, 2, sizeof(unsigned int), &count);
180 */
181
[da70bc2]182 //use pointer instead of malloc + memcpy for global variables
183 //((args*)kernel.arguments)->input = (float*)malloc(sizeof(float) * count);
184 //memcpy(((args *)kernel.arguments)->input, input, sizeof(float) * count);
185 ((args*)kernel.arguments)->input = input;
[d508f18]186
[da70bc2]187 //((args*)kernel.arguments)->output = (float*)malloc(sizeof(float) * count);
188 //memcpy(((args*)kernel.arguments)->output, output, sizeof(float));
189 ((args*)kernel.arguments)->output = output;
[823d00e]190
191 ((args*)kernel.arguments)->count = count;
192 //no malloc needed for non pointers
[d508f18]193
[b93a874]194 //clGetKernelWorkGroupInfo would get a local size optimal for a device, but is not needed here
[d508f18]195 local = LOCAL;
196
197 global = count;
198 /*
199 commands holds the "order" of devices
200 kernel holds program, which holds variables
201 offset not implemented
202 */
[823d00e]203 err = clEnqueueNDRangeKernel(commands, kernel, global, local);
204
[b93a874]205 //Replaces clEnqueueReadBuffer, which takes one of the saved variables and puts it out to another one
[da70bc2]206 memcpy(results, output, sizeof(float) * count);
[823d00e]207
[b93a874]208 //verifies that all values in results are actually squared
[da70bc2]209 correct = 0;
210 for(int i = 0; i < count; i++)
211 {
212 if(results[i] == data[i] * data[i])
213 {
214 correct++;
215 }
216 }
217 printf("Computed '%d/%d' correct values!\n", correct, count);
[823d00e]218
[b93a874]219 //TODO: Think of using void * array instead of regular arguments to make freeing easier
[50088c4]220
[823d00e]221 free(((args*)kernel.arguments)->input);
222 free(((args*)kernel.arguments)->output);
[50088c4]223 /*
[d508f18]224 free(input);
225 free(output);
[50088c4]226 */
[d508f18]227 free(arguments);
[da70bc2]228
[863d90b]229 return 0;
230}
231
[ebbf499]232
233
Note: See TracBrowser for help on using the repository browser.