From be2da43a560a7241c56e727fb481f1389e9f7fdf Mon Sep 17 00:00:00 2001 From: Willem Jan Palenstijn Date: Mon, 22 Nov 2021 14:43:07 +0100 Subject: De-duplicate 2D texture object creation --- cuda/2d/fan_bp.cu | 51 ++++++++++++------------------------- cuda/2d/fan_fp.cu | 33 ++---------------------- cuda/2d/par_bp.cu | 37 +++++---------------------- cuda/2d/par_fp.cu | 39 ++-------------------------- cuda/2d/util.cu | 60 ++++++++++++++++++++++++++++++++++++++++++++ include/astra/cuda/2d/util.h | 4 +++ 6 files changed, 90 insertions(+), 134 deletions(-) diff --git a/cuda/2d/fan_bp.cu b/cuda/2d/fan_bp.cu index ea2d23b..bcda464 100644 --- a/cuda/2d/fan_bp.cu +++ b/cuda/2d/fan_bp.cu @@ -57,34 +57,6 @@ struct DevFanParams { __constant__ DevFanParams gC_C[g_MaxAngles]; - -static bool bindProjDataTexture(float* data, cudaTextureObject_t& texObj, unsigned int pitch, unsigned int width, unsigned int height, cudaTextureAddressMode mode = cudaAddressModeBorder) -{ - cudaChannelFormatDesc channelDesc = - cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKindFloat); - - cudaResourceDesc resDesc; - memset(&resDesc, 0, sizeof(resDesc)); - resDesc.resType = cudaResourceTypePitch2D; - resDesc.res.pitch2D.devPtr = (void*)data; - resDesc.res.pitch2D.desc = channelDesc; - resDesc.res.pitch2D.width = width; - resDesc.res.pitch2D.height = height; - resDesc.res.pitch2D.pitchInBytes = sizeof(float)*pitch; - - cudaTextureDesc texDesc; - memset(&texDesc, 0, sizeof(texDesc)); - texDesc.addressMode[0] = mode; - texDesc.addressMode[1] = mode; - texDesc.filterMode = cudaFilterModeLinear; - texDesc.readMode = cudaReadModeElementType; - texDesc.normalizedCoords = 0; - - texObj = 0; - - return checkCuda(cudaCreateTextureObject(&texObj, &resDesc, &texDesc, NULL), "fan_bp texture"); -} - template __global__ void devFanBP(float* D_volData, unsigned int volPitch, cudaTextureObject_t tex, unsigned int startAngle, const SDimensions dims, float fOutputScale) { @@ -315,11 +287,14 @@ bool FanBP_internal(float* D_volumeData, unsigned int volumePitch, assert(dims.iProjAngles <= g_MaxAngles); cudaTextureObject_t D_texObj; - bindProjDataTexture(D_projData, D_texObj, projPitch, dims.iProjDets, dims.iProjAngles); + if (!createTextureObjectPitch2D(D_projData, D_texObj, projPitch, dims.iProjDets, dims.iProjAngles)) + return false; bool ok = transferConstants(angles, dims.iProjAngles, false); - if (!ok) + if (!ok) { + cudaDestroyTextureObject(D_texObj); return false; + } dim3 dimBlock(g_blockSlices, g_blockSliceSize); dim3 dimGrid((dims.iVolWidth+g_blockSlices-1)/g_blockSlices, @@ -352,11 +327,14 @@ bool FanBP_FBPWeighted_internal(float* D_volumeData, unsigned int volumePitch, assert(dims.iProjAngles <= g_MaxAngles); cudaTextureObject_t D_texObj; - bindProjDataTexture(D_projData, D_texObj, projPitch, dims.iProjDets, dims.iProjAngles); + if (!createTextureObjectPitch2D(D_projData, D_texObj, projPitch, dims.iProjDets, dims.iProjAngles)) + return false; bool ok = transferConstants(angles, dims.iProjAngles, true); - if (!ok) + if (!ok) { + cudaDestroyTextureObject(D_texObj); return false; + } dim3 dimBlock(g_blockSlices, g_blockSliceSize); dim3 dimGrid((dims.iVolWidth+g_blockSlices-1)/g_blockSlices, @@ -387,11 +365,14 @@ bool FanBP_SART(float* D_volumeData, unsigned int volumePitch, { // only one angle cudaTextureObject_t D_texObj; - bindProjDataTexture(D_projData, D_texObj, projPitch, dims.iProjDets, 1, cudaAddressModeClamp); + if (!createTextureObjectPitch2D(D_projData, D_texObj, projPitch, dims.iProjDets, 1, cudaAddressModeClamp)) + return false; bool ok = transferConstants(angles + angle, 1, false); - if (!ok) + if (!ok) { + cudaDestroyTextureObject(D_texObj); return false; + } dim3 dimBlock(g_blockSlices, g_blockSliceSize); dim3 dimGrid((dims.iVolWidth+g_blockSlices-1)/g_blockSlices, @@ -399,7 +380,7 @@ bool FanBP_SART(float* D_volumeData, unsigned int volumePitch, devFanBP_SART<<>>(D_volumeData, volumePitch, D_texObj, dims, fOutputScale); - bool ok = checkCuda(cudaThreadSynchronize(), "FanBP_SART"); + ok = checkCuda(cudaThreadSynchronize(), "FanBP_SART"); cudaDestroyTextureObject(D_texObj); diff --git a/cuda/2d/fan_fp.cu b/cuda/2d/fan_fp.cu index 0801097..ed99e08 100644 --- a/cuda/2d/fan_fp.cu +++ b/cuda/2d/fan_fp.cu @@ -49,36 +49,6 @@ static const unsigned int g_anglesPerBlock = 16; static const unsigned int g_detBlockSize = 32; static const unsigned int g_blockSlices = 64; -static bool bindVolumeDataTexture(float* data, cudaArray*& dataArray, cudaTextureObject_t& texObj, unsigned int pitch, unsigned int width, unsigned int height) -{ - // TODO: For very small sizes (roughly <=512x128) with few angles (<=180) - // not using an array is more efficient. - - cudaChannelFormatDesc channelDesc = - cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKindFloat); - - dataArray = 0; - cudaMallocArray(&dataArray, &channelDesc, width, height); - cudaMemcpy2DToArray(dataArray, 0, 0, data, pitch*sizeof(float), width*sizeof(float), height, cudaMemcpyDeviceToDevice); - - cudaResourceDesc resDesc; - memset(&resDesc, 0, sizeof(resDesc)); - resDesc.resType = cudaResourceTypeArray; - resDesc.res.array.array = dataArray; - - cudaTextureDesc texDesc; - memset(&texDesc, 0, sizeof(texDesc)); - texDesc.addressMode[0] = cudaAddressModeBorder; - texDesc.addressMode[1] = cudaAddressModeBorder; - texDesc.filterMode = cudaFilterModeLinear; - texDesc.readMode = cudaReadModeElementType; - texDesc.normalizedCoords = 0; - - texObj = 0; - - return checkCuda(cudaCreateTextureObject(&texObj, &resDesc, &texDesc, NULL), "fan_fp texture"); -} - // projection for angles that are roughly horizontal // (detector roughly vertical) __global__ void FanFPhorizontal(float* D_projData, unsigned int projPitch, cudaTextureObject_t tex, unsigned int startSlice, unsigned int startAngle, unsigned int endAngle, const SDimensions dims, float outputScale) @@ -231,7 +201,8 @@ bool FanFP_internal(float* D_volumeData, unsigned int volumePitch, cudaArray* D_dataArray; cudaTextureObject_t D_texObj; - bindVolumeDataTexture(D_volumeData, D_dataArray, D_texObj, volumePitch, dims.iVolWidth, dims.iVolHeight); + if (!createTextureObject2D(D_volumeData, D_dataArray, D_texObj, volumePitch, dims.iVolWidth, dims.iVolHeight)) + return false; // transfer angles to constant memory float* tmp = new float[dims.iProjAngles]; diff --git a/cuda/2d/par_bp.cu b/cuda/2d/par_bp.cu index ee94de8..4066912 100644 --- a/cuda/2d/par_bp.cu +++ b/cuda/2d/par_bp.cu @@ -51,33 +51,6 @@ __constant__ float gC_angle_scaled_cos[g_MaxAngles]; __constant__ float gC_angle_offset[g_MaxAngles]; __constant__ float gC_angle_scale[g_MaxAngles]; -static bool bindProjDataTexture(float* data, cudaTextureObject_t& texObj, unsigned int pitch, unsigned int width, unsigned int height, cudaTextureAddressMode mode = cudaAddressModeBorder) -{ - cudaChannelFormatDesc channelDesc = - cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKindFloat); - - cudaResourceDesc resDesc; - memset(&resDesc, 0, sizeof(resDesc)); - resDesc.resType = cudaResourceTypePitch2D; - resDesc.res.pitch2D.devPtr = (void*)data; - resDesc.res.pitch2D.desc = channelDesc; - resDesc.res.pitch2D.width = width; - resDesc.res.pitch2D.height = height; - resDesc.res.pitch2D.pitchInBytes = sizeof(float)*pitch; - - cudaTextureDesc texDesc; - memset(&texDesc, 0, sizeof(texDesc)); - texDesc.addressMode[0] = mode; - texDesc.addressMode[1] = mode; - texDesc.filterMode = cudaFilterModeLinear; - texDesc.readMode = cudaReadModeElementType; - texDesc.normalizedCoords = 0; - - texObj = 0; - - return checkCuda(cudaCreateTextureObject(&texObj, &resDesc, &texDesc, NULL), "par_bp texture"); -} - // TODO: Templated version with/without scale? (Or only the global outputscale) __global__ void devBP(float* D_volData, unsigned int volPitch, cudaTextureObject_t tex, unsigned int startAngle, const SDimensions dims, float fOutputScale) { @@ -196,14 +169,15 @@ bool BP_internal(float* D_volumeData, unsigned int volumePitch, { assert(dims.iProjAngles <= g_MaxAngles); + cudaTextureObject_t D_texObj; + if (!createTextureObjectPitch2D(D_projData, D_texObj, projPitch, dims.iProjDets, dims.iProjAngles)) + return false; + float* angle_scaled_sin = new float[dims.iProjAngles]; float* angle_scaled_cos = new float[dims.iProjAngles]; float* angle_offset = new float[dims.iProjAngles]; float* angle_scale = new float[dims.iProjAngles]; - cudaTextureObject_t D_texObj; - bindProjDataTexture(D_projData, D_texObj, projPitch, dims.iProjDets, dims.iProjAngles); - for (unsigned int i = 0; i < dims.iProjAngles; ++i) { double d = angles[i].fDetUX * angles[i].fRayY - angles[i].fDetUY * angles[i].fRayX; angle_scaled_cos[i] = angles[i].fRayY / d; @@ -284,7 +258,8 @@ bool BP_SART(float* D_volumeData, unsigned int volumePitch, // We need to Clamp to the border pixels instead of to zero, because // SART weights with ray length. cudaTextureObject_t D_texObj; - bindProjDataTexture(D_projData, D_texObj, projPitch, dims.iProjDets, 1, cudaAddressModeClamp); + if (!createTextureObjectPitch2D(D_projData, D_texObj, projPitch, dims.iProjDets, 1, cudaAddressModeClamp)) + return false; double d = angles[angle].fDetUX * angles[angle].fRayY - angles[angle].fDetUY * angles[angle].fRayX; float angle_scaled_cos = angles[angle].fRayY / d; diff --git a/cuda/2d/par_fp.cu b/cuda/2d/par_fp.cu index cccf672..6035e0c 100644 --- a/cuda/2d/par_fp.cu +++ b/cuda/2d/par_fp.cu @@ -47,42 +47,6 @@ static const unsigned int g_anglesPerBlock = 16; static const unsigned int g_detBlockSize = 32; static const unsigned int g_blockSlices = 64; -// fixed point scaling factor -#define fPREC_FACTOR 16.0f -#define iPREC_FACTOR 16 - - -static bool bindVolumeDataTexture(float* data, cudaArray*& dataArray, cudaTextureObject_t& texObj, unsigned int pitch, unsigned int width, unsigned int height) -{ - // TODO: For very small sizes (roughly <=512x128) with few angles (<=180) - // not using an array is more efficient. - - cudaChannelFormatDesc channelDesc = - cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKindFloat); - - dataArray = 0; - cudaMallocArray(&dataArray, &channelDesc, width, height); - cudaMemcpy2DToArray(dataArray, 0, 0, data, pitch*sizeof(float), width*sizeof(float), height, cudaMemcpyDeviceToDevice); - - cudaResourceDesc resDesc; - memset(&resDesc, 0, sizeof(resDesc)); - resDesc.resType = cudaResourceTypeArray; - resDesc.res.array.array = dataArray; - - cudaTextureDesc texDesc; - memset(&texDesc, 0, sizeof(texDesc)); - texDesc.addressMode[0] = cudaAddressModeBorder; - texDesc.addressMode[1] = cudaAddressModeBorder; - texDesc.filterMode = cudaFilterModeLinear; - texDesc.readMode = cudaReadModeElementType; - texDesc.normalizedCoords = 0; - - texObj = 0; - - return checkCuda(cudaCreateTextureObject(&texObj, &resDesc, &texDesc, NULL), "par_fp texture"); -} - - // projection for angles that are roughly horizontal // (detector roughly vertical) __global__ void FPhorizontal_simple(float* D_projData, unsigned int projPitch, cudaTextureObject_t tex, unsigned int startSlice, unsigned int startAngle, unsigned int endAngle, const SDimensions dims, float outputScale) @@ -274,7 +238,8 @@ bool FP_simple_internal(float* D_volumeData, unsigned int volumePitch, cudaArray* D_dataArray; cudaTextureObject_t D_texObj; - bindVolumeDataTexture(D_volumeData, D_dataArray, D_texObj, volumePitch, dims.iVolWidth, dims.iVolHeight); + if (!createTextureObject2D(D_volumeData, D_dataArray, D_texObj, volumePitch, dims.iVolWidth, dims.iVolHeight)) + return false; convertAndUploadAngles(angles, dims.iProjAngles, dims.iProjDets); diff --git a/cuda/2d/util.cu b/cuda/2d/util.cu index ac360f0..2ad3c0f 100644 --- a/cuda/2d/util.cu +++ b/cuda/2d/util.cu @@ -126,6 +126,66 @@ void duplicateProjectionData(float* D_dst, float* D_src, unsigned int pitch, con cudaMemcpy2D(D_dst, sizeof(float)*pitch, D_src, sizeof(float)*pitch, sizeof(float)*dims.iProjDets, dims.iProjAngles, cudaMemcpyDeviceToDevice); } +bool createTextureObject2D(float* data, cudaArray*& dataArray, cudaTextureObject_t& texObj, unsigned int pitch, unsigned int width, unsigned int height) +{ + // TODO: For very small sizes (roughly <=512x128) with few angles (<=180) + // not using an array is more efficient. + + cudaChannelFormatDesc channelDesc = + cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKindFloat); + + dataArray = 0; + cudaMallocArray(&dataArray, &channelDesc, width, height); + cudaMemcpy2DToArray(dataArray, 0, 0, data, pitch*sizeof(float), width*sizeof(float), height, cudaMemcpyDeviceToDevice); + + cudaResourceDesc resDesc; + memset(&resDesc, 0, sizeof(resDesc)); + resDesc.resType = cudaResourceTypeArray; + resDesc.res.array.array = dataArray; + + cudaTextureDesc texDesc; + memset(&texDesc, 0, sizeof(texDesc)); + texDesc.addressMode[0] = cudaAddressModeBorder; + texDesc.addressMode[1] = cudaAddressModeBorder; + texDesc.filterMode = cudaFilterModeLinear; + texDesc.readMode = cudaReadModeElementType; + texDesc.normalizedCoords = 0; + + texObj = 0; + + return checkCuda(cudaCreateTextureObject(&texObj, &resDesc, &texDesc, NULL), "createTextureObject2D"); +} + +bool createTextureObjectPitch2D(float* data, cudaTextureObject_t& texObj, unsigned int pitch, unsigned int width, unsigned int height, cudaTextureAddressMode mode) +{ + cudaChannelFormatDesc channelDesc = + cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKindFloat); + + cudaResourceDesc resDesc; + memset(&resDesc, 0, sizeof(resDesc)); + resDesc.resType = cudaResourceTypePitch2D; + resDesc.res.pitch2D.devPtr = (void*)data; + resDesc.res.pitch2D.desc = channelDesc; + resDesc.res.pitch2D.width = width; + resDesc.res.pitch2D.height = height; + resDesc.res.pitch2D.pitchInBytes = sizeof(float)*pitch; + + cudaTextureDesc texDesc; + memset(&texDesc, 0, sizeof(texDesc)); + texDesc.addressMode[0] = mode; + texDesc.addressMode[1] = mode; + texDesc.filterMode = cudaFilterModeLinear; + texDesc.readMode = cudaReadModeElementType; + texDesc.normalizedCoords = 0; + + texObj = 0; + + return checkCuda(cudaCreateTextureObject(&texObj, &resDesc, &texDesc, NULL), "createTextureObjectPitch2D"); +} + + + + template __global__ void reduce1D(float *g_idata, float *g_odata, unsigned int n) { diff --git a/include/astra/cuda/2d/util.h b/include/astra/cuda/2d/util.h index 0fab9b1..cacf0a9 100644 --- a/include/astra/cuda/2d/util.h +++ b/include/astra/cuda/2d/util.h @@ -66,6 +66,10 @@ bool zeroProjectionData(float* D_ptr, unsigned int pitch, const SDimensions& dim void duplicateVolumeData(float* D_dst, float* D_src, unsigned int pitch, const SDimensions& dims); void duplicateProjectionData(float* D_dst, float* D_src, unsigned int pitch, const SDimensions& dims); +bool createTextureObject2D(float* data, cudaArray*& dataArray, cudaTextureObject_t& texObj, unsigned int pitch, unsigned int width, unsigned int height); +bool createTextureObjectPitch2D(float* data, cudaTextureObject_t& texObj, unsigned int pitch, unsigned int width, unsigned int height, cudaTextureAddressMode mode = cudaAddressModeBorder); + + bool checkCuda(cudaError_t err, const char *msg); float dotProduct2D(float* D_data, unsigned int pitch, -- cgit v1.2.1