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

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

Working on clsetkernelarg

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

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