11#include < iostream>
22#include < chrono>
3- #include < cstdint>
43#include < algorithm>
54
65#include < cuda_runtime.h>
6+ #include < cuda_runtime_api.h>
77
88#include " ComputeGradientCuda.hpp"
99
10- #include < cuda_runtime_api.h>
11-
1210#include " APRParameters.hpp"
1311#include " data_structures/Mesh/PixelData.hpp"
1412#include " data_structures/Mesh/downsample.cuh"
2725#include " bsplineYdir.cuh"
2826#include " bsplineZdir.cuh"
2927#include " findMinMax.cuh"
30-
3128#include " data_structures/APR/access/GenInfoGpuAccess.cuh"
3229
3330
@@ -171,6 +168,7 @@ namespace {
171168 }
172169}
173170
171+
174172template <typename ImgType>
175173void getGradientCuda (const PixelData<ImgType> &image,
176174 ImgType *cudaImage, ImgType *cudaGrad, float *cudalocal_scale_temp,
@@ -180,7 +178,8 @@ void getGradientCuda(const PixelData<ImgType> &image,
180178 // TODO: Used PixelDataDim in all methods below and change input parameter from image to imageDim
181179
182180 // TODO: (APRstreams) isErrorDetected should be handled differently, in current state it blocks streams from
183- // running in parallel
181+ // running in parallel, for example we can skip checking of it here (in 99.9% of cases we won't get any error)
182+ // and check it at the end of computation where we need to synchronize
184183 if (par.lambda > 0 ) {
185184 isErrorDetected = false ;
186185 isErrorDetectedCuda.copyH2D ();
@@ -339,19 +338,6 @@ void runBsplineOffsetAndCopyOriginal(ImgType *cudaImage, ImgType *cudaCopy, floa
339338};
340339
341340
342- template <typename T>
343- __global__ void printKernel (T *input, size_t length) {
344- printf (" DOWNSAMPLED: " );
345- for (int i = 0 ; i < length; i++) printf (" %d " , input[i]);
346- printf (" \n " );
347- }
348-
349- template <typename ImgType>
350- void runPrint (ImgType *cudaImage, size_t length, cudaStream_t aStream) {
351- printKernel<<<1 ,1 , 0 , aStream>>> (cudaImage, length);
352- };
353-
354-
355341template <typename T>
356342__global__ void sampleKernel (T *downsampledLevel, T *parts_cuda, int level, int xLen, int yLen, int zLen, uint64_t *level_xz_vec_cuda, uint64_t *xz_end_vec_cuda, uint16_t *y_vec) {
357343 const int xi = (blockIdx .x * blockDim .x ) + threadIdx .x ;
@@ -371,27 +357,17 @@ __global__ void sampleKernel(T *downsampledLevel, T *parts_cuda, int level, int
371357 }
372358}
373359
360+
374361template <typename ImgType>
375362void runSampleParts (ImgType** downsampled, GenInfo &aprInfo, ImgType *parts_cuda, uint64_t *level_xz_vec_cuda, uint64_t *xz_end_vec_cuda, uint16_t *y_vec, cudaStream_t aStream) {
376- // std::cout << aprInfo << std::endl;
377363 // Run kernels for each level
378364 for (int level = aprInfo.l_min ; level <= aprInfo.l_max ; level++) {
379- // std::cout << "Processing level " << level << std::endl;
380365 dim3 threadsPerBlock (128 , 1 , 8 );
381366 dim3 numBlocks ((aprInfo.x_num [level] + threadsPerBlock.x - 1 ) / threadsPerBlock.x ,
382367 1 ,
383368 (aprInfo.z_num [level] + threadsPerBlock.z - 1 ) / threadsPerBlock.z );
384- // std::cout << downsampled[level] << std::endl;
385- // std::cout << parts_cuda << std::endl;
386- // std::cout << aprInfo.x_num[level] << std::endl;
387- // std::cout << aprInfo.y_num[level] << std::endl;
388- // std::cout << aprInfo.z_num[level] << std::endl;
389- // std::cout << level_xz_vec_cuda << std::endl;
390- // std::cout << xz_end_vec_cuda << std::endl;
391- // std::cout << y_vec << std::endl;
392369 sampleKernel<<<numBlocks, threadsPerBlock, 0 , aStream>>> (downsampled[level], parts_cuda, level, aprInfo.x_num [level], aprInfo.y_num [level], aprInfo.z_num [level], level_xz_vec_cuda, xz_end_vec_cuda, y_vec);
393370 }
394-
395371};
396372
397373
@@ -443,7 +419,6 @@ class GpuProcessingTask<U>::GpuProcessingTaskImpl {
443419 ScopedCudaMemHandler<float *, JUST_ALLOC > local_scale_temp;
444420 ScopedCudaMemHandler<float *, JUST_ALLOC > local_scale_temp2;
445421
446-
447422 // bspline stuff
448423 const float tolerance = 0.0001 ;
449424 std::pair<BsplineParamsCuda, BsplineParamsCudaMemoryHandlers> cudax;
@@ -453,7 +428,6 @@ class GpuProcessingTask<U>::GpuProcessingTaskImpl {
453428 BsplineParamsCuda splineCudaY;
454429 BsplineParamsCuda splineCudaZ;
455430
456-
457431 // bool isErrorDetected;
458432 VectorData<bool > isErrorDetectedPinned;
459433 ScopedCudaMemHandler<bool *, JUST_ALLOC > isErrorDetectedCuda;
@@ -470,7 +444,6 @@ class GpuProcessingTask<U>::GpuProcessingTaskImpl {
470444 ScopedCudaMemHandler<float *, JUST_ALLOC > lstPadded;
471445 ScopedCudaMemHandler<float *, JUST_ALLOC > lst2Padded;
472446
473-
474447 // Structures used by computeLinearStructureCuda
475448 VectorData<uint64_t > xz_end_vec;
476449 VectorData<uint64_t > level_xz_vec;
@@ -521,9 +494,6 @@ public:
521494 splineCudaX = cudax.first ;
522495 splineCudaY = cuday.first ;
523496 splineCudaZ = cudaz.first ;
524- // std::cout << "\n=============== GpuProcessingTaskImpl ===================" << iStream << "\n\n";
525- // std::cout << iCpuImage << std::endl;
526- // std::cout << iCpuLevels << std::endl;
527497
528498 // In LIS we have: var_win[0,1,2] = maximum 3 var_win[3,4,5] = maximum 6
529499 // so maximum paddSize is 6 6 6
@@ -532,7 +502,6 @@ public:
532502 lstPadded.initialize (nullptr , paddedImageSize.size (), iStream);
533503 lst2Padded.initialize (nullptr , paddedImageSize.size (), iStream);
534504
535-
536505 // initialize_xz_linear() - CPU impl.
537506 counter_total = 1 ; // the buffer val to allow -1 calls without checking.
538507 level_xz_vec.resize (iAprInfo.l_max + 2 , 0 ); // includes a buffer for -1 calls, and therefore needs to be called with level + 1;
@@ -542,20 +511,15 @@ public:
542511 level_xz_vec[i + 1 ] = counter_total;
543512 }
544513 xz_end_vec.resize (counter_total, 0 );
545- // std::cout << "----------- iAprInfo.getSize() = " << iAprInfo.getSize() << std::endl;
546514 y_vec.resize (iAprInfo.getSize ()); // resize it to worst case -> same number particles as pixels in input image
547- // std::cout << "----------- iAprInfo.getSize() = " << iAprInfo.getSize() << std::endl;
548515 xz_end_vec_cuda.initialize (xz_end_vec.data (), xz_end_vec.size (), iStream);
549516 level_xz_vec_cuda.initialize (level_xz_vec.data (), level_xz_vec.size (), iStream);
550517
551518 parts.resize (iAprInfo.getSize ()); // resize it to worst case -> same number particles as pixels in input image
552519
553-
554520 isErrorDetectedPinned.resize (1 );
555521 isErrorDetectedCuda.initialize (isErrorDetectedPinned.data (), 1 , iStream);
556522
557-
558-
559523 // In nvidia GPUs maximum number of threads per SM is multiplication of 512 (usually 1536 or 2048)
560524 // Calculate number of blocks to saturate whole SMs
561525 // Multiply it by 8 to have more smaller blocks to have better load balancing in case GPU is busy with other tasks
@@ -589,16 +553,12 @@ public:
589553 size_t levelOffset = 0 ;
590554 for (int l = l_max-1 ; l >= l_min; --l) {
591555 size_t level_size = iAprInfo.x_num [l] * iAprInfo.y_num [l] * iAprInfo.z_num [l];
592- // std::cout << l << " dim: " << iAprInfo.getDimension(l) << " " << iAprInfo.getSize(l) << " " << level_size << std::endl;
593556 downsampled[l] = image.get () + levelOffset;
594557 levelOffset += iAprInfo.getSize (l);
595558
596559 runDownsampleMean (downsampled[l+1 ], downsampled[l], iAprInfo.x_num [l+1 ], iAprInfo.y_num [l+1 ], iAprInfo.z_num [l+1 ], iStream);
597560 }
598561
599- // VectorData<uint64_t> xz_end_vec;
600- // VectorData<uint64_t> level_xz_vec;
601- // VectorData<uint16_t> y_vec;
602562 runSampleParts (downsampled, iAprInfo, parts_cuda.get (), level_xz_vec_cuda.get (), xz_end_vec_cuda.get (), y_vec_cuda.get (), iStream);
603563 }
604564
@@ -607,8 +567,6 @@ public:
607567 }
608568
609569 void processOnGpu () {
610-
611-
612570 // Set it and copy first before copying the image
613571 // It improves *a lot* performance even though it is needed later in computeLinearStructureCuda()
614572 iAprInfo.total_number_particles = 0 ; // reset total_number_particles to 0
@@ -660,16 +618,12 @@ public:
660618 // Copy y_vec from GPU to CPU and synchronize last time - it is needed before we copy data to CPU structures
661619 checkCuda (cudaMemcpyAsync (y_vec.begin (), y_vec_cuda.get (), iAprInfo.total_number_particles * sizeof (uint16_t ), cudaMemcpyDeviceToHost, iStream));
662620
663-
664- // SAMPLE under development
621+ // SAMPLE particles and transfer to CPU
665622 sample ();
666623 parts.resize (iAprInfo.total_number_particles );
667624 // Copy y_vec from GPU to CPU and synchronize last time - it is needed before we copy data to CPU structures
668625 checkCuda (cudaMemcpyAsync (parts.begin (), parts_cuda.get (), iAprInfo.total_number_particles * sizeof (ImgType), cudaMemcpyDeviceToHost, iStream));
669626
670-
671-
672-
673627 // Synchornize last time - at that moment all data from GPU is copied to CPU
674628 checkCuda (cudaStreamSynchronize (iStream));
675629
@@ -716,7 +670,6 @@ template void cudaFilterBsplineFull(PixelData<int16_t> &, float, float, TypeOfRe
716670template void cudaFilterBsplineFull (PixelData<uint8_t > &, float , float , TypeOfRecBsplineFlags, int );
717671
718672
719-
720673template <typename ImgType>
721674void cudaFilterBsplineFull (PixelData<ImgType> &input, float lambda, float tolerance, TypeOfRecBsplineFlags flags, int maxFilterLen) {
722675 cudaStream_t aStream = 0 ;
@@ -809,12 +762,6 @@ void getGradient(PixelData<ImgType> &image, PixelData<ImgType> &grad_temp, Pixel
809762
810763 float tolerance = 0.0001 ;
811764
812- // TODO: This is wrong and done only for compile. BsplineParams has to be computed seperately for each dimension.
813- // Should be fixed when other parts of pipeline are ready.
814-
815- // FIX BSPLINE PARAMS !!!!!!!! to get full gradient pipeline test working !!!!!!!!!!!!!!!!!!!!!!!!!1
816-
817-
818765 BsplineParams px = prepareBsplineStuff (image.x_num , par.lambda , tolerance);
819766 auto cudax = transferSpline (px, aStream);
820767 auto splineCudaX = cudax.first ;
@@ -882,4 +829,3 @@ std::pair<T,T> cudaRunMinMax(PixelData<T> &input_image) {
882829
883830template std::pair<uint16_t , uint16_t > cudaRunMinMax (PixelData<uint16_t > &);
884831template std::pair<int , int > cudaRunMinMax (PixelData<int > &);
885-
0 commit comments