harmonize-python 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,287 @@
1
+
2
+ #ifndef HARMONIZE_UTIL_HOST
3
+ #define HARMONIZE_UTIL_HOST
4
+
5
+ namespace util {
6
+
7
+ namespace host {
8
+
9
+
10
+ static bool check_error(){
11
+
12
+ adapt::GPUrtError_t status = adapt::GPUrtGetLastError();
13
+
14
+ if(status != adapt::GPUrtSuccess){
15
+ const char* err_str = adapt::GPUrtGetErrorString(status);
16
+ printf("ERROR: \"%s\"\n",err_str);
17
+ }
18
+
19
+ return (status != adapt::GPUrtSuccess);
20
+
21
+ }
22
+
23
+
24
+
25
+
26
+ static void auto_throw(adapt::GPUrtError_t status){
27
+ if ( status != adapt::GPUrtSuccess ) {
28
+ std::string message = "GPU Runtime Error: ";
29
+ message += adapt::GPUrtGetErrorString(status);
30
+ throw std::runtime_error(message);
31
+ }
32
+ }
33
+
34
+
35
+ template<typename T>
36
+ T* hardMalloc(size_t size){
37
+ T* result;
38
+ auto_throw( adapt::GPUrtMalloc(&result, sizeof(T)*size) );
39
+ return result;
40
+ }
41
+
42
+
43
+
44
+ template<typename T>
45
+ class DevBuf {
46
+
47
+ protected:
48
+
49
+ struct Inner {
50
+
51
+ size_t size;
52
+ T *adr;
53
+
54
+ Inner(T *adr_val, size_t size_val)
55
+ : adr (adr_val )
56
+ , size(size_val)
57
+ {
58
+ #ifdef HARMONIZE_ALLOC_DEBUG
59
+ printf("Allocated array of type '%s' at %p, with size %zu\n",typeid(*adr).name(),adr,size);
60
+ #endif
61
+ }
62
+
63
+ ~Inner() {
64
+ if ( adr != NULL) {
65
+ #ifdef HARMONIZE_ALLOC_DEBUG
66
+ printf("Freed array of type '%s' at %p, with size %zu\n",typeid(*adr).name(),adr,size);
67
+ #endif
68
+ auto _ = adapt::GPUrtFree(adr);
69
+ }
70
+ }
71
+
72
+ };
73
+
74
+ std::shared_ptr<Inner> inner;
75
+
76
+ public:
77
+
78
+
79
+ operator T*&() { return inner->adr; }
80
+
81
+ __host__ void resize(size_t s){
82
+ T* new_adr = hardMalloc<T>(s);
83
+ size_t copy_count = ( s < inner->size ) ? s : inner->size;
84
+ auto_throw( adapt::GPUrtMemcpy(
85
+ new_adr,
86
+ inner->adr,
87
+ sizeof(T)*copy_count,
88
+ adapt::GPUrtMemcpyDeviceToDevice
89
+ ) );
90
+ if( inner->adr != NULL ) {
91
+ auto_throw( adapt::GPUrtFree(inner->adr) );
92
+ #ifdef HARMONIZE_ALLOC_DEBUG
93
+ printf("Freed (for resize) array of type '%s' at %p, with size %zu\n",typeid(*(inner->adr)).name(),inner->adr,inner->size);
94
+ #endif
95
+ }
96
+ inner->size = s;
97
+ inner->adr = new_adr;
98
+ #ifdef HARMONIZE_ALLOC_DEBUG
99
+ printf("Allocated (for resize) array of type '%s' at %p, with size %zu\n",typeid(*(inner->adr)).name(),inner->adr,inner->size);
100
+ #endif
101
+ }
102
+
103
+ __host__ void operator<<(std::vector<T> &other) {
104
+ if( other.size() != inner->size ){
105
+ resize(other.size());
106
+ }
107
+ auto_throw( adapt::GPUrtMemcpy(
108
+ inner->adr,
109
+ other.data(),
110
+ sizeof(T)*inner->size,
111
+ adapt::GPUrtMemcpyHostToDevice
112
+ ) );
113
+ }
114
+
115
+ __host__ void operator>>(std::vector<T> &other) {
116
+ if( other.size() != inner->size ){
117
+ other.resize(inner->size);
118
+ }
119
+ auto_throw( adapt::GPUrtMemcpy(
120
+ other.data(),
121
+ inner->adr,
122
+ sizeof(T)*inner->size,
123
+ adapt::GPUrtMemcpyDeviceToHost
124
+ ) );
125
+ }
126
+
127
+
128
+ __host__ void operator<<(T &other) {
129
+ if( inner->size != 1 ){
130
+ resize(1);
131
+ }
132
+ auto_throw( adapt::GPUrtMemcpy(
133
+ inner->adr,
134
+ &other,
135
+ sizeof(T),
136
+ adapt::GPUrtMemcpyHostToDevice
137
+ ) );
138
+ }
139
+
140
+ __host__ void operator<<(T &&other) {
141
+ T host_copy = other;
142
+ if( inner->size != 1 ){
143
+ resize(1);
144
+ }
145
+ auto_throw( adapt::GPUrtMemcpy(
146
+ inner->adr,
147
+ &host_copy,
148
+ sizeof(T),
149
+ adapt::GPUrtMemcpyHostToDevice
150
+ ) );
151
+ }
152
+
153
+
154
+ __host__ void operator>>(T &other) {
155
+ auto_throw( adapt::GPUrtMemcpy(
156
+ &other,
157
+ inner->adr,
158
+ sizeof(T),
159
+ adapt::GPUrtMemcpyDeviceToHost
160
+ ) );
161
+ }
162
+
163
+
164
+ DevBuf<T> (T* adr, size_t size)
165
+ : inner(new Inner(adr,size))
166
+ {}
167
+
168
+ DevBuf<T> ()
169
+ : DevBuf<T>((T*)NULL,(size_t)0)
170
+ {}
171
+
172
+ DevBuf<T> (size_t size)
173
+ : DevBuf<T> (hardMalloc<T>(size),size)
174
+ {}
175
+
176
+ DevBuf<T> (T& value)
177
+ : DevBuf<T>()
178
+ {
179
+ (*this) << value;
180
+ }
181
+
182
+ DevBuf<T> (T&& value)
183
+ : DevBuf<T>()
184
+ {
185
+ (*this) << value;
186
+ }
187
+
188
+
189
+ template<typename... ARGS>
190
+ static DevBuf<T> make(ARGS... args)
191
+ {
192
+ return DevBuf<T>( T(args...) );
193
+ }
194
+
195
+
196
+ };
197
+
198
+
199
+
200
+
201
+ template<typename T>
202
+ class DevObj {
203
+
204
+ protected:
205
+
206
+ struct Inner {
207
+
208
+ T *adr;
209
+ T host_copy;
210
+
211
+
212
+ void push_data(){
213
+ auto_throw( adapt::GPUrtMemcpy(
214
+ adr,
215
+ &host_copy,
216
+ sizeof(T),
217
+ adapt::GPUrtMemcpyHostToDevice
218
+ ) );
219
+ }
220
+
221
+ void pull_data(){
222
+ auto_throw( adapt::GPUrtMemcpy(
223
+ &host_copy,
224
+ adr,
225
+ sizeof(T),
226
+ adapt::GPUrtMemcpyDefault
227
+ ) );
228
+ }
229
+
230
+ template<typename... ARGS>
231
+ Inner(T *adr_val, ARGS... args)
232
+ : adr (adr_val)
233
+ , host_copy(args...)
234
+ {
235
+ host_copy.host_init();
236
+ push_data();
237
+ #ifdef HARMONIZE_ALLOC_DEBUG
238
+ printf("Allocated object of type '%s' at %p\n",typeid(*adr).name(),adr);
239
+ #endif
240
+ }
241
+
242
+ ~Inner() {
243
+ if ( adr != NULL) {
244
+ #ifdef HARMONIZE_ALLOC_DEBUG
245
+ printf("Freed object of type '%s' at %p\n",typeid(*adr).name(),adr);
246
+ #endif
247
+ pull_data();
248
+ host_copy.host_free();
249
+ util::host::auto_throw(adapt::GPUrtFree(adr));
250
+ }
251
+ }
252
+
253
+ };
254
+
255
+ std::shared_ptr<Inner> inner;
256
+
257
+ public:
258
+
259
+
260
+ void push_data(){
261
+ inner->push_data();
262
+ }
263
+
264
+ void pull_data(){
265
+ inner->pull_data();
266
+ }
267
+
268
+ operator T*() { return inner->adr; }
269
+
270
+ operator T() { return inner->host_copy; }
271
+
272
+ T& host_copy() { return inner->host_copy; }
273
+
274
+ template<typename... ARGS>
275
+ DevObj<T>(ARGS... args)
276
+ : inner(new Inner(hardMalloc<T>(1),args...))
277
+ {}
278
+
279
+ };
280
+
281
+
282
+ }
283
+
284
+ }
285
+
286
+ #endif
287
+