-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtensor.hpp
167 lines (150 loc) · 4.04 KB
/
tensor.hpp
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
#pragma once
using namespace std;
#define NandN 0
#define TandN 1
#define NandT 2
#define TandT 3
class Tensor
{
public:
float* ptr{nullptr};
int32_t d{0};
int32_t c{0};
int32_t h{0};
int32_t w{0};
int32_t size{0};
#pragma acc routine seq
float& operator[](size_t idx) {
return ptr[idx];
};
explicit Tensor() { };
//コンストラクタでもうgpu側にメモリ領域を確保してしまう
explicit Tensor(int32_t datanum, int32_t channel, int32_t height, int32_t width) {
d = datanum; c = channel; h = height; w = width;
size = datanum * channel * height * width;
try {
ptr = new float[size];
}
catch (bad_alloc) {
cerr << "Memory exhausted" << endl;
}
#pragma acc enter data copyin(this)
#pragma acc enter data create(ptr[0:size])
}
~Tensor() {
#pragma acc exit data delete(ptr[0:size])
#pragma acc exit data delete(this)
delete [] ptr;
ptr = NULL;
d = 0; c = 0; h = 0; w = 0;
size = 0;
}
inline void updateHost() {
#pragma acc update self(ptr[0:size])
}
inline void updateDev() {
#pragma acc update device(ptr[0:size])
}
void Print() {
int32_t idx = 0;
for (int32_t id=0; id < d; id++) {
cout << "data" << id << ":" << endl;
for (int32_t ic=0; ic < c; ic++) {
cout << " channel" << ic << ":" << endl;
for (int32_t ih=0; ih < h; ih++) {
cout << " ";
for (int32_t iw=0; iw < w; iw++) {
cout << ptr[idx] << " ";
idx++;
}
cout << endl;
}
}
}
}
void SetDim(int32_t datanum, int32_t channel, int32_t height, int32_t width) {
d = datanum; c = channel; h = height; w = width;
size = datanum * channel * height * width;
ptr = new float[size];
#pragma acc enter data copyin(this)
#pragma acc enter data create(ptr[0:size])
}
void Reshape(int32_t datanum, int32_t channel, int32_t height, int32_t width) {
if (size != datanum*channel*height*width) {
cout << "Element numnber does not match in Reshape method!" << endl;
return;
}
d = datanum; c = channel; h = height; w = width;
}
};
class IntTensor
{
public:
int32_t* ptr{nullptr};
int32_t d{0};
int32_t c{0};
int32_t h{0};
int32_t w{0};
int32_t size{0};
#pragma acc routine seq
int32_t& operator[](size_t idx) {
return ptr[idx];
};
explicit IntTensor() { };
//コンストラクタでもうgpu側にメモリ領域を確保してしまう
explicit IntTensor(int32_t datanum, int32_t channel, int32_t height, int32_t width) {
d = datanum; c = channel; h = height; w = width;
size = datanum * channel * height * width;
ptr = new int32_t[size];
#pragma acc enter data copyin(this)
#pragma acc enter data create(ptr[0:size])
}
~IntTensor() {
#pragma acc exit data delete(ptr[0:size])
#pragma acc exit data delete(this)
delete [] ptr;
ptr = NULL;
d = 0; c = 0; h = 0; w = 0;
size = 0;
}
inline void updateHost() {
#pragma acc update self(ptr[0:size])
}
inline void updateDev() {
#pragma acc update device(ptr[0:size])
}
void Print() {
int32_t idx = 0;
for (int32_t id=0; id < d; id++) {
cout << "data" << id << ":" << endl;
for (int32_t ic=0; ic < c; ic++) {
cout << " channel" << ic << ":" << endl;
for (int32_t ih=0; ih < h; ih++) {
cout << " ";
for (int32_t iw=0; iw < w; iw++) {
cout << ptr[idx] << " ";
idx++;
}
cout << endl;
}
}
}
}
void SetDim(int32_t datanum, int32_t channel, int32_t height, int32_t width) {
d = datanum; c = channel; h = height; w = width;
size = datanum * channel * height * width;
ptr = new int32_t[size];
#pragma acc enter data copyin(this)
#pragma acc enter data create(ptr[0:size])
}
void Reshape(int32_t datanum, int32_t channel, int32_t height, int32_t width) {
if (size != datanum*channel*height*width) {
cout << "Element numnber does not match in Reshape method!" << endl;
return;
}
d = datanum; c = channel; h = height; w = width;
}
};
void init_random(Tensor &w);
void init_zero(Tensor &w);
void init_zeroint(IntTensor &w);