-
Notifications
You must be signed in to change notification settings - Fork 70
/
DevFunc.cu.bak
277 lines (245 loc) · 6.27 KB
/
DevFunc.cu.bak
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "DevFunc.h"
#include <stdlib.h>
__global__ void kernBinary(int n, float* in_vec, float* rand_vec)
{
int i = (blockIdx.x * blockDim.x) + threadIdx.x;
if (i < n)
{
if(in_vec[i] > rand_vec[i])
{
in_vec[i] = 1.0f;
}
else
{
in_vec[i] = 0.0f;
}
}
}
__global__ void kernWeightMultiP( int n, float p, float* in_vec )
{
// int i = (blockIdx.x * blockDim.x) + threadIdx.x;
// int j = (blockIdx.y * blockDim.y) + threadIdx.y;
// if(i < prev_n&& j < cur_n)
// {
// in_vec[i+cur_n*j] = in_vec[i+cur_n*j]*p;
// }
int i = (blockIdx.x * blockDim.x) + threadIdx.x;
if( i < n )
{
in_vec[i]=in_vec[i]*p;
}
}
__global__ void kernDropout(int n, float p ,float* in, float* rand_vec)
{
int i = (blockIdx.x * blockDim.x) + threadIdx.x;
if(i < n)
{
if(rand_vec[i]<p)
{
in[i]=0;
}
}
}
__global__ void kernSigmoid(int n, float* in_vec, float* out_vec)
{
int i = (blockIdx.x * blockDim.x) + threadIdx.x;
if (i < n)
out_vec[i] = 1.0f/(1.0f + expf(- in_vec[i]));
}
__global__ void kernDsigmoid(int n, float* in_vec, float* out_vec)
{
int i = (blockIdx.x * blockDim.x) + threadIdx.x;
if (i<n)
{
const float y = in_vec[i];
out_vec[i] = (1.0f - y) * y;
}
}
__global__ void kernSoftmax(int rows, int cols, float* in_vec, float* out_vec)
{
int row = (blockIdx.x * blockDim.x) + threadIdx.x;
if (row < rows)
{
int i;
const int index = row * cols;
const float* invec = &in_vec[index];
float* outvec = &out_vec[index];
const float* inptr;
float* outptr;
// First find the max of each vector
float max;
inptr = invec;
max = *inptr++;
for (i=cols-1; i!=0; i--)
{
float val;
val = *inptr++;
if (val>max)
max = val;
}
// Now put exp(in-max) in out
inptr = invec;
outptr = outvec;
float sumexp = 0;
for (i=cols; i!=0; i--)
{
float f, e;
f = *inptr++;
e = expf(f - max);
*outptr++ = e;
sumexp += e;
}
// Now scale the output
float scale = 1.0f/sumexp;
outptr = outvec;
for (i=cols; i!=0; i--)
{
*outptr = (*outptr) * scale;
outptr++;
}
}
}
__global__ void kernLinearOutCopy(int rows, int cols, float* in_vec, float* out_vec)
{
int row = (blockIdx.x * blockDim.x) + threadIdx.x;
if (row < rows)
{
//int i; //xuyong
//const int index = row * cols;
//const float* invec = &in_vec[index];
//float* outvec = &in_vec[index];
////////////////////////////////////////////////////
int j;
for(j =0; j< cols;j++)
out_vec[cols *row +j] = in_vec[cols *row +j];
}
}
__global__ void kernMultiCopy(int mat_height, int vec_len,
float* vec, float* mat)
{
int col = (blockIdx.x * blockDim.x) + threadIdx.x;
if (col < vec_len)
{
int j;
float val = vec[col];
float* top = &mat[col];
for (j=mat_height; j!=0; j--)
{
*top = val;
top += vec_len;
}
}
}
__global__ void kernSumcol(int rows, int cols, float* in, float* res)
{
int col = (blockIdx.x * blockDim.x) + threadIdx.x;
if (col < cols)
{
int j;
const float* fromp = &in[col];
float* top = &res[col];
(*top) = (*fromp);
fromp +=cols;
for (j=rows-1; j!=0; j--)
{
(*top) += (*fromp);
fromp+=cols;
}
}
}
__global__ void kernAccSumcol(int rows, int cols, float* in, float* res, float alpha, float beta)
{
int col = (blockIdx.x * blockDim.x) + threadIdx.x;
if (col < cols)
{
int j;
const float* fromp = &in[col];
float* top = &res[col];
(*top) = (*top) *alpha + beta *(*fromp);
fromp +=cols;
for (j=rows-1; j!=0; j--)
{
(*top) += beta *(*fromp);
fromp+=cols;
}
}
}
__global__ void kernAccSumrow(int rows, int cols, float* in, float* res, float alpha, float beta)
{
int row = (blockIdx.x * blockDim.x) + threadIdx.x;
if (row < rows)
{
int j;
const float* fromp = &in[row];
float* top = &res[row];
(*top) = (*top) *alpha + beta *(*fromp);
fromp +=rows;
for (j= cols -1; j!=0; j--)
{
(*top) += beta *(*fromp);
fromp += rows;
}
}
}
__global__ void kernVecMul(int n, float* in_vec1, float* in_vec2, float* out_vec)
{
int i = (blockIdx.x * blockDim.x) + threadIdx.x;
if (i<n)
out_vec[i] = in_vec1[i] * in_vec2[i];
}
//__global__ void kernSubIndex( int rows , int cols, const float *in_vec1, const int *in_index, float *res_vec)
__global__ void kernSubClean( int rows , int cols, const float *in_vec1, const float *in_clean, float *res_vec)
{
int i = (blockIdx.x * blockDim.x) + threadIdx.x;
if(i < rows)
{
int j;
for(j =0; j< cols;j++)
{ //res_vec[cols *i +j] = in_vec1[cols *i +j];
//int ind = in_index[i];
//res_vec[cols *i + ind] = in_vec1[cols *i +ind] - 1.0f;
res_vec[cols *i + j] = (2.0f/rows)*(in_vec1[cols *i +j]-in_clean[cols *i +j]);
//res_vec[cols *i + j] = 2.0f*(in_vec1[cols *i +j]-in_clean[cols *i +j]);
//printf("in kernSubClean, res_vec=%f ",res_vec[cols *i + j]);
}
}
}
__global__ void kernAccSum(int n, float* in, float* res, float beta)
{
int i = (blockIdx.x * blockDim.x) + threadIdx.x;
if(i <n)
{
res[i] = in[i] + beta *res[i];
}
}
//__global__ void kernGetMaxIndex(int rows, int cols, float* invec, int* outvec)
//{
// int i = (blockIdx.x * blockDim.x) + threadIdx.x;
// if(i < cols)
// {
// float *p = invec + rows * i;
// int maxinx = 0;
// float max = *p;
// for(int j=1;j< rows;j++)
// {
// if(p[j] > max)
// {
// max = p[j];
// maxinx = j;
// }
// }
// outvec[i] = maxinx;
// }
//}
__global__ void kernDivide(int n, float* in_vec, float* out_vec,float beta)
{
int i = (blockIdx.x * blockDim.x) + threadIdx.x;
if (i < n)
out_vec[i] = in_vec[i]/beta;
}
__global__ void kernUpdatedelta(int size, float* delta, float* weights, float* gradient, int n, float momentum, float lr, float weightcost)
{
int i = (blockIdx.x * blockDim.x) + threadIdx.x;
if (i < size)
delta[i] = momentum * delta[i] - lr * (gradient[i] / n + weightcost * weights[i]);
}