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

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

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

  • Property mode set to 100644
File size: 4.0 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
99 memcpy(input, data, sizeof(float) * count);
100 //came from clEnqueueWriteBuffer rather than the start of code
101
102
103 //Phase after this is the definitions
104 int devices[NUM_DEVICES];
105 process now[NUM_DEVICES];
106 //put device_ids
107 for(int i = 0; i < NUM_DEVICES; i++)
108 {
109 now[i].device_id = i;
110 /*
111 //other variables, put into a separate place
112 now[i].input = input;
113 now[i].output = output;
114 now[i].count = count;
115 */
116 }
117
118 //spawns processes according to devices, uses the struct for inputs
119 $proc procs[NUM_DEVICES];
120 for(int i = 0; i < NUM_DEVICES; i++)
121 {
122 procs[i] = $spawn square(now[i].device_id, now[i].input, now[i].output, now[i].count);
123 }
124
125 for(int i = 0; i < NUM_DEVICES; i++)
126 {
127 $wait(procs[i]);
128 }
129
130 //$barrier barrier = $barrier_create($here, gbarrier, now[i].device_id);
131 //$barrier_call(barrier);
132 //$barrier_destroy(barrier);
133
134
135
136 $gbarrier_destroy(gbarrier);
137
138 //use the information from clEnqueueReadBuffer
139 //may have to alter later
140 for(int i = 0; i < count; i++)
141 {
142 results[i] = output[i];
143 }
144
145 correct = 0;
146 for(i = 0; i < count; i++)
147 {
148 if(results[i] == data[i] * data[i])
149 {
150 correct++;
151 }
152 }
153
154 // Print a brief summary detailing the results
155 //
156 printf("Computed '%d/%d' correct values!\n", correct, count);
157
158
159 return 0;
160}
Note: See TracBrowser for help on using the repository browser.