source: CIVL/examples/translation/openclversion2/square.cvl@ 4540352

1.23 2.0 main test-branch
Last change on this file since 4540352 was f95a8a5, checked in by Jacob Trieu <fuufusuu@…>, 12 years ago

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

  • Property mode set to 100644
File size: 3.9 KB
Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4
5/*
6If increase MAX_DATA_SIZE, nothing happens if DATA_SIZE is given a concrete value
7If I increase DATA_SIZE to 2, I get
8Computed '2/2' correct values!
9Computed '1/2' correct values!
10
11If I make num_devices >= data_size, I get a num/num correct values, like the default
12data = 3, devices = 1
13Computed '3/3' correct values!
14Computed '2/3' correct values!
15Computed '2/3' correct values!
16Computed '1/3' correct values!
17
18data = 3, devices = 2
19Computed '3/3' correct values!
20Computed '2/3' correct values!
21
22data = 3, devices = 3
23Computed '3/3' correct values!
24*/
25$input int DATA_SIZE;
26$input int NUM_DEVICES;
27$input int MAX_DATA_SIZE;
28$input int MAX_NUM_DEVICES;
29$assume 0 < DATA_SIZE && DATA_SIZE < MAX_DATA_SIZE;
30$assume 0 < NUM_DEVICES && NUM_DEVICES < MAX_NUM_DEVICES;
31$gbarrier gbarrier = $gbarrier_create($here, NUM_DEVICES);
32//struct goes here
33typedef struct
34{
35 int device_id;
36 //other variables
37 float * input;
38 float * output;
39 int count;
40}process;
41
42//kernel goes here
43void square(int device_id, float* input, float* output, const unsigned int count)
44{
45 //int i = get_global_id(0);
46 int i = device_id;
47 if (i < count)
48 {
49 output[i] = input[i] * input[i];
50 }
51}
52/*
53Note that the original lines were "__kernel void square( \n" \
54" __global float* input, \n" \
55" __global float* output, \n" \
56" const unsigned int count) \n" \
57
58Any parser must take note of and don't input \n, "", or \ as is
59__global float * input, __global float * output, int count;
60*/
61
62
63int main(int argc, char** argv)
64{
65 //get the number from clGetDeviceIDs 3rd parameter
66 //int num_devices = 1;
67
68
69 //variables from __kernel come here
70 float * input;
71 float * output;
72 int count;
73
74
75 //from the code before
76 float data[DATA_SIZE]; // original data set given to device
77 float results[DATA_SIZE]; // results returned from device
78 int correct; // number of correct results returned
79 //handle the definitions being put in different places
80
81 int i = 0;
82 count = DATA_SIZE; //count defined here
83 for(i = 0; i < count; i++)
84 {
85 data[i] = rand() / (float)RAND_MAX;
86 }
87
88
89
90 //comes from clCreateBuffer
91 input = (float *) malloc(sizeof(float) * count);
92 output = (float *) malloc(sizeof(float) * count);
93
94 //Possibly keep a list of variables, with a flag for whether they are init or not
95 //Not init, malloc one from what is found in
96 //output = clCreateBuffer(context, CL_MEM_WRITE_ONLY, sizeof(float) * count, NULL, NULL);
97
98 input = data; //input defined here
99 //came from clEnqueueWriteBuffer rather than the start of code
100
101
102 //Phase after this is the definitions
103 process now[NUM_DEVICES];
104 //put device_ids
105 for(int i = 0; i < NUM_DEVICES; i++)
106 {
107 now[i].device_id = i;
108 //other variables
109 now[i].input = input;
110 now[i].output = output;
111 now[i].count = count;
112 }
113
114 //spawns processes according to devices, uses the struct for inputs
115 $proc procs[NUM_DEVICES];
116 for(int i = 0; i < NUM_DEVICES; i++)
117 {
118 procs[i] = $spawn square(now[i].device_id, now[i].input, now[i].output, now[i].count);
119 }
120
121 for(int i = 0; i < NUM_DEVICES; i++)
122 {
123 $wait(procs[i]);
124 }
125
126 //$barrier barrier = $barrier_create($here, gbarrier, now[i].device_id);
127 //$barrier_call(barrier);
128 //$barrier_destroy(barrier);
129
130
131
132 $gbarrier_destroy(gbarrier);
133
134 //use the information from clEnqueueReadBuffer
135 //may have to alter later
136 for(int i = 0; i < count; i++)
137 {
138 results[i] = output[i];
139 }
140
141 correct = 0;
142 for(i = 0; i < count; i++)
143 {
144 if(results[i] == data[i] * data[i])
145 {
146 correct++;
147 }
148 }
149
150 // Print a brief summary detailing the results
151 //
152 printf("Computed '%d/%d' correct values!\n", correct, count);
153
154
155 return 0;
156}
Note: See TracBrowser for help on using the repository browser.