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
RevLine 
[d508f18]1//Forget the program
2
[863d90b]3#include "openCLshared.cvl"
[4686c50]4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
[863d90b]7
8$input int NUM_DEVICES;
[4686c50]9$input int MAX_NUM_DEVICES;
[863d90b]10$assume 0 < NUM_DEVICES && NUM_DEVICES < MAX_NUM_DEVICES;
11
[141e866]12$input int DATA_SIZE;
13$input int MAX_DATA_SIZE;
14$assume 0 < DATA_SIZE && DATA_SIZE < MAX_NUM_DEVICES;
15
[d508f18]16$input int LOCAL;
17
[141e866]18typedef struct
19{
20 //Variables for kernels
[d508f18]21
[141e866]22 int * input;
23 int * output;
[ad33b8b]24 int count;
[d508f18]25
[141e866]26}args;
27
[d508f18]28
29cl_kernel clCreateKernel(args * argument)
30{
31 cl_kernel kernel;
32 kernel.arguments = argument;
33
34 return kernel;
35}
[ad33b8b]36
[d508f18]37int clEnqueueNDRangeKernel(cl_command_queue commands, cl_kernel kernel, int global, int local)
38{
39
40 return CL_SUCCESS;
41}
42
[863d90b]43//kernel
[ad33b8b]44void square(int workgroup, int global_id, int local_id, float* input, float* output, int count)
[863d90b]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{
[d508f18]68 args * arguments;
69 arguments = (args*)malloc(sizeof(args));
[141e866]70
[85120bb]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
[863d90b]74
[85120bb]75 size_t global; // global domain size for our calculation
76 size_t local; // local domain size for our calculation
[863d90b]77
[85120bb]78 cl_device_id device_id; // compute device id
79 cl_context context; // compute context
80 cl_command_queue commands; // compute command queue
[d508f18]81 //cl_program program; // compute program
[85120bb]82 cl_kernel kernel; // compute kernel
[d508f18]83
84
85 float * input; // device memory used for the input array
86 float * output; // device memory used for the output array
[85120bb]87
[d508f18]88 unsigned int count = DATA_SIZE;
89 for(int i = 0; i < count; i++)
90 {
91 data[i] = i;
92 }
[85120bb]93
[1e4e5da]94 int err = clGetDeviceIDs(1, &device_id);
[4686c50]95
[141e866]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
[d508f18]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);
[ad33b8b]115 //clEnqueueWriteBuffer
[d508f18]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 */
[ad33b8b]123 //kernel.arguments.input = input;
124
125 //kernel.arguments->input;
[d508f18]126
[ad33b8b]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));
[d508f18]130
[ad33b8b]131 //Not anymore, ignore it and use clSetKernelArg
[d508f18]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);
[141e866]143
[d508f18]144 free(input);
145 free(output);
146 free(arguments);
[863d90b]147 return 0;
148}
149
Note: See TracBrowser for help on using the repository browser.