-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstencilcpu.c
More file actions
209 lines (157 loc) · 4.92 KB
/
stencilcpu.c
File metadata and controls
209 lines (157 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <CL/opencl.h>
#include <pthread.h>
#include <omp.h>
#include "constantes.h"
#define TIME_DIFF(t1, t2) \
((t2.tv_sec - t1.tv_sec) * 1000000 + (t2.tv_usec - t1.tv_usec))
#define MAX_DEVICES 5
#define error(...) do { fprintf(stderr, "Error: " __VA_ARGS__); exit(EXIT_FAILURE); } while(0)
#define check(err, ...) \
do { \
if(err != CL_SUCCESS) { \
fprintf(stderr, "(%d) Error: " __VA_ARGS__, err); \
exit(EXIT_FAILURE); \
} \
} while(0)
//
//float optimizer;
//struct timeval tcpu,tgpu;
//unsigned int ydim_gpu = YDIM_GPU;
//
/* Version CPU multicoeur */
void stencil_multi(float* B, const float* A, int ydim)
{
#pragma omp parallel for schedule(guided)
for(int y=0; y<ydim; y++)
for(int x=0; x<XDIM; x++)
B[y*LINESIZE + x] = 0.75*A[y*LINESIZE + x] +
0.25*( A[y*LINESIZE + x - 1] + A[y*LINESIZE + x + 1] +
A[(y-1)*LINESIZE + x] + A[(y+1)*LINESIZE + x]);
}
/* Version CPU pour comparaison */
void stencil(float* B, const float* A, int ydim)
{
for(int y=0; y<ydim; y++)
for(int x=0; x<XDIM; x++)
B[y*LINESIZE + x] = 0.75*A[y*LINESIZE + x] +
0.25*( A[y*LINESIZE + x - 1] + A[y*LINESIZE + x + 1] +
A[(y-1)*LINESIZE + x] + A[(y+1)*LINESIZE + x]);
}
struct timeval temps1, temps2;
/* fonction appelée par le thread dès sa création */
void* calcul_cpu(void* p)
{
struct double_matrice* container = (struct double_matrice*) p;
stencil_multi(container->out, container->in, container->ydim_cpu);
gettimeofday(&temps1,NULL);
return NULL;
}
int ydim_gpu = 512;
int main(int argc, char** argv)
{
int numIterations = NB_ITER;
if(argc > 0)
{
numIterations = atoi(argv[1]);
ydim_gpu = atoi(argv[2]);
}
const unsigned int line_size = LINESIZE;
const unsigned int mem_size= TOTALSIZE*sizeof(float);
const unsigned int mem_size_gpu = SIZE_GPU * sizeof(float);
float *h_idata = NULL;
float *h_odata = NULL;
struct double_matrice container;
struct timeval tv1,tv2,tcpu1,tcpu2;
// Allocation of input & output matrices
//
h_idata = malloc(mem_size);
h_odata = malloc(mem_size);
container.in = h_idata + LINESIZE * (YDIM_GPU) + OFFSET;
container.out = h_odata + LINESIZE * (YDIM_GPU) + OFFSET;
container.ydim_cpu = YDIM_CPU;
// Initialization of input & output matrices
//
srand(1234);
for(unsigned int i = 0; i < TOTALSIZE; i++)
{
h_idata[i]=rand();
h_odata[i]=0.0;
}
/* Version cpu pour comparaison */
void * tmp_switch;
float* reference = (float*) malloc(mem_size);
float* reference_i = (float*) malloc(mem_size);
for(unsigned int i = 0; i < TOTALSIZE; i++)
{
reference[i] = 0.0;
reference_i[i] = h_idata[i];
}
gettimeofday(&tcpu1,NULL);
for(int i=0; i<numIterations; ++i)
{
stencil(reference + OFFSET, reference_i + OFFSET, YDIM);
tmp_switch = reference;
reference = reference_i;
reference_i = tmp_switch;
}
if(numIterations%2)
{
tmp_switch = reference;
reference = reference_i;
reference_i = tmp_switch;
}
gettimeofday(&tcpu2,NULL);
float timecpu=((float)TIME_DIFF(tcpu1,tcpu2)) / 1000;
pthread_t thread;
printf("nombre d'itérations: %d\n",numIterations);
gettimeofday(&tv1, NULL);
for(int i = 0; i<numIterations; i++) // Iterations are done inside the kernel
{
stencil_multi(container.out,container.in,container.ydim_cpu);
tmp_switch = container.out;
container.out = container.in;
container.in = tmp_switch;
}
gettimeofday(&tv2, NULL);
tmp_switch = d_odata;
d_odata = d_idata;
d_idata = tmp_switch;
float time1=((float)TIME_DIFF(tv1,tv2)) / 1000;
// Read back the results from the device to verify the output
//
printf("%f\t%f ms (%fGo/s)\t%f ms (%fGo/s)\n", timecpu/time1,
time1, numIterations * 3*mem_size / time1 / 1000000,
timecpu, numIterations * 3*mem_size / timecpu / 1000000);
// Validate our results
//
unsigned int errors=0;
float * h_fdata = h_odata;
for(unsigned int i=0; i<TOTALSIZE; i++)
{
if((reference[i]-h_fdata[i])/reference[i] > 1e-6)
{
if(errors < 10) printf(" %u %f vs %f\n", i, h_fdata[i], reference[i]);
errors++;
}
}
if(errors)
fprintf(stderr,"%d erreurs !\n", errors);
else
fprintf(stderr,"pas d'erreurs, cool !\n");
free(reference_i);
free(reference);
// Shutdown and cleanup
//
free(h_odata);
free(h_idata);
return 0;
}