-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsplit.cu
144 lines (121 loc) · 3.78 KB
/
split.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
/*
Parallel split kernels
*/
#include <iostream>
#include <cstdio>
#include <cuda_runtime.h>
#include <device_launch_parameters.h>
#include <cstdlib>
#include <ctime>
#include <helper_cuda.h>
#include <thrust/scan.h>
#include <thrust/device_ptr.h>
#include <thrust/device_vector.h>
using namespace std;
__device__
int getPartID(int element)
{
return element % 2;
}
__global__
void mapPart(int *d_R,int *d_pidArray,int r_len)
{
int threadId = blockIdx.x * blockDim.x + threadIdx.x;
int threadNumber = blockDim.x * gridDim.x;
while(threadId < r_len)
{
d_pidArray[threadId] = getPartID(d_R[threadId]);
threadId += threadNumber;
}
}
__global__
void count_Hist(int *d_Hist,int *d_pidArray,int r_len, int numPart)
{
__shared__ int s_Hist[2048];
int threadId = blockIdx.x * blockDim.x + threadIdx.x;
int threadNumber = blockDim.x * gridDim.x;
int offset = threadIdx.x * numPart;
for(int i = 0; i < numPart; ++i)
s_Hist[i + offset] = 0;
for(int i = threadId; i < r_len; i += threadNumber)
s_Hist[offset + d_pidArray[i]]++;
for(int i = 0; i < numPart; ++i)
d_Hist[i * threadNumber + threadId] = s_Hist[offset + i];
}
__global__
void write_Hist(int d_pidArray[],int d_psSum[],int d_loc[],int numPart,int r_len)
{
__shared__ int s_psSum[2048];
int threadId = threadIdx.x + blockIdx.x * blockDim.x;
int threadNumber = gridDim.x * blockDim.x;
int offset = threadIdx.x * numPart;
for(int i = 0; i < numPart; ++i)
s_psSum[i + offset] = d_psSum[threadId + i * threadNumber];
for(int i = threadId; i < r_len; i += threadNumber)
{
int pid = d_pidArray[i];
d_loc[i] = s_psSum[pid + offset];
s_psSum[pid+ offset]++;
}
}
__global__
void scatter(int d_Rin[],int d_Rout[],int d_loc[],int r_len)
{
int threadId = threadIdx.x + blockIdx.x * blockDim.x;
int threadNumber = blockDim.x * gridDim.x;
while(threadId < r_len)
{
d_Rout[d_loc[threadId]] = d_Rin[threadId];
threadId += threadNumber;
}
}
int main()
{
int *d_R,*d_result,*d_pidArray,*d_loc,*d_Hist,*d_psSum;
int *h_R,*h_result;
int r_len = 1024 * 1024;
int numPart = 2; //even or odd
dim3 grid(256);
dim3 block(512);
int numThread = grid.x * block.x;
int Hist_len = numThread * numPart;
h_R = (int*) malloc(sizeof(int) * r_len);
h_result = (int*) malloc(sizeof(int) * r_len);
checkCudaErrors(cudaMalloc(&d_R,sizeof(int) * r_len));
checkCudaErrors(cudaMalloc(&d_result,sizeof(int) * r_len));
checkCudaErrors(cudaMalloc(&d_pidArray,sizeof(int) * r_len));
checkCudaErrors(cudaMalloc(&d_loc,sizeof(int) * r_len));
checkCudaErrors(cudaMalloc(&d_Hist,sizeof(int) * Hist_len));
checkCudaErrors(cudaMalloc(&d_psSum,sizeof(int) * Hist_len));
srand(0);
for(int i = 0; i < r_len; ++i)
h_R[i] = rand() % 2;
cudaMemcpy(d_R,h_R,sizeof(int) * r_len, cudaMemcpyHostToDevice);
mapPart<<<grid,block>>>(d_R,d_pidArray,r_len);
count_Hist<<<grid,block>>>(d_Hist,d_pidArray,r_len,numPart);
thrust::device_ptr<int> dev_Hist(d_Hist);
thrust::device_ptr<int> dev_psSum(d_psSum);
thrust::exclusive_scan(dev_Hist,dev_Hist + Hist_len,dev_psSum);
write_Hist<<<grid,block>>>(d_pidArray,d_psSum,d_loc,numPart,r_len);
scatter<<<grid,block>>>(d_R,d_result,d_loc,r_len);
cudaMemcpy(h_result,d_result,sizeof(int) * r_len,cudaMemcpyDeviceToHost);
freopen("before.txt","w",stdout);
for(int i = 0; i < r_len; ++i)
printf("%d %d\n",i,h_R[i]);
fclose(stdout);
freopen("/dev/tty/","w",stdout);
freopen("after.txt","w",stdout);
for(int i = 0; i < r_len; ++i)
printf("%d %d\n",i,h_result[i]);
fclose(stdout);
freopen("/dev/tty","w",stdout);
free(h_R);
free(h_result);
checkCudaErrors(cudaFree(d_R));
checkCudaErrors(cudaFree(d_result));
checkCudaErrors(cudaFree(d_pidArray));
checkCudaErrors(cudaFree(d_loc));
checkCudaErrors(cudaFree(d_Hist));
checkCudaErrors(cudaFree(d_psSum));
return 0;
}