-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdwt_gpu.cu
More file actions
1589 lines (1209 loc) · 45.1 KB
/
dwt_gpu.cu
File metadata and controls
1589 lines (1209 loc) · 45.1 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
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
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
CUDA implementation of Haar discrete wavelet transform.
Ed Karrels, ed.karrels@gmail.com, June 2014
*/
#include "dwt_cpu.h"
#include "dwt_gpu.h"
#include "transpose_gpu.h"
#include "cucheck.h"
#include "nixtimer.h"
#include "cuda_timer.h"
#include "test_compress_gpu.h"
#define SQRT2 1.4142135623730950488f
#define INV_SQRT2 0.70710678118654752440f
#define HAAR_3D_BLOCK_SIZE 128
#define CDF97_3D_BLOCK_SIZE 128
#define HAAR_V2_BLOCK_SIZE 128
#define CDF97_V2_BLOCK_SIZE 128
#define CDF97_V3_BLOCK_WIDTH 32
#define CDF97_V3_BLOCK_HEIGHT 10
#define CDF97_V3_TILE_WIDTH 32
#define CDF97_V3_TILE_HEIGHT 32
#define MAX_KERNEL_ROW_LEN 12000
/*
To see a previous version of this code that tried out surfaces and
iterating down columns (rather than rows), see the version as checked
into Git history as checkin "736c52c":
git show 736c52c:dwt_gpu.cu | less
*/
/*
Call structure:
haar_2d_cuda
haar_2d_cuda_internal
haar_2d_kernel
haar_inv_2d_kernel
haar_transpose_2d_kernel
haar_inv_transpose_2d_kernel
*/
template<typename NUM>
float haar_2d_cuda_internal
(int size, NUM *data, bool inverse, int stepCount, int threadBlockSize,
bool useCombinedTranspose);
void printArray(const float *array, int width, int height, int depth,
const char *name) {
if (name) printf("%s\n", name);
for (int level=0; level < depth; level++) {
printf("z=%d\n", level);
for (int row=0; row < height; row++) {
for (int col=0; col < width; col++) {
printf("%8.4f ", array[(level*height + row)*width + col]);
}
putchar('\n');
}
putchar('\n');
}
putchar('\n');
}
void printArray(const int *array, int width, int height, int depth,
const char *name) {
if (name) printf("%s\n", name);
for (int level=0; level < depth; level++) {
printf("z=%d\n", level);
for (int row=0; row < height; row++) {
for (int col=0; col < width; col++) {
printf("%5d ", array[(level*height + row)*width + col]);
}
putchar('\n');
}
putchar('\n');
}
putchar('\n');
}
void printDeviceArray(const float *array_dev, scu_wavelet::int3 size,
const char *name) {
printDeviceArray(array_dev, size.x, size.y, size.z, name);
}
void printDeviceArray(const float *array_dev, int width, int height, int depth,
const char *name) {
float *array = new float[width*height*depth];
CUCHECK(cudaMemcpy(array, array_dev, sizeof(float)*width*height*depth,
cudaMemcpyDeviceToHost));
printArray(array, width, height, depth, name);
delete[] array;
}
void printDeviceArray(const int *array_dev, scu_wavelet::int3 size,
const char *name) {
printDeviceArray(array_dev, size.x, size.y, size.z, name);
}
void printDeviceArray(const int *array_dev, int width, int height, int depth,
const char *name) {
int *array = new int[width*height*depth];
CUCHECK(cudaMemcpy(array, array_dev, sizeof(int)*width*height*depth,
cudaMemcpyDeviceToHost));
printArray(array, width, height, depth, name);
delete[] array;
}
/**
Perform one pass of the Haar transform on the first 'length'
elements of inputRow using outputRow as temp space.
*/
template<typename NUM>
__device__ void haar_kernel_row(int length, NUM *outputRow,
const NUM *inputRow) {
const int half = length >> 1;
// point d at the first half of the temporary row
NUM *s = outputRow;
// point d at the second half of the temporary row
NUM *d = s + half;
for (int i=threadIdx.x; i < half; i += blockDim.x) {
NUM a = inputRow[2*i], b = inputRow[2*i + 1];
d[i] = (a - b) * INV_SQRT2;
s[i] = (a + b) * INV_SQRT2;
}
}
/**
Perform one pass of the inverse Haar transform on the first 'length'
elements of inputRow using outputRow as temp space.
*/
template<typename NUM>
__device__ void haar_kernel_row_inverse(int length, NUM *outputRow,
NUM *inputRow) {
// Set s to point to my row in the input data
NUM *s = inputRow;
int half = length >> 1;
// point d at the second half of the temporary row
NUM *d = s + half;
for (int i=threadIdx.x; i < half; i += blockDim.x) {
outputRow[2*i] = INV_SQRT2 * (s[i] + d[i]);
outputRow[2*i+1] = INV_SQRT2 * (s[i] - d[i]);
}
}
/*
This does a Haar discrete wavelet transform on each row of
a 2-d array. Each thread block processes one row.
All data is in global memory.
Input data is in data[], results will be in result[].
*/
template<typename NUM>
__global__ void haar_2d_kernel
(int arrayWidth, int transformLength, NUM *data, NUM *result) {
// each thread block processes one row of data
int y = blockIdx.x;
// make pointers to my row of data
NUM *inputRow = data + y * arrayWidth;
NUM *outputRow = result + y * arrayWidth;
haar_kernel_row(transformLength, outputRow, inputRow);
}
/* Inverse Haar wavelet transform. */
template<typename NUM>
__global__ void haar_inv_2d_kernel
(int arrayWidth, int transformLength, NUM *data, NUM *result) {
// each thread block processes one row of data
int y = blockIdx.x;
// make pointers to my row of data
NUM *inputRow = data + y * arrayWidth;
NUM *outputRow = result + y * arrayWidth;
haar_kernel_row_inverse(transformLength, outputRow, inputRow);
}
/*
Do one pass of a Haar discrete wavelet transform and transpose the
matrix at the same time. This splits the data into tiles, computing
the transform and writing the results into a transposed matrix.
In these diagrams, the number indicates the thread that reads
or writes each element. It depicts each thread block as a 4x4 block
of 16 threads, but in reality it will be larger--16x16 or 32x32.
Input matrix (global memory)
+----------------------------------------
| 0 0 1 1 2 2 3 3 ...next tile...
| 4 4 5 5 6 6 7 7
| 8 8 9 9 10 10 11 11
| 12 12 13 13 14 14 15 15
| ...next tile...
Shared memory temporary storage
The results from each "sum" operation are stored in one 2d array,
and the results form each "difference" operation are stored in a
different 2d array.
Write in this order:
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
Read in this order:
0 4 8 12
1 5 9 13
2 6 10 14
3 7 11 15
Output matrix (global memory)
+----------------------------------------
| 0 1 2 3 ...next tile...
| 4 5 6 7
| 8 9 10 11
| 12 13 14 15
| ...
| ...
| ...lower half....
| 0 1 2 3
| 4 5 6 7
| 8 9 10 11
| 12 13 14 15
| ...
Success!
Before:
Transform time: 40.707 ms (2 calls)
Transpose time: 57.732 ms (2 calls)
After:
Transform time: 52.512 ms (2 calls)
Transpose time: 0.004 ms (2 calls)
*/
template<typename NUM>
__global__ void haar_transpose_2d_kernel
(int arrayWidth, int transformLength, NUM *data, NUM *result,
int tileSize) {
// dynamically-sized shared memory
extern __shared__ int shared[];
// assign parts of shared memory to my arrays
NUM *sums, *diffs;
sums = (NUM*) shared;
diffs = sums + tileSize * (tileSize+1);
int inputx = (blockIdx.x*blockDim.x + threadIdx.x) * 2;
int inputy = blockIdx.y*blockDim.y + threadIdx.y;
// read a tile 2*tileSize wide, tileSize tall, compute
// the sum and difference coefficients, and store those coefficients
// transposed in the sums and diffs shared memory arrays.
int readIdx = inputy * arrayWidth + inputx;
if (inputx+1 < transformLength && inputy < transformLength) {
NUM a = data[readIdx], b = data[readIdx+1];
int shidx = threadIdx.x + threadIdx.y*(tileSize+1);
sums [shidx] = (a + b) * INV_SQRT2;
diffs[shidx] = (a - b) * INV_SQRT2;
}
__syncthreads();
// Read the transposed sums and diffs shared memory arrays,
// and write the data to a tile whose position has been transposed
int writey = blockIdx.x*blockDim.x + threadIdx.y;
int writex = blockIdx.y*blockDim.y + threadIdx.x;
if (writex < transformLength && writey*2 < transformLength) {
int writeIdx = writey * arrayWidth + writex;
int shidx = threadIdx.y + threadIdx.x*(tileSize+1);
result[writeIdx] = sums[shidx];
writeIdx += arrayWidth*(transformLength>>1);
result[writeIdx] = diffs[shidx];
}
}
template<typename NUM>
__global__ void haar_inv_transpose_2d_kernel
(int arrayWidth, int transformLength, NUM *data, NUM *result, int tileSize) {
// dynamically-sized shared memory
extern __shared__ int shared[];
// assign parts of shared memory to my arrays
NUM *v1, *v2;
v1 = (NUM*) shared;
v2 = v1 + tileSize * (tileSize+1);
int inputx = blockIdx.x*blockDim.x + threadIdx.x;
int inputy = blockIdx.y*blockDim.y + threadIdx.y;
// Read the sum and difference coefficients, where the difference coeff
// is in the second half of the array. Compute the original values v1 and v2,
// and store them in two shared memory arrays.
int readIdx1 = inputy * arrayWidth + inputx;
int readIdx2 = readIdx1 + (transformLength>>1);
if (inputx < (transformLength>>1) && inputy < transformLength) {
NUM s = data[readIdx1], d = data[readIdx2];
int shidx = threadIdx.x * (tileSize+1) + threadIdx.y;
v1[shidx] = (s + d) * INV_SQRT2;
v2[shidx] = (s - d) * INV_SQRT2;
}
__syncthreads();
// Read the transposed pair of values v1 and v2 from the transposed
// shared memory arrays, and write the values to a tile tileSize wide
// and tileSize*2 tall.
int writex = blockIdx.y*blockDim.y + threadIdx.x;
int writey = (blockIdx.x*blockDim.x + threadIdx.y) * 2;
if (writex < transformLength && writey+1 < transformLength) {
int writeIdx1 = writey * arrayWidth + writex;
int writeIdx2 = writeIdx1 + arrayWidth;
int shidx = threadIdx.y * (tileSize+1) + threadIdx.x;
result[writeIdx1] = v1[shidx];
result[writeIdx2] = v2[shidx];
}
}
float haar_2d_cuda
(int size, float *data, bool inverse, int stepCount, int threadBlockSize,
bool useCombinedTranspose) {
return haar_2d_cuda_internal
(size, data, inverse, stepCount, threadBlockSize, useCombinedTranspose);
}
// double support was added in version 1.3
#if !defined(__CUDA_ARCH__) || (__CUDA_ARCH__ >= 130)
float haar_2d_cuda
(int size, double *data, bool inverse, int stepCount, int threadBlockSize,
bool useCombinedTranspose) {
return haar_2d_cuda_internal
(size, data, inverse, stepCount, threadBlockSize, useCombinedTranspose);
}
#endif
// haar_transpose_2d_kernel and haar_inv_transpose_2d_kernel use tiles
// to optimize the memory access pattern. After testing tile sizes from
// 8x8 to 32x32 on a few different GPUs, here are the sizes that produced
// the best performance:
//
// GTX 480: 16 (compute level 2.0)
// GTX 570: 16 (compute level 2.0)
// K2000M: 16 (compute level 3.0, laptop)
// GTX 680: 32 (compute level 3.0)
// GTX 690: 32 (compute level 3.0)
// Tesla K20c: 32 (compute level 3.5)
//
int bestHaarGPUTileSize() {
int gpuId;
cudaDeviceProp prop;
CUCHECK(cudaGetDevice(&gpuId));
CUCHECK(cudaGetDeviceProperties(&prop, gpuId));
// Based on the tests listed above, older (Fermi) and smaller (laptop)
// GPUs seem to work better with 16x16 tiles, but newer regular GPUs
// are faster with 32x32 tiles.
if (prop.major <= 2 || prop.multiProcessorCount <= 2)
return 16;
else
return 32;
}
// Wrapper function that handles the CUDA details.
template<typename NUM>
float haar_2d_cuda_internal
(int size, NUM *data, bool inverse, int stepCount, int threadBlockSize,
bool useCombinedTranspose) {
int tileSize = bestHaarGPUTileSize();
if (useCombinedTranspose) printf("Tile size %dx%d\n", tileSize, tileSize);
int maxSteps = dwtMaximumSteps(size);
if (stepCount < 1 || stepCount > maxSteps)
stepCount = maxSteps;
// create timers
CudaTimer overallTimer, copyToTimer, copyFromTimer,
transformTimer, transposeTimer;
// allocate memory for the data and the temp space on the GPU
NUM *data1_dev, *data2_dev;
size_t totalBytes = size * size * sizeof(NUM);
CUCHECK(cudaMalloc((void**) &data1_dev, totalBytes));
CUCHECK(cudaMalloc((void**) &data2_dev, totalBytes));
// Create a stream to enable asynchronous operation, to minimize
// time between kernel calls.
cudaStream_t stream = 0;
CUCHECK(cudaStreamCreate(&stream));
// start the timer
double startTimeCPU = NixTimer::time();
overallTimer.start(stream);
// copy the data to the GPU
copyToTimer.start(stream);
CUCHECK(cudaMemcpyAsync(data1_dev, data, totalBytes, cudaMemcpyHostToDevice,
stream));
copyToTimer.end(stream);
size_t sharedMemSize = tileSize * (tileSize+1)
* 2 * sizeof(float);
int transformLength;
if (inverse) {
// inverse
transformLength = size >> (stepCount - 1);
for (int i=0; i < stepCount; i++) {
dim3 gridDim((transformLength - 1) / (tileSize*2) + 1,
(transformLength - 1) / (tileSize) + 1);
dim3 blockDim(tileSize, tileSize);
if (useCombinedTranspose) {
// transform columns and transpose
transformTimer.start(stream);
haar_inv_transpose_2d_kernel
<<<gridDim, blockDim, sharedMemSize, stream>>>
(size, transformLength, data1_dev, data2_dev, tileSize);
transformTimer.end(stream);
// transform rows and transpose
transformTimer.start(stream);
haar_inv_transpose_2d_kernel
<<<gridDim, blockDim, sharedMemSize, stream>>>
(size, transformLength, data2_dev, data1_dev, tileSize);
transformTimer.end(stream);
} else {
// transform columns
transformTimer.start(stream);
haar_inv_2d_kernel
<<<transformLength, threadBlockSize, 0, stream>>>
(size, transformLength, data1_dev, data2_dev);
transformTimer.end(stream);
// transpose the matrix into temp_dev
transposeTimer.start(stream);
gpuTransposeSquare(size, transformLength, data2_dev, data1_dev, stream);
transposeTimer.end(stream);
// transform rows
transformTimer.start(stream);
haar_inv_2d_kernel
<<<transformLength, threadBlockSize, 0, stream>>>
(size, transformLength, data1_dev, data2_dev);
transformTimer.end(stream);
// transpose the matrix into data_dev
transposeTimer.start(stream);
gpuTransposeSquare(size, transformLength, data2_dev, data1_dev, stream);
transposeTimer.end(stream);
// results are in data1_dev
}
transformLength <<= 1;
}
} else {
// forward
transformLength = size;
for (int i=0; i < stepCount; i++) {
dim3 gridDim((transformLength - 1) / (tileSize*2) + 1,
(transformLength - 1) / (tileSize) + 1);
dim3 blockDim(tileSize, tileSize);
if (useCombinedTranspose) {
// do the wavelet transform on rows
transformTimer.start(stream);
haar_transpose_2d_kernel
<<<gridDim, blockDim, sharedMemSize, stream>>>
(size, transformLength, data1_dev, data2_dev, tileSize);
transformTimer.end(stream);
// do the wavelet transform on columns
transformTimer.start(stream);
haar_transpose_2d_kernel
<<<gridDim, blockDim, sharedMemSize, stream>>>
(size, transformLength, data2_dev, data1_dev, tileSize);
transformTimer.end(stream);
} else {
// do the wavelet transform on rows
transformTimer.start(stream);
haar_2d_kernel
<<<transformLength, threadBlockSize, 0, stream>>>
(size, transformLength, data1_dev, data2_dev);
transformTimer.end(stream);
// transpose the matrix into temp_dev
transposeTimer.start(stream);
gpuTransposeSquare(size, transformLength, data2_dev, data1_dev, stream);
transposeTimer.end(stream);
// do the wavelet transform on columns
transformTimer.start(stream);
haar_2d_kernel
<<<transformLength, threadBlockSize, 0, stream>>>
(size, transformLength, data1_dev, data2_dev);
transformTimer.end(stream);
// transpose the matrix back into data_dev
transposeTimer.start(stream);
gpuTransposeSquare(size, transformLength, data2_dev, data1_dev, stream);
transposeTimer.end(stream);
}
transformLength >>= 1;
}
}
// copy the data back from the GPU
copyFromTimer.start(stream);
CUCHECK(cudaMemcpyAsync(data, data1_dev, totalBytes, cudaMemcpyDeviceToHost,
stream));
copyFromTimer.end(stream);
// Since all the GPU tasks were started asynchronously, control should
// flow to this point very quickly. The cudaEventSynchronize() call will
// wait until the GPU is finished.
double endTimeCPU = NixTimer::time();
printf("Time elapsed creating GPU tasks: %.3f ms\n",
1000*(endTimeCPU - startTimeCPU));
fflush(stdout);
// stop the timer
overallTimer.end(stream);
CUCHECK(cudaEventSynchronize(overallTimer.getLastEvent()));
cudaStreamDestroy(stream);
// check for errors
CUCHECK(cudaGetLastError());
printf("Times:\n");
printf(" Copy data to GPU: %9.3f ms\n", copyToTimer.time());
printf(" Transform time: %9.3f ms (%d calls)\n",
transformTimer.time(), transformTimer.count());
if (transposeTimer.count() > 0) {
printf(" Transpose time: %9.3f ms (%d calls)\n",
transposeTimer.time(), transposeTimer.count());
}
printf(" Copy data from GPU: %9.3f ms\n", copyFromTimer.time());
// deallocate GPU memory
CUCHECK(cudaFree(data1_dev));
CUCHECK(cudaFree(data2_dev));
return overallTimer.time();
}
__device__ void copy_row(int count, float *dest, const float *src) {
int i = threadIdx.x;
while (i < count) {
dest[i] = src[i];
i += blockDim.x;
}
}
__device__ void pad_row_mirrored(int length, float *row, int padLen) {
assert(blockDim.x >= padLen*2);
if (threadIdx.x < padLen*2) {
int factor, offset;
if (threadIdx.x < padLen) { // first few threads pad the beginning
factor = -1;
offset = -1;
} else { // other half of the threads pad the end
factor = 1;
offset = length-padLen;
}
MirroredArray<float> mirrored(length, row);
int i = threadIdx.x * factor + offset;
row[i] = mirrored[i];
}
}
__global__ void haar_3d_kernel_allglobal
(float *data1, float *data2, int rowLength, int stepCount) {
int offset = rowLength * (blockIdx.y + blockIdx.z*gridDim.y);
float *row = data1 + offset;
float *tempRow = data2 + offset;
while (stepCount > 0) {
// copy data from row to tempRow
copy_row(rowLength, tempRow, row);
__syncthreads();
// read tempRow, write to row
haar_kernel_row(rowLength, row, tempRow);
stepCount--;
rowLength >>= 1;
__syncthreads();
}
}
__global__ void haar_3d_kernel_allglobal_inverse
(float *data1, float *data2, int rowLength, int stepCount) {
int offset = rowLength * (blockIdx.y + blockIdx.z*gridDim.y);
float *row = data1 + offset;
float *tempRow = data2 + offset;
rowLength >>= (stepCount-1);
while (stepCount > 0) {
// copy data from row to tempRow
copy_row(rowLength, tempRow, row);
__syncthreads();
// read tempRow, write to row
haar_kernel_row_inverse(rowLength, row, tempRow);
stepCount--;
rowLength <<= 1;
__syncthreads();
}
}
/**
Input data is in data1. data2 is temp space.
Update 'size', since the dimensions will rotate.
On each pass, the input row is copied to a temp row.
Entries in the temp row are read, processed, and written to the input row.
If the row is short enough to fit in shared memory, use shared memory
as the temp row.
If the row is short enough to fit two copies in shared memory
1. Copy input row from global to shared memory.
2. Do regular processing with shared memory as temp row.
3. Copy input row from shared to global memory.
If the row is short enough to fit in registers, use them as the temp row.
*/
void haar_3d_cuda(float *data, float *tmpData, scu_wavelet::int3 &size,
scu_wavelet::int3 stepCount, bool inverse,
CudaTimer *transformTimer,
CudaTimer *transposeTimer) {
// limitation on the number of thread blocks
if (!(size <= scu_wavelet::int3(65535,65535,65535))) {
fprintf(stderr, "Cubelet too large: max is 65535x65535x65535\n");
return;
}
if (inverse) stepCount.rotateBack();
if (!is_padded_for_wavelet(size, stepCount)) {
fprintf(stderr, "%dx%dx%d data is not properly padded for %d,%d,%d "
"transform steps.\n",
size.x, size.y, size.z, stepCount.x, stepCount.y, stepCount.z);
return;
}
if (inverse) stepCount.rotateFwd();
dim3 gridDim;
dim3 blockDim(HAAR_3D_BLOCK_SIZE);
CudaTimer mytimer("haar_3d_cuda");
mytimer.start();
if (!inverse) {
// X transform
gridDim.y = size.y;
gridDim.z = size.z;
if (transformTimer) transformTimer->start();
haar_3d_kernel_allglobal<<<gridDim, blockDim>>>
(data, tmpData, size.x, stepCount.x);
if (transformTimer) transformTimer->end();
// transpose XYZ -> YZX
if (transposeTimer) transposeTimer->start();
gpuTranspose3dFwd(tmpData, data, size);
if (transposeTimer) transposeTimer->end();
// data is now in tmpData
// Y transform
gridDim.y = size.y;
gridDim.z = size.z;
if (transformTimer) transformTimer->start();
haar_3d_kernel_allglobal<<<gridDim, blockDim>>>
(tmpData, data, size.x, stepCount.y);
if (transformTimer) transformTimer->end();
// transpose YZX -> ZXY
if (transposeTimer) transposeTimer->start();
gpuTranspose3dFwd(data, tmpData, size);
if (transposeTimer) transposeTimer->end();
// data is back in data
// Z transform
gridDim.y = size.y;
gridDim.z = size.z;
if (transformTimer) transformTimer->start();
haar_3d_kernel_allglobal<<<gridDim, blockDim>>>
(data, tmpData, size.x, stepCount.z);
if (transformTimer) transformTimer->end();
} else { // is inverse
// inverse Z transform
gridDim.y = size.y;
gridDim.z = size.z;
if (transformTimer) transformTimer->start();
haar_3d_kernel_allglobal_inverse<<<gridDim, blockDim>>>
(data, tmpData, size.x, stepCount.z);
if (transformTimer) transformTimer->end();
// transpose ZXY -> YZX
if (transposeTimer) transposeTimer->start();
gpuTranspose3dBack(tmpData, data, size);
if (transposeTimer) transposeTimer->end();
// data is in 'tmpData'
// inverse Y transform
gridDim.y = size.y;
gridDim.z = size.z;
if (transformTimer) transformTimer->start();
haar_3d_kernel_allglobal_inverse<<<gridDim, blockDim>>>
(tmpData, data, size.x, stepCount.y);
if (transformTimer) transformTimer->end();
// transpose YZX -> XYZ
if (transposeTimer) transposeTimer->start();
gpuTranspose3dBack(data, tmpData, size);
if (transposeTimer) transposeTimer->end();
// data is in 'data'
// inverse X transform
gridDim.y = size.y;
gridDim.z = size.z;
if (transformTimer) transformTimer->start();
haar_3d_kernel_allglobal_inverse<<<gridDim, blockDim>>>
(data, tmpData, size.x, stepCount.x);
if (transformTimer) transformTimer->end();
}
mytimer.end();
mytimer.sync();
mytimer.print();
/*
CUCHECK(cudaMemcpy(data, tmpData, sizeof(float)*size.x*size.y*size.z,
cudaMemcpyDeviceToDevice));
Mean squared error: 469.826, peak SNR: 21.411
Huffman build table 0.779 ms
Huffman encoding: 288 bytes, 4.48 bits/pixel, longest encoding = 10 bits
Write data file: 3.32 ms
Total: 19.02 ms
*/
}
template<class ARRAY>
__device__ void cdf97_row(int rowLength, float *outputRow,
const ARRAY inputRow) {
const int half = rowLength >> 1;
int writeIdx = threadIdx.x;
while (writeIdx < half) {
int readIdx = writeIdx << 1;
// Apply the sums convolution, write result to lower half of the row
float t0, t1, t2, t3, t4, t5, t6, t7, t8;
t0 = inputRow[readIdx - 4];
t1 = inputRow[readIdx - 3];
t2 = inputRow[readIdx - 2];
t3 = inputRow[readIdx - 1];
t4 = inputRow[readIdx];
t5 = inputRow[readIdx + 1];
t6 = inputRow[readIdx + 2];
t7 = inputRow[readIdx + 3];
t8 = inputRow[readIdx + 4];
outputRow[writeIdx] =
CDF97_ANALYSIS_LOWPASS_FILTER_0 * t4
+ CDF97_ANALYSIS_LOWPASS_FILTER_1 * (t3+t5)
+ CDF97_ANALYSIS_LOWPASS_FILTER_2 * (t2+t6)
+ CDF97_ANALYSIS_LOWPASS_FILTER_3 * (t1+t7)
+ CDF97_ANALYSIS_LOWPASS_FILTER_4 * (t0+t8);
// Apply the differences convolution, write result to upper half of the row
outputRow[writeIdx+half] =
CDF97_ANALYSIS_HIGHPASS_FILTER_0 * t5
+ CDF97_ANALYSIS_HIGHPASS_FILTER_1 * (t4+t6)
+ CDF97_ANALYSIS_HIGHPASS_FILTER_2 * (t3+t7)
+ CDF97_ANALYSIS_HIGHPASS_FILTER_3 * (t2+t8);
writeIdx += blockDim.x;
}
}
// interleave: 01234567 -> 04152636
__device__ void cdf97_row_interleave(int rowLength, float *outputRow,
const float *inputRow) {
const int half = rowLength >> 1;
int i = threadIdx.x;
while (i < half) {
outputRow[i*2] = inputRow[i];
outputRow[i*2+1] = inputRow[i+half];
i += blockDim.x;
}
}
template<class ARRAY>
__device__ void cdf97_row_inverse(int rowLength, float *outputRow,
const ARRAY inputRow) {
int i = threadIdx.x * 2;
while (i < rowLength) {
float t0, t1, t2, t3, t4, t5, t6, t7, t8;
t0 = inputRow[i-3];
t1 = inputRow[i-2];
t2 = inputRow[i-1];
t3 = inputRow[i];
t4 = inputRow[i+1];
t5 = inputRow[i+2];
t6 = inputRow[i+3];
t7 = inputRow[i+4];
t8 = inputRow[i+5];
// evens
outputRow[i] =
CDF97_SYNTHESIS_LOWPASS_FILTER_0 * t3
+ CDF97_SYNTHESIS_LOWPASS_FILTER_1 * (t2+t4)
+ CDF97_SYNTHESIS_LOWPASS_FILTER_2 * (t1+t5)
+ CDF97_SYNTHESIS_LOWPASS_FILTER_3 * (t0+t6);
// odds
outputRow[i+1] =
CDF97_SYNTHESIS_HIGHPASS_FILTER_0 * t4
+ CDF97_SYNTHESIS_HIGHPASS_FILTER_1 * (t3+t5)
+ CDF97_SYNTHESIS_HIGHPASS_FILTER_2 * (t2+t6)
+ CDF97_SYNTHESIS_HIGHPASS_FILTER_3 * (t1+t7)
+ CDF97_SYNTHESIS_HIGHPASS_FILTER_4 * (t0+t8);
i += blockDim.x*2;
}
}
__global__ void cdf97_3d_kernel
(float *data, int rowLength, int stepCount) {
int offset = rowLength * (blockIdx.y + blockIdx.z*gridDim.y);
float *row = data+offset;
extern __shared__ float sharedData[];
float *tempRow = sharedData+4;
while (stepCount > 0) {
// copy data from global memory to tempRow
copy_row(rowLength, tempRow, row);
__syncthreads();
// pad the data
pad_row_mirrored(rowLength, tempRow, 4);
__syncthreads();
// read tempRow, write to row in global memory
cdf97_row(rowLength, row, tempRow);
stepCount--;
rowLength >>= 1;
__syncthreads();
}
}
__global__ void cdf97_3d_kernel_inverse
(float *data, float *tempData, int rowLength, int stepCount) {
int offset = rowLength * (blockIdx.y + blockIdx.z*gridDim.y);
float *row = data + offset;
// float *tempRow = tempData + offset;
extern __shared__ float sharedData[];
float *tempRow = sharedData+4;
rowLength >>= (stepCount-1);
// MirroredArray inputRow(rowLength, tempRow);
while (stepCount > 0) {
// inputRow.setLength(rowLength);
// copy data to tempData, interleaving 01234567 -> 04152636
cdf97_row_interleave(rowLength, tempRow, row);
__syncthreads();
pad_row_mirrored(rowLength, tempRow, 4);
__syncthreads();
// read tempRow, write to row
cdf97_row_inverse(rowLength, row, tempRow);
stepCount--;
rowLength <<= 1;
__syncthreads();
}
}
void cdf97_3d_cuda(float *data, float *tmpData,
scu_wavelet::int3 &size, scu_wavelet::int3 stepCount,
bool inverse,
CudaTimer *transformTimer, CudaTimer *transposeTimer) {