-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix3d.cu
680 lines (541 loc) · 17.7 KB
/
Matrix3d.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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
#include <iostream>
#include <fstream>
#include <cassert>
#include <cuda.h>
#include "gpu_utils.h"
#include "cuda_utils.h"
#include "Matrix3d.h"
const int TILEDIM = 32;
const int TILEROWS = 8;
template <typename T>
__global__ void transpose_xyz_yzx_kernel() {
}
//
// Copies a 3d matrixL data_in(x, y, z) -> data_out(x, y, z)
//
template <typename T>
__global__ void copy_kernel(const int nx, const int ny, const int nz,
const int xsize_in, const int ysize_in, const int zsize_in,
const int xsize_out, const int ysize_out, const int zsize_out,
const T* data_in, T* data_out) {
const int x = blockIdx.x * TILEDIM + threadIdx.x;
const int y = blockIdx.y * TILEDIM + threadIdx.y;
const int z = blockIdx.z + threadIdx.z;
for (int j=0;j < TILEDIM;j += TILEROWS)
if ((x < nx) && (y < ny) && (z < nz))
data_out[x + (y + j + z*ysize_out)*xsize_out] = data_in[x + (y + j + z*ysize_in)*xsize_in];
}
//
// Transposes a 3d matrix out-of-place: data_in(x, y, z) -> data_out(y, z, x)
//
template <typename T>
__global__ void transpose_xyz_yzx_kernel(const int nx, const int ny, const int nz,
const int xsize_in, const int ysize_in, const int zsize_in,
const int xsize_out, const int ysize_out, const int zsize_out,
const T* data_in, T* data_out) {
// Shared memory
__shared__ T tile[TILEDIM][TILEDIM+1];
int x = blockIdx.x * TILEDIM + threadIdx.x;
int y = blockIdx.y * TILEDIM + threadIdx.y;
int z = blockIdx.z + threadIdx.z;
// Read (x,y) data_in into tile (shared memory)
for (int j=0;j < TILEDIM;j += TILEROWS)
if ((x < nx) && (y + j < ny) && (z < nz))
tile[threadIdx.y + j][threadIdx.x] = data_in[x + (y + j + z*ysize_in)*xsize_in];
__syncthreads();
// Write (y,x) tile into data_out
x = blockIdx.x * TILEDIM + threadIdx.y;
y = blockIdx.y * TILEDIM + threadIdx.x;
for (int j=0;j < TILEDIM;j += TILEROWS)
if ((x + j < nx) && (y < ny) && (z < nz))
data_out[y + (z + (x+j)*ysize_out)*xsize_out] = tile[threadIdx.x][threadIdx.y + j];
}
//
// Transposes a 3d matrix out-of-place: data_in(x, y, z) -> data_out(z, x, y)
//
template <typename T>
__global__ void transpose_xyz_zxy_kernel(const int nx, const int ny, const int nz,
const int xsize, const int ysize, const int zsize,
const T* data_in, T* data_out) {
// Shared memory
__shared__ T tile[TILEDIM][TILEDIM+1];
int x = blockIdx.x * TILEDIM + threadIdx.x;
int y = blockIdx.z + threadIdx.z;
int z = blockIdx.y * TILEDIM + threadIdx.y;
// Read (x,z) data_in into tile (shared memory)
for (int k=0;k < TILEDIM;k += TILEROWS)
if ((x < nx) && (y < ny) && (z + k < nz))
tile[threadIdx.y + k][threadIdx.x] = data_in[x + (y + (z + k)*ysize)*xsize];
__syncthreads();
// Write (z,x) tile into data_out
x = blockIdx.x * TILEDIM + threadIdx.y;
z = blockIdx.y * TILEDIM + threadIdx.x;
for (int k=0;k < TILEDIM;k += TILEROWS)
if ((x + k < nx) && (y < ny) && (z < nz))
data_out[z + (x + k + y*xsize)*zsize] = tile[threadIdx.x][threadIdx.y + k];
}
__device__ inline float2 operator*(float2 lhs, const float2& rhs) {
lhs.x *= rhs.x;
lhs.y *= rhs.y;
return lhs;
}
//
// Scales matrix
//
template <typename T>
__global__ void scale_kernel(const int nx, const int ny, const int nz,
const int xsize, const int ysize, const int zsize,
const T fac, T* data) {
int tid = blockIdx.x * blockDim.x + threadIdx.x;
int z = tid/(nx*ny);
tid -= z*nx*ny;
int y = tid/nx;
tid -= y*nx;
int x = tid;
if ((x < nx) && (y < ny) && (z < nz))
data[x + (y + z*ysize)*xsize] = data[x + (y + z*ysize)*xsize]*fac;
}
//template <typename T>
//Matrix3d<T>::Matrix3d() : nx(0), ny(0), nz(0), xsize(0), ysize(0), zsize(0) {
// data = NULL;
// external_storage = false;
//}
template <typename T>
Matrix3d<T>::Matrix3d(const int nx, const int ny, const int nz, T* ext_data) :
nx(nx), ny(ny), nz(nz), xsize(nx), ysize(ny), zsize(nz) {
assert(nx > 0);
assert(ny > 0);
assert(nz > 0);
init(xsize*ysize*zsize, ext_data);
}
template <typename T>
Matrix3d<T>::Matrix3d(const int nx, const int ny, const int nz,
const int xsize, const int ysize, const int zsize, T* ext_data) :
nx(nx), ny(ny), nz(nz), xsize(xsize), ysize(ysize), zsize(zsize) {
assert(nx > 0);
assert(ny > 0);
assert(nz > 0);
assert(xsize >= nx);
assert(ysize >= ny);
assert(zsize >= nz);
init(xsize*ysize*zsize, ext_data);
}
template <typename T>
Matrix3d<T>::Matrix3d(const int nx, const int ny, const int nz,
const char *filename, T* ext_data) :
nx(nx), ny(ny), nz(nz), xsize(nx), ysize(ny), zsize(nz) {
assert(nx > 0);
assert(ny > 0);
assert(nz > 0);
init(xsize*ysize*zsize, ext_data);
load(nx, ny, nz, filename);
}
template <typename T>
Matrix3d<T>::~Matrix3d() {
if (!external_storage)
deallocate<T>(&data);
}
template <typename T>
void Matrix3d<T>::init(const int size, T* ext_data) {
assert(size > 0);
if (ext_data == NULL) {
allocate<T>(&data, size);
external_storage = false;
} else {
data = ext_data;
external_storage = true;
}
}
//
// Prints matrix size on screen
//
template <typename T>
void Matrix3d<T>::print_info() {
std::cout << "nx ny nz = " << nx << " "<< ny << " "<< nz << std::endl;
std::cout << "xsize ysize zsize = " << xsize << " "<< ysize << " "<< zsize << std::endl;
}
template <>
inline double Matrix3d<long long int>::norm(long long int a, long long int b) {
return (double)llabs(a-b);
}
template <>
inline double Matrix3d<int>::norm(int a, int b) {
return (double)abs(a-b);
}
template <>
inline double Matrix3d<float>::norm(float a, float b) {
return (double)fabsf(a-b);
}
template <>
inline double Matrix3d<float2>::norm(float2 a, float2 b) {
return (double)max(fabsf(a.x-b.x), fabsf(a.y-b.y) );
}
template<>
inline bool Matrix3d<long long int>::is_nan(long long int a) {return false;};
template<>
inline bool Matrix3d<int>::is_nan(int a) {return false;};
template<>
inline bool Matrix3d<float>::is_nan(float a) {
return isnan(a);
}
template<>
inline bool Matrix3d<float2>::is_nan(float2 a) {
return (isnan(a.x) || isnan(a.y));
}
std::ostream& operator<<(std::ostream& os, float2& a) {
os << a.x << " " << a.y;
return os;
}
std::istream& operator>>(std::istream& is, float2& a) {
is >> a.x >> a.y;
return is;
}
//
// Compares two matrices, returns true if the difference is within tolerance
// NOTE: Comparison is done in double precision
//
template <typename T>
bool Matrix3d<T>::compare(Matrix3d<T>* mat, const double tol, double& max_diff) {
assert(mat->nx == nx);
assert(mat->ny == ny);
assert(mat->nz == nz);
T *h_data1 = new T[xsize*ysize*zsize];
T *h_data2 = new T[mat->xsize*mat->ysize*mat->zsize];
copy_DtoH<T>(data, h_data1, xsize*ysize*zsize);
copy_DtoH<T>(mat->data, h_data2, mat->xsize*mat->ysize*mat->zsize);
bool ok = true;
max_diff = 0.0;
int x, y, z;
double diff;
try {
for (z=0;z < nz;z++)
for (y=0;y < ny;y++)
for (x=0;x < nx;x++) {
if (is_nan(h_data1[x + (y + z*ysize)*xsize]) ||
is_nan(h_data2[x + (y + z*mat->ysize)*mat->xsize])) throw 1;
diff = norm(h_data1[x + (y + z*ysize)*xsize], h_data2[x + (y + z*mat->ysize)*mat->xsize]);
max_diff = (diff > max_diff) ? diff : max_diff;
if (diff > tol) throw 2;
}
}
catch (int a) {
std::cout << "x y z = " << x << " "<< y << " "<< z << std::endl;
std::cout << "this: " << h_data1[x + (y + z*ysize)*xsize] << std::endl;
std::cout << "mat: " << h_data2[x + (y + z*mat->ysize)*mat->xsize] << std::endl;
if (a == 2) std::cout << "difference: " << diff << std::endl;
ok = false;
}
delete [] h_data1;
delete [] h_data2;
return ok;
}
//
// Transposes a 3d matrix out-of-place: data(x, y, z) -> data(y, z, x)
// Copies a block
// NOTE: this is a slow reference calculation performed on the host
//
template <typename T>
void Matrix3d<T>::transpose_xyz_yzx_host(int src_x0, int src_y0, int src_z0,
int dst_x0, int dst_y0, int dst_z0,
int xlen, int ylen, int zlen,
Matrix3d<T>* mat) {
assert(xlen > 0);
assert(ylen > 0);
assert(zlen > 0);
assert(src_x0 >= 0 && src_x0 + xlen <= nx);
assert(src_y0 >= 0 && src_y0 + ylen <= ny);
assert(src_z0 >= 0 && src_z0 + zlen <= nz);
assert(dst_x0 >= 0 && dst_x0 + ylen <= mat->nx);
assert(dst_y0 >= 0 && dst_y0 + zlen <= mat->ny);
assert(dst_z0 >= 0 && dst_z0 + xlen <= mat->nz);
T *h_data1 = new T[xsize*ysize*zsize];
T *h_data2 = new T[mat->xsize*mat->ysize*mat->zsize];
copy_DtoH<T>(data, h_data1, xsize*ysize*zsize);
copy_DtoH<T>(mat->data, h_data2, mat->xsize*mat->ysize*mat->zsize);
for (int z=0;z < zlen;z++)
for (int y=0;y < ylen;y++)
for (int x=0;x < xlen;x++) {
h_data2[y+dst_x0 + (z+dst_y0 + (x+dst_z0)*mat->ysize)*mat->xsize] =
h_data1[x+src_x0 + (y+src_y0 + (z+src_z0)*ysize)*xsize];
}
copy_HtoD<T>(h_data2, mat->data, mat->xsize*mat->ysize*mat->zsize);
delete [] h_data1;
delete [] h_data2;
}
//
// Transposes a 3d matrix out-of-place: data(x, y, z) -> data(y, z, x)
// NOTE: this is a slow reference calculation performed on the host
//
template <typename T>
void Matrix3d<T>::transpose_xyz_yzx_host(Matrix3d<T>* mat) {
assert(mat->nx == ny);
assert(mat->ny == nz);
assert(mat->nz == nx);
transpose_xyz_yzx_host(0,0,0, 0,0,0, nx,ny,nz, mat);
}
//
// Transposes a 3d matrix out-of-place: data(x, y, z) -> data(y, z, x)
// NOTE: this is a slow reference calculation performed on the host
//
template <typename T>
void Matrix3d<T>::transpose_xyz_zxy_host(Matrix3d<T>* mat) {
assert(mat->nx == nz);
assert(mat->ny == nx);
assert(mat->nz == ny);
assert(mat->xsize == zsize);
assert(mat->ysize == xsize);
assert(mat->zsize == ysize);
T *h_data1 = new T[xsize*ysize*zsize];
T *h_data2 = new T[xsize*ysize*zsize];
copy_DtoH<T>(data, h_data1, xsize*ysize*zsize);
copy_DtoH<T>(mat->data, h_data2, xsize*ysize*zsize);
for (int z=0;z < nz;z++)
for (int y=0;y < ny;y++)
for (int x=0;x < nx;x++)
h_data2[z + (x + y*xsize)*zsize] = h_data1[x + (y + z*ysize)*xsize];
copy_HtoD<T>(h_data2, mat->data, xsize*ysize*zsize);
delete [] h_data1;
delete [] h_data2;
}
//
// Transposes a 3d matrix out-of-place: data(x, y, z) -> data(y, z, x)
//
template <typename T>
void Matrix3d<T>::transpose_xyz_yzx(Matrix3d<T>* mat) {
assert(mat->nx == ny);
assert(mat->ny == nz);
assert(mat->nz == nx);
transpose_xyz_yzx(0,0,0, 0,0,0, nx,ny,nz, mat);
}
//
// Transposes a sub block of a 3d matrix out-of-place: data(x, y, z) -> data(y, z, x)
// Sub block is: (x0...x1) x (y0...y1) x (z0...z1)
//
template <typename T>
void Matrix3d<T>::transpose_xyz_yzx(int src_x0, int src_y0, int src_z0,
int dst_x0, int dst_y0, int dst_z0,
int xlen, int ylen, int zlen,
Matrix3d<T>* mat) {
assert(xlen > 0);
assert(ylen > 0);
assert(zlen > 0);
assert(src_x0 >= 0 && src_x0 + xlen <= nx);
assert(src_y0 >= 0 && src_y0 + ylen <= ny);
assert(src_z0 >= 0 && src_z0 + zlen <= nz);
assert(dst_x0 >= 0 && dst_x0 + ylen <= mat->nx);
assert(dst_y0 >= 0 && dst_y0 + zlen <= mat->ny);
assert(dst_z0 >= 0 && dst_z0 + xlen <= mat->nz);
dim3 nthread(TILEDIM, TILEROWS, 1);
dim3 nblock((xlen-1)/TILEDIM+1, (ylen-1)/TILEDIM+1, zlen);
int src_pos = src_x0 + (src_y0 + src_z0*ysize)*xsize;
int dst_pos = dst_x0 + (dst_y0 + dst_z0*mat->ysize)*mat->xsize;
transpose_xyz_yzx_kernel<<< nblock, nthread >>>(xlen, ylen, zlen,
xsize, ysize, zsize,
mat->xsize, mat->ysize, mat->zsize,
&data[src_pos], &mat->data[dst_pos]);
cudaCheck(cudaGetLastError());
}
//
// Transposes a 3d matrix out-of-place: data(x, y, z) -> data(z, x, y)
//
template <typename T>
void Matrix3d<T>::transpose_xyz_zxy(Matrix3d<T>* mat) {
assert(mat->nx == nz);
assert(mat->ny == nx);
assert(mat->nz == ny);
assert(mat->xsize == zsize);
assert(mat->ysize == xsize);
assert(mat->zsize == ysize);
dim3 nthread(TILEDIM, TILEROWS, 1);
dim3 nblock((nx-1)/TILEDIM+1, (nz-1)/TILEDIM+1, ny);
transpose_xyz_zxy_kernel<<< nblock, nthread >>>(nx, ny, nz, xsize, ysize, zsize,
data, mat->data);
cudaCheck(cudaGetLastError());
}
template <typename T>
void Matrix3d<T>::copy_host(int src_x0, int src_y0, int src_z0,
int dst_x0, int dst_y0, int dst_z0,
int xlen, int ylen, int zlen,
Matrix3d<T>* mat) {
assert(xlen > 0);
assert(ylen > 0);
assert(zlen > 0);
assert(src_x0 >= 0 && src_x0 + xlen <= nx);
assert(src_y0 >= 0 && src_y0 + ylen <= ny);
assert(src_z0 >= 0 && src_z0 + zlen <= nz);
assert(dst_x0 >= 0 && dst_x0 + xlen <= mat->nx);
assert(dst_y0 >= 0 && dst_y0 + ylen <= mat->ny);
assert(dst_z0 >= 0 && dst_z0 + zlen <= mat->nz);
T *h_data1 = new T[xsize*ysize*zsize];
T *h_data2 = new T[mat->xsize*mat->ysize*mat->zsize];
copy_DtoH<T>(data, h_data1, xsize*ysize*zsize);
copy_DtoH<T>(mat->data, h_data2, mat->xsize*mat->ysize*mat->zsize);
for (int z=0;z < zlen;z++)
for (int y=0;y < ylen;y++)
for (int x=0;x < xlen;x++) {
h_data2[x+dst_x0 + (y+dst_y0 + (z+dst_z0)*mat->ysize)*mat->xsize] =
h_data1[x+src_x0 + (y+src_y0 + (z+src_z0)*ysize)*xsize];
}
copy_HtoD<T>(h_data2, mat->data, mat->xsize*mat->ysize*mat->zsize);
delete [] h_data1;
delete [] h_data2;
}
//
// Copies a 3d matrix data(x, y, z) -> data(x, y, z)
//
template <typename T>
void Matrix3d<T>::copy(int src_x0, int src_y0, int src_z0,
int dst_x0, int dst_y0, int dst_z0,
int xlen, int ylen, int zlen,
Matrix3d<T>* mat) {
assert(xlen > 0);
assert(ylen > 0);
assert(zlen > 0);
assert(src_x0 >= 0 && src_x0 + xlen <= nx);
assert(src_y0 >= 0 && src_y0 + ylen <= ny);
assert(src_z0 >= 0 && src_z0 + zlen <= nz);
assert(dst_x0 >= 0 && dst_x0 + xlen <= mat->nx);
assert(dst_y0 >= 0 && dst_y0 + ylen <= mat->ny);
assert(dst_z0 >= 0 && dst_z0 + zlen <= mat->nz);
dim3 nthread(TILEDIM, TILEROWS, 1);
dim3 nblock((xlen-1)/TILEDIM+1, (ylen-1)/TILEDIM+1, zlen);
int src_pos = src_x0 + (src_y0 + src_z0*ysize)*xsize;
int dst_pos = dst_x0 + (dst_y0 + dst_z0*mat->ysize)*mat->xsize;
std::cout << "src_pos = " << src_pos << std::endl;
std::cout << "dst_pos = " << dst_pos << std::endl;
std::cout << "nthread = " << nthread.x << " " << nthread.y << " " << nthread.z << std::endl;
std::cout << "nblock = " << nblock.x << " " << nblock.y << " " << nblock.z << std::endl;
copy_kernel<<< nblock, nthread >>>(xlen, ylen, zlen,
xsize, ysize, zsize,
mat->xsize, mat->ysize, mat->zsize,
&data[src_pos], &mat->data[dst_pos]);
cudaCheck(cudaThreadSynchronize());
std::cout << "After copy:"<< std::endl;
mat->print(0,0,0,0,1,1);
cudaCheck(cudaGetLastError());
}
//
// Copies a 3d matrix data(x, y, z) -> data(x, y, z)
//
template <typename T>
void Matrix3d<T>::copy(Matrix3d<T>* mat) {
assert(mat->nx == nx);
assert(mat->ny == ny);
assert(mat->nz == nz);
copy(0,0,0, 0,0,0, nx, ny, nz, mat);
}
//
// Prints part of matrix (x0:x1, y0:y1, z0:z1) on screen
//
template <typename T>
void Matrix3d<T>::print(const int x0, const int x1,
const int y0, const int y1,
const int z0, const int z1) {
T *h_data = new T[xsize*ysize*zsize];
copy_DtoH<T>(data, h_data, xsize*ysize*zsize);
for (int z=z0;z <= z1;z++)
for (int y=y0;y <= y1;y++)
for (int x=x0;x <= x1;x++)
std::cout << h_data[x + (y + z*ysize)*xsize] << std::endl;
delete [] h_data;
}
//
// Loads Matrix block (x0...x1) x (y0...y1) x (z0...z1) from file "filename"
// Matrix in file has size nx x ny x nz
//
template <typename T>
void Matrix3d<T>::load(const int x0, const int x1, const int nx,
const int y0, const int y1, const int ny,
const int z0, const int z1, const int nz,
const char *filename) {
assert(x0 < x1);
assert(y0 < y1);
assert(z0 < z1);
assert(x0 >= 0 && x1 < nx);
assert(y0 >= 0 && y1 < ny);
assert(z0 >= 0 && z1 < nz);
std::ifstream file;
file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try {
// Open file
file.open(filename);
// Allocate CPU memory
T *h_data = new T[xsize*ysize*zsize];
// Read data
for (int z=0;z < nz;z++)
for (int y=0;y < ny;y++)
for (int x=0;x < nx;x++)
if (x >= x0 && x <= x1 &&
y >= y0 && y <= y1 &&
z >= z0 && z <= z1) {
file >> h_data[x-x0 + (y-y0 + (z-z0)*ysize)*xsize];
} else {
T dummy;
file >> dummy;
}
// Copy data from CPU to GPU
copy_HtoD<T>(h_data, data, xsize*ysize*zsize);
// Deallocate CPU memory
delete [] h_data;
// Close file
file.close();
}
catch(std::ifstream::failure e) {
std::cerr << "Error opening/reading/closing file " << filename << std::endl;
exit(1);
}
}
//
// Loads Matrix of size nx x ny x nz from file "filename"
//
template <typename T>
void Matrix3d<T>::load(const int nx, const int ny, const int nz,
const char *filename) {
assert(this->nx == nx);
assert(this->ny == ny);
assert(this->nz == nz);
load(0, nx-1, nx,
0, ny-1, ny,
0, nz-1, nz, filename);
}
//
// Scales the matrix by a factor "fac"
//
template <typename T>
void Matrix3d<T>::scale(const T fac) {
int nthread = 512;
int nblock = (nx*ny*nz-1)/512 + 1;
scale_kernel<<< nblock, nthread >>>(nx, ny, nz, xsize, ysize, zsize, fac, data);
cudaCheck(cudaGetLastError());
}
template <typename T>
int Matrix3d<T>::get_nx() {
return nx;
}
template <typename T>
int Matrix3d<T>::get_ny() {
return ny;
}
template <typename T>
int Matrix3d<T>::get_nz() {
return nz;
}
template <typename T>
int Matrix3d<T>::get_xsize() {
return xsize;
}
template <typename T>
int Matrix3d<T>::get_ysize() {
return ysize;
}
template <typename T>
int Matrix3d<T>::get_zsize() {
return zsize;
}
//
// Explicit instances of Matrix3d
//
template class Matrix3d<float>;
template class Matrix3d<float2>;
template class Matrix3d<long long int>;
template class Matrix3d<int>;