forked from nest/nest-gpu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnested_loop.cu
More file actions
178 lines (155 loc) · 4.61 KB
/
Copy pathnested_loop.cu
File metadata and controls
178 lines (155 loc) · 4.61 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
/*
* This file is part of NESTGPU.
*
* Copyright (C) 2021 The NEST Initiative
*
* NESTGPU is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* NESTGPU is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with NESTGPU. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
//#include "cuda_error_nl.h"
#include "cuda_error.h"
#include "nested_loop.h"
//TMP
#include "getRealTime.h"
//
//////////////////////////////////////////////////////////////////////
// declare here the functions called by the nested loop
__device__ void NestedLoopFunction0(int ix, int iy);
__device__ void NestedLoopFunction1(int ix, int iy);
//////////////////////////////////////////////////////////////////////
namespace NestedLoop
{
PrefixScan prefix_scan_;
int *d_Ny_cumul_sum_;
}
__device__ int locate(int val, int *data, int n)
{
int i_left = 0;
int i_right = n-1;
int i = (i_left+i_right)/2;
while(i_right-i_left>1) {
if (data[i] > val) i_right = i;
else if (data[i]<val) i_left = i;
else break;
i=(i_left+i_right)/2;
}
return i;
}
__global__ void CumulSumNestedLoopKernel0(int Nx, int *Ny_cumul_sum,
int Ny_sum)
{
int blockId = blockIdx.y * gridDim.x + blockIdx.x;
int array_idx = blockId * blockDim.x + threadIdx.x;
if (array_idx<Ny_sum) {
int ix = locate(array_idx, Ny_cumul_sum, Nx + 1);
int iy = (int)(array_idx - Ny_cumul_sum[ix]);
NestedLoopFunction0(ix, iy);
}
}
__global__ void CumulSumNestedLoopKernel1(int Nx, int *Ny_cumul_sum,
int Ny_sum)
{
int blockId = blockIdx.y * gridDim.x + blockIdx.x;
int array_idx = blockId * blockDim.x + threadIdx.x;
if (array_idx<Ny_sum) {
int ix = locate(array_idx, Ny_cumul_sum, Nx + 1);
int iy = (int)(array_idx - Ny_cumul_sum[ix]);
NestedLoopFunction1(ix, iy);
}
}
//////////////////////////////////////////////////////////////////////
int NestedLoop::Init()
{
//prefix_scan_.Init();
gpuErrchk(cudaMalloc(&d_Ny_cumul_sum_,
PrefixScan::AllocSize*sizeof(int)));
return 0;
}
//////////////////////////////////////////////////////////////////////
int NestedLoop::Run(int Nx, int *d_Ny, int i_func)
{
return CumulSumNestedLoop(Nx, d_Ny, i_func);
}
//////////////////////////////////////////////////////////////////////
int NestedLoop::CumulSumNestedLoop(int Nx, int *d_Ny, int i_func)
{
//TMP
//double time_mark=getRealTime();
//
prefix_scan_.Scan(d_Ny_cumul_sum_, d_Ny, Nx+1);
//TMP
//printf("pst: %lf\n", getRealTime()-time_mark);
//
int Ny_sum;
gpuErrchk(cudaMemcpy(&Ny_sum, &d_Ny_cumul_sum_[Nx],
sizeof(int), cudaMemcpyDeviceToHost));
//printf("CSNL: %d %d\n", Nx, Ny_sum);
//printf("Ny_sum %u\n", Ny_sum);
//temporary - remove
/*
if (Ny_sum==0) {
printf("Nx %d\n", Nx);
for (int i=0; i<Nx+1; i++) {
int psum;
gpuErrchk(cudaMemcpy(&psum, &d_Ny_cumul_sum_[i],
sizeof(int), cudaMemcpyDeviceToHost));
printf("%d %d\n", i, psum);
}
}
*/
////
if(Ny_sum>0) {
int grid_dim_x, grid_dim_y;
if (Ny_sum<65536*1024) { // max grid dim * max block dim
grid_dim_x = (Ny_sum+1023)/1024;
grid_dim_y = 1;
}
else {
grid_dim_x = 32; // I think it's not necessary to increase it
if (Ny_sum>grid_dim_x*1024*65535) {
throw ngpu_exception(std::string("Ny sum ") + std::to_string(Ny_sum) +
" larger than threshold "
+ std::to_string(grid_dim_x*1024*65535));
}
grid_dim_y = (Ny_sum + grid_dim_x*1024 -1) / (grid_dim_x*1024);
}
dim3 numBlocks(grid_dim_x, grid_dim_y);
//TMP
//double time_mark=getRealTime();
//
switch (i_func) {
case 0:
CumulSumNestedLoopKernel0<<<numBlocks, 1024>>>
(Nx, d_Ny_cumul_sum_, Ny_sum);
gpuErrchk(cudaPeekAtLastError());
gpuErrchk(cudaDeviceSynchronize());
break;
case 1:
CumulSumNestedLoopKernel1<<<numBlocks, 1024>>>
(Nx, d_Ny_cumul_sum_, Ny_sum);
gpuErrchk(cudaPeekAtLastError());
gpuErrchk(cudaDeviceSynchronize());
break;
default:
throw ngpu_exception("unknown nested loop function");
}
//TMP
//printf("cst: %lf\n", getRealTime()-time_mark);
//
}
return 0;
}