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

1.23 2.0 main test-branch
Last change on this file since 8a50139 was 774abd2, checked in by Manchun Zheng <zmanchun@…>, 12 years ago

fixes

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

  • Property mode set to 100644
File size: 5.6 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\n", param.workgroup, param.global_id, param.local_id);
71 printf("workfunc var is %s,\n", reduceChar);
72 printf(" kernel method name is %s\n", param.method);
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 = 1;
133 int local = 1;
134
135 args arguments[numKernels];
136 //arguments = (args*)malloc(sizeof(args) * numKernels);
137 //Must have enough arguments for each kernel
138 //try not on the heap
139
140 int one = 1;
141 int counter = 0;
142 //don't need to put on the heap
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 char * strin = (char*)malloc(sizeof(char) * 4);
151 strin[0]='a';
152 strin[1]='d';
153 strin[2]='d';
154 strin[3]=0;
155 for (int i = 0; i < numKernels; i++)
156 {
157 kernels[i] = clCreateKernel(arguments+i, strin);
158
159
160 }
161 //For each kernel, put in the variables
162 for (int j = 0; j < numKernels; j++)
163 {
164
165 ((args*)kernels[j].arguments)->param[0] = (int*)malloc(sizeof(int));
166 ((args*)kernels[j].arguments)->mallocflag[0] = 1;
167 memcpy(((args*)kernels[j].arguments)->param[0], &one, sizeof(int));
168
169 //param[0] is a __local variable, so it gets the flag put to 1
170
171 //printf("I am kernel of index %d, with a value of %d\n", j, ((args*)kernels[j].arguments)->param[0]);
172
173 ((args*)kernels[j].arguments)->param[1] = &counter;
174 //This is a __global variable, so it does not get the flag changed in this iteration.
175 //TODO: determine if global variables were freed by clReleaseKernel or the programmer.
176 }
177
178 //printf("assigned properly\n");
179
180 for (int i = 0; i < numKernels; i++)
181 {
182 clEnqueueNDRangeKernel(kernels[i], global, local);
183 }
184 int ans = *((int*)((args*)kernels[0].arguments)->param[1]);
185 //memcpy(ans, &((args*)kernels[0].arguments)->param[1], sizeof(int));
186 printf("There are %d kernels, and the final value is %d\n", numKernels, ans);
187
188 for (int k = 0; k < numKernels; k++)
189 {
190 clReleaseKernel(kernels[k]);
191 }
192
193 return 0;
194}
195
196
Note: See TracBrowser for help on using the repository browser.