-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathromberg_hybrid.cu
executable file
·216 lines (181 loc) · 5.71 KB
/
romberg_hybrid.cu
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
209
210
211
212
213
214
215
216
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <sys/time.h>
#include <math.h>
#include <omp.h>
#define warp_size 32
#define Hwarp_size 16
#define A 0
#define B 15
void checkCUDAError(const char* msg);
__host__ __device__ inline double f(double x)
{
return exp(x)*sin(x);
}
__host__ __device__ inline unsigned int getFirstSetBitPos(int n)
{
return log2((float)(n&-n))+1;
}
__global__ void rombergOMP(double a, double b, int row_size, double *omp_f)
{
extern __shared__ double local_array[];
double diff = (b-a)/gridDim.x, step;
int max_eval = (1<<(row_size-1)),k;
b = a + (blockIdx.x+1)*diff;
a += blockIdx.x*diff;
step = (b-a)/max_eval;
double local_col[25];
for(int i = 0; i < row_size; i++)
local_col[i] = 0.0;
if(!threadIdx.x)
{
k = blockDim.x;
local_col[0] = f(a) + f(b);
}
else
k = threadIdx.x;
for(; k < max_eval; k += blockDim.x)
{
local_col[row_size - getFirstSetBitPos(k)] += 2.0*f(a + step*k);
}
for(int i = 0; i < row_size; i++)
{
local_array[row_size*threadIdx.x + i] = local_col[i];
}
__syncthreads();
if(threadIdx.x < row_size)
{
double sum = 0.0;
for(int i = threadIdx.x; i < blockDim.x*row_size; i+=row_size)
sum += local_array[i];
//local_array[threadIdx.x] = sum;
omp_f[blockIdx.x*row_size + threadIdx.x] = sum;
}
}
__global__ void romberg(double a, double b, int row_size, double *result) //row_size<=25, preferably 14
{
extern __shared__ double local_array[];
double diff = (b-a)/gridDim.x, step;
int max_eval = (1<<(row_size-1)),k;
b = a + (blockIdx.x+1)*diff;
a += blockIdx.x*diff;
step = (b-a)/max_eval;
double local_col[25];
for(int i = 0; i < row_size; i++)
local_col[i] = 0.0;
if(!threadIdx.x)
{
k = blockDim.x;
local_col[0] = f(a) + f(b);
}
else
k = threadIdx.x;
for(; k < max_eval; k += blockDim.x)
{
local_col[row_size - getFirstSetBitPos(k)] += 2.0*f(a + step*k);
}
for(int i = 0; i < row_size; i++)
{
local_array[row_size*threadIdx.x + i] = local_col[i];
}
__syncthreads();
if(threadIdx.x < row_size)
{
double sum = 0.0;
for(int i = threadIdx.x; i < blockDim.x*row_size; i+=row_size)
sum += local_array[i];
local_array[threadIdx.x] = sum;
}
if(!threadIdx.x)
{
double *romberg_table = local_col;
romberg_table[0] = local_array[0];
for(int k = 1; k < row_size; k++)
romberg_table[k] = romberg_table[k-1] + local_array[k];
for(int k = 0; k < row_size; k++)
romberg_table[k]*= (b-a)/(1<<(k+1));
for(int col = 0 ; col < row_size-1 ; col++)
{
for(int row = row_size-1; row > col; row--)
{
romberg_table[row] = romberg_table[row] + (romberg_table[row] - romberg_table[row-1])/((1<<(2*col+1))-1);
}
}
result[blockIdx.x] = romberg_table[row_size-1];
}
}
int main( int argc, char** argv)
{
double *h_omp_f,*d_omp_f,*d_result, *h_result,sum=0.0,ompA,ompB = B,gpuA = A,gpuB;
int numBlocks = 128,numBlocksOMP, numThreadsPerBlock = 64, row_size = 13, max_eval = (1<<(row_size-1)), core = 6;
double my_sum[6] = {0.0};
cudaDeviceSetCacheConfig(cudaFuncCachePreferShared);
cudaStream_t streams[2];
omp_set_num_threads(core);
numBlocksOMP = numBlocks/4; //TODO : hyperparameter
gpuB = B/4; //TODO : hyperparameter
ompA = gpuB;
cudaMalloc( (void **) &d_result, numBlocks*sizeof(double) );
cudaHostAlloc( (void**)&d_omp_f, numBlocksOMP*row_size*sizeof(double), cudaHostAllocDefault );
//cudaMalloc( (void **) &d_omp_f, numBlocksOMP*row_size*sizeof(double) );
h_result = new double[numBlocks];
h_omp_f = new double[numBlocksOMP*row_size];
timeval t;
double t1,t2;
cudaStreamCreate(&streams[0]);
cudaStreamCreate(&streams[1]);
gettimeofday(&t, NULL);
t1 = t.tv_sec*1000.0 + (t.tv_usec/1000.0);
rombergOMP<<< numBlocksOMP, numThreadsPerBlock, row_size*numThreadsPerBlock*sizeof(double),streams[0] >>>(ompA,ompB,row_size,d_omp_f);
cudaStreamSynchronize (streams[0]);
//cudaThreadSynchronize();
cudaMemcpyAsync( h_omp_f, d_omp_f, numBlocksOMP*row_size*sizeof(double), cudaMemcpyDeviceToHost, streams[0] );
romberg<<< numBlocks-numBlocksOMP, numThreadsPerBlock, row_size*numThreadsPerBlock*sizeof(double), streams[1] >>>(gpuA,gpuB,row_size,d_result);
#pragma omp parallel for default(shared) schedule(static)
for(int p = 0; p < numBlocksOMP; p++)
{
//double t3; timeval _t;
//gettimeofday(&_t, NULL);
//t3 = _t.tv_sec*1000.0 + (_t.tv_usec/1000.0);
double romberg_table[row_size];
romberg_table[0] = h_omp_f[p*row_size];
for(int k = 1; k < row_size; k++)
romberg_table[k] = romberg_table[k-1] + h_omp_f[p*row_size + k];
for(int k = 0; k < row_size; k++)
romberg_table[k] *= ((ompB-ompA)/numBlocksOMP)/(1<<(k+1));
for(int col = 0 ; col < row_size-1 ; col++)
{
for(int row = row_size-1; row > col; row--)
{
romberg_table[row] = romberg_table[row] + (romberg_table[row] - romberg_table[row-1])/((1<<(2*col+1))-1);
}
}
my_sum[omp_get_thread_num()] += romberg_table[row_size-1];
//gettimeofday(&_t, NULL);
//t3 = _t.tv_sec*1000.0 + (_t.tv_usec/1000.0) - t3;
//printf("p = %d, time = %lf ms\n",p,t3);
}
cudaThreadSynchronize();
gettimeofday(&t, NULL);
t2 = t.tv_sec*1000.0 + (t.tv_usec/1000.0);
checkCUDAError("kernel invocation");
cudaMemcpy( h_result, d_result, numBlocks*sizeof(double), cudaMemcpyDeviceToHost );
checkCUDAError("memcpy");
//for(int k = 0; k<(max_eval+1)*numBlocks; k++ )
// printf("%lf\t",h_result[k]);
for(int k=0;k<numBlocksOMP;k++)
sum+=my_sum[k];
for(int k=0;k<numBlocks-numBlocksOMP;k++)
sum+=h_result[k];
printf("TIME : %lf ms with ans = %lf\n\n\n",t2-t1,sum);
}
void checkCUDAError(const char *msg)
{
cudaError_t err = cudaGetLastError();
if( cudaSuccess != err)
{
fprintf(stderr, "Cuda error: %s: %s.\n", msg, cudaGetErrorString( err) );
exit(EXIT_FAILURE);
}
}