source: CIVL/examples/opencl/2.15/arrKernels.cvl@ bb03188

main test-branch
Last change on this file since bb03188 was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago

Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.

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

  • Property mode set to 100644
File size: 3.1 KB
Line 
1 /*
2 cl_kernel kernel;
3 kernel = clCreateKernel(arguments, "nothing");
4
5 ((args*)kernel.arguments)->param[0] = (int*)malloc(sizeof(int));
6 memcpy(((args*)kernel.arguments)->param[0], &star, sizeof(int));
7
8 ((args*)kernel.arguments)->param[1] = one;
9
10 clReleaseKernel(kernel);
11
12 printf("single kernel works\n");
13 */
14
15
16#include "cl.cvl"
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <civlc.h>
21#include "multiplekernel.cvl"
22
23int numArgs = 2;
24int numKernels = 3;
25/*
26 This version of the kernel has a flag for each element in the param array
27 If the mallocflag is set to 1, it means that it will be freed by clReleaseKernel
28*/
29typedef struct
30{
31 void * param[numArgs];
32 int mallocflag[numArgs];
33}args;
34/*
35 clCreateKernel in this iteration turns each flag to 0 to start off with, otherwise it gets a symbolic expression
36*/
37cl_kernel clCreateKernel(args * argument, char * function)
38{
39 cl_kernel kernel;
40 kernel.arguments = argument;
41 kernel.method = function;
42
43 for(int j = 0; j < numArgs; j++)
44 {
45 ((args*)kernel.arguments)->mallocflag[j] = 0;
46 }
47
48 return kernel;
49}
50/*
51 Checks the flag to make sure that it was a local variable that must be freed every step of the way
52*/
53void clReleaseKernel(cl_kernel kernel)
54{
55 for (int i = 0; i < numArgs; i++)
56 {
57 printf("I am argument %d with value %d\n", i, ((args*)kernel.arguments)->mallocflag[i]);
58 if (((args*)kernel.arguments)->mallocflag[i] == 1)
59 {
60 printf("and I pass the flag check\n");
61 free(((args*)kernel.arguments)->param[i]);
62 }
63 }
64}
65
66int main(int argc, char** argv)
67{
68 args * arguments;
69 arguments = (args*)malloc(sizeof(args) * numKernels);
70 //Must have enough arguments for each kernel
71
72 int star = 1;
73
74 int * one = (int*)malloc(sizeof(int));
75 memcpy(one, &star, sizeof(int));
76
77 //cl_kernel *kernels = (cl_kernel*)malloc(numKernels * sizeof(cl_kernel));
78 cl_kernel kernels[numKernels];
79
80 printf("%d, %d\n", star, *one);
81
82 //For loop to create multiple kernels, the programmer must put the loop himself
83 for (int i = 0; i < numKernels; i++)
84 {
85 kernels[i] = clCreateKernel(arguments+i, "eh");
86
87
88 }
89 //For each kernel, put in the variables
90 for (int j = 0; j < numKernels; j++)
91 {
92
93 ((args*)kernels[j].arguments)->param[0] = (int*)malloc(sizeof(int));
94 ((args*)kernels[j].arguments)->mallocflag[0] = 1;
95 memcpy(((args*)kernels[j].arguments)->param[0], &star, sizeof(int));
96
97 //param[0] is a __local variable, so it gets the flag put to 1
98
99 //printf("I am kernel of index %d, with a value of %d\n", j, ((args*)kernels[j].arguments)->param[0]);
100
101 ((args*)kernels[j].arguments)->param[1] = one;
102 //This is a __global variable, so it does not get the flag changed in this iteration.
103 //TODO: determine if global variables were freed by clReleaseKernel or the programmer.
104 }
105
106 //printf("assigned properly\n");
107
108 for (int k = 0; k < numKernels; k++)
109 {
110 clReleaseKernel(kernels[k]);
111 }
112
113 for (int k = 0; k < numKernels; k++)
114 {
115 //free(((args*)kernels[k].arguments)->param[0]);
116 }
117
118
119 free(one);
120 free(arguments);
121 return 0;
122}
123
124
Note: See TracBrowser for help on using the repository browser.