-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdnn_kernel.cpp
More file actions
executable file
·166 lines (153 loc) · 2.97 KB
/
dnn_kernel.cpp
File metadata and controls
executable file
·166 lines (153 loc) · 2.97 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
#include "dnn_kernel.h"
#include <math.h>
#include <immintrin.h>
#include <pthread.h>
#include <mpi.h>
#include <omp.h>
#define OMP_THREADS 10
#define PACK 8
typedef __m256 data_t;
int rank;//MPI Global Variable
extern "C" int setmatY(float *Y, float *B, int row, int col)
{
data_t* a = (data_t*) Y;
data_t* b = (data_t*) B;
int idx;
omp_set_num_threads(OMP_THREADS);
#pragma omp parallel for
for(int i=0; i<row; i++)
{
for(int j=0; j<col/PACK; j++)
{
idx = i*col + j;
a[idx] = b[j];
}
}
return 0;
}
extern "C" int sigmoidY(float *Y, int row, int col)
{
//data_t* a = (data_t*) Y;
omp_set_num_threads(OMP_THREADS);
#pragma omp parallel for
for(int i = 0;i < row*col/PACK; ++i)
{
Y[i] = 1.0f/(1.0f + expf(-Y[i]));
}
}
extern "C" int softmaxZ(float* in_vec, float* out_vec, int row, int col)
{
data_t* a = (data_t*) in_vec;
data_t* b = (data_t*) out_vec;
int idx, base;
float max, tmp;
float sumexp = 0.0f;
omp_set_num_threads(OMP_THREADS);
#pragma omp parallel for
for(int i=0; i<row; i++)
{
base = i*col + 0;
max = in_vec[base];
for(int j=1; j<col; j++)
{
idx = i*col + j;
if(in_vec[idx] > max)
{
max = in_vec[idx];
}
}
sumexp = 0.0f;
for(int j=0; j<col; j++)
{
idx = i*col + j;
tmp = expf(in_vec[idx] - max);
sumexp += tmp;
in_vec[idx] = tmp;
}
tmp = 1.0f/sumexp;
for(int j=0; j<col; j++)
{
idx = i*col + j;
out_vec[idx] = in_vec[idx] * tmp;
}
}
}
extern "C" int errorTrans(float *E, float *Y, int row, int col)
{
data_t* a = (data_t*) E;
data_t* b = (data_t*) Y;
float tmp;
int idx;
omp_set_num_threads(OMP_THREADS);
#pragma omp parallel for
for(int i=0; i<row; i++)
{
for(int j=0; j<col/PACK; j++)
{
idx = i*col + j;
tmp = b[idx];
a[idx] = a[idx] * tmp * (1-tmp);
}
}
return 0;
}
extern "C" int errorOutput(float *E, float *Z, int *T, int row, int col)
{
data_t* a = (data_t*) E;
data_t* b = (data_t*) Z;
data_t* c = (data_t*) T;
float tmp;
int idx;
omp_set_num_threads(OMP_THREADS);
#pragma omp parallel for
for(int i=0; i<row; i++)
{
//Make sure to pack j into vector to compare to c
for(int j=0; j<col/PACK; j++)
{
idx = i*col + j;
tmp = (j == c[i]) ? 1.0f:0.0f;
E[idx] = Z[idx] - tmp;
}
}
return 0;
}
extern "C" int updateW(float *W, float *Wdelta, int row, int col)
{
int idx;
data_t* a = (data_t*) W;
data_t* b = (data_t*) Wdelta;
omp_set_num_threads(OMP_THREADS);
#pragma omp parallel for
for(int i=0; i<row; i++)
{
for(int j=0; j<col/PACK; j++)
{
idx = i*col + j;
a[idx] += b[idx];
}
}
return 0;
}
extern "C" int updateB(float *E, float *B, float *Bdelta, int row, int col, float alpha)
{
int idx;
data_t* a = (data_t*) E;
data_t* b = (data_t*) B;
data_t* c = (data_t*) Bdelta;
float sum = 0.0f;
omp_set_num_threads(OMP_THREADS);
#pragma omp parallel for
for(int i=0; i<col/PACK; i++)
{
sum = 0.0;
for(int j=0; j<row; j++)
{
idx = j*col + i;
sum += a[idx];
}
c[i] = alpha * sum;
b[i] += c[i];
}
return 0;
}