From 1add687367a5d7bbe1dd56d575b08a495a350db9 Mon Sep 17 00:00:00 2001 From: Jakub Mitura <53857487+jakubMitura14@users.noreply.github.com> Date: Mon, 4 Apr 2022 20:36:46 +0200 Subject: [PATCH 01/41] initial docs --- monai/_extensions/hausdorff/hausdorff.cpp | 88 + monai/_extensions/hausdorff/hausdorff_cuda.cu | 2628 +++++++++++++++++ .../morphological_hausdorff_distance.py | 81 + 3 files changed, 2797 insertions(+) create mode 100644 monai/_extensions/hausdorff/hausdorff.cpp create mode 100644 monai/_extensions/hausdorff/hausdorff_cuda.cu create mode 100644 monai/metrics/morphological_hausdorff_distance.py diff --git a/monai/_extensions/hausdorff/hausdorff.cpp b/monai/_extensions/hausdorff/hausdorff.cpp new file mode 100644 index 00000000000..1bf55c49163 --- /dev/null +++ b/monai/_extensions/hausdorff/hausdorff.cpp @@ -0,0 +1,88 @@ +#include + +#include + +// CUDA declarations + + +int getHausdorffDistance_CUDA( + torch::Tensor goldStandard, + torch::Tensor algoOutput + , const int xDim, const int yDim + , const int zDim,const float robustnessPercent); + + +at::Tensor getHausdorffDistance_CUDA_FullResList(at::Tensor goldStandard, + at::Tensor algoOutput + , int WIDTH, int HEIGHT, int DEPTH, float robustnessPercent); + + + +std::tuple benchmarkOlivieraCUDA( + torch::Tensor goldStandard, + torch::Tensor algoOutput + , const int xDim, const int yDim + , const int zDim); + +// C++ interface + +// NOTE: AT_ASSERT has become AT_CHECK on master after 0.4. +#define CHECK_CUDA(x) AT_ASSERTM(x.type().is_cuda(), #x " must be a CUDA tensor") +#define CHECK_CONTIGUOUS(x) AT_ASSERTM(x.is_contiguous(), #x " must be contiguous") +#define CHECK_INPUT(x) CHECK_CUDA(x); CHECK_CONTIGUOUS(x) + + +int getHausdorffDistance( + torch::Tensor goldStandard, + torch::Tensor algoOutput + , const int xDim, const int yDim, const int zDim + , const float robustnessPercent=1.0 +) { + + CHECK_INPUT(goldStandard); + CHECK_INPUT(algoOutput); + + + return getHausdorffDistance_CUDA(goldStandard, algoOutput, xDim, yDim, zDim, robustnessPercent); + + +} + + + + +at::Tensor getHausdorffDistance_FullResList( + torch::Tensor goldStandard, + torch::Tensor algoOutput + , const int xDim, const int yDim, const int zDim + , const float robustnessPercent = 1.0 +) { + + CHECK_INPUT(goldStandard); + CHECK_INPUT(algoOutput); + + + return getHausdorffDistance_CUDA_FullResList(goldStandard, algoOutput, xDim, yDim, zDim, robustnessPercent); + + +} + + + + +std::tuple benchmarkOlivieraCUDAOnlyBool( + torch::Tensor goldStandard, + torch::Tensor algoOutput + , const int xDim, const int yDim + , const int zDim) { + return benchmarkOlivieraCUDA(goldStandard, algoOutput, xDim, yDim, zDim); + + +} + + +PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) { + m.def("getHausdorffDistance", &getHausdorffDistance, "Basic version of Hausdorff distance"); + m.def("benchmarkOlivieraCUDA", &benchmarkOlivieraCUDA, "Algorithm by Oliviera - just for comparison sake - accept only boolean arrays "); + m.def("getHausdorffDistance_FullResList", &getHausdorffDistance_FullResList, " return additionally full result list indicating in which dilatation iterations results were recorded "); +} diff --git a/monai/_extensions/hausdorff/hausdorff_cuda.cu b/monai/_extensions/hausdorff/hausdorff_cuda.cu new file mode 100644 index 00000000000..3c72e1cd496 --- /dev/null +++ b/monai/_extensions/hausdorff/hausdorff_cuda.cu @@ -0,0 +1,2628 @@ +#include +//#include +#include + +#include +#include + +#include +#include +//#include +#include +#include +//#include +#include +#include + +#include +#include +#include +#include +#include +#include "device_launch_parameters.h" + +using namespace cooperative_groups; + + +#pragma once + +//constants describing the meaning of main shared memory spaces +constexpr uint32_t localWorkQueLength = 300; +constexpr uint32_t startOfLocalWorkQ = 4160; +constexpr uint32_t lengthOfMainShmem = 4460;//4460; +constexpr uint32_t begResShmem = 1088; +constexpr uint32_t begfirstRegShmem = 2112; +constexpr uint32_t begSecRegShmem = 3136; +constexpr uint32_t begSMallRegShmemA = 0; +constexpr uint32_t begSMallRegShmemB = 1056; +constexpr uint32_t begSourceShmem = 32; + +//as the pipeline is so asynchronous we need some additional storage for saving data related to the operations +//will define how big is the amount of space in order to be able to use fast version of modulo operator it should be power of 2 ... +constexpr uint32_t modForPipelineVars = 16; +constexpr uint32_t fpLocCountBeg = 4460; + + + + +// other +//added to linear index meta in order to mark weather block is of type gold or not +constexpr uint32_t isGoldOffset = (UINT16_MAX * 10); + + +/** +In order to be able to use cuda malloc 3d we will implemnt it as a series +of 3d arrays +*/ + + +#pragma once +extern "C" struct MetaDataCPU { + int metaXLength; + int MetaYLength; + int MetaZLength; + int totalMetaLength; + + + //1)maxX 2)minX 3)maxY 4) minY 5) maxZ 6) minZ - minimal and maximum coordinates of blocks with some entries of intrest + //7)global FP count; 8)global FN count 9) workQueueCounter 10)resultFP globalCounter 11) resultFn globalCounter + //12) global FPandFn offset 13)globalIterationNumb + //array3dWithDimsCPU minMaxes; + unsigned int* minMaxes; + + ////// counts of false positive and false negatives in given metadata blocks + + ///// sizes of array below will be established on the basis of fp and fn values known after boolKernel finished execution + + //work queue - workqueue counter already present in minMaxes as entry 9 + //in practice it is matrix of length the same as FP+FN global count +1 and width of 5 + //1) xMeta; 2)yMeta 3)zMeta 4)isGold 5)iteration number + //we use one single long rewsult list - in order to avoid overwriting each block each block has established offset where it would write it's results + uint32_t* resultList; + +}; + +#pragma once +extern "C" struct MetaDataGPU { + int metaXLength; + int MetaYLength; + int MetaZLength; + int totalMetaLength; + + //1)maxX 2)minX 3)maxY 4) minY 5) maxZ 6) minZ - minimal and maximum coordinates of blocks with some entries of intrest + //7)global FP count; 8)global FN count 9) workQueueCounter 10)resultFP globalCounter 11) resultFn globalCounter + //12) global FPandFn offset 13)globalIterationNumb + + unsigned int* minMaxes; + + //represents x from description of main Arr + unsigned int mainArrXLength; + //have length 4x + unsigned int mainArrSectionLength; + + unsigned int metaDataSectionLength = 20; + + // now we will store here also calculated by min maxes kernel values of minimum and maximumvalues + //1)maxX 2)minX 3)maxY 4) minY 5) maxZ 6) minZ + unsigned int maxX; + unsigned int minX; + unsigned int maxY; + unsigned int minY; + unsigned int maxZ; + unsigned int minZ; +}; + + + + + + + + +/* +* Basically holding the arguments for master functions controlling full preparation to get all for Housedorff kernel +*/ +#pragma once +template +struct ForFullBoolPrepArgs { + + //metadata struct + MetaDataCPU metaData; + + // dimensions of data block + int dbXLength; + int dbYLength; + int dbZLength; + // gold standard and segmentation output array + torch::Tensor goldArr; + torch::Tensor segmArr; + + int Nx; + int Ny; + int Nz; + + TFF numberToLookFor;// what we will look for in arrays + //number and dimensionality of threads and blocks required to lounch bool kernel + dim3 threads; + int blocks; + //threads and blocks for first metadata pass kernel + int threadsFirstMetaDataPass; + int blocksFirstMetaDataPass; + //threads and blocks for main pass + dim3 threadsMainPass; + int blocksMainPass; + //threads and blocks for padding pass + dim3 threadsPaddingPass; + int blocksPaddingPass; + //threads and blocks for non first metadata passes + int threadsOtherMetaDataPasses; + int blocksOtherMetaDataPasses; + // will establish how many points we want to include in dilatation and how many we can ignore so typically set to 95% - so we will ignore only 5% most distant + float robustnessPercent = 1.0; // 0.95; + + int32_t* resultListPointerMeta; + int32_t* resultListPointerLocalCPU; + int32_t* resultListPointerIterNumb; + +}; + + +/* +* Basically holding the arguments for main kernel in the FullBoolPrep +*/ +#pragma once +template +struct ForBoolKernelArgs { + //matadata struct + MetaDataGPU metaData; + // dimensions of data block + int dbXLength; + int dbYLength; + int dbZLength; + // gold standard and segmentation output array + torch::Tensor goldArr; + torch::Tensor segmArr; + TFB numberToLookFor; + int Nx; + int Ny; + int Nz; + + + // Frequent accesses to "a" and "b"; infrequent accesses to "x" and "y": + //cuda::annotated_ptr a_p{ a }, b_p{ b }; + //cuda::annotated_ptr x_s{ x }, y_s{ y }; + + int32_t* resultListPointerMeta; + int32_t* resultListPointerLocal; + int32_t* resultListPointerIterNumb; + + torch::Tensor resultListPointerIterNumbTensor; + + + + + uint32_t* origArrsPointer; + uint32_t* mainArrAPointer; + uint32_t* mainArrBPointer; + uint32_t* metaDataArrPointer; + + uint32_t* workQueuePointer; + unsigned int* minMaxes; + + + /* +main array with all required data organized in sections for each metadata block +x- is block dimx times block dimy +now what occupies what positions +##mainArrA +(0) - x-1 : reducedGoldRef +(x) - 2x-1 : reducedSegmRef +##mainArrB +() - 3x-1 : reducedGoldPrev +(x) - 4x-1 : reducedSegmPrev + +##metaDataArr +0: empty +1 :fpCount +2 :fnCount +3 :fpCounter +4 :fnCounter +5 :fpOffset +6 :fnOffset +7 :isActiveGold +8 :isFullGold +9 :isActiveSegm +10 :isFullSegm +11 :isToBeActivatedGold +12 :isToBeActivatedSegm +//now linear indexes of the blocks in all sides - if there is no block in given direction it will equal UINT32_MAX +13 : top +14 : bottom +15 : left +16 : right +17 : anterior +18 : posterior +19 : empty +20 : empty + + +###origArrs +0-x : reducedGold +(x+1) - 2x : reducedSegm + +*/ +// uint32_t* mainArr; + + + + + float robustnessPercent = 1.0;// 0.95; + +}; + + + + + +//just utility for unit testing - set some data bout points +#pragma once +extern "C" struct forTestPointStruct { + int x; + int y; + int z; + + bool isGold; + bool isGoldAndSegm; + + int xMeta; + int yMeta; + int zMeta; + + + + bool shouldBeInResAfterOneDil; + bool shouldBeInResAfterTwoDil; + bool isFoundAndDilatated = false; + bool isFoundInResult = false; + + bool isFoundDilTop = false; + bool isFoundDilBottom = false; + + bool isFoundDilAnterior = false; + bool isFoundDilPosterior = false; + + bool isFoundDilLeft = false; + bool isFoundDilRight = false; + +}; + + +#pragma once +extern "C" struct forTestMetaDataStruct { + + int xMeta; + int yMeta; + int zMeta; + + int requiredspaceInFpResultList; + int requiredspaceInFnResultList; + + bool isToBeActiveAtStart; + bool isToBeActiveAfterOneIter; + bool isToBeActiveAfterTwoIter; + + bool isToBeValidatedFpAfterOneIter; + bool isToBeValidatedFpAfterTwoIter; + + bool isToBeValidatedFnAfterOneIter; + bool isToBeValidatedFnAfterTwoIter; + + + bool isToBeFullAfterOneIter; + bool isToBeFullAfterTwoIter; + + int fpCount; + int fnCount; + + int fpConterAfterOneDil; + int fpConterAfterTwoDil; + + int fnConterAfterOneDil; + int fnConterAfterTwoDil; + + +}; + + +/* +data from occupancy calculator API used to get optimal number of thread blocks and threads per thread block +*/ +struct occupancyCalcData { + int warpsNumbForMinMax; + int blockSizeForMinMax; + + int warpsNumbForboolPrepareKernel; + int blockSizeFoboolPrepareKernel; + + int theadsForFirstMetaPass; + int blockForFirstMetaPass; + + int warpsNumbForMainPass; + int blockForMainPass; +}; + + + +/* +copy from host to device +*/ +#pragma once +inline MetaDataGPU allocateMetaDataOnGPU(MetaDataCPU metaDataCPU, unsigned int*& minMaxes) { + MetaDataGPU res; + + metaDataCPU.minMaxes[1] = 0; + metaDataCPU.minMaxes[2] = 1000; + metaDataCPU.minMaxes[3] = 0; + metaDataCPU.minMaxes[4] = 1000; + metaDataCPU.minMaxes[5] = 0; + metaDataCPU.minMaxes[6] = 1000; + metaDataCPU.minMaxes[7] = 0; + metaDataCPU.minMaxes[8] = 0; + metaDataCPU.minMaxes[9] = 0; + metaDataCPU.minMaxes[10] = 0; + metaDataCPU.minMaxes[11] = 0; + metaDataCPU.minMaxes[12] = 0; + metaDataCPU.minMaxes[13] = 0; + metaDataCPU.minMaxes[14] = 0; + metaDataCPU.minMaxes[15] = 0; + metaDataCPU.minMaxes[16] = 0; + metaDataCPU.minMaxes[17] = 0; + metaDataCPU.minMaxes[18] = 0; + metaDataCPU.minMaxes[19] = 0; + metaDataCPU.minMaxes[20] = 0; + + size_t size = sizeof(unsigned int) * 20; + cudaMemcpy(minMaxes, metaDataCPU.minMaxes, size, cudaMemcpyHostToDevice); + + //res.resultList = allocate3dInGPU(metaDataCPU.resultList); + + //res.metaXLength = metaDataCPU.metaXLength; + //res.MetaYLength = metaDataCPU.MetaYLength; + //res.MetaZLength = metaDataCPU.MetaZLength; + + //res.totalMetaLength = metaDataCPU.totalMetaLength; + //allocating on GPU and copying cpu data onto GPU + + return res; + +} + +/* +copy from device to host +*/ +#pragma once +inline void copyMetaDataToCPU(MetaDataCPU metaDataCPU, MetaDataGPU metaDataGPU) { + //copyDeviceToHost3d(metaDataGPU.fpCount, metaDataCPU.fpCount); + //copyDeviceToHost3d(metaDataGPU.fnCount, metaDataCPU.fnCount); + size_t size = sizeof(unsigned int) * 20; + + cudaMemcpy(metaDataCPU.minMaxes, metaDataGPU.minMaxes, size, cudaMemcpyDeviceToHost); +} + + +#pragma once +inline cudaError_t checkCuda(cudaError_t result, std::string description) +{ + if (result != cudaSuccess) { + std::cout << description; + std::cout << "\n"; + // printf("%d", description); + fprintf(stderr, "CUDA Runtime Error in %d : %s\n", description, cudaGetErrorString(result)); + assert(result == cudaSuccess); + } + return result; +} + + + + + +/* +setting the linear index of metadata blocks that are in given direction if there is no such (out of range) we will save it as UINT32_MAX +*/ +#pragma once +template +__device__ inline void setNeighbourBlocks(ForBoolKernelArgs fbArgs + , uint8_t idX, uint8_t inArrIndex, bool predicate, uint32_t toAdd + , uint32_t linIdexMeta, MetaDataGPU metaData, uint32_t localBlockMetaData[20]) { + + if ((threadIdx.x == idX) && (threadIdx.y == 0)) { + if (predicate) { + + + localBlockMetaData[inArrIndex] = (linIdexMeta + toAdd); + } + else { + localBlockMetaData[inArrIndex] = isGoldOffset; + } + }; +} + + + + +/* +iteration over metadata - becouse metadata may be small and to maximize occupancy we use linear index and then clalculate xMeta,ymeta,zMeta from this linear index ... +*/ +#pragma once +template +__global__ void getMinMaxes(ForBoolKernelArgs fbArgs + , unsigned int* minMaxes + , TYO* goldArr + , TYO* segmArr + , MetaDataGPU metaData +) { + + // __global__ void getMinMaxes(unsigned int* minMaxes) { + ////////////some initializations + thread_block cta = this_thread_block(); + //thread_block_tile<32> tile = tiled_partition<32>(cta); + + + + //shared memory + + __shared__ bool anyInGold[1]; + //__shared__ uint32_t reduction_s[32]; + //1)maxX 2)minX 3)maxY 4) minY 5) maxZ 6) minZ + __shared__ unsigned int minMaxesInShmem[7]; + + if ((threadIdx.x == 1) && (threadIdx.y == 0)) { minMaxesInShmem[1] = 0; }; + if ((threadIdx.x == 2) && (threadIdx.y == 0)) { minMaxesInShmem[2] = 1000; }; + + if ((threadIdx.x == 3) && (threadIdx.y == 0)) { minMaxesInShmem[3] = 0; }; + if ((threadIdx.x == 4) && (threadIdx.y == 0)) { minMaxesInShmem[4] = 1000; }; + + if ((threadIdx.x == 5) && (threadIdx.y == 0)) { minMaxesInShmem[5] = 0; }; + if ((threadIdx.x == 6) && (threadIdx.y == 0)) { minMaxesInShmem[6] = 1000; }; + + if ((threadIdx.x == 7) && (threadIdx.y == 0)) { anyInGold[1] = false; }; + + + if ((threadIdx.x == 1) && (threadIdx.y == 0) && (blockIdx.x == 0)) { + + + // if (blockIdx.x == 0) { + // printf(" dims meta in min maxes kernel Meta X %d MetaY %d metaZ %d dbXSize %d dbYsize %d dbZsize %d minX %d minY %d minZ \n " + // , metaData.metaXLength, metaData.MetaYLength, metaData.MetaZLength + // , fbArgs.dbXLength, fbArgs.dbYLength, fbArgs.dbZLength + // , metaData.minX, metaData.minY, metaData.minZ + // ); + + } + + __syncthreads(); + + ///////////////////////// + + + //main metadata iteration + for (auto linIdexMeta = blockIdx.x; linIdexMeta < metaData.totalMetaLength; linIdexMeta += gridDim.x) { + //we get from linear index the coordinates of the metadata block of intrest + int xMeta = linIdexMeta % metaData.metaXLength; + int zMeta = int(floor((float)(linIdexMeta / (fbArgs.metaData.metaXLength * metaData.MetaYLength)))); + int yMeta = int(floor((float)((linIdexMeta - ((zMeta * metaData.metaXLength * metaData.MetaYLength) + xMeta)) / metaData.metaXLength))); + //iterating over data block + for (uint8_t xLoc = threadIdx.x; xLoc < 32; xLoc += blockDim.x) { + uint32_t x = xMeta * fbArgs.dbXLength + xLoc;//absolute position + for (uint8_t yLoc = threadIdx.y; yLoc < fbArgs.dbYLength; yLoc += blockDim.y) { + uint32_t y = yMeta * fbArgs.dbYLength + yLoc;//absolute position + //if (y == 0) { + // printf("x %d in min maxes \n ", x); + + //} + if (y < fbArgs.Ny && x < fbArgs.Nx) { + + // resetting + + + for (uint8_t zLoc = 0; zLoc < fbArgs.dbZLength; zLoc++) { + uint32_t z = zMeta * fbArgs.dbZLength + zLoc;//absolute position + if (z < fbArgs.Nz) { + //first array gold + //uint8_t& zLocRef = zLoc; uint8_t& yLocRef = yLoc; uint8_t& xLocRef = xLoc; + + // setting bits + bool goldBool = goldArr[x + y * fbArgs.Nx + z * fbArgs.Nx * fbArgs.Ny] == fbArgs.numberToLookFor; // (getTensorRow(tensorslice, fbArgs.goldArr, fbArgs.goldArr.Ny, y, z)[x] == fbArgs.numberToLookFor); + bool segmBool = segmArr[x + y * fbArgs.Nx + z * fbArgs.Nx * fbArgs.Ny] == fbArgs.numberToLookFor; + if (goldBool || segmBool) { + anyInGold[0] = true; + //printf(" \n in min maxes dims meta in min maxes x %d y%d z%d xMeta %d yMeta %d zMeta %d kernel Meta X %d MetaY %d metaZ %d dbXSize %d dbYsize %d dbZsize %d minX %d minY %d minZ %d linIdexMeta %d counted %d \n " + // ,x,y,z, + // xMeta,yMeta,zMeta + // , metaData.metaXLength, metaData.MetaYLength, metaData.MetaZLength + // , fbArgs.dbXLength, fbArgs.dbYLength, fbArgs.dbZLength + // , metaData.minX, metaData.minY, metaData.minZ + // , linIdexMeta + // , int(floor((float)((linIdexMeta - ((zMeta * metaData.metaXLength * metaData.MetaYLength) + xMeta)) / metaData.metaXLength))) + //); + + } + + + + } + + } + } + + // __syncthreads(); + //waiting so shared memory will be loaded evrywhere + //on single thread we do last sum reduction + + /////////////////// setting min and maxes + // //1)maxX 2)minX 3)maxY 4) minY 5) maxZ 6) minZ + auto active = coalesced_threads(); + sync(cta); + active.sync(); + + if ((threadIdx.x == 0) && (threadIdx.y == 0) && anyInGold[0]) { minMaxesInShmem[1] = max(xMeta, minMaxesInShmem[1]); }; + if ((threadIdx.x == 1) && (threadIdx.y == 0) && anyInGold[0]) { minMaxesInShmem[2] = min(xMeta, minMaxesInShmem[2]); }; + + if ((threadIdx.x == 2) && (threadIdx.y == 0) && anyInGold[0]) { + + minMaxesInShmem[3] = max(yMeta, minMaxesInShmem[3]); + + //if (minMaxesInShmem[3] > 0) { + // printf(" prim minMaxesInShmem maxY %d meta %d \n ", minMaxesInShmem[3], yMeta); + //} + + }; + if ((threadIdx.x == 3) && (threadIdx.y == 0) && anyInGold[0]) { minMaxesInShmem[4] = min(yMeta, minMaxesInShmem[4]); }; + + if ((threadIdx.x == 4) && (threadIdx.y == 0) && anyInGold[0]) { minMaxesInShmem[5] = max(zMeta, minMaxesInShmem[5]); }; + if ((threadIdx.x == 5) && (threadIdx.y == 0) && anyInGold[0]) { + minMaxesInShmem[6] = min(zMeta, minMaxesInShmem[6]); + // printf("local fifth %d \n", minMaxesInShmem[6]); + }; + // active.sync(); + sync(cta); // just to reduce the warp divergence + anyInGold[0] = false; + + + + + } + } + + } + sync(cta); + + auto active = coalesced_threads(); + + if ((threadIdx.x == 1) && (threadIdx.y == 0)) { + // printf("\n in minMaxes internal %d \n", minMaxesInShmem[1]); + //getTensorRow(tensorslice, fbArgs.metaData.minMaxes, fbArgs.metaData.minMaxes.Ny, 0, 0)[0] = 61; + atomicMax(&minMaxes[1], minMaxesInShmem[1]); + //atomicMax(&minMaxes[1], 2); + // minMaxes[1] = 0; + }; + + if ((threadIdx.x == 0) && (threadIdx.y == 0)) { + + atomicMin(&minMaxes[2], minMaxesInShmem[2]); + }; + + if ((threadIdx.x == 1) && (threadIdx.y == 0)) { + atomicMax(&minMaxes[3], minMaxesInShmem[3]); + // printf(" minMaxesInShmem maxY %d \n ", minMaxes[3]); + + }; + + if ((threadIdx.x == 2) && (threadIdx.y == 0)) { + atomicMin(&minMaxes[4], minMaxesInShmem[4]); + // printf(" minMaxesInShmem minY %d \n ", minMaxes[4]); + + }; + + + + if (threadIdx.x == 3 && threadIdx.y == 0) { + atomicMax(&minMaxes[5], minMaxesInShmem[5]); + // printf(" minMaxesInShmem %d \n ", minMaxes[5]); + }; + + if (threadIdx.x == 4 && threadIdx.y == 0) { + atomicMin(&minMaxes[6], minMaxesInShmem[6]); + // printf(" minMaxesInShmem %d \n ", minMaxes[6]); + + }; + + + + + +} + + + +/* +iteration over metadata - becouse metadata may be small and to maximize occupancy we use linear index and then clalculate xMeta,ymeta,zMeta from this linear index ... +*/ +#pragma once +template +__global__ void boolPrepareKernel(ForBoolKernelArgs fbArgs + , MetaDataGPU metaData, uint32_t* origArrs, uint32_t* metaDataArr + , TYO* goldArr + , TYO* segmArr + , unsigned int* minMaxes) { + + ////////////some initializations + bool goldBool = false; + bool segmBool = false; + bool isNotEmpty = false; + + thread_block cta = this_thread_block(); + thread_block_tile<32> tile = tiled_partition<32>(cta); + uint32_t sumFp = 0; + uint32_t sumFn = 0; + + //auto pipeline = cuda::make_pipeline(); + + + //shared memory + + //TODO() make it dynamically sized + __shared__ uint32_t sharedForGold[1024]; + __shared__ uint32_t sharedForSegm[1024]; + + + //for storing fp and fn sums to later accumulate it to global values + __shared__ uint32_t fpSFnS[2]; + __shared__ uint32_t localBlockMetaData[20]; + + __shared__ bool anyInGold[1]; + __shared__ bool anyInSegm[1]; + //__shared__ uint32_t reduction_s[32]; + //1)maxX 2)minX 3)maxY 4) minY 5) maxZ 6) minZ + __shared__ int minMaxesInShmem[7]; + + + + if ((threadIdx.x == 1) && (threadIdx.y == 1)) { fpSFnS[0] = 0; }; + if ((threadIdx.x == 2) && (threadIdx.y == 1)) { fpSFnS[1] = 0; }; + if ((threadIdx.x == 3) && (threadIdx.y == 1)) { anyInGold[1] = false; }; + if ((threadIdx.x == 4) && (threadIdx.y == 1)) { + anyInSegm[1] = false; + + + }; + + + + sync(cta); + + ///////////////////////// + + + //main metadata iteration + for (uint32_t linIdexMeta = blockIdx.x; linIdexMeta < metaData.totalMetaLength; linIdexMeta += gridDim.x) { + //we get from linear index the coordinates of the metadata block of intrest + int xMeta = int(linIdexMeta % (metaData.metaXLength)); + int zMeta = int(floor((float)(linIdexMeta / (metaData.metaXLength * metaData.MetaYLength)))); + int yMeta = int(floor((float)((linIdexMeta - ((zMeta * metaData.metaXLength * metaData.MetaYLength) + xMeta)) / metaData.metaXLength))); + //reset + isNotEmpty = false; + sumFp = 0; + sumFn = 0; + anyInGold[0] = false; + anyInSegm[0] = false; + //iterating over data block + sync(cta); + for (uint8_t xLoc = threadIdx.x; xLoc < fbArgs.dbXLength; xLoc += blockDim.x) { + uint32_t x = (xMeta + metaData.minX) * fbArgs.dbXLength + xLoc;//absolute position + for (uint8_t yLoc = threadIdx.y; yLoc < fbArgs.dbYLength; yLoc += blockDim.y) { + uint32_t y = (yMeta + metaData.minY) * fbArgs.dbYLength + yLoc;//absolute position + if (y < fbArgs.Ny && x < fbArgs.Nx) { + + // resetting + sharedForGold[xLoc + yLoc * fbArgs.dbXLength] = 0; + sharedForSegm[xLoc + yLoc * fbArgs.dbXLength] = 0; + + + for (uint8_t zLoc = 0; zLoc < fbArgs.dbZLength; zLoc++) { + uint32_t z = (zMeta + metaData.minZ) * fbArgs.dbZLength + zLoc;//absolute position + if (z < fbArgs.Nz) { + //char* tensorslice; + + //first array gold + bool goldBool = goldArr[x + y * fbArgs.Nx + z * fbArgs.Nx * fbArgs.Ny] == fbArgs.numberToLookFor; + bool segmBool = segmArr[x + y * fbArgs.Nx + z * fbArgs.Nx * fbArgs.Ny] == fbArgs.numberToLookFor; + //goldBool = true; + + // setting bits + sharedForGold[xLoc + yLoc * fbArgs.dbXLength] |= goldBool << zLoc; + sharedForSegm[xLoc + yLoc * fbArgs.dbXLength] |= segmBool << zLoc; + // setting value of local boolean marking that any of the entries was evaluated to true in either of arrays + isNotEmpty = (isNotEmpty || (goldBool || segmBool)); + sumFp += (!goldBool && segmBool); + sumFn += (goldBool && !segmBool); + if (goldBool) anyInGold[0] = true; + if (segmBool) anyInSegm[0] = true; + + //if (goldBool) { + // printf("in kernel gold x %d y %d z %d xMeta %d yMeta %d zMeta %d counted ymeta %d linmeta %d \n", x, y, z, xMeta, yMeta,zMeta + // , int(floor((float)((linIdexMeta - ((zMeta * metaData.metaXLength * metaData.MetaYLength) + xMeta)) / metaData.metaXLength))) + // , linIdexMeta); + //} + + //if (segmBool) { + // printf("in kernel segm x %d y %d z %d xMeta %d yMeta %d zMeta %d counted ymeta %d linmeta %d \n", x, y, z, xMeta, yMeta, zMeta + // , int(floor((float)((linIdexMeta - ((zMeta * metaData.metaXLength * metaData.MetaYLength) + xMeta)) / metaData.metaXLength))) + // , linIdexMeta); + //} + + + } + } + } + + //if (sharedForGold[xLoc + yLoc * fbArgs.dbXLength] > 0) { + // printf("in kernel Metax %d yMeta %d zMeta %d linearLocal %d linIdexMeta %d column %d \n" + // , xMeta, yMeta, zMeta, xLoc + yLoc * fbArgs.dbXLength, linIdexMeta + // , sharedForGold[xLoc + yLoc * fbArgs.dbXLength]); + //} + + + } + } + //reset local metadata + if ((threadIdx.x < 20) && (threadIdx.y == 0)) { + localBlockMetaData[threadIdx.x] = 0; + } + + + + isNotEmpty = __syncthreads_or(isNotEmpty); + //exporting to global memory + for (uint8_t xLoc = threadIdx.x; xLoc < fbArgs.dbXLength; xLoc += blockDim.x) { + uint32_t x = (xMeta + metaData.minX) * fbArgs.dbXLength + xLoc;//absolute position + for (uint8_t yLoc = threadIdx.y; yLoc < fbArgs.dbYLength; yLoc += blockDim.y) { + uint32_t y = (yMeta + metaData.minY) * fbArgs.dbYLength + yLoc;//absolute position + if (y < fbArgs.Ny && x < fbArgs.Nx) { + + origArrs[linIdexMeta * metaData.mainArrSectionLength + yLoc * 32 + xLoc] = sharedForGold[yLoc * 32 + xLoc]; + origArrs[linIdexMeta * metaData.mainArrSectionLength + yLoc * 32 + xLoc + metaData.mainArrXLength] = sharedForSegm[yLoc * 32 + xLoc]; + + + } + } + } + + // sync(cta); + + + + //cuda::memcpy_async(cta, (&origArrs[linIdexMeta * metaData.mainArrSectionLength]) , (sharedForGold), sizeof(uint32_t) * cta.size(), barrier); + //barrier.arrive_and_wait(); // Waits for all copies to complete + + + // cuda::memcpy_async(cta, (&origArrs[linIdexMeta * metaData.mainArrSectionLength]), (sharedForGoldB), (sizeof(uint32_t) * blockDim.x * blockDim.y), barrier); + //barrier.arrive_and_wait(); // Waits for all copies to complete + + //cuda::memcpy_async(cta, (&origArrs[linIdexMeta * metaData.mainArrSectionLength + metaData.mainArrXLength]), (sharedForSegmB), (sizeof(uint32_t) * blockDim.x * blockDim.y), barrier); + //barrier.arrive_and_wait(); // Waits for all copies to complete + + //cuda::memcpy_async(cta, (&mainArr[linIdexMeta * metaData.mainArrSectionLength ]), (sharedForGoldB), (sizeof(uint32_t) * blockDim.x * blockDim.y), barrier); + // barrier.arrive_and_wait(); // Waits for all copies to complete + + // cuda::memcpy_async(cta, (&mainArr[linIdexMeta * metaData.mainArrSectionLength + metaData.mainArrXLength*1]), (sharedForSegmB), (sizeof(uint32_t) * blockDim.x * blockDim.y) , barrier); + // barrier.arrive_and_wait(); // Waits for all copies to complete + + // cuda::memcpy_async(cta, (&mainArr[linIdexMeta * metaData.mainArrSectionLength + metaData.mainArrXLength*2]), (sharedForGoldB), (sizeof(uint32_t) * blockDim.x * blockDim.y), barrier); + // barrier.arrive_and_wait(); // Waits for all copies to complete + + // cuda::memcpy_async(cta, (&mainArr[linIdexMeta * metaData.mainArrSectionLength + metaData.mainArrXLength*3]), (sharedForSegmB), (sizeof(uint32_t) * blockDim.x * blockDim.y), barrier); + // barrier.arrive_and_wait(); // Waits for all copies to complete + + sync(cta); + + + + /////adding the block and total number of the Fp's and Fn's + sumFp = reduce(tile, sumFp, plus()); + sumFn = reduce(tile, sumFn, plus()); + //reusing shared memory and adding accumulated values from tiles + if (tile.thread_rank() == 0) { + sharedForGold[tile.meta_group_rank()] = sumFp; + sharedForSegm[tile.meta_group_rank()] = sumFn; + } + sync(cta);//waiting so shared memory will be loaded evrywhere + //on single thread we do last sum reduction + auto active = coalesced_threads(); + + //if ((threadIdx.x == 0) && (threadIdx.y == 0)) { + // printf("xMeta %d yMeta %d zMeta %d \n", xMeta, yMeta, zMeta); + //} + + if ((threadIdx.x == 0) && (threadIdx.y == 0) && isNotEmpty) { + sharedForGold[33] = 0;//reset + for (int i = 0; i < tile.meta_group_size(); i += 1) { + sharedForGold[33] += sharedForGold[i]; + /* if (sharedForGold[i]>0) { + printf("adding sharedForGold[i] %d in gold \n ", sharedForGold[i]); + }*/ + + }; + fpSFnS[0] += sharedForGold[33];// will be needed later for global set + //metaDataArr[linIdexMeta * metaData.metaDataSectionLength + 1] = sharedForGold[33]; + localBlockMetaData[1] = sharedForGold[33]; + + } + // if (isToBeExecutedOnActive(active, 1) && isNotEmpty) { + if ((threadIdx.x == 0) && (threadIdx.y == 1) && isNotEmpty) { + + + sharedForSegm[33] = 0;//reset + for (int i = 0; i < tile.meta_group_size(); i += 1) { + sharedForSegm[33] += sharedForSegm[i]; + }; + fpSFnS[1] += sharedForSegm[33];// will be needed later for global set + //setting metadata + localBlockMetaData[2] = sharedForSegm[33]; + + } + + //marking as active +//FP pass + if ((threadIdx.x == 0) && (threadIdx.y == 0) && isNotEmpty && anyInGold[0]) { + localBlockMetaData[7] = 1; + + }; + //FN pass + if ((threadIdx.x == 1) && (threadIdx.y == 0) && isNotEmpty && anyInSegm[0]) { + localBlockMetaData[9] = 1; + + }; + + + //after we streamed over all block we save also information about indicies of the surrounding blocks - given they are in range if not UINT32_MAX will be saved + //top + + + + setNeighbourBlocks(fbArgs, 3, 13, (zMeta > 0), (-(metaData.metaXLength * metaData.MetaYLength)), linIdexMeta, metaData, localBlockMetaData);//top + setNeighbourBlocks(fbArgs, 4, 14, (zMeta < (metaData.MetaZLength - 1)), (metaData.metaXLength * metaData.MetaYLength), linIdexMeta, metaData, localBlockMetaData);//bottom + + setNeighbourBlocks(fbArgs, 6, 15, (xMeta > 0), (-1), linIdexMeta, metaData, localBlockMetaData);//left + setNeighbourBlocks(fbArgs, 7, 16, (xMeta < (metaData.metaXLength - 1)), 1, linIdexMeta, metaData, localBlockMetaData);//right + + setNeighbourBlocks(fbArgs, 8, 17, (yMeta < (metaData.MetaYLength - 1)), metaData.metaXLength, linIdexMeta, metaData, localBlockMetaData);//anterior + setNeighbourBlocks(fbArgs, 9, 18, (yMeta > 0), (-metaData.metaXLength), linIdexMeta, metaData, localBlockMetaData);//posterior + + if ((threadIdx.x < 20) && (threadIdx.y == 0)) { + metaDataArr[linIdexMeta * metaData.metaDataSectionLength + threadIdx.x] = localBlockMetaData[threadIdx.x]; + }; + + sync(cta); // just to reduce the warp divergence + + // copy metadata to global memory + + //cuda::memcpy_async(cta, &metaDataArr[linIdexMeta * metaData.metaDataSectionLength], (&localBlockMetaData[0]), (sizeof(uint32_t) * 20), barrier); + // barrier.arrive_and_wait(); // Waits for all copies to complete + + } + + + + sync(cta); + + + //setting global fp and fn + if ((threadIdx.x == 0) && (threadIdx.y == 0)) { + atomicAdd(&(minMaxes[7]), fpSFnS[0]); + }; + + if ((threadIdx.x == 1) && (threadIdx.y == 0)) { + atomicAdd(&(minMaxes[8]), fpSFnS[1]); + + }; +} + + + +/* + a) we define offsets in the result list to have the results organized and avoid overwiting + b) if metadata block is active we add it in the work queue +*/ + + +/* +we add here to appropriate queue data about metadata of blocks of intrest +minMaxesPos- marks in minmaxes the postion of global offset counter -12) global FP offset 13) global FnOffset +offsetMetadataArr- arrays from metadata holding data about result list offsets it can be either fbArgs.metaData.fpOffset or fbArgs.metaData.fnOffset +*/ + + +#pragma once +__device__ inline void addToQueue(uint32_t linIdexMeta, uint8_t isGold + , unsigned int fpFnLocCounter[1], uint32_t localWorkQueue[1600], uint32_t localOffsetQueue[1600], unsigned int localWorkQueueCounter[1] + , uint8_t countIndexNumb, uint8_t isActiveIndexNumb, uint8_t offsetIndexNumb + , uint32_t* metaDataArr, MetaDataGPU metaData, unsigned int* minMaxes, uint32_t* workQueue) { + + unsigned int count = metaDataArr[linIdexMeta * metaData.metaDataSectionLength + countIndexNumb]; + //given fp is non zero we need to add this to local queue + if (metaDataArr[linIdexMeta * metaData.metaDataSectionLength + isActiveIndexNumb] == 1) { + + // printf("adding to local in first meta pass linIdexMeta %d isGold %d isActiveIndexNumb %d \n ", linIdexMeta, isGold, isActiveIndexNumb); + + count = atomicAdd_block(&fpFnLocCounter[0], count); + unsigned int old = atomicAdd_block(&localWorkQueueCounter[0], 1); + //we check weather we still have space in shared memory + if (old < 1590) {// so we still have space in shared memory + // will be equal or above isGoldOffset if it is gold pass + localWorkQueue[old] = linIdexMeta + (isGoldOffset * isGold); + localOffsetQueue[old] = uint32_t(count); + } + else {// so we do not have any space more in the sared memory - it is unlikely so we will just in this case save immidiately to global memory + old = atomicAdd(&(minMaxes[9]), old); + //workQueue + workQueue[old] = linIdexMeta + (isGoldOffset * isGold); + //and offset + metaDataArr[linIdexMeta * metaData.metaDataSectionLength + offsetIndexNumb] = atomicAdd(&(minMaxes[12]), count); + }; + } +} + + +#pragma once +template +__global__ void firstMetaPrepareKernel(ForBoolKernelArgs fbArgs + , MetaDataGPU metaData, unsigned int* minMaxes, uint32_t* workQueue + , uint32_t* origArrs, uint32_t* metaDataArr) { + + //////initializations + thread_block cta = this_thread_block(); + char* tensorslice;// needed for iterations over 3d arrays + //local offset counters for fp and fn's + __shared__ unsigned int fpFnLocCounter[1]; + // used to store the start position in global memory for whole block + __shared__ unsigned int globalOffsetForBlock[1]; + __shared__ unsigned int globalWorkQueueCounter[1]; + //used as local work queue counter + __shared__ unsigned int localWorkQueueCounter[1]; + //according to https://forums.developer.nvidia.com/t/find-the-limit-of-shared-memory-that-can-be-used-per-block/48556 it is good to keep shared memory below 16kb kilo bytes so it will give us 1600 length of shared memory + //so here we will store locally the calculated offsets and coordinates of meta data block of intrest marking also wheather we are talking about gold or segmentation pass (fp or fn ) + __shared__ uint32_t localWorkQueue[1600]; + __shared__ uint32_t localOffsetQueue[1600]; + if ((threadIdx.x == 0)) { + fpFnLocCounter[0] = 0; + } + if ((threadIdx.x == 1)) { + localWorkQueueCounter[0] = 0; + } + if ((threadIdx.x == 2)) { + globalWorkQueueCounter[0] = 0; + } + if ((threadIdx.x == 3)) { + globalOffsetForBlock[0] = 0; + } + sync(cta); + + + // classical grid stride loop - in case of unlikely event we will run out of space we will empty it prematurly + //main metadata iteration + for (uint32_t linIdexMeta = blockIdx.x * blockDim.x + threadIdx.x; linIdexMeta < metaData.totalMetaLength; linIdexMeta += blockDim.x * gridDim.x) { + + // if ((threadIdx.x == 0) && (threadIdx.y == 0)) { + // printf("in first meta pass linIdexMeta %d blockIdx.x %d blockDim.x %d metaData.totalMetaLength %d threadIdx.x %d \n ", linIdexMeta, blockIdx.x, blockDim.x, metaData.totalMetaLength, threadIdx.x ); + //} + + //goldpass + addToQueue(linIdexMeta, 0 + , fpFnLocCounter, localWorkQueue, localOffsetQueue, localWorkQueueCounter + , 1, 9, 6 + , metaDataArr, metaData, minMaxes, workQueue); + //segmPass + addToQueue(linIdexMeta, 1 + , fpFnLocCounter, localWorkQueue, localOffsetQueue, localWorkQueueCounter + , 2, 7, 5 + , metaDataArr, metaData, minMaxes, workQueue); + + + + /* addToQueue(fbArgs, old, count, tensorslice, xMeta, yMeta, zMeta, fbArgs.metaData.fpOffset, fbArgs.metaData.fpCount, 0, fbArgs.metaData.isActiveSegm, fpFnLocCounter, localWorkAndOffsetQueue, localWorkQueueCounter); + addToQueue(fbArgs, old, count, tensorslice, xMeta, yMeta, zMeta, fbArgs.metaData.fnOffset, fbArgs.metaData.fnCount, 1, fbArgs.metaData.isActiveGold, fpFnLocCounter, localWorkAndOffsetQueue, localWorkQueueCounter);*/ + } + sync(cta); + if ((threadIdx.x == 0)) { + globalOffsetForBlock[0] = atomicAdd(&(minMaxes[12]), (fpFnLocCounter[0])); + + /* if (fpFnLocCounter[0]>0) { + printf("\n in meta first pass global offset %d locCounter %d \n ", globalOffsetForBlock[0], fpFnLocCounter[0]); + }*/ + }; + if ((threadIdx.x == 1)) { + if (localWorkQueueCounter[0] > 0) { + globalWorkQueueCounter[0] = atomicAdd(&(minMaxes[9]), (localWorkQueueCounter[0])); + } + } + sync(cta); + + //exporting to global work queue + //cooperative_groups::memcpy_async(cta, (&workQueue[globalWorkQueueCounter[0]]), (localWorkQueue), (sizeof(uint32_t) * localWorkQueueCounter[0])); + + + //setting offsets + for (uint32_t i = threadIdx.x; i < localWorkQueueCounter[0]; i += blockDim.x) { + workQueue[globalWorkQueueCounter[0] + i] = localWorkQueue[i]; + + /* printf("FFIrst meta pass lin meta to Work Q %d is gold %d to spot %d \n " + , localWorkQueue[i] - isGoldOffset*(localWorkQueue[i] >= isGoldOffset) + , (localWorkQueue[i] >= isGoldOffset), globalWorkQueueCounter[0] + i);*/ + + //FP pass + if (localWorkQueue[i] >= isGoldOffset) { + metaDataArr[(localWorkQueue[i] - isGoldOffset) * metaData.metaDataSectionLength + 5] = localOffsetQueue[i] + globalOffsetForBlock[0]; + //printf("fp offset lin meta %d total offset %d global part %d local part %d \n " + // , localWorkQueue[i] - isGoldOffset + // , localOffsetQueue[i] + globalOffsetForBlock[0] + //, globalOffsetForBlock[0] + //, localOffsetQueue[i]); + + } + //FN pass + else { + metaDataArr[(localWorkQueue[i]) * metaData.metaDataSectionLength + 6] = localOffsetQueue[i] + globalOffsetForBlock[0]; + //printf("fn offset lin meta %d total offset %d global part %d local part %d \n " + // , localWorkQueue[i] + // , localOffsetQueue[i] + globalOffsetForBlock[0] + // , globalOffsetForBlock[0] + // , localOffsetQueue[i]); + + }; + + //sync(cta); + + + } + + + +}; + + + + + + + +// helper functions and utilities to work with CUDA from https://github.com/NVIDIA/cuda-samples + + + + + + + + +/* +*/ +#pragma once +template +inline ForBoolKernelArgs getArgsForKernel(ForFullBoolPrepArgs& mainFunArgs + , int& warpsNumbForMainPass, int& blockForMainPass + , const int xLen, const int yLen, const int zLen, cudaStream_t stream +) { + + + mainFunArgs.Nx = xLen; + mainFunArgs.Ny = yLen; + mainFunArgs.Nz = zLen; + + //main arrays allocations + //TCC* goldArrPointer; + //TCC* segmArrPointer; + //size_t sizeMainArr = (sizeof(T) * WIDTH * HEIGHT * DEPTH); + //size_t sizeMainArr = (sizeof(TCC) * xLen * yLen * zLen); + //array3dWithDimsGPU goldArr = allocateMainArray(goldArrPointer, mainFunArgs.goldArr.arrP, xLen, yLen, zLen, stream); + //array3dWithDimsGPU segmArr = allocateMainArray(segmArrPointer, mainFunArgs.segmArr.arrP, xLen, yLen, zLen, stream); + unsigned int* minMaxes; + size_t sizeminMaxes = sizeof(unsigned int) * 20; + cudaMallocAsync(&minMaxes, sizeminMaxes, stream); + ForBoolKernelArgs res; + + res.Nx = xLen; + res.Ny = yLen; + res.Nz = zLen; + + res.metaData = allocateMetaDataOnGPU(mainFunArgs.metaData, minMaxes); + res.metaData.minMaxes = minMaxes; + res.minMaxes = minMaxes; + res.numberToLookFor = mainFunArgs.numberToLookFor; + res.dbXLength = 32; + res.dbYLength = warpsNumbForMainPass; + res.dbZLength = 32; + + //printf("in setting bool args ylen %d dbYlen %d calculated meta %d \n ", yLen, res.dbYLength, int(ceil(yLen / res.dbYLength))); + res.metaData.metaXLength = int(ceil(xLen / res.dbXLength)); + res.metaData.MetaYLength = int(ceil(yLen / res.dbYLength));; + res.metaData.MetaZLength = int(ceil(zLen / res.dbZLength));; + res.metaData.minX = 0; + res.metaData.minY = 0; + res.metaData.minZ = 0; + res.metaData.maxX = res.metaData.metaXLength; + res.metaData.maxY = res.metaData.MetaYLength; + res.metaData.maxZ = res.metaData.MetaZLength; + + res.metaData.totalMetaLength = res.metaData.metaXLength * res.metaData.MetaYLength * res.metaData.MetaZLength; + res.goldArr = mainFunArgs.goldArr; + res.segmArr = mainFunArgs.segmArr; + + + return res; +} + + + + + + + +#pragma once +template +inline MetaDataGPU allocateMemoryAfterMinMaxesKernel(ForBoolKernelArgs& gpuArgs, ForFullBoolPrepArgs& cpuArgs, cudaStream_t stream) { + ////reduced arrays + uint32_t* origArr; + uint32_t* metaDataArr; + uint32_t* workQueue; + //copy on cpu + size_t size = sizeof(unsigned int) * 20; + cudaMemcpyAsync(cpuArgs.metaData.minMaxes, gpuArgs.minMaxes, size, cudaMemcpyDeviceToHost, stream); + + //read an modify + //1)maxX 2)minX 3)maxY 4) minY 5) maxZ 6) minZ + //7)global FP count; 8)global FN count + unsigned int xRange = cpuArgs.metaData.minMaxes[1] - cpuArgs.metaData.minMaxes[2] + 1; + unsigned int yRange = cpuArgs.metaData.minMaxes[3] - cpuArgs.metaData.minMaxes[4] + 1; + unsigned int zRange = cpuArgs.metaData.minMaxes[5] - cpuArgs.metaData.minMaxes[6] + 1; + unsigned int totalMetaLength = (xRange) * (yRange) * (zRange); + //updating size informations + gpuArgs.metaData.metaXLength = xRange; + gpuArgs.metaData.MetaYLength = yRange; + gpuArgs.metaData.MetaZLength = zRange; + gpuArgs.metaData.totalMetaLength = totalMetaLength; + //saving min maxes + gpuArgs.metaData.maxX = cpuArgs.metaData.minMaxes[1]; + gpuArgs.metaData.minX = cpuArgs.metaData.minMaxes[2]; + gpuArgs.metaData.maxY = cpuArgs.metaData.minMaxes[3]; + gpuArgs.metaData.minY = cpuArgs.metaData.minMaxes[4]; + gpuArgs.metaData.maxZ = cpuArgs.metaData.minMaxes[5]; + gpuArgs.metaData.minZ = cpuArgs.metaData.minMaxes[6]; + + //allocating needed memory + // main array + unsigned int mainArrXLength = gpuArgs.dbXLength * gpuArgs.dbYLength; + unsigned int mainArrSectionLength = (mainArrXLength * 2); + gpuArgs.metaData.mainArrXLength = mainArrXLength; + gpuArgs.metaData.mainArrSectionLength = mainArrSectionLength; + + size_t sizeB = totalMetaLength * mainArrSectionLength * sizeof(uint32_t); + //cudaMallocAsync(&mainArr, sizeB, 0); + size_t sizeorigArr = totalMetaLength * (mainArrXLength * 2) * sizeof(uint32_t); + cudaMallocAsync(&origArr, sizeorigArr, stream); + size_t sizemetaDataArr = totalMetaLength * (20) * sizeof(uint32_t) + 100; + cudaMallocAsync(&metaDataArr, sizemetaDataArr, stream); + size_t sizeC = (totalMetaLength * 2 * sizeof(uint32_t) + 50); + cudaMallocAsync(&workQueue, sizeC, stream); + gpuArgs.origArrsPointer = origArr; + gpuArgs.metaDataArrPointer = metaDataArr; + gpuArgs.workQueuePointer = workQueue; + return gpuArgs.metaData; +}; + + + + +/* +becouse we need a lot of the additional memory spaces to minimize memory consumption allocations will be postponed after first kernel run enabling +*/ +#pragma once +template +inline int allocateMemoryAfterBoolKernel(ForBoolKernelArgs& gpuArgs, ForFullBoolPrepArgs& cpuArgs, cudaStream_t stream, bool resIterNeeded) { + + int32_t* resultListPointerMeta; + int32_t* resultListPointerLocal; + int32_t* resultListPointerIterNumb; + uint32_t* mainArrAPointer; + uint32_t* mainArrBPointer; + //free no longer needed arrays + //cudaFreeAsync(gpuArgs.goldArr.arrP, stream); + //cudaFreeAsync(gpuArgs.segmArr.arrP, stream); + + //copy on cpu + size_t size = sizeof(unsigned int) * 20; + cudaMemcpyAsync(cpuArgs.metaData.minMaxes, gpuArgs.metaData.minMaxes, size, cudaMemcpyDeviceToHost, stream); + + unsigned int fpPlusFn = cpuArgs.metaData.minMaxes[7] + cpuArgs.metaData.minMaxes[8]; + size = sizeof(int32_t) * (fpPlusFn + 50); + + + cudaMallocAsync(&resultListPointerLocal, size, stream); + cudaMallocAsync(&resultListPointerIterNumb, size, stream); + cudaMallocAsync(&resultListPointerMeta, size, stream); + + +// we will allocate only if we need + if (resIterNeeded) { + auto options = + torch::TensorOptions() + .dtype(torch::kInt32) + .device(torch::kCUDA, 0) + .requires_grad(false); + + auto resultListPointerIterNumbTensor = torch::empty((fpPlusFn + 50), options); + gpuArgs.resultListPointerIterNumbTensor = resultListPointerIterNumbTensor; + } + + + auto xRange = gpuArgs.metaData.metaXLength; + auto yRange = gpuArgs.metaData.MetaYLength; + auto zRange = gpuArgs.metaData.MetaZLength; + + + size_t sizeB = gpuArgs.metaData.totalMetaLength * gpuArgs.metaData.mainArrSectionLength * sizeof(uint32_t); + + //printf("size of reduced main arr %d total meta len %d mainArrSectionLen %d \n", sizeB, metaData.totalMetaLength, metaData.mainArrSectionLength); + + cudaMallocAsync(&mainArrAPointer, sizeB, 0); + cudaMemcpyAsync(mainArrAPointer, gpuArgs.origArrsPointer, sizeB, cudaMemcpyDeviceToDevice, stream); + + + cudaMallocAsync(&mainArrBPointer, sizeB, 0); + cudaMemcpyAsync(mainArrBPointer, gpuArgs.origArrsPointer, sizeB, cudaMemcpyDeviceToDevice, stream); + + //just in order set it to 0 + int32_t* resultListPointerMetaCPU = (int32_t*)calloc(fpPlusFn + 50, sizeof(int32_t)); + cudaMemcpyAsync(resultListPointerMeta, resultListPointerMetaCPU, size, cudaMemcpyHostToDevice, stream); + cudaMemcpyAsync(resultListPointerIterNumb, resultListPointerMetaCPU, size, cudaMemcpyHostToDevice, stream); + free(resultListPointerMetaCPU); + + gpuArgs.resultListPointerMeta = resultListPointerMeta; + gpuArgs.resultListPointerLocal = resultListPointerLocal; + gpuArgs.resultListPointerIterNumb = resultListPointerIterNumb; + + //fbArgs.origArrsPointer = origArrsPointer; + gpuArgs.mainArrAPointer = mainArrAPointer; + gpuArgs.mainArrBPointer = mainArrBPointer; + + + return fpPlusFn; +}; + + + + + + + + +#pragma once +template +inline void copyResultstoCPU(ForBoolKernelArgs& gpuArgs, ForFullBoolPrepArgs& cpuArgs, cudaStream_t stream) { + + + ////copy on cpu + size_t size = sizeof(unsigned int) * 20; + cudaMemcpyAsync(cpuArgs.metaData.minMaxes, gpuArgs.metaData.minMaxes, size, cudaMemcpyDeviceToHost, stream); + unsigned int fpPlusFn = cpuArgs.metaData.minMaxes[7] + cpuArgs.metaData.minMaxes[8]; + size = sizeof(uint32_t) * (fpPlusFn + 50); + + //uint32_t* resultListPointerMeta = (uint32_t*)calloc(fpPlusFn + 50, sizeof(uint32_t)); + //uint32_t* resultListPointerLocal = (uint32_t*)calloc(fpPlusFn + 50, sizeof(uint32_t)); + //uint32_t* resultListPointerIterNumb = (uint32_t*)calloc(fpPlusFn + 50, sizeof(uint32_t)); + + cpuArgs.resultListPointerMeta = (uint32_t*)calloc(fpPlusFn + 50, sizeof(uint32_t));; + cpuArgs.resultListPointerLocalCPU = (uint32_t*)calloc(fpPlusFn + 50, sizeof(uint32_t));; + cpuArgs.resultListPointerIterNumb = (uint32_t*)calloc(fpPlusFn + 50, sizeof(uint32_t));; + + cudaMemcpyAsync(cpuArgs.resultListPointerMeta, gpuArgs.resultListPointerMeta, size, cudaMemcpyDeviceToHost, stream); + cudaMemcpyAsync(cpuArgs.resultListPointerLocalCPU, gpuArgs.resultListPointerLocal, size, cudaMemcpyDeviceToHost, stream); + cudaMemcpyAsync(cpuArgs.resultListPointerIterNumb, gpuArgs.resultListPointerIterNumb, size, cudaMemcpyDeviceToHost, stream); + + +}; + + + +/* +gettinng array for dilatations +basically arrays will alternate between iterations once one will be source other target then they will switch - we will decide upon knowing +wheather the iteration number is odd or even +*/ +#pragma once +template +inline __device__ uint32_t* getSourceReduced(const ForBoolKernelArgs& fbArgs, const int(&iterationNumb)[1]) { + + + if ((iterationNumb[0] & 1) == 0) { + return fbArgs.mainArrAPointer; + + } + else { + return fbArgs.mainArrBPointer; + } + + +} + + +/* +gettinng target array for dilatations +*/ +#pragma once +template +inline __device__ uint32_t* getTargetReduced(const ForBoolKernelArgs& fbArgs, const int(&iterationNumb)[1]) { + + if ((iterationNumb[0] & 1) == 0) { + //printf(" BB "); + + return fbArgs.mainArrBPointer; + + } + else { + // printf(" AA "); + + return fbArgs.mainArrAPointer; + + } + +} + + +/* +dilatation up and down - using bitwise operators +*/ +#pragma once +inline __device__ uint32_t bitDilatate(const uint32_t& x) { + return ((x) >> 1) | (x) | ((x) << 1); +} + +/* +return 1 if at given position of given number bit is set otherwise 0 +*/ +#pragma once +inline __device__ uint32_t isBitAt(const uint32_t& numb, const int pos) { + return (numb & (1 << (pos))); +} + +#pragma once +inline uint32_t isBitAtCPU(const uint32_t& numb, const int pos) { + return (numb & (1 << (pos))); +} + + + + + + + +/* +5)Main block + a) we define the work queue iteration - so we divide complete work queue into parts and each thread block analyzes its own part - one data block at a textLinesFromStrings + b) we load values of data block into shared memory and immidiately do the bit wise up and down dilatations, and mark booleans needed to establish is the datablock full + c) synthreads - left,right, anterior,posterior dilatations... + d) add the dilatated info into dilatation array and padding info from dilatation to global memory + e) if block is to be validated we check is there is in the point of currently coverd voxel some voxel in other mas if so we add it to the result list and increment local reult counter + f) syncgrid() +6)analyze padding + we iterate over work queue as in 5 + a) we load into shared memory information from padding from blocks all around the block of intrest checking for boundary conditions + b) we save data of dilatated voxels into dilatation array making sure to synchronize appropriately in the thread block + c) we analyze the positive entries given the block is to be validated so we check is such entry is already in dilatation mask if not is it in other mask if first no and second yes we add to the result + d) also given any positive entry we set block as to be activated simple sum reduction should be sufficient + e) sync grid +*/ +template +inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { + + + + thread_block cta = cooperative_groups::this_thread_block(); + + grid_group grid = cooperative_groups::this_grid(); + + /* + * according to https://forums.developer.nvidia.com/t/find-the-limit-of-shared-memory-that-can-be-used-per-block/48556 it is good to keep shared memory below 16kb kilo bytes + main shared memory spaces + 0-1023 : sourceShmem + 1024-2047 : resShmem + 2048-3071 : first register space + 3072-4095 : second register space + 4096- 4127: small 32 length resgister 3 space + 4128-4500 (372 length) : place for local work queue in dilatation kernels + */ + // __shared__ uint32_t mainShmem[lengthOfMainShmem]; + __shared__ uint32_t mainShmem[lengthOfMainShmem]; + //cuda::associate_access_property(&mainShmem, cuda::access_property::shared{}); + + + + constexpr size_t stages_count = 2; // Pipeline stages number + + // Allocate shared storage for a two-stage cuda::pipeline: + __shared__ cuda::pipeline_shared_state< + cuda::thread_scope::thread_scope_block, + stages_count + > shared_state; + + //cuda::pipeline pipeline = cuda::make_pipeline(cta, &shared_state); + cuda::pipeline pipeline = cuda::make_pipeline(cta, &shared_state); + + + + //usefull for iterating through local work queue + __shared__ bool isGoldForLocQueue[localWorkQueLength]; + // holding data about paddings + + + // holding data weather we have anything in padding 0)top 1)bottom, 2)left 3)right, 4)anterior, 5)posterior, + __shared__ bool isAnythingInPadding[6]; + + __shared__ bool isBlockFull[2]; + + __shared__ uint32_t lastI[1]; + + + //variables needed for all threads + __shared__ int iterationNumb[1]; + __shared__ unsigned int globalWorkQueueOffset[1]; + __shared__ unsigned int globalWorkQueueCounter[1]; + __shared__ unsigned int localWorkQueueCounter[1]; + // keeping data wheather gold or segmentation pass should continue - on the basis of global counters + + __shared__ unsigned int localTotalLenthOfWorkQueue[1]; + //counters for per block number of results added in this iteration + __shared__ unsigned int localFpConter[1]; + __shared__ unsigned int localFnConter[1]; + + __shared__ unsigned int blockFpConter[1]; + __shared__ unsigned int blockFnConter[1]; + + __shared__ unsigned int fpFnLocCounter[1]; + + //result list offset - needed to know where to write a result in a result list + __shared__ unsigned int resultfpOffset[1]; + __shared__ unsigned int resultfnOffset[1]; + + __shared__ unsigned int worQueueStep[1]; + + + /* will be used to store all of the minMaxes varibles from global memory (from 7 to 11) + 0 : global FP count; + 1 : global FN count; + 2 : workQueueCounter + 3 : resultFP globalCounter + 4 : resultFn globalCounter + */ + __shared__ unsigned int localMinMaxes[5]; + + /* will be used to store all of block metadata + nothing at 0 index + 1 :fpCount + 2 :fnCount + 3 :fpCounter + 4 :fnCounter + 5 :fpOffset + 6 :fnOffset + 7 :isActiveGold + 8 :isFullGold + 9 :isActiveSegm + 10 :isFullSegm + 11 :isToBeActivatedGold + 12 :isToBeActivatedSegm + 12 :isToBeActivatedSegm +//now linear indexes of the blocks in all sides - if there is no block in given direction it will equal UINT32_MAX + 13 : top + 14 : bottom + 15 : left + 16 : right + 17 : anterior + 18 : posterior + */ + + __shared__ uint32_t localBlockMetaData[40]; + + /* + //now linear indexes of the previous block in all sides - if there is no block in given direction it will equal UINT32_MAX + 0 : top + 1 : bottom + 2 : left + 3 : right + 4 : anterior + 5 : posterior + */ + + + /////used mainly in meta passes + +// __shared__ unsigned int fpFnLocCounter[1]; + __shared__ bool isGoldPassToContinue[1]; + __shared__ bool isSegmPassToContinue[1]; + + + + + + //initializations and loading + if (threadIdx.x == 9 && threadIdx.y == 0) { iterationNumb[0] = -1; }; + if (threadIdx.x == 11 && threadIdx.y == 0) { + isGoldPassToContinue[0] = true; + }; + if (threadIdx.x == 12 && threadIdx.y == 0) { + isSegmPassToContinue[0] = true; + + }; + + + //here we caclulate the offset for given block depending on length of the workqueue and number of the available blocks in a grid + // - this will give us number of work queue items per block - we will calculate offset on the basis of the block number + sync(cta); + + do { + + for (uint8_t isPaddingPass = 0; isPaddingPass < 2; isPaddingPass++) { + + + /////////////////////////**************************************************************************************************************** + /////////////////////////**************************************************************************************************************** + /////////////////////////**************************************************************************************************************** + /////////////////////////**************************************************************************************************************** + /////////////////////////**************************************************************************************************************** + /// dilataions + + //initial cleaning and initializations include loading min maxes + if (threadIdx.x == 7 && threadIdx.y == 0 && !isPaddingPass) { + iterationNumb[0] += 1; + }; + + if (threadIdx.x == 6 && threadIdx.y == 0) { + localWorkQueueCounter[0] = 0; + }; + + if (threadIdx.x == 1 && threadIdx.y == 0) { + blockFpConter[0] = 0; + }; + if (threadIdx.x == 2 && threadIdx.y == 0) { + blockFnConter[0] = 0; + }; + if (threadIdx.x == 3 && threadIdx.y == 0) { + localFpConter[0] = 0; + }; + if (threadIdx.x == 4 && threadIdx.y == 0) { + localFnConter[0] = 0; + }; + if (threadIdx.x == 9 && threadIdx.y == 0) { + isBlockFull[0] = true; + }; + if (threadIdx.x == 9 && threadIdx.y == 1) { + isBlockFull[1] = true; + }; + + if (threadIdx.x == 10 && threadIdx.y == 0) { + fpFnLocCounter[0] = 0; + }; + + + + if (threadIdx.x == 0 && threadIdx.y == 0) { + localTotalLenthOfWorkQueue[0] = fbArgs.minMaxes[9]; + globalWorkQueueOffset[0] = floor((float)(localTotalLenthOfWorkQueue[0] / gridDim.x)) + 1; + worQueueStep[0] = min(localWorkQueLength, globalWorkQueueOffset[0]); + }; + + if (threadIdx.y == 1) { + cooperative_groups::memcpy_async(cta, (&localMinMaxes[0]), (&fbArgs.minMaxes[7]), cuda::aligned_size_t<4>(sizeof(unsigned int) * 5)); + } + + sync(cta); + + /// load work QueueData into shared memory + for (uint32_t bigloop = blockIdx.x * globalWorkQueueOffset[0]; bigloop < ((blockIdx.x + 1) * globalWorkQueueOffset[0]); bigloop += worQueueStep[0]) { + + //grid stride loop - sadly most of threads will be idle + ///////// loading to work queue + if (((bigloop) < localTotalLenthOfWorkQueue[0]) && ((bigloop) < ((blockIdx.x + 1) * globalWorkQueueOffset[0]))) { + + for (uint16_t ii = cta.thread_rank(); ii < worQueueStep[0]; ii += cta.size()) { + + mainShmem[startOfLocalWorkQ + ii] = fbArgs.workQueuePointer[bigloop + ii]; + isGoldForLocQueue[ii] = (mainShmem[startOfLocalWorkQ + ii] >= isGoldOffset); + mainShmem[startOfLocalWorkQ + ii] = mainShmem[startOfLocalWorkQ + ii] - isGoldOffset * isGoldForLocQueue[ii]; + + + } + + } + //now all of the threads in the block needs to have the same i value so we will increment by 1 we are preloading to the pipeline block metaData + ////##### pipeline Step 0 + + sync(cta); + + + + + //loading metadata + pipeline.producer_acquire(); + if (((bigloop) < localTotalLenthOfWorkQueue[0]) && ((bigloop) < ((blockIdx.x + 1) * globalWorkQueueOffset[0]))) { + + cuda::memcpy_async(cta, (&localBlockMetaData[0]), + (&fbArgs.metaDataArrPointer[mainShmem[startOfLocalWorkQ] * fbArgs.metaData.metaDataSectionLength]) + , cuda::aligned_size_t<4>(sizeof(uint32_t) * 20), pipeline); + + } + pipeline.producer_commit(); + + + sync(cta); + + for (uint32_t i = 0; i < worQueueStep[0]; i += 1) { + + + + + if (((bigloop + i) < localTotalLenthOfWorkQueue[0]) && ((bigloop + i) < ((blockIdx.x + 1) * globalWorkQueueOffset[0]))) { + + + + pipeline.producer_acquire(); + cuda::memcpy_async(cta, &mainShmem[begSourceShmem], &getSourceReduced(fbArgs, iterationNumb)[ + mainShmem[startOfLocalWorkQ + i] * fbArgs.metaData.mainArrSectionLength + fbArgs.metaData.mainArrXLength * (1 - isGoldForLocQueue[i])], + cuda::aligned_size_t<128>(sizeof(uint32_t) * fbArgs.metaData.mainArrXLength), pipeline); + pipeline.producer_commit(); + + //just so pipeline will work well + pipeline.consumer_wait(); + + + + pipeline.consumer_release(); + sync(cta); + + ///////// step 1 load top and process main data + //load top + pipeline.producer_acquire(); + if (localBlockMetaData[(i & 1) * 20 + 13] < isGoldOffset) { + cuda::memcpy_async(cta, (&mainShmem[begfirstRegShmem]), + &getSourceReduced(fbArgs, iterationNumb)[localBlockMetaData[(i & 1) * 20 + 13] + * fbArgs.metaData.mainArrSectionLength + fbArgs.metaData.mainArrXLength * (1 - isGoldForLocQueue[i])], + cuda::aligned_size_t<128>(sizeof(uint32_t) * fbArgs.metaData.mainArrXLength) + , pipeline); + } + pipeline.producer_commit(); + //process main + pipeline.consumer_wait(); + //marking weather block is already full and no more dilatations are possible + if (__popc(mainShmem[begSourceShmem + threadIdx.x + threadIdx.y * 32]) < 32) { + isBlockFull[i & 1] = false; + } + mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] = bitDilatate(mainShmem[begSourceShmem + threadIdx.x + threadIdx.y * 32]); + pipeline.consumer_release(); + + ///////// step 2 load bottom and process top + //load bottom + pipeline.producer_acquire(); + if (localBlockMetaData[(i & 1) * 20 + 14] < isGoldOffset) { + cuda::memcpy_async(cta, (&mainShmem[begSecRegShmem]), + &getSourceReduced(fbArgs, iterationNumb)[localBlockMetaData[(i & 1) * 20 + 14] + * fbArgs.metaData.mainArrSectionLength + fbArgs.metaData.mainArrXLength * (1 - isGoldForLocQueue[i])], + cuda::aligned_size_t<128>(sizeof(uint32_t) * fbArgs.metaData.mainArrXLength) + , pipeline); + } + pipeline.producer_commit(); + //process top + pipeline.consumer_wait(); + + + if (localBlockMetaData[(i & 1) * 20 + 13] < isGoldOffset) { + if (isBitAt(mainShmem[begSourceShmem + threadIdx.x + threadIdx.y * 32], 0)) { + // printf("setting padding top val %d \n ", isAnythingInPadding[0]); + isAnythingInPadding[0] = true; + }; + // if in bit of intrest of neighbour block is set + mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] |= ((mainShmem[begfirstRegShmem + threadIdx.x + threadIdx.y * 32] >> 31) & 1) << 0; + } + + pipeline.consumer_release(); + sync(cta); + + /////////// step 3 load right process bottom + pipeline.producer_acquire(); + if (localBlockMetaData[(i & 1) * 20 + 16] < isGoldOffset) { + cuda::memcpy_async(cta, (&mainShmem[begfirstRegShmem]), + &getSourceReduced(fbArgs, iterationNumb)[localBlockMetaData[(i & 1) * 20 + 16] * fbArgs.metaData.mainArrSectionLength + fbArgs.metaData.mainArrXLength * (1 - isGoldForLocQueue[i])], + cuda::aligned_size_t<128>(sizeof(uint32_t) * fbArgs.metaData.mainArrXLength) + , pipeline); + } + pipeline.producer_commit(); + //process bototm + pipeline.consumer_wait(); + + + if (localBlockMetaData[(i & 1) * 20 + 14] < isGoldOffset) { + if (isBitAt(mainShmem[begSourceShmem + threadIdx.x + threadIdx.y * 32], 31)) { + isAnythingInPadding[1] = true; + }; + // if in bit of intrest of neighbour block is set + mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] |= ((mainShmem[begSecRegShmem + threadIdx.x + threadIdx.y * 32] >> 0) & 1) << 31; + } + + + + /* dilatateHelperTopDown(1, mainShmem, isAnythingInPadding, localBlockMetaData, 14 + , 0, 31 + , begSecRegShmem, i);*/ + + pipeline.consumer_release(); + /////////// step 4 load left process right + //load left + pipeline.producer_acquire(); + if (mainShmem[startOfLocalWorkQ + i] > 0) { + cuda::memcpy_async(cta, (&mainShmem[begSecRegShmem]), + &getSourceReduced(fbArgs, iterationNumb)[(mainShmem[startOfLocalWorkQ + i] - 1) * fbArgs.metaData.mainArrSectionLength + fbArgs.metaData.mainArrXLength * (1 - isGoldForLocQueue[i])], + cuda::aligned_size_t<128>(sizeof(uint32_t) * fbArgs.metaData.mainArrXLength) + , pipeline); + } + pipeline.producer_commit(); + //process right + pipeline.consumer_wait(); + + if (threadIdx.x == (fbArgs.dbXLength - 1)) { + // now we need to load the data from the neigbouring blocks + //first checking is there anything to look to + if (localBlockMetaData[(i & 1) * 20 + 16] < isGoldOffset) { + //now we load - we already done earlier up and down so now we are considering only anterior, posterior , left , right possibilities + if (mainShmem[begSourceShmem + threadIdx.x + threadIdx.y * 32] > 0) { + isAnythingInPadding[3] = true; + + }; + mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] = + mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] + | mainShmem[begfirstRegShmem + (threadIdx.y * 32)]; + + }; + } + else {//given we are not in corner case we need just to do the dilatation using biwise or with the data inside the block + mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] + = mainShmem[begSourceShmem + (threadIdx.x + 1) + (threadIdx.y) * 32] + | mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32]; + + } + + pipeline.consumer_release(); + sync(cta); + /////// step 5 load anterior process left + //load anterior + pipeline.producer_acquire(); + if (localBlockMetaData[(i & 1) * 20 + 17] < isGoldOffset) { + + cuda::memcpy_async(cta, (&mainShmem[begfirstRegShmem]), + &getSourceReduced(fbArgs, iterationNumb)[localBlockMetaData[(i & 1) * 20 + 17] * fbArgs.metaData.mainArrSectionLength + fbArgs.metaData.mainArrXLength * (1 - isGoldForLocQueue[i])], + cuda::aligned_size_t<128>(sizeof(uint32_t) * fbArgs.metaData.mainArrXLength) + , pipeline); + } + pipeline.producer_commit(); + //process left + pipeline.consumer_wait(); + + // so we first check for corner cases + if (threadIdx.x == 0) { + // now we need to load the data from the neigbouring blocks + //first checking is there anything to look to + if (localBlockMetaData[(i & 1) * 20 + 15] < isGoldOffset) { + //now we load - we already done earlier up and down so now we are considering only anterior, posterior , left , right possibilities + if (mainShmem[begSourceShmem + threadIdx.x + threadIdx.y * 32] > 0) { + isAnythingInPadding[2] = true; + + }; + mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] = + mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] + | mainShmem[begSecRegShmem + 31 + threadIdx.y * 32]; + + }; + } + else {//given we are not in corner case we need just to do the dilatation using biwise or with the data inside the block + mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] + = mainShmem[begSourceShmem + (threadIdx.x - 1) + (threadIdx.y) * 32] + | mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32]; + + } + + + pipeline.consumer_release(); + sync(cta); + + /////// step 6 load posterior process anterior + //load posterior + pipeline.producer_acquire(); + if (localBlockMetaData[(i & 1) * 20 + 18] < isGoldOffset) { + + + cuda::memcpy_async(cta, (&mainShmem[begSecRegShmem]), + &getSourceReduced(fbArgs, iterationNumb)[localBlockMetaData[(i & 1) * 20 + 18] * fbArgs.metaData.mainArrSectionLength + fbArgs.metaData.mainArrXLength * (1 - isGoldForLocQueue[i])], + cuda::aligned_size_t<128>(sizeof(uint32_t) * fbArgs.metaData.mainArrXLength) + , pipeline); + } + pipeline.producer_commit(); + + //process anterior + pipeline.consumer_wait(); + + // so we first check for corner cases + if (threadIdx.y == (fbArgs.dbYLength - 1)) { + // now we need to load the data from the neigbouring blocks + //first checking is there anything to look to + if (localBlockMetaData[(i & 1) * 20 + 17] < isGoldOffset) { + //now we load - we already done earlier up and down so now we are considering only anterior, posterior , left , right possibilities + if (mainShmem[begSourceShmem + threadIdx.x + threadIdx.y * 32] > 0) { + isAnythingInPadding[4] = true; + + }; + mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] = + mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] + | mainShmem[begfirstRegShmem + threadIdx.x]; + + }; + } + else {//given we are not in corner case we need just to do the dilatation using biwise or with the data inside the block + mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] + = mainShmem[begSourceShmem + (threadIdx.x) + (threadIdx.y + 1) * 32] + | mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32]; + + } + + + pipeline.consumer_release(); + sync(cta); + + /////// step 7 + //load reference if needed or data for next iteration if there is such + //process posterior, save data from res shmem to global memory also we mark weather block is full + pipeline.producer_acquire(); + + //if block should be validated we load data for validation + if (localBlockMetaData[(i & 1) * 20 + ((1 - isGoldForLocQueue[i]) + 1)] //fp for gold and fn count for not gold + > localBlockMetaData[(i & 1) * 20 + ((1 - isGoldForLocQueue[i]) + 3)]) {// so count is bigger than counter so we should validate + cuda::memcpy_async(cta, (&mainShmem[begfirstRegShmem]), + &fbArgs.origArrsPointer[mainShmem[startOfLocalWorkQ + i] * fbArgs.metaData.mainArrSectionLength + fbArgs.metaData.mainArrXLength * (isGoldForLocQueue[i])], //we look for + cuda::aligned_size_t<128>(sizeof(uint32_t) * fbArgs.metaData.mainArrXLength) + , pipeline); + + } + else {//if we are not validating we immidiately start loading data for next loop + if (i + 1 < worQueueStep[0]) { + cuda::memcpy_async(cta, (&localBlockMetaData[((i + 1) & 1) * 20]), + (&fbArgs.metaDataArrPointer[(mainShmem[startOfLocalWorkQ + 1 + i]) + * fbArgs.metaData.metaDataSectionLength]) + , cuda::aligned_size_t<4>(sizeof(uint32_t) * 20), pipeline); + + + } + } + + + pipeline.producer_commit(); + + //processPosteriorAndSaveResShmem + + pipeline.consumer_wait(); + //dilatate posterior + + + // so we first check for corner cases + if (threadIdx.y == 0) { + // now we need to load the data from the neigbouring blocks + //first checking is there anything to look to + if (localBlockMetaData[(i & 1) * 20 + 18] < isGoldOffset) { + //now we load - we already done earlier up and down so now we are considering only anterior, posterior , left , right possibilities + if (mainShmem[begSourceShmem + threadIdx.x + threadIdx.y * 32] > 0) { + isAnythingInPadding[5] = true; + + }; + mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] = + mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] + | mainShmem[begSecRegShmem + threadIdx.x + (fbArgs.dbYLength - 1) * 32]; + + }; + } + else {//given we are not in corner case we need just to do the dilatation using biwise or with the data inside the block + mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] + = mainShmem[begSourceShmem + (threadIdx.x) + (threadIdx.y - 1) * 32] + | mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32]; + + } + + //now all data should be properly dilatated we save it to global memory + //try save target reduced via mempcy async ... + + + //cuda::memcpy_async(cta, + // &getTargetReduced(fbArgs, iterationNumb)[mainShmem[startOfLocalWorkQ + i] * fbArgs.metaData.mainArrSectionLength + fbArgs.metaData.mainArrXLength * (1 - isGoldForLocQueue[i])] + // , (&mainShmem[begResShmem]), + // cuda::aligned_size_t<128>(sizeof(uint32_t) * fbArgs.metaData.mainArrXLength) + // , pipeline); + + + + getTargetReduced(fbArgs, iterationNumb)[mainShmem[startOfLocalWorkQ + i] * fbArgs.metaData.mainArrSectionLength + fbArgs.metaData.mainArrXLength * (1 - isGoldForLocQueue[i]) + + threadIdx.x + threadIdx.y * 32] + = mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32]; + + + + + + pipeline.consumer_release(); + + sync(cta); + + //////// step 8 basically in order to complete here anyting the count need to be bigger than counter + // loading for next block if block is not to be validated it was already done earlier + pipeline.producer_acquire(); + if (localBlockMetaData[(i & 1) * 20 + ((1 - isGoldForLocQueue[i]) + 1)] //fp for gold and fn count for not gold + > localBlockMetaData[(i & 1) * 20 + ((1 - isGoldForLocQueue[i]) + 3)]) {// so count is bigger than counter so we should validate + if (i + 1 < worQueueStep[0]) { + + + cuda::memcpy_async(cta, (&localBlockMetaData[((i + 1) & 1) * 20]), + (&fbArgs.metaDataArrPointer[(mainShmem[startOfLocalWorkQ + 1 + i]) + * fbArgs.metaData.metaDataSectionLength]) + , cuda::aligned_size_t<4>(sizeof(uint32_t) * 20), pipeline); + + } + } + pipeline.producer_commit(); + + + + + sync(cta); + + //validation - so looking for newly covered voxel for opposite array so new fps or new fns + pipeline.consumer_wait(); + + if (localBlockMetaData[(i & 1) * 20 + ((1 - isGoldForLocQueue[i]) + 1)] //fp for gold and fn count for not gold + > localBlockMetaData[(i & 1) * 20 + ((1 - isGoldForLocQueue[i]) + 3)]) {// so count is bigger than counter so we should validate + // now we look through bits and when some is set we call it a result +#pragma unroll + for (uint8_t bitPos = 0; bitPos < 32; bitPos++) { + //if any bit here is set it means it should be added to result list + if (isBitAt(mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32], bitPos) + && !isBitAt(mainShmem[begSourceShmem + threadIdx.x + threadIdx.y * 32], bitPos) + && isBitAt(mainShmem[begfirstRegShmem + threadIdx.x + threadIdx.y * 32], bitPos) + ) { + + //just re + mainShmem[begSecRegShmem + threadIdx.x + threadIdx.y * 32] = 0; + ////// IMPORTANT for some reason in order to make it work resultfnOffset and resultfnOffset swith places + if (isGoldForLocQueue[i]) { + mainShmem[begSecRegShmem + threadIdx.x + threadIdx.y * 32] = uint32_t(atomicAdd_block(&(localFpConter[0]), 1) + localBlockMetaData[(i & 1) * 20 + 6] + localBlockMetaData[(i & 1) * 20 + 3]); + + } + else { + + mainShmem[begSecRegShmem + threadIdx.x + threadIdx.y * 32] = uint32_t(atomicAdd_block(&(localFnConter[0]), 1) + localBlockMetaData[(i & 1) * 20 + 5] + localBlockMetaData[(i & 1) * 20 + 4]); + + + }; + // add results to global memory + //we add one gere jjust to distinguish it from empty result + fbArgs.resultListPointerMeta[mainShmem[begSecRegShmem + threadIdx.x + threadIdx.y * 32]] = uint32_t(mainShmem[startOfLocalWorkQ + i] + (isGoldOffset * isGoldForLocQueue[i]) + 1); + fbArgs.resultListPointerLocal[mainShmem[begSecRegShmem + threadIdx.x + threadIdx.y * 32]] = uint32_t((fbArgs.dbYLength * 32 * bitPos) + (threadIdx.y * 32) + (threadIdx.x)); + fbArgs.resultListPointerIterNumb[mainShmem[begSecRegShmem + threadIdx.x + threadIdx.y * 32]] = uint32_t(iterationNumb[0]); + + + + + } + + }; + + } + ///////// + pipeline.consumer_release(); + + /// /// cleaning + + sync(cta); + + if (threadIdx.x == 9 && threadIdx.y == 2) {// this is how it is encoded wheather it is gold or segm block + + //executed in case of previous block + if (isBlockFull[i & 1] && i >= 0) { + //setting data in metadata that block is full + fbArgs.metaDataArrPointer[mainShmem[startOfLocalWorkQ + i] * fbArgs.metaData.metaDataSectionLength + 10 - (isGoldForLocQueue[i] * 2)] = true; + } + //resetting for some reason block 0 gets as full even if it should not ... + isBlockFull[i & 1] = true;// mainShmem[startOfLocalWorkQ + i]>0;//!isPaddingPass; + }; + + + + + //we do it only for non padding pass + if (threadIdx.x < 6 && threadIdx.y == 1 && !isPaddingPass) { + //executed in case of previous block + if (i >= 0) { + + if (localBlockMetaData[(i & 1) * 20 + 13 + threadIdx.x] < isGoldOffset) { + + if (isAnythingInPadding[threadIdx.x]) { + // holding data weather we have anything in padding 0)top 1)bottom, 2)left 3)right, 4)anterior, 5)posterior, + fbArgs.metaDataArrPointer[localBlockMetaData[(i & 1) * 20 + 13 + threadIdx.x] * fbArgs.metaData.metaDataSectionLength + 12 - isGoldForLocQueue[i]] = 1; + } + + } + } + isAnythingInPadding[threadIdx.x] = false; + }; + + + + + + + if (threadIdx.x == 7 && threadIdx.y == 0) { + //this will be executed only if fp or fn counters are bigger than 0 so not during first pass + if (localFpConter[0] > 0) { + fbArgs.metaDataArrPointer[mainShmem[startOfLocalWorkQ + i] * fbArgs.metaData.metaDataSectionLength + 3] += localFpConter[0]; + + blockFpConter[0] += localFpConter[0]; + localFpConter[0] = 0; + } + + + }; + if (threadIdx.x == 8 && threadIdx.y == 0) { + + if (localFnConter[0] > 0) { + fbArgs.metaDataArrPointer[mainShmem[startOfLocalWorkQ + i] * fbArgs.metaData.metaDataSectionLength + 4] += localFnConter[0]; + + blockFnConter[0] += localFnConter[0]; + localFnConter[0] = 0; + } + }; + + sync(cta); + + } + } + + //here we are after all of the blocks planned to be processed by this block are + + // just for pipeline to work + pipeline.consumer_wait(); + + + + pipeline.consumer_release(); + + } + + sync(cta); + + // updating global counters + if (threadIdx.x == 0 && threadIdx.y == 0) { + if (blockFpConter[0] > 0) { + atomicAdd(&(fbArgs.minMaxes[10]), (blockFpConter[0])); + } + }; + if (threadIdx.x == 1 && threadIdx.y == 0) { + if (blockFnConter[0] > 0) { + //if (blockFnConter[0]>10) { + // printf("Fn %d ", blockFnConter[0]); + //} + atomicAdd(&(fbArgs.minMaxes[11]), (blockFnConter[0])); + } + }; + + // in first thread block we zero work queue counter + if (threadIdx.x == 2 && threadIdx.y == 0) { + if (blockIdx.x == 0) { + + fbArgs.minMaxes[9] = 0; + } + }; + + grid.sync(); + /////////////////////////**************************************************************************************************************** +/////////////////////////**************************************************************************************************************** +/////////////////////////**************************************************************************************************************** +/////////////////////////**************************************************************************************************************** +/////////////////////////**************************************************************************************************************** +/// metadata pass + + + + + + + + + + + // preparation loads + if (threadIdx.x == 0 && threadIdx.y == 0) { + fpFnLocCounter[0] = 0; + } + if (threadIdx.x == 1 && threadIdx.y == 0) { + localWorkQueueCounter[0] = 0; + } + if (threadIdx.x == 2 && threadIdx.y == 0) { + localWorkQueueCounter[0] = 0; + } + if (threadIdx.x == 3 && threadIdx.y == 0) { + localWorkQueueCounter[0] = 0; + + } + + if (threadIdx.x == 0 && threadIdx.y == 1) { + + isGoldPassToContinue[0] + = ((fbArgs.minMaxes[7] * fbArgs.robustnessPercent) > fbArgs.minMaxes[10]); + + }; + + if (threadIdx.x == 0 && threadIdx.y == 1) { + + isSegmPassToContinue[0] + = ((fbArgs.minMaxes[8] * fbArgs.robustnessPercent) > fbArgs.minMaxes[11]); + }; + + + __syncthreads(); + + ///////////////////////////////// + + for (uint32_t linIdexMeta = blockIdx.x * blockDim.x * blockDim.y + threadIdx.y * blockDim.x + threadIdx.x + ; linIdexMeta <= fbArgs.metaData.totalMetaLength + ; linIdexMeta += (blockDim.x * blockDim.y * gridDim.x) + ) { + + + if (isPaddingPass == 0) { + + //goldpass + if (isGoldPassToContinue[0] && fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 11] + && !fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 7] + && !fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 8]) { + + mainShmem[atomicAdd_block(&localWorkQueueCounter[0], 1)] = linIdexMeta + (isGoldOffset); + //setting to be activated to 0 + fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 11] = 0; + //setting active to 1 + fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 7] = 1; + + + }; + + } + //contrary to number it is when we are not in padding pass + else { + //gold pass + if (isGoldPassToContinue[0] && fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 7] + && !fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 8]) { + + mainShmem[atomicAdd_block(&localWorkQueueCounter[0], 1)] = linIdexMeta + (isGoldOffset); + + }; + + } + } + + __syncthreads(); + + if (localWorkQueueCounter[0] > 0) { + if (threadIdx.x == 0 && threadIdx.y == 0) { + globalWorkQueueCounter[0] = atomicAdd(&(fbArgs.minMaxes[9]), (localWorkQueueCounter[0])); + + + } + __syncthreads(); + for (uint32_t linI = threadIdx.y * blockDim.x + threadIdx.x; linI < localWorkQueueCounter[0]; linI += blockDim.x * blockDim.y) { + fbArgs.workQueuePointer[globalWorkQueueCounter[0] + linI] = mainShmem[linI]; + } + __syncthreads(); + + } + + __syncthreads(); + + if (threadIdx.x == 0 && threadIdx.y == 0) { + + localWorkQueueCounter[0] = 0; + } + __syncthreads(); + + for (uint32_t linIdexMeta = blockIdx.x * blockDim.x * blockDim.y + threadIdx.y * blockDim.x + threadIdx.x + ; linIdexMeta <= fbArgs.metaData.totalMetaLength + ; linIdexMeta += (blockDim.x * blockDim.y * gridDim.x) + ) { + + + if (isPaddingPass == 0) { + + //segm pass + if ((isSegmPassToContinue[0] && fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 12] + && !fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 9] + && !fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 10])) { + + + + mainShmem[atomicAdd_block(&localWorkQueueCounter[0], 1)] = linIdexMeta; + + //setting to be activated to 0 + fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 12] = 0; + //setting active to 1 + fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 9] = 1; + + } + + } + //contrary to number it is when we are not in padding pass + else { + //segm pass + if ((isSegmPassToContinue[0] && fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 9] + && !fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 10])) { + + + + mainShmem[atomicAdd_block(&localWorkQueueCounter[0], 1)] = linIdexMeta; + } + + } + } + __syncthreads(); + + if (localWorkQueueCounter[0] > 0) { + if (threadIdx.x == 0 && threadIdx.y == 0) { + globalWorkQueueCounter[0] = atomicAdd(&(fbArgs.minMaxes[9]), (localWorkQueueCounter[0])); + + + } + __syncthreads(); + for (uint32_t linI = threadIdx.y * blockDim.x + threadIdx.x; linI < localWorkQueueCounter[0]; linI += blockDim.x * blockDim.y) { + fbArgs.workQueuePointer[globalWorkQueueCounter[0] + linI] = mainShmem[linI]; + + } + + } + + + + grid.sync(); + } + + + + } while (isGoldPassToContinue[0] || isSegmPassToContinue[0]); + + + //setting global iteration number to local one + if (blockIdx.x == 0) { + if (threadIdx.x == 2 && threadIdx.y == 0) { + fbArgs.metaData.minMaxes[13] = iterationNumb[0]; + } + } +} + + + + + + + + +/* +get data from occupancy calculator API used to get optimal number of thread blocks and threads per thread block +*/ +template +inline occupancyCalcData getOccupancy() { + + occupancyCalcData res; + + int blockSize; // The launch configurator returned block size + int minGridSize; // The minimum grid size needed to achieve the maximum occupancy for a full device launch + int gridSize; // The actual grid size needed, based on input size + + // for min maxes kernel + cudaOccupancyMaxPotentialBlockSize( + &minGridSize, + &blockSize, + (void*)getMinMaxes, + 0); + res.warpsNumbForMinMax = blockSize / 32; + res.blockSizeForMinMax = minGridSize; + + // for min maxes kernel + cudaOccupancyMaxPotentialBlockSize( + &minGridSize, + &blockSize, + (void*)boolPrepareKernel, + 0); + res.warpsNumbForboolPrepareKernel = blockSize / 32; + res.blockSizeFoboolPrepareKernel = minGridSize; + // for first meta pass kernel + cudaOccupancyMaxPotentialBlockSize( + &minGridSize, + &blockSize, + (void*)boolPrepareKernel, + 0); + res.theadsForFirstMetaPass = blockSize; + res.blockForFirstMetaPass = minGridSize; + //for main pass kernel + cudaOccupancyMaxPotentialBlockSize( + &minGridSize, + &blockSize, + (void*)mainPassKernel, + 0); + res.warpsNumbForMainPass = blockSize / 32; + res.blockForMainPass = minGridSize; + + // res.blockForMainPass = 1; + //res.blockForMainPass = 136; + //res.warpsNumbForMainPass = 8; + + //printf("warpsNumbForMainPass %d blockForMainPass %d ", res.warpsNumbForMainPass, res.blockForMainPass); + return res; +} + + + +/* +TODO consider representing as a CUDA graph +executing Algorithm as CUDA graph based on official documentation and +https://codingbyexample.com/2020/09/25/cuda-graph-usage/ +*/ +#pragma once +template +ForBoolKernelArgs executeHausdoff(ForFullBoolPrepArgs& fFArgs, const int WIDTH, const int HEIGHT, const int DEPTH, occupancyCalcData& occData, + cudaStream_t stream, bool resToSave, float robustnessPercent, bool resIterNeeded) { + fFArgs.robustnessPercent = robustnessPercent; + T* goldArrPointer = (T*)fFArgs.goldArr.data_ptr(); + T* segmArrPointer = (T*)fFArgs.segmArr.data_ptr(); + + ForBoolKernelArgs fbArgs = getArgsForKernel(fFArgs, occData.warpsNumbForMainPass, occData.blockForMainPass, WIDTH, HEIGHT, DEPTH, stream); + + getMinMaxes << > > (fbArgs, fbArgs.minMaxes + , goldArrPointer + , segmArrPointer + , fbArgs.metaData); + + + + fbArgs.metaData = allocateMemoryAfterMinMaxesKernel(fbArgs, fFArgs, stream); + fbArgs.robustnessPercent = robustnessPercent; + boolPrepareKernel << > > ( + fbArgs, fbArgs.metaData, fbArgs.origArrsPointer, fbArgs.metaDataArrPointer + , goldArrPointer + , segmArrPointer + , fbArgs.minMaxes); + + + int fpPlusFn = allocateMemoryAfterBoolKernel(fbArgs, fFArgs, stream, resIterNeeded); + + + + firstMetaPrepareKernel << > > (fbArgs, fbArgs.metaData, fbArgs.minMaxes, fbArgs.workQueuePointer, fbArgs.origArrsPointer, fbArgs.metaDataArrPointer); + + + + void* kernel_args[] = { &fbArgs }; + cudaLaunchCooperativeKernel((void*)(mainPassKernel), occData.blockForMainPass, dim3(32, occData.warpsNumbForMainPass), kernel_args); + + + //copy to the output tensor the rsult + if (resIterNeeded) { + auto size = sizeof(int32_t) * fbArgs.resultListPointerIterNumbTensor.sizes()[0]; + cudaMemcpyAsync(fbArgs.resultListPointerIterNumbTensor.data_ptr(), fbArgs.resultListPointerIterNumb, size, cudaMemcpyDeviceToDevice, stream); + } + + + cudaFreeAsync(fbArgs.resultListPointerMeta, stream); + cudaFreeAsync(fbArgs.resultListPointerLocal, stream); + cudaFreeAsync(fbArgs.resultListPointerIterNumb, stream); + cudaFreeAsync(fbArgs.workQueuePointer, stream); + cudaFreeAsync(fbArgs.origArrsPointer, stream); + cudaFreeAsync(fbArgs.metaDataArrPointer, stream); + cudaFreeAsync(fbArgs.mainArrAPointer, stream); + cudaFreeAsync(fbArgs.mainArrBPointer, stream); + + + return fbArgs; + +} + + + +#pragma once +template +ForBoolKernelArgs mainKernelsRun(ForFullBoolPrepArgs& fFArgs, const int WIDTH, const int HEIGHT, const int DEPTH, cudaStream_t stream, bool resToSave = false,bool resIterNeeded=false +) { + + //cudaDeviceReset(); + cudaError_t syncErr; + cudaError_t asyncErr; + + occupancyCalcData occData = getOccupancy(); + + //pointers ... + ForBoolKernelArgs fbArgs = executeHausdoff(fFArgs, WIDTH, HEIGHT, DEPTH, occData, resToSave, stream,1.0, resIterNeeded); + + checkCuda(cudaDeviceSynchronize(), "last "); + + /////////// error handling + syncErr = cudaGetLastError(); + asyncErr = cudaDeviceSynchronize(); + if (syncErr != cudaSuccess) printf("Error in syncErr: %s\n", cudaGetErrorString(syncErr)); + if (asyncErr != cudaSuccess) printf("Error in asyncErr: %s\n", cudaGetErrorString(asyncErr)); + + + cudaDeviceReset(); + + return fbArgs; +} + + + + +// modified from https://github.com/pytorch/pytorch/blob/61d6c4386459441710fb4cfa2929a3f77e95e5f7/aten/src/ATen/Dispatch.h +#define AT_DISPATCH_ALL_TYPESWithBool(TYPE, NAME, ...) \ + [&] { \ + const auto& the_type = TYPE; \ + /* don't use TYPE again in case it is an expensive or side-effect op */ \ + at::ScalarType _st = ::detail::scalar_type(the_type); \ + RECORD_KERNEL_FUNCTION_DTYPE(NAME, _st); \ + switch (_st) { \ + AT_PRIVATE_CASE_TYPE(NAME, at::ScalarType::Byte, uint8_t, __VA_ARGS__) \ + AT_PRIVATE_CASE_TYPE(NAME, at::ScalarType::Char, int8_t, __VA_ARGS__) \ + AT_PRIVATE_CASE_TYPE(NAME, at::ScalarType::Double, double, __VA_ARGS__) \ + AT_PRIVATE_CASE_TYPE(NAME, at::ScalarType::Float, float, __VA_ARGS__) \ + AT_PRIVATE_CASE_TYPE(NAME, at::ScalarType::Int, int32_t, __VA_ARGS__) \ + AT_PRIVATE_CASE_TYPE(NAME, at::ScalarType::Long, int64_t, __VA_ARGS__) \ + AT_PRIVATE_CASE_TYPE(NAME, at::ScalarType::Short, int16_t, __VA_ARGS__) \ + AT_PRIVATE_CASE_TYPE(NAME, at::ScalarType::Bool, bool, __VA_ARGS__) \ + default: \ + AT_ERROR(#NAME, " not implemented for '", toString(_st), "'"); \ + } \ + }() + + + + +void lltm_cuda_forward( + torch::Tensor input, + torch::Tensor output, int xDim, int yDim, int zDim) { + + + + //benchmarkOliviera(input, output, xDim, yDim, zDim); + benchmarkMitura(input, output, xDim, yDim, zDim); + + +}; + + + + + +template +int getHausdorffDistance_CUDA_Generic(at::Tensor goldStandard, + at::Tensor algoOutput + , int WIDTH, int HEIGHT, int DEPTH, float robustnessPercent, bool resIterNeeded= false) { + //TODO() use https ://pytorch.org/cppdocs/notes/tensor_cuda_stream.html + cudaStream_t stream1; + cudaStreamCreate(&stream1); + + MetaDataCPU metaData; + size_t size = sizeof(unsigned int) * 20; + unsigned int* minMaxesCPU = (unsigned int*)malloc(size); + metaData.minMaxes = minMaxesCPU; + + ForFullBoolPrepArgs forFullBoolPrepArgs; + forFullBoolPrepArgs.metaData = metaData; + forFullBoolPrepArgs.numberToLookFor = true; + forFullBoolPrepArgs.goldArr = goldStandard; + forFullBoolPrepArgs.segmArr = algoOutput; + + occupancyCalcData occData = getOccupancy(); + + ForBoolKernelArgs fbArgs = executeHausdoff(forFullBoolPrepArgs, WIDTH, HEIGHT, DEPTH, occData, stream1, false, robustnessPercent, resIterNeeded); + + size_t sizeMinMax = sizeof(unsigned int) * 20; + //making sure we have all resultsto copy on cpu + cudaDeviceSynchronize(); + cudaMemcpy(minMaxesCPU, fbArgs.metaData.minMaxes, sizeMinMax, cudaMemcpyDeviceToHost); + + int result = minMaxesCPU[13]; + + cudaFreeAsync(fbArgs.minMaxes, stream1); + free(minMaxesCPU); + + + cudaStreamDestroy(stream1); + cudaDeviceSynchronize(); + + return result; +} + + + +template +at::Tensor getHausdorffDistance_CUDA_FullResList_local(at::Tensor goldStandard, + at::Tensor algoOutput + , int WIDTH, int HEIGHT, int DEPTH, float robustnessPercent) { + //TODO() use https ://pytorch.org/cppdocs/notes/tensor_cuda_stream.html + cudaStream_t stream1; + cudaStreamCreate(&stream1); + + MetaDataCPU metaData; + size_t size = sizeof(unsigned int) * 20; + unsigned int* minMaxesCPU = (unsigned int*)malloc(size); + metaData.minMaxes = minMaxesCPU; + + ForFullBoolPrepArgs forFullBoolPrepArgs; + forFullBoolPrepArgs.metaData = metaData; + forFullBoolPrepArgs.numberToLookFor = true; + forFullBoolPrepArgs.goldArr = goldStandard; + forFullBoolPrepArgs.segmArr = algoOutput; + + occupancyCalcData occData = getOccupancy(); + + ForBoolKernelArgs fbArgs = executeHausdoff(forFullBoolPrepArgs, WIDTH, HEIGHT, DEPTH, occData, stream1, false, robustnessPercent, true); + + + + + cudaFreeAsync(fbArgs.minMaxes, stream1); + free(minMaxesCPU); + + + + cudaStreamDestroy(stream1); + + return fbArgs.resultListPointerIterNumbTensor; +} + + + +int getHausdorffDistance_CUDA(at::Tensor goldStandard, + at::Tensor algoOutput + , const int WIDTH,const int HEIGHT,const int DEPTH + ,const float robustnessPercent) { + + int res = 0; + + AT_DISPATCH_ALL_TYPESWithBool(goldStandard.type(), "getHausdorffDistance_CUDA", ([&] { + res= getHausdorffDistance_CUDA_Generic(goldStandard, algoOutput, WIDTH, HEIGHT, DEPTH, robustnessPercent); + + })); + return res; +} + + + +at::Tensor getHausdorffDistance_CUDA_FullResList(at::Tensor goldStandard, + at::Tensor algoOutput + , const int WIDTH, const int HEIGHT, const int DEPTH + , const float robustnessPercent) { + + + at::Tensor res; + AT_DISPATCH_ALL_TYPESWithBool(goldStandard.type(), "getHausdorffDistance_CUDA", ([&] { + res= getHausdorffDistance_CUDA_FullResList_local(goldStandard, algoOutput, WIDTH, HEIGHT, DEPTH, robustnessPercent); + + })); + + return res; +} \ No newline at end of file diff --git a/monai/metrics/morphological_hausdorff_distance.py b/monai/metrics/morphological_hausdorff_distance.py new file mode 100644 index 00000000000..176c5867684 --- /dev/null +++ b/monai/metrics/morphological_hausdorff_distance.py @@ -0,0 +1,81 @@ +# Copyright (c) MONAI Consortium +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import warnings +from typing import Optional, Union + +import numpy as np +import torch + +from .metric import CumulativeIterationMetric + +__all__ = ["MorphologicalHausdorffDistanceMetric", "compute_morphological_hausdorff_distance", "compute_morphological_percent_hausdorff_distance"] + + +class MorphologicalHausdorffDistanceMetric(CumulativeIterationMetric): + """ + Work is based onthe principle of application of mathemathical morphology more precisely dilatation, more details is presented in works [1] and [2]. Calculated is roughly related to the largest +thickness of the difference between the true and estimatedmasks, and constitutes Approximation of true Hausdorff distance. The correlation between true and morphological Hausdorf distance is around 0.9, less for robust version . +Hovewer this implementation is also around 100 times faster than exact MONAI Hausdorff distance calculation. One can measure 3 diffrent metrics based on Hausdorff ditance. + + 1)simple Hausdorff distance - it tell what is the distance between two most distant points one from y and other from y_pred + + 2)robust Hausdorff distance - modification of 1) where we stop analyzing points when we already analyzed given percent of total number of points - is less sensitive to outliers. It is also slightly faster + + 3)mean Hausdorff distance - can be understood as approximately mean distance between pair of points where one point is from y and other from y_pred. + + 4)additionally one can request as a result one dimensional tensor that will contain data for each point what is HD value for it (like we would do Hausdorff distance calculation only for this point and all points from other mask) + + Compute Hausdorff Distance between two tensors. In addition, specify the `percent` (for robust Hausdorff distance ) + parameter can get the percentile of the distance. Input `y_pred` is compared with ground truth `y`. + + Args: + percent: an optional float number between 0 and 1. If specified, the corresponding + percent of the Hausdorff Distance rather than the maximum result will be achieved. + Defaults to 1.0 . + + 1) Érick Oliveira Rodrigues,An efficient and locality-oriented Hausdorff distance algorithm: Proposal and analysis of paradigms and implementations, Pattern Recognition,Volume 117,2021 + 2) Karimi, Davood & Salcudean, Septimiu. (2019). Reducing the Hausdorff Distance in Medical Image Segmentation With Convolutional Neural Networks. IEEE Transactions on Medical Imaging. PP. 1-1. 10.1109/TMI.2019.2930068. + + """ + + def __init__( + self, + percent: float = 1.0, + ) -> None: + super().__init__() + self.percent = percent + + +def compute_hausdorff_distance( + y_pred: torch.Tensor, + y: torch.Tensor, + percent: float = 1.0): + """ + Compute the Hausdorff distance. + + Args: + y_pred: input data to compute, typical segmentation model output. + It must be one-hot format and first dim is batch, example shape: [16, 3, 32, 32]. The values + should be binarized. + y: ground truth to compute mean the distance. It must be one-hot format and first dim is batch. + The values should be binarized. + percent: an optional float number between 0 and 1. If specified, the corresponding + percent of the Hausdorff Distance rather than the maximum result will be achieved. + Defaults to 1.0 . + """ + + if y.shape != y_pred.shape: + raise ValueError(f"y_pred and y should have same shapes, got {y_pred.shape} and {y.shape}.") + + return xxx + + From bd17a2d03616fe6866848f2afb22f9e90aa49441 Mon Sep 17 00:00:00 2001 From: Jakub Mitura <53857487+jakubMitura14@users.noreply.github.com> Date: Tue, 5 Apr 2022 19:27:44 +0200 Subject: [PATCH 02/41] added first simple test --- .../{hausdorff.cpp => hausdorff_cpp.cpp} | 0 monai/_extensions/hausdorff/hausdorff_cuda.cu | 36 ++++------------ .../morphological_hausdorff_distance.py | 22 ++++++---- tests/test_hausdorff_distance_morphologic.py | 41 +++++++++++++++++++ 4 files changed, 64 insertions(+), 35 deletions(-) rename monai/_extensions/hausdorff/{hausdorff.cpp => hausdorff_cpp.cpp} (100%) create mode 100644 tests/test_hausdorff_distance_morphologic.py diff --git a/monai/_extensions/hausdorff/hausdorff.cpp b/monai/_extensions/hausdorff/hausdorff_cpp.cpp similarity index 100% rename from monai/_extensions/hausdorff/hausdorff.cpp rename to monai/_extensions/hausdorff/hausdorff_cpp.cpp diff --git a/monai/_extensions/hausdorff/hausdorff_cuda.cu b/monai/_extensions/hausdorff/hausdorff_cuda.cu index 3c72e1cd496..3a6748ab0f0 100644 --- a/monai/_extensions/hausdorff/hausdorff_cuda.cu +++ b/monai/_extensions/hausdorff/hausdorff_cuda.cu @@ -2500,26 +2500,11 @@ ForBoolKernelArgs mainKernelsRun(ForFullBoolPrepArgs& fFArgs, const int WI -void lltm_cuda_forward( - torch::Tensor input, - torch::Tensor output, int xDim, int yDim, int zDim) { - - - - //benchmarkOliviera(input, output, xDim, yDim, zDim); - benchmarkMitura(input, output, xDim, yDim, zDim); - - -}; - - - - template int getHausdorffDistance_CUDA_Generic(at::Tensor goldStandard, at::Tensor algoOutput - , int WIDTH, int HEIGHT, int DEPTH, float robustnessPercent, bool resIterNeeded= false) { + , int WIDTH, int HEIGHT, int DEPTH, float robustnessPercent, bool resIterNeeded, at::Tensor numberToLookFor) { //TODO() use https ://pytorch.org/cppdocs/notes/tensor_cuda_stream.html cudaStream_t stream1; cudaStreamCreate(&stream1); @@ -2531,7 +2516,7 @@ int getHausdorffDistance_CUDA_Generic(at::Tensor goldStandard, ForFullBoolPrepArgs forFullBoolPrepArgs; forFullBoolPrepArgs.metaData = metaData; - forFullBoolPrepArgs.numberToLookFor = true; + forFullBoolPrepArgs.numberToLookFor = numberToLookFor.item(); forFullBoolPrepArgs.goldArr = goldStandard; forFullBoolPrepArgs.segmArr = algoOutput; @@ -2557,11 +2542,10 @@ int getHausdorffDistance_CUDA_Generic(at::Tensor goldStandard, } - template at::Tensor getHausdorffDistance_CUDA_FullResList_local(at::Tensor goldStandard, at::Tensor algoOutput - , int WIDTH, int HEIGHT, int DEPTH, float robustnessPercent) { + , int WIDTH, int HEIGHT, int DEPTH, float robustnessPercent, at::Tensor numberToLookFor) { //TODO() use https ://pytorch.org/cppdocs/notes/tensor_cuda_stream.html cudaStream_t stream1; cudaStreamCreate(&stream1); @@ -2573,7 +2557,7 @@ at::Tensor getHausdorffDistance_CUDA_FullResList_local(at::Tensor goldStandard, ForFullBoolPrepArgs forFullBoolPrepArgs; forFullBoolPrepArgs.metaData = metaData; - forFullBoolPrepArgs.numberToLookFor = true; + forFullBoolPrepArgs.numberToLookFor = numberToLookFor.item(); forFullBoolPrepArgs.goldArr = goldStandard; forFullBoolPrepArgs.segmArr = algoOutput; @@ -2595,32 +2579,30 @@ at::Tensor getHausdorffDistance_CUDA_FullResList_local(at::Tensor goldStandard, } - int getHausdorffDistance_CUDA(at::Tensor goldStandard, at::Tensor algoOutput , const int WIDTH,const int HEIGHT,const int DEPTH - ,const float robustnessPercent) { + ,const float robustnessPercent, at::Tensor numberToLookFor) { int res = 0; AT_DISPATCH_ALL_TYPESWithBool(goldStandard.type(), "getHausdorffDistance_CUDA", ([&] { - res= getHausdorffDistance_CUDA_Generic(goldStandard, algoOutput, WIDTH, HEIGHT, DEPTH, robustnessPercent); + res= getHausdorffDistance_CUDA_Generic(goldStandard, algoOutput, WIDTH, HEIGHT, DEPTH, robustnessPercent,false, numberToLookFor); })); return res; } - at::Tensor getHausdorffDistance_CUDA_FullResList(at::Tensor goldStandard, at::Tensor algoOutput , const int WIDTH, const int HEIGHT, const int DEPTH - , const float robustnessPercent) { + , const float robustnessPercent, at::Tensor numberToLookFor) { at::Tensor res; - AT_DISPATCH_ALL_TYPESWithBool(goldStandard.type(), "getHausdorffDistance_CUDA", ([&] { - res= getHausdorffDistance_CUDA_FullResList_local(goldStandard, algoOutput, WIDTH, HEIGHT, DEPTH, robustnessPercent); + AT_DISPATCH_ALL_TYPESWithBool(goldStandard.type(), "getHausdorffDistance_CUDA_FullResList", ([&] { + res= getHausdorffDistance_CUDA_FullResList_local(goldStandard, algoOutput, WIDTH, HEIGHT, DEPTH, robustnessPercent, numberToLookFor); })); diff --git a/monai/metrics/morphological_hausdorff_distance.py b/monai/metrics/morphological_hausdorff_distance.py index 176c5867684..780371938c5 100644 --- a/monai/metrics/morphological_hausdorff_distance.py +++ b/monai/metrics/morphological_hausdorff_distance.py @@ -14,7 +14,7 @@ import numpy as np import torch - +from monai._extensions.loader import load_module from .metric import CumulativeIterationMetric __all__ = ["MorphologicalHausdorffDistanceMetric", "compute_morphological_hausdorff_distance", "compute_morphological_percent_hausdorff_distance"] @@ -53,29 +53,35 @@ def __init__( ) -> None: super().__init__() self.percent = percent + self.compiled_extension = load_module( + "hausdorff_cpp" + ) def compute_hausdorff_distance( + self, y_pred: torch.Tensor, y: torch.Tensor, - percent: float = 1.0): + numberToLookFor: torch.Tensor): """ Compute the Hausdorff distance. Args: - y_pred: input data to compute, typical segmentation model output. - It must be one-hot format and first dim is batch, example shape: [16, 3, 32, 32]. The values - should be binarized. - y: ground truth to compute mean the distance. It must be one-hot format and first dim is batch. - The values should be binarized. + y_pred: input data to compute, It must be 3 dimensional + y: ground truth to compute mean the distance. It must be 3 dimensional, Dimensionality needs to be identical as in y_pred + percent: an optional float number between 0 and 1. If specified, the corresponding percent of the Hausdorff Distance rather than the maximum result will be achieved. Defaults to 1.0 . + numberToLookFor: 0 dimensional tensor marking what value are we intrested in the supplied y and y_pred, defined becouse in case of multiorgan segmentations frequently we can have information about multiple organs in the same mask just marked by diffrent numbers + for example in case of boolean array we can suupply it like torch.ones(1, dtype =bool) + """ if y.shape != y_pred.shape: raise ValueError(f"y_pred and y should have same shapes, got {y_pred.shape} and {y.shape}.") + sizz = y.shape - return xxx + return self.compiled_extension.getHausdorffDistance(y_pred,y,sizz[0],sizz[1],sizz[2], self.percent, numberToLookFor ) diff --git a/tests/test_hausdorff_distance_morphologic.py b/tests/test_hausdorff_distance_morphologic.py new file mode 100644 index 00000000000..03caa050a27 --- /dev/null +++ b/tests/test_hausdorff_distance_morphologic.py @@ -0,0 +1,41 @@ +# Copyright (c) MONAI Consortium +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import unittest + +import torch +from parameterized import parameterized +import numpy as np + +from monai.metrics import MorphologicalHausdorffDistanceMetric + +a = torch.zeros(20,20,20) +b = torch.zeros(20,20,20) +a[0,0,0]=1 +b[0,0,10]=1 +numbToLookFor= torch.ones(1) + +TEST_CASES = [ + [[a,b,1.0,numbToLookFor], 10] + +] +class TestHausdorffDistanceMorphological(unittest.TestCase): + @parameterized.expand(TEST_CASES) + def test_value(self, input_data, expected_value): + [y_pred, y, percentt, numberToLookFor] = input_data + hd_metric = MorphologicalHausdorffDistanceMetric(percent=percentt) + result= hd_metric.compute_hausdorff_distance(y_pred,y,numberToLookFor ) + np.testing.assert_allclose(expected_value, result, rtol=1e-7) + + + +if __name__ == "__main__": + unittest.main() From cbfa8070e430687fdb12f5fdbbec9ca602a44675 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 5 Apr 2022 17:29:36 +0000 Subject: [PATCH 03/41] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- monai/_extensions/hausdorff/hausdorff_cuda.cu | 140 +++++++++--------- .../morphological_hausdorff_distance.py | 20 ++- tests/test_hausdorff_distance_morphologic.py | 2 +- 3 files changed, 80 insertions(+), 82 deletions(-) diff --git a/monai/_extensions/hausdorff/hausdorff_cuda.cu b/monai/_extensions/hausdorff/hausdorff_cuda.cu index 3a6748ab0f0..18cfb63e0b3 100644 --- a/monai/_extensions/hausdorff/hausdorff_cuda.cu +++ b/monai/_extensions/hausdorff/hausdorff_cuda.cu @@ -38,7 +38,7 @@ constexpr uint32_t begSMallRegShmemB = 1056; constexpr uint32_t begSourceShmem = 32; //as the pipeline is so asynchronous we need some additional storage for saving data related to the operations -//will define how big is the amount of space in order to be able to use fast version of modulo operator it should be power of 2 ... +//will define how big is the amount of space in order to be able to use fast version of modulo operator it should be power of 2 ... constexpr uint32_t modForPipelineVars = 16; constexpr uint32_t fpLocCountBeg = 4460; @@ -46,7 +46,7 @@ constexpr uint32_t fpLocCountBeg = 4460; // other -//added to linear index meta in order to mark weather block is of type gold or not +//added to linear index meta in order to mark weather block is of type gold or not constexpr uint32_t isGoldOffset = (UINT16_MAX * 10); @@ -65,7 +65,7 @@ extern "C" struct MetaDataCPU { //1)maxX 2)minX 3)maxY 4) minY 5) maxZ 6) minZ - minimal and maximum coordinates of blocks with some entries of intrest - //7)global FP count; 8)global FN count 9) workQueueCounter 10)resultFP globalCounter 11) resultFn globalCounter + //7)global FP count; 8)global FN count 9) workQueueCounter 10)resultFP globalCounter 11) resultFn globalCounter //12) global FPandFn offset 13)globalIterationNumb //array3dWithDimsCPU minMaxes; unsigned int* minMaxes; @@ -74,10 +74,10 @@ extern "C" struct MetaDataCPU { ///// sizes of array below will be established on the basis of fp and fn values known after boolKernel finished execution - //work queue - workqueue counter already present in minMaxes as entry 9 + //work queue - workqueue counter already present in minMaxes as entry 9 //in practice it is matrix of length the same as FP+FN global count +1 and width of 5 - //1) xMeta; 2)yMeta 3)zMeta 4)isGold 5)iteration number - //we use one single long rewsult list - in order to avoid overwriting each block each block has established offset where it would write it's results + //1) xMeta; 2)yMeta 3)zMeta 4)isGold 5)iteration number + //we use one single long rewsult list - in order to avoid overwriting each block each block has established offset where it would write it's results uint32_t* resultList; }; @@ -102,8 +102,8 @@ extern "C" struct MetaDataGPU { unsigned int metaDataSectionLength = 20; - // now we will store here also calculated by min maxes kernel values of minimum and maximumvalues - //1)maxX 2)minX 3)maxY 4) minY 5) maxZ 6) minZ + // now we will store here also calculated by min maxes kernel values of minimum and maximumvalues + //1)maxX 2)minX 3)maxY 4) minY 5) maxZ 6) minZ unsigned int maxX; unsigned int minX; unsigned int maxY; @@ -148,13 +148,13 @@ struct ForFullBoolPrepArgs { //threads and blocks for first metadata pass kernel int threadsFirstMetaDataPass; int blocksFirstMetaDataPass; - //threads and blocks for main pass + //threads and blocks for main pass dim3 threadsMainPass; int blocksMainPass; - //threads and blocks for padding pass + //threads and blocks for padding pass dim3 threadsPaddingPass; int blocksPaddingPass; - //threads and blocks for non first metadata passes + //threads and blocks for non first metadata passes int threadsOtherMetaDataPasses; int blocksOtherMetaDataPasses; // will establish how many points we want to include in dilatation and how many we can ignore so typically set to 95% - so we will ignore only 5% most distant @@ -523,7 +523,7 @@ __global__ void getMinMaxes(ForBoolKernelArgs fbArgs //} if (y < fbArgs.Ny && x < fbArgs.Nx) { - // resetting + // resetting for (uint8_t zLoc = 0; zLoc < fbArgs.dbZLength; zLoc++) { @@ -672,7 +672,7 @@ __global__ void boolPrepareKernel(ForBoolKernelArgs fbArgs //shared memory - //TODO() make it dynamically sized + //TODO() make it dynamically sized __shared__ uint32_t sharedForGold[1024]; __shared__ uint32_t sharedForSegm[1024]; @@ -725,7 +725,7 @@ __global__ void boolPrepareKernel(ForBoolKernelArgs fbArgs uint32_t y = (yMeta + metaData.minY) * fbArgs.dbYLength + yLoc;//absolute position if (y < fbArgs.Ny && x < fbArgs.Nx) { - // resetting + // resetting sharedForGold[xLoc + yLoc * fbArgs.dbXLength] = 0; sharedForSegm[xLoc + yLoc * fbArgs.dbXLength] = 0; @@ -829,7 +829,7 @@ __global__ void boolPrepareKernel(ForBoolKernelArgs fbArgs - /////adding the block and total number of the Fp's and Fn's + /////adding the block and total number of the Fp's and Fn's sumFp = reduce(tile, sumFp, plus()); sumFn = reduce(tile, sumFn, plus()); //reusing shared memory and adding accumulated values from tiles @@ -873,7 +873,7 @@ __global__ void boolPrepareKernel(ForBoolKernelArgs fbArgs } - //marking as active + //marking as active //FP pass if ((threadIdx.x == 0) && (threadIdx.y == 0) && isNotEmpty && anyInGold[0]) { localBlockMetaData[7] = 1; @@ -886,7 +886,7 @@ __global__ void boolPrepareKernel(ForBoolKernelArgs fbArgs }; - //after we streamed over all block we save also information about indicies of the surrounding blocks - given they are in range if not UINT32_MAX will be saved + //after we streamed over all block we save also information about indicies of the surrounding blocks - given they are in range if not UINT32_MAX will be saved //top @@ -968,7 +968,7 @@ __device__ inline void addToQueue(uint32_t linIdexMeta, uint8_t isGold old = atomicAdd(&(minMaxes[9]), old); //workQueue workQueue[old] = linIdexMeta + (isGoldOffset * isGold); - //and offset + //and offset metaDataArr[linIdexMeta * metaData.metaDataSectionLength + offsetIndexNumb] = atomicAdd(&(minMaxes[12]), count); }; } @@ -1023,7 +1023,7 @@ __global__ void firstMetaPrepareKernel(ForBoolKernelArgs fbArgs , fpFnLocCounter, localWorkQueue, localOffsetQueue, localWorkQueueCounter , 1, 9, 6 , metaDataArr, metaData, minMaxes, workQueue); - //segmPass + //segmPass addToQueue(linIdexMeta, 1 , fpFnLocCounter, localWorkQueue, localOffsetQueue, localWorkQueueCounter , 2, 7, 5 @@ -1066,7 +1066,7 @@ __global__ void firstMetaPrepareKernel(ForBoolKernelArgs fbArgs metaDataArr[(localWorkQueue[i] - isGoldOffset) * metaData.metaDataSectionLength + 5] = localOffsetQueue[i] + globalOffsetForBlock[0]; //printf("fp offset lin meta %d total offset %d global part %d local part %d \n " // , localWorkQueue[i] - isGoldOffset - // , localOffsetQueue[i] + globalOffsetForBlock[0] + // , localOffsetQueue[i] + globalOffsetForBlock[0] //, globalOffsetForBlock[0] //, localOffsetQueue[i]); @@ -1075,7 +1075,7 @@ __global__ void firstMetaPrepareKernel(ForBoolKernelArgs fbArgs else { metaDataArr[(localWorkQueue[i]) * metaData.metaDataSectionLength + 6] = localOffsetQueue[i] + globalOffsetForBlock[0]; //printf("fn offset lin meta %d total offset %d global part %d local part %d \n " - // , localWorkQueue[i] + // , localWorkQueue[i] // , localOffsetQueue[i] + globalOffsetForBlock[0] // , globalOffsetForBlock[0] // , localOffsetQueue[i]); @@ -1463,7 +1463,7 @@ inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { //usefull for iterating through local work queue __shared__ bool isGoldForLocQueue[localWorkQueLength]; - // holding data about paddings + // holding data about paddings // holding data weather we have anything in padding 0)top 1)bottom, 2)left 3)right, 4)anterior, 5)posterior, @@ -1554,7 +1554,7 @@ inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { - //initializations and loading + //initializations and loading if (threadIdx.x == 9 && threadIdx.y == 0) { iterationNumb[0] = -1; }; if (threadIdx.x == 11 && threadIdx.y == 0) { isGoldPassToContinue[0] = true; @@ -1574,11 +1574,11 @@ inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { for (uint8_t isPaddingPass = 0; isPaddingPass < 2; isPaddingPass++) { - /////////////////////////**************************************************************************************************************** - /////////////////////////**************************************************************************************************************** - /////////////////////////**************************************************************************************************************** - /////////////////////////**************************************************************************************************************** - /////////////////////////**************************************************************************************************************** + /////////////////////////**************************************************************************************************************** + /////////////////////////**************************************************************************************************************** + /////////////////////////**************************************************************************************************************** + /////////////////////////**************************************************************************************************************** + /////////////////////////**************************************************************************************************************** /// dilataions //initial cleaning and initializations include loading min maxes @@ -1627,10 +1627,10 @@ inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { sync(cta); - /// load work QueueData into shared memory + /// load work QueueData into shared memory for (uint32_t bigloop = blockIdx.x * globalWorkQueueOffset[0]; bigloop < ((blockIdx.x + 1) * globalWorkQueueOffset[0]); bigloop += worQueueStep[0]) { - //grid stride loop - sadly most of threads will be idle + //grid stride loop - sadly most of threads will be idle ///////// loading to work queue if (((bigloop) < localTotalLenthOfWorkQueue[0]) && ((bigloop) < ((blockIdx.x + 1) * globalWorkQueueOffset[0]))) { @@ -1689,8 +1689,8 @@ inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { pipeline.consumer_release(); sync(cta); - ///////// step 1 load top and process main data - //load top + ///////// step 1 load top and process main data + //load top pipeline.producer_acquire(); if (localBlockMetaData[(i & 1) * 20 + 13] < isGoldOffset) { cuda::memcpy_async(cta, (&mainShmem[begfirstRegShmem]), @@ -1702,14 +1702,14 @@ inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { pipeline.producer_commit(); //process main pipeline.consumer_wait(); - //marking weather block is already full and no more dilatations are possible + //marking weather block is already full and no more dilatations are possible if (__popc(mainShmem[begSourceShmem + threadIdx.x + threadIdx.y * 32]) < 32) { isBlockFull[i & 1] = false; } mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] = bitDilatate(mainShmem[begSourceShmem + threadIdx.x + threadIdx.y * 32]); pipeline.consumer_release(); - ///////// step 2 load bottom and process top + ///////// step 2 load bottom and process top //load bottom pipeline.producer_acquire(); if (localBlockMetaData[(i & 1) * 20 + 14] < isGoldOffset) { @@ -1736,7 +1736,7 @@ inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { pipeline.consumer_release(); sync(cta); - /////////// step 3 load right process bottom + /////////// step 3 load right process bottom pipeline.producer_acquire(); if (localBlockMetaData[(i & 1) * 20 + 16] < isGoldOffset) { cuda::memcpy_async(cta, (&mainShmem[begfirstRegShmem]), @@ -1764,8 +1764,8 @@ inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { , begSecRegShmem, i);*/ pipeline.consumer_release(); - /////////// step 4 load left process right - //load left + /////////// step 4 load left process right + //load left pipeline.producer_acquire(); if (mainShmem[startOfLocalWorkQ + i] > 0) { cuda::memcpy_async(cta, (&mainShmem[begSecRegShmem]), @@ -1779,7 +1779,7 @@ inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { if (threadIdx.x == (fbArgs.dbXLength - 1)) { // now we need to load the data from the neigbouring blocks - //first checking is there anything to look to + //first checking is there anything to look to if (localBlockMetaData[(i & 1) * 20 + 16] < isGoldOffset) { //now we load - we already done earlier up and down so now we are considering only anterior, posterior , left , right possibilities if (mainShmem[begSourceShmem + threadIdx.x + threadIdx.y * 32] > 0) { @@ -1801,7 +1801,7 @@ inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { pipeline.consumer_release(); sync(cta); - /////// step 5 load anterior process left + /////// step 5 load anterior process left //load anterior pipeline.producer_acquire(); if (localBlockMetaData[(i & 1) * 20 + 17] < isGoldOffset) { @@ -1812,13 +1812,13 @@ inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { , pipeline); } pipeline.producer_commit(); - //process left + //process left pipeline.consumer_wait(); - // so we first check for corner cases + // so we first check for corner cases if (threadIdx.x == 0) { // now we need to load the data from the neigbouring blocks - //first checking is there anything to look to + //first checking is there anything to look to if (localBlockMetaData[(i & 1) * 20 + 15] < isGoldOffset) { //now we load - we already done earlier up and down so now we are considering only anterior, posterior , left , right possibilities if (mainShmem[begSourceShmem + threadIdx.x + threadIdx.y * 32] > 0) { @@ -1842,7 +1842,7 @@ inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { pipeline.consumer_release(); sync(cta); - /////// step 6 load posterior process anterior + /////// step 6 load posterior process anterior //load posterior pipeline.producer_acquire(); if (localBlockMetaData[(i & 1) * 20 + 18] < isGoldOffset) { @@ -1858,10 +1858,10 @@ inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { //process anterior pipeline.consumer_wait(); - // so we first check for corner cases + // so we first check for corner cases if (threadIdx.y == (fbArgs.dbYLength - 1)) { // now we need to load the data from the neigbouring blocks - //first checking is there anything to look to + //first checking is there anything to look to if (localBlockMetaData[(i & 1) * 20 + 17] < isGoldOffset) { //now we load - we already done earlier up and down so now we are considering only anterior, posterior , left , right possibilities if (mainShmem[begSourceShmem + threadIdx.x + threadIdx.y * 32] > 0) { @@ -1885,8 +1885,8 @@ inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { pipeline.consumer_release(); sync(cta); - /////// step 7 - //load reference if needed or data for next iteration if there is such + /////// step 7 + //load reference if needed or data for next iteration if there is such //process posterior, save data from res shmem to global memory also we mark weather block is full pipeline.producer_acquire(); @@ -1894,7 +1894,7 @@ inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { if (localBlockMetaData[(i & 1) * 20 + ((1 - isGoldForLocQueue[i]) + 1)] //fp for gold and fn count for not gold > localBlockMetaData[(i & 1) * 20 + ((1 - isGoldForLocQueue[i]) + 3)]) {// so count is bigger than counter so we should validate cuda::memcpy_async(cta, (&mainShmem[begfirstRegShmem]), - &fbArgs.origArrsPointer[mainShmem[startOfLocalWorkQ + i] * fbArgs.metaData.mainArrSectionLength + fbArgs.metaData.mainArrXLength * (isGoldForLocQueue[i])], //we look for + &fbArgs.origArrsPointer[mainShmem[startOfLocalWorkQ + i] * fbArgs.metaData.mainArrSectionLength + fbArgs.metaData.mainArrXLength * (isGoldForLocQueue[i])], //we look for cuda::aligned_size_t<128>(sizeof(uint32_t) * fbArgs.metaData.mainArrXLength) , pipeline); @@ -1916,13 +1916,13 @@ inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { //processPosteriorAndSaveResShmem pipeline.consumer_wait(); - //dilatate posterior + //dilatate posterior - // so we first check for corner cases + // so we first check for corner cases if (threadIdx.y == 0) { // now we need to load the data from the neigbouring blocks - //first checking is there anything to look to + //first checking is there anything to look to if (localBlockMetaData[(i & 1) * 20 + 18] < isGoldOffset) { //now we load - we already done earlier up and down so now we are considering only anterior, posterior , left , right possibilities if (mainShmem[begSourceShmem + threadIdx.x + threadIdx.y * 32] > 0) { @@ -1993,10 +1993,10 @@ inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { if (localBlockMetaData[(i & 1) * 20 + ((1 - isGoldForLocQueue[i]) + 1)] //fp for gold and fn count for not gold > localBlockMetaData[(i & 1) * 20 + ((1 - isGoldForLocQueue[i]) + 3)]) {// so count is bigger than counter so we should validate - // now we look through bits and when some is set we call it a result + // now we look through bits and when some is set we call it a result #pragma unroll for (uint8_t bitPos = 0; bitPos < 32; bitPos++) { - //if any bit here is set it means it should be added to result list + //if any bit here is set it means it should be added to result list if (isBitAt(mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32], bitPos) && !isBitAt(mainShmem[begSourceShmem + threadIdx.x + threadIdx.y * 32], bitPos) && isBitAt(mainShmem[begfirstRegShmem + threadIdx.x + threadIdx.y * 32], bitPos) @@ -2015,7 +2015,7 @@ inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { }; - // add results to global memory + // add results to global memory //we add one gere jjust to distinguish it from empty result fbArgs.resultListPointerMeta[mainShmem[begSecRegShmem + threadIdx.x + threadIdx.y * 32]] = uint32_t(mainShmem[startOfLocalWorkQ + i] + (isGoldOffset * isGoldForLocQueue[i]) + 1); fbArgs.resultListPointerLocal[mainShmem[begSecRegShmem + threadIdx.x + threadIdx.y * 32]] = uint32_t((fbArgs.dbYLength * 32 * bitPos) + (threadIdx.y * 32) + (threadIdx.x)); @@ -2032,7 +2032,7 @@ inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { ///////// pipeline.consumer_release(); - /// /// cleaning + /// /// cleaning sync(cta); @@ -2135,11 +2135,11 @@ inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { }; grid.sync(); - /////////////////////////**************************************************************************************************************** -/////////////////////////**************************************************************************************************************** -/////////////////////////**************************************************************************************************************** -/////////////////////////**************************************************************************************************************** -/////////////////////////**************************************************************************************************************** + /////////////////////////**************************************************************************************************************** +/////////////////////////**************************************************************************************************************** +/////////////////////////**************************************************************************************************************** +/////////////////////////**************************************************************************************************************** +/////////////////////////**************************************************************************************************************** /// metadata pass @@ -2198,7 +2198,7 @@ inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { && !fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 8]) { mainShmem[atomicAdd_block(&localWorkQueueCounter[0], 1)] = linIdexMeta + (isGoldOffset); - //setting to be activated to 0 + //setting to be activated to 0 fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 11] = 0; //setting active to 1 fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 7] = 1; @@ -2261,7 +2261,7 @@ inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { mainShmem[atomicAdd_block(&localWorkQueueCounter[0], 1)] = linIdexMeta; - //setting to be activated to 0 + //setting to be activated to 0 fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 12] = 0; //setting active to 1 fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 9] = 1; @@ -2308,7 +2308,7 @@ inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { } while (isGoldPassToContinue[0] || isSegmPassToContinue[0]); - //setting global iteration number to local one + //setting global iteration number to local one if (blockIdx.x == 0) { if (threadIdx.x == 2 && threadIdx.y == 0) { fbArgs.metaData.minMaxes[13] = iterationNumb[0]; @@ -2335,7 +2335,7 @@ inline occupancyCalcData getOccupancy() { int minGridSize; // The minimum grid size needed to achieve the maximum occupancy for a full device launch int gridSize; // The actual grid size needed, based on input size - // for min maxes kernel + // for min maxes kernel cudaOccupancyMaxPotentialBlockSize( &minGridSize, &blockSize, @@ -2344,7 +2344,7 @@ inline occupancyCalcData getOccupancy() { res.warpsNumbForMinMax = blockSize / 32; res.blockSizeForMinMax = minGridSize; - // for min maxes kernel + // for min maxes kernel cudaOccupancyMaxPotentialBlockSize( &minGridSize, &blockSize, @@ -2461,7 +2461,7 @@ ForBoolKernelArgs mainKernelsRun(ForFullBoolPrepArgs& fFArgs, const int WI checkCuda(cudaDeviceSynchronize(), "last "); - /////////// error handling + /////////// error handling syncErr = cudaGetLastError(); asyncErr = cudaDeviceSynchronize(); if (syncErr != cudaSuccess) printf("Error in syncErr: %s\n", cudaGetErrorString(syncErr)); @@ -2508,7 +2508,7 @@ int getHausdorffDistance_CUDA_Generic(at::Tensor goldStandard, //TODO() use https ://pytorch.org/cppdocs/notes/tensor_cuda_stream.html cudaStream_t stream1; cudaStreamCreate(&stream1); - + MetaDataCPU metaData; size_t size = sizeof(unsigned int) * 20; unsigned int* minMaxesCPU = (unsigned int*)malloc(size); @@ -2521,7 +2521,7 @@ int getHausdorffDistance_CUDA_Generic(at::Tensor goldStandard, forFullBoolPrepArgs.segmArr = algoOutput; occupancyCalcData occData = getOccupancy(); - + ForBoolKernelArgs fbArgs = executeHausdoff(forFullBoolPrepArgs, WIDTH, HEIGHT, DEPTH, occData, stream1, false, robustnessPercent, resIterNeeded); size_t sizeMinMax = sizeof(unsigned int) * 20; @@ -2599,7 +2599,7 @@ at::Tensor getHausdorffDistance_CUDA_FullResList(at::Tensor goldStandard, , const int WIDTH, const int HEIGHT, const int DEPTH , const float robustnessPercent, at::Tensor numberToLookFor) { - + at::Tensor res; AT_DISPATCH_ALL_TYPESWithBool(goldStandard.type(), "getHausdorffDistance_CUDA_FullResList", ([&] { res= getHausdorffDistance_CUDA_FullResList_local(goldStandard, algoOutput, WIDTH, HEIGHT, DEPTH, robustnessPercent, numberToLookFor); @@ -2607,4 +2607,4 @@ at::Tensor getHausdorffDistance_CUDA_FullResList(at::Tensor goldStandard, })); return res; -} \ No newline at end of file +} diff --git a/monai/metrics/morphological_hausdorff_distance.py b/monai/metrics/morphological_hausdorff_distance.py index 780371938c5..ecf0e1d189b 100644 --- a/monai/metrics/morphological_hausdorff_distance.py +++ b/monai/metrics/morphological_hausdorff_distance.py @@ -24,26 +24,26 @@ class MorphologicalHausdorffDistanceMetric(CumulativeIterationMetric): """ Work is based onthe principle of application of mathemathical morphology more precisely dilatation, more details is presented in works [1] and [2]. Calculated is roughly related to the largest thickness of the difference between the true and estimatedmasks, and constitutes Approximation of true Hausdorff distance. The correlation between true and morphological Hausdorf distance is around 0.9, less for robust version . -Hovewer this implementation is also around 100 times faster than exact MONAI Hausdorff distance calculation. One can measure 3 diffrent metrics based on Hausdorff ditance. +Hovewer this implementation is also around 100 times faster than exact MONAI Hausdorff distance calculation. One can measure 3 diffrent metrics based on Hausdorff ditance. 1)simple Hausdorff distance - it tell what is the distance between two most distant points one from y and other from y_pred - + 2)robust Hausdorff distance - modification of 1) where we stop analyzing points when we already analyzed given percent of total number of points - is less sensitive to outliers. It is also slightly faster - 3)mean Hausdorff distance - can be understood as approximately mean distance between pair of points where one point is from y and other from y_pred. + 3)mean Hausdorff distance - can be understood as approximately mean distance between pair of points where one point is from y and other from y_pred. 4)additionally one can request as a result one dimensional tensor that will contain data for each point what is HD value for it (like we would do Hausdorff distance calculation only for this point and all points from other mask) Compute Hausdorff Distance between two tensors. In addition, specify the `percent` (for robust Hausdorff distance ) parameter can get the percentile of the distance. Input `y_pred` is compared with ground truth `y`. - + Args: percent: an optional float number between 0 and 1. If specified, the corresponding percent of the Hausdorff Distance rather than the maximum result will be achieved. Defaults to 1.0 . 1) Érick Oliveira Rodrigues,An efficient and locality-oriented Hausdorff distance algorithm: Proposal and analysis of paradigms and implementations, Pattern Recognition,Volume 117,2021 - 2) Karimi, Davood & Salcudean, Septimiu. (2019). Reducing the Hausdorff Distance in Medical Image Segmentation With Convolutional Neural Networks. IEEE Transactions on Medical Imaging. PP. 1-1. 10.1109/TMI.2019.2930068. + 2) Karimi, Davood & Salcudean, Septimiu. (2019). Reducing the Hausdorff Distance in Medical Image Segmentation With Convolutional Neural Networks. IEEE Transactions on Medical Imaging. PP. 1-1. 10.1109/TMI.2019.2930068. """ @@ -55,7 +55,7 @@ def __init__( self.percent = percent self.compiled_extension = load_module( "hausdorff_cpp" - ) + ) def compute_hausdorff_distance( @@ -67,14 +67,14 @@ def compute_hausdorff_distance( Compute the Hausdorff distance. Args: - y_pred: input data to compute, It must be 3 dimensional + y_pred: input data to compute, It must be 3 dimensional y: ground truth to compute mean the distance. It must be 3 dimensional, Dimensionality needs to be identical as in y_pred percent: an optional float number between 0 and 1. If specified, the corresponding percent of the Hausdorff Distance rather than the maximum result will be achieved. Defaults to 1.0 . - numberToLookFor: 0 dimensional tensor marking what value are we intrested in the supplied y and y_pred, defined becouse in case of multiorgan segmentations frequently we can have information about multiple organs in the same mask just marked by diffrent numbers - for example in case of boolean array we can suupply it like torch.ones(1, dtype =bool) + numberToLookFor: 0 dimensional tensor marking what value are we intrested in the supplied y and y_pred, defined becouse in case of multiorgan segmentations frequently we can have information about multiple organs in the same mask just marked by diffrent numbers + for example in case of boolean array we can suupply it like torch.ones(1, dtype =bool) """ @@ -83,5 +83,3 @@ def compute_hausdorff_distance( sizz = y.shape return self.compiled_extension.getHausdorffDistance(y_pred,y,sizz[0],sizz[1],sizz[2], self.percent, numberToLookFor ) - - diff --git a/tests/test_hausdorff_distance_morphologic.py b/tests/test_hausdorff_distance_morphologic.py index 03caa050a27..5af06727257 100644 --- a/tests/test_hausdorff_distance_morphologic.py +++ b/tests/test_hausdorff_distance_morphologic.py @@ -25,7 +25,7 @@ TEST_CASES = [ [[a,b,1.0,numbToLookFor], 10] - + ] class TestHausdorffDistanceMorphological(unittest.TestCase): @parameterized.expand(TEST_CASES) From 3f0f47a48cbf8027e435e2135f13fe578dc0c32b Mon Sep 17 00:00:00 2001 From: Jakub Mitura <53857487+jakubMitura14@users.noreply.github.com> Date: Wed, 6 Apr 2022 11:33:08 +0200 Subject: [PATCH 04/41] Added licenses and entry in init.py in metrics --- monai/_extensions/hausdorff/hausdorff_cpp.cpp | 13 +++++++++++++ monai/_extensions/hausdorff/hausdorff_cuda.cu | 11 +++++++++++ monai/metrics/__init__.py | 1 + 3 files changed, 25 insertions(+) diff --git a/monai/_extensions/hausdorff/hausdorff_cpp.cpp b/monai/_extensions/hausdorff/hausdorff_cpp.cpp index 1bf55c49163..5e4aea18fcb 100644 --- a/monai/_extensions/hausdorff/hausdorff_cpp.cpp +++ b/monai/_extensions/hausdorff/hausdorff_cpp.cpp @@ -1,3 +1,16 @@ +// Copyright (c) MONAI Consortium +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + + + #include #include diff --git a/monai/_extensions/hausdorff/hausdorff_cuda.cu b/monai/_extensions/hausdorff/hausdorff_cuda.cu index 3a6748ab0f0..08cf40188a7 100644 --- a/monai/_extensions/hausdorff/hausdorff_cuda.cu +++ b/monai/_extensions/hausdorff/hausdorff_cuda.cu @@ -1,3 +1,14 @@ +// Copyright (c) MONAI Consortium +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + #include //#include #include diff --git a/monai/metrics/__init__.py b/monai/metrics/__init__.py index d18c20f7b28..01cb9b29c75 100644 --- a/monai/metrics/__init__.py +++ b/monai/metrics/__init__.py @@ -13,6 +13,7 @@ from .cumulative_average import CumulativeAverage from .froc import compute_fp_tp_probs, compute_froc_curve_data, compute_froc_score from .hausdorff_distance import HausdorffDistanceMetric, compute_hausdorff_distance, compute_percent_hausdorff_distance +from .morphological_hausdorff_distance import MorphologicalHausdorffDistanceMetric, compute_hausdorff_distance from .meandice import DiceMetric, compute_meandice from .metric import Cumulative, CumulativeIterationMetric, IterationMetric, Metric from .regression import MAEMetric, MSEMetric, PSNRMetric, RMSEMetric From 427e11407602f0cb0cd96c8571cbde85ea465290 Mon Sep 17 00:00:00 2001 From: Jakub Mitura <53857487+jakubMitura14@users.noreply.github.com> Date: Wed, 6 Apr 2022 12:15:24 +0200 Subject: [PATCH 05/41] improved docs --- monai/_extensions/hausdorff/hausdorff_cuda.cu | 1060 ++++++++--------- 1 file changed, 513 insertions(+), 547 deletions(-) diff --git a/monai/_extensions/hausdorff/hausdorff_cuda.cu b/monai/_extensions/hausdorff/hausdorff_cuda.cu index 929ce6cdb2b..b8199ebf893 100644 --- a/monai/_extensions/hausdorff/hausdorff_cuda.cu +++ b/monai/_extensions/hausdorff/hausdorff_cuda.cu @@ -44,29 +44,23 @@ constexpr uint32_t lengthOfMainShmem = 4460;//4460; constexpr uint32_t begResShmem = 1088; constexpr uint32_t begfirstRegShmem = 2112; constexpr uint32_t begSecRegShmem = 3136; -constexpr uint32_t begSMallRegShmemA = 0; -constexpr uint32_t begSMallRegShmemB = 1056; constexpr uint32_t begSourceShmem = 32; -//as the pipeline is so asynchronous we need some additional storage for saving data related to the operations -//will define how big is the amount of space in order to be able to use fast version of modulo operator it should be power of 2 ... -constexpr uint32_t modForPipelineVars = 16; -constexpr uint32_t fpLocCountBeg = 4460; - - - -// other //added to linear index meta in order to mark weather block is of type gold or not constexpr uint32_t isGoldOffset = (UINT16_MAX * 10); -/** -In order to be able to use cuda malloc 3d we will implemnt it as a series -of 3d arrays -*/ +/*************************************** + * structs + * ********************************/ + + /** + In order to be able to use cuda malloc 3d we will implemnt it as a series + of 3d arrays + */ #pragma once extern "C" struct MetaDataCPU { int metaXLength; @@ -80,15 +74,6 @@ extern "C" struct MetaDataCPU { //12) global FPandFn offset 13)globalIterationNumb //array3dWithDimsCPU minMaxes; unsigned int* minMaxes; - - ////// counts of false positive and false negatives in given metadata blocks - - ///// sizes of array below will be established on the basis of fp and fn values known after boolKernel finished execution - - //work queue - workqueue counter already present in minMaxes as entry 9 - //in practice it is matrix of length the same as FP+FN global count +1 and width of 5 - //1) xMeta; 2)yMeta 3)zMeta 4)isGold 5)iteration number - //we use one single long rewsult list - in order to avoid overwriting each block each block has established offset where it would write it's results uint32_t* resultList; }; @@ -198,20 +183,12 @@ struct ForBoolKernelArgs { int Ny; int Nz; - - // Frequent accesses to "a" and "b"; infrequent accesses to "x" and "y": - //cuda::annotated_ptr a_p{ a }, b_p{ b }; - //cuda::annotated_ptr x_s{ x }, y_s{ y }; - int32_t* resultListPointerMeta; int32_t* resultListPointerLocal; int32_t* resultListPointerIterNumb; torch::Tensor resultListPointerIterNumbTensor; - - - uint32_t* origArrsPointer; uint32_t* mainArrAPointer; uint32_t* mainArrBPointer; @@ -257,14 +234,10 @@ now what occupies what positions 20 : empty -###origArrs +###main arrays 0-x : reducedGold (x+1) - 2x : reducedSegm - */ -// uint32_t* mainArr; - - float robustnessPercent = 1.0;// 0.95; @@ -274,77 +247,6 @@ now what occupies what positions - -//just utility for unit testing - set some data bout points -#pragma once -extern "C" struct forTestPointStruct { - int x; - int y; - int z; - - bool isGold; - bool isGoldAndSegm; - - int xMeta; - int yMeta; - int zMeta; - - - - bool shouldBeInResAfterOneDil; - bool shouldBeInResAfterTwoDil; - bool isFoundAndDilatated = false; - bool isFoundInResult = false; - - bool isFoundDilTop = false; - bool isFoundDilBottom = false; - - bool isFoundDilAnterior = false; - bool isFoundDilPosterior = false; - - bool isFoundDilLeft = false; - bool isFoundDilRight = false; - -}; - - -#pragma once -extern "C" struct forTestMetaDataStruct { - - int xMeta; - int yMeta; - int zMeta; - - int requiredspaceInFpResultList; - int requiredspaceInFnResultList; - - bool isToBeActiveAtStart; - bool isToBeActiveAfterOneIter; - bool isToBeActiveAfterTwoIter; - - bool isToBeValidatedFpAfterOneIter; - bool isToBeValidatedFpAfterTwoIter; - - bool isToBeValidatedFnAfterOneIter; - bool isToBeValidatedFnAfterTwoIter; - - - bool isToBeFullAfterOneIter; - bool isToBeFullAfterTwoIter; - - int fpCount; - int fnCount; - - int fpConterAfterOneDil; - int fpConterAfterTwoDil; - - int fnConterAfterOneDil; - int fnConterAfterTwoDil; - - -}; - - /* data from occupancy calculator API used to get optimal number of thread blocks and threads per thread block */ @@ -364,9 +266,14 @@ struct occupancyCalcData { -/* -copy from host to device -*/ +/*************************************** + * utils + * ********************************/ + + + /* + copy from host to device + */ #pragma once inline MetaDataGPU allocateMetaDataOnGPU(MetaDataCPU metaDataCPU, unsigned int*& minMaxes) { MetaDataGPU res; @@ -395,77 +302,107 @@ inline MetaDataGPU allocateMetaDataOnGPU(MetaDataCPU metaDataCPU, unsigned int*& size_t size = sizeof(unsigned int) * 20; cudaMemcpy(minMaxes, metaDataCPU.minMaxes, size, cudaMemcpyHostToDevice); - //res.resultList = allocate3dInGPU(metaDataCPU.resultList); + return res; - //res.metaXLength = metaDataCPU.metaXLength; - //res.MetaYLength = metaDataCPU.MetaYLength; - //res.MetaZLength = metaDataCPU.MetaZLength; +} - //res.totalMetaLength = metaDataCPU.totalMetaLength; - //allocating on GPU and copying cpu data onto GPU +/*************************************** + * utils + * ********************************/ - return res; + /* + setting the linear index of metadata blocks that are in given direction if there is no such (out of range) we will save it as UINT32_MAX + */ +#pragma once +template +__device__ inline void setNeighbourBlocks(ForBoolKernelArgs fbArgs + , uint8_t idX, uint8_t inArrIndex, bool predicate, uint32_t toAdd + , uint32_t linIdexMeta, MetaDataGPU metaData, uint32_t localBlockMetaData[20]) { + + if ((threadIdx.x == idX) && (threadIdx.y == 0)) { + if (predicate) { + + localBlockMetaData[inArrIndex] = (linIdexMeta + toAdd); + } + else { + localBlockMetaData[inArrIndex] = isGoldOffset; + } + }; } + /* -copy from device to host +gettinng array for dilatations +basically arrays will alternate between iterations once one will be source other target then they will switch - we will decide upon knowing +wheather the iteration number is odd or even */ #pragma once -inline void copyMetaDataToCPU(MetaDataCPU metaDataCPU, MetaDataGPU metaDataGPU) { - //copyDeviceToHost3d(metaDataGPU.fpCount, metaDataCPU.fpCount); - //copyDeviceToHost3d(metaDataGPU.fnCount, metaDataCPU.fnCount); - size_t size = sizeof(unsigned int) * 20; +template +inline __device__ uint32_t* getSourceReduced(const ForBoolKernelArgs& fbArgs, const int(&iterationNumb)[1]) { - cudaMemcpy(metaDataCPU.minMaxes, metaDataGPU.minMaxes, size, cudaMemcpyDeviceToHost); -} + if ((iterationNumb[0] & 1) == 0) { + return fbArgs.mainArrAPointer; -#pragma once -inline cudaError_t checkCuda(cudaError_t result, std::string description) -{ - if (result != cudaSuccess) { - std::cout << description; - std::cout << "\n"; - // printf("%d", description); - fprintf(stderr, "CUDA Runtime Error in %d : %s\n", description, cudaGetErrorString(result)); - assert(result == cudaSuccess); } - return result; -} - + else { + return fbArgs.mainArrBPointer; + } +} /* -setting the linear index of metadata blocks that are in given direction if there is no such (out of range) we will save it as UINT32_MAX +gettinng target array for dilatations +basically arrays will alternate between iterations once one will be source other target then they will switch - we will decide upon knowing +wheather the iteration number is odd or even */ #pragma once -template -__device__ inline void setNeighbourBlocks(ForBoolKernelArgs fbArgs - , uint8_t idX, uint8_t inArrIndex, bool predicate, uint32_t toAdd - , uint32_t linIdexMeta, MetaDataGPU metaData, uint32_t localBlockMetaData[20]) { +template +inline __device__ uint32_t* getTargetReduced(const ForBoolKernelArgs& fbArgs, const int(&iterationNumb)[1]) { - if ((threadIdx.x == idX) && (threadIdx.y == 0)) { - if (predicate) { + if ((iterationNumb[0] & 1) == 0) { + return fbArgs.mainArrBPointer; - localBlockMetaData[inArrIndex] = (linIdexMeta + toAdd); - } - else { - localBlockMetaData[inArrIndex] = isGoldOffset; - } - }; -} + } + else { + return fbArgs.mainArrAPointer; + } +} /* -iteration over metadata - becouse metadata may be small and to maximize occupancy we use linear index and then clalculate xMeta,ymeta,zMeta from this linear index ... +dilatation up and down - using bitwise operators +*/ +#pragma once +inline __device__ uint32_t bitDilatate(const uint32_t& x) { + return ((x) >> 1) | (x) | ((x) << 1); +} + +/* +return 1 if at given position of given number bit is set otherwise 0 */ #pragma once +inline __device__ uint32_t isBitAt(const uint32_t& numb, const int pos) { + return (numb & (1 << (pos))); +} + + + + +/*************************************** + * MinMaxes kernel + * ********************************/ + /* + + iteration over metadata - becouse metadata may be small and to maximize occupancy we use linear index and then clalculate xMeta,ymeta,zMeta from this linear index ... + */ +#pragma once template __global__ void getMinMaxes(ForBoolKernelArgs fbArgs , unsigned int* minMaxes @@ -474,15 +411,9 @@ __global__ void getMinMaxes(ForBoolKernelArgs fbArgs , MetaDataGPU metaData ) { - // __global__ void getMinMaxes(unsigned int* minMaxes) { - ////////////some initializations thread_block cta = this_thread_block(); - //thread_block_tile<32> tile = tiled_partition<32>(cta); - - //shared memory - __shared__ bool anyInGold[1]; //__shared__ uint32_t reduction_s[32]; //1)maxX 2)minX 3)maxY 4) minY 5) maxZ 6) minZ @@ -502,21 +433,10 @@ __global__ void getMinMaxes(ForBoolKernelArgs fbArgs if ((threadIdx.x == 1) && (threadIdx.y == 0) && (blockIdx.x == 0)) { - - // if (blockIdx.x == 0) { - // printf(" dims meta in min maxes kernel Meta X %d MetaY %d metaZ %d dbXSize %d dbYsize %d dbZsize %d minX %d minY %d minZ \n " - // , metaData.metaXLength, metaData.MetaYLength, metaData.MetaZLength - // , fbArgs.dbXLength, fbArgs.dbYLength, fbArgs.dbZLength - // , metaData.minX, metaData.minY, metaData.minZ - // ); - } __syncthreads(); - ///////////////////////// - - //main metadata iteration for (auto linIdexMeta = blockIdx.x; linIdexMeta < metaData.totalMetaLength; linIdexMeta += gridDim.x) { //we get from linear index the coordinates of the metadata block of intrest @@ -528,15 +448,10 @@ __global__ void getMinMaxes(ForBoolKernelArgs fbArgs uint32_t x = xMeta * fbArgs.dbXLength + xLoc;//absolute position for (uint8_t yLoc = threadIdx.y; yLoc < fbArgs.dbYLength; yLoc += blockDim.y) { uint32_t y = yMeta * fbArgs.dbYLength + yLoc;//absolute position - //if (y == 0) { - // printf("x %d in min maxes \n ", x); - //} if (y < fbArgs.Ny && x < fbArgs.Nx) { // resetting - - for (uint8_t zLoc = 0; zLoc < fbArgs.dbZLength; zLoc++) { uint32_t z = zMeta * fbArgs.dbZLength + zLoc;//absolute position if (z < fbArgs.Nz) { @@ -548,22 +463,8 @@ __global__ void getMinMaxes(ForBoolKernelArgs fbArgs bool segmBool = segmArr[x + y * fbArgs.Nx + z * fbArgs.Nx * fbArgs.Ny] == fbArgs.numberToLookFor; if (goldBool || segmBool) { anyInGold[0] = true; - //printf(" \n in min maxes dims meta in min maxes x %d y%d z%d xMeta %d yMeta %d zMeta %d kernel Meta X %d MetaY %d metaZ %d dbXSize %d dbYsize %d dbZsize %d minX %d minY %d minZ %d linIdexMeta %d counted %d \n " - // ,x,y,z, - // xMeta,yMeta,zMeta - // , metaData.metaXLength, metaData.MetaYLength, metaData.MetaZLength - // , fbArgs.dbXLength, fbArgs.dbYLength, fbArgs.dbZLength - // , metaData.minX, metaData.minY, metaData.minZ - // , linIdexMeta - // , int(floor((float)((linIdexMeta - ((zMeta * metaData.metaXLength * metaData.MetaYLength) + xMeta)) / metaData.metaXLength))) - //); - } - - - } - } } @@ -573,9 +474,7 @@ __global__ void getMinMaxes(ForBoolKernelArgs fbArgs /////////////////// setting min and maxes // //1)maxX 2)minX 3)maxY 4) minY 5) maxZ 6) minZ - auto active = coalesced_threads(); - sync(cta); - active.sync(); + __syncthreads(); if ((threadIdx.x == 0) && (threadIdx.y == 0) && anyInGold[0]) { minMaxesInShmem[1] = max(xMeta, minMaxesInShmem[1]); }; if ((threadIdx.x == 1) && (threadIdx.y == 0) && anyInGold[0]) { minMaxesInShmem[2] = min(xMeta, minMaxesInShmem[2]); }; @@ -584,20 +483,14 @@ __global__ void getMinMaxes(ForBoolKernelArgs fbArgs minMaxesInShmem[3] = max(yMeta, minMaxesInShmem[3]); - //if (minMaxesInShmem[3] > 0) { - // printf(" prim minMaxesInShmem maxY %d meta %d \n ", minMaxesInShmem[3], yMeta); - //} - }; if ((threadIdx.x == 3) && (threadIdx.y == 0) && anyInGold[0]) { minMaxesInShmem[4] = min(yMeta, minMaxesInShmem[4]); }; if ((threadIdx.x == 4) && (threadIdx.y == 0) && anyInGold[0]) { minMaxesInShmem[5] = max(zMeta, minMaxesInShmem[5]); }; if ((threadIdx.x == 5) && (threadIdx.y == 0) && anyInGold[0]) { minMaxesInShmem[6] = min(zMeta, minMaxesInShmem[6]); - // printf("local fifth %d \n", minMaxesInShmem[6]); }; - // active.sync(); - sync(cta); // just to reduce the warp divergence + __syncthreads(); // just to reduce the warp divergence anyInGold[0] = false; @@ -607,16 +500,12 @@ __global__ void getMinMaxes(ForBoolKernelArgs fbArgs } } - sync(cta); + __syncthreads(); auto active = coalesced_threads(); if ((threadIdx.x == 1) && (threadIdx.y == 0)) { - // printf("\n in minMaxes internal %d \n", minMaxesInShmem[1]); - //getTensorRow(tensorslice, fbArgs.metaData.minMaxes, fbArgs.metaData.minMaxes.Ny, 0, 0)[0] = 61; atomicMax(&minMaxes[1], minMaxesInShmem[1]); - //atomicMax(&minMaxes[1], 2); - // minMaxes[1] = 0; }; if ((threadIdx.x == 0) && (threadIdx.y == 0)) { @@ -626,36 +515,30 @@ __global__ void getMinMaxes(ForBoolKernelArgs fbArgs if ((threadIdx.x == 1) && (threadIdx.y == 0)) { atomicMax(&minMaxes[3], minMaxesInShmem[3]); - // printf(" minMaxesInShmem maxY %d \n ", minMaxes[3]); }; if ((threadIdx.x == 2) && (threadIdx.y == 0)) { atomicMin(&minMaxes[4], minMaxesInShmem[4]); - // printf(" minMaxesInShmem minY %d \n ", minMaxes[4]); - }; if (threadIdx.x == 3 && threadIdx.y == 0) { atomicMax(&minMaxes[5], minMaxesInShmem[5]); - // printf(" minMaxesInShmem %d \n ", minMaxes[5]); }; if (threadIdx.x == 4 && threadIdx.y == 0) { atomicMin(&minMaxes[6], minMaxesInShmem[6]); - // printf(" minMaxesInShmem %d \n ", minMaxes[6]); }; - - - - - } +/*************************************** + * boolPrepareKernel + * ********************************/ + /* iteration over metadata - becouse metadata may be small and to maximize occupancy we use linear index and then clalculate xMeta,ymeta,zMeta from this linear index ... @@ -678,9 +561,6 @@ __global__ void boolPrepareKernel(ForBoolKernelArgs fbArgs uint32_t sumFp = 0; uint32_t sumFn = 0; - //auto pipeline = cuda::make_pipeline(); - - //shared memory //TODO() make it dynamically sized @@ -698,20 +578,14 @@ __global__ void boolPrepareKernel(ForBoolKernelArgs fbArgs //1)maxX 2)minX 3)maxY 4) minY 5) maxZ 6) minZ __shared__ int minMaxesInShmem[7]; - - if ((threadIdx.x == 1) && (threadIdx.y == 1)) { fpSFnS[0] = 0; }; if ((threadIdx.x == 2) && (threadIdx.y == 1)) { fpSFnS[1] = 0; }; if ((threadIdx.x == 3) && (threadIdx.y == 1)) { anyInGold[1] = false; }; - if ((threadIdx.x == 4) && (threadIdx.y == 1)) { - anyInSegm[1] = false; + if ((threadIdx.x == 4) && (threadIdx.y == 1)) { anyInSegm[1] = false; }; - }; - - - sync(cta); + __syncthreads(); ///////////////////////// @@ -729,7 +603,7 @@ __global__ void boolPrepareKernel(ForBoolKernelArgs fbArgs anyInGold[0] = false; anyInSegm[0] = false; //iterating over data block - sync(cta); + __syncthreads(); for (uint8_t xLoc = threadIdx.x; xLoc < fbArgs.dbXLength; xLoc += blockDim.x) { uint32_t x = (xMeta + metaData.minX) * fbArgs.dbXLength + xLoc;//absolute position for (uint8_t yLoc = threadIdx.y; yLoc < fbArgs.dbYLength; yLoc += blockDim.y) { @@ -761,30 +635,10 @@ __global__ void boolPrepareKernel(ForBoolKernelArgs fbArgs if (goldBool) anyInGold[0] = true; if (segmBool) anyInSegm[0] = true; - //if (goldBool) { - // printf("in kernel gold x %d y %d z %d xMeta %d yMeta %d zMeta %d counted ymeta %d linmeta %d \n", x, y, z, xMeta, yMeta,zMeta - // , int(floor((float)((linIdexMeta - ((zMeta * metaData.metaXLength * metaData.MetaYLength) + xMeta)) / metaData.metaXLength))) - // , linIdexMeta); - //} - - //if (segmBool) { - // printf("in kernel segm x %d y %d z %d xMeta %d yMeta %d zMeta %d counted ymeta %d linmeta %d \n", x, y, z, xMeta, yMeta, zMeta - // , int(floor((float)((linIdexMeta - ((zMeta * metaData.metaXLength * metaData.MetaYLength) + xMeta)) / metaData.metaXLength))) - // , linIdexMeta); - //} - - } } } - //if (sharedForGold[xLoc + yLoc * fbArgs.dbXLength] > 0) { - // printf("in kernel Metax %d yMeta %d zMeta %d linearLocal %d linIdexMeta %d column %d \n" - // , xMeta, yMeta, zMeta, xLoc + yLoc * fbArgs.dbXLength, linIdexMeta - // , sharedForGold[xLoc + yLoc * fbArgs.dbXLength]); - //} - - } } //reset local metadata @@ -810,35 +664,7 @@ __global__ void boolPrepareKernel(ForBoolKernelArgs fbArgs } } - // sync(cta); - - - - //cuda::memcpy_async(cta, (&origArrs[linIdexMeta * metaData.mainArrSectionLength]) , (sharedForGold), sizeof(uint32_t) * cta.size(), barrier); - //barrier.arrive_and_wait(); // Waits for all copies to complete - - - // cuda::memcpy_async(cta, (&origArrs[linIdexMeta * metaData.mainArrSectionLength]), (sharedForGoldB), (sizeof(uint32_t) * blockDim.x * blockDim.y), barrier); - //barrier.arrive_and_wait(); // Waits for all copies to complete - - //cuda::memcpy_async(cta, (&origArrs[linIdexMeta * metaData.mainArrSectionLength + metaData.mainArrXLength]), (sharedForSegmB), (sizeof(uint32_t) * blockDim.x * blockDim.y), barrier); - //barrier.arrive_and_wait(); // Waits for all copies to complete - - //cuda::memcpy_async(cta, (&mainArr[linIdexMeta * metaData.mainArrSectionLength ]), (sharedForGoldB), (sizeof(uint32_t) * blockDim.x * blockDim.y), barrier); - // barrier.arrive_and_wait(); // Waits for all copies to complete - - // cuda::memcpy_async(cta, (&mainArr[linIdexMeta * metaData.mainArrSectionLength + metaData.mainArrXLength*1]), (sharedForSegmB), (sizeof(uint32_t) * blockDim.x * blockDim.y) , barrier); - // barrier.arrive_and_wait(); // Waits for all copies to complete - - // cuda::memcpy_async(cta, (&mainArr[linIdexMeta * metaData.mainArrSectionLength + metaData.mainArrXLength*2]), (sharedForGoldB), (sizeof(uint32_t) * blockDim.x * blockDim.y), barrier); - // barrier.arrive_and_wait(); // Waits for all copies to complete - - // cuda::memcpy_async(cta, (&mainArr[linIdexMeta * metaData.mainArrSectionLength + metaData.mainArrXLength*3]), (sharedForSegmB), (sizeof(uint32_t) * blockDim.x * blockDim.y), barrier); - // barrier.arrive_and_wait(); // Waits for all copies to complete - - sync(cta); - - + __syncthreads(); /////adding the block and total number of the Fp's and Fn's sumFp = reduce(tile, sumFp, plus()); @@ -848,29 +674,23 @@ __global__ void boolPrepareKernel(ForBoolKernelArgs fbArgs sharedForGold[tile.meta_group_rank()] = sumFp; sharedForSegm[tile.meta_group_rank()] = sumFn; } - sync(cta);//waiting so shared memory will be loaded evrywhere + __syncthreads();//waiting so shared memory will be loaded evrywhere //on single thread we do last sum reduction auto active = coalesced_threads(); - //if ((threadIdx.x == 0) && (threadIdx.y == 0)) { - // printf("xMeta %d yMeta %d zMeta %d \n", xMeta, yMeta, zMeta); - //} + if ((threadIdx.x == 0) && (threadIdx.y == 0) && isNotEmpty) { sharedForGold[33] = 0;//reset for (int i = 0; i < tile.meta_group_size(); i += 1) { sharedForGold[33] += sharedForGold[i]; - /* if (sharedForGold[i]>0) { - printf("adding sharedForGold[i] %d in gold \n ", sharedForGold[i]); - }*/ + }; fpSFnS[0] += sharedForGold[33];// will be needed later for global set - //metaDataArr[linIdexMeta * metaData.metaDataSectionLength + 1] = sharedForGold[33]; localBlockMetaData[1] = sharedForGold[33]; } - // if (isToBeExecutedOnActive(active, 1) && isNotEmpty) { if ((threadIdx.x == 0) && (threadIdx.y == 1) && isNotEmpty) { @@ -915,18 +735,14 @@ __global__ void boolPrepareKernel(ForBoolKernelArgs fbArgs metaDataArr[linIdexMeta * metaData.metaDataSectionLength + threadIdx.x] = localBlockMetaData[threadIdx.x]; }; - sync(cta); // just to reduce the warp divergence + __syncthreads(); // just to reduce the warp divergence - // copy metadata to global memory - - //cuda::memcpy_async(cta, &metaDataArr[linIdexMeta * metaData.metaDataSectionLength], (&localBlockMetaData[0]), (sizeof(uint32_t) * 20), barrier); - // barrier.arrive_and_wait(); // Waits for all copies to complete } - sync(cta); + __syncthreads(); //setting global fp and fn @@ -942,19 +758,16 @@ __global__ void boolPrepareKernel(ForBoolKernelArgs fbArgs -/* - a) we define offsets in the result list to have the results organized and avoid overwiting - b) if metadata block is active we add it in the work queue -*/ +/*************************************** + * firstMetaPrepareKernel + * ********************************/ /* we add here to appropriate queue data about metadata of blocks of intrest minMaxesPos- marks in minmaxes the postion of global offset counter -12) global FP offset 13) global FnOffset offsetMetadataArr- arrays from metadata holding data about result list offsets it can be either fbArgs.metaData.fpOffset or fbArgs.metaData.fnOffset */ - - #pragma once __device__ inline void addToQueue(uint32_t linIdexMeta, uint8_t isGold , unsigned int fpFnLocCounter[1], uint32_t localWorkQueue[1600], uint32_t localOffsetQueue[1600], unsigned int localWorkQueueCounter[1] @@ -1018,17 +831,13 @@ __global__ void firstMetaPrepareKernel(ForBoolKernelArgs fbArgs if ((threadIdx.x == 3)) { globalOffsetForBlock[0] = 0; } - sync(cta); + __syncthreads(); // classical grid stride loop - in case of unlikely event we will run out of space we will empty it prematurly //main metadata iteration for (uint32_t linIdexMeta = blockIdx.x * blockDim.x + threadIdx.x; linIdexMeta < metaData.totalMetaLength; linIdexMeta += blockDim.x * gridDim.x) { - // if ((threadIdx.x == 0) && (threadIdx.y == 0)) { - // printf("in first meta pass linIdexMeta %d blockIdx.x %d blockDim.x %d metaData.totalMetaLength %d threadIdx.x %d \n ", linIdexMeta, blockIdx.x, blockDim.x, metaData.totalMetaLength, threadIdx.x ); - //} - //goldpass addToQueue(linIdexMeta, 0 , fpFnLocCounter, localWorkQueue, localOffsetQueue, localWorkQueueCounter @@ -1041,60 +850,34 @@ __global__ void firstMetaPrepareKernel(ForBoolKernelArgs fbArgs , metaDataArr, metaData, minMaxes, workQueue); - - /* addToQueue(fbArgs, old, count, tensorslice, xMeta, yMeta, zMeta, fbArgs.metaData.fpOffset, fbArgs.metaData.fpCount, 0, fbArgs.metaData.isActiveSegm, fpFnLocCounter, localWorkAndOffsetQueue, localWorkQueueCounter); - addToQueue(fbArgs, old, count, tensorslice, xMeta, yMeta, zMeta, fbArgs.metaData.fnOffset, fbArgs.metaData.fnCount, 1, fbArgs.metaData.isActiveGold, fpFnLocCounter, localWorkAndOffsetQueue, localWorkQueueCounter);*/ } - sync(cta); + __syncthreads(); if ((threadIdx.x == 0)) { globalOffsetForBlock[0] = atomicAdd(&(minMaxes[12]), (fpFnLocCounter[0])); - /* if (fpFnLocCounter[0]>0) { - printf("\n in meta first pass global offset %d locCounter %d \n ", globalOffsetForBlock[0], fpFnLocCounter[0]); - }*/ - }; + }; if ((threadIdx.x == 1)) { if (localWorkQueueCounter[0] > 0) { globalWorkQueueCounter[0] = atomicAdd(&(minMaxes[9]), (localWorkQueueCounter[0])); } } - sync(cta); - - //exporting to global work queue - //cooperative_groups::memcpy_async(cta, (&workQueue[globalWorkQueueCounter[0]]), (localWorkQueue), (sizeof(uint32_t) * localWorkQueueCounter[0])); + __syncthreads(); //setting offsets for (uint32_t i = threadIdx.x; i < localWorkQueueCounter[0]; i += blockDim.x) { workQueue[globalWorkQueueCounter[0] + i] = localWorkQueue[i]; - /* printf("FFIrst meta pass lin meta to Work Q %d is gold %d to spot %d \n " - , localWorkQueue[i] - isGoldOffset*(localWorkQueue[i] >= isGoldOffset) - , (localWorkQueue[i] >= isGoldOffset), globalWorkQueueCounter[0] + i);*/ - //FP pass if (localWorkQueue[i] >= isGoldOffset) { metaDataArr[(localWorkQueue[i] - isGoldOffset) * metaData.metaDataSectionLength + 5] = localOffsetQueue[i] + globalOffsetForBlock[0]; - //printf("fp offset lin meta %d total offset %d global part %d local part %d \n " - // , localWorkQueue[i] - isGoldOffset - // , localOffsetQueue[i] + globalOffsetForBlock[0] - //, globalOffsetForBlock[0] - //, localOffsetQueue[i]); - } //FN pass else { metaDataArr[(localWorkQueue[i]) * metaData.metaDataSectionLength + 6] = localOffsetQueue[i] + globalOffsetForBlock[0]; - //printf("fn offset lin meta %d total offset %d global part %d local part %d \n " - // , localWorkQueue[i] - // , localOffsetQueue[i] + globalOffsetForBlock[0] - // , globalOffsetForBlock[0] - // , localOffsetQueue[i]); }; - //sync(cta); - } @@ -1105,19 +888,13 @@ __global__ void firstMetaPrepareKernel(ForBoolKernelArgs fbArgs - - - -// helper functions and utilities to work with CUDA from https://github.com/NVIDIA/cuda-samples - - - - - - +/*************************************** + * memoery allocations + * ********************************/ /* +Get arguments for kernels */ #pragma once template @@ -1131,13 +908,7 @@ inline ForBoolKernelArgs getArgsForKernel(ForFullBoolPrepArgs& mainFun mainFunArgs.Ny = yLen; mainFunArgs.Nz = zLen; - //main arrays allocations - //TCC* goldArrPointer; - //TCC* segmArrPointer; - //size_t sizeMainArr = (sizeof(T) * WIDTH * HEIGHT * DEPTH); - //size_t sizeMainArr = (sizeof(TCC) * xLen * yLen * zLen); - //array3dWithDimsGPU goldArr = allocateMainArray(goldArrPointer, mainFunArgs.goldArr.arrP, xLen, yLen, zLen, stream); - //array3dWithDimsGPU segmArr = allocateMainArray(segmArrPointer, mainFunArgs.segmArr.arrP, xLen, yLen, zLen, stream); + unsigned int* minMaxes; size_t sizeminMaxes = sizeof(unsigned int) * 20; cudaMallocAsync(&minMaxes, sizeminMaxes, stream); @@ -1175,12 +946,10 @@ inline ForBoolKernelArgs getArgsForKernel(ForFullBoolPrepArgs& mainFun } - - - - - #pragma once +/* +allocate memory after first kernel +*/ template inline MetaDataGPU allocateMemoryAfterMinMaxesKernel(ForBoolKernelArgs& gpuArgs, ForFullBoolPrepArgs& cpuArgs, cudaStream_t stream) { ////reduced arrays @@ -1247,10 +1016,6 @@ inline int allocateMemoryAfterBoolKernel(ForBoolKernelArgs& gpuArgs, ForFul int32_t* resultListPointerIterNumb; uint32_t* mainArrAPointer; uint32_t* mainArrBPointer; - //free no longer needed arrays - //cudaFreeAsync(gpuArgs.goldArr.arrP, stream); - //cudaFreeAsync(gpuArgs.segmArr.arrP, stream); - //copy on cpu size_t size = sizeof(unsigned int) * 20; cudaMemcpyAsync(cpuArgs.metaData.minMaxes, gpuArgs.metaData.minMaxes, size, cudaMemcpyDeviceToHost, stream); @@ -1264,7 +1029,7 @@ inline int allocateMemoryAfterBoolKernel(ForBoolKernelArgs& gpuArgs, ForFul cudaMallocAsync(&resultListPointerMeta, size, stream); -// we will allocate only if we need + // we will allocate only if we need if (resIterNeeded) { auto options = torch::TensorOptions() @@ -1284,7 +1049,6 @@ inline int allocateMemoryAfterBoolKernel(ForBoolKernelArgs& gpuArgs, ForFul size_t sizeB = gpuArgs.metaData.totalMetaLength * gpuArgs.metaData.mainArrSectionLength * sizeof(uint32_t); - //printf("size of reduced main arr %d total meta len %d mainArrSectionLen %d \n", sizeB, metaData.totalMetaLength, metaData.mainArrSectionLength); cudaMallocAsync(&mainArrAPointer, sizeB, 0); cudaMemcpyAsync(mainArrAPointer, gpuArgs.origArrsPointer, sizeB, cudaMemcpyDeviceToDevice, stream); @@ -1303,7 +1067,6 @@ inline int allocateMemoryAfterBoolKernel(ForBoolKernelArgs& gpuArgs, ForFul gpuArgs.resultListPointerLocal = resultListPointerLocal; gpuArgs.resultListPointerIterNumb = resultListPointerIterNumb; - //fbArgs.origArrsPointer = origArrsPointer; gpuArgs.mainArrAPointer = mainArrAPointer; gpuArgs.mainArrBPointer = mainArrBPointer; @@ -1317,123 +1080,9 @@ inline int allocateMemoryAfterBoolKernel(ForBoolKernelArgs& gpuArgs, ForFul - -#pragma once -template -inline void copyResultstoCPU(ForBoolKernelArgs& gpuArgs, ForFullBoolPrepArgs& cpuArgs, cudaStream_t stream) { - - - ////copy on cpu - size_t size = sizeof(unsigned int) * 20; - cudaMemcpyAsync(cpuArgs.metaData.minMaxes, gpuArgs.metaData.minMaxes, size, cudaMemcpyDeviceToHost, stream); - unsigned int fpPlusFn = cpuArgs.metaData.minMaxes[7] + cpuArgs.metaData.minMaxes[8]; - size = sizeof(uint32_t) * (fpPlusFn + 50); - - //uint32_t* resultListPointerMeta = (uint32_t*)calloc(fpPlusFn + 50, sizeof(uint32_t)); - //uint32_t* resultListPointerLocal = (uint32_t*)calloc(fpPlusFn + 50, sizeof(uint32_t)); - //uint32_t* resultListPointerIterNumb = (uint32_t*)calloc(fpPlusFn + 50, sizeof(uint32_t)); - - cpuArgs.resultListPointerMeta = (uint32_t*)calloc(fpPlusFn + 50, sizeof(uint32_t));; - cpuArgs.resultListPointerLocalCPU = (uint32_t*)calloc(fpPlusFn + 50, sizeof(uint32_t));; - cpuArgs.resultListPointerIterNumb = (uint32_t*)calloc(fpPlusFn + 50, sizeof(uint32_t));; - - cudaMemcpyAsync(cpuArgs.resultListPointerMeta, gpuArgs.resultListPointerMeta, size, cudaMemcpyDeviceToHost, stream); - cudaMemcpyAsync(cpuArgs.resultListPointerLocalCPU, gpuArgs.resultListPointerLocal, size, cudaMemcpyDeviceToHost, stream); - cudaMemcpyAsync(cpuArgs.resultListPointerIterNumb, gpuArgs.resultListPointerIterNumb, size, cudaMemcpyDeviceToHost, stream); - - -}; - - - -/* -gettinng array for dilatations -basically arrays will alternate between iterations once one will be source other target then they will switch - we will decide upon knowing -wheather the iteration number is odd or even -*/ -#pragma once -template -inline __device__ uint32_t* getSourceReduced(const ForBoolKernelArgs& fbArgs, const int(&iterationNumb)[1]) { - - - if ((iterationNumb[0] & 1) == 0) { - return fbArgs.mainArrAPointer; - - } - else { - return fbArgs.mainArrBPointer; - } - - -} - - -/* -gettinng target array for dilatations -*/ -#pragma once -template -inline __device__ uint32_t* getTargetReduced(const ForBoolKernelArgs& fbArgs, const int(&iterationNumb)[1]) { - - if ((iterationNumb[0] & 1) == 0) { - //printf(" BB "); - - return fbArgs.mainArrBPointer; - - } - else { - // printf(" AA "); - - return fbArgs.mainArrAPointer; - - } - -} - - -/* -dilatation up and down - using bitwise operators -*/ -#pragma once -inline __device__ uint32_t bitDilatate(const uint32_t& x) { - return ((x) >> 1) | (x) | ((x) << 1); -} - -/* -return 1 if at given position of given number bit is set otherwise 0 -*/ -#pragma once -inline __device__ uint32_t isBitAt(const uint32_t& numb, const int pos) { - return (numb & (1 << (pos))); -} - -#pragma once -inline uint32_t isBitAtCPU(const uint32_t& numb, const int pos) { - return (numb & (1 << (pos))); -} - - - - - - - -/* -5)Main block - a) we define the work queue iteration - so we divide complete work queue into parts and each thread block analyzes its own part - one data block at a textLinesFromStrings - b) we load values of data block into shared memory and immidiately do the bit wise up and down dilatations, and mark booleans needed to establish is the datablock full - c) synthreads - left,right, anterior,posterior dilatations... - d) add the dilatated info into dilatation array and padding info from dilatation to global memory - e) if block is to be validated we check is there is in the point of currently coverd voxel some voxel in other mas if so we add it to the result list and increment local reult counter - f) syncgrid() -6)analyze padding - we iterate over work queue as in 5 - a) we load into shared memory information from padding from blocks all around the block of intrest checking for boundary conditions - b) we save data of dilatated voxels into dilatation array making sure to synchronize appropriately in the thread block - c) we analyze the positive entries given the block is to be validated so we check is such entry is already in dilatation mask if not is it in other mask if first no and second yes we add to the result - d) also given any positive entry we set block as to be activated simple sum reduction should be sufficient - e) sync grid -*/ +/*************************************** + * main kernel + * ********************************/ template inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { @@ -1578,7 +1227,7 @@ inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { //here we caclulate the offset for given block depending on length of the workqueue and number of the available blocks in a grid // - this will give us number of work queue items per block - we will calculate offset on the basis of the block number - sync(cta); + __syncthreads(); do { @@ -1586,11 +1235,7 @@ inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { /////////////////////////**************************************************************************************************************** - /////////////////////////**************************************************************************************************************** - /////////////////////////**************************************************************************************************************** - /////////////////////////**************************************************************************************************************** - /////////////////////////**************************************************************************************************************** - /// dilataions + /// dilataions //initial cleaning and initializations include loading min maxes if (threadIdx.x == 7 && threadIdx.y == 0 && !isPaddingPass) { @@ -1636,7 +1281,7 @@ inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { cooperative_groups::memcpy_async(cta, (&localMinMaxes[0]), (&fbArgs.minMaxes[7]), cuda::aligned_size_t<4>(sizeof(unsigned int) * 5)); } - sync(cta); + __syncthreads(); /// load work QueueData into shared memory for (uint32_t bigloop = blockIdx.x * globalWorkQueueOffset[0]; bigloop < ((blockIdx.x + 1) * globalWorkQueueOffset[0]); bigloop += worQueueStep[0]) { @@ -1658,7 +1303,7 @@ inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { //now all of the threads in the block needs to have the same i value so we will increment by 1 we are preloading to the pipeline block metaData ////##### pipeline Step 0 - sync(cta); + __syncthreads(); @@ -1675,7 +1320,7 @@ inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { pipeline.producer_commit(); - sync(cta); + __syncthreads(); for (uint32_t i = 0; i < worQueueStep[0]; i += 1) { @@ -1698,7 +1343,7 @@ inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { pipeline.consumer_release(); - sync(cta); + __syncthreads(); ///////// step 1 load top and process main data //load top @@ -1745,7 +1390,7 @@ inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { } pipeline.consumer_release(); - sync(cta); + __syncthreads(); /////////// step 3 load right process bottom pipeline.producer_acquire(); @@ -1811,7 +1456,7 @@ inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { } pipeline.consumer_release(); - sync(cta); + __syncthreads(); /////// step 5 load anterior process left //load anterior pipeline.producer_acquire(); @@ -1851,7 +1496,7 @@ inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { pipeline.consumer_release(); - sync(cta); + __syncthreads(); /////// step 6 load posterior process anterior //load posterior @@ -1894,7 +1539,7 @@ inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { pipeline.consumer_release(); - sync(cta); + __syncthreads(); /////// step 7 //load reference if needed or data for next iteration if there is such @@ -1975,7 +1620,7 @@ inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { pipeline.consumer_release(); - sync(cta); + __syncthreads(); //////// step 8 basically in order to complete here anyting the count need to be bigger than counter // loading for next block if block is not to be validated it was already done earlier @@ -1997,7 +1642,7 @@ inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { - sync(cta); + __syncthreads(); //validation - so looking for newly covered voxel for opposite array so new fps or new fns pipeline.consumer_wait(); @@ -2005,7 +1650,7 @@ inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { if (localBlockMetaData[(i & 1) * 20 + ((1 - isGoldForLocQueue[i]) + 1)] //fp for gold and fn count for not gold > localBlockMetaData[(i & 1) * 20 + ((1 - isGoldForLocQueue[i]) + 3)]) {// so count is bigger than counter so we should validate // now we look through bits and when some is set we call it a result -#pragma unroll + #pragma unroll for (uint8_t bitPos = 0; bitPos < 32; bitPos++) { //if any bit here is set it means it should be added to result list if (isBitAt(mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32], bitPos) @@ -2045,7 +1690,7 @@ inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { /// /// cleaning - sync(cta); + __syncthreads(); if (threadIdx.x == 9 && threadIdx.y == 2) {// this is how it is encoded wheather it is gold or segm block @@ -2104,7 +1749,7 @@ inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { } }; - sync(cta); + __syncthreads(); } } @@ -2120,7 +1765,7 @@ inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { } - sync(cta); + __syncthreads(); // updating global counters if (threadIdx.x == 0 && threadIdx.y == 0) { @@ -2147,19 +1792,8 @@ inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { grid.sync(); /////////////////////////**************************************************************************************************************** -/////////////////////////**************************************************************************************************************** -/////////////////////////**************************************************************************************************************** -/////////////////////////**************************************************************************************************************** -/////////////////////////**************************************************************************************************************** -/// metadata pass - - - - - - - + /// metadata pass // preparation loads @@ -2334,6 +1968,11 @@ inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { +/*************************************** + * putting all kernels and memory allocations together + * ********************************/ + + /* get data from occupancy calculator API used to get optimal number of thread blocks and threads per thread block */ @@ -2380,10 +2019,6 @@ inline occupancyCalcData getOccupancy() { res.warpsNumbForMainPass = blockSize / 32; res.blockForMainPass = minGridSize; - // res.blockForMainPass = 1; - //res.blockForMainPass = 136; - //res.warpsNumbForMainPass = 8; - //printf("warpsNumbForMainPass %d blockForMainPass %d ", res.warpsNumbForMainPass, res.blockForMainPass); return res; } @@ -2403,7 +2038,7 @@ ForBoolKernelArgs executeHausdoff(ForFullBoolPrepArgs& fFArgs, const int W T* goldArrPointer = (T*)fFArgs.goldArr.data_ptr(); T* segmArrPointer = (T*)fFArgs.segmArr.data_ptr(); - ForBoolKernelArgs fbArgs = getArgsForKernel(fFArgs, occData.warpsNumbForMainPass, occData.blockForMainPass, WIDTH, HEIGHT, DEPTH, stream); + ForBoolKernelArgs fbArgs = getArgsForKernel(fFArgs, occData.warpsNumbForMainPass, occData.blockForMainPass, WIDTH, HEIGHT, DEPTH, stream); getMinMaxes << > > (fbArgs, fbArgs.minMaxes , goldArrPointer @@ -2456,37 +2091,6 @@ ForBoolKernelArgs executeHausdoff(ForFullBoolPrepArgs& fFArgs, const int W -#pragma once -template -ForBoolKernelArgs mainKernelsRun(ForFullBoolPrepArgs& fFArgs, const int WIDTH, const int HEIGHT, const int DEPTH, cudaStream_t stream, bool resToSave = false,bool resIterNeeded=false -) { - - //cudaDeviceReset(); - cudaError_t syncErr; - cudaError_t asyncErr; - - occupancyCalcData occData = getOccupancy(); - - //pointers ... - ForBoolKernelArgs fbArgs = executeHausdoff(fFArgs, WIDTH, HEIGHT, DEPTH, occData, resToSave, stream,1.0, resIterNeeded); - - checkCuda(cudaDeviceSynchronize(), "last "); - - /////////// error handling - syncErr = cudaGetLastError(); - asyncErr = cudaDeviceSynchronize(); - if (syncErr != cudaSuccess) printf("Error in syncErr: %s\n", cudaGetErrorString(syncErr)); - if (asyncErr != cudaSuccess) printf("Error in asyncErr: %s\n", cudaGetErrorString(asyncErr)); - - - cudaDeviceReset(); - - return fbArgs; -} - - - - // modified from https://github.com/pytorch/pytorch/blob/61d6c4386459441710fb4cfa2929a3f77e95e5f7/aten/src/ATen/Dispatch.h #define AT_DISPATCH_ALL_TYPESWithBool(TYPE, NAME, ...) \ [&] { \ @@ -2589,16 +2193,18 @@ at::Tensor getHausdorffDistance_CUDA_FullResList_local(at::Tensor goldStandard, return fbArgs.resultListPointerIterNumbTensor; } - +/* +Functions for pybind +*/ int getHausdorffDistance_CUDA(at::Tensor goldStandard, at::Tensor algoOutput - , const int WIDTH,const int HEIGHT,const int DEPTH - ,const float robustnessPercent, at::Tensor numberToLookFor) { + , const int WIDTH, const int HEIGHT, const int DEPTH + , const float robustnessPercent, at::Tensor numberToLookFor) { int res = 0; AT_DISPATCH_ALL_TYPESWithBool(goldStandard.type(), "getHausdorffDistance_CUDA", ([&] { - res= getHausdorffDistance_CUDA_Generic(goldStandard, algoOutput, WIDTH, HEIGHT, DEPTH, robustnessPercent,false, numberToLookFor); + res = getHausdorffDistance_CUDA_Generic(goldStandard, algoOutput, WIDTH, HEIGHT, DEPTH, robustnessPercent, false, numberToLookFor); })); return res; @@ -2613,9 +2219,369 @@ at::Tensor getHausdorffDistance_CUDA_FullResList(at::Tensor goldStandard, at::Tensor res; AT_DISPATCH_ALL_TYPESWithBool(goldStandard.type(), "getHausdorffDistance_CUDA_FullResList", ([&] { - res= getHausdorffDistance_CUDA_FullResList_local(goldStandard, algoOutput, WIDTH, HEIGHT, DEPTH, robustnessPercent, numberToLookFor); + res = getHausdorffDistance_CUDA_FullResList_local(goldStandard, algoOutput, WIDTH, HEIGHT, DEPTH, robustnessPercent, numberToLookFor); })); return res; } + + + + + + + + + +/************************************************************* +*Oliviera Algorithm +*************************************************************************************/ + + +typedef unsigned char uchar; +typedef unsigned int uint; +#pragma once +class Volume { + +private: + bool* volume; + int width, height, depth; + int getLinearIndex(int x, int y, int z); +public: + bool getVoxelValue(int x, int y, int z); + bool getPixelValue(int x, int y); + uint getWidth(); + uint getHeight(); + uint getDepth(); + bool* getVolume(); + void setVoxelValue(bool value, int x, int y, int z); + void setPixelValue(bool value, int x, int y); + Volume(int width, int height, int depth); + Volume(int width, int height); + void dispose(); + +}; + + + + +#define CUDA_DEVICE_INDEX 0 //setting the index of your CUDA device + +#define IS_3D 1 //setting this to 0 would grant a very slightly improvement on the performance if working with images only +#define CHEBYSHEV 0 //if not set to 1, then this algorithm would use an Euclidean-like metric, it is just an approximation. +//It can be changed according to the structuring element +#pragma once +class HausdorffDistance { + +private: + void print(cudaError_t error, char* msg); + +public: + int computeDistance(Volume* img1, Volume* img2, bool* d_img1, bool* d_img2); + +}; + + +inline Volume::Volume(const int width, const int height, const int depth) { + this->width = width; this->height = height; this->depth = depth; + volume = (bool*)calloc(width * height * depth, sizeof(bool)); +} + +#pragma once +inline Volume::Volume(const int width, const int height) { + this->width = width; this->height = height; this->depth = 1; + volume = (bool*)calloc(width * height * depth, sizeof(bool)); +} +#pragma once +inline int Volume::getLinearIndex(const int x, const int y, const int z) { + const int a = 1, b = width, c = (width) * (height); + return a * x + b * y + c * z; +} + +inline uint Volume::getWidth() { return this->width; } +inline uint Volume::getHeight() { return this->height; } +inline uint Volume::getDepth() { return this->depth; } +inline bool* Volume::getVolume() { return this->volume; } +inline bool Volume::getPixelValue(int x, int y) { return this->volume[getLinearIndex(x, y, 0)]; } +#pragma once +inline bool Volume::getVoxelValue(int x, int y, int z) { + return volume[getLinearIndex(x, y, z)]; +} +#pragma once +inline void Volume::setPixelValue(bool value, const int x, const int y) { + volume[getLinearIndex(x, y, 0)] = value; +} +#pragma once +inline void Volume::setVoxelValue(bool value, const int x, const int y, const int z) { + volume[getLinearIndex(x, y, z)] = value; +} +#pragma once +inline void Volume::dispose() { + free(volume); +} + +typedef unsigned char uchar; +typedef unsigned int uint; + +#pragma once +__device__ int finished; //global variable that contains a boolean which indicates when to stop the kernel processing +#pragma once +__constant__ __device__ int WIDTH, HEIGHT, DEPTH; //constant variables that contain the size of the volume + + +#pragma once +__global__ void dilate(const bool* IMG1, const bool* IMG2, const bool* img1Read, const bool* img2Read, + bool* img1Write, bool* img2Write) { + + const int id = blockDim.x * blockIdx.x + threadIdx.x; +#if !IS_3D + const int x = id % WIDTH, y = id / WIDTH; +#else + const int x = id % WIDTH, y = (id / WIDTH) % HEIGHT, z = (id / WIDTH) / HEIGHT; +#endif + + if (id < WIDTH * HEIGHT * DEPTH) { + + + if (img1Read[id]) { + if (x + 1 < WIDTH) img1Write[id + 1] = true; + if (x - 1 >= 0) img1Write[id - 1] = true; + if (y + 1 < HEIGHT) img1Write[id + WIDTH] = true; + if (y - 1 >= 0) img1Write[id - WIDTH] = true; +#if IS_3D //if working with 3d volumes, then the 3D part + if (z + 1 < DEPTH) img1Write[id + WIDTH * HEIGHT] = true; + if (z - 1 >= 0) img1Write[id - WIDTH * HEIGHT] = true; +#endif + +#if CHEBYSHEV + //diagonals + if (x + 1 < WIDTH && y - 1 >= 0) img1Write[id - WIDTH + 1] = true; + if (x - 1 >= 0 && y - 1 >= 0) img1Write[id - WIDTH - 1] = true; + if (x + 1 < WIDTH && y + 1 < HEIGHT) img1Write[id + WIDTH + 1] = true; + if (x - 1 >= 0 && y + 1 < HEIGHT) img1Write[id + WIDTH - 1] = true; +#if IS_3D //if working with 3d volumes, then the 3D part + if (z + 1 < DEPTH && x + 1 < WIDTH && y - 1 >= 0) img1Write[id - WIDTH + 1 + WIDTH * HEIGHT] = true; + if (z + 1 < DEPTH && x - 1 >= 0 && y - 1 >= 0) img1Write[id - WIDTH - 1 + WIDTH * HEIGHT] = true; + if (z + 1 < DEPTH && x + 1 < WIDTH && y + 1 < HEIGHT) img1Write[id + WIDTH + 1 + WIDTH * HEIGHT] = true; + if (z + 1 < DEPTH && x - 1 >= 0 && y + 1 < HEIGHT) img1Write[id + WIDTH - 1 + WIDTH * HEIGHT] = true; + if (z - 1 >= 0 && x + 1 < WIDTH && y - 1 >= 0) img1Write[id - WIDTH + 1 - WIDTH * HEIGHT] = true; + if (z - 1 >= 0 && x - 1 >= 0 && y - 1 >= 0) img1Write[id - WIDTH - 1 - WIDTH * HEIGHT] = true; + if (z - 1 >= 0 && x + 1 < WIDTH && y + 1 < HEIGHT) img1Write[id + WIDTH + 1 - WIDTH * HEIGHT] = true; + if (z - 1 >= 0 && x - 1 >= 0 && y + 1 < HEIGHT) img1Write[id + WIDTH - 1 - WIDTH * HEIGHT] = true; +#endif +#endif + } + + + if (img2Read[id]) { + if (x + 1 < WIDTH) img2Write[id + 1] = true; + if (x - 1 >= 0) img2Write[id - 1] = true; + if (y + 1 < HEIGHT) img2Write[id + WIDTH] = true; + if (y - 1 >= 0) img2Write[id - WIDTH] = true; +#if IS_3D //if working with 3d volumes, then the 3D part + if (z + 1 < DEPTH) img2Write[id + WIDTH * HEIGHT] = true; + if (z - 1 >= 0) img2Write[id - WIDTH * HEIGHT] = true; +#endif + +#if CHEBYSHEV + //diagonals + if (x + 1 < WIDTH && y - 1 >= 0) img2Write[id - WIDTH + 1] = true; + if (x - 1 >= 0 && y - 1 >= 0) img2Write[id - WIDTH - 1] = true; + if (x + 1 < WIDTH && y + 1 < HEIGHT) img2Write[id + WIDTH + 1] = true; + if (x - 1 >= 0 && y + 1 < HEIGHT) img2Write[id + WIDTH - 1] = true; +#if IS_3D //if working with 3d volumes, then the 3D part + if (z + 1 < DEPTH && x + 1 < WIDTH && y - 1 >= 0) img2Write[id - WIDTH + 1 + WIDTH * HEIGHT] = true; + if (z + 1 < DEPTH && x - 1 >= 0 && y - 1 >= 0) img2Write[id - WIDTH - 1 + WIDTH * HEIGHT] = true; + if (z + 1 < DEPTH && x + 1 < WIDTH && y + 1 < HEIGHT) img2Write[id + WIDTH + 1 + WIDTH * HEIGHT] = true; + if (z + 1 < DEPTH && x - 1 >= 0 && y + 1 < HEIGHT) img2Write[id + WIDTH - 1 + WIDTH * HEIGHT] = true; + if (z - 1 >= 0 && x + 1 < WIDTH && y - 1 >= 0) img2Write[id - WIDTH + 1 - WIDTH * HEIGHT] = true; + if (z - 1 >= 0 && x - 1 >= 0 && y - 1 >= 0) img2Write[id - WIDTH - 1 - WIDTH * HEIGHT] = true; + if (z - 1 >= 0 && x + 1 < WIDTH && y + 1 < HEIGHT) img2Write[id + WIDTH + 1 - WIDTH * HEIGHT] = true; + if (z - 1 >= 0 && x - 1 >= 0 && y + 1 < HEIGHT) img2Write[id + WIDTH - 1 - WIDTH * HEIGHT] = true; +#endif +#endif + } + + + //this is an atomic and computed to the finished global variable, if image 1 contains all of image 2 and image 2 contains all pixels of + //image 1 then finished is true + atomicAnd(&finished, (img2Read[id] || !IMG1[id]) && (img1Read[id] || !IMG2[id])); + } +} + +#pragma once +int HausdorffDistance::computeDistance(Volume* img1, Volume* img2, bool* d_img1, bool* d_img2) { + + const int height = (*img1).getHeight(), width = (*img1).getWidth(), depth = (*img1).getDepth(); + + size_t size = width * height * depth * sizeof(bool); + + //getting details of your CUDA device + cudaDeviceProp props; + cudaGetDeviceProperties(&props, CUDA_DEVICE_INDEX); //device index = 0, you can change it if you have more CUDA devices + const int threadsPerBlock = props.maxThreadsPerBlock / 2; + const int blocksPerGrid = (height * width * depth + threadsPerBlock - 1) / threadsPerBlock; + + + //copying the dimensions to the GPU + cudaMemcpyToSymbolAsync(WIDTH, &width, sizeof(width), 0); + cudaMemcpyToSymbolAsync(HEIGHT, &height, sizeof(height), 0); + cudaMemcpyToSymbolAsync(DEPTH, &depth, sizeof(depth), 0); + + + //allocating the input images on the GPU + + + + //allocating the images that will be the processing ones + bool* d_img1Write, * d_img1Read, * d_img2Write, * d_img2Read; + cudaMalloc(&d_img1Write, size); cudaMalloc(&d_img1Read, size); + cudaMalloc(&d_img2Write, size); cudaMalloc(&d_img2Read, size); + + + //cloning the input images to these two image versions (write and read) + cudaMemcpyAsync(d_img1Read, d_img1, size, cudaMemcpyDeviceToDevice); + cudaMemcpyAsync(d_img2Read, d_img2, size, cudaMemcpyDeviceToDevice); + cudaMemcpyAsync(d_img1Write, d_img1, size, cudaMemcpyDeviceToDevice); + cudaMemcpyAsync(d_img2Write, d_img2, size, cudaMemcpyDeviceToDevice); + + + + //required variables to compute the distance + int h_finished = false, t = true; + int distance = -1; + + //where the magic happens + while (!h_finished) { + //reset the bool variable that verifies if the processing ended + cudaMemcpyToSymbol(finished, &t, sizeof(h_finished)); + + + //lauching the verify kernel, which verifies if the processing finished + dilate << < blocksPerGrid, threadsPerBlock >> > (d_img1, d_img2, d_img1Read, d_img2Read, d_img1Write, d_img2Write); + + //cudaDeviceSynchronize(); + + //updating the imgRead (cloning imgWrite to imgRead) + cudaMemcpy(d_img1Read, d_img1Write, size, cudaMemcpyDeviceToDevice); + cudaMemcpy(d_img2Read, d_img2Write, size, cudaMemcpyDeviceToDevice); + + //copying the result back to host memory + cudaMemcpyFromSymbol(&h_finished, finished, sizeof(h_finished)); + + + //incrementing the distance at each iteration + distance++; + } + + + //freeing memory + cudaFree(d_img1); cudaFree(d_img2); + cudaFree(d_img1Write); cudaFree(d_img1Read); + cudaFree(d_img2Write); cudaFree(d_img2Read); + + //resetting device + // cudaDeviceReset(); + + //print(cudaGetLastError(), "processing CUDA. Something may be wrong with your CUDA device."); + + return distance; + +} +#pragma once +inline void HausdorffDistance::print(cudaError_t error, char* msg) { + if (error != cudaSuccess) + { + printf("Error on %s ", msg); + fprintf(stderr, "Error code: %s!\n", cudaGetErrorString(error)); + exit(EXIT_FAILURE); + } +} + + + +/* +benchmark for original code from https://github.com/Oyatsumi/HausdorffDistanceComparison +*/ +std::tuple benchmarkOlivieraCUDA(torch::Tensor goldStandardA, + torch::Tensor algoOutputA, int WIDTH, int HEIGHT + , int DEPTH) { + + //just originally it started for cpu so ... + + int lenn = WIDTH * HEIGHT * DEPTH; + size_t sizee = sizeof(bool) * lenn; + + + + bool* goldStandard = (bool*)calloc(lenn, sizeof(bool)); + bool* algoOutput = (bool*)calloc(lenn, sizeof(bool)); + + cudaMemcpy(goldStandard, goldStandardA.data_ptr(), sizee, cudaMemcpyDeviceToHost); + cudaMemcpy(algoOutput, algoOutputA.data_ptr(), sizee, cudaMemcpyDeviceToHost); + + + //auto goldStandardA.data_ptr() + + + //bool* goldStandard = (bool*)goldStandardA.to(torch::kCPU).data_ptr(); + //bool* algoOutput = (bool*)algoOutputA.to(torch::kCPU).data_ptr(); + + + + Volume img1 = Volume(WIDTH, HEIGHT, DEPTH), img2 = Volume(WIDTH, HEIGHT, DEPTH); + + for (int x = 0; x < WIDTH; x++) { + for (int y = 0; y < HEIGHT; y++) { + for (int z = 0; z < DEPTH; z++) { + img1.setVoxelValue(algoOutput[x + y * WIDTH + z * WIDTH * HEIGHT], x, y, z); + img2.setVoxelValue(goldStandard[x + y * WIDTH + z * WIDTH * HEIGHT], x, y, z); + } + } + } + + size_t size = WIDTH * HEIGHT * DEPTH * sizeof(bool); + + + bool* d_img1, * d_img2; + cudaMalloc(&d_img1, size); + cudaMalloc(&d_img2, size); + + + //copying the data to the allocated memory on the GPU + cudaMemcpyAsync(d_img1, (img1).getVolume(), size, cudaMemcpyHostToDevice); + cudaMemcpyAsync(d_img2, (img2).getVolume(), size, cudaMemcpyHostToDevice); + + + auto begin = std::chrono::high_resolution_clock::now(); + HausdorffDistance* hd = new HausdorffDistance(); + + cudaDeviceSynchronize(); + + int dist = (*hd).computeDistance(&img1, &img2, d_img1, d_img2); + cudaDeviceSynchronize(); + + + auto end = std::chrono::high_resolution_clock::now(); + + + + // std::cout << "Total elapsed time: "; + double time = (double)(std::chrono::duration_cast(end - begin).count() / (double)1000000000); + //std::cout << time << "s" << std::endl; + + // printf("HD: %d \n", dist); + + + + //freeing memory + img1.dispose(); img2.dispose(); + free(goldStandard); + free(algoOutput); + //Datasize: 216530944 + //Datasize : 216530944 + //Total elapsed time : 2.62191s + //HD : 234 + return { dist, time }; +} From 7fb1821c8c2aa82d8aaa21bfbd1a69237b0ea960 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 6 Apr 2022 10:15:50 +0000 Subject: [PATCH 06/41] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- monai/_extensions/hausdorff/hausdorff_cuda.cu | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monai/_extensions/hausdorff/hausdorff_cuda.cu b/monai/_extensions/hausdorff/hausdorff_cuda.cu index b8199ebf893..29256a7ec34 100644 --- a/monai/_extensions/hausdorff/hausdorff_cuda.cu +++ b/monai/_extensions/hausdorff/hausdorff_cuda.cu @@ -2269,7 +2269,7 @@ public: #define CUDA_DEVICE_INDEX 0 //setting the index of your CUDA device #define IS_3D 1 //setting this to 0 would grant a very slightly improvement on the performance if working with images only -#define CHEBYSHEV 0 //if not set to 1, then this algorithm would use an Euclidean-like metric, it is just an approximation. +#define CHEBYSHEV 0 //if not set to 1, then this algorithm would use an Euclidean-like metric, it is just an approximation. //It can be changed according to the structuring element #pragma once class HausdorffDistance { From 4aea64ca5d437c609b439c48849f8bf31eae16f2 Mon Sep 17 00:00:00 2001 From: Jakub Mitura <53857487+jakubMitura14@users.noreply.github.com> Date: Wed, 6 Apr 2022 14:49:09 +0200 Subject: [PATCH 07/41] single stream, signed commit Signed-off-by: Jakub Mitura <53857487+jakubMitura14@users.noreply.github.com> --- monai/_extensions/hausdorff/hausdorff_cuda.cu | 385 +----------------- 1 file changed, 16 insertions(+), 369 deletions(-) diff --git a/monai/_extensions/hausdorff/hausdorff_cuda.cu b/monai/_extensions/hausdorff/hausdorff_cuda.cu index b8199ebf893..413f62a32cb 100644 --- a/monai/_extensions/hausdorff/hausdorff_cuda.cu +++ b/monai/_extensions/hausdorff/hausdorff_cuda.cu @@ -73,7 +73,7 @@ extern "C" struct MetaDataCPU { //7)global FP count; 8)global FN count 9) workQueueCounter 10)resultFP globalCounter 11) resultFn globalCounter //12) global FPandFn offset 13)globalIterationNumb //array3dWithDimsCPU minMaxes; - unsigned int* minMaxes; + unsigned int minMaxes[20]; uint32_t* resultList; }; @@ -889,7 +889,7 @@ __global__ void firstMetaPrepareKernel(ForBoolKernelArgs fbArgs /*************************************** - * memoery allocations + * memory allocations * ********************************/ @@ -2040,7 +2040,7 @@ ForBoolKernelArgs executeHausdoff(ForFullBoolPrepArgs& fFArgs, const int W ForBoolKernelArgs fbArgs = getArgsForKernel(fFArgs, occData.warpsNumbForMainPass, occData.blockForMainPass, WIDTH, HEIGHT, DEPTH, stream); - getMinMaxes << > > (fbArgs, fbArgs.minMaxes + getMinMaxes << > > (fbArgs, fbArgs.minMaxes , goldArrPointer , segmArrPointer , fbArgs.metaData); @@ -2049,7 +2049,7 @@ ForBoolKernelArgs executeHausdoff(ForFullBoolPrepArgs& fFArgs, const int W fbArgs.metaData = allocateMemoryAfterMinMaxesKernel(fbArgs, fFArgs, stream); fbArgs.robustnessPercent = robustnessPercent; - boolPrepareKernel << > > ( + boolPrepareKernel << > > ( fbArgs, fbArgs.metaData, fbArgs.origArrsPointer, fbArgs.metaDataArrPointer , goldArrPointer , segmArrPointer @@ -2060,12 +2060,12 @@ ForBoolKernelArgs executeHausdoff(ForFullBoolPrepArgs& fFArgs, const int W - firstMetaPrepareKernel << > > (fbArgs, fbArgs.metaData, fbArgs.minMaxes, fbArgs.workQueuePointer, fbArgs.origArrsPointer, fbArgs.metaDataArrPointer); + firstMetaPrepareKernel << > > (fbArgs, fbArgs.metaData, fbArgs.minMaxes, fbArgs.workQueuePointer, fbArgs.origArrsPointer, fbArgs.metaDataArrPointer); void* kernel_args[] = { &fbArgs }; - cudaLaunchCooperativeKernel((void*)(mainPassKernel), occData.blockForMainPass, dim3(32, occData.warpsNumbForMainPass), kernel_args); + cudaLaunchCooperativeKernel((void*)(mainPassKernel), occData.blockForMainPass, dim3(32, occData.warpsNumbForMainPass), kernel_args, 0,stream); //copy to the output tensor the rsult @@ -2125,9 +2125,9 @@ int getHausdorffDistance_CUDA_Generic(at::Tensor goldStandard, cudaStreamCreate(&stream1); MetaDataCPU metaData; - size_t size = sizeof(unsigned int) * 20; - unsigned int* minMaxesCPU = (unsigned int*)malloc(size); - metaData.minMaxes = minMaxesCPU; + //size_t size = sizeof(unsigned int) * 20; + //unsigned int* minMaxesCPU = (unsigned int*)malloc(size); + //metaData.minMaxes = minMaxesCPU; ForFullBoolPrepArgs forFullBoolPrepArgs; forFullBoolPrepArgs.metaData = metaData; @@ -2142,12 +2142,12 @@ int getHausdorffDistance_CUDA_Generic(at::Tensor goldStandard, size_t sizeMinMax = sizeof(unsigned int) * 20; //making sure we have all resultsto copy on cpu cudaDeviceSynchronize(); - cudaMemcpy(minMaxesCPU, fbArgs.metaData.minMaxes, sizeMinMax, cudaMemcpyDeviceToHost); + cudaMemcpy(metaData.minMaxes, fbArgs.metaData.minMaxes, sizeMinMax, cudaMemcpyDeviceToHost); - int result = minMaxesCPU[13]; + int result = metaData.minMaxes[13]; cudaFreeAsync(fbArgs.minMaxes, stream1); - free(minMaxesCPU); + //free(minMaxesCPU); cudaStreamDestroy(stream1); @@ -2166,9 +2166,9 @@ at::Tensor getHausdorffDistance_CUDA_FullResList_local(at::Tensor goldStandard, cudaStreamCreate(&stream1); MetaDataCPU metaData; - size_t size = sizeof(unsigned int) * 20; - unsigned int* minMaxesCPU = (unsigned int*)malloc(size); - metaData.minMaxes = minMaxesCPU; + //size_t size = sizeof(unsigned int) * 20; + //unsigned int* minMaxesCPU = (unsigned int*)malloc(size); + //metaData.minMaxes = minMaxesCPU; ForFullBoolPrepArgs forFullBoolPrepArgs; forFullBoolPrepArgs.metaData = metaData; @@ -2184,7 +2184,7 @@ at::Tensor getHausdorffDistance_CUDA_FullResList_local(at::Tensor goldStandard, cudaFreeAsync(fbArgs.minMaxes, stream1); - free(minMaxesCPU); + //free(metaData.minMaxesCPU); @@ -2232,356 +2232,3 @@ at::Tensor getHausdorffDistance_CUDA_FullResList(at::Tensor goldStandard, - - -/************************************************************* -*Oliviera Algorithm -*************************************************************************************/ - - -typedef unsigned char uchar; -typedef unsigned int uint; -#pragma once -class Volume { - -private: - bool* volume; - int width, height, depth; - int getLinearIndex(int x, int y, int z); -public: - bool getVoxelValue(int x, int y, int z); - bool getPixelValue(int x, int y); - uint getWidth(); - uint getHeight(); - uint getDepth(); - bool* getVolume(); - void setVoxelValue(bool value, int x, int y, int z); - void setPixelValue(bool value, int x, int y); - Volume(int width, int height, int depth); - Volume(int width, int height); - void dispose(); - -}; - - - - -#define CUDA_DEVICE_INDEX 0 //setting the index of your CUDA device - -#define IS_3D 1 //setting this to 0 would grant a very slightly improvement on the performance if working with images only -#define CHEBYSHEV 0 //if not set to 1, then this algorithm would use an Euclidean-like metric, it is just an approximation. -//It can be changed according to the structuring element -#pragma once -class HausdorffDistance { - -private: - void print(cudaError_t error, char* msg); - -public: - int computeDistance(Volume* img1, Volume* img2, bool* d_img1, bool* d_img2); - -}; - - -inline Volume::Volume(const int width, const int height, const int depth) { - this->width = width; this->height = height; this->depth = depth; - volume = (bool*)calloc(width * height * depth, sizeof(bool)); -} - -#pragma once -inline Volume::Volume(const int width, const int height) { - this->width = width; this->height = height; this->depth = 1; - volume = (bool*)calloc(width * height * depth, sizeof(bool)); -} -#pragma once -inline int Volume::getLinearIndex(const int x, const int y, const int z) { - const int a = 1, b = width, c = (width) * (height); - return a * x + b * y + c * z; -} - -inline uint Volume::getWidth() { return this->width; } -inline uint Volume::getHeight() { return this->height; } -inline uint Volume::getDepth() { return this->depth; } -inline bool* Volume::getVolume() { return this->volume; } -inline bool Volume::getPixelValue(int x, int y) { return this->volume[getLinearIndex(x, y, 0)]; } -#pragma once -inline bool Volume::getVoxelValue(int x, int y, int z) { - return volume[getLinearIndex(x, y, z)]; -} -#pragma once -inline void Volume::setPixelValue(bool value, const int x, const int y) { - volume[getLinearIndex(x, y, 0)] = value; -} -#pragma once -inline void Volume::setVoxelValue(bool value, const int x, const int y, const int z) { - volume[getLinearIndex(x, y, z)] = value; -} -#pragma once -inline void Volume::dispose() { - free(volume); -} - -typedef unsigned char uchar; -typedef unsigned int uint; - -#pragma once -__device__ int finished; //global variable that contains a boolean which indicates when to stop the kernel processing -#pragma once -__constant__ __device__ int WIDTH, HEIGHT, DEPTH; //constant variables that contain the size of the volume - - -#pragma once -__global__ void dilate(const bool* IMG1, const bool* IMG2, const bool* img1Read, const bool* img2Read, - bool* img1Write, bool* img2Write) { - - const int id = blockDim.x * blockIdx.x + threadIdx.x; -#if !IS_3D - const int x = id % WIDTH, y = id / WIDTH; -#else - const int x = id % WIDTH, y = (id / WIDTH) % HEIGHT, z = (id / WIDTH) / HEIGHT; -#endif - - if (id < WIDTH * HEIGHT * DEPTH) { - - - if (img1Read[id]) { - if (x + 1 < WIDTH) img1Write[id + 1] = true; - if (x - 1 >= 0) img1Write[id - 1] = true; - if (y + 1 < HEIGHT) img1Write[id + WIDTH] = true; - if (y - 1 >= 0) img1Write[id - WIDTH] = true; -#if IS_3D //if working with 3d volumes, then the 3D part - if (z + 1 < DEPTH) img1Write[id + WIDTH * HEIGHT] = true; - if (z - 1 >= 0) img1Write[id - WIDTH * HEIGHT] = true; -#endif - -#if CHEBYSHEV - //diagonals - if (x + 1 < WIDTH && y - 1 >= 0) img1Write[id - WIDTH + 1] = true; - if (x - 1 >= 0 && y - 1 >= 0) img1Write[id - WIDTH - 1] = true; - if (x + 1 < WIDTH && y + 1 < HEIGHT) img1Write[id + WIDTH + 1] = true; - if (x - 1 >= 0 && y + 1 < HEIGHT) img1Write[id + WIDTH - 1] = true; -#if IS_3D //if working with 3d volumes, then the 3D part - if (z + 1 < DEPTH && x + 1 < WIDTH && y - 1 >= 0) img1Write[id - WIDTH + 1 + WIDTH * HEIGHT] = true; - if (z + 1 < DEPTH && x - 1 >= 0 && y - 1 >= 0) img1Write[id - WIDTH - 1 + WIDTH * HEIGHT] = true; - if (z + 1 < DEPTH && x + 1 < WIDTH && y + 1 < HEIGHT) img1Write[id + WIDTH + 1 + WIDTH * HEIGHT] = true; - if (z + 1 < DEPTH && x - 1 >= 0 && y + 1 < HEIGHT) img1Write[id + WIDTH - 1 + WIDTH * HEIGHT] = true; - if (z - 1 >= 0 && x + 1 < WIDTH && y - 1 >= 0) img1Write[id - WIDTH + 1 - WIDTH * HEIGHT] = true; - if (z - 1 >= 0 && x - 1 >= 0 && y - 1 >= 0) img1Write[id - WIDTH - 1 - WIDTH * HEIGHT] = true; - if (z - 1 >= 0 && x + 1 < WIDTH && y + 1 < HEIGHT) img1Write[id + WIDTH + 1 - WIDTH * HEIGHT] = true; - if (z - 1 >= 0 && x - 1 >= 0 && y + 1 < HEIGHT) img1Write[id + WIDTH - 1 - WIDTH * HEIGHT] = true; -#endif -#endif - } - - - if (img2Read[id]) { - if (x + 1 < WIDTH) img2Write[id + 1] = true; - if (x - 1 >= 0) img2Write[id - 1] = true; - if (y + 1 < HEIGHT) img2Write[id + WIDTH] = true; - if (y - 1 >= 0) img2Write[id - WIDTH] = true; -#if IS_3D //if working with 3d volumes, then the 3D part - if (z + 1 < DEPTH) img2Write[id + WIDTH * HEIGHT] = true; - if (z - 1 >= 0) img2Write[id - WIDTH * HEIGHT] = true; -#endif - -#if CHEBYSHEV - //diagonals - if (x + 1 < WIDTH && y - 1 >= 0) img2Write[id - WIDTH + 1] = true; - if (x - 1 >= 0 && y - 1 >= 0) img2Write[id - WIDTH - 1] = true; - if (x + 1 < WIDTH && y + 1 < HEIGHT) img2Write[id + WIDTH + 1] = true; - if (x - 1 >= 0 && y + 1 < HEIGHT) img2Write[id + WIDTH - 1] = true; -#if IS_3D //if working with 3d volumes, then the 3D part - if (z + 1 < DEPTH && x + 1 < WIDTH && y - 1 >= 0) img2Write[id - WIDTH + 1 + WIDTH * HEIGHT] = true; - if (z + 1 < DEPTH && x - 1 >= 0 && y - 1 >= 0) img2Write[id - WIDTH - 1 + WIDTH * HEIGHT] = true; - if (z + 1 < DEPTH && x + 1 < WIDTH && y + 1 < HEIGHT) img2Write[id + WIDTH + 1 + WIDTH * HEIGHT] = true; - if (z + 1 < DEPTH && x - 1 >= 0 && y + 1 < HEIGHT) img2Write[id + WIDTH - 1 + WIDTH * HEIGHT] = true; - if (z - 1 >= 0 && x + 1 < WIDTH && y - 1 >= 0) img2Write[id - WIDTH + 1 - WIDTH * HEIGHT] = true; - if (z - 1 >= 0 && x - 1 >= 0 && y - 1 >= 0) img2Write[id - WIDTH - 1 - WIDTH * HEIGHT] = true; - if (z - 1 >= 0 && x + 1 < WIDTH && y + 1 < HEIGHT) img2Write[id + WIDTH + 1 - WIDTH * HEIGHT] = true; - if (z - 1 >= 0 && x - 1 >= 0 && y + 1 < HEIGHT) img2Write[id + WIDTH - 1 - WIDTH * HEIGHT] = true; -#endif -#endif - } - - - //this is an atomic and computed to the finished global variable, if image 1 contains all of image 2 and image 2 contains all pixels of - //image 1 then finished is true - atomicAnd(&finished, (img2Read[id] || !IMG1[id]) && (img1Read[id] || !IMG2[id])); - } -} - -#pragma once -int HausdorffDistance::computeDistance(Volume* img1, Volume* img2, bool* d_img1, bool* d_img2) { - - const int height = (*img1).getHeight(), width = (*img1).getWidth(), depth = (*img1).getDepth(); - - size_t size = width * height * depth * sizeof(bool); - - //getting details of your CUDA device - cudaDeviceProp props; - cudaGetDeviceProperties(&props, CUDA_DEVICE_INDEX); //device index = 0, you can change it if you have more CUDA devices - const int threadsPerBlock = props.maxThreadsPerBlock / 2; - const int blocksPerGrid = (height * width * depth + threadsPerBlock - 1) / threadsPerBlock; - - - //copying the dimensions to the GPU - cudaMemcpyToSymbolAsync(WIDTH, &width, sizeof(width), 0); - cudaMemcpyToSymbolAsync(HEIGHT, &height, sizeof(height), 0); - cudaMemcpyToSymbolAsync(DEPTH, &depth, sizeof(depth), 0); - - - //allocating the input images on the GPU - - - - //allocating the images that will be the processing ones - bool* d_img1Write, * d_img1Read, * d_img2Write, * d_img2Read; - cudaMalloc(&d_img1Write, size); cudaMalloc(&d_img1Read, size); - cudaMalloc(&d_img2Write, size); cudaMalloc(&d_img2Read, size); - - - //cloning the input images to these two image versions (write and read) - cudaMemcpyAsync(d_img1Read, d_img1, size, cudaMemcpyDeviceToDevice); - cudaMemcpyAsync(d_img2Read, d_img2, size, cudaMemcpyDeviceToDevice); - cudaMemcpyAsync(d_img1Write, d_img1, size, cudaMemcpyDeviceToDevice); - cudaMemcpyAsync(d_img2Write, d_img2, size, cudaMemcpyDeviceToDevice); - - - - //required variables to compute the distance - int h_finished = false, t = true; - int distance = -1; - - //where the magic happens - while (!h_finished) { - //reset the bool variable that verifies if the processing ended - cudaMemcpyToSymbol(finished, &t, sizeof(h_finished)); - - - //lauching the verify kernel, which verifies if the processing finished - dilate << < blocksPerGrid, threadsPerBlock >> > (d_img1, d_img2, d_img1Read, d_img2Read, d_img1Write, d_img2Write); - - //cudaDeviceSynchronize(); - - //updating the imgRead (cloning imgWrite to imgRead) - cudaMemcpy(d_img1Read, d_img1Write, size, cudaMemcpyDeviceToDevice); - cudaMemcpy(d_img2Read, d_img2Write, size, cudaMemcpyDeviceToDevice); - - //copying the result back to host memory - cudaMemcpyFromSymbol(&h_finished, finished, sizeof(h_finished)); - - - //incrementing the distance at each iteration - distance++; - } - - - //freeing memory - cudaFree(d_img1); cudaFree(d_img2); - cudaFree(d_img1Write); cudaFree(d_img1Read); - cudaFree(d_img2Write); cudaFree(d_img2Read); - - //resetting device - // cudaDeviceReset(); - - //print(cudaGetLastError(), "processing CUDA. Something may be wrong with your CUDA device."); - - return distance; - -} -#pragma once -inline void HausdorffDistance::print(cudaError_t error, char* msg) { - if (error != cudaSuccess) - { - printf("Error on %s ", msg); - fprintf(stderr, "Error code: %s!\n", cudaGetErrorString(error)); - exit(EXIT_FAILURE); - } -} - - - -/* -benchmark for original code from https://github.com/Oyatsumi/HausdorffDistanceComparison -*/ -std::tuple benchmarkOlivieraCUDA(torch::Tensor goldStandardA, - torch::Tensor algoOutputA, int WIDTH, int HEIGHT - , int DEPTH) { - - //just originally it started for cpu so ... - - int lenn = WIDTH * HEIGHT * DEPTH; - size_t sizee = sizeof(bool) * lenn; - - - - bool* goldStandard = (bool*)calloc(lenn, sizeof(bool)); - bool* algoOutput = (bool*)calloc(lenn, sizeof(bool)); - - cudaMemcpy(goldStandard, goldStandardA.data_ptr(), sizee, cudaMemcpyDeviceToHost); - cudaMemcpy(algoOutput, algoOutputA.data_ptr(), sizee, cudaMemcpyDeviceToHost); - - - //auto goldStandardA.data_ptr() - - - //bool* goldStandard = (bool*)goldStandardA.to(torch::kCPU).data_ptr(); - //bool* algoOutput = (bool*)algoOutputA.to(torch::kCPU).data_ptr(); - - - - Volume img1 = Volume(WIDTH, HEIGHT, DEPTH), img2 = Volume(WIDTH, HEIGHT, DEPTH); - - for (int x = 0; x < WIDTH; x++) { - for (int y = 0; y < HEIGHT; y++) { - for (int z = 0; z < DEPTH; z++) { - img1.setVoxelValue(algoOutput[x + y * WIDTH + z * WIDTH * HEIGHT], x, y, z); - img2.setVoxelValue(goldStandard[x + y * WIDTH + z * WIDTH * HEIGHT], x, y, z); - } - } - } - - size_t size = WIDTH * HEIGHT * DEPTH * sizeof(bool); - - - bool* d_img1, * d_img2; - cudaMalloc(&d_img1, size); - cudaMalloc(&d_img2, size); - - - //copying the data to the allocated memory on the GPU - cudaMemcpyAsync(d_img1, (img1).getVolume(), size, cudaMemcpyHostToDevice); - cudaMemcpyAsync(d_img2, (img2).getVolume(), size, cudaMemcpyHostToDevice); - - - auto begin = std::chrono::high_resolution_clock::now(); - HausdorffDistance* hd = new HausdorffDistance(); - - cudaDeviceSynchronize(); - - int dist = (*hd).computeDistance(&img1, &img2, d_img1, d_img2); - cudaDeviceSynchronize(); - - - auto end = std::chrono::high_resolution_clock::now(); - - - - // std::cout << "Total elapsed time: "; - double time = (double)(std::chrono::duration_cast(end - begin).count() / (double)1000000000); - //std::cout << time << "s" << std::endl; - - // printf("HD: %d \n", dist); - - - - //freeing memory - img1.dispose(); img2.dispose(); - free(goldStandard); - free(algoOutput); - //Datasize: 216530944 - //Datasize : 216530944 - //Total elapsed time : 2.62191s - //HD : 234 - return { dist, time }; -} From 5d3f6681e1fc093cafd4a6dbcfbd52933f4d7ee4 Mon Sep 17 00:00:00 2001 From: Jakub Mitura <53857487+jakubMitura14@users.noreply.github.com> Date: Wed, 6 Apr 2022 14:51:11 +0200 Subject: [PATCH 08/41] added indantation Signed-off-by: Jakub Mitura <53857487+jakubMitura14@users.noreply.github.com> --- .../morphological_hausdorff_distance.py | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/monai/metrics/morphological_hausdorff_distance.py b/monai/metrics/morphological_hausdorff_distance.py index ecf0e1d189b..871195f9e76 100644 --- a/monai/metrics/morphological_hausdorff_distance.py +++ b/monai/metrics/morphological_hausdorff_distance.py @@ -58,28 +58,28 @@ def __init__( ) -def compute_hausdorff_distance( - self, - y_pred: torch.Tensor, - y: torch.Tensor, - numberToLookFor: torch.Tensor): - """ - Compute the Hausdorff distance. + def compute_hausdorff_distance( + self, + y_pred: torch.Tensor, + y: torch.Tensor, + numberToLookFor: torch.Tensor): + """ + Compute the Hausdorff distance. - Args: - y_pred: input data to compute, It must be 3 dimensional - y: ground truth to compute mean the distance. It must be 3 dimensional, Dimensionality needs to be identical as in y_pred + Args: + y_pred: input data to compute, It must be 3 dimensional + y: ground truth to compute mean the distance. It must be 3 dimensional, Dimensionality needs to be identical as in y_pred - percent: an optional float number between 0 and 1. If specified, the corresponding - percent of the Hausdorff Distance rather than the maximum result will be achieved. - Defaults to 1.0 . - numberToLookFor: 0 dimensional tensor marking what value are we intrested in the supplied y and y_pred, defined becouse in case of multiorgan segmentations frequently we can have information about multiple organs in the same mask just marked by diffrent numbers - for example in case of boolean array we can suupply it like torch.ones(1, dtype =bool) + percent: an optional float number between 0 and 1. If specified, the corresponding + percent of the Hausdorff Distance rather than the maximum result will be achieved. + Defaults to 1.0 . + numberToLookFor: 0 dimensional tensor marking what value are we intrested in the supplied y and y_pred, defined becouse in case of multiorgan segmentations frequently we can have information about multiple organs in the same mask just marked by diffrent numbers + for example in case of boolean array we can suupply it like torch.ones(1, dtype =bool) - """ + """ - if y.shape != y_pred.shape: - raise ValueError(f"y_pred and y should have same shapes, got {y_pred.shape} and {y.shape}.") - sizz = y.shape + if y.shape != y_pred.shape: + raise ValueError(f"y_pred and y should have same shapes, got {y_pred.shape} and {y.shape}.") + sizz = y.shape - return self.compiled_extension.getHausdorffDistance(y_pred,y,sizz[0],sizz[1],sizz[2], self.percent, numberToLookFor ) + return self.compiled_extension.getHausdorffDistance(y_pred,y,sizz[0],sizz[1],sizz[2], self.percent, numberToLookFor ) From 87bcf33063be036b357009ee5b2c63fa6dc3105c Mon Sep 17 00:00:00 2001 From: Jakub Mitura <53857487+jakubMitura14@users.noreply.github.com> Date: Wed, 6 Apr 2022 15:02:55 +0200 Subject: [PATCH 09/41] updated imports Signed-off-by: Jakub Mitura <53857487+jakubMitura14@users.noreply.github.com> --- monai/metrics/__init__.py | 2 +- monai/metrics/morphological_hausdorff_distance.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/monai/metrics/__init__.py b/monai/metrics/__init__.py index 01cb9b29c75..a3869f54ce8 100644 --- a/monai/metrics/__init__.py +++ b/monai/metrics/__init__.py @@ -13,7 +13,7 @@ from .cumulative_average import CumulativeAverage from .froc import compute_fp_tp_probs, compute_froc_curve_data, compute_froc_score from .hausdorff_distance import HausdorffDistanceMetric, compute_hausdorff_distance, compute_percent_hausdorff_distance -from .morphological_hausdorff_distance import MorphologicalHausdorffDistanceMetric, compute_hausdorff_distance +from .morphological_hausdorff_distance import MorphologicalHausdorffDistanceMetric from .meandice import DiceMetric, compute_meandice from .metric import Cumulative, CumulativeIterationMetric, IterationMetric, Metric from .regression import MAEMetric, MSEMetric, PSNRMetric, RMSEMetric diff --git a/monai/metrics/morphological_hausdorff_distance.py b/monai/metrics/morphological_hausdorff_distance.py index 871195f9e76..8894fab57f5 100644 --- a/monai/metrics/morphological_hausdorff_distance.py +++ b/monai/metrics/morphological_hausdorff_distance.py @@ -17,7 +17,7 @@ from monai._extensions.loader import load_module from .metric import CumulativeIterationMetric -__all__ = ["MorphologicalHausdorffDistanceMetric", "compute_morphological_hausdorff_distance", "compute_morphological_percent_hausdorff_distance"] +__all__ = ["MorphologicalHausdorffDistanceMetric"] class MorphologicalHausdorffDistanceMetric(CumulativeIterationMetric): @@ -58,7 +58,7 @@ def __init__( ) - def compute_hausdorff_distance( + def compute_morphological_hausdorff_distance( self, y_pred: torch.Tensor, y: torch.Tensor, From 433c0755936429393056c8e3309c79463b6f08e2 Mon Sep 17 00:00:00 2001 From: monai-bot Date: Wed, 6 Apr 2022 14:45:18 +0000 Subject: [PATCH 10/41] [MONAI] python code formatting Signed-off-by: monai-bot --- monai/metrics/__init__.py | 2 +- .../morphological_hausdorff_distance.py | 52 +++++++++---------- tests/test_hausdorff_distance_morphologic.py | 20 ++++--- 3 files changed, 34 insertions(+), 40 deletions(-) diff --git a/monai/metrics/__init__.py b/monai/metrics/__init__.py index a3869f54ce8..df8dc65fbad 100644 --- a/monai/metrics/__init__.py +++ b/monai/metrics/__init__.py @@ -13,9 +13,9 @@ from .cumulative_average import CumulativeAverage from .froc import compute_fp_tp_probs, compute_froc_curve_data, compute_froc_score from .hausdorff_distance import HausdorffDistanceMetric, compute_hausdorff_distance, compute_percent_hausdorff_distance -from .morphological_hausdorff_distance import MorphologicalHausdorffDistanceMetric from .meandice import DiceMetric, compute_meandice from .metric import Cumulative, CumulativeIterationMetric, IterationMetric, Metric +from .morphological_hausdorff_distance import MorphologicalHausdorffDistanceMetric from .regression import MAEMetric, MSEMetric, PSNRMetric, RMSEMetric from .rocauc import ROCAUCMetric, compute_roc_auc from .surface_distance import SurfaceDistanceMetric, compute_average_surface_distance diff --git a/monai/metrics/morphological_hausdorff_distance.py b/monai/metrics/morphological_hausdorff_distance.py index 8894fab57f5..776d422bd0c 100644 --- a/monai/metrics/morphological_hausdorff_distance.py +++ b/monai/metrics/morphological_hausdorff_distance.py @@ -14,7 +14,9 @@ import numpy as np import torch + from monai._extensions.loader import load_module + from .metric import CumulativeIterationMetric __all__ = ["MorphologicalHausdorffDistanceMetric"] @@ -22,47 +24,39 @@ class MorphologicalHausdorffDistanceMetric(CumulativeIterationMetric): """ - Work is based onthe principle of application of mathemathical morphology more precisely dilatation, more details is presented in works [1] and [2]. Calculated is roughly related to the largest -thickness of the difference between the true and estimatedmasks, and constitutes Approximation of true Hausdorff distance. The correlation between true and morphological Hausdorf distance is around 0.9, less for robust version . -Hovewer this implementation is also around 100 times faster than exact MONAI Hausdorff distance calculation. One can measure 3 diffrent metrics based on Hausdorff ditance. + Work is based onthe principle of application of mathemathical morphology more precisely dilatation, more details is presented in works [1] and [2]. Calculated is roughly related to the largest + thickness of the difference between the true and estimatedmasks, and constitutes Approximation of true Hausdorff distance. The correlation between true and morphological Hausdorf distance is around 0.9, less for robust version . + Hovewer this implementation is also around 100 times faster than exact MONAI Hausdorff distance calculation. One can measure 3 diffrent metrics based on Hausdorff ditance. - 1)simple Hausdorff distance - it tell what is the distance between two most distant points one from y and other from y_pred + 1)simple Hausdorff distance - it tell what is the distance between two most distant points one from y and other from y_pred - 2)robust Hausdorff distance - modification of 1) where we stop analyzing points when we already analyzed given percent of total number of points - is less sensitive to outliers. It is also slightly faster + 2)robust Hausdorff distance - modification of 1) where we stop analyzing points when we already analyzed given percent of total number of points - is less sensitive to outliers. It is also slightly faster - 3)mean Hausdorff distance - can be understood as approximately mean distance between pair of points where one point is from y and other from y_pred. + 3)mean Hausdorff distance - can be understood as approximately mean distance between pair of points where one point is from y and other from y_pred. - 4)additionally one can request as a result one dimensional tensor that will contain data for each point what is HD value for it (like we would do Hausdorff distance calculation only for this point and all points from other mask) + 4)additionally one can request as a result one dimensional tensor that will contain data for each point what is HD value for it (like we would do Hausdorff distance calculation only for this point and all points from other mask) - Compute Hausdorff Distance between two tensors. In addition, specify the `percent` (for robust Hausdorff distance ) - parameter can get the percentile of the distance. Input `y_pred` is compared with ground truth `y`. + Compute Hausdorff Distance between two tensors. In addition, specify the `percent` (for robust Hausdorff distance ) + parameter can get the percentile of the distance. Input `y_pred` is compared with ground truth `y`. - Args: - percent: an optional float number between 0 and 1. If specified, the corresponding - percent of the Hausdorff Distance rather than the maximum result will be achieved. - Defaults to 1.0 . + Args: + percent: an optional float number between 0 and 1. If specified, the corresponding + percent of the Hausdorff Distance rather than the maximum result will be achieved. + Defaults to 1.0 . - 1) Érick Oliveira Rodrigues,An efficient and locality-oriented Hausdorff distance algorithm: Proposal and analysis of paradigms and implementations, Pattern Recognition,Volume 117,2021 - 2) Karimi, Davood & Salcudean, Septimiu. (2019). Reducing the Hausdorff Distance in Medical Image Segmentation With Convolutional Neural Networks. IEEE Transactions on Medical Imaging. PP. 1-1. 10.1109/TMI.2019.2930068. + 1) Érick Oliveira Rodrigues,An efficient and locality-oriented Hausdorff distance algorithm: Proposal and analysis of paradigms and implementations, Pattern Recognition,Volume 117,2021 + 2) Karimi, Davood & Salcudean, Septimiu. (2019). Reducing the Hausdorff Distance in Medical Image Segmentation With Convolutional Neural Networks. IEEE Transactions on Medical Imaging. PP. 1-1. 10.1109/TMI.2019.2930068. """ - def __init__( - self, - percent: float = 1.0, - ) -> None: + def __init__(self, percent: float = 1.0) -> None: super().__init__() self.percent = percent - self.compiled_extension = load_module( - "hausdorff_cpp" - ) - + self.compiled_extension = load_module("hausdorff_cpp") def compute_morphological_hausdorff_distance( - self, - y_pred: torch.Tensor, - y: torch.Tensor, - numberToLookFor: torch.Tensor): + self, y_pred: torch.Tensor, y: torch.Tensor, numberToLookFor: torch.Tensor + ): """ Compute the Hausdorff distance. @@ -82,4 +76,6 @@ def compute_morphological_hausdorff_distance( raise ValueError(f"y_pred and y should have same shapes, got {y_pred.shape} and {y.shape}.") sizz = y.shape - return self.compiled_extension.getHausdorffDistance(y_pred,y,sizz[0],sizz[1],sizz[2], self.percent, numberToLookFor ) + return self.compiled_extension.getHausdorffDistance( + y_pred, y, sizz[0], sizz[1], sizz[2], self.percent, numberToLookFor + ) diff --git a/tests/test_hausdorff_distance_morphologic.py b/tests/test_hausdorff_distance_morphologic.py index 5af06727257..02c341e08fa 100644 --- a/tests/test_hausdorff_distance_morphologic.py +++ b/tests/test_hausdorff_distance_morphologic.py @@ -11,31 +11,29 @@ import unittest +import numpy as np import torch from parameterized import parameterized -import numpy as np from monai.metrics import MorphologicalHausdorffDistanceMetric -a = torch.zeros(20,20,20) -b = torch.zeros(20,20,20) -a[0,0,0]=1 -b[0,0,10]=1 -numbToLookFor= torch.ones(1) +a = torch.zeros(20, 20, 20) +b = torch.zeros(20, 20, 20) +a[0, 0, 0] = 1 +b[0, 0, 10] = 1 +numbToLookFor = torch.ones(1) + +TEST_CASES = [[[a, b, 1.0, numbToLookFor], 10]] -TEST_CASES = [ - [[a,b,1.0,numbToLookFor], 10] -] class TestHausdorffDistanceMorphological(unittest.TestCase): @parameterized.expand(TEST_CASES) def test_value(self, input_data, expected_value): [y_pred, y, percentt, numberToLookFor] = input_data hd_metric = MorphologicalHausdorffDistanceMetric(percent=percentt) - result= hd_metric.compute_hausdorff_distance(y_pred,y,numberToLookFor ) + result = hd_metric.compute_hausdorff_distance(y_pred, y, numberToLookFor) np.testing.assert_allclose(expected_value, result, rtol=1e-7) - if __name__ == "__main__": unittest.main() From 378a874322b9dd2bf29ec47178d99529cefc57f0 Mon Sep 17 00:00:00 2001 From: Jakub Mitura <53857487+jakubMitura14@users.noreply.github.com> Date: Wed, 6 Apr 2022 18:56:45 +0200 Subject: [PATCH 11/41] numberToLookFor changed, comments smaller colums Signed-off-by: Jakub Mitura <53857487+jakubMitura14@users.noreply.github.com> --- monai/_extensions/hausdorff/hausdorff_cuda.cu | 30 ++++++++-------- .../morphological_hausdorff_distance.py | 34 +++++++++++++------ tests/test_hausdorff_distance_morphologic.py | 4 +-- 3 files changed, 40 insertions(+), 28 deletions(-) diff --git a/monai/_extensions/hausdorff/hausdorff_cuda.cu b/monai/_extensions/hausdorff/hausdorff_cuda.cu index 1450c0563e9..213d4bb286d 100644 --- a/monai/_extensions/hausdorff/hausdorff_cuda.cu +++ b/monai/_extensions/hausdorff/hausdorff_cuda.cu @@ -137,7 +137,7 @@ struct ForFullBoolPrepArgs { int Ny; int Nz; - TFF numberToLookFor;// what we will look for in arrays + TFF compare_values;// what we will look for in arrays //number and dimensionality of threads and blocks required to lounch bool kernel dim3 threads; int blocks; @@ -178,7 +178,7 @@ struct ForBoolKernelArgs { // gold standard and segmentation output array torch::Tensor goldArr; torch::Tensor segmArr; - TFB numberToLookFor; + TFB compare_values; int Nx; int Ny; int Nz; @@ -459,8 +459,8 @@ __global__ void getMinMaxes(ForBoolKernelArgs fbArgs //uint8_t& zLocRef = zLoc; uint8_t& yLocRef = yLoc; uint8_t& xLocRef = xLoc; // setting bits - bool goldBool = goldArr[x + y * fbArgs.Nx + z * fbArgs.Nx * fbArgs.Ny] == fbArgs.numberToLookFor; // (getTensorRow(tensorslice, fbArgs.goldArr, fbArgs.goldArr.Ny, y, z)[x] == fbArgs.numberToLookFor); - bool segmBool = segmArr[x + y * fbArgs.Nx + z * fbArgs.Nx * fbArgs.Ny] == fbArgs.numberToLookFor; + bool goldBool = goldArr[x + y * fbArgs.Nx + z * fbArgs.Nx * fbArgs.Ny] == fbArgs.compare_values; // (getTensorRow(tensorslice, fbArgs.goldArr, fbArgs.goldArr.Ny, y, z)[x] == fbArgs.compare_values); + bool segmBool = segmArr[x + y * fbArgs.Nx + z * fbArgs.Nx * fbArgs.Ny] == fbArgs.compare_values; if (goldBool || segmBool) { anyInGold[0] = true; } @@ -621,8 +621,8 @@ __global__ void boolPrepareKernel(ForBoolKernelArgs fbArgs //char* tensorslice; //first array gold - bool goldBool = goldArr[x + y * fbArgs.Nx + z * fbArgs.Nx * fbArgs.Ny] == fbArgs.numberToLookFor; - bool segmBool = segmArr[x + y * fbArgs.Nx + z * fbArgs.Nx * fbArgs.Ny] == fbArgs.numberToLookFor; + bool goldBool = goldArr[x + y * fbArgs.Nx + z * fbArgs.Nx * fbArgs.Ny] == fbArgs.compare_values; + bool segmBool = segmArr[x + y * fbArgs.Nx + z * fbArgs.Nx * fbArgs.Ny] == fbArgs.compare_values; //goldBool = true; // setting bits @@ -921,7 +921,7 @@ inline ForBoolKernelArgs getArgsForKernel(ForFullBoolPrepArgs& mainFun res.metaData = allocateMetaDataOnGPU(mainFunArgs.metaData, minMaxes); res.metaData.minMaxes = minMaxes; res.minMaxes = minMaxes; - res.numberToLookFor = mainFunArgs.numberToLookFor; + res.compare_values = mainFunArgs.compare_values; res.dbXLength = 32; res.dbYLength = warpsNumbForMainPass; res.dbZLength = 32; @@ -2119,7 +2119,7 @@ ForBoolKernelArgs executeHausdoff(ForFullBoolPrepArgs& fFArgs, const int W template int getHausdorffDistance_CUDA_Generic(at::Tensor goldStandard, at::Tensor algoOutput - , int WIDTH, int HEIGHT, int DEPTH, float robustnessPercent, bool resIterNeeded, at::Tensor numberToLookFor) { + , int WIDTH, int HEIGHT, int DEPTH, float robustnessPercent, bool resIterNeeded, at::Tensor compare_values) { //TODO() use https ://pytorch.org/cppdocs/notes/tensor_cuda_stream.html cudaStream_t stream1; cudaStreamCreate(&stream1); @@ -2131,7 +2131,7 @@ int getHausdorffDistance_CUDA_Generic(at::Tensor goldStandard, ForFullBoolPrepArgs forFullBoolPrepArgs; forFullBoolPrepArgs.metaData = metaData; - forFullBoolPrepArgs.numberToLookFor = numberToLookFor.item(); + forFullBoolPrepArgs.compare_values = compare_values.item(); forFullBoolPrepArgs.goldArr = goldStandard; forFullBoolPrepArgs.segmArr = algoOutput; @@ -2160,7 +2160,7 @@ int getHausdorffDistance_CUDA_Generic(at::Tensor goldStandard, template at::Tensor getHausdorffDistance_CUDA_FullResList_local(at::Tensor goldStandard, at::Tensor algoOutput - , int WIDTH, int HEIGHT, int DEPTH, float robustnessPercent, at::Tensor numberToLookFor) { + , int WIDTH, int HEIGHT, int DEPTH, float robustnessPercent, at::Tensor compare_values) { //TODO() use https ://pytorch.org/cppdocs/notes/tensor_cuda_stream.html cudaStream_t stream1; cudaStreamCreate(&stream1); @@ -2172,7 +2172,7 @@ at::Tensor getHausdorffDistance_CUDA_FullResList_local(at::Tensor goldStandard, ForFullBoolPrepArgs forFullBoolPrepArgs; forFullBoolPrepArgs.metaData = metaData; - forFullBoolPrepArgs.numberToLookFor = numberToLookFor.item(); + forFullBoolPrepArgs.compare_values = compare_values.item(); forFullBoolPrepArgs.goldArr = goldStandard; forFullBoolPrepArgs.segmArr = algoOutput; @@ -2199,12 +2199,12 @@ Functions for pybind int getHausdorffDistance_CUDA(at::Tensor goldStandard, at::Tensor algoOutput , const int WIDTH, const int HEIGHT, const int DEPTH - , const float robustnessPercent, at::Tensor numberToLookFor) { + , const float robustnessPercent, at::Tensor compare_values) { int res = 0; AT_DISPATCH_ALL_TYPESWithBool(goldStandard.type(), "getHausdorffDistance_CUDA", ([&] { - res = getHausdorffDistance_CUDA_Generic(goldStandard, algoOutput, WIDTH, HEIGHT, DEPTH, robustnessPercent, false, numberToLookFor); + res = getHausdorffDistance_CUDA_Generic(goldStandard, algoOutput, WIDTH, HEIGHT, DEPTH, robustnessPercent, false, compare_values); })); return res; @@ -2214,12 +2214,12 @@ int getHausdorffDistance_CUDA(at::Tensor goldStandard, at::Tensor getHausdorffDistance_CUDA_FullResList(at::Tensor goldStandard, at::Tensor algoOutput , const int WIDTH, const int HEIGHT, const int DEPTH - , const float robustnessPercent, at::Tensor numberToLookFor) { + , const float robustnessPercent, at::Tensor compare_values) { at::Tensor res; AT_DISPATCH_ALL_TYPESWithBool(goldStandard.type(), "getHausdorffDistance_CUDA_FullResList", ([&] { - res = getHausdorffDistance_CUDA_FullResList_local(goldStandard, algoOutput, WIDTH, HEIGHT, DEPTH, robustnessPercent, numberToLookFor); + res = getHausdorffDistance_CUDA_FullResList_local(goldStandard, algoOutput, WIDTH, HEIGHT, DEPTH, robustnessPercent, compare_values); })); diff --git a/monai/metrics/morphological_hausdorff_distance.py b/monai/metrics/morphological_hausdorff_distance.py index 8894fab57f5..72f0aabe5d7 100644 --- a/monai/metrics/morphological_hausdorff_distance.py +++ b/monai/metrics/morphological_hausdorff_distance.py @@ -22,17 +22,24 @@ class MorphologicalHausdorffDistanceMetric(CumulativeIterationMetric): """ - Work is based onthe principle of application of mathemathical morphology more precisely dilatation, more details is presented in works [1] and [2]. Calculated is roughly related to the largest -thickness of the difference between the true and estimatedmasks, and constitutes Approximation of true Hausdorff distance. The correlation between true and morphological Hausdorf distance is around 0.9, less for robust version . -Hovewer this implementation is also around 100 times faster than exact MONAI Hausdorff distance calculation. One can measure 3 diffrent metrics based on Hausdorff ditance. + Work is based onthe principle of application of mathemathical morphology more precisely dilatation + , more details is presented in works [1] and [2]. Calculated is roughly related to the largest +thickness of the difference between the true and estimatedmasks, and constitutes Approximation of true Hausdorff distance. + The correlation between true and morphological Hausdorf distance is around 0.9, less for robust version . +Hovewer this implementation is also around 100 times faster than exact MONAI Hausdorff distance calculation. + One can measure 3 diffrent metrics based on Hausdorff ditance. 1)simple Hausdorff distance - it tell what is the distance between two most distant points one from y and other from y_pred - 2)robust Hausdorff distance - modification of 1) where we stop analyzing points when we already analyzed given percent of total number of points - is less sensitive to outliers. It is also slightly faster + 2)robust Hausdorff distance - modification of 1) where we stop analyzing points + when we already analyzed given percent of total number of points - is less sensitive to outliers. It is also slightly faster - 3)mean Hausdorff distance - can be understood as approximately mean distance between pair of points where one point is from y and other from y_pred. + 3)mean Hausdorff distance - can be understood as approximately + mean distance between pair of points where one point is from y and other from y_pred. - 4)additionally one can request as a result one dimensional tensor that will contain data for each point what is HD value for it (like we would do Hausdorff distance calculation only for this point and all points from other mask) + 4)additionally one can request as a result one dimensional tensor + that will contain data for each point what is HD value for it + (like we would do Hausdorff distance calculation only for this point and all points from other mask) Compute Hausdorff Distance between two tensors. In addition, specify the `percent` (for robust Hausdorff distance ) parameter can get the percentile of the distance. Input `y_pred` is compared with ground truth `y`. @@ -42,8 +49,11 @@ class MorphologicalHausdorffDistanceMetric(CumulativeIterationMetric): percent of the Hausdorff Distance rather than the maximum result will be achieved. Defaults to 1.0 . - 1) Érick Oliveira Rodrigues,An efficient and locality-oriented Hausdorff distance algorithm: Proposal and analysis of paradigms and implementations, Pattern Recognition,Volume 117,2021 - 2) Karimi, Davood & Salcudean, Septimiu. (2019). Reducing the Hausdorff Distance in Medical Image Segmentation With Convolutional Neural Networks. IEEE Transactions on Medical Imaging. PP. 1-1. 10.1109/TMI.2019.2930068. + 1) Érick Oliveira Rodrigues,An efficient and locality-oriented Hausdorff distance algorithm: + Proposal and analysis of paradigms and implementations, Pattern Recognition,Volume 117,2021 + 2) Karimi, Davood & Salcudean, Septimiu. (2019). + Reducing the Hausdorff Distance in Medical Image Segmentation With Convolutional Neural Networks. + IEEE Transactions on Medical Imaging. PP. 1-1. 10.1109/TMI.2019.2930068. """ @@ -62,7 +72,7 @@ def compute_morphological_hausdorff_distance( self, y_pred: torch.Tensor, y: torch.Tensor, - numberToLookFor: torch.Tensor): + compare_values: torch.Tensor): """ Compute the Hausdorff distance. @@ -73,7 +83,9 @@ def compute_morphological_hausdorff_distance( percent: an optional float number between 0 and 1. If specified, the corresponding percent of the Hausdorff Distance rather than the maximum result will be achieved. Defaults to 1.0 . - numberToLookFor: 0 dimensional tensor marking what value are we intrested in the supplied y and y_pred, defined becouse in case of multiorgan segmentations frequently we can have information about multiple organs in the same mask just marked by diffrent numbers + compare_values: 0 dimensional tensor marking what value are we intrested in the supplied y and y_pred, + defined becouse in case of multiorgan segmentations frequently we can have information + about multiple organs in the same mask just marked by diffrent numbers for example in case of boolean array we can suupply it like torch.ones(1, dtype =bool) """ @@ -82,4 +94,4 @@ def compute_morphological_hausdorff_distance( raise ValueError(f"y_pred and y should have same shapes, got {y_pred.shape} and {y.shape}.") sizz = y.shape - return self.compiled_extension.getHausdorffDistance(y_pred,y,sizz[0],sizz[1],sizz[2], self.percent, numberToLookFor ) + return self.compiled_extension.getHausdorffDistance(y_pred,y,sizz[0],sizz[1],sizz[2], self.percent, compare_values ) diff --git a/tests/test_hausdorff_distance_morphologic.py b/tests/test_hausdorff_distance_morphologic.py index 5af06727257..e8014d23747 100644 --- a/tests/test_hausdorff_distance_morphologic.py +++ b/tests/test_hausdorff_distance_morphologic.py @@ -30,9 +30,9 @@ class TestHausdorffDistanceMorphological(unittest.TestCase): @parameterized.expand(TEST_CASES) def test_value(self, input_data, expected_value): - [y_pred, y, percentt, numberToLookFor] = input_data + [y_pred, y, percentt, compare_values] = input_data hd_metric = MorphologicalHausdorffDistanceMetric(percent=percentt) - result= hd_metric.compute_hausdorff_distance(y_pred,y,numberToLookFor ) + result= hd_metric.compute_hausdorff_distance(y_pred,y,compare_values ) np.testing.assert_allclose(expected_value, result, rtol=1e-7) From cdaf58956f5883ae297fd1bbd3e74efc9c64218a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 6 Apr 2022 17:00:24 +0000 Subject: [PATCH 12/41] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- monai/metrics/morphological_hausdorff_distance.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/monai/metrics/morphological_hausdorff_distance.py b/monai/metrics/morphological_hausdorff_distance.py index ac1b9676e89..e0fbd40e636 100644 --- a/monai/metrics/morphological_hausdorff_distance.py +++ b/monai/metrics/morphological_hausdorff_distance.py @@ -33,14 +33,14 @@ class MorphologicalHausdorffDistanceMetric(CumulativeIterationMetric): 1)simple Hausdorff distance - it tell what is the distance between two most distant points one from y and other from y_pred - 2)robust Hausdorff distance - modification of 1) where we stop analyzing points + 2)robust Hausdorff distance - modification of 1) where we stop analyzing points when we already analyzed given percent of total number of points - is less sensitive to outliers. It is also slightly faster - 3)mean Hausdorff distance - can be understood as approximately + 3)mean Hausdorff distance - can be understood as approximately mean distance between pair of points where one point is from y and other from y_pred. 4)additionally one can request as a result one dimensional tensor - that will contain data for each point what is HD value for it + that will contain data for each point what is HD value for it (like we would do Hausdorff distance calculation only for this point and all points from other mask) Compute Hausdorff Distance between two tensors. In addition, specify the `percent` (for robust Hausdorff distance ) @@ -51,10 +51,10 @@ class MorphologicalHausdorffDistanceMetric(CumulativeIterationMetric): percent of the Hausdorff Distance rather than the maximum result will be achieved. Defaults to 1.0 . - 1) Érick Oliveira Rodrigues,An efficient and locality-oriented Hausdorff distance algorithm: + 1) Érick Oliveira Rodrigues,An efficient and locality-oriented Hausdorff distance algorithm: Proposal and analysis of paradigms and implementations, Pattern Recognition,Volume 117,2021 - 2) Karimi, Davood & Salcudean, Septimiu. (2019). - Reducing the Hausdorff Distance in Medical Image Segmentation With Convolutional Neural Networks. + 2) Karimi, Davood & Salcudean, Septimiu. (2019). + Reducing the Hausdorff Distance in Medical Image Segmentation With Convolutional Neural Networks. IEEE Transactions on Medical Imaging. PP. 1-1. 10.1109/TMI.2019.2930068. """ From 5b6a09ade7a9ca7f6de417df28f40989da38726c Mon Sep 17 00:00:00 2001 From: Jakub Mitura <53857487+jakubMitura14@users.noreply.github.com> Date: Fri, 8 Apr 2022 19:55:14 +0200 Subject: [PATCH 13/41] added more tests Signed-off-by: Jakub Mitura <53857487+jakubMitura14@users.noreply.github.com> --- .../morphological_hausdorff_distance.py | 17 ++-- tests/test_hausdorff_distance_morphologic.py | 99 +++++++++++++++++-- 2 files changed, 103 insertions(+), 13 deletions(-) diff --git a/monai/metrics/morphological_hausdorff_distance.py b/monai/metrics/morphological_hausdorff_distance.py index ac1b9676e89..9a5119f7e72 100644 --- a/monai/metrics/morphological_hausdorff_distance.py +++ b/monai/metrics/morphological_hausdorff_distance.py @@ -65,7 +65,8 @@ def __init__(self, percent: float = 1.0) -> None: self.compiled_extension = load_module("hausdorff_cpp") def compute_morphological_hausdorff_distance( - self, y_pred: torch.Tensor, y: torch.Tensor, numberToLookFor: torch.Tensor + self, y_pred: torch.Tensor, y: torch.Tensor + ,compare_values: torch.Tensor, to_invert_dims=False ): """ Compute the Hausdorff distance. @@ -81,13 +82,17 @@ def compute_morphological_hausdorff_distance( defined becouse in case of multiorgan segmentations frequently we can have information about multiple organs in the same mask just marked by diffrent numbers for example in case of boolean array we can suupply it like torch.ones(1, dtype =bool) - + to_invert_dims - inverts first with third dim - used fo testing only """ if y.shape != y_pred.shape: raise ValueError(f"y_pred and y should have same shapes, got {y_pred.shape} and {y.shape}.") sizz = y.shape - - return self.compiled_extension.getHausdorffDistance( - y_pred, y, sizz[0], sizz[1], sizz[2], self.percent, numberToLookFor - ) + if to_invert_dims: + return self.compiled_extension.getHausdorffDistance( + y_pred, y, sizz[2], sizz[1], sizz[0], self.percent, compare_values + ) + else: + return self.compiled_extension.getHausdorffDistance( + y_pred, y, sizz[0], sizz[1], sizz[2], self.percent, compare_values + ) diff --git a/tests/test_hausdorff_distance_morphologic.py b/tests/test_hausdorff_distance_morphologic.py index 0afb3587c44..e16a7b69ae2 100644 --- a/tests/test_hausdorff_distance_morphologic.py +++ b/tests/test_hausdorff_distance_morphologic.py @@ -17,21 +17,106 @@ from monai.metrics import MorphologicalHausdorffDistanceMetric -a = torch.zeros(20, 20, 20) -b = torch.zeros(20, 20, 20) +device = torch.device("cuda") + + +### testing single points diffrent dims +#dim1 +compare_values = torch.ones(1).to(device) +a = torch.zeros(100, 100, 100).to(device) +b = torch.zeros(100, 100, 100).to(device) a[0, 0, 0] = 1 -b[0, 0, 10] = 1 -numbToLookFor = torch.ones(1) +b[10, 0, 0] = 1 + +#dim2 +a1 = torch.zeros(200, 200, 200).to(device) +b1 = torch.zeros(200, 200, 200).to(device) +a1[0, 0, 0] = 1 +b1[0, 15, 0] = 1 + +#dim3 +a2 = torch.zeros(400, 200, 300).to(device) +b2 = torch.zeros(400, 200, 300).to(device) +a2[0, 0, 10] = 1 +b2[0, 0, 150] = 1 + +### testing whole llines and compare_values set to 2 +compare_valuesB = torch.ones(1).to(device) +compare_valuesB[0]=2 +a3 = torch.zeros(400, 200, 300).to(device) +b3 = torch.zeros(400, 200, 300).to(device) +a3[:, 0, 10] = 2 +b3[:, 0, 150] = 2 + +a4 = torch.zeros(400, 200, 300).to(device) +b4 = torch.zeros(400, 200, 300).to(device) +a4[10, 0, :] = 2 +b4[120, 0, :] = 2 + + +a5 = torch.zeros(400, 200, 300).to(device) +b5 = torch.zeros(400, 200, 300).to(device) +a5[10, :, 0] = 2 +b5[120, :, 0] = 2 + + +## testing whole planes +a6 = torch.zeros(400, 200, 300).to(device) +b6 = torch.zeros(400, 200, 300).to(device) +a6[10, :, :] = 2 +b6[120, :, :] = 2 + + +a7 = torch.zeros(400, 200, 300).to(device) +b7 = torch.zeros(400, 200, 300).to(device) +a7[:, 0, :] = 2 +b7[:,110, :] = 2 + +a8 = torch.zeros(400, 200, 300).to(device) +b8 = torch.zeros(400, 200, 300).to(device) +#a8[:, :, 20] = 2 +#b8[:,:, 130] = 2 + + +a8[1, 1, 20]= 2 +b8[1,1, 130]= 2 +a8[2, 2, 20]= 2 +b8[2,2, 130]= 2 + +#multi points +a9 = torch.zeros(400, 200, 300).to(device) +b9 = torch.zeros(400, 200, 300).to(device) -TEST_CASES = [[[a, b, 1.0, numbToLookFor], 10]] +a9[0, 20,0 ]= 2 +a9[0, 0,30 ]= 2 +a9[40, 0,0 ]= 2 +b9[0,0,0 ]= 2 +TEST_CASES = [ + [[a, b, 1.0, compare_values], 10] + ,[[a1, b1, 1.0, compare_values], 15] + ,[[a2, b2, 1.0, compare_values], 140] + ,[[a3, b3, 1.0, compare_valuesB], 140] + ,[[a4, b4, 1.0, compare_valuesB], 110] + ,[[a5, b5, 1.0, compare_valuesB], 110] + ,[[a6, b6, 1.0, compare_valuesB], 110] + ,[[a7, b7, 1.0, compare_valuesB], 110] + ,[[a8, b8, 1.0, compare_valuesB], 110] + + #testing robust + ,[[a6, b6, 0.9, compare_valuesB], 110] + ,[[a7, b7, 0.85, compare_valuesB], 110] + ,[[a8, b8, 0.8, compare_valuesB], 110] + #multi points + ,[[a9, b9, 1.0, compare_valuesB], 40] + ] class TestHausdorffDistanceMorphological(unittest.TestCase): @parameterized.expand(TEST_CASES) def test_value(self, input_data, expected_value): [y_pred, y, percentt, compare_values] = input_data - hd_metric = MorphologicalHausdorffDistanceMetric(percent=percentt) - result = hd_metric.compute_hausdorff_distance(y_pred, y, numberToLookFor) + hd_metric = MorphologicalHausdorffDistanceMetric(percentt) + result = hd_metric.compute_hausdorff_distance(y_pred, y, compare_values,True) np.testing.assert_allclose(expected_value, result, rtol=1e-7) From 206aebc35076d1841ce08651911499f29b85028e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 8 Apr 2022 17:55:40 +0000 Subject: [PATCH 14/41] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- monai/metrics/morphological_hausdorff_distance.py | 2 +- tests/test_hausdorff_distance_morphologic.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/monai/metrics/morphological_hausdorff_distance.py b/monai/metrics/morphological_hausdorff_distance.py index b46e70cddba..9b7189118ee 100644 --- a/monai/metrics/morphological_hausdorff_distance.py +++ b/monai/metrics/morphological_hausdorff_distance.py @@ -95,4 +95,4 @@ def compute_morphological_hausdorff_distance( else: return self.compiled_extension.getHausdorffDistance( y_pred, y, sizz[0], sizz[1], sizz[2], self.percent, compare_values - ) + ) diff --git a/tests/test_hausdorff_distance_morphologic.py b/tests/test_hausdorff_distance_morphologic.py index e16a7b69ae2..bc3657419eb 100644 --- a/tests/test_hausdorff_distance_morphologic.py +++ b/tests/test_hausdorff_distance_morphologic.py @@ -102,7 +102,7 @@ ,[[a6, b6, 1.0, compare_valuesB], 110] ,[[a7, b7, 1.0, compare_valuesB], 110] ,[[a8, b8, 1.0, compare_valuesB], 110] - + #testing robust ,[[a6, b6, 0.9, compare_valuesB], 110] ,[[a7, b7, 0.85, compare_valuesB], 110] From 034f45f0ebd4eaf611941e7521a566ef9a4f6687 Mon Sep 17 00:00:00 2001 From: Jakub Mitura <53857487+jakubMitura14@users.noreply.github.com> Date: Fri, 8 Apr 2022 21:02:11 +0200 Subject: [PATCH 15/41] added skip_if_no_cuda Signed-off-by: Jakub Mitura <53857487+jakubMitura14@users.noreply.github.com> --- tests/test_hausdorff_distance_morphologic.py | 49 +++++++++++--------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/tests/test_hausdorff_distance_morphologic.py b/tests/test_hausdorff_distance_morphologic.py index e16a7b69ae2..b8f6a94851d 100644 --- a/tests/test_hausdorff_distance_morphologic.py +++ b/tests/test_hausdorff_distance_morphologic.py @@ -16,64 +16,65 @@ from parameterized import parameterized from monai.metrics import MorphologicalHausdorffDistanceMetric +from tests.utils import skip_if_no_cuda device = torch.device("cuda") ### testing single points diffrent dims #dim1 -compare_values = torch.ones(1).to(device) -a = torch.zeros(100, 100, 100).to(device) -b = torch.zeros(100, 100, 100).to(device) +compare_values = torch.ones(1) +a = torch.zeros(100, 100, 100) +b = torch.zeros(100, 100, 100) a[0, 0, 0] = 1 b[10, 0, 0] = 1 #dim2 -a1 = torch.zeros(200, 200, 200).to(device) -b1 = torch.zeros(200, 200, 200).to(device) +a1 = torch.zeros(200, 200, 200) +b1 = torch.zeros(200, 200, 200) a1[0, 0, 0] = 1 b1[0, 15, 0] = 1 #dim3 -a2 = torch.zeros(400, 200, 300).to(device) -b2 = torch.zeros(400, 200, 300).to(device) +a2 = torch.zeros(400, 200, 300) +b2 = torch.zeros(400, 200, 300) a2[0, 0, 10] = 1 b2[0, 0, 150] = 1 ### testing whole llines and compare_values set to 2 -compare_valuesB = torch.ones(1).to(device) +compare_valuesB = torch.ones(1) compare_valuesB[0]=2 -a3 = torch.zeros(400, 200, 300).to(device) -b3 = torch.zeros(400, 200, 300).to(device) +a3 = torch.zeros(400, 200, 300) +b3 = torch.zeros(400, 200, 300) a3[:, 0, 10] = 2 b3[:, 0, 150] = 2 -a4 = torch.zeros(400, 200, 300).to(device) -b4 = torch.zeros(400, 200, 300).to(device) +a4 = torch.zeros(400, 200, 300) +b4 = torch.zeros(400, 200, 300) a4[10, 0, :] = 2 b4[120, 0, :] = 2 -a5 = torch.zeros(400, 200, 300).to(device) -b5 = torch.zeros(400, 200, 300).to(device) +a5 = torch.zeros(400, 200, 300) +b5 = torch.zeros(400, 200, 300) a5[10, :, 0] = 2 b5[120, :, 0] = 2 ## testing whole planes -a6 = torch.zeros(400, 200, 300).to(device) -b6 = torch.zeros(400, 200, 300).to(device) +a6 = torch.zeros(400, 200, 300) +b6 = torch.zeros(400, 200, 300) a6[10, :, :] = 2 b6[120, :, :] = 2 -a7 = torch.zeros(400, 200, 300).to(device) -b7 = torch.zeros(400, 200, 300).to(device) +a7 = torch.zeros(400, 200, 300) +b7 = torch.zeros(400, 200, 300) a7[:, 0, :] = 2 b7[:,110, :] = 2 -a8 = torch.zeros(400, 200, 300).to(device) -b8 = torch.zeros(400, 200, 300).to(device) +a8 = torch.zeros(400, 200, 300) +b8 = torch.zeros(400, 200, 300) #a8[:, :, 20] = 2 #b8[:,:, 130] = 2 @@ -84,8 +85,8 @@ b8[2,2, 130]= 2 #multi points -a9 = torch.zeros(400, 200, 300).to(device) -b9 = torch.zeros(400, 200, 300).to(device) +a9 = torch.zeros(400, 200, 300) +b9 = torch.zeros(400, 200, 300) a9[0, 20,0 ]= 2 a9[0, 0,30 ]= 2 @@ -111,12 +112,14 @@ ,[[a9, b9, 1.0, compare_valuesB], 40] ] + +@skip_if_no_cuda class TestHausdorffDistanceMorphological(unittest.TestCase): @parameterized.expand(TEST_CASES) def test_value(self, input_data, expected_value): [y_pred, y, percentt, compare_values] = input_data hd_metric = MorphologicalHausdorffDistanceMetric(percentt) - result = hd_metric.compute_hausdorff_distance(y_pred, y, compare_values,True) + result = hd_metric.compute_hausdorff_distance(y_pred.to(device), y.to(device), compare_values.to(device),True) np.testing.assert_allclose(expected_value, result, rtol=1e-7) From 9580d168aaa9ee3e980d5207485796fd19c86639 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 8 Apr 2022 19:02:38 +0000 Subject: [PATCH 16/41] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_hausdorff_distance_morphologic.py | 46 ++++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/tests/test_hausdorff_distance_morphologic.py b/tests/test_hausdorff_distance_morphologic.py index 046097d98a4..e791068ccc4 100644 --- a/tests/test_hausdorff_distance_morphologic.py +++ b/tests/test_hausdorff_distance_morphologic.py @@ -23,58 +23,58 @@ ### testing single points diffrent dims #dim1 -compare_values = torch.ones(1) -a = torch.zeros(100, 100, 100) -b = torch.zeros(100, 100, 100) +compare_values = torch.ones(1) +a = torch.zeros(100, 100, 100) +b = torch.zeros(100, 100, 100) a[0, 0, 0] = 1 b[10, 0, 0] = 1 #dim2 -a1 = torch.zeros(200, 200, 200) -b1 = torch.zeros(200, 200, 200) +a1 = torch.zeros(200, 200, 200) +b1 = torch.zeros(200, 200, 200) a1[0, 0, 0] = 1 b1[0, 15, 0] = 1 #dim3 -a2 = torch.zeros(400, 200, 300) -b2 = torch.zeros(400, 200, 300) +a2 = torch.zeros(400, 200, 300) +b2 = torch.zeros(400, 200, 300) a2[0, 0, 10] = 1 b2[0, 0, 150] = 1 ### testing whole llines and compare_values set to 2 -compare_valuesB = torch.ones(1) +compare_valuesB = torch.ones(1) compare_valuesB[0]=2 -a3 = torch.zeros(400, 200, 300) -b3 = torch.zeros(400, 200, 300) +a3 = torch.zeros(400, 200, 300) +b3 = torch.zeros(400, 200, 300) a3[:, 0, 10] = 2 b3[:, 0, 150] = 2 -a4 = torch.zeros(400, 200, 300) -b4 = torch.zeros(400, 200, 300) +a4 = torch.zeros(400, 200, 300) +b4 = torch.zeros(400, 200, 300) a4[10, 0, :] = 2 b4[120, 0, :] = 2 -a5 = torch.zeros(400, 200, 300) -b5 = torch.zeros(400, 200, 300) +a5 = torch.zeros(400, 200, 300) +b5 = torch.zeros(400, 200, 300) a5[10, :, 0] = 2 b5[120, :, 0] = 2 ## testing whole planes -a6 = torch.zeros(400, 200, 300) -b6 = torch.zeros(400, 200, 300) +a6 = torch.zeros(400, 200, 300) +b6 = torch.zeros(400, 200, 300) a6[10, :, :] = 2 b6[120, :, :] = 2 -a7 = torch.zeros(400, 200, 300) -b7 = torch.zeros(400, 200, 300) +a7 = torch.zeros(400, 200, 300) +b7 = torch.zeros(400, 200, 300) a7[:, 0, :] = 2 b7[:,110, :] = 2 -a8 = torch.zeros(400, 200, 300) -b8 = torch.zeros(400, 200, 300) +a8 = torch.zeros(400, 200, 300) +b8 = torch.zeros(400, 200, 300) #a8[:, :, 20] = 2 #b8[:,:, 130] = 2 @@ -85,8 +85,8 @@ b8[2,2, 130]= 2 #multi points -a9 = torch.zeros(400, 200, 300) -b9 = torch.zeros(400, 200, 300) +a9 = torch.zeros(400, 200, 300) +b9 = torch.zeros(400, 200, 300) a9[0, 20,0 ]= 2 a9[0, 0,30 ]= 2 @@ -112,7 +112,7 @@ ,[[a9, b9, 1.0, compare_valuesB], 40] ] - + @skip_if_no_cuda class TestHausdorffDistanceMorphological(unittest.TestCase): @parameterized.expand(TEST_CASES) From 11710a2c219a1449ef86eb5e475587a508367547 Mon Sep 17 00:00:00 2001 From: monai-bot Date: Sat, 9 Apr 2022 06:45:57 +0000 Subject: [PATCH 17/41] [MONAI] python code formatting Signed-off-by: monai-bot --- monai/_extensions/hausdorff/hausdorff_cpp.cpp | 116 +- monai/_extensions/hausdorff/hausdorff_cuda.cu | 4223 ++++++++--------- .../morphological_hausdorff_distance.py | 53 +- tests/test_hausdorff_distance_morphologic.py | 71 +- 4 files changed, 2192 insertions(+), 2271 deletions(-) diff --git a/monai/_extensions/hausdorff/hausdorff_cpp.cpp b/monai/_extensions/hausdorff/hausdorff_cpp.cpp index 5e4aea18fcb..52d8637b4b9 100644 --- a/monai/_extensions/hausdorff/hausdorff_cpp.cpp +++ b/monai/_extensions/hausdorff/hausdorff_cpp.cpp @@ -9,93 +9,87 @@ // See the License for the specific language governing permissions and // limitations under the License. - - #include #include // CUDA declarations - int getHausdorffDistance_CUDA( torch::Tensor goldStandard, - torch::Tensor algoOutput - , const int xDim, const int yDim - , const int zDim,const float robustnessPercent); - - -at::Tensor getHausdorffDistance_CUDA_FullResList(at::Tensor goldStandard, - at::Tensor algoOutput - , int WIDTH, int HEIGHT, int DEPTH, float robustnessPercent); - - - -std::tuple benchmarkOlivieraCUDA( + torch::Tensor algoOutput, + const int xDim, + const int yDim, + const int zDim, + const float robustnessPercent); + +at::Tensor getHausdorffDistance_CUDA_FullResList( + at::Tensor goldStandard, + at::Tensor algoOutput, + int WIDTH, + int HEIGHT, + int DEPTH, + float robustnessPercent); + +std::tuple benchmarkOlivieraCUDA( torch::Tensor goldStandard, - torch::Tensor algoOutput - , const int xDim, const int yDim - , const int zDim); + torch::Tensor algoOutput, + const int xDim, + const int yDim, + const int zDim); // C++ interface // NOTE: AT_ASSERT has become AT_CHECK on master after 0.4. #define CHECK_CUDA(x) AT_ASSERTM(x.type().is_cuda(), #x " must be a CUDA tensor") #define CHECK_CONTIGUOUS(x) AT_ASSERTM(x.is_contiguous(), #x " must be contiguous") -#define CHECK_INPUT(x) CHECK_CUDA(x); CHECK_CONTIGUOUS(x) - +#define CHECK_INPUT(x) \ + CHECK_CUDA(x); \ + CHECK_CONTIGUOUS(x) int getHausdorffDistance( torch::Tensor goldStandard, - torch::Tensor algoOutput - , const int xDim, const int yDim, const int zDim - , const float robustnessPercent=1.0 -) { - - CHECK_INPUT(goldStandard); - CHECK_INPUT(algoOutput); - - - return getHausdorffDistance_CUDA(goldStandard, algoOutput, xDim, yDim, zDim, robustnessPercent); - - + torch::Tensor algoOutput, + const int xDim, + const int yDim, + const int zDim, + const float robustnessPercent = 1.0) { + CHECK_INPUT(goldStandard); + CHECK_INPUT(algoOutput); + + return getHausdorffDistance_CUDA(goldStandard, algoOutput, xDim, yDim, zDim, robustnessPercent); } - - - at::Tensor getHausdorffDistance_FullResList( torch::Tensor goldStandard, - torch::Tensor algoOutput - , const int xDim, const int yDim, const int zDim - , const float robustnessPercent = 1.0 -) { - - CHECK_INPUT(goldStandard); - CHECK_INPUT(algoOutput); - - - return getHausdorffDistance_CUDA_FullResList(goldStandard, algoOutput, xDim, yDim, zDim, robustnessPercent); - - + torch::Tensor algoOutput, + const int xDim, + const int yDim, + const int zDim, + const float robustnessPercent = 1.0) { + CHECK_INPUT(goldStandard); + CHECK_INPUT(algoOutput); + + return getHausdorffDistance_CUDA_FullResList(goldStandard, algoOutput, xDim, yDim, zDim, robustnessPercent); } - - - -std::tuple benchmarkOlivieraCUDAOnlyBool( +std::tuple benchmarkOlivieraCUDAOnlyBool( torch::Tensor goldStandard, - torch::Tensor algoOutput - , const int xDim, const int yDim - , const int zDim) { - return benchmarkOlivieraCUDA(goldStandard, algoOutput, xDim, yDim, zDim); - - + torch::Tensor algoOutput, + const int xDim, + const int yDim, + const int zDim) { + return benchmarkOlivieraCUDA(goldStandard, algoOutput, xDim, yDim, zDim); } - PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) { - m.def("getHausdorffDistance", &getHausdorffDistance, "Basic version of Hausdorff distance"); - m.def("benchmarkOlivieraCUDA", &benchmarkOlivieraCUDA, "Algorithm by Oliviera - just for comparison sake - accept only boolean arrays "); - m.def("getHausdorffDistance_FullResList", &getHausdorffDistance_FullResList, " return additionally full result list indicating in which dilatation iterations results were recorded "); + m.def("getHausdorffDistance", &getHausdorffDistance, "Basic version of Hausdorff distance"); + m.def( + "benchmarkOlivieraCUDA", + &benchmarkOlivieraCUDA, + "Algorithm by Oliviera - just for comparison sake - accept only boolean arrays "); + m.def( + "getHausdorffDistance_FullResList", + &getHausdorffDistance_FullResList, + " return additionally full result list indicating in which dilatation iterations results were recorded "); } diff --git a/monai/_extensions/hausdorff/hausdorff_cuda.cu b/monai/_extensions/hausdorff/hausdorff_cuda.cu index 213d4bb286d..c166f8ec071 100644 --- a/monai/_extensions/hausdorff/hausdorff_cuda.cu +++ b/monai/_extensions/hausdorff/hausdorff_cuda.cu @@ -16,189 +16,171 @@ #include #include -#include #include +#include //#include #include #include //#include -#include #include +#include -#include -#include +#include #include #include -#include +#include #include "device_launch_parameters.h" using namespace cooperative_groups; - #pragma once -//constants describing the meaning of main shared memory spaces +// constants describing the meaning of main shared memory spaces constexpr uint32_t localWorkQueLength = 300; constexpr uint32_t startOfLocalWorkQ = 4160; -constexpr uint32_t lengthOfMainShmem = 4460;//4460; +constexpr uint32_t lengthOfMainShmem = 4460; // 4460; constexpr uint32_t begResShmem = 1088; constexpr uint32_t begfirstRegShmem = 2112; constexpr uint32_t begSecRegShmem = 3136; constexpr uint32_t begSourceShmem = 32; - -//added to linear index meta in order to mark weather block is of type gold or not -constexpr uint32_t isGoldOffset = (UINT16_MAX * 10); - - +// added to linear index meta in order to mark weather block is of type gold or not +constexpr uint32_t isGoldOffset = (UINT16_MAX * 10); /*************************************** * structs * ********************************/ - - /** - In order to be able to use cuda malloc 3d we will implemnt it as a series - of 3d arrays - */ +/** +In order to be able to use cuda malloc 3d we will implemnt it as a series +of 3d arrays +*/ #pragma once extern "C" struct MetaDataCPU { - int metaXLength; - int MetaYLength; - int MetaZLength; - int totalMetaLength; - - - //1)maxX 2)minX 3)maxY 4) minY 5) maxZ 6) minZ - minimal and maximum coordinates of blocks with some entries of intrest - //7)global FP count; 8)global FN count 9) workQueueCounter 10)resultFP globalCounter 11) resultFn globalCounter - //12) global FPandFn offset 13)globalIterationNumb - //array3dWithDimsCPU minMaxes; - unsigned int minMaxes[20]; - uint32_t* resultList; - + int metaXLength; + int MetaYLength; + int MetaZLength; + int totalMetaLength; + + // 1)maxX 2)minX 3)maxY 4) minY 5) maxZ 6) minZ - minimal and maximum coordinates of blocks with some entries of + // intrest 7)global FP count; 8)global FN count 9) workQueueCounter 10)resultFP globalCounter 11) resultFn + // globalCounter 12) global FPandFn offset 13)globalIterationNumb + // array3dWithDimsCPU minMaxes; + unsigned int minMaxes[20]; + uint32_t* resultList; }; #pragma once extern "C" struct MetaDataGPU { - int metaXLength; - int MetaYLength; - int MetaZLength; - int totalMetaLength; - - //1)maxX 2)minX 3)maxY 4) minY 5) maxZ 6) minZ - minimal and maximum coordinates of blocks with some entries of intrest - //7)global FP count; 8)global FN count 9) workQueueCounter 10)resultFP globalCounter 11) resultFn globalCounter - //12) global FPandFn offset 13)globalIterationNumb - - unsigned int* minMaxes; - - //represents x from description of main Arr - unsigned int mainArrXLength; - //have length 4x - unsigned int mainArrSectionLength; - - unsigned int metaDataSectionLength = 20; - - // now we will store here also calculated by min maxes kernel values of minimum and maximumvalues - //1)maxX 2)minX 3)maxY 4) minY 5) maxZ 6) minZ - unsigned int maxX; - unsigned int minX; - unsigned int maxY; - unsigned int minY; - unsigned int maxZ; - unsigned int minZ; + int metaXLength; + int MetaYLength; + int MetaZLength; + int totalMetaLength; + + // 1)maxX 2)minX 3)maxY 4) minY 5) maxZ 6) minZ - minimal and maximum coordinates of blocks with some entries of + // intrest 7)global FP count; 8)global FN count 9) workQueueCounter 10)resultFP globalCounter 11) resultFn + // globalCounter 12) global FPandFn offset 13)globalIterationNumb + + unsigned int* minMaxes; + + // represents x from description of main Arr + unsigned int mainArrXLength; + // have length 4x + unsigned int mainArrSectionLength; + + unsigned int metaDataSectionLength = 20; + + // now we will store here also calculated by min maxes kernel values of minimum and maximumvalues + // 1)maxX 2)minX 3)maxY 4) minY 5) maxZ 6) minZ + unsigned int maxX; + unsigned int minX; + unsigned int maxY; + unsigned int minY; + unsigned int maxZ; + unsigned int minZ; }; - - - - - - - /* -* Basically holding the arguments for master functions controlling full preparation to get all for Housedorff kernel -*/ + * Basically holding the arguments for master functions controlling full preparation to get all for Housedorff kernel + */ #pragma once template struct ForFullBoolPrepArgs { - - //metadata struct - MetaDataCPU metaData; - - // dimensions of data block - int dbXLength; - int dbYLength; - int dbZLength; - // gold standard and segmentation output array - torch::Tensor goldArr; - torch::Tensor segmArr; - - int Nx; - int Ny; - int Nz; - - TFF compare_values;// what we will look for in arrays - //number and dimensionality of threads and blocks required to lounch bool kernel - dim3 threads; - int blocks; - //threads and blocks for first metadata pass kernel - int threadsFirstMetaDataPass; - int blocksFirstMetaDataPass; - //threads and blocks for main pass - dim3 threadsMainPass; - int blocksMainPass; - //threads and blocks for padding pass - dim3 threadsPaddingPass; - int blocksPaddingPass; - //threads and blocks for non first metadata passes - int threadsOtherMetaDataPasses; - int blocksOtherMetaDataPasses; - // will establish how many points we want to include in dilatation and how many we can ignore so typically set to 95% - so we will ignore only 5% most distant - float robustnessPercent = 1.0; // 0.95; - - int32_t* resultListPointerMeta; - int32_t* resultListPointerLocalCPU; - int32_t* resultListPointerIterNumb; - + // metadata struct + MetaDataCPU metaData; + + // dimensions of data block + int dbXLength; + int dbYLength; + int dbZLength; + // gold standard and segmentation output array + torch::Tensor goldArr; + torch::Tensor segmArr; + + int Nx; + int Ny; + int Nz; + + TFF compare_values; // what we will look for in arrays + // number and dimensionality of threads and blocks required to lounch bool kernel + dim3 threads; + int blocks; + // threads and blocks for first metadata pass kernel + int threadsFirstMetaDataPass; + int blocksFirstMetaDataPass; + // threads and blocks for main pass + dim3 threadsMainPass; + int blocksMainPass; + // threads and blocks for padding pass + dim3 threadsPaddingPass; + int blocksPaddingPass; + // threads and blocks for non first metadata passes + int threadsOtherMetaDataPasses; + int blocksOtherMetaDataPasses; + // will establish how many points we want to include in dilatation and how many we can ignore so typically set to 95% + // - so we will ignore only 5% most distant + float robustnessPercent = 1.0; // 0.95; + + int32_t* resultListPointerMeta; + int32_t* resultListPointerLocalCPU; + int32_t* resultListPointerIterNumb; }; - /* -* Basically holding the arguments for main kernel in the FullBoolPrep -*/ + * Basically holding the arguments for main kernel in the FullBoolPrep + */ #pragma once template struct ForBoolKernelArgs { - //matadata struct - MetaDataGPU metaData; - // dimensions of data block - int dbXLength; - int dbYLength; - int dbZLength; - // gold standard and segmentation output array - torch::Tensor goldArr; - torch::Tensor segmArr; - TFB compare_values; - int Nx; - int Ny; - int Nz; - - int32_t* resultListPointerMeta; - int32_t* resultListPointerLocal; - int32_t* resultListPointerIterNumb; - - torch::Tensor resultListPointerIterNumbTensor; - - uint32_t* origArrsPointer; - uint32_t* mainArrAPointer; - uint32_t* mainArrBPointer; - uint32_t* metaDataArrPointer; - - uint32_t* workQueuePointer; - unsigned int* minMaxes; - - - /* + // matadata struct + MetaDataGPU metaData; + // dimensions of data block + int dbXLength; + int dbYLength; + int dbZLength; + // gold standard and segmentation output array + torch::Tensor goldArr; + torch::Tensor segmArr; + TFB compare_values; + int Nx; + int Ny; + int Nz; + + int32_t* resultListPointerMeta; + int32_t* resultListPointerLocal; + int32_t* resultListPointerIterNumb; + + torch::Tensor resultListPointerIterNumbTensor; + + uint32_t* origArrsPointer; + uint32_t* mainArrAPointer; + uint32_t* mainArrBPointer; + uint32_t* metaDataArrPointer; + + uint32_t* workQueuePointer; + unsigned int* minMaxes; + + /* main array with all required data organized in sections for each metadata block x- is block dimx times block dimy now what occupies what positions @@ -239,149 +221,130 @@ now what occupies what positions (x+1) - 2x : reducedSegm */ - - float robustnessPercent = 1.0;// 0.95; - + float robustnessPercent = 1.0; // 0.95; }; - - - /* data from occupancy calculator API used to get optimal number of thread blocks and threads per thread block */ struct occupancyCalcData { - int warpsNumbForMinMax; - int blockSizeForMinMax; + int warpsNumbForMinMax; + int blockSizeForMinMax; - int warpsNumbForboolPrepareKernel; - int blockSizeFoboolPrepareKernel; + int warpsNumbForboolPrepareKernel; + int blockSizeFoboolPrepareKernel; - int theadsForFirstMetaPass; - int blockForFirstMetaPass; + int theadsForFirstMetaPass; + int blockForFirstMetaPass; - int warpsNumbForMainPass; - int blockForMainPass; + int warpsNumbForMainPass; + int blockForMainPass; }; - - /*************************************** * utils * ********************************/ - - /* - copy from host to device - */ +/* +copy from host to device +*/ #pragma once inline MetaDataGPU allocateMetaDataOnGPU(MetaDataCPU metaDataCPU, unsigned int*& minMaxes) { - MetaDataGPU res; - - metaDataCPU.minMaxes[1] = 0; - metaDataCPU.minMaxes[2] = 1000; - metaDataCPU.minMaxes[3] = 0; - metaDataCPU.minMaxes[4] = 1000; - metaDataCPU.minMaxes[5] = 0; - metaDataCPU.minMaxes[6] = 1000; - metaDataCPU.minMaxes[7] = 0; - metaDataCPU.minMaxes[8] = 0; - metaDataCPU.minMaxes[9] = 0; - metaDataCPU.minMaxes[10] = 0; - metaDataCPU.minMaxes[11] = 0; - metaDataCPU.minMaxes[12] = 0; - metaDataCPU.minMaxes[13] = 0; - metaDataCPU.minMaxes[14] = 0; - metaDataCPU.minMaxes[15] = 0; - metaDataCPU.minMaxes[16] = 0; - metaDataCPU.minMaxes[17] = 0; - metaDataCPU.minMaxes[18] = 0; - metaDataCPU.minMaxes[19] = 0; - metaDataCPU.minMaxes[20] = 0; - - size_t size = sizeof(unsigned int) * 20; - cudaMemcpy(minMaxes, metaDataCPU.minMaxes, size, cudaMemcpyHostToDevice); - - return res; - + MetaDataGPU res; + + metaDataCPU.minMaxes[1] = 0; + metaDataCPU.minMaxes[2] = 1000; + metaDataCPU.minMaxes[3] = 0; + metaDataCPU.minMaxes[4] = 1000; + metaDataCPU.minMaxes[5] = 0; + metaDataCPU.minMaxes[6] = 1000; + metaDataCPU.minMaxes[7] = 0; + metaDataCPU.minMaxes[8] = 0; + metaDataCPU.minMaxes[9] = 0; + metaDataCPU.minMaxes[10] = 0; + metaDataCPU.minMaxes[11] = 0; + metaDataCPU.minMaxes[12] = 0; + metaDataCPU.minMaxes[13] = 0; + metaDataCPU.minMaxes[14] = 0; + metaDataCPU.minMaxes[15] = 0; + metaDataCPU.minMaxes[16] = 0; + metaDataCPU.minMaxes[17] = 0; + metaDataCPU.minMaxes[18] = 0; + metaDataCPU.minMaxes[19] = 0; + metaDataCPU.minMaxes[20] = 0; + + size_t size = sizeof(unsigned int) * 20; + cudaMemcpy(minMaxes, metaDataCPU.minMaxes, size, cudaMemcpyHostToDevice); + + return res; } /*************************************** * utils * ********************************/ - /* - setting the linear index of metadata blocks that are in given direction if there is no such (out of range) we will save it as UINT32_MAX - */ +/* +setting the linear index of metadata blocks that are in given direction if there is no such (out of range) we will save +it as UINT32_MAX +*/ #pragma once template -__device__ inline void setNeighbourBlocks(ForBoolKernelArgs fbArgs - , uint8_t idX, uint8_t inArrIndex, bool predicate, uint32_t toAdd - , uint32_t linIdexMeta, MetaDataGPU metaData, uint32_t localBlockMetaData[20]) { - - if ((threadIdx.x == idX) && (threadIdx.y == 0)) { - if (predicate) { - - - localBlockMetaData[inArrIndex] = (linIdexMeta + toAdd); - } - else { - localBlockMetaData[inArrIndex] = isGoldOffset; - } - }; +__device__ inline void setNeighbourBlocks( + ForBoolKernelArgs fbArgs, + uint8_t idX, + uint8_t inArrIndex, + bool predicate, + uint32_t toAdd, + uint32_t linIdexMeta, + MetaDataGPU metaData, + uint32_t localBlockMetaData[20]) { + if ((threadIdx.x == idX) && (threadIdx.y == 0)) { + if (predicate) { + localBlockMetaData[inArrIndex] = (linIdexMeta + toAdd); + } else { + localBlockMetaData[inArrIndex] = isGoldOffset; + } + }; } - /* gettinng array for dilatations -basically arrays will alternate between iterations once one will be source other target then they will switch - we will decide upon knowing -wheather the iteration number is odd or even +basically arrays will alternate between iterations once one will be source other target then they will switch - we will +decide upon knowing wheather the iteration number is odd or even */ #pragma once template -inline __device__ uint32_t* getSourceReduced(const ForBoolKernelArgs& fbArgs, const int(&iterationNumb)[1]) { - - - if ((iterationNumb[0] & 1) == 0) { - return fbArgs.mainArrAPointer; - - } - else { - return fbArgs.mainArrBPointer; - } - +inline __device__ uint32_t* getSourceReduced(const ForBoolKernelArgs& fbArgs, const int (&iterationNumb)[1]) { + if ((iterationNumb[0] & 1) == 0) { + return fbArgs.mainArrAPointer; + } else { + return fbArgs.mainArrBPointer; + } } - /* gettinng target array for dilatations -basically arrays will alternate between iterations once one will be source other target then they will switch - we will decide upon knowing -wheather the iteration number is odd or even +basically arrays will alternate between iterations once one will be source other target then they will switch - we will +decide upon knowing wheather the iteration number is odd or even */ #pragma once template -inline __device__ uint32_t* getTargetReduced(const ForBoolKernelArgs& fbArgs, const int(&iterationNumb)[1]) { - - if ((iterationNumb[0] & 1) == 0) { - - return fbArgs.mainArrBPointer; - - } - else { - return fbArgs.mainArrAPointer; - - } +inline __device__ uint32_t* getTargetReduced(const ForBoolKernelArgs& fbArgs, const int (&iterationNumb)[1]) { + if ((iterationNumb[0] & 1) == 0) { + return fbArgs.mainArrBPointer; + } else { + return fbArgs.mainArrAPointer; + } } - /* dilatation up and down - using bitwise operators */ #pragma once inline __device__ uint32_t bitDilatate(const uint32_t& x) { - return ((x) >> 1) | (x) | ((x) << 1); + return ((x) >> 1) | (x) | ((x) << 1); } /* @@ -389,1642 +352,1560 @@ return 1 if at given position of given number bit is set otherwise 0 */ #pragma once inline __device__ uint32_t isBitAt(const uint32_t& numb, const int pos) { - return (numb & (1 << (pos))); + return (numb & (1 << (pos))); } - - - /*************************************** * MinMaxes kernel * ********************************/ - /* +/* - iteration over metadata - becouse metadata may be small and to maximize occupancy we use linear index and then clalculate xMeta,ymeta,zMeta from this linear index ... - */ +iteration over metadata - becouse metadata may be small and to maximize occupancy we use linear index and then +clalculate xMeta,ymeta,zMeta from this linear index ... +*/ #pragma once template -__global__ void getMinMaxes(ForBoolKernelArgs fbArgs - , unsigned int* minMaxes - , TYO* goldArr - , TYO* segmArr - , MetaDataGPU metaData -) { - - thread_block cta = this_thread_block(); - - //shared memory - __shared__ bool anyInGold[1]; - //__shared__ uint32_t reduction_s[32]; - //1)maxX 2)minX 3)maxY 4) minY 5) maxZ 6) minZ - __shared__ unsigned int minMaxesInShmem[7]; - - if ((threadIdx.x == 1) && (threadIdx.y == 0)) { minMaxesInShmem[1] = 0; }; - if ((threadIdx.x == 2) && (threadIdx.y == 0)) { minMaxesInShmem[2] = 1000; }; - - if ((threadIdx.x == 3) && (threadIdx.y == 0)) { minMaxesInShmem[3] = 0; }; - if ((threadIdx.x == 4) && (threadIdx.y == 0)) { minMaxesInShmem[4] = 1000; }; +__global__ void getMinMaxes( + ForBoolKernelArgs fbArgs, + unsigned int* minMaxes, + TYO* goldArr, + TYO* segmArr, + MetaDataGPU metaData) { + thread_block cta = this_thread_block(); + + // shared memory + __shared__ bool anyInGold[1]; + //__shared__ uint32_t reduction_s[32]; + // 1)maxX 2)minX 3)maxY 4) minY 5) maxZ 6) minZ + __shared__ unsigned int minMaxesInShmem[7]; + + if ((threadIdx.x == 1) && (threadIdx.y == 0)) { + minMaxesInShmem[1] = 0; + }; + if ((threadIdx.x == 2) && (threadIdx.y == 0)) { + minMaxesInShmem[2] = 1000; + }; + + if ((threadIdx.x == 3) && (threadIdx.y == 0)) { + minMaxesInShmem[3] = 0; + }; + if ((threadIdx.x == 4) && (threadIdx.y == 0)) { + minMaxesInShmem[4] = 1000; + }; + + if ((threadIdx.x == 5) && (threadIdx.y == 0)) { + minMaxesInShmem[5] = 0; + }; + if ((threadIdx.x == 6) && (threadIdx.y == 0)) { + minMaxesInShmem[6] = 1000; + }; + + if ((threadIdx.x == 7) && (threadIdx.y == 0)) { + anyInGold[1] = false; + }; + + if ((threadIdx.x == 1) && (threadIdx.y == 0) && (blockIdx.x == 0)) { + } + + __syncthreads(); + + // main metadata iteration + for (auto linIdexMeta = blockIdx.x; linIdexMeta < metaData.totalMetaLength; linIdexMeta += gridDim.x) { + // we get from linear index the coordinates of the metadata block of intrest + int xMeta = linIdexMeta % metaData.metaXLength; + int zMeta = int(floor((float)(linIdexMeta / (fbArgs.metaData.metaXLength * metaData.MetaYLength)))); + int yMeta = int(floor(( + float)((linIdexMeta - ((zMeta * metaData.metaXLength * metaData.MetaYLength) + xMeta)) / metaData.metaXLength))); + // iterating over data block + for (uint8_t xLoc = threadIdx.x; xLoc < 32; xLoc += blockDim.x) { + uint32_t x = xMeta * fbArgs.dbXLength + xLoc; // absolute position + for (uint8_t yLoc = threadIdx.y; yLoc < fbArgs.dbYLength; yLoc += blockDim.y) { + uint32_t y = yMeta * fbArgs.dbYLength + yLoc; // absolute position + + if (y < fbArgs.Ny && x < fbArgs.Nx) { + // resetting + for (uint8_t zLoc = 0; zLoc < fbArgs.dbZLength; zLoc++) { + uint32_t z = zMeta * fbArgs.dbZLength + zLoc; // absolute position + if (z < fbArgs.Nz) { + // first array gold + // uint8_t& zLocRef = zLoc; uint8_t& yLocRef = yLoc; uint8_t& xLocRef = xLoc; + + // setting bits + bool goldBool = goldArr[x + y * fbArgs.Nx + z * fbArgs.Nx * fbArgs.Ny] == + fbArgs.compare_values; // (getTensorRow(tensorslice, fbArgs.goldArr, fbArgs.goldArr.Ny, y, z)[x] + // == fbArgs.compare_values); + bool segmBool = segmArr[x + y * fbArgs.Nx + z * fbArgs.Nx * fbArgs.Ny] == fbArgs.compare_values; + if (goldBool || segmBool) { + anyInGold[0] = true; + } + } + } + } - if ((threadIdx.x == 5) && (threadIdx.y == 0)) { minMaxesInShmem[5] = 0; }; - if ((threadIdx.x == 6) && (threadIdx.y == 0)) { minMaxesInShmem[6] = 1000; }; + // __syncthreads(); + // waiting so shared memory will be loaded evrywhere + // on single thread we do last sum reduction - if ((threadIdx.x == 7) && (threadIdx.y == 0)) { anyInGold[1] = false; }; + /////////////////// setting min and maxes + // //1)maxX 2)minX 3)maxY 4) minY 5) maxZ 6) minZ + __syncthreads(); + if ((threadIdx.x == 0) && (threadIdx.y == 0) && anyInGold[0]) { + minMaxesInShmem[1] = max(xMeta, minMaxesInShmem[1]); + }; + if ((threadIdx.x == 1) && (threadIdx.y == 0) && anyInGold[0]) { + minMaxesInShmem[2] = min(xMeta, minMaxesInShmem[2]); + }; - if ((threadIdx.x == 1) && (threadIdx.y == 0) && (blockIdx.x == 0)) { + if ((threadIdx.x == 2) && (threadIdx.y == 0) && anyInGold[0]) { + minMaxesInShmem[3] = max(yMeta, minMaxesInShmem[3]); + }; + if ((threadIdx.x == 3) && (threadIdx.y == 0) && anyInGold[0]) { + minMaxesInShmem[4] = min(yMeta, minMaxesInShmem[4]); + }; + if ((threadIdx.x == 4) && (threadIdx.y == 0) && anyInGold[0]) { + minMaxesInShmem[5] = max(zMeta, minMaxesInShmem[5]); + }; + if ((threadIdx.x == 5) && (threadIdx.y == 0) && anyInGold[0]) { + minMaxesInShmem[6] = min(zMeta, minMaxesInShmem[6]); + }; + __syncthreads(); // just to reduce the warp divergence + anyInGold[0] = false; + } } + } + __syncthreads(); - __syncthreads(); - - //main metadata iteration - for (auto linIdexMeta = blockIdx.x; linIdexMeta < metaData.totalMetaLength; linIdexMeta += gridDim.x) { - //we get from linear index the coordinates of the metadata block of intrest - int xMeta = linIdexMeta % metaData.metaXLength; - int zMeta = int(floor((float)(linIdexMeta / (fbArgs.metaData.metaXLength * metaData.MetaYLength)))); - int yMeta = int(floor((float)((linIdexMeta - ((zMeta * metaData.metaXLength * metaData.MetaYLength) + xMeta)) / metaData.metaXLength))); - //iterating over data block - for (uint8_t xLoc = threadIdx.x; xLoc < 32; xLoc += blockDim.x) { - uint32_t x = xMeta * fbArgs.dbXLength + xLoc;//absolute position - for (uint8_t yLoc = threadIdx.y; yLoc < fbArgs.dbYLength; yLoc += blockDim.y) { - uint32_t y = yMeta * fbArgs.dbYLength + yLoc;//absolute position - - if (y < fbArgs.Ny && x < fbArgs.Nx) { - - // resetting - for (uint8_t zLoc = 0; zLoc < fbArgs.dbZLength; zLoc++) { - uint32_t z = zMeta * fbArgs.dbZLength + zLoc;//absolute position - if (z < fbArgs.Nz) { - //first array gold - //uint8_t& zLocRef = zLoc; uint8_t& yLocRef = yLoc; uint8_t& xLocRef = xLoc; - - // setting bits - bool goldBool = goldArr[x + y * fbArgs.Nx + z * fbArgs.Nx * fbArgs.Ny] == fbArgs.compare_values; // (getTensorRow(tensorslice, fbArgs.goldArr, fbArgs.goldArr.Ny, y, z)[x] == fbArgs.compare_values); - bool segmBool = segmArr[x + y * fbArgs.Nx + z * fbArgs.Nx * fbArgs.Ny] == fbArgs.compare_values; - if (goldBool || segmBool) { - anyInGold[0] = true; - } - } - } - } - - // __syncthreads(); - //waiting so shared memory will be loaded evrywhere - //on single thread we do last sum reduction - - /////////////////// setting min and maxes - // //1)maxX 2)minX 3)maxY 4) minY 5) maxZ 6) minZ - __syncthreads(); - - if ((threadIdx.x == 0) && (threadIdx.y == 0) && anyInGold[0]) { minMaxesInShmem[1] = max(xMeta, minMaxesInShmem[1]); }; - if ((threadIdx.x == 1) && (threadIdx.y == 0) && anyInGold[0]) { minMaxesInShmem[2] = min(xMeta, minMaxesInShmem[2]); }; + auto active = coalesced_threads(); - if ((threadIdx.x == 2) && (threadIdx.y == 0) && anyInGold[0]) { + if ((threadIdx.x == 1) && (threadIdx.y == 0)) { + atomicMax(&minMaxes[1], minMaxesInShmem[1]); + }; - minMaxesInShmem[3] = max(yMeta, minMaxesInShmem[3]); + if ((threadIdx.x == 0) && (threadIdx.y == 0)) { + atomicMin(&minMaxes[2], minMaxesInShmem[2]); + }; - }; - if ((threadIdx.x == 3) && (threadIdx.y == 0) && anyInGold[0]) { minMaxesInShmem[4] = min(yMeta, minMaxesInShmem[4]); }; + if ((threadIdx.x == 1) && (threadIdx.y == 0)) { + atomicMax(&minMaxes[3], minMaxesInShmem[3]); + }; - if ((threadIdx.x == 4) && (threadIdx.y == 0) && anyInGold[0]) { minMaxesInShmem[5] = max(zMeta, minMaxesInShmem[5]); }; - if ((threadIdx.x == 5) && (threadIdx.y == 0) && anyInGold[0]) { - minMaxesInShmem[6] = min(zMeta, minMaxesInShmem[6]); - }; - __syncthreads(); // just to reduce the warp divergence - anyInGold[0] = false; + if ((threadIdx.x == 2) && (threadIdx.y == 0)) { + atomicMin(&minMaxes[4], minMaxesInShmem[4]); + }; + if (threadIdx.x == 3 && threadIdx.y == 0) { + atomicMax(&minMaxes[5], minMaxesInShmem[5]); + }; + if (threadIdx.x == 4 && threadIdx.y == 0) { + atomicMin(&minMaxes[6], minMaxesInShmem[6]); + }; +} +/*************************************** + * boolPrepareKernel + * ********************************/ +/* +iteration over metadata - becouse metadata may be small and to maximize occupancy we use linear index and then +clalculate xMeta,ymeta,zMeta from this linear index ... +*/ +#pragma once +template +__global__ void boolPrepareKernel( + ForBoolKernelArgs fbArgs, + MetaDataGPU metaData, + uint32_t* origArrs, + uint32_t* metaDataArr, + TYO* goldArr, + TYO* segmArr, + unsigned int* minMaxes) { + ////////////some initializations + bool goldBool = false; + bool segmBool = false; + bool isNotEmpty = false; + + thread_block cta = this_thread_block(); + thread_block_tile<32> tile = tiled_partition<32>(cta); + uint32_t sumFp = 0; + uint32_t sumFn = 0; + + // shared memory + + // TODO() make it dynamically sized + __shared__ uint32_t sharedForGold[1024]; + __shared__ uint32_t sharedForSegm[1024]; + + // for storing fp and fn sums to later accumulate it to global values + __shared__ uint32_t fpSFnS[2]; + __shared__ uint32_t localBlockMetaData[20]; + + __shared__ bool anyInGold[1]; + __shared__ bool anyInSegm[1]; + //__shared__ uint32_t reduction_s[32]; + // 1)maxX 2)minX 3)maxY 4) minY 5) maxZ 6) minZ + __shared__ int minMaxesInShmem[7]; + + if ((threadIdx.x == 1) && (threadIdx.y == 1)) { + fpSFnS[0] = 0; + }; + if ((threadIdx.x == 2) && (threadIdx.y == 1)) { + fpSFnS[1] = 0; + }; + if ((threadIdx.x == 3) && (threadIdx.y == 1)) { + anyInGold[1] = false; + }; + if ((threadIdx.x == 4) && (threadIdx.y == 1)) { + anyInSegm[1] = false; + }; + + __syncthreads(); + + ///////////////////////// + + // main metadata iteration + for (uint32_t linIdexMeta = blockIdx.x; linIdexMeta < metaData.totalMetaLength; linIdexMeta += gridDim.x) { + // we get from linear index the coordinates of the metadata block of intrest + int xMeta = int(linIdexMeta % (metaData.metaXLength)); + int zMeta = int(floor((float)(linIdexMeta / (metaData.metaXLength * metaData.MetaYLength)))); + int yMeta = int(floor(( + float)((linIdexMeta - ((zMeta * metaData.metaXLength * metaData.MetaYLength) + xMeta)) / metaData.metaXLength))); + // reset + isNotEmpty = false; + sumFp = 0; + sumFn = 0; + anyInGold[0] = false; + anyInSegm[0] = false; + // iterating over data block + __syncthreads(); + for (uint8_t xLoc = threadIdx.x; xLoc < fbArgs.dbXLength; xLoc += blockDim.x) { + uint32_t x = (xMeta + metaData.minX) * fbArgs.dbXLength + xLoc; // absolute position + for (uint8_t yLoc = threadIdx.y; yLoc < fbArgs.dbYLength; yLoc += blockDim.y) { + uint32_t y = (yMeta + metaData.minY) * fbArgs.dbYLength + yLoc; // absolute position + if (y < fbArgs.Ny && x < fbArgs.Nx) { + // resetting + sharedForGold[xLoc + yLoc * fbArgs.dbXLength] = 0; + sharedForSegm[xLoc + yLoc * fbArgs.dbXLength] = 0; + + for (uint8_t zLoc = 0; zLoc < fbArgs.dbZLength; zLoc++) { + uint32_t z = (zMeta + metaData.minZ) * fbArgs.dbZLength + zLoc; // absolute position + if (z < fbArgs.Nz) { + // char* tensorslice; + + // first array gold + bool goldBool = goldArr[x + y * fbArgs.Nx + z * fbArgs.Nx * fbArgs.Ny] == fbArgs.compare_values; + bool segmBool = segmArr[x + y * fbArgs.Nx + z * fbArgs.Nx * fbArgs.Ny] == fbArgs.compare_values; + // goldBool = true; + + // setting bits + sharedForGold[xLoc + yLoc * fbArgs.dbXLength] |= goldBool << zLoc; + sharedForSegm[xLoc + yLoc * fbArgs.dbXLength] |= segmBool << zLoc; + // setting value of local boolean marking that any of the entries was evaluated to true in either of + // arrays + isNotEmpty = (isNotEmpty || (goldBool || segmBool)); + sumFp += (!goldBool && segmBool); + sumFn += (goldBool && !segmBool); + if (goldBool) + anyInGold[0] = true; + if (segmBool) + anyInSegm[0] = true; } + } } + } + } + // reset local metadata + if ((threadIdx.x < 20) && (threadIdx.y == 0)) { + localBlockMetaData[threadIdx.x] = 0; + } + isNotEmpty = __syncthreads_or(isNotEmpty); + // exporting to global memory + for (uint8_t xLoc = threadIdx.x; xLoc < fbArgs.dbXLength; xLoc += blockDim.x) { + uint32_t x = (xMeta + metaData.minX) * fbArgs.dbXLength + xLoc; // absolute position + for (uint8_t yLoc = threadIdx.y; yLoc < fbArgs.dbYLength; yLoc += blockDim.y) { + uint32_t y = (yMeta + metaData.minY) * fbArgs.dbYLength + yLoc; // absolute position + if (y < fbArgs.Ny && x < fbArgs.Nx) { + origArrs[linIdexMeta * metaData.mainArrSectionLength + yLoc * 32 + xLoc] = sharedForGold[yLoc * 32 + xLoc]; + origArrs[linIdexMeta * metaData.mainArrSectionLength + yLoc * 32 + xLoc + metaData.mainArrXLength] = + sharedForSegm[yLoc * 32 + xLoc]; + } + } } + __syncthreads(); + /////adding the block and total number of the Fp's and Fn's + sumFp = reduce(tile, sumFp, plus()); + sumFn = reduce(tile, sumFn, plus()); + // reusing shared memory and adding accumulated values from tiles + if (tile.thread_rank() == 0) { + sharedForGold[tile.meta_group_rank()] = sumFp; + sharedForSegm[tile.meta_group_rank()] = sumFn; + } + __syncthreads(); // waiting so shared memory will be loaded evrywhere + // on single thread we do last sum reduction auto active = coalesced_threads(); - if ((threadIdx.x == 1) && (threadIdx.y == 0)) { - atomicMax(&minMaxes[1], minMaxesInShmem[1]); - }; - - if ((threadIdx.x == 0) && (threadIdx.y == 0)) { + if ((threadIdx.x == 0) && (threadIdx.y == 0) && isNotEmpty) { + sharedForGold[33] = 0; // reset + for (int i = 0; i < tile.meta_group_size(); i += 1) { + sharedForGold[33] += sharedForGold[i]; + }; + fpSFnS[0] += sharedForGold[33]; // will be needed later for global set + localBlockMetaData[1] = sharedForGold[33]; + } + if ((threadIdx.x == 0) && (threadIdx.y == 1) && isNotEmpty) { + sharedForSegm[33] = 0; // reset + for (int i = 0; i < tile.meta_group_size(); i += 1) { + sharedForSegm[33] += sharedForSegm[i]; + }; + fpSFnS[1] += sharedForSegm[33]; // will be needed later for global set + // setting metadata + localBlockMetaData[2] = sharedForSegm[33]; + } - atomicMin(&minMaxes[2], minMaxesInShmem[2]); + // marking as active + // FP pass + if ((threadIdx.x == 0) && (threadIdx.y == 0) && isNotEmpty && anyInGold[0]) { + localBlockMetaData[7] = 1; }; - - if ((threadIdx.x == 1) && (threadIdx.y == 0)) { - atomicMax(&minMaxes[3], minMaxesInShmem[3]); - + // FN pass + if ((threadIdx.x == 1) && (threadIdx.y == 0) && isNotEmpty && anyInSegm[0]) { + localBlockMetaData[9] = 1; }; - if ((threadIdx.x == 2) && (threadIdx.y == 0)) { - atomicMin(&minMaxes[4], minMaxesInShmem[4]); + // after we streamed over all block we save also information about indicies of the surrounding blocks - given they + // are in range if not UINT32_MAX will be saved top + + setNeighbourBlocks( + fbArgs, + 3, + 13, + (zMeta > 0), + (-(metaData.metaXLength * metaData.MetaYLength)), + linIdexMeta, + metaData, + localBlockMetaData); // top + setNeighbourBlocks( + fbArgs, + 4, + 14, + (zMeta < (metaData.MetaZLength - 1)), + (metaData.metaXLength * metaData.MetaYLength), + linIdexMeta, + metaData, + localBlockMetaData); // bottom + + setNeighbourBlocks(fbArgs, 6, 15, (xMeta > 0), (-1), linIdexMeta, metaData, localBlockMetaData); // left + setNeighbourBlocks( + fbArgs, 7, 16, (xMeta < (metaData.metaXLength - 1)), 1, linIdexMeta, metaData, localBlockMetaData); // right + + setNeighbourBlocks( + fbArgs, + 8, + 17, + (yMeta < (metaData.MetaYLength - 1)), + metaData.metaXLength, + linIdexMeta, + metaData, + localBlockMetaData); // anterior + setNeighbourBlocks( + fbArgs, 9, 18, (yMeta > 0), (-metaData.metaXLength), linIdexMeta, metaData, localBlockMetaData); // posterior + + if ((threadIdx.x < 20) && (threadIdx.y == 0)) { + metaDataArr[linIdexMeta * metaData.metaDataSectionLength + threadIdx.x] = localBlockMetaData[threadIdx.x]; }; + __syncthreads(); // just to reduce the warp divergence + } + __syncthreads(); - if (threadIdx.x == 3 && threadIdx.y == 0) { - atomicMax(&minMaxes[5], minMaxesInShmem[5]); - }; + // setting global fp and fn + if ((threadIdx.x == 0) && (threadIdx.y == 0)) { + atomicAdd(&(minMaxes[7]), fpSFnS[0]); + }; + + if ((threadIdx.x == 1) && (threadIdx.y == 0)) { + atomicAdd(&(minMaxes[8]), fpSFnS[1]); + }; +} - if (threadIdx.x == 4 && threadIdx.y == 0) { - atomicMin(&minMaxes[6], minMaxesInShmem[6]); +/*************************************** + * firstMetaPrepareKernel + * ********************************/ +/* +we add here to appropriate queue data about metadata of blocks of intrest +minMaxesPos- marks in minmaxes the postion of global offset counter -12) global FP offset 13) global FnOffset +offsetMetadataArr- arrays from metadata holding data about result list offsets it can be either fbArgs.metaData.fpOffset +or fbArgs.metaData.fnOffset +*/ +#pragma once +__device__ inline void addToQueue( + uint32_t linIdexMeta, + uint8_t isGold, + unsigned int fpFnLocCounter[1], + uint32_t localWorkQueue[1600], + uint32_t localOffsetQueue[1600], + unsigned int localWorkQueueCounter[1], + uint8_t countIndexNumb, + uint8_t isActiveIndexNumb, + uint8_t offsetIndexNumb, + uint32_t* metaDataArr, + MetaDataGPU metaData, + unsigned int* minMaxes, + uint32_t* workQueue) { + unsigned int count = metaDataArr[linIdexMeta * metaData.metaDataSectionLength + countIndexNumb]; + // given fp is non zero we need to add this to local queue + if (metaDataArr[linIdexMeta * metaData.metaDataSectionLength + isActiveIndexNumb] == 1) { + // printf("adding to local in first meta pass linIdexMeta %d isGold %d isActiveIndexNumb %d \n ", linIdexMeta, + // isGold, isActiveIndexNumb); + + count = atomicAdd_block(&fpFnLocCounter[0], count); + unsigned int old = atomicAdd_block(&localWorkQueueCounter[0], 1); + // we check weather we still have space in shared memory + if (old < 1590) { // so we still have space in shared memory + // will be equal or above isGoldOffset if it is gold pass + localWorkQueue[old] = linIdexMeta + (isGoldOffset * isGold); + localOffsetQueue[old] = uint32_t(count); + } else { // so we do not have any space more in the sared memory - it is unlikely so we will just in this case save + // immidiately to global memory + old = atomicAdd(&(minMaxes[9]), old); + // workQueue + workQueue[old] = linIdexMeta + (isGoldOffset * isGold); + // and offset + metaDataArr[linIdexMeta * metaData.metaDataSectionLength + offsetIndexNumb] = atomicAdd(&(minMaxes[12]), count); }; + } } +#pragma once +template +__global__ void firstMetaPrepareKernel( + ForBoolKernelArgs fbArgs, + MetaDataGPU metaData, + unsigned int* minMaxes, + uint32_t* workQueue, + uint32_t* origArrs, + uint32_t* metaDataArr) { + //////initializations + thread_block cta = this_thread_block(); + char* tensorslice; // needed for iterations over 3d arrays + // local offset counters for fp and fn's + __shared__ unsigned int fpFnLocCounter[1]; + // used to store the start position in global memory for whole block + __shared__ unsigned int globalOffsetForBlock[1]; + __shared__ unsigned int globalWorkQueueCounter[1]; + // used as local work queue counter + __shared__ unsigned int localWorkQueueCounter[1]; + // according to https://forums.developer.nvidia.com/t/find-the-limit-of-shared-memory-that-can-be-used-per-block/48556 + // it is good to keep shared memory below 16kb kilo bytes so it will give us 1600 length of shared memory so here we + // will store locally the calculated offsets and coordinates of meta data block of intrest marking also wheather we are + // talking about gold or segmentation pass (fp or fn ) + __shared__ uint32_t localWorkQueue[1600]; + __shared__ uint32_t localOffsetQueue[1600]; + if ((threadIdx.x == 0)) { + fpFnLocCounter[0] = 0; + } + if ((threadIdx.x == 1)) { + localWorkQueueCounter[0] = 0; + } + if ((threadIdx.x == 2)) { + globalWorkQueueCounter[0] = 0; + } + if ((threadIdx.x == 3)) { + globalOffsetForBlock[0] = 0; + } + __syncthreads(); + + // classical grid stride loop - in case of unlikely event we will run out of space we will empty it prematurly + // main metadata iteration + for (uint32_t linIdexMeta = blockIdx.x * blockDim.x + threadIdx.x; linIdexMeta < metaData.totalMetaLength; + linIdexMeta += blockDim.x * gridDim.x) { + // goldpass + addToQueue( + linIdexMeta, + 0, + fpFnLocCounter, + localWorkQueue, + localOffsetQueue, + localWorkQueueCounter, + 1, + 9, + 6, + metaDataArr, + metaData, + minMaxes, + workQueue); + // segmPass + addToQueue( + linIdexMeta, + 1, + fpFnLocCounter, + localWorkQueue, + localOffsetQueue, + localWorkQueueCounter, + 2, + 7, + 5, + metaDataArr, + metaData, + minMaxes, + workQueue); + } + __syncthreads(); + if ((threadIdx.x == 0)) { + globalOffsetForBlock[0] = atomicAdd(&(minMaxes[12]), (fpFnLocCounter[0])); + }; + if ((threadIdx.x == 1)) { + if (localWorkQueueCounter[0] > 0) { + globalWorkQueueCounter[0] = atomicAdd(&(minMaxes[9]), (localWorkQueueCounter[0])); + } + } + __syncthreads(); + + // setting offsets + for (uint32_t i = threadIdx.x; i < localWorkQueueCounter[0]; i += blockDim.x) { + workQueue[globalWorkQueueCounter[0] + i] = localWorkQueue[i]; + + // FP pass + if (localWorkQueue[i] >= isGoldOffset) { + metaDataArr[(localWorkQueue[i] - isGoldOffset) * metaData.metaDataSectionLength + 5] = + localOffsetQueue[i] + globalOffsetForBlock[0]; + } + // FN pass + else { + metaDataArr[(localWorkQueue[i]) * metaData.metaDataSectionLength + 6] = + localOffsetQueue[i] + globalOffsetForBlock[0]; + }; + } +}; /*************************************** - * boolPrepareKernel + * memory allocations * ********************************/ - /* -iteration over metadata - becouse metadata may be small and to maximize occupancy we use linear index and then clalculate xMeta,ymeta,zMeta from this linear index ... +Get arguments for kernels */ #pragma once -template -__global__ void boolPrepareKernel(ForBoolKernelArgs fbArgs - , MetaDataGPU metaData, uint32_t* origArrs, uint32_t* metaDataArr - , TYO* goldArr - , TYO* segmArr - , unsigned int* minMaxes) { - - ////////////some initializations - bool goldBool = false; - bool segmBool = false; - bool isNotEmpty = false; +template +inline ForBoolKernelArgs getArgsForKernel( + ForFullBoolPrepArgs& mainFunArgs, + int& warpsNumbForMainPass, + int& blockForMainPass, + const int xLen, + const int yLen, + const int zLen, + cudaStream_t stream) { + mainFunArgs.Nx = xLen; + mainFunArgs.Ny = yLen; + mainFunArgs.Nz = zLen; + + unsigned int* minMaxes; + size_t sizeminMaxes = sizeof(unsigned int) * 20; + cudaMallocAsync(&minMaxes, sizeminMaxes, stream); + ForBoolKernelArgs res; + + res.Nx = xLen; + res.Ny = yLen; + res.Nz = zLen; + + res.metaData = allocateMetaDataOnGPU(mainFunArgs.metaData, minMaxes); + res.metaData.minMaxes = minMaxes; + res.minMaxes = minMaxes; + res.compare_values = mainFunArgs.compare_values; + res.dbXLength = 32; + res.dbYLength = warpsNumbForMainPass; + res.dbZLength = 32; + + // printf("in setting bool args ylen %d dbYlen %d calculated meta %d \n ", yLen, res.dbYLength, int(ceil(yLen / + // res.dbYLength))); + res.metaData.metaXLength = int(ceil(xLen / res.dbXLength)); + res.metaData.MetaYLength = int(ceil(yLen / res.dbYLength)); + ; + res.metaData.MetaZLength = int(ceil(zLen / res.dbZLength)); + ; + res.metaData.minX = 0; + res.metaData.minY = 0; + res.metaData.minZ = 0; + res.metaData.maxX = res.metaData.metaXLength; + res.metaData.maxY = res.metaData.MetaYLength; + res.metaData.maxZ = res.metaData.MetaZLength; + + res.metaData.totalMetaLength = res.metaData.metaXLength * res.metaData.MetaYLength * res.metaData.MetaZLength; + res.goldArr = mainFunArgs.goldArr; + res.segmArr = mainFunArgs.segmArr; + + return res; +} - thread_block cta = this_thread_block(); - thread_block_tile<32> tile = tiled_partition<32>(cta); - uint32_t sumFp = 0; - uint32_t sumFn = 0; +#pragma once +/* +allocate memory after first kernel +*/ +template +inline MetaDataGPU allocateMemoryAfterMinMaxesKernel( + ForBoolKernelArgs& gpuArgs, + ForFullBoolPrepArgs& cpuArgs, + cudaStream_t stream) { + ////reduced arrays + uint32_t* origArr; + uint32_t* metaDataArr; + uint32_t* workQueue; + // copy on cpu + size_t size = sizeof(unsigned int) * 20; + cudaMemcpyAsync(cpuArgs.metaData.minMaxes, gpuArgs.minMaxes, size, cudaMemcpyDeviceToHost, stream); + + // read an modify + // 1)maxX 2)minX 3)maxY 4) minY 5) maxZ 6) minZ + // 7)global FP count; 8)global FN count + unsigned int xRange = cpuArgs.metaData.minMaxes[1] - cpuArgs.metaData.minMaxes[2] + 1; + unsigned int yRange = cpuArgs.metaData.minMaxes[3] - cpuArgs.metaData.minMaxes[4] + 1; + unsigned int zRange = cpuArgs.metaData.minMaxes[5] - cpuArgs.metaData.minMaxes[6] + 1; + unsigned int totalMetaLength = (xRange) * (yRange) * (zRange); + // updating size informations + gpuArgs.metaData.metaXLength = xRange; + gpuArgs.metaData.MetaYLength = yRange; + gpuArgs.metaData.MetaZLength = zRange; + gpuArgs.metaData.totalMetaLength = totalMetaLength; + // saving min maxes + gpuArgs.metaData.maxX = cpuArgs.metaData.minMaxes[1]; + gpuArgs.metaData.minX = cpuArgs.metaData.minMaxes[2]; + gpuArgs.metaData.maxY = cpuArgs.metaData.minMaxes[3]; + gpuArgs.metaData.minY = cpuArgs.metaData.minMaxes[4]; + gpuArgs.metaData.maxZ = cpuArgs.metaData.minMaxes[5]; + gpuArgs.metaData.minZ = cpuArgs.metaData.minMaxes[6]; + + // allocating needed memory + // main array + unsigned int mainArrXLength = gpuArgs.dbXLength * gpuArgs.dbYLength; + unsigned int mainArrSectionLength = (mainArrXLength * 2); + gpuArgs.metaData.mainArrXLength = mainArrXLength; + gpuArgs.metaData.mainArrSectionLength = mainArrSectionLength; + + size_t sizeB = totalMetaLength * mainArrSectionLength * sizeof(uint32_t); + // cudaMallocAsync(&mainArr, sizeB, 0); + size_t sizeorigArr = totalMetaLength * (mainArrXLength * 2) * sizeof(uint32_t); + cudaMallocAsync(&origArr, sizeorigArr, stream); + size_t sizemetaDataArr = totalMetaLength * (20) * sizeof(uint32_t) + 100; + cudaMallocAsync(&metaDataArr, sizemetaDataArr, stream); + size_t sizeC = (totalMetaLength * 2 * sizeof(uint32_t) + 50); + cudaMallocAsync(&workQueue, sizeC, stream); + gpuArgs.origArrsPointer = origArr; + gpuArgs.metaDataArrPointer = metaDataArr; + gpuArgs.workQueuePointer = workQueue; + return gpuArgs.metaData; +}; - //shared memory +/* +becouse we need a lot of the additional memory spaces to minimize memory consumption allocations will be postponed after +first kernel run enabling +*/ +#pragma once +template +inline int allocateMemoryAfterBoolKernel( + ForBoolKernelArgs& gpuArgs, + ForFullBoolPrepArgs& cpuArgs, + cudaStream_t stream, + bool resIterNeeded) { + int32_t* resultListPointerMeta; + int32_t* resultListPointerLocal; + int32_t* resultListPointerIterNumb; + uint32_t* mainArrAPointer; + uint32_t* mainArrBPointer; + // copy on cpu + size_t size = sizeof(unsigned int) * 20; + cudaMemcpyAsync(cpuArgs.metaData.minMaxes, gpuArgs.metaData.minMaxes, size, cudaMemcpyDeviceToHost, stream); + + unsigned int fpPlusFn = cpuArgs.metaData.minMaxes[7] + cpuArgs.metaData.minMaxes[8]; + size = sizeof(int32_t) * (fpPlusFn + 50); + + cudaMallocAsync(&resultListPointerLocal, size, stream); + cudaMallocAsync(&resultListPointerIterNumb, size, stream); + cudaMallocAsync(&resultListPointerMeta, size, stream); + + // we will allocate only if we need + if (resIterNeeded) { + auto options = torch::TensorOptions().dtype(torch::kInt32).device(torch::kCUDA, 0).requires_grad(false); + + auto resultListPointerIterNumbTensor = torch::empty((fpPlusFn + 50), options); + gpuArgs.resultListPointerIterNumbTensor = resultListPointerIterNumbTensor; + } + + auto xRange = gpuArgs.metaData.metaXLength; + auto yRange = gpuArgs.metaData.MetaYLength; + auto zRange = gpuArgs.metaData.MetaZLength; + + size_t sizeB = gpuArgs.metaData.totalMetaLength * gpuArgs.metaData.mainArrSectionLength * sizeof(uint32_t); + + cudaMallocAsync(&mainArrAPointer, sizeB, 0); + cudaMemcpyAsync(mainArrAPointer, gpuArgs.origArrsPointer, sizeB, cudaMemcpyDeviceToDevice, stream); + + cudaMallocAsync(&mainArrBPointer, sizeB, 0); + cudaMemcpyAsync(mainArrBPointer, gpuArgs.origArrsPointer, sizeB, cudaMemcpyDeviceToDevice, stream); + + // just in order set it to 0 + int32_t* resultListPointerMetaCPU = (int32_t*)calloc(fpPlusFn + 50, sizeof(int32_t)); + cudaMemcpyAsync(resultListPointerMeta, resultListPointerMetaCPU, size, cudaMemcpyHostToDevice, stream); + cudaMemcpyAsync(resultListPointerIterNumb, resultListPointerMetaCPU, size, cudaMemcpyHostToDevice, stream); + free(resultListPointerMetaCPU); + + gpuArgs.resultListPointerMeta = resultListPointerMeta; + gpuArgs.resultListPointerLocal = resultListPointerLocal; + gpuArgs.resultListPointerIterNumb = resultListPointerIterNumb; + + gpuArgs.mainArrAPointer = mainArrAPointer; + gpuArgs.mainArrBPointer = mainArrBPointer; + + return fpPlusFn; +}; - //TODO() make it dynamically sized - __shared__ uint32_t sharedForGold[1024]; - __shared__ uint32_t sharedForSegm[1024]; +/*************************************** + * main kernel + * ********************************/ +template +inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { + thread_block cta = cooperative_groups::this_thread_block(); + grid_group grid = cooperative_groups::this_grid(); - //for storing fp and fn sums to later accumulate it to global values - __shared__ uint32_t fpSFnS[2]; - __shared__ uint32_t localBlockMetaData[20]; + /* + * according to https://forums.developer.nvidia.com/t/find-the-limit-of-shared-memory-that-can-be-used-per-block/48556 + it is good to keep shared memory below 16kb kilo bytes main shared memory spaces 0-1023 : sourceShmem 1024-2047 : + resShmem 2048-3071 : first register space 3072-4095 : second register space 4096- 4127: small 32 length resgister 3 + space 4128-4500 (372 length) : place for local work queue in dilatation kernels + */ + // __shared__ uint32_t mainShmem[lengthOfMainShmem]; + __shared__ uint32_t mainShmem[lengthOfMainShmem]; + // cuda::associate_access_property(&mainShmem, cuda::access_property::shared{}); - __shared__ bool anyInGold[1]; - __shared__ bool anyInSegm[1]; - //__shared__ uint32_t reduction_s[32]; - //1)maxX 2)minX 3)maxY 4) minY 5) maxZ 6) minZ - __shared__ int minMaxesInShmem[7]; + constexpr size_t stages_count = 2; // Pipeline stages number - if ((threadIdx.x == 1) && (threadIdx.y == 1)) { fpSFnS[0] = 0; }; - if ((threadIdx.x == 2) && (threadIdx.y == 1)) { fpSFnS[1] = 0; }; - if ((threadIdx.x == 3) && (threadIdx.y == 1)) { anyInGold[1] = false; }; - if ((threadIdx.x == 4) && (threadIdx.y == 1)) { anyInSegm[1] = false; }; + // Allocate shared storage for a two-stage cuda::pipeline: + __shared__ cuda::pipeline_shared_state shared_state; + // cuda::pipeline pipeline = cuda::make_pipeline(cta, &shared_state); + cuda::pipeline pipeline = cuda::make_pipeline(cta, &shared_state); + // usefull for iterating through local work queue + __shared__ bool isGoldForLocQueue[localWorkQueLength]; + // holding data about paddings - __syncthreads(); + // holding data weather we have anything in padding 0)top 1)bottom, 2)left 3)right, 4)anterior, 5)posterior, + __shared__ bool isAnythingInPadding[6]; - ///////////////////////// + __shared__ bool isBlockFull[2]; + __shared__ uint32_t lastI[1]; - //main metadata iteration - for (uint32_t linIdexMeta = blockIdx.x; linIdexMeta < metaData.totalMetaLength; linIdexMeta += gridDim.x) { - //we get from linear index the coordinates of the metadata block of intrest - int xMeta = int(linIdexMeta % (metaData.metaXLength)); - int zMeta = int(floor((float)(linIdexMeta / (metaData.metaXLength * metaData.MetaYLength)))); - int yMeta = int(floor((float)((linIdexMeta - ((zMeta * metaData.metaXLength * metaData.MetaYLength) + xMeta)) / metaData.metaXLength))); - //reset - isNotEmpty = false; - sumFp = 0; - sumFn = 0; - anyInGold[0] = false; - anyInSegm[0] = false; - //iterating over data block - __syncthreads(); - for (uint8_t xLoc = threadIdx.x; xLoc < fbArgs.dbXLength; xLoc += blockDim.x) { - uint32_t x = (xMeta + metaData.minX) * fbArgs.dbXLength + xLoc;//absolute position - for (uint8_t yLoc = threadIdx.y; yLoc < fbArgs.dbYLength; yLoc += blockDim.y) { - uint32_t y = (yMeta + metaData.minY) * fbArgs.dbYLength + yLoc;//absolute position - if (y < fbArgs.Ny && x < fbArgs.Nx) { - - // resetting - sharedForGold[xLoc + yLoc * fbArgs.dbXLength] = 0; - sharedForSegm[xLoc + yLoc * fbArgs.dbXLength] = 0; - - - for (uint8_t zLoc = 0; zLoc < fbArgs.dbZLength; zLoc++) { - uint32_t z = (zMeta + metaData.minZ) * fbArgs.dbZLength + zLoc;//absolute position - if (z < fbArgs.Nz) { - //char* tensorslice; - - //first array gold - bool goldBool = goldArr[x + y * fbArgs.Nx + z * fbArgs.Nx * fbArgs.Ny] == fbArgs.compare_values; - bool segmBool = segmArr[x + y * fbArgs.Nx + z * fbArgs.Nx * fbArgs.Ny] == fbArgs.compare_values; - //goldBool = true; - - // setting bits - sharedForGold[xLoc + yLoc * fbArgs.dbXLength] |= goldBool << zLoc; - sharedForSegm[xLoc + yLoc * fbArgs.dbXLength] |= segmBool << zLoc; - // setting value of local boolean marking that any of the entries was evaluated to true in either of arrays - isNotEmpty = (isNotEmpty || (goldBool || segmBool)); - sumFp += (!goldBool && segmBool); - sumFn += (goldBool && !segmBool); - if (goldBool) anyInGold[0] = true; - if (segmBool) anyInSegm[0] = true; - - } - } - } + // variables needed for all threads + __shared__ int iterationNumb[1]; + __shared__ unsigned int globalWorkQueueOffset[1]; + __shared__ unsigned int globalWorkQueueCounter[1]; + __shared__ unsigned int localWorkQueueCounter[1]; + // keeping data wheather gold or segmentation pass should continue - on the basis of global counters - } - } - //reset local metadata - if ((threadIdx.x < 20) && (threadIdx.y == 0)) { - localBlockMetaData[threadIdx.x] = 0; - } + __shared__ unsigned int localTotalLenthOfWorkQueue[1]; + // counters for per block number of results added in this iteration + __shared__ unsigned int localFpConter[1]; + __shared__ unsigned int localFnConter[1]; + __shared__ unsigned int blockFpConter[1]; + __shared__ unsigned int blockFnConter[1]; + __shared__ unsigned int fpFnLocCounter[1]; - isNotEmpty = __syncthreads_or(isNotEmpty); - //exporting to global memory - for (uint8_t xLoc = threadIdx.x; xLoc < fbArgs.dbXLength; xLoc += blockDim.x) { - uint32_t x = (xMeta + metaData.minX) * fbArgs.dbXLength + xLoc;//absolute position - for (uint8_t yLoc = threadIdx.y; yLoc < fbArgs.dbYLength; yLoc += blockDim.y) { - uint32_t y = (yMeta + metaData.minY) * fbArgs.dbYLength + yLoc;//absolute position - if (y < fbArgs.Ny && x < fbArgs.Nx) { + // result list offset - needed to know where to write a result in a result list + __shared__ unsigned int resultfpOffset[1]; + __shared__ unsigned int resultfnOffset[1]; - origArrs[linIdexMeta * metaData.mainArrSectionLength + yLoc * 32 + xLoc] = sharedForGold[yLoc * 32 + xLoc]; - origArrs[linIdexMeta * metaData.mainArrSectionLength + yLoc * 32 + xLoc + metaData.mainArrXLength] = sharedForSegm[yLoc * 32 + xLoc]; + __shared__ unsigned int worQueueStep[1]; + /* will be used to store all of the minMaxes varibles from global memory (from 7 to 11) + 0 : global FP count; + 1 : global FN count; + 2 : workQueueCounter + 3 : resultFP globalCounter + 4 : resultFn globalCounter + */ + __shared__ unsigned int localMinMaxes[5]; - } - } + /* will be used to store all of block metadata +nothing at 0 index +1 :fpCount +2 :fnCount +3 :fpCounter +4 :fnCounter +5 :fpOffset +6 :fnOffset +7 :isActiveGold +8 :isFullGold +9 :isActiveSegm +10 :isFullSegm +11 :isToBeActivatedGold +12 :isToBeActivatedSegm +12 :isToBeActivatedSegm +//now linear indexes of the blocks in all sides - if there is no block in given direction it will equal UINT32_MAX +13 : top +14 : bottom +15 : left +16 : right +17 : anterior +18 : posterior + */ + + __shared__ uint32_t localBlockMetaData[40]; + + /* +//now linear indexes of the previous block in all sides - if there is no block in given direction it will equal +UINT32_MAX 0 : top 1 : bottom 2 : left 3 : right 4 : anterior 5 : posterior + */ + + /////used mainly in meta passes + + // __shared__ unsigned int fpFnLocCounter[1]; + __shared__ bool isGoldPassToContinue[1]; + __shared__ bool isSegmPassToContinue[1]; + + // initializations and loading + if (threadIdx.x == 9 && threadIdx.y == 0) { + iterationNumb[0] = -1; + }; + if (threadIdx.x == 11 && threadIdx.y == 0) { + isGoldPassToContinue[0] = true; + }; + if (threadIdx.x == 12 && threadIdx.y == 0) { + isSegmPassToContinue[0] = true; + }; + + // here we caclulate the offset for given block depending on length of the workqueue and number of the available + // blocks in a grid + // - this will give us number of work queue items per block - we will calculate offset on the basis of the block + // number + __syncthreads(); + + do { + for (uint8_t isPaddingPass = 0; isPaddingPass < 2; isPaddingPass++) { + /////////////////////////**************************************************************************************************************** + /// dilataions + + // initial cleaning and initializations include loading min maxes + if (threadIdx.x == 7 && threadIdx.y == 0 && !isPaddingPass) { + iterationNumb[0] += 1; + }; + + if (threadIdx.x == 6 && threadIdx.y == 0) { + localWorkQueueCounter[0] = 0; + }; + + if (threadIdx.x == 1 && threadIdx.y == 0) { + blockFpConter[0] = 0; + }; + if (threadIdx.x == 2 && threadIdx.y == 0) { + blockFnConter[0] = 0; + }; + if (threadIdx.x == 3 && threadIdx.y == 0) { + localFpConter[0] = 0; + }; + if (threadIdx.x == 4 && threadIdx.y == 0) { + localFnConter[0] = 0; + }; + if (threadIdx.x == 9 && threadIdx.y == 0) { + isBlockFull[0] = true; + }; + if (threadIdx.x == 9 && threadIdx.y == 1) { + isBlockFull[1] = true; + }; + + if (threadIdx.x == 10 && threadIdx.y == 0) { + fpFnLocCounter[0] = 0; + }; + + if (threadIdx.x == 0 && threadIdx.y == 0) { + localTotalLenthOfWorkQueue[0] = fbArgs.minMaxes[9]; + globalWorkQueueOffset[0] = floor((float)(localTotalLenthOfWorkQueue[0] / gridDim.x)) + 1; + worQueueStep[0] = min(localWorkQueLength, globalWorkQueueOffset[0]); + }; + + if (threadIdx.y == 1) { + cooperative_groups::memcpy_async( + cta, (&localMinMaxes[0]), (&fbArgs.minMaxes[7]), cuda::aligned_size_t<4>(sizeof(unsigned int) * 5)); + } + + __syncthreads(); + + /// load work QueueData into shared memory + for (uint32_t bigloop = blockIdx.x * globalWorkQueueOffset[0]; + bigloop < ((blockIdx.x + 1) * globalWorkQueueOffset[0]); + bigloop += worQueueStep[0]) { + // grid stride loop - sadly most of threads will be idle + ///////// loading to work queue + if (((bigloop) < localTotalLenthOfWorkQueue[0]) && + ((bigloop) < ((blockIdx.x + 1) * globalWorkQueueOffset[0]))) { + for (uint16_t ii = cta.thread_rank(); ii < worQueueStep[0]; ii += cta.size()) { + mainShmem[startOfLocalWorkQ + ii] = fbArgs.workQueuePointer[bigloop + ii]; + isGoldForLocQueue[ii] = (mainShmem[startOfLocalWorkQ + ii] >= isGoldOffset); + mainShmem[startOfLocalWorkQ + ii] = + mainShmem[startOfLocalWorkQ + ii] - isGoldOffset * isGoldForLocQueue[ii]; + } } + // now all of the threads in the block needs to have the same i value so we will increment by 1 we are + // preloading to the pipeline block metaData + ////##### pipeline Step 0 __syncthreads(); - /////adding the block and total number of the Fp's and Fn's - sumFp = reduce(tile, sumFp, plus()); - sumFn = reduce(tile, sumFn, plus()); - //reusing shared memory and adding accumulated values from tiles - if (tile.thread_rank() == 0) { - sharedForGold[tile.meta_group_rank()] = sumFp; - sharedForSegm[tile.meta_group_rank()] = sumFn; + // loading metadata + pipeline.producer_acquire(); + if (((bigloop) < localTotalLenthOfWorkQueue[0]) && + ((bigloop) < ((blockIdx.x + 1) * globalWorkQueueOffset[0]))) { + cuda::memcpy_async( + cta, + (&localBlockMetaData[0]), + (&fbArgs.metaDataArrPointer[mainShmem[startOfLocalWorkQ] * fbArgs.metaData.metaDataSectionLength]), + cuda::aligned_size_t<4>(sizeof(uint32_t) * 20), + pipeline); } - __syncthreads();//waiting so shared memory will be loaded evrywhere - //on single thread we do last sum reduction - auto active = coalesced_threads(); + pipeline.producer_commit(); + __syncthreads(); + for (uint32_t i = 0; i < worQueueStep[0]; i += 1) { + if (((bigloop + i) < localTotalLenthOfWorkQueue[0]) && + ((bigloop + i) < ((blockIdx.x + 1) * globalWorkQueueOffset[0]))) { + pipeline.producer_acquire(); + cuda::memcpy_async( + cta, + &mainShmem[begSourceShmem], + &getSourceReduced(fbArgs, iterationNumb) + [mainShmem[startOfLocalWorkQ + i] * fbArgs.metaData.mainArrSectionLength + + fbArgs.metaData.mainArrXLength * (1 - isGoldForLocQueue[i])], + cuda::aligned_size_t<128>(sizeof(uint32_t) * fbArgs.metaData.mainArrXLength), + pipeline); + pipeline.producer_commit(); + + // just so pipeline will work well + pipeline.consumer_wait(); + + pipeline.consumer_release(); + __syncthreads(); - if ((threadIdx.x == 0) && (threadIdx.y == 0) && isNotEmpty) { - sharedForGold[33] = 0;//reset - for (int i = 0; i < tile.meta_group_size(); i += 1) { - sharedForGold[33] += sharedForGold[i]; + ///////// step 1 load top and process main data + // load top + pipeline.producer_acquire(); + if (localBlockMetaData[(i & 1) * 20 + 13] < isGoldOffset) { + cuda::memcpy_async( + cta, + (&mainShmem[begfirstRegShmem]), + &getSourceReduced(fbArgs, iterationNumb) + [localBlockMetaData[(i & 1) * 20 + 13] * fbArgs.metaData.mainArrSectionLength + + fbArgs.metaData.mainArrXLength * (1 - isGoldForLocQueue[i])], + cuda::aligned_size_t<128>(sizeof(uint32_t) * fbArgs.metaData.mainArrXLength), + pipeline); + } + pipeline.producer_commit(); + // process main + pipeline.consumer_wait(); + // marking weather block is already full and no more dilatations are possible + if (__popc(mainShmem[begSourceShmem + threadIdx.x + threadIdx.y * 32]) < 32) { + isBlockFull[i & 1] = false; + } + mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] = + bitDilatate(mainShmem[begSourceShmem + threadIdx.x + threadIdx.y * 32]); + pipeline.consumer_release(); + + ///////// step 2 load bottom and process top + // load bottom + pipeline.producer_acquire(); + if (localBlockMetaData[(i & 1) * 20 + 14] < isGoldOffset) { + cuda::memcpy_async( + cta, + (&mainShmem[begSecRegShmem]), + &getSourceReduced(fbArgs, iterationNumb) + [localBlockMetaData[(i & 1) * 20 + 14] * fbArgs.metaData.mainArrSectionLength + + fbArgs.metaData.mainArrXLength * (1 - isGoldForLocQueue[i])], + cuda::aligned_size_t<128>(sizeof(uint32_t) * fbArgs.metaData.mainArrXLength), + pipeline); + } + pipeline.producer_commit(); + // process top + pipeline.consumer_wait(); + + if (localBlockMetaData[(i & 1) * 20 + 13] < isGoldOffset) { + if (isBitAt(mainShmem[begSourceShmem + threadIdx.x + threadIdx.y * 32], 0)) { + // printf("setting padding top val %d \n ", isAnythingInPadding[0]); + isAnythingInPadding[0] = true; + }; + // if in bit of intrest of neighbour block is set + mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] |= + ((mainShmem[begfirstRegShmem + threadIdx.x + threadIdx.y * 32] >> 31) & 1) << 0; + } + pipeline.consumer_release(); + __syncthreads(); - }; - fpSFnS[0] += sharedForGold[33];// will be needed later for global set - localBlockMetaData[1] = sharedForGold[33]; + /////////// step 3 load right process bottom + pipeline.producer_acquire(); + if (localBlockMetaData[(i & 1) * 20 + 16] < isGoldOffset) { + cuda::memcpy_async( + cta, + (&mainShmem[begfirstRegShmem]), + &getSourceReduced(fbArgs, iterationNumb) + [localBlockMetaData[(i & 1) * 20 + 16] * fbArgs.metaData.mainArrSectionLength + + fbArgs.metaData.mainArrXLength * (1 - isGoldForLocQueue[i])], + cuda::aligned_size_t<128>(sizeof(uint32_t) * fbArgs.metaData.mainArrXLength), + pipeline); + } + pipeline.producer_commit(); + // process bototm + pipeline.consumer_wait(); + + if (localBlockMetaData[(i & 1) * 20 + 14] < isGoldOffset) { + if (isBitAt(mainShmem[begSourceShmem + threadIdx.x + threadIdx.y * 32], 31)) { + isAnythingInPadding[1] = true; + }; + // if in bit of intrest of neighbour block is set + mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] |= + ((mainShmem[begSecRegShmem + threadIdx.x + threadIdx.y * 32] >> 0) & 1) << 31; + } - } - if ((threadIdx.x == 0) && (threadIdx.y == 1) && isNotEmpty) { + /* dilatateHelperTopDown(1, mainShmem, isAnythingInPadding, localBlockMetaData, 14 + , 0, 31 + , begSecRegShmem, i);*/ + + pipeline.consumer_release(); + /////////// step 4 load left process right + // load left + pipeline.producer_acquire(); + if (mainShmem[startOfLocalWorkQ + i] > 0) { + cuda::memcpy_async( + cta, + (&mainShmem[begSecRegShmem]), + &getSourceReduced(fbArgs, iterationNumb) + [(mainShmem[startOfLocalWorkQ + i] - 1) * fbArgs.metaData.mainArrSectionLength + + fbArgs.metaData.mainArrXLength * (1 - isGoldForLocQueue[i])], + cuda::aligned_size_t<128>(sizeof(uint32_t) * fbArgs.metaData.mainArrXLength), + pipeline); + } + pipeline.producer_commit(); + // process right + pipeline.consumer_wait(); + + if (threadIdx.x == (fbArgs.dbXLength - 1)) { + // now we need to load the data from the neigbouring blocks + // first checking is there anything to look to + if (localBlockMetaData[(i & 1) * 20 + 16] < isGoldOffset) { + // now we load - we already done earlier up and down so now we are considering only anterior, posterior + // , left , right possibilities + if (mainShmem[begSourceShmem + threadIdx.x + threadIdx.y * 32] > 0) { + isAnythingInPadding[3] = true; + }; + mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] = + mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] | + mainShmem[begfirstRegShmem + (threadIdx.y * 32)]; + }; + } else { // given we are not in corner case we need just to do the dilatation using biwise or with the data + // inside the block + mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] = + mainShmem[begSourceShmem + (threadIdx.x + 1) + (threadIdx.y) * 32] | + mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32]; + } + pipeline.consumer_release(); + __syncthreads(); + /////// step 5 load anterior process left + // load anterior + pipeline.producer_acquire(); + if (localBlockMetaData[(i & 1) * 20 + 17] < isGoldOffset) { + cuda::memcpy_async( + cta, + (&mainShmem[begfirstRegShmem]), + &getSourceReduced(fbArgs, iterationNumb) + [localBlockMetaData[(i & 1) * 20 + 17] * fbArgs.metaData.mainArrSectionLength + + fbArgs.metaData.mainArrXLength * (1 - isGoldForLocQueue[i])], + cuda::aligned_size_t<128>(sizeof(uint32_t) * fbArgs.metaData.mainArrXLength), + pipeline); + } + pipeline.producer_commit(); + // process left + pipeline.consumer_wait(); + + // so we first check for corner cases + if (threadIdx.x == 0) { + // now we need to load the data from the neigbouring blocks + // first checking is there anything to look to + if (localBlockMetaData[(i & 1) * 20 + 15] < isGoldOffset) { + // now we load - we already done earlier up and down so now we are considering only anterior, posterior + // , left , right possibilities + if (mainShmem[begSourceShmem + threadIdx.x + threadIdx.y * 32] > 0) { + isAnythingInPadding[2] = true; + }; + mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] = + mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] | + mainShmem[begSecRegShmem + 31 + threadIdx.y * 32]; + }; + } else { // given we are not in corner case we need just to do the dilatation using biwise or with the data + // inside the block + mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] = + mainShmem[begSourceShmem + (threadIdx.x - 1) + (threadIdx.y) * 32] | + mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32]; + } - sharedForSegm[33] = 0;//reset - for (int i = 0; i < tile.meta_group_size(); i += 1) { - sharedForSegm[33] += sharedForSegm[i]; - }; - fpSFnS[1] += sharedForSegm[33];// will be needed later for global set - //setting metadata - localBlockMetaData[2] = sharedForSegm[33]; + pipeline.consumer_release(); + __syncthreads(); - } + /////// step 6 load posterior process anterior + // load posterior + pipeline.producer_acquire(); + if (localBlockMetaData[(i & 1) * 20 + 18] < isGoldOffset) { + cuda::memcpy_async( + cta, + (&mainShmem[begSecRegShmem]), + &getSourceReduced(fbArgs, iterationNumb) + [localBlockMetaData[(i & 1) * 20 + 18] * fbArgs.metaData.mainArrSectionLength + + fbArgs.metaData.mainArrXLength * (1 - isGoldForLocQueue[i])], + cuda::aligned_size_t<128>(sizeof(uint32_t) * fbArgs.metaData.mainArrXLength), + pipeline); + } + pipeline.producer_commit(); + + // process anterior + pipeline.consumer_wait(); + + // so we first check for corner cases + if (threadIdx.y == (fbArgs.dbYLength - 1)) { + // now we need to load the data from the neigbouring blocks + // first checking is there anything to look to + if (localBlockMetaData[(i & 1) * 20 + 17] < isGoldOffset) { + // now we load - we already done earlier up and down so now we are considering only anterior, posterior + // , left , right possibilities + if (mainShmem[begSourceShmem + threadIdx.x + threadIdx.y * 32] > 0) { + isAnythingInPadding[4] = true; + }; + mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] = + mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] | mainShmem[begfirstRegShmem + threadIdx.x]; + }; + } else { // given we are not in corner case we need just to do the dilatation using biwise or with the data + // inside the block + mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] = + mainShmem[begSourceShmem + (threadIdx.x) + (threadIdx.y + 1) * 32] | + mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32]; + } - //marking as active -//FP pass - if ((threadIdx.x == 0) && (threadIdx.y == 0) && isNotEmpty && anyInGold[0]) { - localBlockMetaData[7] = 1; + pipeline.consumer_release(); + __syncthreads(); - }; - //FN pass - if ((threadIdx.x == 1) && (threadIdx.y == 0) && isNotEmpty && anyInSegm[0]) { - localBlockMetaData[9] = 1; + /////// step 7 + // load reference if needed or data for next iteration if there is such + // process posterior, save data from res shmem to global memory also we mark weather block is full + pipeline.producer_acquire(); + + // if block should be validated we load data for validation + if (localBlockMetaData[(i & 1) * 20 + ((1 - isGoldForLocQueue[i]) + 1)] // fp for gold and fn count for not + // gold + > + localBlockMetaData[(i & 1) * 20 + ((1 - isGoldForLocQueue[i]) + 3)]) { // so count is bigger than + // counter so we should validate + cuda::memcpy_async( + cta, + (&mainShmem[begfirstRegShmem]), + &fbArgs.origArrsPointer + [mainShmem[startOfLocalWorkQ + i] * fbArgs.metaData.mainArrSectionLength + + fbArgs.metaData.mainArrXLength * (isGoldForLocQueue[i])], // we look for + cuda::aligned_size_t<128>(sizeof(uint32_t) * fbArgs.metaData.mainArrXLength), + pipeline); + + } else { // if we are not validating we immidiately start loading data for next loop + if (i + 1 < worQueueStep[0]) { + cuda::memcpy_async( + cta, + (&localBlockMetaData[((i + 1) & 1) * 20]), + (&fbArgs.metaDataArrPointer + [(mainShmem[startOfLocalWorkQ + 1 + i]) * fbArgs.metaData.metaDataSectionLength]), + cuda::aligned_size_t<4>(sizeof(uint32_t) * 20), + pipeline); + } + } - }; + pipeline.producer_commit(); + // processPosteriorAndSaveResShmem - //after we streamed over all block we save also information about indicies of the surrounding blocks - given they are in range if not UINT32_MAX will be saved - //top + pipeline.consumer_wait(); + // dilatate posterior + // so we first check for corner cases + if (threadIdx.y == 0) { + // now we need to load the data from the neigbouring blocks + // first checking is there anything to look to + if (localBlockMetaData[(i & 1) * 20 + 18] < isGoldOffset) { + // now we load - we already done earlier up and down so now we are considering only anterior, posterior + // , left , right possibilities + if (mainShmem[begSourceShmem + threadIdx.x + threadIdx.y * 32] > 0) { + isAnythingInPadding[5] = true; + }; + mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] = + mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] | + mainShmem[begSecRegShmem + threadIdx.x + (fbArgs.dbYLength - 1) * 32]; + }; + } else { // given we are not in corner case we need just to do the dilatation using biwise or with the data + // inside the block + mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] = + mainShmem[begSourceShmem + (threadIdx.x) + (threadIdx.y - 1) * 32] | + mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32]; + } + // now all data should be properly dilatated we save it to global memory + // try save target reduced via mempcy async ... - setNeighbourBlocks(fbArgs, 3, 13, (zMeta > 0), (-(metaData.metaXLength * metaData.MetaYLength)), linIdexMeta, metaData, localBlockMetaData);//top - setNeighbourBlocks(fbArgs, 4, 14, (zMeta < (metaData.MetaZLength - 1)), (metaData.metaXLength * metaData.MetaYLength), linIdexMeta, metaData, localBlockMetaData);//bottom + // cuda::memcpy_async(cta, + // &getTargetReduced(fbArgs, iterationNumb)[mainShmem[startOfLocalWorkQ + i] * + // fbArgs.metaData.mainArrSectionLength + fbArgs.metaData.mainArrXLength * (1 - isGoldForLocQueue[i])] , + // (&mainShmem[begResShmem]), cuda::aligned_size_t<128>(sizeof(uint32_t) * + // fbArgs.metaData.mainArrXLength) , pipeline); - setNeighbourBlocks(fbArgs, 6, 15, (xMeta > 0), (-1), linIdexMeta, metaData, localBlockMetaData);//left - setNeighbourBlocks(fbArgs, 7, 16, (xMeta < (metaData.metaXLength - 1)), 1, linIdexMeta, metaData, localBlockMetaData);//right + getTargetReduced(fbArgs, iterationNumb) + [mainShmem[startOfLocalWorkQ + i] * fbArgs.metaData.mainArrSectionLength + + fbArgs.metaData.mainArrXLength * (1 - isGoldForLocQueue[i]) + threadIdx.x + threadIdx.y * 32] = + mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32]; - setNeighbourBlocks(fbArgs, 8, 17, (yMeta < (metaData.MetaYLength - 1)), metaData.metaXLength, linIdexMeta, metaData, localBlockMetaData);//anterior - setNeighbourBlocks(fbArgs, 9, 18, (yMeta > 0), (-metaData.metaXLength), linIdexMeta, metaData, localBlockMetaData);//posterior + pipeline.consumer_release(); - if ((threadIdx.x < 20) && (threadIdx.y == 0)) { - metaDataArr[linIdexMeta * metaData.metaDataSectionLength + threadIdx.x] = localBlockMetaData[threadIdx.x]; - }; + __syncthreads(); - __syncthreads(); // just to reduce the warp divergence + //////// step 8 basically in order to complete here anyting the count need to be bigger than counter + // loading for next block if block is not to be validated it was already done earlier + pipeline.producer_acquire(); + if (localBlockMetaData[(i & 1) * 20 + ((1 - isGoldForLocQueue[i]) + 1)] // fp for gold and fn count for not + // gold + > + localBlockMetaData[(i & 1) * 20 + ((1 - isGoldForLocQueue[i]) + 3)]) { // so count is bigger than + // counter so we should validate + if (i + 1 < worQueueStep[0]) { + cuda::memcpy_async( + cta, + (&localBlockMetaData[((i + 1) & 1) * 20]), + (&fbArgs.metaDataArrPointer + [(mainShmem[startOfLocalWorkQ + 1 + i]) * fbArgs.metaData.metaDataSectionLength]), + cuda::aligned_size_t<4>(sizeof(uint32_t) * 20), + pipeline); + } + } + pipeline.producer_commit(); + __syncthreads(); - } + // validation - so looking for newly covered voxel for opposite array so new fps or new fns + pipeline.consumer_wait(); + + if (localBlockMetaData[(i & 1) * 20 + ((1 - isGoldForLocQueue[i]) + 1)] // fp for gold and fn count for not + // gold + > + localBlockMetaData[(i & 1) * 20 + ((1 - isGoldForLocQueue[i]) + 3)]) { // so count is bigger than + // counter so we should validate + // now we look through bits and when some is set we call it a result +#pragma unroll + for (uint8_t bitPos = 0; bitPos < 32; bitPos++) { + // if any bit here is set it means it should be added to result list + if (isBitAt(mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32], bitPos) && + !isBitAt(mainShmem[begSourceShmem + threadIdx.x + threadIdx.y * 32], bitPos) && + isBitAt(mainShmem[begfirstRegShmem + threadIdx.x + threadIdx.y * 32], bitPos)) { + // just re + mainShmem[begSecRegShmem + threadIdx.x + threadIdx.y * 32] = 0; + ////// IMPORTANT for some reason in order to make it work resultfnOffset and resultfnOffset swith + ///places + if (isGoldForLocQueue[i]) { + mainShmem[begSecRegShmem + threadIdx.x + threadIdx.y * 32] = uint32_t( + atomicAdd_block(&(localFpConter[0]), 1) + localBlockMetaData[(i & 1) * 20 + 6] + + localBlockMetaData[(i & 1) * 20 + 3]); + + } else { + mainShmem[begSecRegShmem + threadIdx.x + threadIdx.y * 32] = uint32_t( + atomicAdd_block(&(localFnConter[0]), 1) + localBlockMetaData[(i & 1) * 20 + 5] + + localBlockMetaData[(i & 1) * 20 + 4]); + }; + // add results to global memory + // we add one gere jjust to distinguish it from empty result + fbArgs.resultListPointerMeta[mainShmem[begSecRegShmem + threadIdx.x + threadIdx.y * 32]] = + uint32_t(mainShmem[startOfLocalWorkQ + i] + (isGoldOffset * isGoldForLocQueue[i]) + 1); + fbArgs.resultListPointerLocal[mainShmem[begSecRegShmem + threadIdx.x + threadIdx.y * 32]] = + uint32_t((fbArgs.dbYLength * 32 * bitPos) + (threadIdx.y * 32) + (threadIdx.x)); + fbArgs.resultListPointerIterNumb[mainShmem[begSecRegShmem + threadIdx.x + threadIdx.y * 32]] = + uint32_t(iterationNumb[0]); + } + }; + } + ///////// + pipeline.consumer_release(); + /// /// cleaning + __syncthreads(); - __syncthreads(); + if (threadIdx.x == 9 && threadIdx.y == 2) { // this is how it is encoded wheather it is gold or segm block + + // executed in case of previous block + if (isBlockFull[i & 1] && i >= 0) { + // setting data in metadata that block is full + fbArgs.metaDataArrPointer + [mainShmem[startOfLocalWorkQ + i] * fbArgs.metaData.metaDataSectionLength + 10 - + (isGoldForLocQueue[i] * 2)] = true; + } + // resetting for some reason block 0 gets as full even if it should not ... + isBlockFull[i & 1] = true; // mainShmem[startOfLocalWorkQ + i]>0;//!isPaddingPass; + }; + // we do it only for non padding pass + if (threadIdx.x < 6 && threadIdx.y == 1 && !isPaddingPass) { + // executed in case of previous block + if (i >= 0) { + if (localBlockMetaData[(i & 1) * 20 + 13 + threadIdx.x] < isGoldOffset) { + if (isAnythingInPadding[threadIdx.x]) { + // holding data weather we have anything in padding 0)top 1)bottom, 2)left 3)right, 4)anterior, + // 5)posterior, + fbArgs.metaDataArrPointer + [localBlockMetaData[(i & 1) * 20 + 13 + threadIdx.x] * fbArgs.metaData.metaDataSectionLength + + 12 - isGoldForLocQueue[i]] = 1; + } + } + } + isAnythingInPadding[threadIdx.x] = false; + }; - //setting global fp and fn - if ((threadIdx.x == 0) && (threadIdx.y == 0)) { - atomicAdd(&(minMaxes[7]), fpSFnS[0]); - }; + if (threadIdx.x == 7 && threadIdx.y == 0) { + // this will be executed only if fp or fn counters are bigger than 0 so not during first pass + if (localFpConter[0] > 0) { + fbArgs + .metaDataArrPointer[mainShmem[startOfLocalWorkQ + i] * fbArgs.metaData.metaDataSectionLength + 3] += + localFpConter[0]; - if ((threadIdx.x == 1) && (threadIdx.y == 0)) { - atomicAdd(&(minMaxes[8]), fpSFnS[1]); + blockFpConter[0] += localFpConter[0]; + localFpConter[0] = 0; + } + }; + if (threadIdx.x == 8 && threadIdx.y == 0) { + if (localFnConter[0] > 0) { + fbArgs + .metaDataArrPointer[mainShmem[startOfLocalWorkQ + i] * fbArgs.metaData.metaDataSectionLength + 4] += + localFnConter[0]; - }; -} + blockFnConter[0] += localFnConter[0]; + localFnConter[0] = 0; + } + }; + __syncthreads(); + } + } + // here we are after all of the blocks planned to be processed by this block are + // just for pipeline to work + pipeline.consumer_wait(); -/*************************************** - * firstMetaPrepareKernel - * ********************************/ + pipeline.consumer_release(); + } -/* -we add here to appropriate queue data about metadata of blocks of intrest -minMaxesPos- marks in minmaxes the postion of global offset counter -12) global FP offset 13) global FnOffset -offsetMetadataArr- arrays from metadata holding data about result list offsets it can be either fbArgs.metaData.fpOffset or fbArgs.metaData.fnOffset -*/ -#pragma once -__device__ inline void addToQueue(uint32_t linIdexMeta, uint8_t isGold - , unsigned int fpFnLocCounter[1], uint32_t localWorkQueue[1600], uint32_t localOffsetQueue[1600], unsigned int localWorkQueueCounter[1] - , uint8_t countIndexNumb, uint8_t isActiveIndexNumb, uint8_t offsetIndexNumb - , uint32_t* metaDataArr, MetaDataGPU metaData, unsigned int* minMaxes, uint32_t* workQueue) { - - unsigned int count = metaDataArr[linIdexMeta * metaData.metaDataSectionLength + countIndexNumb]; - //given fp is non zero we need to add this to local queue - if (metaDataArr[linIdexMeta * metaData.metaDataSectionLength + isActiveIndexNumb] == 1) { - - // printf("adding to local in first meta pass linIdexMeta %d isGold %d isActiveIndexNumb %d \n ", linIdexMeta, isGold, isActiveIndexNumb); - - count = atomicAdd_block(&fpFnLocCounter[0], count); - unsigned int old = atomicAdd_block(&localWorkQueueCounter[0], 1); - //we check weather we still have space in shared memory - if (old < 1590) {// so we still have space in shared memory - // will be equal or above isGoldOffset if it is gold pass - localWorkQueue[old] = linIdexMeta + (isGoldOffset * isGold); - localOffsetQueue[old] = uint32_t(count); - } - else {// so we do not have any space more in the sared memory - it is unlikely so we will just in this case save immidiately to global memory - old = atomicAdd(&(minMaxes[9]), old); - //workQueue - workQueue[old] = linIdexMeta + (isGoldOffset * isGold); - //and offset - metaDataArr[linIdexMeta * metaData.metaDataSectionLength + offsetIndexNumb] = atomicAdd(&(minMaxes[12]), count); - }; - } -} + __syncthreads(); + // updating global counters + if (threadIdx.x == 0 && threadIdx.y == 0) { + if (blockFpConter[0] > 0) { + atomicAdd(&(fbArgs.minMaxes[10]), (blockFpConter[0])); + } + }; + if (threadIdx.x == 1 && threadIdx.y == 0) { + if (blockFnConter[0] > 0) { + // if (blockFnConter[0]>10) { + // printf("Fn %d ", blockFnConter[0]); + //} + atomicAdd(&(fbArgs.minMaxes[11]), (blockFnConter[0])); + } + }; -#pragma once -template -__global__ void firstMetaPrepareKernel(ForBoolKernelArgs fbArgs - , MetaDataGPU metaData, unsigned int* minMaxes, uint32_t* workQueue - , uint32_t* origArrs, uint32_t* metaDataArr) { - - //////initializations - thread_block cta = this_thread_block(); - char* tensorslice;// needed for iterations over 3d arrays - //local offset counters for fp and fn's - __shared__ unsigned int fpFnLocCounter[1]; - // used to store the start position in global memory for whole block - __shared__ unsigned int globalOffsetForBlock[1]; - __shared__ unsigned int globalWorkQueueCounter[1]; - //used as local work queue counter - __shared__ unsigned int localWorkQueueCounter[1]; - //according to https://forums.developer.nvidia.com/t/find-the-limit-of-shared-memory-that-can-be-used-per-block/48556 it is good to keep shared memory below 16kb kilo bytes so it will give us 1600 length of shared memory - //so here we will store locally the calculated offsets and coordinates of meta data block of intrest marking also wheather we are talking about gold or segmentation pass (fp or fn ) - __shared__ uint32_t localWorkQueue[1600]; - __shared__ uint32_t localOffsetQueue[1600]; - if ((threadIdx.x == 0)) { - fpFnLocCounter[0] = 0; - } - if ((threadIdx.x == 1)) { - localWorkQueueCounter[0] = 0; - } - if ((threadIdx.x == 2)) { - globalWorkQueueCounter[0] = 0; - } - if ((threadIdx.x == 3)) { - globalOffsetForBlock[0] = 0; - } - __syncthreads(); + // in first thread block we zero work queue counter + if (threadIdx.x == 2 && threadIdx.y == 0) { + if (blockIdx.x == 0) { + fbArgs.minMaxes[9] = 0; + } + }; + grid.sync(); + /////////////////////////**************************************************************************************************************** - // classical grid stride loop - in case of unlikely event we will run out of space we will empty it prematurly - //main metadata iteration - for (uint32_t linIdexMeta = blockIdx.x * blockDim.x + threadIdx.x; linIdexMeta < metaData.totalMetaLength; linIdexMeta += blockDim.x * gridDim.x) { + /// metadata pass - //goldpass - addToQueue(linIdexMeta, 0 - , fpFnLocCounter, localWorkQueue, localOffsetQueue, localWorkQueueCounter - , 1, 9, 6 - , metaDataArr, metaData, minMaxes, workQueue); - //segmPass - addToQueue(linIdexMeta, 1 - , fpFnLocCounter, localWorkQueue, localOffsetQueue, localWorkQueueCounter - , 2, 7, 5 - , metaDataArr, metaData, minMaxes, workQueue); + // preparation loads + if (threadIdx.x == 0 && threadIdx.y == 0) { + fpFnLocCounter[0] = 0; + } + if (threadIdx.x == 1 && threadIdx.y == 0) { + localWorkQueueCounter[0] = 0; + } + if (threadIdx.x == 2 && threadIdx.y == 0) { + localWorkQueueCounter[0] = 0; + } + if (threadIdx.x == 3 && threadIdx.y == 0) { + localWorkQueueCounter[0] = 0; + } + + if (threadIdx.x == 0 && threadIdx.y == 1) { + isGoldPassToContinue[0] = ((fbArgs.minMaxes[7] * fbArgs.robustnessPercent) > fbArgs.minMaxes[10]); + }; + + if (threadIdx.x == 0 && threadIdx.y == 1) { + isSegmPassToContinue[0] = ((fbArgs.minMaxes[8] * fbArgs.robustnessPercent) > fbArgs.minMaxes[11]); + }; + + __syncthreads(); + + ///////////////////////////////// + + for (uint32_t linIdexMeta = blockIdx.x * blockDim.x * blockDim.y + threadIdx.y * blockDim.x + threadIdx.x; + linIdexMeta <= fbArgs.metaData.totalMetaLength; + linIdexMeta += (blockDim.x * blockDim.y * gridDim.x)) { + if (isPaddingPass == 0) { + // goldpass + if (isGoldPassToContinue[0] && + fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 11] && + !fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 7] && + !fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 8]) { + mainShmem[atomicAdd_block(&localWorkQueueCounter[0], 1)] = linIdexMeta + (isGoldOffset); + // setting to be activated to 0 + fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 11] = 0; + // setting active to 1 + fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 7] = 1; + }; + } + // contrary to number it is when we are not in padding pass + else { + // gold pass + if (isGoldPassToContinue[0] && + fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 7] && + !fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 8]) { + mainShmem[atomicAdd_block(&localWorkQueueCounter[0], 1)] = linIdexMeta + (isGoldOffset); + }; + } + } - } - __syncthreads(); - if ((threadIdx.x == 0)) { - globalOffsetForBlock[0] = atomicAdd(&(minMaxes[12]), (fpFnLocCounter[0])); + __syncthreads(); - }; - if ((threadIdx.x == 1)) { - if (localWorkQueueCounter[0] > 0) { - globalWorkQueueCounter[0] = atomicAdd(&(minMaxes[9]), (localWorkQueueCounter[0])); + if (localWorkQueueCounter[0] > 0) { + if (threadIdx.x == 0 && threadIdx.y == 0) { + globalWorkQueueCounter[0] = atomicAdd(&(fbArgs.minMaxes[9]), (localWorkQueueCounter[0])); } - } - __syncthreads(); + __syncthreads(); + for (uint32_t linI = threadIdx.y * blockDim.x + threadIdx.x; linI < localWorkQueueCounter[0]; + linI += blockDim.x * blockDim.y) { + fbArgs.workQueuePointer[globalWorkQueueCounter[0] + linI] = mainShmem[linI]; + } + __syncthreads(); + } + __syncthreads(); - //setting offsets - for (uint32_t i = threadIdx.x; i < localWorkQueueCounter[0]; i += blockDim.x) { - workQueue[globalWorkQueueCounter[0] + i] = localWorkQueue[i]; + if (threadIdx.x == 0 && threadIdx.y == 0) { + localWorkQueueCounter[0] = 0; + } + __syncthreads(); + + for (uint32_t linIdexMeta = blockIdx.x * blockDim.x * blockDim.y + threadIdx.y * blockDim.x + threadIdx.x; + linIdexMeta <= fbArgs.metaData.totalMetaLength; + linIdexMeta += (blockDim.x * blockDim.y * gridDim.x)) { + if (isPaddingPass == 0) { + // segm pass + if ((isSegmPassToContinue[0] && + fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 12] && + !fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 9] && + !fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 10])) { + mainShmem[atomicAdd_block(&localWorkQueueCounter[0], 1)] = linIdexMeta; + + // setting to be activated to 0 + fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 12] = 0; + // setting active to 1 + fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 9] = 1; + } - //FP pass - if (localWorkQueue[i] >= isGoldOffset) { - metaDataArr[(localWorkQueue[i] - isGoldOffset) * metaData.metaDataSectionLength + 5] = localOffsetQueue[i] + globalOffsetForBlock[0]; } - //FN pass + // contrary to number it is when we are not in padding pass else { - metaDataArr[(localWorkQueue[i]) * metaData.metaDataSectionLength + 6] = localOffsetQueue[i] + globalOffsetForBlock[0]; - - }; - - - } - - - -}; - - - - -/*************************************** - * memory allocations - * ********************************/ - - -/* -Get arguments for kernels -*/ -#pragma once -template -inline ForBoolKernelArgs getArgsForKernel(ForFullBoolPrepArgs& mainFunArgs - , int& warpsNumbForMainPass, int& blockForMainPass - , const int xLen, const int yLen, const int zLen, cudaStream_t stream -) { - - - mainFunArgs.Nx = xLen; - mainFunArgs.Ny = yLen; - mainFunArgs.Nz = zLen; - - - unsigned int* minMaxes; - size_t sizeminMaxes = sizeof(unsigned int) * 20; - cudaMallocAsync(&minMaxes, sizeminMaxes, stream); - ForBoolKernelArgs res; - - res.Nx = xLen; - res.Ny = yLen; - res.Nz = zLen; - - res.metaData = allocateMetaDataOnGPU(mainFunArgs.metaData, minMaxes); - res.metaData.minMaxes = minMaxes; - res.minMaxes = minMaxes; - res.compare_values = mainFunArgs.compare_values; - res.dbXLength = 32; - res.dbYLength = warpsNumbForMainPass; - res.dbZLength = 32; - - //printf("in setting bool args ylen %d dbYlen %d calculated meta %d \n ", yLen, res.dbYLength, int(ceil(yLen / res.dbYLength))); - res.metaData.metaXLength = int(ceil(xLen / res.dbXLength)); - res.metaData.MetaYLength = int(ceil(yLen / res.dbYLength));; - res.metaData.MetaZLength = int(ceil(zLen / res.dbZLength));; - res.metaData.minX = 0; - res.metaData.minY = 0; - res.metaData.minZ = 0; - res.metaData.maxX = res.metaData.metaXLength; - res.metaData.maxY = res.metaData.MetaYLength; - res.metaData.maxZ = res.metaData.MetaZLength; - - res.metaData.totalMetaLength = res.metaData.metaXLength * res.metaData.MetaYLength * res.metaData.MetaZLength; - res.goldArr = mainFunArgs.goldArr; - res.segmArr = mainFunArgs.segmArr; - - - return res; -} - - -#pragma once -/* -allocate memory after first kernel -*/ -template -inline MetaDataGPU allocateMemoryAfterMinMaxesKernel(ForBoolKernelArgs& gpuArgs, ForFullBoolPrepArgs& cpuArgs, cudaStream_t stream) { - ////reduced arrays - uint32_t* origArr; - uint32_t* metaDataArr; - uint32_t* workQueue; - //copy on cpu - size_t size = sizeof(unsigned int) * 20; - cudaMemcpyAsync(cpuArgs.metaData.minMaxes, gpuArgs.minMaxes, size, cudaMemcpyDeviceToHost, stream); - - //read an modify - //1)maxX 2)minX 3)maxY 4) minY 5) maxZ 6) minZ - //7)global FP count; 8)global FN count - unsigned int xRange = cpuArgs.metaData.minMaxes[1] - cpuArgs.metaData.minMaxes[2] + 1; - unsigned int yRange = cpuArgs.metaData.minMaxes[3] - cpuArgs.metaData.minMaxes[4] + 1; - unsigned int zRange = cpuArgs.metaData.minMaxes[5] - cpuArgs.metaData.minMaxes[6] + 1; - unsigned int totalMetaLength = (xRange) * (yRange) * (zRange); - //updating size informations - gpuArgs.metaData.metaXLength = xRange; - gpuArgs.metaData.MetaYLength = yRange; - gpuArgs.metaData.MetaZLength = zRange; - gpuArgs.metaData.totalMetaLength = totalMetaLength; - //saving min maxes - gpuArgs.metaData.maxX = cpuArgs.metaData.minMaxes[1]; - gpuArgs.metaData.minX = cpuArgs.metaData.minMaxes[2]; - gpuArgs.metaData.maxY = cpuArgs.metaData.minMaxes[3]; - gpuArgs.metaData.minY = cpuArgs.metaData.minMaxes[4]; - gpuArgs.metaData.maxZ = cpuArgs.metaData.minMaxes[5]; - gpuArgs.metaData.minZ = cpuArgs.metaData.minMaxes[6]; - - //allocating needed memory - // main array - unsigned int mainArrXLength = gpuArgs.dbXLength * gpuArgs.dbYLength; - unsigned int mainArrSectionLength = (mainArrXLength * 2); - gpuArgs.metaData.mainArrXLength = mainArrXLength; - gpuArgs.metaData.mainArrSectionLength = mainArrSectionLength; - - size_t sizeB = totalMetaLength * mainArrSectionLength * sizeof(uint32_t); - //cudaMallocAsync(&mainArr, sizeB, 0); - size_t sizeorigArr = totalMetaLength * (mainArrXLength * 2) * sizeof(uint32_t); - cudaMallocAsync(&origArr, sizeorigArr, stream); - size_t sizemetaDataArr = totalMetaLength * (20) * sizeof(uint32_t) + 100; - cudaMallocAsync(&metaDataArr, sizemetaDataArr, stream); - size_t sizeC = (totalMetaLength * 2 * sizeof(uint32_t) + 50); - cudaMallocAsync(&workQueue, sizeC, stream); - gpuArgs.origArrsPointer = origArr; - gpuArgs.metaDataArrPointer = metaDataArr; - gpuArgs.workQueuePointer = workQueue; - return gpuArgs.metaData; -}; - - - - -/* -becouse we need a lot of the additional memory spaces to minimize memory consumption allocations will be postponed after first kernel run enabling -*/ -#pragma once -template -inline int allocateMemoryAfterBoolKernel(ForBoolKernelArgs& gpuArgs, ForFullBoolPrepArgs& cpuArgs, cudaStream_t stream, bool resIterNeeded) { - - int32_t* resultListPointerMeta; - int32_t* resultListPointerLocal; - int32_t* resultListPointerIterNumb; - uint32_t* mainArrAPointer; - uint32_t* mainArrBPointer; - //copy on cpu - size_t size = sizeof(unsigned int) * 20; - cudaMemcpyAsync(cpuArgs.metaData.minMaxes, gpuArgs.metaData.minMaxes, size, cudaMemcpyDeviceToHost, stream); - - unsigned int fpPlusFn = cpuArgs.metaData.minMaxes[7] + cpuArgs.metaData.minMaxes[8]; - size = sizeof(int32_t) * (fpPlusFn + 50); - - - cudaMallocAsync(&resultListPointerLocal, size, stream); - cudaMallocAsync(&resultListPointerIterNumb, size, stream); - cudaMallocAsync(&resultListPointerMeta, size, stream); - - - // we will allocate only if we need - if (resIterNeeded) { - auto options = - torch::TensorOptions() - .dtype(torch::kInt32) - .device(torch::kCUDA, 0) - .requires_grad(false); - - auto resultListPointerIterNumbTensor = torch::empty((fpPlusFn + 50), options); - gpuArgs.resultListPointerIterNumbTensor = resultListPointerIterNumbTensor; - } - - - auto xRange = gpuArgs.metaData.metaXLength; - auto yRange = gpuArgs.metaData.MetaYLength; - auto zRange = gpuArgs.metaData.MetaZLength; - - - size_t sizeB = gpuArgs.metaData.totalMetaLength * gpuArgs.metaData.mainArrSectionLength * sizeof(uint32_t); - - - cudaMallocAsync(&mainArrAPointer, sizeB, 0); - cudaMemcpyAsync(mainArrAPointer, gpuArgs.origArrsPointer, sizeB, cudaMemcpyDeviceToDevice, stream); - - - cudaMallocAsync(&mainArrBPointer, sizeB, 0); - cudaMemcpyAsync(mainArrBPointer, gpuArgs.origArrsPointer, sizeB, cudaMemcpyDeviceToDevice, stream); - - //just in order set it to 0 - int32_t* resultListPointerMetaCPU = (int32_t*)calloc(fpPlusFn + 50, sizeof(int32_t)); - cudaMemcpyAsync(resultListPointerMeta, resultListPointerMetaCPU, size, cudaMemcpyHostToDevice, stream); - cudaMemcpyAsync(resultListPointerIterNumb, resultListPointerMetaCPU, size, cudaMemcpyHostToDevice, stream); - free(resultListPointerMetaCPU); - - gpuArgs.resultListPointerMeta = resultListPointerMeta; - gpuArgs.resultListPointerLocal = resultListPointerLocal; - gpuArgs.resultListPointerIterNumb = resultListPointerIterNumb; - - gpuArgs.mainArrAPointer = mainArrAPointer; - gpuArgs.mainArrBPointer = mainArrBPointer; - - - return fpPlusFn; -}; - - - - - - - -/*************************************** - * main kernel - * ********************************/ -template -inline __global__ void mainPassKernel(ForBoolKernelArgs fbArgs) { - - - - thread_block cta = cooperative_groups::this_thread_block(); - - grid_group grid = cooperative_groups::this_grid(); - - /* - * according to https://forums.developer.nvidia.com/t/find-the-limit-of-shared-memory-that-can-be-used-per-block/48556 it is good to keep shared memory below 16kb kilo bytes - main shared memory spaces - 0-1023 : sourceShmem - 1024-2047 : resShmem - 2048-3071 : first register space - 3072-4095 : second register space - 4096- 4127: small 32 length resgister 3 space - 4128-4500 (372 length) : place for local work queue in dilatation kernels - */ - // __shared__ uint32_t mainShmem[lengthOfMainShmem]; - __shared__ uint32_t mainShmem[lengthOfMainShmem]; - //cuda::associate_access_property(&mainShmem, cuda::access_property::shared{}); - - - - constexpr size_t stages_count = 2; // Pipeline stages number - - // Allocate shared storage for a two-stage cuda::pipeline: - __shared__ cuda::pipeline_shared_state< - cuda::thread_scope::thread_scope_block, - stages_count - > shared_state; - - //cuda::pipeline pipeline = cuda::make_pipeline(cta, &shared_state); - cuda::pipeline pipeline = cuda::make_pipeline(cta, &shared_state); - - - - //usefull for iterating through local work queue - __shared__ bool isGoldForLocQueue[localWorkQueLength]; - // holding data about paddings - - - // holding data weather we have anything in padding 0)top 1)bottom, 2)left 3)right, 4)anterior, 5)posterior, - __shared__ bool isAnythingInPadding[6]; - - __shared__ bool isBlockFull[2]; - - __shared__ uint32_t lastI[1]; - - - //variables needed for all threads - __shared__ int iterationNumb[1]; - __shared__ unsigned int globalWorkQueueOffset[1]; - __shared__ unsigned int globalWorkQueueCounter[1]; - __shared__ unsigned int localWorkQueueCounter[1]; - // keeping data wheather gold or segmentation pass should continue - on the basis of global counters - - __shared__ unsigned int localTotalLenthOfWorkQueue[1]; - //counters for per block number of results added in this iteration - __shared__ unsigned int localFpConter[1]; - __shared__ unsigned int localFnConter[1]; - - __shared__ unsigned int blockFpConter[1]; - __shared__ unsigned int blockFnConter[1]; - - __shared__ unsigned int fpFnLocCounter[1]; - - //result list offset - needed to know where to write a result in a result list - __shared__ unsigned int resultfpOffset[1]; - __shared__ unsigned int resultfnOffset[1]; - - __shared__ unsigned int worQueueStep[1]; - - - /* will be used to store all of the minMaxes varibles from global memory (from 7 to 11) - 0 : global FP count; - 1 : global FN count; - 2 : workQueueCounter - 3 : resultFP globalCounter - 4 : resultFn globalCounter - */ - __shared__ unsigned int localMinMaxes[5]; - - /* will be used to store all of block metadata - nothing at 0 index - 1 :fpCount - 2 :fnCount - 3 :fpCounter - 4 :fnCounter - 5 :fpOffset - 6 :fnOffset - 7 :isActiveGold - 8 :isFullGold - 9 :isActiveSegm - 10 :isFullSegm - 11 :isToBeActivatedGold - 12 :isToBeActivatedSegm - 12 :isToBeActivatedSegm -//now linear indexes of the blocks in all sides - if there is no block in given direction it will equal UINT32_MAX - 13 : top - 14 : bottom - 15 : left - 16 : right - 17 : anterior - 18 : posterior - */ - - __shared__ uint32_t localBlockMetaData[40]; - - /* - //now linear indexes of the previous block in all sides - if there is no block in given direction it will equal UINT32_MAX - 0 : top - 1 : bottom - 2 : left - 3 : right - 4 : anterior - 5 : posterior - */ - - - /////used mainly in meta passes - -// __shared__ unsigned int fpFnLocCounter[1]; - __shared__ bool isGoldPassToContinue[1]; - __shared__ bool isSegmPassToContinue[1]; - - - - - - //initializations and loading - if (threadIdx.x == 9 && threadIdx.y == 0) { iterationNumb[0] = -1; }; - if (threadIdx.x == 11 && threadIdx.y == 0) { - isGoldPassToContinue[0] = true; - }; - if (threadIdx.x == 12 && threadIdx.y == 0) { - isSegmPassToContinue[0] = true; - - }; - - - //here we caclulate the offset for given block depending on length of the workqueue and number of the available blocks in a grid - // - this will give us number of work queue items per block - we will calculate offset on the basis of the block number - __syncthreads(); - - do { - - for (uint8_t isPaddingPass = 0; isPaddingPass < 2; isPaddingPass++) { - - - /////////////////////////**************************************************************************************************************** - /// dilataions - - //initial cleaning and initializations include loading min maxes - if (threadIdx.x == 7 && threadIdx.y == 0 && !isPaddingPass) { - iterationNumb[0] += 1; - }; - - if (threadIdx.x == 6 && threadIdx.y == 0) { - localWorkQueueCounter[0] = 0; - }; - - if (threadIdx.x == 1 && threadIdx.y == 0) { - blockFpConter[0] = 0; - }; - if (threadIdx.x == 2 && threadIdx.y == 0) { - blockFnConter[0] = 0; - }; - if (threadIdx.x == 3 && threadIdx.y == 0) { - localFpConter[0] = 0; - }; - if (threadIdx.x == 4 && threadIdx.y == 0) { - localFnConter[0] = 0; - }; - if (threadIdx.x == 9 && threadIdx.y == 0) { - isBlockFull[0] = true; - }; - if (threadIdx.x == 9 && threadIdx.y == 1) { - isBlockFull[1] = true; - }; - - if (threadIdx.x == 10 && threadIdx.y == 0) { - fpFnLocCounter[0] = 0; - }; - - - - if (threadIdx.x == 0 && threadIdx.y == 0) { - localTotalLenthOfWorkQueue[0] = fbArgs.minMaxes[9]; - globalWorkQueueOffset[0] = floor((float)(localTotalLenthOfWorkQueue[0] / gridDim.x)) + 1; - worQueueStep[0] = min(localWorkQueLength, globalWorkQueueOffset[0]); - }; - - if (threadIdx.y == 1) { - cooperative_groups::memcpy_async(cta, (&localMinMaxes[0]), (&fbArgs.minMaxes[7]), cuda::aligned_size_t<4>(sizeof(unsigned int) * 5)); - } - - __syncthreads(); - - /// load work QueueData into shared memory - for (uint32_t bigloop = blockIdx.x * globalWorkQueueOffset[0]; bigloop < ((blockIdx.x + 1) * globalWorkQueueOffset[0]); bigloop += worQueueStep[0]) { - - //grid stride loop - sadly most of threads will be idle - ///////// loading to work queue - if (((bigloop) < localTotalLenthOfWorkQueue[0]) && ((bigloop) < ((blockIdx.x + 1) * globalWorkQueueOffset[0]))) { - - for (uint16_t ii = cta.thread_rank(); ii < worQueueStep[0]; ii += cta.size()) { - - mainShmem[startOfLocalWorkQ + ii] = fbArgs.workQueuePointer[bigloop + ii]; - isGoldForLocQueue[ii] = (mainShmem[startOfLocalWorkQ + ii] >= isGoldOffset); - mainShmem[startOfLocalWorkQ + ii] = mainShmem[startOfLocalWorkQ + ii] - isGoldOffset * isGoldForLocQueue[ii]; - - - } - - } - //now all of the threads in the block needs to have the same i value so we will increment by 1 we are preloading to the pipeline block metaData - ////##### pipeline Step 0 - - __syncthreads(); - - - - - //loading metadata - pipeline.producer_acquire(); - if (((bigloop) < localTotalLenthOfWorkQueue[0]) && ((bigloop) < ((blockIdx.x + 1) * globalWorkQueueOffset[0]))) { - - cuda::memcpy_async(cta, (&localBlockMetaData[0]), - (&fbArgs.metaDataArrPointer[mainShmem[startOfLocalWorkQ] * fbArgs.metaData.metaDataSectionLength]) - , cuda::aligned_size_t<4>(sizeof(uint32_t) * 20), pipeline); - - } - pipeline.producer_commit(); - - - __syncthreads(); - - for (uint32_t i = 0; i < worQueueStep[0]; i += 1) { - - - - - if (((bigloop + i) < localTotalLenthOfWorkQueue[0]) && ((bigloop + i) < ((blockIdx.x + 1) * globalWorkQueueOffset[0]))) { - - - - pipeline.producer_acquire(); - cuda::memcpy_async(cta, &mainShmem[begSourceShmem], &getSourceReduced(fbArgs, iterationNumb)[ - mainShmem[startOfLocalWorkQ + i] * fbArgs.metaData.mainArrSectionLength + fbArgs.metaData.mainArrXLength * (1 - isGoldForLocQueue[i])], - cuda::aligned_size_t<128>(sizeof(uint32_t) * fbArgs.metaData.mainArrXLength), pipeline); - pipeline.producer_commit(); - - //just so pipeline will work well - pipeline.consumer_wait(); - - - - pipeline.consumer_release(); - __syncthreads(); - - ///////// step 1 load top and process main data - //load top - pipeline.producer_acquire(); - if (localBlockMetaData[(i & 1) * 20 + 13] < isGoldOffset) { - cuda::memcpy_async(cta, (&mainShmem[begfirstRegShmem]), - &getSourceReduced(fbArgs, iterationNumb)[localBlockMetaData[(i & 1) * 20 + 13] - * fbArgs.metaData.mainArrSectionLength + fbArgs.metaData.mainArrXLength * (1 - isGoldForLocQueue[i])], - cuda::aligned_size_t<128>(sizeof(uint32_t) * fbArgs.metaData.mainArrXLength) - , pipeline); - } - pipeline.producer_commit(); - //process main - pipeline.consumer_wait(); - //marking weather block is already full and no more dilatations are possible - if (__popc(mainShmem[begSourceShmem + threadIdx.x + threadIdx.y * 32]) < 32) { - isBlockFull[i & 1] = false; - } - mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] = bitDilatate(mainShmem[begSourceShmem + threadIdx.x + threadIdx.y * 32]); - pipeline.consumer_release(); - - ///////// step 2 load bottom and process top - //load bottom - pipeline.producer_acquire(); - if (localBlockMetaData[(i & 1) * 20 + 14] < isGoldOffset) { - cuda::memcpy_async(cta, (&mainShmem[begSecRegShmem]), - &getSourceReduced(fbArgs, iterationNumb)[localBlockMetaData[(i & 1) * 20 + 14] - * fbArgs.metaData.mainArrSectionLength + fbArgs.metaData.mainArrXLength * (1 - isGoldForLocQueue[i])], - cuda::aligned_size_t<128>(sizeof(uint32_t) * fbArgs.metaData.mainArrXLength) - , pipeline); - } - pipeline.producer_commit(); - //process top - pipeline.consumer_wait(); - - - if (localBlockMetaData[(i & 1) * 20 + 13] < isGoldOffset) { - if (isBitAt(mainShmem[begSourceShmem + threadIdx.x + threadIdx.y * 32], 0)) { - // printf("setting padding top val %d \n ", isAnythingInPadding[0]); - isAnythingInPadding[0] = true; - }; - // if in bit of intrest of neighbour block is set - mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] |= ((mainShmem[begfirstRegShmem + threadIdx.x + threadIdx.y * 32] >> 31) & 1) << 0; - } - - pipeline.consumer_release(); - __syncthreads(); - - /////////// step 3 load right process bottom - pipeline.producer_acquire(); - if (localBlockMetaData[(i & 1) * 20 + 16] < isGoldOffset) { - cuda::memcpy_async(cta, (&mainShmem[begfirstRegShmem]), - &getSourceReduced(fbArgs, iterationNumb)[localBlockMetaData[(i & 1) * 20 + 16] * fbArgs.metaData.mainArrSectionLength + fbArgs.metaData.mainArrXLength * (1 - isGoldForLocQueue[i])], - cuda::aligned_size_t<128>(sizeof(uint32_t) * fbArgs.metaData.mainArrXLength) - , pipeline); - } - pipeline.producer_commit(); - //process bototm - pipeline.consumer_wait(); - - - if (localBlockMetaData[(i & 1) * 20 + 14] < isGoldOffset) { - if (isBitAt(mainShmem[begSourceShmem + threadIdx.x + threadIdx.y * 32], 31)) { - isAnythingInPadding[1] = true; - }; - // if in bit of intrest of neighbour block is set - mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] |= ((mainShmem[begSecRegShmem + threadIdx.x + threadIdx.y * 32] >> 0) & 1) << 31; - } - - - - /* dilatateHelperTopDown(1, mainShmem, isAnythingInPadding, localBlockMetaData, 14 - , 0, 31 - , begSecRegShmem, i);*/ - - pipeline.consumer_release(); - /////////// step 4 load left process right - //load left - pipeline.producer_acquire(); - if (mainShmem[startOfLocalWorkQ + i] > 0) { - cuda::memcpy_async(cta, (&mainShmem[begSecRegShmem]), - &getSourceReduced(fbArgs, iterationNumb)[(mainShmem[startOfLocalWorkQ + i] - 1) * fbArgs.metaData.mainArrSectionLength + fbArgs.metaData.mainArrXLength * (1 - isGoldForLocQueue[i])], - cuda::aligned_size_t<128>(sizeof(uint32_t) * fbArgs.metaData.mainArrXLength) - , pipeline); - } - pipeline.producer_commit(); - //process right - pipeline.consumer_wait(); - - if (threadIdx.x == (fbArgs.dbXLength - 1)) { - // now we need to load the data from the neigbouring blocks - //first checking is there anything to look to - if (localBlockMetaData[(i & 1) * 20 + 16] < isGoldOffset) { - //now we load - we already done earlier up and down so now we are considering only anterior, posterior , left , right possibilities - if (mainShmem[begSourceShmem + threadIdx.x + threadIdx.y * 32] > 0) { - isAnythingInPadding[3] = true; - - }; - mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] = - mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] - | mainShmem[begfirstRegShmem + (threadIdx.y * 32)]; - - }; - } - else {//given we are not in corner case we need just to do the dilatation using biwise or with the data inside the block - mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] - = mainShmem[begSourceShmem + (threadIdx.x + 1) + (threadIdx.y) * 32] - | mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32]; - - } - - pipeline.consumer_release(); - __syncthreads(); - /////// step 5 load anterior process left - //load anterior - pipeline.producer_acquire(); - if (localBlockMetaData[(i & 1) * 20 + 17] < isGoldOffset) { - - cuda::memcpy_async(cta, (&mainShmem[begfirstRegShmem]), - &getSourceReduced(fbArgs, iterationNumb)[localBlockMetaData[(i & 1) * 20 + 17] * fbArgs.metaData.mainArrSectionLength + fbArgs.metaData.mainArrXLength * (1 - isGoldForLocQueue[i])], - cuda::aligned_size_t<128>(sizeof(uint32_t) * fbArgs.metaData.mainArrXLength) - , pipeline); - } - pipeline.producer_commit(); - //process left - pipeline.consumer_wait(); - - // so we first check for corner cases - if (threadIdx.x == 0) { - // now we need to load the data from the neigbouring blocks - //first checking is there anything to look to - if (localBlockMetaData[(i & 1) * 20 + 15] < isGoldOffset) { - //now we load - we already done earlier up and down so now we are considering only anterior, posterior , left , right possibilities - if (mainShmem[begSourceShmem + threadIdx.x + threadIdx.y * 32] > 0) { - isAnythingInPadding[2] = true; - - }; - mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] = - mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] - | mainShmem[begSecRegShmem + 31 + threadIdx.y * 32]; - - }; - } - else {//given we are not in corner case we need just to do the dilatation using biwise or with the data inside the block - mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] - = mainShmem[begSourceShmem + (threadIdx.x - 1) + (threadIdx.y) * 32] - | mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32]; - - } - - - pipeline.consumer_release(); - __syncthreads(); - - /////// step 6 load posterior process anterior - //load posterior - pipeline.producer_acquire(); - if (localBlockMetaData[(i & 1) * 20 + 18] < isGoldOffset) { - - - cuda::memcpy_async(cta, (&mainShmem[begSecRegShmem]), - &getSourceReduced(fbArgs, iterationNumb)[localBlockMetaData[(i & 1) * 20 + 18] * fbArgs.metaData.mainArrSectionLength + fbArgs.metaData.mainArrXLength * (1 - isGoldForLocQueue[i])], - cuda::aligned_size_t<128>(sizeof(uint32_t) * fbArgs.metaData.mainArrXLength) - , pipeline); - } - pipeline.producer_commit(); - - //process anterior - pipeline.consumer_wait(); - - // so we first check for corner cases - if (threadIdx.y == (fbArgs.dbYLength - 1)) { - // now we need to load the data from the neigbouring blocks - //first checking is there anything to look to - if (localBlockMetaData[(i & 1) * 20 + 17] < isGoldOffset) { - //now we load - we already done earlier up and down so now we are considering only anterior, posterior , left , right possibilities - if (mainShmem[begSourceShmem + threadIdx.x + threadIdx.y * 32] > 0) { - isAnythingInPadding[4] = true; - - }; - mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] = - mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] - | mainShmem[begfirstRegShmem + threadIdx.x]; - - }; - } - else {//given we are not in corner case we need just to do the dilatation using biwise or with the data inside the block - mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] - = mainShmem[begSourceShmem + (threadIdx.x) + (threadIdx.y + 1) * 32] - | mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32]; - - } - - - pipeline.consumer_release(); - __syncthreads(); - - /////// step 7 - //load reference if needed or data for next iteration if there is such - //process posterior, save data from res shmem to global memory also we mark weather block is full - pipeline.producer_acquire(); - - //if block should be validated we load data for validation - if (localBlockMetaData[(i & 1) * 20 + ((1 - isGoldForLocQueue[i]) + 1)] //fp for gold and fn count for not gold - > localBlockMetaData[(i & 1) * 20 + ((1 - isGoldForLocQueue[i]) + 3)]) {// so count is bigger than counter so we should validate - cuda::memcpy_async(cta, (&mainShmem[begfirstRegShmem]), - &fbArgs.origArrsPointer[mainShmem[startOfLocalWorkQ + i] * fbArgs.metaData.mainArrSectionLength + fbArgs.metaData.mainArrXLength * (isGoldForLocQueue[i])], //we look for - cuda::aligned_size_t<128>(sizeof(uint32_t) * fbArgs.metaData.mainArrXLength) - , pipeline); - - } - else {//if we are not validating we immidiately start loading data for next loop - if (i + 1 < worQueueStep[0]) { - cuda::memcpy_async(cta, (&localBlockMetaData[((i + 1) & 1) * 20]), - (&fbArgs.metaDataArrPointer[(mainShmem[startOfLocalWorkQ + 1 + i]) - * fbArgs.metaData.metaDataSectionLength]) - , cuda::aligned_size_t<4>(sizeof(uint32_t) * 20), pipeline); - - - } - } - - - pipeline.producer_commit(); - - //processPosteriorAndSaveResShmem - - pipeline.consumer_wait(); - //dilatate posterior - - - // so we first check for corner cases - if (threadIdx.y == 0) { - // now we need to load the data from the neigbouring blocks - //first checking is there anything to look to - if (localBlockMetaData[(i & 1) * 20 + 18] < isGoldOffset) { - //now we load - we already done earlier up and down so now we are considering only anterior, posterior , left , right possibilities - if (mainShmem[begSourceShmem + threadIdx.x + threadIdx.y * 32] > 0) { - isAnythingInPadding[5] = true; - - }; - mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] = - mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] - | mainShmem[begSecRegShmem + threadIdx.x + (fbArgs.dbYLength - 1) * 32]; - - }; - } - else {//given we are not in corner case we need just to do the dilatation using biwise or with the data inside the block - mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32] - = mainShmem[begSourceShmem + (threadIdx.x) + (threadIdx.y - 1) * 32] - | mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32]; - - } - - //now all data should be properly dilatated we save it to global memory - //try save target reduced via mempcy async ... - - - //cuda::memcpy_async(cta, - // &getTargetReduced(fbArgs, iterationNumb)[mainShmem[startOfLocalWorkQ + i] * fbArgs.metaData.mainArrSectionLength + fbArgs.metaData.mainArrXLength * (1 - isGoldForLocQueue[i])] - // , (&mainShmem[begResShmem]), - // cuda::aligned_size_t<128>(sizeof(uint32_t) * fbArgs.metaData.mainArrXLength) - // , pipeline); - - - - getTargetReduced(fbArgs, iterationNumb)[mainShmem[startOfLocalWorkQ + i] * fbArgs.metaData.mainArrSectionLength + fbArgs.metaData.mainArrXLength * (1 - isGoldForLocQueue[i]) - + threadIdx.x + threadIdx.y * 32] - = mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32]; - - - - - - pipeline.consumer_release(); - - __syncthreads(); - - //////// step 8 basically in order to complete here anyting the count need to be bigger than counter - // loading for next block if block is not to be validated it was already done earlier - pipeline.producer_acquire(); - if (localBlockMetaData[(i & 1) * 20 + ((1 - isGoldForLocQueue[i]) + 1)] //fp for gold and fn count for not gold - > localBlockMetaData[(i & 1) * 20 + ((1 - isGoldForLocQueue[i]) + 3)]) {// so count is bigger than counter so we should validate - if (i + 1 < worQueueStep[0]) { - - - cuda::memcpy_async(cta, (&localBlockMetaData[((i + 1) & 1) * 20]), - (&fbArgs.metaDataArrPointer[(mainShmem[startOfLocalWorkQ + 1 + i]) - * fbArgs.metaData.metaDataSectionLength]) - , cuda::aligned_size_t<4>(sizeof(uint32_t) * 20), pipeline); - - } - } - pipeline.producer_commit(); - - - - - __syncthreads(); - - //validation - so looking for newly covered voxel for opposite array so new fps or new fns - pipeline.consumer_wait(); - - if (localBlockMetaData[(i & 1) * 20 + ((1 - isGoldForLocQueue[i]) + 1)] //fp for gold and fn count for not gold - > localBlockMetaData[(i & 1) * 20 + ((1 - isGoldForLocQueue[i]) + 3)]) {// so count is bigger than counter so we should validate - // now we look through bits and when some is set we call it a result - #pragma unroll - for (uint8_t bitPos = 0; bitPos < 32; bitPos++) { - //if any bit here is set it means it should be added to result list - if (isBitAt(mainShmem[begResShmem + threadIdx.x + threadIdx.y * 32], bitPos) - && !isBitAt(mainShmem[begSourceShmem + threadIdx.x + threadIdx.y * 32], bitPos) - && isBitAt(mainShmem[begfirstRegShmem + threadIdx.x + threadIdx.y * 32], bitPos) - ) { - - //just re - mainShmem[begSecRegShmem + threadIdx.x + threadIdx.y * 32] = 0; - ////// IMPORTANT for some reason in order to make it work resultfnOffset and resultfnOffset swith places - if (isGoldForLocQueue[i]) { - mainShmem[begSecRegShmem + threadIdx.x + threadIdx.y * 32] = uint32_t(atomicAdd_block(&(localFpConter[0]), 1) + localBlockMetaData[(i & 1) * 20 + 6] + localBlockMetaData[(i & 1) * 20 + 3]); - - } - else { - - mainShmem[begSecRegShmem + threadIdx.x + threadIdx.y * 32] = uint32_t(atomicAdd_block(&(localFnConter[0]), 1) + localBlockMetaData[(i & 1) * 20 + 5] + localBlockMetaData[(i & 1) * 20 + 4]); - - - }; - // add results to global memory - //we add one gere jjust to distinguish it from empty result - fbArgs.resultListPointerMeta[mainShmem[begSecRegShmem + threadIdx.x + threadIdx.y * 32]] = uint32_t(mainShmem[startOfLocalWorkQ + i] + (isGoldOffset * isGoldForLocQueue[i]) + 1); - fbArgs.resultListPointerLocal[mainShmem[begSecRegShmem + threadIdx.x + threadIdx.y * 32]] = uint32_t((fbArgs.dbYLength * 32 * bitPos) + (threadIdx.y * 32) + (threadIdx.x)); - fbArgs.resultListPointerIterNumb[mainShmem[begSecRegShmem + threadIdx.x + threadIdx.y * 32]] = uint32_t(iterationNumb[0]); - - - - - } - - }; - - } - ///////// - pipeline.consumer_release(); - - /// /// cleaning - - __syncthreads(); - - if (threadIdx.x == 9 && threadIdx.y == 2) {// this is how it is encoded wheather it is gold or segm block - - //executed in case of previous block - if (isBlockFull[i & 1] && i >= 0) { - //setting data in metadata that block is full - fbArgs.metaDataArrPointer[mainShmem[startOfLocalWorkQ + i] * fbArgs.metaData.metaDataSectionLength + 10 - (isGoldForLocQueue[i] * 2)] = true; - } - //resetting for some reason block 0 gets as full even if it should not ... - isBlockFull[i & 1] = true;// mainShmem[startOfLocalWorkQ + i]>0;//!isPaddingPass; - }; - - - - - //we do it only for non padding pass - if (threadIdx.x < 6 && threadIdx.y == 1 && !isPaddingPass) { - //executed in case of previous block - if (i >= 0) { - - if (localBlockMetaData[(i & 1) * 20 + 13 + threadIdx.x] < isGoldOffset) { - - if (isAnythingInPadding[threadIdx.x]) { - // holding data weather we have anything in padding 0)top 1)bottom, 2)left 3)right, 4)anterior, 5)posterior, - fbArgs.metaDataArrPointer[localBlockMetaData[(i & 1) * 20 + 13 + threadIdx.x] * fbArgs.metaData.metaDataSectionLength + 12 - isGoldForLocQueue[i]] = 1; - } - - } - } - isAnythingInPadding[threadIdx.x] = false; - }; - - - - - - - if (threadIdx.x == 7 && threadIdx.y == 0) { - //this will be executed only if fp or fn counters are bigger than 0 so not during first pass - if (localFpConter[0] > 0) { - fbArgs.metaDataArrPointer[mainShmem[startOfLocalWorkQ + i] * fbArgs.metaData.metaDataSectionLength + 3] += localFpConter[0]; - - blockFpConter[0] += localFpConter[0]; - localFpConter[0] = 0; - } - - - }; - if (threadIdx.x == 8 && threadIdx.y == 0) { - - if (localFnConter[0] > 0) { - fbArgs.metaDataArrPointer[mainShmem[startOfLocalWorkQ + i] * fbArgs.metaData.metaDataSectionLength + 4] += localFnConter[0]; - - blockFnConter[0] += localFnConter[0]; - localFnConter[0] = 0; - } - }; - - __syncthreads(); - - } - } - - //here we are after all of the blocks planned to be processed by this block are - - // just for pipeline to work - pipeline.consumer_wait(); - - - - pipeline.consumer_release(); - - } - - __syncthreads(); - - // updating global counters - if (threadIdx.x == 0 && threadIdx.y == 0) { - if (blockFpConter[0] > 0) { - atomicAdd(&(fbArgs.minMaxes[10]), (blockFpConter[0])); - } - }; - if (threadIdx.x == 1 && threadIdx.y == 0) { - if (blockFnConter[0] > 0) { - //if (blockFnConter[0]>10) { - // printf("Fn %d ", blockFnConter[0]); - //} - atomicAdd(&(fbArgs.minMaxes[11]), (blockFnConter[0])); - } - }; - - // in first thread block we zero work queue counter - if (threadIdx.x == 2 && threadIdx.y == 0) { - if (blockIdx.x == 0) { - - fbArgs.minMaxes[9] = 0; - } - }; - - grid.sync(); - /////////////////////////**************************************************************************************************************** - - /// metadata pass - - - // preparation loads - if (threadIdx.x == 0 && threadIdx.y == 0) { - fpFnLocCounter[0] = 0; - } - if (threadIdx.x == 1 && threadIdx.y == 0) { - localWorkQueueCounter[0] = 0; - } - if (threadIdx.x == 2 && threadIdx.y == 0) { - localWorkQueueCounter[0] = 0; - } - if (threadIdx.x == 3 && threadIdx.y == 0) { - localWorkQueueCounter[0] = 0; - - } - - if (threadIdx.x == 0 && threadIdx.y == 1) { - - isGoldPassToContinue[0] - = ((fbArgs.minMaxes[7] * fbArgs.robustnessPercent) > fbArgs.minMaxes[10]); - - }; - - if (threadIdx.x == 0 && threadIdx.y == 1) { - - isSegmPassToContinue[0] - = ((fbArgs.minMaxes[8] * fbArgs.robustnessPercent) > fbArgs.minMaxes[11]); - }; - - - __syncthreads(); - - ///////////////////////////////// - - for (uint32_t linIdexMeta = blockIdx.x * blockDim.x * blockDim.y + threadIdx.y * blockDim.x + threadIdx.x - ; linIdexMeta <= fbArgs.metaData.totalMetaLength - ; linIdexMeta += (blockDim.x * blockDim.y * gridDim.x) - ) { - - - if (isPaddingPass == 0) { - - //goldpass - if (isGoldPassToContinue[0] && fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 11] - && !fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 7] - && !fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 8]) { - - mainShmem[atomicAdd_block(&localWorkQueueCounter[0], 1)] = linIdexMeta + (isGoldOffset); - //setting to be activated to 0 - fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 11] = 0; - //setting active to 1 - fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 7] = 1; - - - }; - - } - //contrary to number it is when we are not in padding pass - else { - //gold pass - if (isGoldPassToContinue[0] && fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 7] - && !fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 8]) { - - mainShmem[atomicAdd_block(&localWorkQueueCounter[0], 1)] = linIdexMeta + (isGoldOffset); - - }; - - } - } - - __syncthreads(); - - if (localWorkQueueCounter[0] > 0) { - if (threadIdx.x == 0 && threadIdx.y == 0) { - globalWorkQueueCounter[0] = atomicAdd(&(fbArgs.minMaxes[9]), (localWorkQueueCounter[0])); - - - } - __syncthreads(); - for (uint32_t linI = threadIdx.y * blockDim.x + threadIdx.x; linI < localWorkQueueCounter[0]; linI += blockDim.x * blockDim.y) { - fbArgs.workQueuePointer[globalWorkQueueCounter[0] + linI] = mainShmem[linI]; - } - __syncthreads(); - - } - - __syncthreads(); - - if (threadIdx.x == 0 && threadIdx.y == 0) { - - localWorkQueueCounter[0] = 0; - } - __syncthreads(); - - for (uint32_t linIdexMeta = blockIdx.x * blockDim.x * blockDim.y + threadIdx.y * blockDim.x + threadIdx.x - ; linIdexMeta <= fbArgs.metaData.totalMetaLength - ; linIdexMeta += (blockDim.x * blockDim.y * gridDim.x) - ) { - - - if (isPaddingPass == 0) { - - //segm pass - if ((isSegmPassToContinue[0] && fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 12] - && !fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 9] - && !fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 10])) { - - - - mainShmem[atomicAdd_block(&localWorkQueueCounter[0], 1)] = linIdexMeta; - - //setting to be activated to 0 - fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 12] = 0; - //setting active to 1 - fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 9] = 1; - - } - - } - //contrary to number it is when we are not in padding pass - else { - //segm pass - if ((isSegmPassToContinue[0] && fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 9] - && !fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 10])) { - - - - mainShmem[atomicAdd_block(&localWorkQueueCounter[0], 1)] = linIdexMeta; - } - - } - } - __syncthreads(); - - if (localWorkQueueCounter[0] > 0) { - if (threadIdx.x == 0 && threadIdx.y == 0) { - globalWorkQueueCounter[0] = atomicAdd(&(fbArgs.minMaxes[9]), (localWorkQueueCounter[0])); - - - } - __syncthreads(); - for (uint32_t linI = threadIdx.y * blockDim.x + threadIdx.x; linI < localWorkQueueCounter[0]; linI += blockDim.x * blockDim.y) { - fbArgs.workQueuePointer[globalWorkQueueCounter[0] + linI] = mainShmem[linI]; - - } - - } - - - - grid.sync(); + // segm pass + if ((isSegmPassToContinue[0] && + fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 9] && + !fbArgs.metaDataArrPointer[linIdexMeta * fbArgs.metaData.metaDataSectionLength + 10])) { + mainShmem[atomicAdd_block(&localWorkQueueCounter[0], 1)] = linIdexMeta; + } } + } + __syncthreads(); + if (localWorkQueueCounter[0] > 0) { + if (threadIdx.x == 0 && threadIdx.y == 0) { + globalWorkQueueCounter[0] = atomicAdd(&(fbArgs.minMaxes[9]), (localWorkQueueCounter[0])); + } + __syncthreads(); + for (uint32_t linI = threadIdx.y * blockDim.x + threadIdx.x; linI < localWorkQueueCounter[0]; + linI += blockDim.x * blockDim.y) { + fbArgs.workQueuePointer[globalWorkQueueCounter[0] + linI] = mainShmem[linI]; + } + } + grid.sync(); + } - } while (isGoldPassToContinue[0] || isSegmPassToContinue[0]); - + } while (isGoldPassToContinue[0] || isSegmPassToContinue[0]); - //setting global iteration number to local one - if (blockIdx.x == 0) { - if (threadIdx.x == 2 && threadIdx.y == 0) { - fbArgs.metaData.minMaxes[13] = iterationNumb[0]; - } + // setting global iteration number to local one + if (blockIdx.x == 0) { + if (threadIdx.x == 2 && threadIdx.y == 0) { + fbArgs.metaData.minMaxes[13] = iterationNumb[0]; } + } } - - - - - - - /*************************************** * putting all kernels and memory allocations together * ********************************/ - /* get data from occupancy calculator API used to get optimal number of thread blocks and threads per thread block */ template inline occupancyCalcData getOccupancy() { - - occupancyCalcData res; - - int blockSize; // The launch configurator returned block size - int minGridSize; // The minimum grid size needed to achieve the maximum occupancy for a full device launch - int gridSize; // The actual grid size needed, based on input size - - // for min maxes kernel - cudaOccupancyMaxPotentialBlockSize( - &minGridSize, - &blockSize, - (void*)getMinMaxes, - 0); - res.warpsNumbForMinMax = blockSize / 32; - res.blockSizeForMinMax = minGridSize; - - // for min maxes kernel - cudaOccupancyMaxPotentialBlockSize( - &minGridSize, - &blockSize, - (void*)boolPrepareKernel, - 0); - res.warpsNumbForboolPrepareKernel = blockSize / 32; - res.blockSizeFoboolPrepareKernel = minGridSize; - // for first meta pass kernel - cudaOccupancyMaxPotentialBlockSize( - &minGridSize, - &blockSize, - (void*)boolPrepareKernel, - 0); - res.theadsForFirstMetaPass = blockSize; - res.blockForFirstMetaPass = minGridSize; - //for main pass kernel - cudaOccupancyMaxPotentialBlockSize( - &minGridSize, - &blockSize, - (void*)mainPassKernel, - 0); - res.warpsNumbForMainPass = blockSize / 32; - res.blockForMainPass = minGridSize; - - //printf("warpsNumbForMainPass %d blockForMainPass %d ", res.warpsNumbForMainPass, res.blockForMainPass); - return res; + occupancyCalcData res; + + int blockSize; // The launch configurator returned block size + int minGridSize; // The minimum grid size needed to achieve the maximum occupancy for a full device launch + int gridSize; // The actual grid size needed, based on input size + + // for min maxes kernel + cudaOccupancyMaxPotentialBlockSize(&minGridSize, &blockSize, (void*)getMinMaxes, 0); + res.warpsNumbForMinMax = blockSize / 32; + res.blockSizeForMinMax = minGridSize; + + // for min maxes kernel + cudaOccupancyMaxPotentialBlockSize(&minGridSize, &blockSize, (void*)boolPrepareKernel, 0); + res.warpsNumbForboolPrepareKernel = blockSize / 32; + res.blockSizeFoboolPrepareKernel = minGridSize; + // for first meta pass kernel + cudaOccupancyMaxPotentialBlockSize(&minGridSize, &blockSize, (void*)boolPrepareKernel, 0); + res.theadsForFirstMetaPass = blockSize; + res.blockForFirstMetaPass = minGridSize; + // for main pass kernel + cudaOccupancyMaxPotentialBlockSize(&minGridSize, &blockSize, (void*)mainPassKernel, 0); + res.warpsNumbForMainPass = blockSize / 32; + res.blockForMainPass = minGridSize; + + // printf("warpsNumbForMainPass %d blockForMainPass %d ", res.warpsNumbForMainPass, res.blockForMainPass); + return res; } - - /* TODO consider representing as a CUDA graph executing Algorithm as CUDA graph based on official documentation and @@ -2032,198 +1913,225 @@ https://codingbyexample.com/2020/09/25/cuda-graph-usage/ */ #pragma once template -ForBoolKernelArgs executeHausdoff(ForFullBoolPrepArgs& fFArgs, const int WIDTH, const int HEIGHT, const int DEPTH, occupancyCalcData& occData, - cudaStream_t stream, bool resToSave, float robustnessPercent, bool resIterNeeded) { - fFArgs.robustnessPercent = robustnessPercent; - T* goldArrPointer = (T*)fFArgs.goldArr.data_ptr(); - T* segmArrPointer = (T*)fFArgs.segmArr.data_ptr(); - - ForBoolKernelArgs fbArgs = getArgsForKernel(fFArgs, occData.warpsNumbForMainPass, occData.blockForMainPass, WIDTH, HEIGHT, DEPTH, stream); - - getMinMaxes << > > (fbArgs, fbArgs.minMaxes - , goldArrPointer - , segmArrPointer - , fbArgs.metaData); - - - - fbArgs.metaData = allocateMemoryAfterMinMaxesKernel(fbArgs, fFArgs, stream); - fbArgs.robustnessPercent = robustnessPercent; - boolPrepareKernel << > > ( - fbArgs, fbArgs.metaData, fbArgs.origArrsPointer, fbArgs.metaDataArrPointer - , goldArrPointer - , segmArrPointer - , fbArgs.minMaxes); - - - int fpPlusFn = allocateMemoryAfterBoolKernel(fbArgs, fFArgs, stream, resIterNeeded); - - - - firstMetaPrepareKernel << > > (fbArgs, fbArgs.metaData, fbArgs.minMaxes, fbArgs.workQueuePointer, fbArgs.origArrsPointer, fbArgs.metaDataArrPointer); - - - - void* kernel_args[] = { &fbArgs }; - cudaLaunchCooperativeKernel((void*)(mainPassKernel), occData.blockForMainPass, dim3(32, occData.warpsNumbForMainPass), kernel_args, 0,stream); - - - //copy to the output tensor the rsult - if (resIterNeeded) { - auto size = sizeof(int32_t) * fbArgs.resultListPointerIterNumbTensor.sizes()[0]; - cudaMemcpyAsync(fbArgs.resultListPointerIterNumbTensor.data_ptr(), fbArgs.resultListPointerIterNumb, size, cudaMemcpyDeviceToDevice, stream); - } - - - cudaFreeAsync(fbArgs.resultListPointerMeta, stream); - cudaFreeAsync(fbArgs.resultListPointerLocal, stream); - cudaFreeAsync(fbArgs.resultListPointerIterNumb, stream); - cudaFreeAsync(fbArgs.workQueuePointer, stream); - cudaFreeAsync(fbArgs.origArrsPointer, stream); - cudaFreeAsync(fbArgs.metaDataArrPointer, stream); - cudaFreeAsync(fbArgs.mainArrAPointer, stream); - cudaFreeAsync(fbArgs.mainArrBPointer, stream); - - - return fbArgs; - +ForBoolKernelArgs executeHausdoff( + ForFullBoolPrepArgs& fFArgs, + const int WIDTH, + const int HEIGHT, + const int DEPTH, + occupancyCalcData& occData, + cudaStream_t stream, + bool resToSave, + float robustnessPercent, + bool resIterNeeded) { + fFArgs.robustnessPercent = robustnessPercent; + T* goldArrPointer = (T*)fFArgs.goldArr.data_ptr(); + T* segmArrPointer = (T*)fFArgs.segmArr.data_ptr(); + + ForBoolKernelArgs fbArgs = + getArgsForKernel(fFArgs, occData.warpsNumbForMainPass, occData.blockForMainPass, WIDTH, HEIGHT, DEPTH, stream); + + getMinMaxes<<>>( + fbArgs, fbArgs.minMaxes, goldArrPointer, segmArrPointer, fbArgs.metaData); + + fbArgs.metaData = allocateMemoryAfterMinMaxesKernel(fbArgs, fFArgs, stream); + fbArgs.robustnessPercent = robustnessPercent; + boolPrepareKernel<<< + occData.blockSizeFoboolPrepareKernel, + dim3(32, occData.warpsNumbForboolPrepareKernel), + 0, + stream>>>( + fbArgs, + fbArgs.metaData, + fbArgs.origArrsPointer, + fbArgs.metaDataArrPointer, + goldArrPointer, + segmArrPointer, + fbArgs.minMaxes); + + int fpPlusFn = allocateMemoryAfterBoolKernel(fbArgs, fFArgs, stream, resIterNeeded); + + firstMetaPrepareKernel<<>>( + fbArgs, + fbArgs.metaData, + fbArgs.minMaxes, + fbArgs.workQueuePointer, + fbArgs.origArrsPointer, + fbArgs.metaDataArrPointer); + + void* kernel_args[] = {&fbArgs}; + cudaLaunchCooperativeKernel( + (void*)(mainPassKernel), + occData.blockForMainPass, + dim3(32, occData.warpsNumbForMainPass), + kernel_args, + 0, + stream); + + // copy to the output tensor the rsult + if (resIterNeeded) { + auto size = sizeof(int32_t) * fbArgs.resultListPointerIterNumbTensor.sizes()[0]; + cudaMemcpyAsync( + fbArgs.resultListPointerIterNumbTensor.data_ptr(), + fbArgs.resultListPointerIterNumb, + size, + cudaMemcpyDeviceToDevice, + stream); + } + + cudaFreeAsync(fbArgs.resultListPointerMeta, stream); + cudaFreeAsync(fbArgs.resultListPointerLocal, stream); + cudaFreeAsync(fbArgs.resultListPointerIterNumb, stream); + cudaFreeAsync(fbArgs.workQueuePointer, stream); + cudaFreeAsync(fbArgs.origArrsPointer, stream); + cudaFreeAsync(fbArgs.metaDataArrPointer, stream); + cudaFreeAsync(fbArgs.mainArrAPointer, stream); + cudaFreeAsync(fbArgs.mainArrBPointer, stream); + + return fbArgs; } - - -// modified from https://github.com/pytorch/pytorch/blob/61d6c4386459441710fb4cfa2929a3f77e95e5f7/aten/src/ATen/Dispatch.h -#define AT_DISPATCH_ALL_TYPESWithBool(TYPE, NAME, ...) \ - [&] { \ - const auto& the_type = TYPE; \ - /* don't use TYPE again in case it is an expensive or side-effect op */ \ - at::ScalarType _st = ::detail::scalar_type(the_type); \ - RECORD_KERNEL_FUNCTION_DTYPE(NAME, _st); \ - switch (_st) { \ - AT_PRIVATE_CASE_TYPE(NAME, at::ScalarType::Byte, uint8_t, __VA_ARGS__) \ - AT_PRIVATE_CASE_TYPE(NAME, at::ScalarType::Char, int8_t, __VA_ARGS__) \ - AT_PRIVATE_CASE_TYPE(NAME, at::ScalarType::Double, double, __VA_ARGS__) \ - AT_PRIVATE_CASE_TYPE(NAME, at::ScalarType::Float, float, __VA_ARGS__) \ - AT_PRIVATE_CASE_TYPE(NAME, at::ScalarType::Int, int32_t, __VA_ARGS__) \ - AT_PRIVATE_CASE_TYPE(NAME, at::ScalarType::Long, int64_t, __VA_ARGS__) \ - AT_PRIVATE_CASE_TYPE(NAME, at::ScalarType::Short, int16_t, __VA_ARGS__) \ - AT_PRIVATE_CASE_TYPE(NAME, at::ScalarType::Bool, bool, __VA_ARGS__) \ - default: \ - AT_ERROR(#NAME, " not implemented for '", toString(_st), "'"); \ - } \ +// modified from +// https://github.com/pytorch/pytorch/blob/61d6c4386459441710fb4cfa2929a3f77e95e5f7/aten/src/ATen/Dispatch.h +#define AT_DISPATCH_ALL_TYPESWithBool(TYPE, NAME, ...) \ + [&] { \ + const auto& the_type = TYPE; \ + /* don't use TYPE again in case it is an expensive or side-effect op */ \ + at::ScalarType _st = ::detail::scalar_type(the_type); \ + RECORD_KERNEL_FUNCTION_DTYPE(NAME, _st); \ + switch (_st) { \ + AT_PRIVATE_CASE_TYPE(NAME, at::ScalarType::Byte, uint8_t, __VA_ARGS__) \ + AT_PRIVATE_CASE_TYPE(NAME, at::ScalarType::Char, int8_t, __VA_ARGS__) \ + AT_PRIVATE_CASE_TYPE(NAME, at::ScalarType::Double, double, __VA_ARGS__) \ + AT_PRIVATE_CASE_TYPE(NAME, at::ScalarType::Float, float, __VA_ARGS__) \ + AT_PRIVATE_CASE_TYPE(NAME, at::ScalarType::Int, int32_t, __VA_ARGS__) \ + AT_PRIVATE_CASE_TYPE(NAME, at::ScalarType::Long, int64_t, __VA_ARGS__) \ + AT_PRIVATE_CASE_TYPE(NAME, at::ScalarType::Short, int16_t, __VA_ARGS__) \ + AT_PRIVATE_CASE_TYPE(NAME, at::ScalarType::Bool, bool, __VA_ARGS__) \ + default: \ + AT_ERROR(#NAME, " not implemented for '", toString(_st), "'"); \ + } \ }() - - - - template -int getHausdorffDistance_CUDA_Generic(at::Tensor goldStandard, - at::Tensor algoOutput - , int WIDTH, int HEIGHT, int DEPTH, float robustnessPercent, bool resIterNeeded, at::Tensor compare_values) { - //TODO() use https ://pytorch.org/cppdocs/notes/tensor_cuda_stream.html - cudaStream_t stream1; - cudaStreamCreate(&stream1); - - MetaDataCPU metaData; - //size_t size = sizeof(unsigned int) * 20; - //unsigned int* minMaxesCPU = (unsigned int*)malloc(size); - //metaData.minMaxes = minMaxesCPU; - - ForFullBoolPrepArgs forFullBoolPrepArgs; - forFullBoolPrepArgs.metaData = metaData; - forFullBoolPrepArgs.compare_values = compare_values.item(); - forFullBoolPrepArgs.goldArr = goldStandard; - forFullBoolPrepArgs.segmArr = algoOutput; - - occupancyCalcData occData = getOccupancy(); - - ForBoolKernelArgs fbArgs = executeHausdoff(forFullBoolPrepArgs, WIDTH, HEIGHT, DEPTH, occData, stream1, false, robustnessPercent, resIterNeeded); - - size_t sizeMinMax = sizeof(unsigned int) * 20; - //making sure we have all resultsto copy on cpu - cudaDeviceSynchronize(); - cudaMemcpy(metaData.minMaxes, fbArgs.metaData.minMaxes, sizeMinMax, cudaMemcpyDeviceToHost); - - int result = metaData.minMaxes[13]; - - cudaFreeAsync(fbArgs.minMaxes, stream1); - //free(minMaxesCPU); - - - cudaStreamDestroy(stream1); - cudaDeviceSynchronize(); - - return result; +int getHausdorffDistance_CUDA_Generic( + at::Tensor goldStandard, + at::Tensor algoOutput, + int WIDTH, + int HEIGHT, + int DEPTH, + float robustnessPercent, + bool resIterNeeded, + at::Tensor compare_values) { + // TODO() use https ://pytorch.org/cppdocs/notes/tensor_cuda_stream.html + cudaStream_t stream1; + cudaStreamCreate(&stream1); + + MetaDataCPU metaData; + // size_t size = sizeof(unsigned int) * 20; + // unsigned int* minMaxesCPU = (unsigned int*)malloc(size); + // metaData.minMaxes = minMaxesCPU; + + ForFullBoolPrepArgs forFullBoolPrepArgs; + forFullBoolPrepArgs.metaData = metaData; + forFullBoolPrepArgs.compare_values = compare_values.item(); + forFullBoolPrepArgs.goldArr = goldStandard; + forFullBoolPrepArgs.segmArr = algoOutput; + + occupancyCalcData occData = getOccupancy(); + + ForBoolKernelArgs fbArgs = executeHausdoff( + forFullBoolPrepArgs, WIDTH, HEIGHT, DEPTH, occData, stream1, false, robustnessPercent, resIterNeeded); + + size_t sizeMinMax = sizeof(unsigned int) * 20; + // making sure we have all resultsto copy on cpu + cudaDeviceSynchronize(); + cudaMemcpy(metaData.minMaxes, fbArgs.metaData.minMaxes, sizeMinMax, cudaMemcpyDeviceToHost); + + int result = metaData.minMaxes[13]; + + cudaFreeAsync(fbArgs.minMaxes, stream1); + // free(minMaxesCPU); + + cudaStreamDestroy(stream1); + cudaDeviceSynchronize(); + + return result; } - template -at::Tensor getHausdorffDistance_CUDA_FullResList_local(at::Tensor goldStandard, - at::Tensor algoOutput - , int WIDTH, int HEIGHT, int DEPTH, float robustnessPercent, at::Tensor compare_values) { - //TODO() use https ://pytorch.org/cppdocs/notes/tensor_cuda_stream.html - cudaStream_t stream1; - cudaStreamCreate(&stream1); - - MetaDataCPU metaData; - //size_t size = sizeof(unsigned int) * 20; - //unsigned int* minMaxesCPU = (unsigned int*)malloc(size); - //metaData.minMaxes = minMaxesCPU; - - ForFullBoolPrepArgs forFullBoolPrepArgs; - forFullBoolPrepArgs.metaData = metaData; - forFullBoolPrepArgs.compare_values = compare_values.item(); - forFullBoolPrepArgs.goldArr = goldStandard; - forFullBoolPrepArgs.segmArr = algoOutput; - - occupancyCalcData occData = getOccupancy(); - - ForBoolKernelArgs fbArgs = executeHausdoff(forFullBoolPrepArgs, WIDTH, HEIGHT, DEPTH, occData, stream1, false, robustnessPercent, true); - - - - - cudaFreeAsync(fbArgs.minMaxes, stream1); - //free(metaData.minMaxesCPU); - - - - cudaStreamDestroy(stream1); - - return fbArgs.resultListPointerIterNumbTensor; +at::Tensor getHausdorffDistance_CUDA_FullResList_local( + at::Tensor goldStandard, + at::Tensor algoOutput, + int WIDTH, + int HEIGHT, + int DEPTH, + float robustnessPercent, + at::Tensor compare_values) { + // TODO() use https ://pytorch.org/cppdocs/notes/tensor_cuda_stream.html + cudaStream_t stream1; + cudaStreamCreate(&stream1); + + MetaDataCPU metaData; + // size_t size = sizeof(unsigned int) * 20; + // unsigned int* minMaxesCPU = (unsigned int*)malloc(size); + // metaData.minMaxes = minMaxesCPU; + + ForFullBoolPrepArgs forFullBoolPrepArgs; + forFullBoolPrepArgs.metaData = metaData; + forFullBoolPrepArgs.compare_values = compare_values.item(); + forFullBoolPrepArgs.goldArr = goldStandard; + forFullBoolPrepArgs.segmArr = algoOutput; + + occupancyCalcData occData = getOccupancy(); + + ForBoolKernelArgs fbArgs = + executeHausdoff(forFullBoolPrepArgs, WIDTH, HEIGHT, DEPTH, occData, stream1, false, robustnessPercent, true); + + cudaFreeAsync(fbArgs.minMaxes, stream1); + // free(metaData.minMaxesCPU); + + cudaStreamDestroy(stream1); + + return fbArgs.resultListPointerIterNumbTensor; } /* Functions for pybind */ -int getHausdorffDistance_CUDA(at::Tensor goldStandard, - at::Tensor algoOutput - , const int WIDTH, const int HEIGHT, const int DEPTH - , const float robustnessPercent, at::Tensor compare_values) { - - int res = 0; - - AT_DISPATCH_ALL_TYPESWithBool(goldStandard.type(), "getHausdorffDistance_CUDA", ([&] { - res = getHausdorffDistance_CUDA_Generic(goldStandard, algoOutput, WIDTH, HEIGHT, DEPTH, robustnessPercent, false, compare_values); - - })); - return res; +int getHausdorffDistance_CUDA( + at::Tensor goldStandard, + at::Tensor algoOutput, + const int WIDTH, + const int HEIGHT, + const int DEPTH, + const float robustnessPercent, + at::Tensor compare_values) { + int res = 0; + + AT_DISPATCH_ALL_TYPESWithBool( + goldStandard.type(), "getHausdorffDistance_CUDA", ([&] { + res = getHausdorffDistance_CUDA_Generic( + goldStandard, algoOutput, WIDTH, HEIGHT, DEPTH, robustnessPercent, false, compare_values); + })); + return res; } - -at::Tensor getHausdorffDistance_CUDA_FullResList(at::Tensor goldStandard, - at::Tensor algoOutput - , const int WIDTH, const int HEIGHT, const int DEPTH - , const float robustnessPercent, at::Tensor compare_values) { - - - at::Tensor res; - AT_DISPATCH_ALL_TYPESWithBool(goldStandard.type(), "getHausdorffDistance_CUDA_FullResList", ([&] { - res = getHausdorffDistance_CUDA_FullResList_local(goldStandard, algoOutput, WIDTH, HEIGHT, DEPTH, robustnessPercent, compare_values); - - })); - - return res; +at::Tensor getHausdorffDistance_CUDA_FullResList( + at::Tensor goldStandard, + at::Tensor algoOutput, + const int WIDTH, + const int HEIGHT, + const int DEPTH, + const float robustnessPercent, + at::Tensor compare_values) { + at::Tensor res; + AT_DISPATCH_ALL_TYPESWithBool( + goldStandard.type(), "getHausdorffDistance_CUDA_FullResList", ([&] { + res = getHausdorffDistance_CUDA_FullResList_local( + goldStandard, algoOutput, WIDTH, HEIGHT, DEPTH, robustnessPercent, compare_values); + })); + + return res; } @@ -2235,356 +2143,379 @@ at::Tensor getHausdorffDistance_CUDA_FullResList(at::Tensor goldStandard, <<<<<<< HEAD ======= - /************************************************************* -*Oliviera Algorithm -*************************************************************************************/ - + *Oliviera Algorithm + *************************************************************************************/ typedef unsigned char uchar; typedef unsigned int uint; #pragma once class Volume { - -private: - bool* volume; - int width, height, depth; - int getLinearIndex(int x, int y, int z); -public: - bool getVoxelValue(int x, int y, int z); - bool getPixelValue(int x, int y); - uint getWidth(); - uint getHeight(); - uint getDepth(); - bool* getVolume(); - void setVoxelValue(bool value, int x, int y, int z); - void setPixelValue(bool value, int x, int y); - Volume(int width, int height, int depth); - Volume(int width, int height); - void dispose(); - + private: + bool* volume; + int width, height, depth; + int getLinearIndex(int x, int y, int z); + + public: + bool getVoxelValue(int x, int y, int z); + bool getPixelValue(int x, int y); + uint getWidth(); + uint getHeight(); + uint getDepth(); + bool* getVolume(); + void setVoxelValue(bool value, int x, int y, int z); + void setPixelValue(bool value, int x, int y); + Volume(int width, int height, int depth); + Volume(int width, int height); + void dispose(); }; +#define CUDA_DEVICE_INDEX 0 // setting the index of your CUDA device - - -#define CUDA_DEVICE_INDEX 0 //setting the index of your CUDA device - -#define IS_3D 1 //setting this to 0 would grant a very slightly improvement on the performance if working with images only -#define CHEBYSHEV 0 //if not set to 1, then this algorithm would use an Euclidean-like metric, it is just an approximation. -//It can be changed according to the structuring element +#define IS_3D \ + 1 // setting this to 0 would grant a very slightly improvement on the performance if working with images only +#define CHEBYSHEV \ + 0 // if not set to 1, then this algorithm would use an Euclidean-like metric, it is just an approximation. +// It can be changed according to the structuring element #pragma once class HausdorffDistance { + private: + void print(cudaError_t error, char* msg); -private: - void print(cudaError_t error, char* msg); - -public: - int computeDistance(Volume* img1, Volume* img2, bool* d_img1, bool* d_img2); - + public: + int computeDistance(Volume* img1, Volume* img2, bool* d_img1, bool* d_img2); }; - inline Volume::Volume(const int width, const int height, const int depth) { - this->width = width; this->height = height; this->depth = depth; - volume = (bool*)calloc(width * height * depth, sizeof(bool)); + this->width = width; + this->height = height; + this->depth = depth; + volume = (bool*)calloc(width * height * depth, sizeof(bool)); } #pragma once inline Volume::Volume(const int width, const int height) { - this->width = width; this->height = height; this->depth = 1; - volume = (bool*)calloc(width * height * depth, sizeof(bool)); + this->width = width; + this->height = height; + this->depth = 1; + volume = (bool*)calloc(width * height * depth, sizeof(bool)); } #pragma once inline int Volume::getLinearIndex(const int x, const int y, const int z) { - const int a = 1, b = width, c = (width) * (height); - return a * x + b * y + c * z; + const int a = 1, b = width, c = (width) * (height); + return a * x + b * y + c * z; } -inline uint Volume::getWidth() { return this->width; } -inline uint Volume::getHeight() { return this->height; } -inline uint Volume::getDepth() { return this->depth; } -inline bool* Volume::getVolume() { return this->volume; } -inline bool Volume::getPixelValue(int x, int y) { return this->volume[getLinearIndex(x, y, 0)]; } +inline uint Volume::getWidth() { + return this->width; +} +inline uint Volume::getHeight() { + return this->height; +} +inline uint Volume::getDepth() { + return this->depth; +} +inline bool* Volume::getVolume() { + return this->volume; +} +inline bool Volume::getPixelValue(int x, int y) { + return this->volume[getLinearIndex(x, y, 0)]; +} #pragma once inline bool Volume::getVoxelValue(int x, int y, int z) { - return volume[getLinearIndex(x, y, z)]; + return volume[getLinearIndex(x, y, z)]; } #pragma once inline void Volume::setPixelValue(bool value, const int x, const int y) { - volume[getLinearIndex(x, y, 0)] = value; + volume[getLinearIndex(x, y, 0)] = value; } #pragma once inline void Volume::setVoxelValue(bool value, const int x, const int y, const int z) { - volume[getLinearIndex(x, y, z)] = value; + volume[getLinearIndex(x, y, z)] = value; } #pragma once inline void Volume::dispose() { - free(volume); + free(volume); } typedef unsigned char uchar; typedef unsigned int uint; #pragma once -__device__ int finished; //global variable that contains a boolean which indicates when to stop the kernel processing +__device__ int finished; // global variable that contains a boolean which indicates when to stop the kernel processing #pragma once -__constant__ __device__ int WIDTH, HEIGHT, DEPTH; //constant variables that contain the size of the volume - +__constant__ __device__ int WIDTH, HEIGHT, DEPTH; // constant variables that contain the size of the volume #pragma once -__global__ void dilate(const bool* IMG1, const bool* IMG2, const bool* img1Read, const bool* img2Read, - bool* img1Write, bool* img2Write) { - - const int id = blockDim.x * blockIdx.x + threadIdx.x; +__global__ void dilate( + const bool* IMG1, + const bool* IMG2, + const bool* img1Read, + const bool* img2Read, + bool* img1Write, + bool* img2Write) { + const int id = blockDim.x * blockIdx.x + threadIdx.x; #if !IS_3D - const int x = id % WIDTH, y = id / WIDTH; + const int x = id % WIDTH, y = id / WIDTH; #else - const int x = id % WIDTH, y = (id / WIDTH) % HEIGHT, z = (id / WIDTH) / HEIGHT; + const int x = id % WIDTH, y = (id / WIDTH) % HEIGHT, z = (id / WIDTH) / HEIGHT; #endif - if (id < WIDTH * HEIGHT * DEPTH) { - - - if (img1Read[id]) { - if (x + 1 < WIDTH) img1Write[id + 1] = true; - if (x - 1 >= 0) img1Write[id - 1] = true; - if (y + 1 < HEIGHT) img1Write[id + WIDTH] = true; - if (y - 1 >= 0) img1Write[id - WIDTH] = true; -#if IS_3D //if working with 3d volumes, then the 3D part - if (z + 1 < DEPTH) img1Write[id + WIDTH * HEIGHT] = true; - if (z - 1 >= 0) img1Write[id - WIDTH * HEIGHT] = true; + if (id < WIDTH * HEIGHT * DEPTH) { + if (img1Read[id]) { + if (x + 1 < WIDTH) + img1Write[id + 1] = true; + if (x - 1 >= 0) + img1Write[id - 1] = true; + if (y + 1 < HEIGHT) + img1Write[id + WIDTH] = true; + if (y - 1 >= 0) + img1Write[id - WIDTH] = true; +#if IS_3D // if working with 3d volumes, then the 3D part + if (z + 1 < DEPTH) + img1Write[id + WIDTH * HEIGHT] = true; + if (z - 1 >= 0) + img1Write[id - WIDTH * HEIGHT] = true; #endif #if CHEBYSHEV - //diagonals - if (x + 1 < WIDTH && y - 1 >= 0) img1Write[id - WIDTH + 1] = true; - if (x - 1 >= 0 && y - 1 >= 0) img1Write[id - WIDTH - 1] = true; - if (x + 1 < WIDTH && y + 1 < HEIGHT) img1Write[id + WIDTH + 1] = true; - if (x - 1 >= 0 && y + 1 < HEIGHT) img1Write[id + WIDTH - 1] = true; -#if IS_3D //if working with 3d volumes, then the 3D part - if (z + 1 < DEPTH && x + 1 < WIDTH && y - 1 >= 0) img1Write[id - WIDTH + 1 + WIDTH * HEIGHT] = true; - if (z + 1 < DEPTH && x - 1 >= 0 && y - 1 >= 0) img1Write[id - WIDTH - 1 + WIDTH * HEIGHT] = true; - if (z + 1 < DEPTH && x + 1 < WIDTH && y + 1 < HEIGHT) img1Write[id + WIDTH + 1 + WIDTH * HEIGHT] = true; - if (z + 1 < DEPTH && x - 1 >= 0 && y + 1 < HEIGHT) img1Write[id + WIDTH - 1 + WIDTH * HEIGHT] = true; - if (z - 1 >= 0 && x + 1 < WIDTH && y - 1 >= 0) img1Write[id - WIDTH + 1 - WIDTH * HEIGHT] = true; - if (z - 1 >= 0 && x - 1 >= 0 && y - 1 >= 0) img1Write[id - WIDTH - 1 - WIDTH * HEIGHT] = true; - if (z - 1 >= 0 && x + 1 < WIDTH && y + 1 < HEIGHT) img1Write[id + WIDTH + 1 - WIDTH * HEIGHT] = true; - if (z - 1 >= 0 && x - 1 >= 0 && y + 1 < HEIGHT) img1Write[id + WIDTH - 1 - WIDTH * HEIGHT] = true; + // diagonals + if (x + 1 < WIDTH && y - 1 >= 0) + img1Write[id - WIDTH + 1] = true; + if (x - 1 >= 0 && y - 1 >= 0) + img1Write[id - WIDTH - 1] = true; + if (x + 1 < WIDTH && y + 1 < HEIGHT) + img1Write[id + WIDTH + 1] = true; + if (x - 1 >= 0 && y + 1 < HEIGHT) + img1Write[id + WIDTH - 1] = true; +#if IS_3D // if working with 3d volumes, then the 3D part + if (z + 1 < DEPTH && x + 1 < WIDTH && y - 1 >= 0) + img1Write[id - WIDTH + 1 + WIDTH * HEIGHT] = true; + if (z + 1 < DEPTH && x - 1 >= 0 && y - 1 >= 0) + img1Write[id - WIDTH - 1 + WIDTH * HEIGHT] = true; + if (z + 1 < DEPTH && x + 1 < WIDTH && y + 1 < HEIGHT) + img1Write[id + WIDTH + 1 + WIDTH * HEIGHT] = true; + if (z + 1 < DEPTH && x - 1 >= 0 && y + 1 < HEIGHT) + img1Write[id + WIDTH - 1 + WIDTH * HEIGHT] = true; + if (z - 1 >= 0 && x + 1 < WIDTH && y - 1 >= 0) + img1Write[id - WIDTH + 1 - WIDTH * HEIGHT] = true; + if (z - 1 >= 0 && x - 1 >= 0 && y - 1 >= 0) + img1Write[id - WIDTH - 1 - WIDTH * HEIGHT] = true; + if (z - 1 >= 0 && x + 1 < WIDTH && y + 1 < HEIGHT) + img1Write[id + WIDTH + 1 - WIDTH * HEIGHT] = true; + if (z - 1 >= 0 && x - 1 >= 0 && y + 1 < HEIGHT) + img1Write[id + WIDTH - 1 - WIDTH * HEIGHT] = true; #endif #endif - } - + } - if (img2Read[id]) { - if (x + 1 < WIDTH) img2Write[id + 1] = true; - if (x - 1 >= 0) img2Write[id - 1] = true; - if (y + 1 < HEIGHT) img2Write[id + WIDTH] = true; - if (y - 1 >= 0) img2Write[id - WIDTH] = true; -#if IS_3D //if working with 3d volumes, then the 3D part - if (z + 1 < DEPTH) img2Write[id + WIDTH * HEIGHT] = true; - if (z - 1 >= 0) img2Write[id - WIDTH * HEIGHT] = true; + if (img2Read[id]) { + if (x + 1 < WIDTH) + img2Write[id + 1] = true; + if (x - 1 >= 0) + img2Write[id - 1] = true; + if (y + 1 < HEIGHT) + img2Write[id + WIDTH] = true; + if (y - 1 >= 0) + img2Write[id - WIDTH] = true; +#if IS_3D // if working with 3d volumes, then the 3D part + if (z + 1 < DEPTH) + img2Write[id + WIDTH * HEIGHT] = true; + if (z - 1 >= 0) + img2Write[id - WIDTH * HEIGHT] = true; #endif #if CHEBYSHEV - //diagonals - if (x + 1 < WIDTH && y - 1 >= 0) img2Write[id - WIDTH + 1] = true; - if (x - 1 >= 0 && y - 1 >= 0) img2Write[id - WIDTH - 1] = true; - if (x + 1 < WIDTH && y + 1 < HEIGHT) img2Write[id + WIDTH + 1] = true; - if (x - 1 >= 0 && y + 1 < HEIGHT) img2Write[id + WIDTH - 1] = true; -#if IS_3D //if working with 3d volumes, then the 3D part - if (z + 1 < DEPTH && x + 1 < WIDTH && y - 1 >= 0) img2Write[id - WIDTH + 1 + WIDTH * HEIGHT] = true; - if (z + 1 < DEPTH && x - 1 >= 0 && y - 1 >= 0) img2Write[id - WIDTH - 1 + WIDTH * HEIGHT] = true; - if (z + 1 < DEPTH && x + 1 < WIDTH && y + 1 < HEIGHT) img2Write[id + WIDTH + 1 + WIDTH * HEIGHT] = true; - if (z + 1 < DEPTH && x - 1 >= 0 && y + 1 < HEIGHT) img2Write[id + WIDTH - 1 + WIDTH * HEIGHT] = true; - if (z - 1 >= 0 && x + 1 < WIDTH && y - 1 >= 0) img2Write[id - WIDTH + 1 - WIDTH * HEIGHT] = true; - if (z - 1 >= 0 && x - 1 >= 0 && y - 1 >= 0) img2Write[id - WIDTH - 1 - WIDTH * HEIGHT] = true; - if (z - 1 >= 0 && x + 1 < WIDTH && y + 1 < HEIGHT) img2Write[id + WIDTH + 1 - WIDTH * HEIGHT] = true; - if (z - 1 >= 0 && x - 1 >= 0 && y + 1 < HEIGHT) img2Write[id + WIDTH - 1 - WIDTH * HEIGHT] = true; + // diagonals + if (x + 1 < WIDTH && y - 1 >= 0) + img2Write[id - WIDTH + 1] = true; + if (x - 1 >= 0 && y - 1 >= 0) + img2Write[id - WIDTH - 1] = true; + if (x + 1 < WIDTH && y + 1 < HEIGHT) + img2Write[id + WIDTH + 1] = true; + if (x - 1 >= 0 && y + 1 < HEIGHT) + img2Write[id + WIDTH - 1] = true; +#if IS_3D // if working with 3d volumes, then the 3D part + if (z + 1 < DEPTH && x + 1 < WIDTH && y - 1 >= 0) + img2Write[id - WIDTH + 1 + WIDTH * HEIGHT] = true; + if (z + 1 < DEPTH && x - 1 >= 0 && y - 1 >= 0) + img2Write[id - WIDTH - 1 + WIDTH * HEIGHT] = true; + if (z + 1 < DEPTH && x + 1 < WIDTH && y + 1 < HEIGHT) + img2Write[id + WIDTH + 1 + WIDTH * HEIGHT] = true; + if (z + 1 < DEPTH && x - 1 >= 0 && y + 1 < HEIGHT) + img2Write[id + WIDTH - 1 + WIDTH * HEIGHT] = true; + if (z - 1 >= 0 && x + 1 < WIDTH && y - 1 >= 0) + img2Write[id - WIDTH + 1 - WIDTH * HEIGHT] = true; + if (z - 1 >= 0 && x - 1 >= 0 && y - 1 >= 0) + img2Write[id - WIDTH - 1 - WIDTH * HEIGHT] = true; + if (z - 1 >= 0 && x + 1 < WIDTH && y + 1 < HEIGHT) + img2Write[id + WIDTH + 1 - WIDTH * HEIGHT] = true; + if (z - 1 >= 0 && x - 1 >= 0 && y + 1 < HEIGHT) + img2Write[id + WIDTH - 1 - WIDTH * HEIGHT] = true; #endif #endif - } - - - //this is an atomic and computed to the finished global variable, if image 1 contains all of image 2 and image 2 contains all pixels of - //image 1 then finished is true - atomicAnd(&finished, (img2Read[id] || !IMG1[id]) && (img1Read[id] || !IMG2[id])); } + + // this is an atomic and computed to the finished global variable, if image 1 contains all of image 2 and image 2 + // contains all pixels of image 1 then finished is true + atomicAnd(&finished, (img2Read[id] || !IMG1[id]) && (img1Read[id] || !IMG2[id])); + } } #pragma once int HausdorffDistance::computeDistance(Volume* img1, Volume* img2, bool* d_img1, bool* d_img2) { + const int height = (*img1).getHeight(), width = (*img1).getWidth(), depth = (*img1).getDepth(); - const int height = (*img1).getHeight(), width = (*img1).getWidth(), depth = (*img1).getDepth(); - - size_t size = width * height * depth * sizeof(bool); - - //getting details of your CUDA device - cudaDeviceProp props; - cudaGetDeviceProperties(&props, CUDA_DEVICE_INDEX); //device index = 0, you can change it if you have more CUDA devices - const int threadsPerBlock = props.maxThreadsPerBlock / 2; - const int blocksPerGrid = (height * width * depth + threadsPerBlock - 1) / threadsPerBlock; + size_t size = width * height * depth * sizeof(bool); + // getting details of your CUDA device + cudaDeviceProp props; + cudaGetDeviceProperties( + &props, CUDA_DEVICE_INDEX); // device index = 0, you can change it if you have more CUDA devices + const int threadsPerBlock = props.maxThreadsPerBlock / 2; + const int blocksPerGrid = (height * width * depth + threadsPerBlock - 1) / threadsPerBlock; - //copying the dimensions to the GPU - cudaMemcpyToSymbolAsync(WIDTH, &width, sizeof(width), 0); - cudaMemcpyToSymbolAsync(HEIGHT, &height, sizeof(height), 0); - cudaMemcpyToSymbolAsync(DEPTH, &depth, sizeof(depth), 0); + // copying the dimensions to the GPU + cudaMemcpyToSymbolAsync(WIDTH, &width, sizeof(width), 0); + cudaMemcpyToSymbolAsync(HEIGHT, &height, sizeof(height), 0); + cudaMemcpyToSymbolAsync(DEPTH, &depth, sizeof(depth), 0); + // allocating the input images on the GPU - //allocating the input images on the GPU + // allocating the images that will be the processing ones + bool *d_img1Write, *d_img1Read, *d_img2Write, *d_img2Read; + cudaMalloc(&d_img1Write, size); + cudaMalloc(&d_img1Read, size); + cudaMalloc(&d_img2Write, size); + cudaMalloc(&d_img2Read, size); + // cloning the input images to these two image versions (write and read) + cudaMemcpyAsync(d_img1Read, d_img1, size, cudaMemcpyDeviceToDevice); + cudaMemcpyAsync(d_img2Read, d_img2, size, cudaMemcpyDeviceToDevice); + cudaMemcpyAsync(d_img1Write, d_img1, size, cudaMemcpyDeviceToDevice); + cudaMemcpyAsync(d_img2Write, d_img2, size, cudaMemcpyDeviceToDevice); + // required variables to compute the distance + int h_finished = false, t = true; + int distance = -1; - //allocating the images that will be the processing ones - bool* d_img1Write, * d_img1Read, * d_img2Write, * d_img2Read; - cudaMalloc(&d_img1Write, size); cudaMalloc(&d_img1Read, size); - cudaMalloc(&d_img2Write, size); cudaMalloc(&d_img2Read, size); + // where the magic happens + while (!h_finished) { + // reset the bool variable that verifies if the processing ended + cudaMemcpyToSymbol(finished, &t, sizeof(h_finished)); + // lauching the verify kernel, which verifies if the processing finished + dilate<<>>(d_img1, d_img2, d_img1Read, d_img2Read, d_img1Write, d_img2Write); - //cloning the input images to these two image versions (write and read) - cudaMemcpyAsync(d_img1Read, d_img1, size, cudaMemcpyDeviceToDevice); - cudaMemcpyAsync(d_img2Read, d_img2, size, cudaMemcpyDeviceToDevice); - cudaMemcpyAsync(d_img1Write, d_img1, size, cudaMemcpyDeviceToDevice); - cudaMemcpyAsync(d_img2Write, d_img2, size, cudaMemcpyDeviceToDevice); + // cudaDeviceSynchronize(); + // updating the imgRead (cloning imgWrite to imgRead) + cudaMemcpy(d_img1Read, d_img1Write, size, cudaMemcpyDeviceToDevice); + cudaMemcpy(d_img2Read, d_img2Write, size, cudaMemcpyDeviceToDevice); + // copying the result back to host memory + cudaMemcpyFromSymbol(&h_finished, finished, sizeof(h_finished)); - //required variables to compute the distance - int h_finished = false, t = true; - int distance = -1; + // incrementing the distance at each iteration + distance++; + } - //where the magic happens - while (!h_finished) { - //reset the bool variable that verifies if the processing ended - cudaMemcpyToSymbol(finished, &t, sizeof(h_finished)); + // freeing memory + cudaFree(d_img1); + cudaFree(d_img2); + cudaFree(d_img1Write); + cudaFree(d_img1Read); + cudaFree(d_img2Write); + cudaFree(d_img2Read); + // resetting device + // cudaDeviceReset(); - //lauching the verify kernel, which verifies if the processing finished - dilate << < blocksPerGrid, threadsPerBlock >> > (d_img1, d_img2, d_img1Read, d_img2Read, d_img1Write, d_img2Write); - - //cudaDeviceSynchronize(); - - //updating the imgRead (cloning imgWrite to imgRead) - cudaMemcpy(d_img1Read, d_img1Write, size, cudaMemcpyDeviceToDevice); - cudaMemcpy(d_img2Read, d_img2Write, size, cudaMemcpyDeviceToDevice); - - //copying the result back to host memory - cudaMemcpyFromSymbol(&h_finished, finished, sizeof(h_finished)); - - - //incrementing the distance at each iteration - distance++; - } - - - //freeing memory - cudaFree(d_img1); cudaFree(d_img2); - cudaFree(d_img1Write); cudaFree(d_img1Read); - cudaFree(d_img2Write); cudaFree(d_img2Read); - - //resetting device - // cudaDeviceReset(); - - //print(cudaGetLastError(), "processing CUDA. Something may be wrong with your CUDA device."); - - return distance; + // print(cudaGetLastError(), "processing CUDA. Something may be wrong with your CUDA device."); + return distance; } #pragma once inline void HausdorffDistance::print(cudaError_t error, char* msg) { - if (error != cudaSuccess) - { - printf("Error on %s ", msg); - fprintf(stderr, "Error code: %s!\n", cudaGetErrorString(error)); - exit(EXIT_FAILURE); - } + if (error != cudaSuccess) { + printf("Error on %s ", msg); + fprintf(stderr, "Error code: %s!\n", cudaGetErrorString(error)); + exit(EXIT_FAILURE); + } } - - /* benchmark for original code from https://github.com/Oyatsumi/HausdorffDistanceComparison */ -std::tuple benchmarkOlivieraCUDA(torch::Tensor goldStandardA, - torch::Tensor algoOutputA, int WIDTH, int HEIGHT - , int DEPTH) { - - //just originally it started for cpu so ... - - int lenn = WIDTH * HEIGHT * DEPTH; - size_t sizee = sizeof(bool) * lenn; +std::tuple benchmarkOlivieraCUDA( + torch::Tensor goldStandardA, + torch::Tensor algoOutputA, + int WIDTH, + int HEIGHT, + int DEPTH) { + // just originally it started for cpu so ... + int lenn = WIDTH * HEIGHT * DEPTH; + size_t sizee = sizeof(bool) * lenn; + bool* goldStandard = (bool*)calloc(lenn, sizeof(bool)); + bool* algoOutput = (bool*)calloc(lenn, sizeof(bool)); - bool* goldStandard = (bool*)calloc(lenn, sizeof(bool)); - bool* algoOutput = (bool*)calloc(lenn, sizeof(bool)); + cudaMemcpy(goldStandard, goldStandardA.data_ptr(), sizee, cudaMemcpyDeviceToHost); + cudaMemcpy(algoOutput, algoOutputA.data_ptr(), sizee, cudaMemcpyDeviceToHost); - cudaMemcpy(goldStandard, goldStandardA.data_ptr(), sizee, cudaMemcpyDeviceToHost); - cudaMemcpy(algoOutput, algoOutputA.data_ptr(), sizee, cudaMemcpyDeviceToHost); + // auto goldStandardA.data_ptr() + // bool* goldStandard = (bool*)goldStandardA.to(torch::kCPU).data_ptr(); + // bool* algoOutput = (bool*)algoOutputA.to(torch::kCPU).data_ptr(); - //auto goldStandardA.data_ptr() + Volume img1 = Volume(WIDTH, HEIGHT, DEPTH), img2 = Volume(WIDTH, HEIGHT, DEPTH); - - //bool* goldStandard = (bool*)goldStandardA.to(torch::kCPU).data_ptr(); - //bool* algoOutput = (bool*)algoOutputA.to(torch::kCPU).data_ptr(); - - - - Volume img1 = Volume(WIDTH, HEIGHT, DEPTH), img2 = Volume(WIDTH, HEIGHT, DEPTH); - - for (int x = 0; x < WIDTH; x++) { - for (int y = 0; y < HEIGHT; y++) { - for (int z = 0; z < DEPTH; z++) { - img1.setVoxelValue(algoOutput[x + y * WIDTH + z * WIDTH * HEIGHT], x, y, z); - img2.setVoxelValue(goldStandard[x + y * WIDTH + z * WIDTH * HEIGHT], x, y, z); - } - } + for (int x = 0; x < WIDTH; x++) { + for (int y = 0; y < HEIGHT; y++) { + for (int z = 0; z < DEPTH; z++) { + img1.setVoxelValue(algoOutput[x + y * WIDTH + z * WIDTH * HEIGHT], x, y, z); + img2.setVoxelValue(goldStandard[x + y * WIDTH + z * WIDTH * HEIGHT], x, y, z); + } } + } - size_t size = WIDTH * HEIGHT * DEPTH * sizeof(bool); - - - bool* d_img1, * d_img2; - cudaMalloc(&d_img1, size); - cudaMalloc(&d_img2, size); - - - //copying the data to the allocated memory on the GPU - cudaMemcpyAsync(d_img1, (img1).getVolume(), size, cudaMemcpyHostToDevice); - cudaMemcpyAsync(d_img2, (img2).getVolume(), size, cudaMemcpyHostToDevice); - - - auto begin = std::chrono::high_resolution_clock::now(); - HausdorffDistance* hd = new HausdorffDistance(); - - cudaDeviceSynchronize(); - - int dist = (*hd).computeDistance(&img1, &img2, d_img1, d_img2); - cudaDeviceSynchronize(); + size_t size = WIDTH * HEIGHT * DEPTH * sizeof(bool); + bool *d_img1, *d_img2; + cudaMalloc(&d_img1, size); + cudaMalloc(&d_img2, size); - auto end = std::chrono::high_resolution_clock::now(); + // copying the data to the allocated memory on the GPU + cudaMemcpyAsync(d_img1, (img1).getVolume(), size, cudaMemcpyHostToDevice); + cudaMemcpyAsync(d_img2, (img2).getVolume(), size, cudaMemcpyHostToDevice); + auto begin = std::chrono::high_resolution_clock::now(); + HausdorffDistance* hd = new HausdorffDistance(); + cudaDeviceSynchronize(); - // std::cout << "Total elapsed time: "; - double time = (double)(std::chrono::duration_cast(end - begin).count() / (double)1000000000); - //std::cout << time << "s" << std::endl; + int dist = (*hd).computeDistance(&img1, &img2, d_img1, d_img2); + cudaDeviceSynchronize(); - // printf("HD: %d \n", dist); + auto end = std::chrono::high_resolution_clock::now(); + // std::cout << "Total elapsed time: "; + double time = + (double)(std::chrono::duration_cast(end - begin).count() / (double)1000000000); + // std::cout << time << "s" << std::endl; + // printf("HD: %d \n", dist); - //freeing memory - img1.dispose(); img2.dispose(); - free(goldStandard); - free(algoOutput); - //Datasize: 216530944 - //Datasize : 216530944 - //Total elapsed time : 2.62191s - //HD : 234 - return { dist, time }; + // freeing memory + img1.dispose(); + img2.dispose(); + free(goldStandard); + free(algoOutput); + // Datasize: 216530944 + // Datasize : 216530944 + // Total elapsed time : 2.62191s + // HD : 234 + return {dist, time}; } >>>>>>> 7fb1821c8c2aa82d8aaa21bfbd1a69237b0ea960 diff --git a/monai/metrics/morphological_hausdorff_distance.py b/monai/metrics/morphological_hausdorff_distance.py index 9b7189118ee..a1e147e3857 100644 --- a/monai/metrics/morphological_hausdorff_distance.py +++ b/monai/metrics/morphological_hausdorff_distance.py @@ -24,38 +24,38 @@ class MorphologicalHausdorffDistanceMetric(CumulativeIterationMetric): """ - Work is based onthe principle of application of mathemathical morphology more precisely dilatation - , more details is presented in works [1] and [2]. Calculated is roughly related to the largest -thickness of the difference between the true and estimatedmasks, and constitutes Approximation of true Hausdorff distance. - The correlation between true and morphological Hausdorf distance is around 0.9, less for robust version . -Hovewer this implementation is also around 100 times faster than exact MONAI Hausdorff distance calculation. - One can measure 3 diffrent metrics based on Hausdorff ditance. + Work is based onthe principle of application of mathemathical morphology more precisely dilatation + , more details is presented in works [1] and [2]. Calculated is roughly related to the largest + thickness of the difference between the true and estimatedmasks, and constitutes Approximation of true Hausdorff distance. + The correlation between true and morphological Hausdorf distance is around 0.9, less for robust version . + Hovewer this implementation is also around 100 times faster than exact MONAI Hausdorff distance calculation. + One can measure 3 diffrent metrics based on Hausdorff ditance. - 1)simple Hausdorff distance - it tell what is the distance between two most distant points one from y and other from y_pred + 1)simple Hausdorff distance - it tell what is the distance between two most distant points one from y and other from y_pred - 2)robust Hausdorff distance - modification of 1) where we stop analyzing points - when we already analyzed given percent of total number of points - is less sensitive to outliers. It is also slightly faster + 2)robust Hausdorff distance - modification of 1) where we stop analyzing points + when we already analyzed given percent of total number of points - is less sensitive to outliers. It is also slightly faster - 3)mean Hausdorff distance - can be understood as approximately - mean distance between pair of points where one point is from y and other from y_pred. + 3)mean Hausdorff distance - can be understood as approximately + mean distance between pair of points where one point is from y and other from y_pred. - 4)additionally one can request as a result one dimensional tensor - that will contain data for each point what is HD value for it - (like we would do Hausdorff distance calculation only for this point and all points from other mask) + 4)additionally one can request as a result one dimensional tensor + that will contain data for each point what is HD value for it + (like we would do Hausdorff distance calculation only for this point and all points from other mask) - Compute Hausdorff Distance between two tensors. In addition, specify the `percent` (for robust Hausdorff distance ) - parameter can get the percentile of the distance. Input `y_pred` is compared with ground truth `y`. + Compute Hausdorff Distance between two tensors. In addition, specify the `percent` (for robust Hausdorff distance ) + parameter can get the percentile of the distance. Input `y_pred` is compared with ground truth `y`. - Args: - percent: an optional float number between 0 and 1. If specified, the corresponding - percent of the Hausdorff Distance rather than the maximum result will be achieved. - Defaults to 1.0 . + Args: + percent: an optional float number between 0 and 1. If specified, the corresponding + percent of the Hausdorff Distance rather than the maximum result will be achieved. + Defaults to 1.0 . - 1) Érick Oliveira Rodrigues,An efficient and locality-oriented Hausdorff distance algorithm: - Proposal and analysis of paradigms and implementations, Pattern Recognition,Volume 117,2021 - 2) Karimi, Davood & Salcudean, Septimiu. (2019). - Reducing the Hausdorff Distance in Medical Image Segmentation With Convolutional Neural Networks. - IEEE Transactions on Medical Imaging. PP. 1-1. 10.1109/TMI.2019.2930068. + 1) Érick Oliveira Rodrigues,An efficient and locality-oriented Hausdorff distance algorithm: + Proposal and analysis of paradigms and implementations, Pattern Recognition,Volume 117,2021 + 2) Karimi, Davood & Salcudean, Septimiu. (2019). + Reducing the Hausdorff Distance in Medical Image Segmentation With Convolutional Neural Networks. + IEEE Transactions on Medical Imaging. PP. 1-1. 10.1109/TMI.2019.2930068. """ @@ -65,8 +65,7 @@ def __init__(self, percent: float = 1.0) -> None: self.compiled_extension = load_module("hausdorff_cpp") def compute_morphological_hausdorff_distance( - self, y_pred: torch.Tensor, y: torch.Tensor - ,compare_values: torch.Tensor, to_invert_dims=False + self, y_pred: torch.Tensor, y: torch.Tensor, compare_values: torch.Tensor, to_invert_dims=False ): """ Compute the Hausdorff distance. diff --git a/tests/test_hausdorff_distance_morphologic.py b/tests/test_hausdorff_distance_morphologic.py index e791068ccc4..a7871aa332e 100644 --- a/tests/test_hausdorff_distance_morphologic.py +++ b/tests/test_hausdorff_distance_morphologic.py @@ -21,29 +21,29 @@ device = torch.device("cuda") -### testing single points diffrent dims -#dim1 +# testing single points diffrent dims +# dim1 compare_values = torch.ones(1) a = torch.zeros(100, 100, 100) b = torch.zeros(100, 100, 100) a[0, 0, 0] = 1 b[10, 0, 0] = 1 -#dim2 +# dim2 a1 = torch.zeros(200, 200, 200) b1 = torch.zeros(200, 200, 200) a1[0, 0, 0] = 1 b1[0, 15, 0] = 1 -#dim3 +# dim3 a2 = torch.zeros(400, 200, 300) b2 = torch.zeros(400, 200, 300) a2[0, 0, 10] = 1 b2[0, 0, 150] = 1 -### testing whole llines and compare_values set to 2 +# testing whole llines and compare_values set to 2 compare_valuesB = torch.ones(1) -compare_valuesB[0]=2 +compare_valuesB[0] = 2 a3 = torch.zeros(400, 200, 300) b3 = torch.zeros(400, 200, 300) a3[:, 0, 10] = 2 @@ -61,7 +61,7 @@ b5[120, :, 0] = 2 -## testing whole planes +# testing whole planes a6 = torch.zeros(400, 200, 300) b6 = torch.zeros(400, 200, 300) a6[10, :, :] = 2 @@ -71,46 +71,43 @@ a7 = torch.zeros(400, 200, 300) b7 = torch.zeros(400, 200, 300) a7[:, 0, :] = 2 -b7[:,110, :] = 2 +b7[:, 110, :] = 2 a8 = torch.zeros(400, 200, 300) b8 = torch.zeros(400, 200, 300) -#a8[:, :, 20] = 2 -#b8[:,:, 130] = 2 +# a8[:, :, 20] = 2 +# b8[:,:, 130] = 2 -a8[1, 1, 20]= 2 -b8[1,1, 130]= 2 -a8[2, 2, 20]= 2 -b8[2,2, 130]= 2 +a8[1, 1, 20] = 2 +b8[1, 1, 130] = 2 +a8[2, 2, 20] = 2 +b8[2, 2, 130] = 2 -#multi points +# multi points a9 = torch.zeros(400, 200, 300) b9 = torch.zeros(400, 200, 300) -a9[0, 20,0 ]= 2 -a9[0, 0,30 ]= 2 -a9[40, 0,0 ]= 2 -b9[0,0,0 ]= 2 +a9[0, 20, 0] = 2 +a9[0, 0, 30] = 2 +a9[40, 0, 0] = 2 +b9[0, 0, 0] = 2 TEST_CASES = [ - [[a, b, 1.0, compare_values], 10] - ,[[a1, b1, 1.0, compare_values], 15] - ,[[a2, b2, 1.0, compare_values], 140] - ,[[a3, b3, 1.0, compare_valuesB], 140] - ,[[a4, b4, 1.0, compare_valuesB], 110] - ,[[a5, b5, 1.0, compare_valuesB], 110] - ,[[a6, b6, 1.0, compare_valuesB], 110] - ,[[a7, b7, 1.0, compare_valuesB], 110] - ,[[a8, b8, 1.0, compare_valuesB], 110] - - #testing robust - ,[[a6, b6, 0.9, compare_valuesB], 110] - ,[[a7, b7, 0.85, compare_valuesB], 110] - ,[[a8, b8, 0.8, compare_valuesB], 110] - #multi points - ,[[a9, b9, 1.0, compare_valuesB], 40] - ] + [[a, b, 1.0, compare_values], 10], + [[a1, b1, 1.0, compare_values], 15], + [[a2, b2, 1.0, compare_values], 140], + [[a3, b3, 1.0, compare_valuesB], 140], + [[a4, b4, 1.0, compare_valuesB], 110], + [[a5, b5, 1.0, compare_valuesB], 110], + [[a6, b6, 1.0, compare_valuesB], 110], + [[a7, b7, 1.0, compare_valuesB], 110], + [[a8, b8, 1.0, compare_valuesB], 110], # testing robust + [[a6, b6, 0.9, compare_valuesB], 110], + [[a7, b7, 0.85, compare_valuesB], 110], + [[a8, b8, 0.8, compare_valuesB], 110], # multi points + [[a9, b9, 1.0, compare_valuesB], 40], +] @skip_if_no_cuda @@ -119,7 +116,7 @@ class TestHausdorffDistanceMorphological(unittest.TestCase): def test_value(self, input_data, expected_value): [y_pred, y, percentt, compare_values] = input_data hd_metric = MorphologicalHausdorffDistanceMetric(percentt) - result = hd_metric.compute_hausdorff_distance(y_pred.to(device), y.to(device), compare_values.to(device),True) + result = hd_metric.compute_hausdorff_distance(y_pred.to(device), y.to(device), compare_values.to(device), True) np.testing.assert_allclose(expected_value, result, rtol=1e-7) From 21e807655fa5b0884a4a1e3cb66c189cd96b1a5c Mon Sep 17 00:00:00 2001 From: Jakub Mitura <53857487+jakubMitura14@users.noreply.github.com> Date: Sat, 9 Apr 2022 16:42:11 +0200 Subject: [PATCH 18/41] formatted as Lint suggested Signed-off-by: Jakub Mitura <53857487+jakubMitura14@users.noreply.github.com> --- tests/test_hausdorff_distance_morphologic.py | 27 ++++++++++---------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/tests/test_hausdorff_distance_morphologic.py b/tests/test_hausdorff_distance_morphologic.py index 046097d98a4..d92b4b5869a 100644 --- a/tests/test_hausdorff_distance_morphologic.py +++ b/tests/test_hausdorff_distance_morphologic.py @@ -42,8 +42,8 @@ b2[0, 0, 150] = 1 ### testing whole llines and compare_values set to 2 -compare_valuesB = torch.ones(1) -compare_valuesB[0]=2 +compare_values_b = torch.ones(1) +compare_values_b[0]=2 a3 = torch.zeros(400, 200, 300) b3 = torch.zeros(400, 200, 300) a3[:, 0, 10] = 2 @@ -97,19 +97,19 @@ [[a, b, 1.0, compare_values], 10] ,[[a1, b1, 1.0, compare_values], 15] ,[[a2, b2, 1.0, compare_values], 140] - ,[[a3, b3, 1.0, compare_valuesB], 140] - ,[[a4, b4, 1.0, compare_valuesB], 110] - ,[[a5, b5, 1.0, compare_valuesB], 110] - ,[[a6, b6, 1.0, compare_valuesB], 110] - ,[[a7, b7, 1.0, compare_valuesB], 110] - ,[[a8, b8, 1.0, compare_valuesB], 110] + ,[[a3, b3, 1.0, compare_values_b], 140] + ,[[a4, b4, 1.0, compare_values_b], 110] + ,[[a5, b5, 1.0, compare_values_b], 110] + ,[[a6, b6, 1.0, compare_values_b], 110] + ,[[a7, b7, 1.0, compare_values_b], 110] + ,[[a8, b8, 1.0, compare_values_b], 110] #testing robust - ,[[a6, b6, 0.9, compare_valuesB], 110] - ,[[a7, b7, 0.85, compare_valuesB], 110] - ,[[a8, b8, 0.8, compare_valuesB], 110] + ,[[a6, b6, 0.9, compare_values_b], 110] + ,[[a7, b7, 0.85, compare_values_b], 110] + ,[[a8, b8, 0.8, compare_values_b], 110] #multi points - ,[[a9, b9, 1.0, compare_valuesB], 40] + ,[[a9, b9, 1.0, compare_values_b], 40] ] @@ -119,7 +119,8 @@ class TestHausdorffDistanceMorphological(unittest.TestCase): def test_value(self, input_data, expected_value): [y_pred, y, percentt, compare_values] = input_data hd_metric = MorphologicalHausdorffDistanceMetric(percentt) - result = hd_metric.compute_hausdorff_distance(y_pred.to(device), y.to(device), compare_values.to(device),True) + result = hd_metric.compute_hausdorff_distance(y_pred.to(device) + , y.to(device), compare_values.to(device),True) np.testing.assert_allclose(expected_value, result, rtol=1e-7) From 7acb3d52fa9f9f975b939cfa19df01332e2b3366 Mon Sep 17 00:00:00 2001 From: Jakub Mitura <53857487+jakubMitura14@users.noreply.github.com> Date: Sat, 9 Apr 2022 16:43:36 +0200 Subject: [PATCH 19/41] no camel case Signed-off-by: Jakub Mitura <53857487+jakubMitura14@users.noreply.github.com> --- tests/test_hausdorff_distance_morphologic.py | 27 ++++++++++---------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/tests/test_hausdorff_distance_morphologic.py b/tests/test_hausdorff_distance_morphologic.py index a7871aa332e..0f5f3802dd0 100644 --- a/tests/test_hausdorff_distance_morphologic.py +++ b/tests/test_hausdorff_distance_morphologic.py @@ -42,8 +42,8 @@ b2[0, 0, 150] = 1 # testing whole llines and compare_values set to 2 -compare_valuesB = torch.ones(1) -compare_valuesB[0] = 2 +compare_values_b = torch.ones(1) +compare_values_b[0] = 2 a3 = torch.zeros(400, 200, 300) b3 = torch.zeros(400, 200, 300) a3[:, 0, 10] = 2 @@ -97,16 +97,16 @@ [[a, b, 1.0, compare_values], 10], [[a1, b1, 1.0, compare_values], 15], [[a2, b2, 1.0, compare_values], 140], - [[a3, b3, 1.0, compare_valuesB], 140], - [[a4, b4, 1.0, compare_valuesB], 110], - [[a5, b5, 1.0, compare_valuesB], 110], - [[a6, b6, 1.0, compare_valuesB], 110], - [[a7, b7, 1.0, compare_valuesB], 110], - [[a8, b8, 1.0, compare_valuesB], 110], # testing robust - [[a6, b6, 0.9, compare_valuesB], 110], - [[a7, b7, 0.85, compare_valuesB], 110], - [[a8, b8, 0.8, compare_valuesB], 110], # multi points - [[a9, b9, 1.0, compare_valuesB], 40], + [[a3, b3, 1.0, compare_values_b], 140], + [[a4, b4, 1.0, compare_values_b], 110], + [[a5, b5, 1.0, compare_values_b], 110], + [[a6, b6, 1.0, compare_values_b], 110], + [[a7, b7, 1.0, compare_values_b], 110], + [[a8, b8, 1.0, compare_values_b], 110], # testing robust + [[a6, b6, 0.9, compare_values_b], 110], + [[a7, b7, 0.85, compare_values_b], 110], + [[a8, b8, 0.8, compare_values_b], 110], # multi points + [[a9, b9, 1.0, compare_values_b], 40], ] @@ -116,7 +116,8 @@ class TestHausdorffDistanceMorphological(unittest.TestCase): def test_value(self, input_data, expected_value): [y_pred, y, percentt, compare_values] = input_data hd_metric = MorphologicalHausdorffDistanceMetric(percentt) - result = hd_metric.compute_hausdorff_distance(y_pred.to(device), y.to(device), compare_values.to(device), True) + result = hd_metric.compute_hausdorff_distance(y_pred.to(device) + , y.to(device), compare_values.to(device), True) np.testing.assert_allclose(expected_value, result, rtol=1e-7) From 8747ca8262469bc340d0201edaf3d85f49c89e0b Mon Sep 17 00:00:00 2001 From: monai-bot Date: Sun, 10 Apr 2022 08:58:05 +0000 Subject: [PATCH 20/41] [MONAI] python code formatting Signed-off-by: monai-bot --- monai/_extensions/hausdorff/hausdorff_cuda.cu | 6 +++--- tests/test_hausdorff_distance_morphologic.py | 3 +-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/monai/_extensions/hausdorff/hausdorff_cuda.cu b/monai/_extensions/hausdorff/hausdorff_cuda.cu index c166f8ec071..193761ca191 100644 --- a/monai/_extensions/hausdorff/hausdorff_cuda.cu +++ b/monai/_extensions/hausdorff/hausdorff_cuda.cu @@ -804,8 +804,8 @@ __global__ void firstMetaPrepareKernel( __shared__ unsigned int localWorkQueueCounter[1]; // according to https://forums.developer.nvidia.com/t/find-the-limit-of-shared-memory-that-can-be-used-per-block/48556 // it is good to keep shared memory below 16kb kilo bytes so it will give us 1600 length of shared memory so here we - // will store locally the calculated offsets and coordinates of meta data block of intrest marking also wheather we are - // talking about gold or segmentation pass (fp or fn ) + // will store locally the calculated offsets and coordinates of meta data block of intrest marking also wheather we + // are talking about gold or segmentation pass (fp or fn ) __shared__ uint32_t localWorkQueue[1600]; __shared__ uint32_t localOffsetQueue[1600]; if ((threadIdx.x == 0)) { @@ -1618,7 +1618,7 @@ UINT32_MAX 0 : top 1 : bottom 2 : left 3 : right 4 : anterior 5 : posterior // just re mainShmem[begSecRegShmem + threadIdx.x + threadIdx.y * 32] = 0; ////// IMPORTANT for some reason in order to make it work resultfnOffset and resultfnOffset swith - ///places + /// places if (isGoldForLocQueue[i]) { mainShmem[begSecRegShmem + threadIdx.x + threadIdx.y * 32] = uint32_t( atomicAdd_block(&(localFpConter[0]), 1) + localBlockMetaData[(i & 1) * 20 + 6] + diff --git a/tests/test_hausdorff_distance_morphologic.py b/tests/test_hausdorff_distance_morphologic.py index 0f5f3802dd0..d6f0562d6c5 100644 --- a/tests/test_hausdorff_distance_morphologic.py +++ b/tests/test_hausdorff_distance_morphologic.py @@ -116,8 +116,7 @@ class TestHausdorffDistanceMorphological(unittest.TestCase): def test_value(self, input_data, expected_value): [y_pred, y, percentt, compare_values] = input_data hd_metric = MorphologicalHausdorffDistanceMetric(percentt) - result = hd_metric.compute_hausdorff_distance(y_pred.to(device) - , y.to(device), compare_values.to(device), True) + result = hd_metric.compute_hausdorff_distance(y_pred.to(device), y.to(device), compare_values.to(device), True) np.testing.assert_allclose(expected_value, result, rtol=1e-7) From bf20c7f01511296804477c49228278f3cde00ddf Mon Sep 17 00:00:00 2001 From: Jakub Mitura <53857487+jakubMitura14@users.noreply.github.com> Date: Sun, 10 Apr 2022 14:44:07 +0200 Subject: [PATCH 21/41] implemented _compute_tensor Signed-off-by: Jakub Mitura <53857487+jakubMitura14@users.noreply.github.com> --- .../morphological_hausdorff_distance.py | 65 ++++++++++++------- tests/test_hausdorff_distance_morphologic.py | 8 ++- 2 files changed, 48 insertions(+), 25 deletions(-) diff --git a/monai/metrics/morphological_hausdorff_distance.py b/monai/metrics/morphological_hausdorff_distance.py index a1e147e3857..fefd505d4ca 100644 --- a/monai/metrics/morphological_hausdorff_distance.py +++ b/monai/metrics/morphological_hausdorff_distance.py @@ -10,12 +10,13 @@ # limitations under the License. import warnings -from typing import Optional, Union +from typing import Optional, Union, Any import numpy as np import torch from monai._extensions.loader import load_module +from monai.metrics.utils import do_metric_reduction, get_mask_edges, get_surface_distance, ignore_background from .metric import CumulativeIterationMetric @@ -26,15 +27,18 @@ class MorphologicalHausdorffDistanceMetric(CumulativeIterationMetric): """ Work is based onthe principle of application of mathemathical morphology more precisely dilatation , more details is presented in works [1] and [2]. Calculated is roughly related to the largest - thickness of the difference between the true and estimatedmasks, and constitutes Approximation of true Hausdorff distance. + thickness of the difference between the true and estimatedmasks, and constitutes Approximation + of true Hausdorff distance. The correlation between true and morphological Hausdorf distance is around 0.9, less for robust version . Hovewer this implementation is also around 100 times faster than exact MONAI Hausdorff distance calculation. One can measure 3 diffrent metrics based on Hausdorff ditance. - 1)simple Hausdorff distance - it tell what is the distance between two most distant points one from y and other from y_pred + 1)simple Hausdorff distance - it tell what is the distance between two most distant points one from + y and other from y_pred 2)robust Hausdorff distance - modification of 1) where we stop analyzing points - when we already analyzed given percent of total number of points - is less sensitive to outliers. It is also slightly faster + when we already analyzed given percent of total number of points - is less sensitive to outliers. + It is also slightly faster 3)mean Hausdorff distance - can be understood as approximately mean distance between pair of points where one point is from y and other from y_pred. @@ -43,13 +47,19 @@ class MorphologicalHausdorffDistanceMetric(CumulativeIterationMetric): that will contain data for each point what is HD value for it (like we would do Hausdorff distance calculation only for this point and all points from other mask) - Compute Hausdorff Distance between two tensors. In addition, specify the `percent` (for robust Hausdorff distance ) + Compute Hausdorff Distance between two tensors. In addition, specify the `percent` + (for robust Hausdorff distance ) parameter can get the percentile of the distance. Input `y_pred` is compared with ground truth `y`. Args: percent: an optional float number between 0 and 1. If specified, the corresponding percent of the Hausdorff Distance rather than the maximum result will be achieved. Defaults to 1.0 . + compare_values: 0 dimensional tensor marking what value are we intrested in the supplied y and y_pred, + defined becouse in case of multiorgan segmentations frequently we can have information + about multiple organs in the same mask just marked by diffrent numbers + for example in case of boolean array we can suupply it like torch.ones(1, dtype =bool) + to_invert_dims: inverts first with third dim - used fo testing only 1) Érick Oliveira Rodrigues,An efficient and locality-oriented Hausdorff distance algorithm: Proposal and analysis of paradigms and implementations, Pattern Recognition,Volume 117,2021 @@ -59,39 +69,50 @@ class MorphologicalHausdorffDistanceMetric(CumulativeIterationMetric): """ - def __init__(self, percent: float = 1.0) -> None: + def aggregate(self): + """ + Execute reduction logic for the output of `compute_hausdorff_distance`. + + """ + data = self.get_buffer() + if not isinstance(data, torch.Tensor): + raise ValueError("the data to aggregate must be PyTorch Tensor.") + + # do metric reduction + f, not_nans = do_metric_reduction(data, self.reduction) + return (f, not_nans) if self.get_not_nans else f + + def __init__(self + ,compare_values: torch.Tensor + ,percent: float = 1.0 + ,to_invert_dims=False) -> None: super().__init__() self.percent = percent + self.to_invert_dims = to_invert_dims + self.compare_values = compare_values self.compiled_extension = load_module("hausdorff_cpp") - def compute_morphological_hausdorff_distance( - self, y_pred: torch.Tensor, y: torch.Tensor, compare_values: torch.Tensor, to_invert_dims=False - ): + def _compute_tensor(self, y_pred: torch.Tensor, y: Optional[torch.Tensor] = None): """ Compute the Hausdorff distance. + Important!! + Size of y and y_pred Args: y_pred: input data to compute, It must be 3 dimensional - y: ground truth to compute mean the distance. It must be 3 dimensional, Dimensionality needs to be identical as in y_pred - - percent: an optional float number between 0 and 1. If specified, the corresponding - percent of the Hausdorff Distance rather than the maximum result will be achieved. - Defaults to 1.0 . - compare_values: 0 dimensional tensor marking what value are we intrested in the supplied y and y_pred, - defined becouse in case of multiorgan segmentations frequently we can have information - about multiple organs in the same mask just marked by diffrent numbers - for example in case of boolean array we can suupply it like torch.ones(1, dtype =bool) - to_invert_dims - inverts first with third dim - used fo testing only + y: ground truth to compute mean the distance. It must be 3 dimensional, + Dimensionality needs to be identical as in y_pred + """ if y.shape != y_pred.shape: raise ValueError(f"y_pred and y should have same shapes, got {y_pred.shape} and {y.shape}.") sizz = y.shape - if to_invert_dims: + if self.to_invert_dims: return self.compiled_extension.getHausdorffDistance( - y_pred, y, sizz[2], sizz[1], sizz[0], self.percent, compare_values + y_pred, y, sizz[2], sizz[1], sizz[0], self.percent, self.compare_values ) else: return self.compiled_extension.getHausdorffDistance( - y_pred, y, sizz[0], sizz[1], sizz[2], self.percent, compare_values + y_pred, y, sizz[0], sizz[1], sizz[2], self.percent, self.compare_values ) diff --git a/tests/test_hausdorff_distance_morphologic.py b/tests/test_hausdorff_distance_morphologic.py index 0f5f3802dd0..d06214c3781 100644 --- a/tests/test_hausdorff_distance_morphologic.py +++ b/tests/test_hausdorff_distance_morphologic.py @@ -115,9 +115,11 @@ class TestHausdorffDistanceMorphological(unittest.TestCase): @parameterized.expand(TEST_CASES) def test_value(self, input_data, expected_value): [y_pred, y, percentt, compare_values] = input_data - hd_metric = MorphologicalHausdorffDistanceMetric(percentt) - result = hd_metric.compute_hausdorff_distance(y_pred.to(device) - , y.to(device), compare_values.to(device), True) + hd_metric = MorphologicalHausdorffDistanceMetric( + compare_values.to(device) + ,percentt + ,True)# True only for tests + result = hd_metric._compute_tensor(y_pred.to(device),y.to(device)) np.testing.assert_allclose(expected_value, result, rtol=1e-7) From 12ab6a88b6b09cea8c7faa0789a46b1f5f053c08 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 10 Apr 2022 12:45:24 +0000 Subject: [PATCH 22/41] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- monai/metrics/morphological_hausdorff_distance.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monai/metrics/morphological_hausdorff_distance.py b/monai/metrics/morphological_hausdorff_distance.py index fefd505d4ca..a7843632cc2 100644 --- a/monai/metrics/morphological_hausdorff_distance.py +++ b/monai/metrics/morphological_hausdorff_distance.py @@ -96,7 +96,7 @@ def _compute_tensor(self, y_pred: torch.Tensor, y: Optional[torch.Tensor] = None """ Compute the Hausdorff distance. Important!! - Size of y and y_pred + Size of y and y_pred Args: y_pred: input data to compute, It must be 3 dimensional From 0fd3c6efd7b577ea75fc39606c3dbee5d0eec9d8 Mon Sep 17 00:00:00 2001 From: monai-bot Date: Sun, 10 Apr 2022 14:27:32 +0000 Subject: [PATCH 23/41] [MONAI] python code formatting Signed-off-by: monai-bot --- monai/metrics/morphological_hausdorff_distance.py | 7 ++----- tests/test_hausdorff_distance_morphologic.py | 7 +++---- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/monai/metrics/morphological_hausdorff_distance.py b/monai/metrics/morphological_hausdorff_distance.py index a7843632cc2..4cfa98950da 100644 --- a/monai/metrics/morphological_hausdorff_distance.py +++ b/monai/metrics/morphological_hausdorff_distance.py @@ -10,7 +10,7 @@ # limitations under the License. import warnings -from typing import Optional, Union, Any +from typing import Any, Optional, Union import numpy as np import torch @@ -82,10 +82,7 @@ def aggregate(self): f, not_nans = do_metric_reduction(data, self.reduction) return (f, not_nans) if self.get_not_nans else f - def __init__(self - ,compare_values: torch.Tensor - ,percent: float = 1.0 - ,to_invert_dims=False) -> None: + def __init__(self, compare_values: torch.Tensor, percent: float = 1.0, to_invert_dims=False) -> None: super().__init__() self.percent = percent self.to_invert_dims = to_invert_dims diff --git a/tests/test_hausdorff_distance_morphologic.py b/tests/test_hausdorff_distance_morphologic.py index d06214c3781..d8926f96f72 100644 --- a/tests/test_hausdorff_distance_morphologic.py +++ b/tests/test_hausdorff_distance_morphologic.py @@ -116,10 +116,9 @@ class TestHausdorffDistanceMorphological(unittest.TestCase): def test_value(self, input_data, expected_value): [y_pred, y, percentt, compare_values] = input_data hd_metric = MorphologicalHausdorffDistanceMetric( - compare_values.to(device) - ,percentt - ,True)# True only for tests - result = hd_metric._compute_tensor(y_pred.to(device),y.to(device)) + compare_values.to(device), percentt, True + ) # True only for tests + result = hd_metric._compute_tensor(y_pred.to(device), y.to(device)) np.testing.assert_allclose(expected_value, result, rtol=1e-7) From ac3532b76d6cfed9cffadde078a1537a3761a8d7 Mon Sep 17 00:00:00 2001 From: Jakub Mitura <53857487+jakubMitura14@users.noreply.github.com> Date: Sun, 10 Apr 2022 16:59:32 +0200 Subject: [PATCH 24/41] changed hausdorff_cpp into hausdorff Signed-off-by: Jakub Mitura <53857487+jakubMitura14@users.noreply.github.com> --- .../_extensions/hausdorff/{hausdorff_cpp.cpp => hausdorff.cpp} | 0 monai/metrics/morphological_hausdorff_distance.py | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename monai/_extensions/hausdorff/{hausdorff_cpp.cpp => hausdorff.cpp} (100%) diff --git a/monai/_extensions/hausdorff/hausdorff_cpp.cpp b/monai/_extensions/hausdorff/hausdorff.cpp similarity index 100% rename from monai/_extensions/hausdorff/hausdorff_cpp.cpp rename to monai/_extensions/hausdorff/hausdorff.cpp diff --git a/monai/metrics/morphological_hausdorff_distance.py b/monai/metrics/morphological_hausdorff_distance.py index fefd505d4ca..794605517e7 100644 --- a/monai/metrics/morphological_hausdorff_distance.py +++ b/monai/metrics/morphological_hausdorff_distance.py @@ -90,7 +90,7 @@ def __init__(self self.percent = percent self.to_invert_dims = to_invert_dims self.compare_values = compare_values - self.compiled_extension = load_module("hausdorff_cpp") + self.compiled_extension = load_module("hausdorff") def _compute_tensor(self, y_pred: torch.Tensor, y: Optional[torch.Tensor] = None): """ From 12dc5436ac9c23c972441ad4a60937665d8200d0 Mon Sep 17 00:00:00 2001 From: Jakub Mitura <53857487+jakubMitura14@users.noreply.github.com> Date: Sun, 10 Apr 2022 20:30:04 +0200 Subject: [PATCH 25/41] black linting Signed-off-by: Jakub Mitura <53857487+jakubMitura14@users.noreply.github.com> --- monai/metrics/morphological_hausdorff_distance.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monai/metrics/morphological_hausdorff_distance.py b/monai/metrics/morphological_hausdorff_distance.py index 328edf781f7..4c5e15ed444 100644 --- a/monai/metrics/morphological_hausdorff_distance.py +++ b/monai/metrics/morphological_hausdorff_distance.py @@ -16,7 +16,7 @@ import torch from monai._extensions.loader import load_module -from monai.metrics.utils import do_metric_reduction, get_mask_edges, get_surface_distance, ignore_background +from monai.metrics.utils import do_metric_reduction from .metric import CumulativeIterationMetric From d8236f6d98f170e7f1a44d74ecbc8a2464dce414 Mon Sep 17 00:00:00 2001 From: Jakub Mitura <53857487+jakubMitura14@users.noreply.github.com> Date: Tue, 12 Apr 2022 17:45:31 +0200 Subject: [PATCH 26/41] updated imports, reduced size of tests Signed-off-by: Jakub Mitura <53857487+jakubMitura14@users.noreply.github.com> --- .../morphological_hausdorff_distance.py | 4 +- tests/test_hausdorff_distance_morphologic.py | 45 ++++++++++--------- 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/monai/metrics/morphological_hausdorff_distance.py b/monai/metrics/morphological_hausdorff_distance.py index 4c5e15ed444..e9fab0662c1 100644 --- a/monai/metrics/morphological_hausdorff_distance.py +++ b/monai/metrics/morphological_hausdorff_distance.py @@ -9,10 +9,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -import warnings -from typing import Any, Optional, Union +from typing import Any, Optional -import numpy as np import torch from monai._extensions.loader import load_module diff --git a/tests/test_hausdorff_distance_morphologic.py b/tests/test_hausdorff_distance_morphologic.py index d8926f96f72..b570dfbfae6 100644 --- a/tests/test_hausdorff_distance_morphologic.py +++ b/tests/test_hausdorff_distance_morphologic.py @@ -20,61 +20,66 @@ device = torch.device("cuda") +dimA=11 + +dimAA=131 +dimBB=111 +dimCC=151 # testing single points diffrent dims # dim1 compare_values = torch.ones(1) -a = torch.zeros(100, 100, 100) -b = torch.zeros(100, 100, 100) +a = torch.zeros(dimA, dimA, dimA) +b = torch.zeros(dimA, dimA, dimA) a[0, 0, 0] = 1 b[10, 0, 0] = 1 # dim2 -a1 = torch.zeros(200, 200, 200) -b1 = torch.zeros(200, 200, 200) +a1 = torch.zeros(dimAA, dimBB, dimCC) +b1 = torch.zeros(dimAA, dimBB, dimCC) a1[0, 0, 0] = 1 b1[0, 15, 0] = 1 # dim3 -a2 = torch.zeros(400, 200, 300) -b2 = torch.zeros(400, 200, 300) +a2 = torch.zeros(dimAA, dimBB, dimCC) +b2 = torch.zeros(dimAA, dimBB, dimCC) a2[0, 0, 10] = 1 b2[0, 0, 150] = 1 # testing whole llines and compare_values set to 2 compare_values_b = torch.ones(1) compare_values_b[0] = 2 -a3 = torch.zeros(400, 200, 300) -b3 = torch.zeros(400, 200, 300) +a3 = torch.zeros(dimAA, dimBB, dimCC) +b3 = torch.zeros(dimAA, dimBB, dimCC) a3[:, 0, 10] = 2 b3[:, 0, 150] = 2 -a4 = torch.zeros(400, 200, 300) -b4 = torch.zeros(400, 200, 300) +a4 = torch.zeros(dimAA, dimBB, dimCC) +b4 = torch.zeros(dimAA, dimBB, dimCC) a4[10, 0, :] = 2 b4[120, 0, :] = 2 -a5 = torch.zeros(400, 200, 300) -b5 = torch.zeros(400, 200, 300) +a5 = torch.zeros(dimAA, dimBB, dimCC) +b5 = torch.zeros(dimAA, dimBB, dimCC) a5[10, :, 0] = 2 b5[120, :, 0] = 2 # testing whole planes -a6 = torch.zeros(400, 200, 300) -b6 = torch.zeros(400, 200, 300) +a6 = torch.zeros(dimAA, dimBB, dimCC) +b6 = torch.zeros(dimAA, dimBB, dimCC) a6[10, :, :] = 2 b6[120, :, :] = 2 -a7 = torch.zeros(400, 200, 300) -b7 = torch.zeros(400, 200, 300) +a7 = torch.zeros(dimAA, dimBB, dimCC) +b7 = torch.zeros(dimAA, dimBB, dimCC) a7[:, 0, :] = 2 b7[:, 110, :] = 2 -a8 = torch.zeros(400, 200, 300) -b8 = torch.zeros(400, 200, 300) +a8 = torch.zeros(dimAA, dimBB, dimCC) +b8 = torch.zeros(dimAA, dimBB, dimCC) # a8[:, :, 20] = 2 # b8[:,:, 130] = 2 @@ -85,8 +90,8 @@ b8[2, 2, 130] = 2 # multi points -a9 = torch.zeros(400, 200, 300) -b9 = torch.zeros(400, 200, 300) +a9 = torch.zeros(dimAA, dimBB, dimCC) +b9 = torch.zeros(dimAA, dimBB, dimCC) a9[0, 20, 0] = 2 a9[0, 0, 30] = 2 From 222460e8857686dcd0f7b800c4036fe9a53e2c09 Mon Sep 17 00:00:00 2001 From: monai-bot Date: Tue, 12 Apr 2022 16:54:59 +0000 Subject: [PATCH 27/41] [MONAI] python code formatting Signed-off-by: monai-bot --- tests/test_hausdorff_distance_morphologic.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_hausdorff_distance_morphologic.py b/tests/test_hausdorff_distance_morphologic.py index b570dfbfae6..4e2567c2a73 100644 --- a/tests/test_hausdorff_distance_morphologic.py +++ b/tests/test_hausdorff_distance_morphologic.py @@ -20,11 +20,11 @@ device = torch.device("cuda") -dimA=11 +dimA = 11 -dimAA=131 -dimBB=111 -dimCC=151 +dimAA = 131 +dimBB = 111 +dimCC = 151 # testing single points diffrent dims # dim1 From 7b3e6a84fe3bb26f7171c95f0f8e8897d1065454 Mon Sep 17 00:00:00 2001 From: Jakub Mitura <53857487+jakubMitura14@users.noreply.github.com> Date: Wed, 13 Apr 2022 16:13:42 +0200 Subject: [PATCH 28/41] filtered cuda version Signed-off-by: Jakub Mitura <53857487+jakubMitura14@users.noreply.github.com> --- tests/test_hausdorff_distance_morphologic.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/tests/test_hausdorff_distance_morphologic.py b/tests/test_hausdorff_distance_morphologic.py index b570dfbfae6..9295b360998 100644 --- a/tests/test_hausdorff_distance_morphologic.py +++ b/tests/test_hausdorff_distance_morphologic.py @@ -16,6 +16,7 @@ from parameterized import parameterized from monai.metrics import MorphologicalHausdorffDistanceMetric +from monai.utils.module import version_leq from tests.utils import skip_if_no_cuda device = torch.device("cuda") @@ -119,12 +120,13 @@ class TestHausdorffDistanceMorphological(unittest.TestCase): @parameterized.expand(TEST_CASES) def test_value(self, input_data, expected_value): - [y_pred, y, percentt, compare_values] = input_data - hd_metric = MorphologicalHausdorffDistanceMetric( - compare_values.to(device), percentt, True - ) # True only for tests - result = hd_metric._compute_tensor(y_pred.to(device), y.to(device)) - np.testing.assert_allclose(expected_value, result, rtol=1e-7) + if(not version_leq(f"{torch.version.cuda}", "10.100") and not version_leq(f"{torch.version.cuda}", "10.200")): + [y_pred, y, percentt, compare_values] = input_data + hd_metric = MorphologicalHausdorffDistanceMetric( + compare_values.to(device), percentt, True + ) # True only for tests + result = hd_metric._compute_tensor(y_pred.to(device), y.to(device)) + np.testing.assert_allclose(expected_value, result, rtol=1e-7) if __name__ == "__main__": From 560649145c6567d0a7b06d89d1d812142662e22f Mon Sep 17 00:00:00 2001 From: Jakub Mitura <53857487+jakubMitura14@users.noreply.github.com> Date: Wed, 13 Apr 2022 19:43:32 +0200 Subject: [PATCH 29/41] simple tests to debug Signed-off-by: Jakub Mitura <53857487+jakubMitura14@users.noreply.github.com> --- tests/test_hausdorff_distance_morphologic.py | 30 ++++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/tests/test_hausdorff_distance_morphologic.py b/tests/test_hausdorff_distance_morphologic.py index ca816eb47f2..7dd7c54c790 100644 --- a/tests/test_hausdorff_distance_morphologic.py +++ b/tests/test_hausdorff_distance_morphologic.py @@ -23,9 +23,9 @@ dimA = 11 -dimAA = 131 -dimBB = 111 -dimCC = 151 +dimAA = 150 +dimBB = 160 +dimCC = 170 # testing single points diffrent dims # dim1 @@ -101,18 +101,18 @@ TEST_CASES = [ [[a, b, 1.0, compare_values], 10], - [[a1, b1, 1.0, compare_values], 15], - [[a2, b2, 1.0, compare_values], 140], - [[a3, b3, 1.0, compare_values_b], 140], - [[a4, b4, 1.0, compare_values_b], 110], - [[a5, b5, 1.0, compare_values_b], 110], - [[a6, b6, 1.0, compare_values_b], 110], - [[a7, b7, 1.0, compare_values_b], 110], - [[a8, b8, 1.0, compare_values_b], 110], # testing robust - [[a6, b6, 0.9, compare_values_b], 110], - [[a7, b7, 0.85, compare_values_b], 110], - [[a8, b8, 0.8, compare_values_b], 110], # multi points - [[a9, b9, 1.0, compare_values_b], 40], + # [[a1, b1, 1.0, compare_values], 15], + # [[a2, b2, 1.0, compare_values], 140], + # [[a3, b3, 1.0, compare_values_b], 140], + # [[a4, b4, 1.0, compare_values_b], 110], + # [[a5, b5, 1.0, compare_values_b], 110], + # [[a6, b6, 1.0, compare_values_b], 110], + # [[a7, b7, 1.0, compare_values_b], 110], + # [[a8, b8, 1.0, compare_values_b], 110], # testing robust + # [[a6, b6, 0.9, compare_values_b], 110], + # [[a7, b7, 0.85, compare_values_b], 110], + # [[a8, b8, 0.8, compare_values_b], 110], # multi points + # [[a9, b9, 1.0, compare_values_b], 40] ] From 53d75bb80c1e722597fd714b36b7f2784908252d Mon Sep 17 00:00:00 2001 From: Jakub Mitura <53857487+jakubMitura14@users.noreply.github.com> Date: Wed, 13 Apr 2022 20:34:45 +0200 Subject: [PATCH 30/41] updated tests Signed-off-by: Jakub Mitura <53857487+jakubMitura14@users.noreply.github.com> --- tests/test_hausdorff_distance_morphologic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_hausdorff_distance_morphologic.py b/tests/test_hausdorff_distance_morphologic.py index 7dd7c54c790..82537f360ce 100644 --- a/tests/test_hausdorff_distance_morphologic.py +++ b/tests/test_hausdorff_distance_morphologic.py @@ -123,7 +123,7 @@ def test_value(self, input_data, expected_value): if(not version_leq(f"{torch.version.cuda}", "10.100") and not version_leq(f"{torch.version.cuda}", "10.200")): [y_pred, y, percentt, compare_values] = input_data hd_metric = MorphologicalHausdorffDistanceMetric( - compare_values.to(device), percentt, True + compare_values.to(device), percentt, False ) # True only for tests result = hd_metric._compute_tensor(y_pred.to(device), y.to(device)) np.testing.assert_allclose(expected_value, result, rtol=1e-7) From 2ae038d44479e9578d08846d6d293c0eabe81bdb Mon Sep 17 00:00:00 2001 From: Jakub Mitura <53857487+jakubMitura14@users.noreply.github.com> Date: Mon, 18 Apr 2022 18:49:52 +0200 Subject: [PATCH 31/41] hopefully fixed tests Signed-off-by: Jakub Mitura <53857487+jakubMitura14@users.noreply.github.com> --- tests/test_hausdorff_distance_morphologic.py | 34 ++++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/tests/test_hausdorff_distance_morphologic.py b/tests/test_hausdorff_distance_morphologic.py index 82537f360ce..3aa96cdd8bd 100644 --- a/tests/test_hausdorff_distance_morphologic.py +++ b/tests/test_hausdorff_distance_morphologic.py @@ -21,11 +21,11 @@ device = torch.device("cuda") -dimA = 11 +dimA = 50 -dimAA = 150 -dimBB = 160 -dimCC = 170 +dimAA = 170 +dimBB = 190 +dimCC = 200 # testing single points diffrent dims # dim1 @@ -101,18 +101,18 @@ TEST_CASES = [ [[a, b, 1.0, compare_values], 10], - # [[a1, b1, 1.0, compare_values], 15], - # [[a2, b2, 1.0, compare_values], 140], - # [[a3, b3, 1.0, compare_values_b], 140], - # [[a4, b4, 1.0, compare_values_b], 110], - # [[a5, b5, 1.0, compare_values_b], 110], - # [[a6, b6, 1.0, compare_values_b], 110], - # [[a7, b7, 1.0, compare_values_b], 110], - # [[a8, b8, 1.0, compare_values_b], 110], # testing robust - # [[a6, b6, 0.9, compare_values_b], 110], - # [[a7, b7, 0.85, compare_values_b], 110], - # [[a8, b8, 0.8, compare_values_b], 110], # multi points - # [[a9, b9, 1.0, compare_values_b], 40] + [[a1, b1, 1.0, compare_values], 15], + [[a2, b2, 1.0, compare_values], 140], + [[a3, b3, 1.0, compare_values_b], 140], + [[a4, b4, 1.0, compare_values_b], 110], + [[a5, b5, 1.0, compare_values_b], 110], + [[a6, b6, 1.0, compare_values_b], 110], + [[a7, b7, 1.0, compare_values_b], 110], + [[a8, b8, 1.0, compare_values_b], 110], # testing robust + [[a6, b6, 0.9, compare_values_b], 110], + [[a7, b7, 0.85, compare_values_b], 110], + [[a8, b8, 0.8, compare_values_b], 110], # multi points + [[a9, b9, 1.0, compare_values_b], 40] ] @@ -123,7 +123,7 @@ def test_value(self, input_data, expected_value): if(not version_leq(f"{torch.version.cuda}", "10.100") and not version_leq(f"{torch.version.cuda}", "10.200")): [y_pred, y, percentt, compare_values] = input_data hd_metric = MorphologicalHausdorffDistanceMetric( - compare_values.to(device), percentt, False + compare_values.to(device), percentt, True ) # True only for tests result = hd_metric._compute_tensor(y_pred.to(device), y.to(device)) np.testing.assert_allclose(expected_value, result, rtol=1e-7) From d8163324b0d7d434ecae4ba70ae462b87dcb1665 Mon Sep 17 00:00:00 2001 From: Jakub Mitura Date: Sat, 30 Apr 2022 14:39:15 +0200 Subject: [PATCH 32/41] formatted cu --- monai/_extensions/hausdorff/hausdorff_cuda.cu | 23 ++----------------- 1 file changed, 2 insertions(+), 21 deletions(-) diff --git a/monai/_extensions/hausdorff/hausdorff_cuda.cu b/monai/_extensions/hausdorff/hausdorff_cuda.cu index 193761ca191..7c1be2bdd46 100644 --- a/monai/_extensions/hausdorff/hausdorff_cuda.cu +++ b/monai/_extensions/hausdorff/hausdorff_cuda.cu @@ -2022,14 +2022,11 @@ int getHausdorffDistance_CUDA_Generic( float robustnessPercent, bool resIterNeeded, at::Tensor compare_values) { - // TODO() use https ://pytorch.org/cppdocs/notes/tensor_cuda_stream.html cudaStream_t stream1; cudaStreamCreate(&stream1); MetaDataCPU metaData; - // size_t size = sizeof(unsigned int) * 20; - // unsigned int* minMaxesCPU = (unsigned int*)malloc(size); - // metaData.minMaxes = minMaxesCPU; + ForFullBoolPrepArgs forFullBoolPrepArgs; forFullBoolPrepArgs.metaData = metaData; @@ -2140,8 +2137,6 @@ at::Tensor getHausdorffDistance_CUDA_FullResList( -<<<<<<< HEAD -======= /************************************************************* *Oliviera Algorithm @@ -2429,9 +2424,6 @@ int HausdorffDistance::computeDistance(Volume* img1, Volume* img2, bool* d_img1, cudaFree(d_img2Read); // resetting device - // cudaDeviceReset(); - - // print(cudaGetLastError(), "processing CUDA. Something may be wrong with your CUDA device."); return distance; } @@ -2464,10 +2456,7 @@ std::tuple benchmarkOlivieraCUDA( cudaMemcpy(goldStandard, goldStandardA.data_ptr(), sizee, cudaMemcpyDeviceToHost); cudaMemcpy(algoOutput, algoOutputA.data_ptr(), sizee, cudaMemcpyDeviceToHost); - // auto goldStandardA.data_ptr() - // bool* goldStandard = (bool*)goldStandardA.to(torch::kCPU).data_ptr(); - // bool* algoOutput = (bool*)algoOutputA.to(torch::kCPU).data_ptr(); Volume img1 = Volume(WIDTH, HEIGHT, DEPTH), img2 = Volume(WIDTH, HEIGHT, DEPTH); @@ -2500,22 +2489,14 @@ std::tuple benchmarkOlivieraCUDA( auto end = std::chrono::high_resolution_clock::now(); - // std::cout << "Total elapsed time: "; double time = (double)(std::chrono::duration_cast(end - begin).count() / (double)1000000000); - // std::cout << time << "s" << std::endl; - - // printf("HD: %d \n", dist); // freeing memory img1.dispose(); img2.dispose(); free(goldStandard); free(algoOutput); - // Datasize: 216530944 - // Datasize : 216530944 - // Total elapsed time : 2.62191s - // HD : 234 + return {dist, time}; } ->>>>>>> 7fb1821c8c2aa82d8aaa21bfbd1a69237b0ea960 From f1eba75311d628ec98c786ebfd5e3c8635818ab2 Mon Sep 17 00:00:00 2001 From: monai-bot Date: Sat, 30 Apr 2022 12:50:31 +0000 Subject: [PATCH 33/41] [MONAI] python code formatting Signed-off-by: monai-bot --- monai/_extensions/hausdorff/hausdorff_cuda.cu | 10 ---------- tests/test_hausdorff_distance_morphologic.py | 4 ++-- 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/monai/_extensions/hausdorff/hausdorff_cuda.cu b/monai/_extensions/hausdorff/hausdorff_cuda.cu index 7c1be2bdd46..38bd5747722 100644 --- a/monai/_extensions/hausdorff/hausdorff_cuda.cu +++ b/monai/_extensions/hausdorff/hausdorff_cuda.cu @@ -2027,7 +2027,6 @@ int getHausdorffDistance_CUDA_Generic( MetaDataCPU metaData; - ForFullBoolPrepArgs forFullBoolPrepArgs; forFullBoolPrepArgs.metaData = metaData; forFullBoolPrepArgs.compare_values = compare_values.item(); @@ -2131,13 +2130,6 @@ at::Tensor getHausdorffDistance_CUDA_FullResList( return res; } - - - - - - - /************************************************************* *Oliviera Algorithm *************************************************************************************/ @@ -2456,8 +2448,6 @@ std::tuple benchmarkOlivieraCUDA( cudaMemcpy(goldStandard, goldStandardA.data_ptr(), sizee, cudaMemcpyDeviceToHost); cudaMemcpy(algoOutput, algoOutputA.data_ptr(), sizee, cudaMemcpyDeviceToHost); - - Volume img1 = Volume(WIDTH, HEIGHT, DEPTH), img2 = Volume(WIDTH, HEIGHT, DEPTH); for (int x = 0; x < WIDTH; x++) { diff --git a/tests/test_hausdorff_distance_morphologic.py b/tests/test_hausdorff_distance_morphologic.py index 3aa96cdd8bd..2fcbaf87b7a 100644 --- a/tests/test_hausdorff_distance_morphologic.py +++ b/tests/test_hausdorff_distance_morphologic.py @@ -112,7 +112,7 @@ [[a6, b6, 0.9, compare_values_b], 110], [[a7, b7, 0.85, compare_values_b], 110], [[a8, b8, 0.8, compare_values_b], 110], # multi points - [[a9, b9, 1.0, compare_values_b], 40] + [[a9, b9, 1.0, compare_values_b], 40], ] @@ -120,7 +120,7 @@ class TestHausdorffDistanceMorphological(unittest.TestCase): @parameterized.expand(TEST_CASES) def test_value(self, input_data, expected_value): - if(not version_leq(f"{torch.version.cuda}", "10.100") and not version_leq(f"{torch.version.cuda}", "10.200")): + if not version_leq(f"{torch.version.cuda}", "10.100") and not version_leq(f"{torch.version.cuda}", "10.200"): [y_pred, y, percentt, compare_values] = input_data hd_metric = MorphologicalHausdorffDistanceMetric( compare_values.to(device), percentt, True From 49e5a526bd8516060c6a42ad045ac5711a8c3e4f Mon Sep 17 00:00:00 2001 From: Jakub Mitura Date: Sat, 30 Apr 2022 21:20:14 +0200 Subject: [PATCH 34/41] commented out full res list ; correction of naming Signed-off-by: Jakub Mitura --- monai/_extensions/hausdorff/hausdorff.cpp | 80 +- monai/_extensions/hausdorff/hausdorff_cuda.cu | 832 +++++++++--------- tests/test_hausdorff_distance_morphologic.py | 48 +- 3 files changed, 480 insertions(+), 480 deletions(-) diff --git a/monai/_extensions/hausdorff/hausdorff.cpp b/monai/_extensions/hausdorff/hausdorff.cpp index 52d8637b4b9..6de168a59b3 100644 --- a/monai/_extensions/hausdorff/hausdorff.cpp +++ b/monai/_extensions/hausdorff/hausdorff.cpp @@ -23,20 +23,20 @@ int getHausdorffDistance_CUDA( const int zDim, const float robustnessPercent); -at::Tensor getHausdorffDistance_CUDA_FullResList( - at::Tensor goldStandard, - at::Tensor algoOutput, - int WIDTH, - int HEIGHT, - int DEPTH, - float robustnessPercent); +// at::Tensor getHausdorffDistance_CUDA_FullResList( +// at::Tensor goldStandard, +// at::Tensor algoOutput, +// int WIDTH, +// int HEIGHT, +// int DEPTH, +// float robustnessPercent); -std::tuple benchmarkOlivieraCUDA( - torch::Tensor goldStandard, - torch::Tensor algoOutput, - const int xDim, - const int yDim, - const int zDim); +// std::tuple benchmarkOlivieraCUDA( +// torch::Tensor goldStandard, +// torch::Tensor algoOutput, +// const int xDim, +// const int yDim, +// const int zDim); // C++ interface @@ -60,36 +60,36 @@ int getHausdorffDistance( return getHausdorffDistance_CUDA(goldStandard, algoOutput, xDim, yDim, zDim, robustnessPercent); } -at::Tensor getHausdorffDistance_FullResList( - torch::Tensor goldStandard, - torch::Tensor algoOutput, - const int xDim, - const int yDim, - const int zDim, - const float robustnessPercent = 1.0) { - CHECK_INPUT(goldStandard); - CHECK_INPUT(algoOutput); +// at::Tensor getHausdorffDistance_FullResList( +// torch::Tensor goldStandard, +// torch::Tensor algoOutput, +// const int xDim, +// const int yDim, +// const int zDim, +// const float robustnessPercent = 1.0) { +// CHECK_INPUT(goldStandard); +// CHECK_INPUT(algoOutput); - return getHausdorffDistance_CUDA_FullResList(goldStandard, algoOutput, xDim, yDim, zDim, robustnessPercent); -} +// return getHausdorffDistance_CUDA_FullResList(goldStandard, algoOutput, xDim, yDim, zDim, robustnessPercent); +// } -std::tuple benchmarkOlivieraCUDAOnlyBool( - torch::Tensor goldStandard, - torch::Tensor algoOutput, - const int xDim, - const int yDim, - const int zDim) { - return benchmarkOlivieraCUDA(goldStandard, algoOutput, xDim, yDim, zDim); -} +// std::tuple benchmarkOlivieraCUDAOnlyBool( +// torch::Tensor goldStandard, +// torch::Tensor algoOutput, +// const int xDim, +// const int yDim, +// const int zDim) { +// return benchmarkOlivieraCUDA(goldStandard, algoOutput, xDim, yDim, zDim); +// } PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) { m.def("getHausdorffDistance", &getHausdorffDistance, "Basic version of Hausdorff distance"); - m.def( - "benchmarkOlivieraCUDA", - &benchmarkOlivieraCUDA, - "Algorithm by Oliviera - just for comparison sake - accept only boolean arrays "); - m.def( - "getHausdorffDistance_FullResList", - &getHausdorffDistance_FullResList, - " return additionally full result list indicating in which dilatation iterations results were recorded "); + // m.def( + // "benchmarkOlivieraCUDA", + // &benchmarkOlivieraCUDA, + // "Algorithm by Oliviera - just for comparison sake - accept only boolean arrays "); + // m.def( + // "getHausdorffDistance_FullResList", + // &getHausdorffDistance_FullResList, + // " return additionally full result list indicating in which dilatation iterations results were recorded "); } diff --git a/monai/_extensions/hausdorff/hausdorff_cuda.cu b/monai/_extensions/hausdorff/hausdorff_cuda.cu index 7c1be2bdd46..98102070097 100644 --- a/monai/_extensions/hausdorff/hausdorff_cuda.cu +++ b/monai/_extensions/hausdorff/hausdorff_cuda.cu @@ -2055,42 +2055,42 @@ int getHausdorffDistance_CUDA_Generic( return result; } -template -at::Tensor getHausdorffDistance_CUDA_FullResList_local( - at::Tensor goldStandard, - at::Tensor algoOutput, - int WIDTH, - int HEIGHT, - int DEPTH, - float robustnessPercent, - at::Tensor compare_values) { - // TODO() use https ://pytorch.org/cppdocs/notes/tensor_cuda_stream.html - cudaStream_t stream1; - cudaStreamCreate(&stream1); - - MetaDataCPU metaData; - // size_t size = sizeof(unsigned int) * 20; - // unsigned int* minMaxesCPU = (unsigned int*)malloc(size); - // metaData.minMaxes = minMaxesCPU; - - ForFullBoolPrepArgs forFullBoolPrepArgs; - forFullBoolPrepArgs.metaData = metaData; - forFullBoolPrepArgs.compare_values = compare_values.item(); - forFullBoolPrepArgs.goldArr = goldStandard; - forFullBoolPrepArgs.segmArr = algoOutput; - - occupancyCalcData occData = getOccupancy(); - - ForBoolKernelArgs fbArgs = - executeHausdoff(forFullBoolPrepArgs, WIDTH, HEIGHT, DEPTH, occData, stream1, false, robustnessPercent, true); - - cudaFreeAsync(fbArgs.minMaxes, stream1); - // free(metaData.minMaxesCPU); - - cudaStreamDestroy(stream1); - - return fbArgs.resultListPointerIterNumbTensor; -} +// template +// at::Tensor getHausdorffDistance_CUDA_FullResList_local( +// at::Tensor goldStandard, +// at::Tensor algoOutput, +// int WIDTH, +// int HEIGHT, +// int DEPTH, +// float robustnessPercent, +// at::Tensor compare_values) { +// // TODO() use https ://pytorch.org/cppdocs/notes/tensor_cuda_stream.html +// cudaStream_t stream1; +// cudaStreamCreate(&stream1); + +// MetaDataCPU metaData; +// // size_t size = sizeof(unsigned int) * 20; +// // unsigned int* minMaxesCPU = (unsigned int*)malloc(size); +// // metaData.minMaxes = minMaxesCPU; + +// ForFullBoolPrepArgs forFullBoolPrepArgs; +// forFullBoolPrepArgs.metaData = metaData; +// forFullBoolPrepArgs.compare_values = compare_values.item(); +// forFullBoolPrepArgs.goldArr = goldStandard; +// forFullBoolPrepArgs.segmArr = algoOutput; + +// occupancyCalcData occData = getOccupancy(); + +// ForBoolKernelArgs fbArgs = +// executeHausdoff(forFullBoolPrepArgs, WIDTH, HEIGHT, DEPTH, occData, stream1, false, robustnessPercent, true); + +// cudaFreeAsync(fbArgs.minMaxes, stream1); +// // free(metaData.minMaxesCPU); + +// cudaStreamDestroy(stream1); + +// return fbArgs.resultListPointerIterNumbTensor; +// } /* Functions for pybind @@ -2113,390 +2113,390 @@ int getHausdorffDistance_CUDA( return res; } -at::Tensor getHausdorffDistance_CUDA_FullResList( - at::Tensor goldStandard, - at::Tensor algoOutput, - const int WIDTH, - const int HEIGHT, - const int DEPTH, - const float robustnessPercent, - at::Tensor compare_values) { - at::Tensor res; - AT_DISPATCH_ALL_TYPESWithBool( - goldStandard.type(), "getHausdorffDistance_CUDA_FullResList", ([&] { - res = getHausdorffDistance_CUDA_FullResList_local( - goldStandard, algoOutput, WIDTH, HEIGHT, DEPTH, robustnessPercent, compare_values); - })); +// at::Tensor getHausdorffDistance_CUDA_FullResList( +// at::Tensor goldStandard, +// at::Tensor algoOutput, +// const int WIDTH, +// const int HEIGHT, +// const int DEPTH, +// const float robustnessPercent, +// at::Tensor compare_values) { +// at::Tensor res; +// AT_DISPATCH_ALL_TYPESWithBool( +// goldStandard.type(), "getHausdorffDistance_CUDA_FullResList", ([&] { +// res = getHausdorffDistance_CUDA_FullResList_local( +// goldStandard, algoOutput, WIDTH, HEIGHT, DEPTH, robustnessPercent, compare_values); +// })); + +// return res; +// } + + + + + + + + +// /************************************************************* +// *Oliviera Algorithm +// *************************************************************************************/ + +// typedef unsigned char uchar; +// typedef unsigned int uint; +// #pragma once +// class Volume { +// private: +// bool* volume; +// int width, height, depth; +// int getLinearIndex(int x, int y, int z); + +// public: +// bool getVoxelValue(int x, int y, int z); +// bool getPixelValue(int x, int y); +// uint getWidth(); +// uint getHeight(); +// uint getDepth(); +// bool* getVolume(); +// void setVoxelValue(bool value, int x, int y, int z); +// void setPixelValue(bool value, int x, int y); +// Volume(int width, int height, int depth); +// Volume(int width, int height); +// void dispose(); +// }; + +// #define CUDA_DEVICE_INDEX 0 // setting the index of your CUDA device + +// #define IS_3D \ +// 1 // setting this to 0 would grant a very slightly improvement on the performance if working with images only +// #define CHEBYSHEV \ +// 0 // if not set to 1, then this algorithm would use an Euclidean-like metric, it is just an approximation. +// // It can be changed according to the structuring element +// #pragma once +// class HausdorffDistance { +// private: +// void print(cudaError_t error, char* msg); + +// public: +// int computeDistance(Volume* img1, Volume* img2, bool* d_img1, bool* d_img2); +// }; + +// inline Volume::Volume(const int width, const int height, const int depth) { +// this->width = width; +// this->height = height; +// this->depth = depth; +// volume = (bool*)calloc(width * height * depth, sizeof(bool)); +// } + +// #pragma once +// inline Volume::Volume(const int width, const int height) { +// this->width = width; +// this->height = height; +// this->depth = 1; +// volume = (bool*)calloc(width * height * depth, sizeof(bool)); +// } +// #pragma once +// inline int Volume::getLinearIndex(const int x, const int y, const int z) { +// const int a = 1, b = width, c = (width) * (height); +// return a * x + b * y + c * z; +// } + +// inline uint Volume::getWidth() { +// return this->width; +// } +// inline uint Volume::getHeight() { +// return this->height; +// } +// inline uint Volume::getDepth() { +// return this->depth; +// } +// inline bool* Volume::getVolume() { +// return this->volume; +// } +// inline bool Volume::getPixelValue(int x, int y) { +// return this->volume[getLinearIndex(x, y, 0)]; +// } +// #pragma once +// inline bool Volume::getVoxelValue(int x, int y, int z) { +// return volume[getLinearIndex(x, y, z)]; +// } +// #pragma once +// inline void Volume::setPixelValue(bool value, const int x, const int y) { +// volume[getLinearIndex(x, y, 0)] = value; +// } +// #pragma once +// inline void Volume::setVoxelValue(bool value, const int x, const int y, const int z) { +// volume[getLinearIndex(x, y, z)] = value; +// } +// #pragma once +// inline void Volume::dispose() { +// free(volume); +// } + +// typedef unsigned char uchar; +// typedef unsigned int uint; + +// #pragma once +// __device__ int finished; // global variable that contains a boolean which indicates when to stop the kernel processing +// #pragma once +// __constant__ __device__ int WIDTH, HEIGHT, DEPTH; // constant variables that contain the size of the volume + +// #pragma once +// __global__ void dilate( +// const bool* IMG1, +// const bool* IMG2, +// const bool* img1Read, +// const bool* img2Read, +// bool* img1Write, +// bool* img2Write) { +// const int id = blockDim.x * blockIdx.x + threadIdx.x; +// #if !IS_3D +// const int x = id % WIDTH, y = id / WIDTH; +// #else +// const int x = id % WIDTH, y = (id / WIDTH) % HEIGHT, z = (id / WIDTH) / HEIGHT; +// #endif + +// if (id < WIDTH * HEIGHT * DEPTH) { +// if (img1Read[id]) { +// if (x + 1 < WIDTH) +// img1Write[id + 1] = true; +// if (x - 1 >= 0) +// img1Write[id - 1] = true; +// if (y + 1 < HEIGHT) +// img1Write[id + WIDTH] = true; +// if (y - 1 >= 0) +// img1Write[id - WIDTH] = true; +// #if IS_3D // if working with 3d volumes, then the 3D part +// if (z + 1 < DEPTH) +// img1Write[id + WIDTH * HEIGHT] = true; +// if (z - 1 >= 0) +// img1Write[id - WIDTH * HEIGHT] = true; +// #endif + +// #if CHEBYSHEV +// // diagonals +// if (x + 1 < WIDTH && y - 1 >= 0) +// img1Write[id - WIDTH + 1] = true; +// if (x - 1 >= 0 && y - 1 >= 0) +// img1Write[id - WIDTH - 1] = true; +// if (x + 1 < WIDTH && y + 1 < HEIGHT) +// img1Write[id + WIDTH + 1] = true; +// if (x - 1 >= 0 && y + 1 < HEIGHT) +// img1Write[id + WIDTH - 1] = true; +// #if IS_3D // if working with 3d volumes, then the 3D part +// if (z + 1 < DEPTH && x + 1 < WIDTH && y - 1 >= 0) +// img1Write[id - WIDTH + 1 + WIDTH * HEIGHT] = true; +// if (z + 1 < DEPTH && x - 1 >= 0 && y - 1 >= 0) +// img1Write[id - WIDTH - 1 + WIDTH * HEIGHT] = true; +// if (z + 1 < DEPTH && x + 1 < WIDTH && y + 1 < HEIGHT) +// img1Write[id + WIDTH + 1 + WIDTH * HEIGHT] = true; +// if (z + 1 < DEPTH && x - 1 >= 0 && y + 1 < HEIGHT) +// img1Write[id + WIDTH - 1 + WIDTH * HEIGHT] = true; +// if (z - 1 >= 0 && x + 1 < WIDTH && y - 1 >= 0) +// img1Write[id - WIDTH + 1 - WIDTH * HEIGHT] = true; +// if (z - 1 >= 0 && x - 1 >= 0 && y - 1 >= 0) +// img1Write[id - WIDTH - 1 - WIDTH * HEIGHT] = true; +// if (z - 1 >= 0 && x + 1 < WIDTH && y + 1 < HEIGHT) +// img1Write[id + WIDTH + 1 - WIDTH * HEIGHT] = true; +// if (z - 1 >= 0 && x - 1 >= 0 && y + 1 < HEIGHT) +// img1Write[id + WIDTH - 1 - WIDTH * HEIGHT] = true; +// #endif +// #endif +// } + +// if (img2Read[id]) { +// if (x + 1 < WIDTH) +// img2Write[id + 1] = true; +// if (x - 1 >= 0) +// img2Write[id - 1] = true; +// if (y + 1 < HEIGHT) +// img2Write[id + WIDTH] = true; +// if (y - 1 >= 0) +// img2Write[id - WIDTH] = true; +// #if IS_3D // if working with 3d volumes, then the 3D part +// if (z + 1 < DEPTH) +// img2Write[id + WIDTH * HEIGHT] = true; +// if (z - 1 >= 0) +// img2Write[id - WIDTH * HEIGHT] = true; +// #endif + +// #if CHEBYSHEV +// // diagonals +// if (x + 1 < WIDTH && y - 1 >= 0) +// img2Write[id - WIDTH + 1] = true; +// if (x - 1 >= 0 && y - 1 >= 0) +// img2Write[id - WIDTH - 1] = true; +// if (x + 1 < WIDTH && y + 1 < HEIGHT) +// img2Write[id + WIDTH + 1] = true; +// if (x - 1 >= 0 && y + 1 < HEIGHT) +// img2Write[id + WIDTH - 1] = true; +// #if IS_3D // if working with 3d volumes, then the 3D part +// if (z + 1 < DEPTH && x + 1 < WIDTH && y - 1 >= 0) +// img2Write[id - WIDTH + 1 + WIDTH * HEIGHT] = true; +// if (z + 1 < DEPTH && x - 1 >= 0 && y - 1 >= 0) +// img2Write[id - WIDTH - 1 + WIDTH * HEIGHT] = true; +// if (z + 1 < DEPTH && x + 1 < WIDTH && y + 1 < HEIGHT) +// img2Write[id + WIDTH + 1 + WIDTH * HEIGHT] = true; +// if (z + 1 < DEPTH && x - 1 >= 0 && y + 1 < HEIGHT) +// img2Write[id + WIDTH - 1 + WIDTH * HEIGHT] = true; +// if (z - 1 >= 0 && x + 1 < WIDTH && y - 1 >= 0) +// img2Write[id - WIDTH + 1 - WIDTH * HEIGHT] = true; +// if (z - 1 >= 0 && x - 1 >= 0 && y - 1 >= 0) +// img2Write[id - WIDTH - 1 - WIDTH * HEIGHT] = true; +// if (z - 1 >= 0 && x + 1 < WIDTH && y + 1 < HEIGHT) +// img2Write[id + WIDTH + 1 - WIDTH * HEIGHT] = true; +// if (z - 1 >= 0 && x - 1 >= 0 && y + 1 < HEIGHT) +// img2Write[id + WIDTH - 1 - WIDTH * HEIGHT] = true; +// #endif +// #endif +// } + +// // this is an atomic and computed to the finished global variable, if image 1 contains all of image 2 and image 2 +// // contains all pixels of image 1 then finished is true +// atomicAnd(&finished, (img2Read[id] || !IMG1[id]) && (img1Read[id] || !IMG2[id])); +// } +// } + +// #pragma once +// int HausdorffDistance::computeDistance(Volume* img1, Volume* img2, bool* d_img1, bool* d_img2) { +// const int height = (*img1).getHeight(), width = (*img1).getWidth(), depth = (*img1).getDepth(); + +// size_t size = width * height * depth * sizeof(bool); + +// // getting details of your CUDA device +// cudaDeviceProp props; +// cudaGetDeviceProperties( +// &props, CUDA_DEVICE_INDEX); // device index = 0, you can change it if you have more CUDA devices +// const int threadsPerBlock = props.maxThreadsPerBlock / 2; +// const int blocksPerGrid = (height * width * depth + threadsPerBlock - 1) / threadsPerBlock; + +// // copying the dimensions to the GPU +// cudaMemcpyToSymbolAsync(WIDTH, &width, sizeof(width), 0); +// cudaMemcpyToSymbolAsync(HEIGHT, &height, sizeof(height), 0); +// cudaMemcpyToSymbolAsync(DEPTH, &depth, sizeof(depth), 0); + +// // allocating the input images on the GPU + +// // allocating the images that will be the processing ones +// bool *d_img1Write, *d_img1Read, *d_img2Write, *d_img2Read; +// cudaMalloc(&d_img1Write, size); +// cudaMalloc(&d_img1Read, size); +// cudaMalloc(&d_img2Write, size); +// cudaMalloc(&d_img2Read, size); + +// // cloning the input images to these two image versions (write and read) +// cudaMemcpyAsync(d_img1Read, d_img1, size, cudaMemcpyDeviceToDevice); +// cudaMemcpyAsync(d_img2Read, d_img2, size, cudaMemcpyDeviceToDevice); +// cudaMemcpyAsync(d_img1Write, d_img1, size, cudaMemcpyDeviceToDevice); +// cudaMemcpyAsync(d_img2Write, d_img2, size, cudaMemcpyDeviceToDevice); + +// // required variables to compute the distance +// int h_finished = false, t = true; +// int distance = -1; + +// // where the magic happens +// while (!h_finished) { +// // reset the bool variable that verifies if the processing ended +// cudaMemcpyToSymbol(finished, &t, sizeof(h_finished)); + +// // lauching the verify kernel, which verifies if the processing finished +// dilate<<>>(d_img1, d_img2, d_img1Read, d_img2Read, d_img1Write, d_img2Write); + +// // cudaDeviceSynchronize(); + +// // updating the imgRead (cloning imgWrite to imgRead) +// cudaMemcpy(d_img1Read, d_img1Write, size, cudaMemcpyDeviceToDevice); +// cudaMemcpy(d_img2Read, d_img2Write, size, cudaMemcpyDeviceToDevice); + +// // copying the result back to host memory +// cudaMemcpyFromSymbol(&h_finished, finished, sizeof(h_finished)); + +// // incrementing the distance at each iteration +// distance++; +// } + +// // freeing memory +// cudaFree(d_img1); +// cudaFree(d_img2); +// cudaFree(d_img1Write); +// cudaFree(d_img1Read); +// cudaFree(d_img2Write); +// cudaFree(d_img2Read); + +// // resetting device + +// return distance; +// } +// #pragma once +// inline void HausdorffDistance::print(cudaError_t error, char* msg) { +// if (error != cudaSuccess) { +// printf("Error on %s ", msg); +// fprintf(stderr, "Error code: %s!\n", cudaGetErrorString(error)); +// exit(EXIT_FAILURE); +// } +// } + +// /* +// benchmark for original code from https://github.com/Oyatsumi/HausdorffDistanceComparison +// */ +// std::tuple benchmarkOlivieraCUDA( +// torch::Tensor goldStandardA, +// torch::Tensor algoOutputA, +// int WIDTH, +// int HEIGHT, +// int DEPTH) { +// // just originally it started for cpu so ... + +// int lenn = WIDTH * HEIGHT * DEPTH; +// size_t sizee = sizeof(bool) * lenn; + +// bool* goldStandard = (bool*)calloc(lenn, sizeof(bool)); +// bool* algoOutput = (bool*)calloc(lenn, sizeof(bool)); + +// cudaMemcpy(goldStandard, goldStandardA.data_ptr(), sizee, cudaMemcpyDeviceToHost); +// cudaMemcpy(algoOutput, algoOutputA.data_ptr(), sizee, cudaMemcpyDeviceToHost); + + + +// Volume img1 = Volume(WIDTH, HEIGHT, DEPTH), img2 = Volume(WIDTH, HEIGHT, DEPTH); + +// for (int x = 0; x < WIDTH; x++) { +// for (int y = 0; y < HEIGHT; y++) { +// for (int z = 0; z < DEPTH; z++) { +// img1.setVoxelValue(algoOutput[x + y * WIDTH + z * WIDTH * HEIGHT], x, y, z); +// img2.setVoxelValue(goldStandard[x + y * WIDTH + z * WIDTH * HEIGHT], x, y, z); +// } +// } +// } + +// size_t size = WIDTH * HEIGHT * DEPTH * sizeof(bool); + +// bool *d_img1, *d_img2; +// cudaMalloc(&d_img1, size); +// cudaMalloc(&d_img2, size); - return res; -} +// // copying the data to the allocated memory on the GPU +// cudaMemcpyAsync(d_img1, (img1).getVolume(), size, cudaMemcpyHostToDevice); +// cudaMemcpyAsync(d_img2, (img2).getVolume(), size, cudaMemcpyHostToDevice); + +// auto begin = std::chrono::high_resolution_clock::now(); +// HausdorffDistance* hd = new HausdorffDistance(); +// cudaDeviceSynchronize(); +// int dist = (*hd).computeDistance(&img1, &img2, d_img1, d_img2); +// cudaDeviceSynchronize(); +// auto end = std::chrono::high_resolution_clock::now(); +// double time = +// (double)(std::chrono::duration_cast(end - begin).count() / (double)1000000000); +// // freeing memory +// img1.dispose(); +// img2.dispose(); +// free(goldStandard); +// free(algoOutput); - - -/************************************************************* - *Oliviera Algorithm - *************************************************************************************/ - -typedef unsigned char uchar; -typedef unsigned int uint; -#pragma once -class Volume { - private: - bool* volume; - int width, height, depth; - int getLinearIndex(int x, int y, int z); - - public: - bool getVoxelValue(int x, int y, int z); - bool getPixelValue(int x, int y); - uint getWidth(); - uint getHeight(); - uint getDepth(); - bool* getVolume(); - void setVoxelValue(bool value, int x, int y, int z); - void setPixelValue(bool value, int x, int y); - Volume(int width, int height, int depth); - Volume(int width, int height); - void dispose(); -}; - -#define CUDA_DEVICE_INDEX 0 // setting the index of your CUDA device - -#define IS_3D \ - 1 // setting this to 0 would grant a very slightly improvement on the performance if working with images only -#define CHEBYSHEV \ - 0 // if not set to 1, then this algorithm would use an Euclidean-like metric, it is just an approximation. -// It can be changed according to the structuring element -#pragma once -class HausdorffDistance { - private: - void print(cudaError_t error, char* msg); - - public: - int computeDistance(Volume* img1, Volume* img2, bool* d_img1, bool* d_img2); -}; - -inline Volume::Volume(const int width, const int height, const int depth) { - this->width = width; - this->height = height; - this->depth = depth; - volume = (bool*)calloc(width * height * depth, sizeof(bool)); -} - -#pragma once -inline Volume::Volume(const int width, const int height) { - this->width = width; - this->height = height; - this->depth = 1; - volume = (bool*)calloc(width * height * depth, sizeof(bool)); -} -#pragma once -inline int Volume::getLinearIndex(const int x, const int y, const int z) { - const int a = 1, b = width, c = (width) * (height); - return a * x + b * y + c * z; -} - -inline uint Volume::getWidth() { - return this->width; -} -inline uint Volume::getHeight() { - return this->height; -} -inline uint Volume::getDepth() { - return this->depth; -} -inline bool* Volume::getVolume() { - return this->volume; -} -inline bool Volume::getPixelValue(int x, int y) { - return this->volume[getLinearIndex(x, y, 0)]; -} -#pragma once -inline bool Volume::getVoxelValue(int x, int y, int z) { - return volume[getLinearIndex(x, y, z)]; -} -#pragma once -inline void Volume::setPixelValue(bool value, const int x, const int y) { - volume[getLinearIndex(x, y, 0)] = value; -} -#pragma once -inline void Volume::setVoxelValue(bool value, const int x, const int y, const int z) { - volume[getLinearIndex(x, y, z)] = value; -} -#pragma once -inline void Volume::dispose() { - free(volume); -} - -typedef unsigned char uchar; -typedef unsigned int uint; - -#pragma once -__device__ int finished; // global variable that contains a boolean which indicates when to stop the kernel processing -#pragma once -__constant__ __device__ int WIDTH, HEIGHT, DEPTH; // constant variables that contain the size of the volume - -#pragma once -__global__ void dilate( - const bool* IMG1, - const bool* IMG2, - const bool* img1Read, - const bool* img2Read, - bool* img1Write, - bool* img2Write) { - const int id = blockDim.x * blockIdx.x + threadIdx.x; -#if !IS_3D - const int x = id % WIDTH, y = id / WIDTH; -#else - const int x = id % WIDTH, y = (id / WIDTH) % HEIGHT, z = (id / WIDTH) / HEIGHT; -#endif - - if (id < WIDTH * HEIGHT * DEPTH) { - if (img1Read[id]) { - if (x + 1 < WIDTH) - img1Write[id + 1] = true; - if (x - 1 >= 0) - img1Write[id - 1] = true; - if (y + 1 < HEIGHT) - img1Write[id + WIDTH] = true; - if (y - 1 >= 0) - img1Write[id - WIDTH] = true; -#if IS_3D // if working with 3d volumes, then the 3D part - if (z + 1 < DEPTH) - img1Write[id + WIDTH * HEIGHT] = true; - if (z - 1 >= 0) - img1Write[id - WIDTH * HEIGHT] = true; -#endif - -#if CHEBYSHEV - // diagonals - if (x + 1 < WIDTH && y - 1 >= 0) - img1Write[id - WIDTH + 1] = true; - if (x - 1 >= 0 && y - 1 >= 0) - img1Write[id - WIDTH - 1] = true; - if (x + 1 < WIDTH && y + 1 < HEIGHT) - img1Write[id + WIDTH + 1] = true; - if (x - 1 >= 0 && y + 1 < HEIGHT) - img1Write[id + WIDTH - 1] = true; -#if IS_3D // if working with 3d volumes, then the 3D part - if (z + 1 < DEPTH && x + 1 < WIDTH && y - 1 >= 0) - img1Write[id - WIDTH + 1 + WIDTH * HEIGHT] = true; - if (z + 1 < DEPTH && x - 1 >= 0 && y - 1 >= 0) - img1Write[id - WIDTH - 1 + WIDTH * HEIGHT] = true; - if (z + 1 < DEPTH && x + 1 < WIDTH && y + 1 < HEIGHT) - img1Write[id + WIDTH + 1 + WIDTH * HEIGHT] = true; - if (z + 1 < DEPTH && x - 1 >= 0 && y + 1 < HEIGHT) - img1Write[id + WIDTH - 1 + WIDTH * HEIGHT] = true; - if (z - 1 >= 0 && x + 1 < WIDTH && y - 1 >= 0) - img1Write[id - WIDTH + 1 - WIDTH * HEIGHT] = true; - if (z - 1 >= 0 && x - 1 >= 0 && y - 1 >= 0) - img1Write[id - WIDTH - 1 - WIDTH * HEIGHT] = true; - if (z - 1 >= 0 && x + 1 < WIDTH && y + 1 < HEIGHT) - img1Write[id + WIDTH + 1 - WIDTH * HEIGHT] = true; - if (z - 1 >= 0 && x - 1 >= 0 && y + 1 < HEIGHT) - img1Write[id + WIDTH - 1 - WIDTH * HEIGHT] = true; -#endif -#endif - } - - if (img2Read[id]) { - if (x + 1 < WIDTH) - img2Write[id + 1] = true; - if (x - 1 >= 0) - img2Write[id - 1] = true; - if (y + 1 < HEIGHT) - img2Write[id + WIDTH] = true; - if (y - 1 >= 0) - img2Write[id - WIDTH] = true; -#if IS_3D // if working with 3d volumes, then the 3D part - if (z + 1 < DEPTH) - img2Write[id + WIDTH * HEIGHT] = true; - if (z - 1 >= 0) - img2Write[id - WIDTH * HEIGHT] = true; -#endif - -#if CHEBYSHEV - // diagonals - if (x + 1 < WIDTH && y - 1 >= 0) - img2Write[id - WIDTH + 1] = true; - if (x - 1 >= 0 && y - 1 >= 0) - img2Write[id - WIDTH - 1] = true; - if (x + 1 < WIDTH && y + 1 < HEIGHT) - img2Write[id + WIDTH + 1] = true; - if (x - 1 >= 0 && y + 1 < HEIGHT) - img2Write[id + WIDTH - 1] = true; -#if IS_3D // if working with 3d volumes, then the 3D part - if (z + 1 < DEPTH && x + 1 < WIDTH && y - 1 >= 0) - img2Write[id - WIDTH + 1 + WIDTH * HEIGHT] = true; - if (z + 1 < DEPTH && x - 1 >= 0 && y - 1 >= 0) - img2Write[id - WIDTH - 1 + WIDTH * HEIGHT] = true; - if (z + 1 < DEPTH && x + 1 < WIDTH && y + 1 < HEIGHT) - img2Write[id + WIDTH + 1 + WIDTH * HEIGHT] = true; - if (z + 1 < DEPTH && x - 1 >= 0 && y + 1 < HEIGHT) - img2Write[id + WIDTH - 1 + WIDTH * HEIGHT] = true; - if (z - 1 >= 0 && x + 1 < WIDTH && y - 1 >= 0) - img2Write[id - WIDTH + 1 - WIDTH * HEIGHT] = true; - if (z - 1 >= 0 && x - 1 >= 0 && y - 1 >= 0) - img2Write[id - WIDTH - 1 - WIDTH * HEIGHT] = true; - if (z - 1 >= 0 && x + 1 < WIDTH && y + 1 < HEIGHT) - img2Write[id + WIDTH + 1 - WIDTH * HEIGHT] = true; - if (z - 1 >= 0 && x - 1 >= 0 && y + 1 < HEIGHT) - img2Write[id + WIDTH - 1 - WIDTH * HEIGHT] = true; -#endif -#endif - } - - // this is an atomic and computed to the finished global variable, if image 1 contains all of image 2 and image 2 - // contains all pixels of image 1 then finished is true - atomicAnd(&finished, (img2Read[id] || !IMG1[id]) && (img1Read[id] || !IMG2[id])); - } -} - -#pragma once -int HausdorffDistance::computeDistance(Volume* img1, Volume* img2, bool* d_img1, bool* d_img2) { - const int height = (*img1).getHeight(), width = (*img1).getWidth(), depth = (*img1).getDepth(); - - size_t size = width * height * depth * sizeof(bool); - - // getting details of your CUDA device - cudaDeviceProp props; - cudaGetDeviceProperties( - &props, CUDA_DEVICE_INDEX); // device index = 0, you can change it if you have more CUDA devices - const int threadsPerBlock = props.maxThreadsPerBlock / 2; - const int blocksPerGrid = (height * width * depth + threadsPerBlock - 1) / threadsPerBlock; - - // copying the dimensions to the GPU - cudaMemcpyToSymbolAsync(WIDTH, &width, sizeof(width), 0); - cudaMemcpyToSymbolAsync(HEIGHT, &height, sizeof(height), 0); - cudaMemcpyToSymbolAsync(DEPTH, &depth, sizeof(depth), 0); - - // allocating the input images on the GPU - - // allocating the images that will be the processing ones - bool *d_img1Write, *d_img1Read, *d_img2Write, *d_img2Read; - cudaMalloc(&d_img1Write, size); - cudaMalloc(&d_img1Read, size); - cudaMalloc(&d_img2Write, size); - cudaMalloc(&d_img2Read, size); - - // cloning the input images to these two image versions (write and read) - cudaMemcpyAsync(d_img1Read, d_img1, size, cudaMemcpyDeviceToDevice); - cudaMemcpyAsync(d_img2Read, d_img2, size, cudaMemcpyDeviceToDevice); - cudaMemcpyAsync(d_img1Write, d_img1, size, cudaMemcpyDeviceToDevice); - cudaMemcpyAsync(d_img2Write, d_img2, size, cudaMemcpyDeviceToDevice); - - // required variables to compute the distance - int h_finished = false, t = true; - int distance = -1; - - // where the magic happens - while (!h_finished) { - // reset the bool variable that verifies if the processing ended - cudaMemcpyToSymbol(finished, &t, sizeof(h_finished)); - - // lauching the verify kernel, which verifies if the processing finished - dilate<<>>(d_img1, d_img2, d_img1Read, d_img2Read, d_img1Write, d_img2Write); - - // cudaDeviceSynchronize(); - - // updating the imgRead (cloning imgWrite to imgRead) - cudaMemcpy(d_img1Read, d_img1Write, size, cudaMemcpyDeviceToDevice); - cudaMemcpy(d_img2Read, d_img2Write, size, cudaMemcpyDeviceToDevice); - - // copying the result back to host memory - cudaMemcpyFromSymbol(&h_finished, finished, sizeof(h_finished)); - - // incrementing the distance at each iteration - distance++; - } - - // freeing memory - cudaFree(d_img1); - cudaFree(d_img2); - cudaFree(d_img1Write); - cudaFree(d_img1Read); - cudaFree(d_img2Write); - cudaFree(d_img2Read); - - // resetting device - - return distance; -} -#pragma once -inline void HausdorffDistance::print(cudaError_t error, char* msg) { - if (error != cudaSuccess) { - printf("Error on %s ", msg); - fprintf(stderr, "Error code: %s!\n", cudaGetErrorString(error)); - exit(EXIT_FAILURE); - } -} - -/* -benchmark for original code from https://github.com/Oyatsumi/HausdorffDistanceComparison -*/ -std::tuple benchmarkOlivieraCUDA( - torch::Tensor goldStandardA, - torch::Tensor algoOutputA, - int WIDTH, - int HEIGHT, - int DEPTH) { - // just originally it started for cpu so ... - - int lenn = WIDTH * HEIGHT * DEPTH; - size_t sizee = sizeof(bool) * lenn; - - bool* goldStandard = (bool*)calloc(lenn, sizeof(bool)); - bool* algoOutput = (bool*)calloc(lenn, sizeof(bool)); - - cudaMemcpy(goldStandard, goldStandardA.data_ptr(), sizee, cudaMemcpyDeviceToHost); - cudaMemcpy(algoOutput, algoOutputA.data_ptr(), sizee, cudaMemcpyDeviceToHost); - - - - Volume img1 = Volume(WIDTH, HEIGHT, DEPTH), img2 = Volume(WIDTH, HEIGHT, DEPTH); - - for (int x = 0; x < WIDTH; x++) { - for (int y = 0; y < HEIGHT; y++) { - for (int z = 0; z < DEPTH; z++) { - img1.setVoxelValue(algoOutput[x + y * WIDTH + z * WIDTH * HEIGHT], x, y, z); - img2.setVoxelValue(goldStandard[x + y * WIDTH + z * WIDTH * HEIGHT], x, y, z); - } - } - } - - size_t size = WIDTH * HEIGHT * DEPTH * sizeof(bool); - - bool *d_img1, *d_img2; - cudaMalloc(&d_img1, size); - cudaMalloc(&d_img2, size); - - // copying the data to the allocated memory on the GPU - cudaMemcpyAsync(d_img1, (img1).getVolume(), size, cudaMemcpyHostToDevice); - cudaMemcpyAsync(d_img2, (img2).getVolume(), size, cudaMemcpyHostToDevice); - - auto begin = std::chrono::high_resolution_clock::now(); - HausdorffDistance* hd = new HausdorffDistance(); - - cudaDeviceSynchronize(); - - int dist = (*hd).computeDistance(&img1, &img2, d_img1, d_img2); - cudaDeviceSynchronize(); - - auto end = std::chrono::high_resolution_clock::now(); - - double time = - (double)(std::chrono::duration_cast(end - begin).count() / (double)1000000000); - - // freeing memory - img1.dispose(); - img2.dispose(); - free(goldStandard); - free(algoOutput); - - return {dist, time}; -} +// return {dist, time}; +// } diff --git a/tests/test_hausdorff_distance_morphologic.py b/tests/test_hausdorff_distance_morphologic.py index 3aa96cdd8bd..242b596f726 100644 --- a/tests/test_hausdorff_distance_morphologic.py +++ b/tests/test_hausdorff_distance_morphologic.py @@ -21,66 +21,66 @@ device = torch.device("cuda") -dimA = 50 +dim_a = 50 -dimAA = 170 -dimBB = 190 -dimCC = 200 +dim_aa = 170 +dim_bb = 190 +dim_cc = 200 # testing single points diffrent dims # dim1 compare_values = torch.ones(1) -a = torch.zeros(dimA, dimA, dimA) -b = torch.zeros(dimA, dimA, dimA) +a = torch.zeros(dim_a, dim_a, dim_a) +b = torch.zeros(dim_a, dim_a, dim_a) a[0, 0, 0] = 1 b[10, 0, 0] = 1 # dim2 -a1 = torch.zeros(dimAA, dimBB, dimCC) -b1 = torch.zeros(dimAA, dimBB, dimCC) +a1 = torch.zeros(dim_aa, dim_bb, dim_cc) +b1 = torch.zeros(dim_aa, dim_bb, dim_cc) a1[0, 0, 0] = 1 b1[0, 15, 0] = 1 # dim3 -a2 = torch.zeros(dimAA, dimBB, dimCC) -b2 = torch.zeros(dimAA, dimBB, dimCC) +a2 = torch.zeros(dim_aa, dim_bb, dim_cc) +b2 = torch.zeros(dim_aa, dim_bb, dim_cc) a2[0, 0, 10] = 1 b2[0, 0, 150] = 1 # testing whole llines and compare_values set to 2 compare_values_b = torch.ones(1) compare_values_b[0] = 2 -a3 = torch.zeros(dimAA, dimBB, dimCC) -b3 = torch.zeros(dimAA, dimBB, dimCC) +a3 = torch.zeros(dim_aa, dim_bb, dim_cc) +b3 = torch.zeros(dim_aa, dim_bb, dim_cc) a3[:, 0, 10] = 2 b3[:, 0, 150] = 2 -a4 = torch.zeros(dimAA, dimBB, dimCC) -b4 = torch.zeros(dimAA, dimBB, dimCC) +a4 = torch.zeros(dim_aa, dim_bb, dim_cc) +b4 = torch.zeros(dim_aa, dim_bb, dim_cc) a4[10, 0, :] = 2 b4[120, 0, :] = 2 -a5 = torch.zeros(dimAA, dimBB, dimCC) -b5 = torch.zeros(dimAA, dimBB, dimCC) +a5 = torch.zeros(dim_aa, dim_bb, dim_cc) +b5 = torch.zeros(dim_aa, dim_bb, dim_cc) a5[10, :, 0] = 2 b5[120, :, 0] = 2 # testing whole planes -a6 = torch.zeros(dimAA, dimBB, dimCC) -b6 = torch.zeros(dimAA, dimBB, dimCC) +a6 = torch.zeros(dim_aa, dim_bb, dim_cc) +b6 = torch.zeros(dim_aa, dim_bb, dim_cc) a6[10, :, :] = 2 b6[120, :, :] = 2 -a7 = torch.zeros(dimAA, dimBB, dimCC) -b7 = torch.zeros(dimAA, dimBB, dimCC) +a7 = torch.zeros(dim_aa, dim_bb, dim_cc) +b7 = torch.zeros(dim_aa, dim_bb, dim_cc) a7[:, 0, :] = 2 b7[:, 110, :] = 2 -a8 = torch.zeros(dimAA, dimBB, dimCC) -b8 = torch.zeros(dimAA, dimBB, dimCC) +a8 = torch.zeros(dim_aa, dim_bb, dim_cc) +b8 = torch.zeros(dim_aa, dim_bb, dim_cc) # a8[:, :, 20] = 2 # b8[:,:, 130] = 2 @@ -91,8 +91,8 @@ b8[2, 2, 130] = 2 # multi points -a9 = torch.zeros(dimAA, dimBB, dimCC) -b9 = torch.zeros(dimAA, dimBB, dimCC) +a9 = torch.zeros(dim_aa, dim_bb, dim_cc) +b9 = torch.zeros(dim_aa, dim_bb, dim_cc) a9[0, 20, 0] = 2 a9[0, 0, 30] = 2 From 79b44d958ab66b221955fad13c9be0bcb1106467 Mon Sep 17 00:00:00 2001 From: Jakub Mitura Date: Sun, 1 May 2022 07:19:11 +0200 Subject: [PATCH 35/41] fixed imports and commented line Signed-off-by: Jakub Mitura --- monai/_extensions/hausdorff/hausdorff_cuda.cu | 5 +---- monai/metrics/morphological_hausdorff_distance.py | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/monai/_extensions/hausdorff/hausdorff_cuda.cu b/monai/_extensions/hausdorff/hausdorff_cuda.cu index fe08413e56f..322bd105a01 100644 --- a/monai/_extensions/hausdorff/hausdorff_cuda.cu +++ b/monai/_extensions/hausdorff/hausdorff_cuda.cu @@ -2448,13 +2448,10 @@ int getHausdorffDistance_CUDA( // cudaMemcpy(goldStandard, goldStandardA.data_ptr(), sizee, cudaMemcpyDeviceToHost); // cudaMemcpy(algoOutput, algoOutputA.data_ptr(), sizee, cudaMemcpyDeviceToHost); -<<<<<<< HEAD // Volume img1 = Volume(WIDTH, HEIGHT, DEPTH), img2 = Volume(WIDTH, HEIGHT, DEPTH); -======= - Volume img1 = Volume(WIDTH, HEIGHT, DEPTH), img2 = Volume(WIDTH, HEIGHT, DEPTH); ->>>>>>> f1eba75311d628ec98c786ebfd5e3c8635818ab2 + //Volume img1 = Volume(WIDTH, HEIGHT, DEPTH), img2 = Volume(WIDTH, HEIGHT, DEPTH); // for (int x = 0; x < WIDTH; x++) { // for (int y = 0; y < HEIGHT; y++) { diff --git a/monai/metrics/morphological_hausdorff_distance.py b/monai/metrics/morphological_hausdorff_distance.py index e9fab0662c1..6c81ea18361 100644 --- a/monai/metrics/morphological_hausdorff_distance.py +++ b/monai/metrics/morphological_hausdorff_distance.py @@ -9,7 +9,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import Any, Optional +from typing import Optional import torch From f6aa3891c9d04deda050ca74da8c3d2ef02083b3 Mon Sep 17 00:00:00 2001 From: Jakub Mitura Date: Sun, 1 May 2022 20:25:23 +0200 Subject: [PATCH 36/41] cleaning Signed-off-by: Jakub Mitura --- monai/_extensions/hausdorff/hausdorff.cpp | 23 +- monai/_extensions/hausdorff/hausdorff_cuda.cu | 418 ------------------ .../morphological_hausdorff_distance.py | 2 +- 3 files changed, 2 insertions(+), 441 deletions(-) diff --git a/monai/_extensions/hausdorff/hausdorff.cpp b/monai/_extensions/hausdorff/hausdorff.cpp index 6de168a59b3..142606f8ba0 100644 --- a/monai/_extensions/hausdorff/hausdorff.cpp +++ b/monai/_extensions/hausdorff/hausdorff.cpp @@ -23,20 +23,6 @@ int getHausdorffDistance_CUDA( const int zDim, const float robustnessPercent); -// at::Tensor getHausdorffDistance_CUDA_FullResList( -// at::Tensor goldStandard, -// at::Tensor algoOutput, -// int WIDTH, -// int HEIGHT, -// int DEPTH, -// float robustnessPercent); - -// std::tuple benchmarkOlivieraCUDA( -// torch::Tensor goldStandard, -// torch::Tensor algoOutput, -// const int xDim, -// const int yDim, -// const int zDim); // C++ interface @@ -84,12 +70,5 @@ int getHausdorffDistance( PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) { m.def("getHausdorffDistance", &getHausdorffDistance, "Basic version of Hausdorff distance"); - // m.def( - // "benchmarkOlivieraCUDA", - // &benchmarkOlivieraCUDA, - // "Algorithm by Oliviera - just for comparison sake - accept only boolean arrays "); - // m.def( - // "getHausdorffDistance_FullResList", - // &getHausdorffDistance_FullResList, - // " return additionally full result list indicating in which dilatation iterations results were recorded "); } + diff --git a/monai/_extensions/hausdorff/hausdorff_cuda.cu b/monai/_extensions/hausdorff/hausdorff_cuda.cu index 322bd105a01..9749be36ad4 100644 --- a/monai/_extensions/hausdorff/hausdorff_cuda.cu +++ b/monai/_extensions/hausdorff/hausdorff_cuda.cu @@ -2054,42 +2054,6 @@ int getHausdorffDistance_CUDA_Generic( return result; } -// template -// at::Tensor getHausdorffDistance_CUDA_FullResList_local( -// at::Tensor goldStandard, -// at::Tensor algoOutput, -// int WIDTH, -// int HEIGHT, -// int DEPTH, -// float robustnessPercent, -// at::Tensor compare_values) { -// // TODO() use https ://pytorch.org/cppdocs/notes/tensor_cuda_stream.html -// cudaStream_t stream1; -// cudaStreamCreate(&stream1); - -// MetaDataCPU metaData; -// // size_t size = sizeof(unsigned int) * 20; -// // unsigned int* minMaxesCPU = (unsigned int*)malloc(size); -// // metaData.minMaxes = minMaxesCPU; - -// ForFullBoolPrepArgs forFullBoolPrepArgs; -// forFullBoolPrepArgs.metaData = metaData; -// forFullBoolPrepArgs.compare_values = compare_values.item(); -// forFullBoolPrepArgs.goldArr = goldStandard; -// forFullBoolPrepArgs.segmArr = algoOutput; - -// occupancyCalcData occData = getOccupancy(); - -// ForBoolKernelArgs fbArgs = -// executeHausdoff(forFullBoolPrepArgs, WIDTH, HEIGHT, DEPTH, occData, stream1, false, robustnessPercent, true); - -// cudaFreeAsync(fbArgs.minMaxes, stream1); -// // free(metaData.minMaxesCPU); - -// cudaStreamDestroy(stream1); - -// return fbArgs.resultListPointerIterNumbTensor; -// } /* Functions for pybind @@ -2111,385 +2075,3 @@ int getHausdorffDistance_CUDA( })); return res; } - -// at::Tensor getHausdorffDistance_CUDA_FullResList( -// at::Tensor goldStandard, -// at::Tensor algoOutput, -// const int WIDTH, -// const int HEIGHT, -// const int DEPTH, -// const float robustnessPercent, -// at::Tensor compare_values) { -// at::Tensor res; -// AT_DISPATCH_ALL_TYPESWithBool( -// goldStandard.type(), "getHausdorffDistance_CUDA_FullResList", ([&] { -// res = getHausdorffDistance_CUDA_FullResList_local( -// goldStandard, algoOutput, WIDTH, HEIGHT, DEPTH, robustnessPercent, compare_values); -// })); - -// return res; -// } - -/************************************************************* - *Oliviera Algorithm - *************************************************************************************/ - -// typedef unsigned char uchar; -// typedef unsigned int uint; -// #pragma once -// class Volume { -// private: -// bool* volume; -// int width, height, depth; -// int getLinearIndex(int x, int y, int z); - -// public: -// bool getVoxelValue(int x, int y, int z); -// bool getPixelValue(int x, int y); -// uint getWidth(); -// uint getHeight(); -// uint getDepth(); -// bool* getVolume(); -// void setVoxelValue(bool value, int x, int y, int z); -// void setPixelValue(bool value, int x, int y); -// Volume(int width, int height, int depth); -// Volume(int width, int height); -// void dispose(); -// }; - -// #define CUDA_DEVICE_INDEX 0 // setting the index of your CUDA device - -// #define IS_3D \ -// 1 // setting this to 0 would grant a very slightly improvement on the performance if working with images only -// #define CHEBYSHEV \ -// 0 // if not set to 1, then this algorithm would use an Euclidean-like metric, it is just an approximation. -// // It can be changed according to the structuring element -// #pragma once -// class HausdorffDistance { -// private: -// void print(cudaError_t error, char* msg); - -// public: -// int computeDistance(Volume* img1, Volume* img2, bool* d_img1, bool* d_img2); -// }; - -// inline Volume::Volume(const int width, const int height, const int depth) { -// this->width = width; -// this->height = height; -// this->depth = depth; -// volume = (bool*)calloc(width * height * depth, sizeof(bool)); -// } - -// #pragma once -// inline Volume::Volume(const int width, const int height) { -// this->width = width; -// this->height = height; -// this->depth = 1; -// volume = (bool*)calloc(width * height * depth, sizeof(bool)); -// } -// #pragma once -// inline int Volume::getLinearIndex(const int x, const int y, const int z) { -// const int a = 1, b = width, c = (width) * (height); -// return a * x + b * y + c * z; -// } - -// inline uint Volume::getWidth() { -// return this->width; -// } -// inline uint Volume::getHeight() { -// return this->height; -// } -// inline uint Volume::getDepth() { -// return this->depth; -// } -// inline bool* Volume::getVolume() { -// return this->volume; -// } -// inline bool Volume::getPixelValue(int x, int y) { -// return this->volume[getLinearIndex(x, y, 0)]; -// } -// #pragma once -// inline bool Volume::getVoxelValue(int x, int y, int z) { -// return volume[getLinearIndex(x, y, z)]; -// } -// #pragma once -// inline void Volume::setPixelValue(bool value, const int x, const int y) { -// volume[getLinearIndex(x, y, 0)] = value; -// } -// #pragma once -// inline void Volume::setVoxelValue(bool value, const int x, const int y, const int z) { -// volume[getLinearIndex(x, y, z)] = value; -// } -// #pragma once -// inline void Volume::dispose() { -// free(volume); -// } - -// typedef unsigned char uchar; -// typedef unsigned int uint; - -// #pragma once -// __device__ int finished; // global variable that contains a boolean which indicates when to stop the kernel processing -// #pragma once -// __constant__ __device__ int WIDTH, HEIGHT, DEPTH; // constant variables that contain the size of the volume - -// #pragma once -// __global__ void dilate( -// const bool* IMG1, -// const bool* IMG2, -// const bool* img1Read, -// const bool* img2Read, -// bool* img1Write, -// bool* img2Write) { -// const int id = blockDim.x * blockIdx.x + threadIdx.x; -// #if !IS_3D -// const int x = id % WIDTH, y = id / WIDTH; -// #else -// const int x = id % WIDTH, y = (id / WIDTH) % HEIGHT, z = (id / WIDTH) / HEIGHT; -// #endif - -// if (id < WIDTH * HEIGHT * DEPTH) { -// if (img1Read[id]) { -// if (x + 1 < WIDTH) -// img1Write[id + 1] = true; -// if (x - 1 >= 0) -// img1Write[id - 1] = true; -// if (y + 1 < HEIGHT) -// img1Write[id + WIDTH] = true; -// if (y - 1 >= 0) -// img1Write[id - WIDTH] = true; -// #if IS_3D // if working with 3d volumes, then the 3D part -// if (z + 1 < DEPTH) -// img1Write[id + WIDTH * HEIGHT] = true; -// if (z - 1 >= 0) -// img1Write[id - WIDTH * HEIGHT] = true; -// #endif - -// #if CHEBYSHEV -// // diagonals -// if (x + 1 < WIDTH && y - 1 >= 0) -// img1Write[id - WIDTH + 1] = true; -// if (x - 1 >= 0 && y - 1 >= 0) -// img1Write[id - WIDTH - 1] = true; -// if (x + 1 < WIDTH && y + 1 < HEIGHT) -// img1Write[id + WIDTH + 1] = true; -// if (x - 1 >= 0 && y + 1 < HEIGHT) -// img1Write[id + WIDTH - 1] = true; -// #if IS_3D // if working with 3d volumes, then the 3D part -// if (z + 1 < DEPTH && x + 1 < WIDTH && y - 1 >= 0) -// img1Write[id - WIDTH + 1 + WIDTH * HEIGHT] = true; -// if (z + 1 < DEPTH && x - 1 >= 0 && y - 1 >= 0) -// img1Write[id - WIDTH - 1 + WIDTH * HEIGHT] = true; -// if (z + 1 < DEPTH && x + 1 < WIDTH && y + 1 < HEIGHT) -// img1Write[id + WIDTH + 1 + WIDTH * HEIGHT] = true; -// if (z + 1 < DEPTH && x - 1 >= 0 && y + 1 < HEIGHT) -// img1Write[id + WIDTH - 1 + WIDTH * HEIGHT] = true; -// if (z - 1 >= 0 && x + 1 < WIDTH && y - 1 >= 0) -// img1Write[id - WIDTH + 1 - WIDTH * HEIGHT] = true; -// if (z - 1 >= 0 && x - 1 >= 0 && y - 1 >= 0) -// img1Write[id - WIDTH - 1 - WIDTH * HEIGHT] = true; -// if (z - 1 >= 0 && x + 1 < WIDTH && y + 1 < HEIGHT) -// img1Write[id + WIDTH + 1 - WIDTH * HEIGHT] = true; -// if (z - 1 >= 0 && x - 1 >= 0 && y + 1 < HEIGHT) -// img1Write[id + WIDTH - 1 - WIDTH * HEIGHT] = true; -// #endif -// #endif -// } - -// if (img2Read[id]) { -// if (x + 1 < WIDTH) -// img2Write[id + 1] = true; -// if (x - 1 >= 0) -// img2Write[id - 1] = true; -// if (y + 1 < HEIGHT) -// img2Write[id + WIDTH] = true; -// if (y - 1 >= 0) -// img2Write[id - WIDTH] = true; -// #if IS_3D // if working with 3d volumes, then the 3D part -// if (z + 1 < DEPTH) -// img2Write[id + WIDTH * HEIGHT] = true; -// if (z - 1 >= 0) -// img2Write[id - WIDTH * HEIGHT] = true; -// #endif - -// #if CHEBYSHEV -// // diagonals -// if (x + 1 < WIDTH && y - 1 >= 0) -// img2Write[id - WIDTH + 1] = true; -// if (x - 1 >= 0 && y - 1 >= 0) -// img2Write[id - WIDTH - 1] = true; -// if (x + 1 < WIDTH && y + 1 < HEIGHT) -// img2Write[id + WIDTH + 1] = true; -// if (x - 1 >= 0 && y + 1 < HEIGHT) -// img2Write[id + WIDTH - 1] = true; -// #if IS_3D // if working with 3d volumes, then the 3D part -// if (z + 1 < DEPTH && x + 1 < WIDTH && y - 1 >= 0) -// img2Write[id - WIDTH + 1 + WIDTH * HEIGHT] = true; -// if (z + 1 < DEPTH && x - 1 >= 0 && y - 1 >= 0) -// img2Write[id - WIDTH - 1 + WIDTH * HEIGHT] = true; -// if (z + 1 < DEPTH && x + 1 < WIDTH && y + 1 < HEIGHT) -// img2Write[id + WIDTH + 1 + WIDTH * HEIGHT] = true; -// if (z + 1 < DEPTH && x - 1 >= 0 && y + 1 < HEIGHT) -// img2Write[id + WIDTH - 1 + WIDTH * HEIGHT] = true; -// if (z - 1 >= 0 && x + 1 < WIDTH && y - 1 >= 0) -// img2Write[id - WIDTH + 1 - WIDTH * HEIGHT] = true; -// if (z - 1 >= 0 && x - 1 >= 0 && y - 1 >= 0) -// img2Write[id - WIDTH - 1 - WIDTH * HEIGHT] = true; -// if (z - 1 >= 0 && x + 1 < WIDTH && y + 1 < HEIGHT) -// img2Write[id + WIDTH + 1 - WIDTH * HEIGHT] = true; -// if (z - 1 >= 0 && x - 1 >= 0 && y + 1 < HEIGHT) -// img2Write[id + WIDTH - 1 - WIDTH * HEIGHT] = true; -// #endif -// #endif -// } - -// // this is an atomic and computed to the finished global variable, if image 1 contains all of image 2 and image 2 -// // contains all pixels of image 1 then finished is true -// atomicAnd(&finished, (img2Read[id] || !IMG1[id]) && (img1Read[id] || !IMG2[id])); -// } -// } - -// #pragma once -// int HausdorffDistance::computeDistance(Volume* img1, Volume* img2, bool* d_img1, bool* d_img2) { -// const int height = (*img1).getHeight(), width = (*img1).getWidth(), depth = (*img1).getDepth(); - -// size_t size = width * height * depth * sizeof(bool); - -// // getting details of your CUDA device -// cudaDeviceProp props; -// cudaGetDeviceProperties( -// &props, CUDA_DEVICE_INDEX); // device index = 0, you can change it if you have more CUDA devices -// const int threadsPerBlock = props.maxThreadsPerBlock / 2; -// const int blocksPerGrid = (height * width * depth + threadsPerBlock - 1) / threadsPerBlock; - -// // copying the dimensions to the GPU -// cudaMemcpyToSymbolAsync(WIDTH, &width, sizeof(width), 0); -// cudaMemcpyToSymbolAsync(HEIGHT, &height, sizeof(height), 0); -// cudaMemcpyToSymbolAsync(DEPTH, &depth, sizeof(depth), 0); - -// // allocating the input images on the GPU - -// // allocating the images that will be the processing ones -// bool *d_img1Write, *d_img1Read, *d_img2Write, *d_img2Read; -// cudaMalloc(&d_img1Write, size); -// cudaMalloc(&d_img1Read, size); -// cudaMalloc(&d_img2Write, size); -// cudaMalloc(&d_img2Read, size); - -// // cloning the input images to these two image versions (write and read) -// cudaMemcpyAsync(d_img1Read, d_img1, size, cudaMemcpyDeviceToDevice); -// cudaMemcpyAsync(d_img2Read, d_img2, size, cudaMemcpyDeviceToDevice); -// cudaMemcpyAsync(d_img1Write, d_img1, size, cudaMemcpyDeviceToDevice); -// cudaMemcpyAsync(d_img2Write, d_img2, size, cudaMemcpyDeviceToDevice); - -// // required variables to compute the distance -// int h_finished = false, t = true; -// int distance = -1; - -// // where the magic happens -// while (!h_finished) { -// // reset the bool variable that verifies if the processing ended -// cudaMemcpyToSymbol(finished, &t, sizeof(h_finished)); - -// // lauching the verify kernel, which verifies if the processing finished -// dilate<<>>(d_img1, d_img2, d_img1Read, d_img2Read, d_img1Write, d_img2Write); - -// // cudaDeviceSynchronize(); - -// // updating the imgRead (cloning imgWrite to imgRead) -// cudaMemcpy(d_img1Read, d_img1Write, size, cudaMemcpyDeviceToDevice); -// cudaMemcpy(d_img2Read, d_img2Write, size, cudaMemcpyDeviceToDevice); - -// // copying the result back to host memory -// cudaMemcpyFromSymbol(&h_finished, finished, sizeof(h_finished)); - -// // incrementing the distance at each iteration -// distance++; -// } - -// // freeing memory -// cudaFree(d_img1); -// cudaFree(d_img2); -// cudaFree(d_img1Write); -// cudaFree(d_img1Read); -// cudaFree(d_img2Write); -// cudaFree(d_img2Read); - -// // resetting device - -// return distance; -// } -// #pragma once -// inline void HausdorffDistance::print(cudaError_t error, char* msg) { -// if (error != cudaSuccess) { -// printf("Error on %s ", msg); -// fprintf(stderr, "Error code: %s!\n", cudaGetErrorString(error)); -// exit(EXIT_FAILURE); -// } -// } - -// /* -// benchmark for original code from https://github.com/Oyatsumi/HausdorffDistanceComparison -// */ -// std::tuple benchmarkOlivieraCUDA( -// torch::Tensor goldStandardA, -// torch::Tensor algoOutputA, -// int WIDTH, -// int HEIGHT, -// int DEPTH) { -// // just originally it started for cpu so ... - -// int lenn = WIDTH * HEIGHT * DEPTH; -// size_t sizee = sizeof(bool) * lenn; - -// bool* goldStandard = (bool*)calloc(lenn, sizeof(bool)); -// bool* algoOutput = (bool*)calloc(lenn, sizeof(bool)); - -// cudaMemcpy(goldStandard, goldStandardA.data_ptr(), sizee, cudaMemcpyDeviceToHost); -// cudaMemcpy(algoOutput, algoOutputA.data_ptr(), sizee, cudaMemcpyDeviceToHost); - - - -// Volume img1 = Volume(WIDTH, HEIGHT, DEPTH), img2 = Volume(WIDTH, HEIGHT, DEPTH); - //Volume img1 = Volume(WIDTH, HEIGHT, DEPTH), img2 = Volume(WIDTH, HEIGHT, DEPTH); - -// for (int x = 0; x < WIDTH; x++) { -// for (int y = 0; y < HEIGHT; y++) { -// for (int z = 0; z < DEPTH; z++) { -// img1.setVoxelValue(algoOutput[x + y * WIDTH + z * WIDTH * HEIGHT], x, y, z); -// img2.setVoxelValue(goldStandard[x + y * WIDTH + z * WIDTH * HEIGHT], x, y, z); -// } -// } -// } - -// size_t size = WIDTH * HEIGHT * DEPTH * sizeof(bool); - -// bool *d_img1, *d_img2; -// cudaMalloc(&d_img1, size); -// cudaMalloc(&d_img2, size); - -// // copying the data to the allocated memory on the GPU -// cudaMemcpyAsync(d_img1, (img1).getVolume(), size, cudaMemcpyHostToDevice); -// cudaMemcpyAsync(d_img2, (img2).getVolume(), size, cudaMemcpyHostToDevice); - -// auto begin = std::chrono::high_resolution_clock::now(); -// HausdorffDistance* hd = new HausdorffDistance(); - -// cudaDeviceSynchronize(); - -// int dist = (*hd).computeDistance(&img1, &img2, d_img1, d_img2); -// cudaDeviceSynchronize(); - -// auto end = std::chrono::high_resolution_clock::now(); - -// double time = -// (double)(std::chrono::duration_cast(end - begin).count() / (double)1000000000); - -// // freeing memory -// img1.dispose(); -// img2.dispose(); -// free(goldStandard); -// free(algoOutput); - -// return {dist, time}; -// } diff --git a/monai/metrics/morphological_hausdorff_distance.py b/monai/metrics/morphological_hausdorff_distance.py index 6c81ea18361..ad60fe248d1 100644 --- a/monai/metrics/morphological_hausdorff_distance.py +++ b/monai/metrics/morphological_hausdorff_distance.py @@ -101,7 +101,7 @@ def _compute_tensor(self, y_pred: torch.Tensor, y: Optional[torch.Tensor] = None """ if y.shape != y_pred.shape: - raise ValueError(f"y_pred and y should have same shapes, got {y_pred.shape} and {y.shape}.") + raise ValueError(f"y_pred and y should have same shapes") sizz = y.shape if self.to_invert_dims: return self.compiled_extension.getHausdorffDistance( From 02b7a80bac81b0e65c35098bd0a00bf41a464342 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 1 May 2022 18:25:54 +0000 Subject: [PATCH 37/41] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- monai/_extensions/hausdorff/hausdorff.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/monai/_extensions/hausdorff/hausdorff.cpp b/monai/_extensions/hausdorff/hausdorff.cpp index 142606f8ba0..fd2b41254c0 100644 --- a/monai/_extensions/hausdorff/hausdorff.cpp +++ b/monai/_extensions/hausdorff/hausdorff.cpp @@ -71,4 +71,3 @@ int getHausdorffDistance( PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) { m.def("getHausdorffDistance", &getHausdorffDistance, "Basic version of Hausdorff distance"); } - From 1dc76d1d93e91b9d1d77b690cdcac7afda3c0055 Mon Sep 17 00:00:00 2001 From: Jakub Mitura Date: Tue, 3 May 2022 17:22:47 +0200 Subject: [PATCH 38/41] debugging Signed-off-by: Jakub Mitura --- monai/_extensions/hausdorff/hausdorff.cpp | 24 ------------------- .../morphological_hausdorff_distance.py | 2 +- 2 files changed, 1 insertion(+), 25 deletions(-) diff --git a/monai/_extensions/hausdorff/hausdorff.cpp b/monai/_extensions/hausdorff/hausdorff.cpp index 142606f8ba0..1195be42a88 100644 --- a/monai/_extensions/hausdorff/hausdorff.cpp +++ b/monai/_extensions/hausdorff/hausdorff.cpp @@ -24,9 +24,6 @@ int getHausdorffDistance_CUDA( const float robustnessPercent); -// C++ interface - -// NOTE: AT_ASSERT has become AT_CHECK on master after 0.4. #define CHECK_CUDA(x) AT_ASSERTM(x.type().is_cuda(), #x " must be a CUDA tensor") #define CHECK_CONTIGUOUS(x) AT_ASSERTM(x.is_contiguous(), #x " must be contiguous") #define CHECK_INPUT(x) \ @@ -46,27 +43,6 @@ int getHausdorffDistance( return getHausdorffDistance_CUDA(goldStandard, algoOutput, xDim, yDim, zDim, robustnessPercent); } -// at::Tensor getHausdorffDistance_FullResList( -// torch::Tensor goldStandard, -// torch::Tensor algoOutput, -// const int xDim, -// const int yDim, -// const int zDim, -// const float robustnessPercent = 1.0) { -// CHECK_INPUT(goldStandard); -// CHECK_INPUT(algoOutput); - -// return getHausdorffDistance_CUDA_FullResList(goldStandard, algoOutput, xDim, yDim, zDim, robustnessPercent); -// } - -// std::tuple benchmarkOlivieraCUDAOnlyBool( -// torch::Tensor goldStandard, -// torch::Tensor algoOutput, -// const int xDim, -// const int yDim, -// const int zDim) { -// return benchmarkOlivieraCUDA(goldStandard, algoOutput, xDim, yDim, zDim); -// } PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) { m.def("getHausdorffDistance", &getHausdorffDistance, "Basic version of Hausdorff distance"); diff --git a/monai/metrics/morphological_hausdorff_distance.py b/monai/metrics/morphological_hausdorff_distance.py index ad60fe248d1..29ba5891d40 100644 --- a/monai/metrics/morphological_hausdorff_distance.py +++ b/monai/metrics/morphological_hausdorff_distance.py @@ -101,7 +101,7 @@ def _compute_tensor(self, y_pred: torch.Tensor, y: Optional[torch.Tensor] = None """ if y.shape != y_pred.shape: - raise ValueError(f"y_pred and y should have same shapes") + raise ValueError("y_pred and y should have same shapes") sizz = y.shape if self.to_invert_dims: return self.compiled_extension.getHausdorffDistance( From 4f064ce9bb3dc675139936ba3bfc894add308a63 Mon Sep 17 00:00:00 2001 From: Jakub Mitura Date: Tue, 3 May 2022 18:35:45 +0200 Subject: [PATCH 39/41] working with lint Signed-off-by: Jakub Mitura --- monai/metrics/morphological_hausdorff_distance.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/monai/metrics/morphological_hausdorff_distance.py b/monai/metrics/morphological_hausdorff_distance.py index 29ba5891d40..b8b4dbfbe88 100644 --- a/monai/metrics/morphological_hausdorff_distance.py +++ b/monai/metrics/morphological_hausdorff_distance.py @@ -99,10 +99,11 @@ def _compute_tensor(self, y_pred: torch.Tensor, y: Optional[torch.Tensor] = None Dimensionality needs to be identical as in y_pred """ - + sizz=0 if y.shape != y_pred.shape: raise ValueError("y_pred and y should have same shapes") - sizz = y.shape + else: + sizz = y.shape if self.to_invert_dims: return self.compiled_extension.getHausdorffDistance( y_pred, y, sizz[2], sizz[1], sizz[0], self.percent, self.compare_values From cb288edb43c9d66264a76b25eb73ebb4588f1536 Mon Sep 17 00:00:00 2001 From: Jakub Mitura Date: Wed, 4 May 2022 17:09:22 +0200 Subject: [PATCH 40/41] added C++ header Signed-off-by: Jakub Mitura --- monai/_extensions/hausdorff/hausdorff.cpp | 9 +------ .../{hausdorff_cuda.cu => hausdorff.cu} | 0 monai/_extensions/hausdorff/hausdorff.h | 25 +++++++++++++++++++ 3 files changed, 26 insertions(+), 8 deletions(-) rename monai/_extensions/hausdorff/{hausdorff_cuda.cu => hausdorff.cu} (100%) create mode 100644 monai/_extensions/hausdorff/hausdorff.h diff --git a/monai/_extensions/hausdorff/hausdorff.cpp b/monai/_extensions/hausdorff/hausdorff.cpp index 476c8710ad8..18d6de6d515 100644 --- a/monai/_extensions/hausdorff/hausdorff.cpp +++ b/monai/_extensions/hausdorff/hausdorff.cpp @@ -12,16 +12,9 @@ #include #include +#include "hausdorff.h" -// CUDA declarations -int getHausdorffDistance_CUDA( - torch::Tensor goldStandard, - torch::Tensor algoOutput, - const int xDim, - const int yDim, - const int zDim, - const float robustnessPercent); #define CHECK_CUDA(x) AT_ASSERTM(x.type().is_cuda(), #x " must be a CUDA tensor") diff --git a/monai/_extensions/hausdorff/hausdorff_cuda.cu b/monai/_extensions/hausdorff/hausdorff.cu similarity index 100% rename from monai/_extensions/hausdorff/hausdorff_cuda.cu rename to monai/_extensions/hausdorff/hausdorff.cu diff --git a/monai/_extensions/hausdorff/hausdorff.h b/monai/_extensions/hausdorff/hausdorff.h new file mode 100644 index 00000000000..6fcdf1f3311 --- /dev/null +++ b/monai/_extensions/hausdorff/hausdorff.h @@ -0,0 +1,25 @@ +/* +Copyright (c) MONAI Consortium +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +#include + + +// CUDA declarations + +int getHausdorffDistance_CUDA( + torch::Tensor goldStandard, + torch::Tensor algoOutput, + const int xDim, + const int yDim, + const int zDim, + const float robustnessPercent); \ No newline at end of file From b265c5ec480a69777b286b5cb8a90ca803c42be1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 4 May 2022 15:10:04 +0000 Subject: [PATCH 41/41] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- monai/_extensions/hausdorff/hausdorff.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monai/_extensions/hausdorff/hausdorff.h b/monai/_extensions/hausdorff/hausdorff.h index 6fcdf1f3311..8456660fa33 100644 --- a/monai/_extensions/hausdorff/hausdorff.h +++ b/monai/_extensions/hausdorff/hausdorff.h @@ -22,4 +22,4 @@ int getHausdorffDistance_CUDA( const int xDim, const int yDim, const int zDim, - const float robustnessPercent); \ No newline at end of file + const float robustnessPercent);