This repository has been archived by the owner on Aug 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 432
/
test.cu
79 lines (75 loc) · 2.27 KB
/
test.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
#include "test.h"
using namespace mshadow;
int main() {
InitTensorEngine<cpu>();
InitTensorEngine<gpu>();
Tensor<cpu, 3, float> tc = NewTensor<cpu, float>(Shape3(3, 2, 4), 0.0f);
Tensor<gpu, 3, float> tg = NewTensor<gpu, float>(tc.shape_, 0.0f);
// init
for (index_t i = 0; i < tc.size(0); ++i) {
for (index_t j = 0; j < tc.size(1); ++j) {
for (index_t k = 0; k < tc.size(2); ++k) {
tc[i][j][k] = i * 0.1f + j * 0.2f + k * 0.1f;
}
}
}
Copy(tg, tc);
// print
printf("\n#print batch 0 of cpu tensor:\n");
Print2DTensor(tc[0]);
printf("\n");
Print2DTensor(tc[1]);
printf("\n");
Print2DTensor(tc[2]);
// check
if (Check2DTensor(tg[1], tc[1])) {
printf("batch 1 of gpu & cpu tensor are same.\n");
}
// sum of row
Tensor<cpu, 1, float> tmp_tc = NewTensor<cpu, float>(Shape1(tc[0].size(1)), 0.0f);
Tensor<gpu, 1, float> tmp_tg = NewTensor<gpu, float>(Shape1(tg[0].size(1)), 0.0f);
printf("\n#sum_rows of batch 0:\n");
tmp_tc = sum_rows(tc[0]);
tmp_tg = sum_rows(tg[0]);
Print1DTensor(tmp_tc);
if (Check1DTensor(tmp_tg, tmp_tc)) {
printf("cpu & gpu result consists\n");
}
FreeSpace(&tmp_tc);
FreeSpace(&tmp_tg);
// sumall_except_dim
printf("\n#sumall_except_dim<0> of batch 0:\n");
Tensor<cpu, 1, float> red_tc = NewTensor<cpu, float>(Shape1(tc.size(0)), 0.0f);
Tensor<gpu, 1, float> red_tg = NewTensor<gpu, float>(Shape1(tg.size(0)), 0.0f);
red_tc = sumall_except_dim<0>(tc);
red_tg = sumall_except_dim<0>(tg);
Print1DTensor(red_tc);
if (Check1DTensor(red_tg, red_tc)) {
printf("cpu & gpu result consists\n");
}
FreeSpace(&red_tc);
FreeSpace(&red_tg);
// softmax
printf("\n#Softmax\n");
Tensor<cpu, 2, float> sm_tc = NewTensor<cpu, float>(tc[0].shape_, 0.0f);
Tensor<gpu, 2, float> sm_tg = NewTensor<gpu, float>(tg[0].shape_, 0.0f);
Softmax(sm_tc, tc[0]);
Softmax(sm_tg, tg[0]);
if (Check2DTensor(sm_tg, sm_tc)) {
printf("cpu & gpu result consists\n");
}
// mirror
printf("\n#mirror\n");
sm_tc = mirror(tc[0]);
sm_tg = mirror(tg[0]);
if (Check2DTensor(sm_tg, sm_tc)) {
printf("cpu & gpu result consists\n");
}
FreeSpace(&sm_tc);
FreeSpace(&sm_tg);
// reshape
FreeSpace(&tc);
FreeSpace(&tg);
ShutdownTensorEngine<cpu>();
ShutdownTensorEngine<gpu>();
}