-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstd_dev.cu
109 lines (104 loc) · 3.06 KB
/
std_dev.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
%%cu
#include<iostream>
#include<cuda.h>
#include<vector>
#include<math.h>
using namespace std;
void seq(vector<double>a, int n){
double avg=0;
for(int i=0;i<n;i++){
avg+=a[i];
}
avg/=n;
// cout<<avg<<"\n";
double stdv=0;
for(int i=0;i<n;i++){
stdv+=(a[i]-avg)*(a[i]-avg);
}
stdv/=n;
stdv=sqrt(stdv);
cout<<stdv<<"\n";
}
__global__ void summ(double *d_in,double *d_out, int n){
extern __shared__ double shared_mem[];
int actind = blockIdx.x*blockDim.x+threadIdx.x;
int ind = threadIdx.x;
if(actind<n){
shared_mem[ind] = d_in[actind];
}else shared_mem[ind] = 0;
__syncthreads();
for(int steps=blockDim.x/2;steps>0;steps=steps/2){
if(ind<steps){
shared_mem[ind]+=shared_mem[ind+steps];
}
__syncthreads();
}
d_out[blockIdx.x] = shared_mem[0];
}
__global__ void subb(double *d_in, int n, double avg){
int actind = blockIdx.x*blockDim.x+threadIdx.x;
if(actind<n){
d_in[actind] = (d_in[actind]-avg)*(d_in[actind]-avg);
}
}
void parll(vector<double>a, int n){
double *d_in, *d_out;
double h_in[n];
copy(a.begin(),a.end(),h_in);
cudaMalloc(&d_in,sizeof(double)*n);
int blocks = 1+(n-1)/256;
cudaMalloc(&d_out,sizeof(double)*blocks);
cudaMemcpy(d_in,h_in,sizeof(double)*n,cudaMemcpyHostToDevice);
int sz=n;
while(blocks>1){
summ<<<blocks,256,sizeof(double)*256>>>(d_in,d_out,sz);
cudaFree(d_in);
cudaMalloc(&d_in,sizeof(double)*blocks);
cudaMemcpy(d_in,d_out,sizeof(double)*blocks,cudaMemcpyDeviceToDevice);
sz=blocks;
blocks=1+(sz-1)/256;
cudaFree(d_out);
cudaMalloc(&d_out,sizeof(double)*blocks);
}
summ<<<1,256,sizeof(double)*256>>>(d_in,d_out,sz);
double h_out[1];
cudaMemcpy(h_out,d_out,1*sizeof(double),cudaMemcpyDeviceToHost);
double avg=h_out[0]/n;
cudaFree(d_in);
cudaFree(d_out);
cudaMalloc(&d_in,sizeof(double)*n);
cudaMemcpy(d_in,h_in,sizeof(double)*n,cudaMemcpyHostToDevice);
subb<<<1+(n-1)/256,256>>>(d_in,n,avg);
double fin[n];
cudaMemcpy(fin,d_in,sizeof(double)*n,cudaMemcpyDeviceToHost);
cudaFree(&d_in);
cudaMalloc(&d_in,sizeof(double)*n);
blocks = 1+(n-1)/256;
cudaMalloc(&d_out,sizeof(double)*blocks);
cudaMemcpy(d_in,fin,sizeof(double)*n,cudaMemcpyHostToDevice);
sz=n;
while(blocks>1){
summ<<<blocks,256,sizeof(double)*256>>>(d_in,d_out,sz);
cudaFree(d_in);
cudaMalloc(&d_in,sizeof(double)*blocks);
cudaMemcpy(d_in,d_out,sizeof(double)*blocks,cudaMemcpyDeviceToDevice);
sz=blocks;
blocks=1+(sz-1)/256;
cudaFree(d_out);
cudaMalloc(&d_out,sizeof(double)*blocks);
}
summ<<<1,256,sizeof(double)*256>>>(d_in,d_out,sz);
cudaMemcpy(h_out,d_out,1*sizeof(double),cudaMemcpyDeviceToHost);
double ans=sqrt(h_out[0]/n);
cout<<ans<<"\n";
}
int main(){
srand(time(NULL));
int n=100000;
vector<double>a(n);
for(int i=0;i<n;i++){
a[i]=rand()%100;
}
seq(a,n);
parll(a,n);
}