source: CIVL/examples/translation/openclversion2.15/multiple.cvl@ ef9edbd

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

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

  • Property mode set to 100644
File size: 5.5 KB
Line 
1
2//verify examples/translation/openclversion2.15/multiple.cvl
3
4#include "cl.cvl"
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8#include <civlc.h>
9#include "multiplekernel.cvl"
10
11int numArgs = 2;
12int numKernels = 4;
13/*
14 args * argument - Takes in the struct, which is changed for every program using a different kernel
15 int mallocflag[numArgs] - A flag for each element in the param array. If the mallocflag is set to 1, it means that it will be freed by clReleaseKernel
16*/
17typedef struct
18{
19 void * param[numArgs];
20 int mallocflag[numArgs];
21}args;
22/*
23 clCreateKernel in this iteration turns each flag to 0 to start off with, otherwise it gets a symbolic expression
24*/
25cl_kernel clCreateKernel(args * argument, char * function)
26{
27 cl_kernel kernel;
28 kernel.arguments = argument;
29 kernel.method = function;
30
31 for(int j = 0; j < numArgs; j++)
32 {
33 ((args*)kernel.arguments)->mallocflag[j] = 0;
34 }
35
36 return kernel;
37}
38/*
39 Checks the flag to make sure that it was a local variable that must be freed every step of the way
40*/
41void clReleaseKernel(cl_kernel kernel)
42{
43 for (int i = 0; i < numArgs; i++)
44 {
45 //printf("I am argument %d with value %d\n", i, ((args*)kernel.arguments)->mallocflag[i]);
46 if (((args*)kernel.arguments)->mallocflag[i] == 1)
47 {
48 //printf("and I pass the flag check\n");
49 free(((args*)kernel.arguments)->param[i]);
50 }
51 }
52}
53/*
54 workfunc assigns local and global ids, before calling the kernel.
55 Note: The function should be identical in all transformations except the calling of the kernel, which means that it cannot be in openCLshared.cvl
56 size_t local - The size of the workgroups, used to calculate blocks
57 size_t global - The total amount of work to be done
58 cl_kernel param - Holds the data for local_id, global_id, and the workgroup
59 Use the print statement to get a better idea of what it means to split workgroups, local_ids, and global_ids
60*/
61void workfunc(size_t local, size_t global, cl_kernel param)
62{
63 $proc procs[local];
64 char * reduceChar = "add";
65 for(int i = local * param.workgroup; i < local * param.workgroup + local; i++)
66 {
67 int n = *(int*)(((args*)param.arguments)->param[0]);
68 param.local_id = i % local;
69 param.global_id = i;
70 //printf("My workgroup id is %d, my global id is %d, my local id is %d, and my method is %s\n", param.workgroup, param.global_id, param.local_id, param.method);
71
72
73 //if(strcmp(param.method, reduceChar) == 0)
74 //{
75 procs[param.local_id] = $spawn add(param.workgroup, param.global_id, param.local_id, n, ((args*)param.arguments)->param[1]);
76 //}
77 }
78 for(int j = 0; j < local; j++)
79 {
80 $wait(procs[j]);
81 }
82}
83
84/*
85 Splits up and spawns processes based on global and local, using block
86 cl_kernel kernel - Holds all the arguments for the kernel, as well as local_id, global_id, and the workgroup
87 size_t global - The total amount of work to be done
88 size_t local - Number to split into workgroups by
89*/
90int clEnqueueNDRangeKernel(cl_kernel kernel, size_t global, size_t local)
91{
92
93 $assert(global % local == 0);
94 int numworkgroups = global/local;
95 cl_kernel param[numworkgroups];
96 $proc procs[numworkgroups];
97 //consider $parfor
98
99 /*
100 $domain(1) dom = {0 .. numworkgroups - 1};
101
102 $for(int i: dom)
103 {
104 param[i] = kernel;
105 param[i].workgroup = i;
106 }
107 $parfor(int i: dom)
108 {
109 workfunc(local, global, param[i]);
110 }
111 */
112
113 for(int i = 0; i < global/local; i++)
114 {
115 param[i] = kernel;
116 param[i].workgroup = i;
117 procs[i] = $spawn workfunc(local, global, param[i]);
118 }
119
120 //this part here is the new clFinish(commands);
121 for(int i = 0; i < global/local; i++)
122 {
123 $wait(procs[i]);
124 }
125
126 return CL_SUCCESS;
127
128}
129
130int main(int argc, char** argv)
131{
132 int global = numKernels;
133 int local = 2;
134
135 args * arguments;
136 arguments = (args*)malloc(sizeof(args) * numKernels);
137 //Must have enough arguments for each kernel
138
139 int star = 1;
140
141 int * one = (int*)malloc(sizeof(int));
142 memcpy(one, &star, sizeof(int));
143
144 //cl_kernel *kernels = (cl_kernel*)malloc(numKernels * sizeof(cl_kernel));
145 cl_kernel kernels[numKernels];
146
147 //printf("%d, %d\n", star, *one);
148
149 //For loop to create multiple kernels, the programmer must put the loop himself
150 for (int i = 0; i < numKernels; i++)
151 {
152 kernels[i] = clCreateKernel(arguments+i, "add");
153
154
155 }
156 //For each kernel, put in the variables
157 for (int j = 0; j < numKernels; j++)
158 {
159
160 ((args*)kernels[j].arguments)->param[0] = (int*)malloc(sizeof(int));
161 ((args*)kernels[j].arguments)->mallocflag[0] = 1;
162 memcpy(((args*)kernels[j].arguments)->param[0], &star, sizeof(int));
163
164 //param[0] is a __local variable, so it gets the flag put to 1
165
166 //printf("I am kernel of index %d, with a value of %d\n", j, ((args*)kernels[j].arguments)->param[0]);
167
168 ((args*)kernels[j].arguments)->param[1] = one;
169 //This is a __global variable, so it does not get the flag changed in this iteration.
170 //TODO: determine if global variables were freed by clReleaseKernel or the programmer.
171 }
172
173 //printf("assigned properly\n");
174
175 for (int i = 0; i < numKernels; i++)
176 {
177 clEnqueueNDRangeKernel(kernels[i], global, local);
178 }
179 int ans = (int)((args*)kernels[0].arguments)->param[1];
180 //memcpy(ans, &((args*)kernels[0].arguments)->param[1], sizeof(int));
181 printf("There are %d kernels, and the final value is %d\n", numKernels, ans);
182
183 for (int k = 0; k < numKernels; k++)
184 {
185 clReleaseKernel(kernels[k]);
186 }
187
188
189
190 free(one);
191 free(arguments);
192 return 0;
193}
194
195
Note: See TracBrowser for help on using the repository browser.