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

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

KJL

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

  • Property mode set to 100644
File size: 5.2 KB
Line 
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//Didn't initialize variables here
21typedef struct
22{
23 //Variables for kernels
24
25 float * input;
26 float * output;
27 int count;
28
29}args;
30
31
32cl_kernel clCreateKernel(args * argument)
33{
34 cl_kernel kernel;
35 kernel.arguments = argument;
36
37 return kernel;
38}
39
40//kernel
41void square(int workgroup, int global_id, int local_id, float* input, float* output, int count)
42{
43 //int i = get_global_id(0);
44 int i = global_id;
45 if (i < count)
46 {
47 output[i] = input[i] * input[i];
48 //printf("output[%d] is %d\n", i, output[i]);
49 }
50}
51
52
53void workfunc(size_t local, size_t global, cl_kernel param)
54{
55 for(int i = local * param.workgroup; i < local * param.workgroup + local; i++)
56 {
57 param.local_id = i % local;
58 param.global_id = i;
59 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);
60 square(param.workgroup, param.global_id, param.local_id, ((args*)param.arguments)->input, ((args*)param.arguments)->output, ((args*)param.arguments)->count);
61 }
62}
63
64int clEnqueueNDRangeKernel(cl_command_queue commands, cl_kernel kernel, int global, int local)
65{
66 $assert(global % local == 0);
67 cl_kernel param[global/local];
68 $proc procs[global/local];
69 for(int i = 0; i < global/local; i++)
70 {
71 param[i] = kernel;
72 param[i].workgroup = i;
73 procs[i] = $spawn workfunc(local, global, param[i]);
74 }
75
76 //this part here is the new clFinish(commands);
77 for(int i = 0; i < global/local; i++)
78 {
79 $wait(procs[i]);
80 }
81
82 return CL_SUCCESS;
83}
84
85
86int main(int argc, char** argv)
87{
88 args * arguments;
89 arguments = (args*)malloc(sizeof(args));
90
91 float data[DATA_SIZE]; // original data set given to device
92 float results[DATA_SIZE]; // results returned from device
93 unsigned int correct; // number of correct results returned
94
95 size_t global; // global domain size for our calculation
96 size_t local; // local domain size for our calculation
97
98 cl_device_id device_id; // compute device id
99 cl_context context; // compute context
100 cl_command_queue commands; // compute command queue
101 //cl_program program; // compute program
102 cl_kernel kernel; // compute kernel
103
104
105 float * input; // device memory used for the input array
106 float * output; // device memory used for the output array
107
108 unsigned int count = DATA_SIZE;
109 for(int i = 0; i < count; i++)
110 {
111 data[i] = i;
112 }
113
114 int err = clGetDeviceIDs(1, &device_id);
115
116 //ignore clCreateContext for now, until we get an example that uses multiple ones
117
118 //clCreateCommandQueue, could use context later
119 commands = clCreateCommandQueue(device_id);
120
121 //clCreateProgram is far different from the real version, this just stores parameters for the kernel
122 //In order to make this clear, it is clCreateProgram and not something like clCreateProgramFromSource, which actually exists in openCL code
123 //program = clCreateProgram(arguments);
124
125 kernel = clCreateKernel(arguments);
126 //printf("%s", kernel);
127
128 //comes from clCreateBuffer
129 input = (float *) malloc(sizeof(float) * count);
130 output = (float *) malloc(sizeof(float) * count);
131
132
133
134 memcpy(input, data, sizeof(float) * count);
135 //clEnqueueWriteBuffer
136
137 /*
138 err = 0;
139 err = clSetKernelArg(kernel, 0, sizeof(cl_mem), &input);
140 err = clSetKernelArg(kernel, 1, sizeof(cl_mem), &output);
141 err = clSetKernelArg(kernel, 2, sizeof(unsigned int), &count);
142 */
143 //kernel.arguments.input = input;
144
145 //kernel.arguments->input;
146
147 //use pointer instead of malloc + memcpy for global variables
148 //((args*)kernel.arguments)->input = (float*)malloc(sizeof(float) * count);
149 //memcpy(((args *)kernel.arguments)->input, input, sizeof(float) * count);
150 ((args*)kernel.arguments)->input = input;
151
152 //((args*)kernel.arguments)->output = (float*)malloc(sizeof(float) * count);
153 //memcpy(((args*)kernel.arguments)->output, output, sizeof(float));
154 ((args*)kernel.arguments)->output = output;
155
156 ((args*)kernel.arguments)->count = count;
157 //no malloc needed for non pointers
158
159 //clGetKernelWorkGroupInfo
160 local = LOCAL;
161
162 global = count;
163 /*
164 commands holds the "order" of devices
165 kernel holds program, which holds variables
166 offset not implemented
167 */
168 err = clEnqueueNDRangeKernel(commands, kernel, global, local);
169
170 memcpy(results, output, sizeof(float) * count);
171
172 correct = 0;
173 for(int i = 0; i < count; i++)
174 {
175 if(results[i] == data[i] * data[i])
176 {
177 correct++;
178 }
179 }
180 printf("Computed '%d/%d' correct values!\n", correct, count);
181
182 free(((args*)kernel.arguments)->input);
183 free(((args*)kernel.arguments)->output);
184
185 free(input);
186 free(output);
187 free(arguments);
188
189 return 0;
190}
191
192
193
Note: See TracBrowser for help on using the repository browser.