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

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

Getting there

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

  • Property mode set to 100644
File size: 4.0 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
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
40 return CL_SUCCESS;
41}
42
43//kernel
44void square(int workgroup, int global_id, int local_id, float* input, float* output, int count)
45{
46 //int i = get_global_id(0);
47 int i = global_id;
48 if (i < count)
49 {
50 output[i] = input[i] * input[i];
51 //printf("output[%d] is %d\n", i, output[i]);
52 }
53}
54/*
55void workfunc(size_t local, size_t global, kernel param)
56{
57 for(int i = local * param.workgroup; i < local * param.workgroup + local; i++)
58 {
59 param.local_id = i % local;
60 param.global_id = i;
61 //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);
62 square(param.workgroup, param.global_id, param.local_id, param.input, param.output, param.count);
63 }
64}
65*/
66int main(int argc, char** argv)
67{
68 args * arguments;
69 arguments = (args*)malloc(sizeof(args));
70
71 float data[DATA_SIZE]; // original data set given to device
72 float results[DATA_SIZE]; // results returned from device
73 unsigned int correct; // number of correct results returned
74
75 size_t global; // global domain size for our calculation
76 size_t local; // local domain size for our calculation
77
78 cl_device_id device_id; // compute device id
79 cl_context context; // compute context
80 cl_command_queue commands; // compute command queue
81 //cl_program program; // compute program
82 cl_kernel kernel; // compute kernel
83
84
85 float * input; // device memory used for the input array
86 float * output; // device memory used for the output array
87
88 unsigned int count = DATA_SIZE;
89 for(int i = 0; i < count; i++)
90 {
91 data[i] = i;
92 }
93
94 int err = clGetDeviceIDs(1, &device_id);
95
96 //ignore clCreateContext for now, until we get an example that uses multiple ones
97
98 //clCreateCommandQueue, could use context later
99 commands = clCreateCommandQueue(device_id);
100
101 //clCreateProgram is far different from the real version, this just stores parameters for the kernel
102 //In order to make this clear, it is clCreateProgram and not something like clCreateProgramFromSource, which actually exists in openCL code
103 //program = clCreateProgram(arguments);
104
105 kernel = clCreateKernel(arguments);
106
107
108 //comes from clCreateBuffer
109 input = (float *) malloc(sizeof(float) * count);
110 output = (float *) malloc(sizeof(float) * count);
111
112
113
114 memcpy(input, data, sizeof(float) * count);
115 //clEnqueueWriteBuffer
116
117 /*
118 err = 0;
119 err = clSetKernelArg(kernel, 0, sizeof(cl_mem), &input);
120 err = clSetKernelArg(kernel, 1, sizeof(cl_mem), &output);
121 err = clSetKernelArg(kernel, 2, sizeof(unsigned int), &count);
122 */
123 //kernel.arguments.input = input;
124
125 //kernel.arguments->input;
126
127 memcpy(((args *)kernel.arguments)->input, input, sizeof(float));
128 memcpy(((args*)kernel.arguments)->output, output, sizeof(float));
129 memcpy(((args*)kernel.arguments)->count, count, sizeof(int));
130
131 //Not anymore, ignore it and use clSetKernelArg
132
133 //clGetKernelWorkGroupInfo
134 local = LOCAL;
135
136 global = count;
137 /*
138 commands holds the "order" of devices
139 kernel holds program, which holds variables
140 offset not implemented
141 */
142 //err = clEnqueueNDRangeKernel(commands, kernel, global, local);
143
144 free(input);
145 free(output);
146 free(arguments);
147 return 0;
148}
149
Note: See TracBrowser for help on using the repository browser.