From ca4a329c1389f17ce3d3911ecafc227771392b1a Mon Sep 17 00:00:00 2001 From: Willem Jan Palenstijn Date: Mon, 22 Nov 2021 13:06:58 +0100 Subject: Replace texref by texobj in par_fp --- cuda/2d/par_fp.cu | 59 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/cuda/2d/par_fp.cu b/cuda/2d/par_fp.cu index e947428..cccf672 100644 --- a/cuda/2d/par_fp.cu +++ b/cuda/2d/par_fp.cu @@ -34,11 +34,6 @@ along with the ASTRA Toolbox. If not, see . #include #include -typedef texture texture2D; - -static texture2D gT_volumeTexture; - - namespace astraCUDA { static const unsigned g_MaxAngles = 2560; @@ -57,32 +52,40 @@ static const unsigned int g_blockSlices = 64; #define iPREC_FACTOR 16 -static bool bindVolumeDataTexture(float* data, cudaArray*& dataArray, unsigned int pitch, unsigned int width, unsigned int height) +static bool bindVolumeDataTexture(float* data, cudaArray*& dataArray, cudaTextureObject_t& texObj, unsigned int pitch, unsigned int width, unsigned int height) { - cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc(); + // 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); - gT_volumeTexture.addressMode[0] = cudaAddressModeBorder; - gT_volumeTexture.addressMode[1] = cudaAddressModeBorder; - gT_volumeTexture.filterMode = cudaFilterModeLinear; - gT_volumeTexture.normalized = false; + cudaResourceDesc resDesc; + memset(&resDesc, 0, sizeof(resDesc)); + resDesc.resType = cudaResourceTypeArray; + resDesc.res.array.array = dataArray; - // TODO: For very small sizes (roughly <=512x128) with few angles (<=180) - // not using an array is more efficient. -// cudaBindTexture2D(0, gT_volumeTexture, (const void*)data, channelDesc, width, height, sizeof(float)*pitch); - cudaBindTextureToArray(gT_volumeTexture, dataArray, channelDesc); + cudaTextureDesc texDesc; + memset(&texDesc, 0, sizeof(texDesc)); + texDesc.addressMode[0] = cudaAddressModeBorder; + texDesc.addressMode[1] = cudaAddressModeBorder; + texDesc.filterMode = cudaFilterModeLinear; + texDesc.readMode = cudaReadModeElementType; + texDesc.normalizedCoords = 0; - // TODO: error value? + texObj = 0; - return true; + 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, unsigned int startSlice, unsigned int startAngle, unsigned int endAngle, const SDimensions dims, float outputScale) +__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) { const int relDet = threadIdx.x; const int relAngle = threadIdx.y; @@ -134,7 +137,7 @@ __global__ void FPhorizontal_simple(float* D_projData, unsigned int projPitch, u for (int slice = startSlice; slice < endSlice; ++slice) { for (int iSubT = 0; iSubT < dims.iRaysPerDet; ++iSubT) { - fVal += tex2D(gT_volumeTexture, fS, fP); + fVal += tex2D(tex, fS, fP); fP += fSubDetStep; } fP += fSliceStep; @@ -145,7 +148,7 @@ __global__ void FPhorizontal_simple(float* D_projData, unsigned int projPitch, u for (int slice = startSlice; slice < endSlice; ++slice) { - fVal += tex2D(gT_volumeTexture, fS, fP); + fVal += tex2D(tex, fS, fP); fP += fSliceStep; fS += 1.0f; } @@ -159,7 +162,7 @@ __global__ void FPhorizontal_simple(float* D_projData, unsigned int projPitch, u // projection for angles that are roughly vertical // (detector roughly horizontal) -__global__ void FPvertical_simple(float* D_projData, unsigned int projPitch, unsigned int startSlice, unsigned int startAngle, unsigned int endAngle, const SDimensions dims, float outputScale) +__global__ void FPvertical_simple(float* D_projData, unsigned int projPitch, cudaTextureObject_t tex, unsigned int startSlice, unsigned int startAngle, unsigned int endAngle, const SDimensions dims, float outputScale) { const int relDet = threadIdx.x; const int relAngle = threadIdx.y; @@ -210,7 +213,7 @@ __global__ void FPvertical_simple(float* D_projData, unsigned int projPitch, uns for (int slice = startSlice; slice < endSlice; ++slice) { for (int iSubT = 0; iSubT < dims.iRaysPerDet; ++iSubT) { - fVal += tex2D(gT_volumeTexture, fP, fS); + fVal += tex2D(tex, fP, fS); fP += fSubDetStep; } fP += fSliceStep; @@ -221,7 +224,7 @@ __global__ void FPvertical_simple(float* D_projData, unsigned int projPitch, uns for (int slice = startSlice; slice < endSlice; ++slice) { - fVal += tex2D(gT_volumeTexture, fP, fS); + fVal += tex2D(tex, fP, fS); fP += fSliceStep; fS += 1.0f; } @@ -269,7 +272,9 @@ bool FP_simple_internal(float* D_volumeData, unsigned int volumePitch, assert(angles); cudaArray* D_dataArray; - bindVolumeDataTexture(D_volumeData, D_dataArray, volumePitch, dims.iVolWidth, dims.iVolHeight); + cudaTextureObject_t D_texObj; + + bindVolumeDataTexture(D_volumeData, D_dataArray, D_texObj, volumePitch, dims.iVolWidth, dims.iVolHeight); convertAndUploadAngles(angles, dims.iProjAngles, dims.iProjDets); @@ -313,10 +318,10 @@ bool FP_simple_internal(float* D_volumeData, unsigned int volumePitch, //printf("angle block: %d to %d, %d\n", blockStart, blockEnd, blockVertical); if (!blockVertical) for (unsigned int i = 0; i < dims.iVolWidth; i += g_blockSlices) - FPhorizontal_simple<<>>(D_projData, projPitch, i, blockStart, blockEnd, dims, outputScale); + FPhorizontal_simple<<>>(D_projData, projPitch, D_texObj, i, blockStart, blockEnd, dims, outputScale); else for (unsigned int i = 0; i < dims.iVolHeight; i += g_blockSlices) - FPvertical_simple<<>>(D_projData, projPitch, i, blockStart, blockEnd, dims, outputScale); + FPvertical_simple<<>>(D_projData, projPitch, D_texObj, i, blockStart, blockEnd, dims, outputScale); } blockVertical = vertical; blockStart = a; @@ -332,6 +337,8 @@ bool FP_simple_internal(float* D_volumeData, unsigned int volumePitch, cudaFreeArray(D_dataArray); + cudaDestroyTextureObject(D_texObj); + return ok; } -- cgit v1.2.1 From 38d53a0900a41d013879168236278b8ba9cff99b Mon Sep 17 00:00:00 2001 From: Willem Jan Palenstijn Date: Mon, 22 Nov 2021 13:12:35 +0100 Subject: Replace texref by texobj in fan_fp --- cuda/2d/fan_fp.cu | 56 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/cuda/2d/fan_fp.cu b/cuda/2d/fan_fp.cu index 342ca4c..0801097 100644 --- a/cuda/2d/fan_fp.cu +++ b/cuda/2d/fan_fp.cu @@ -33,12 +33,6 @@ along with the ASTRA Toolbox. If not, see . #include #include - -typedef texture texture2D; - -static texture2D gT_FanVolumeTexture; - - namespace astraCUDA { static const unsigned g_MaxAngles = 2560; @@ -55,31 +49,39 @@ 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, unsigned int pitch, unsigned int width, unsigned int height) +static bool bindVolumeDataTexture(float* data, cudaArray*& dataArray, cudaTextureObject_t& texObj, unsigned int pitch, unsigned int width, unsigned int height) { - cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc(); + // 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); - gT_FanVolumeTexture.addressMode[0] = cudaAddressModeBorder; - gT_FanVolumeTexture.addressMode[1] = cudaAddressModeBorder; - gT_FanVolumeTexture.filterMode = cudaFilterModeLinear; - gT_FanVolumeTexture.normalized = false; + cudaResourceDesc resDesc; + memset(&resDesc, 0, sizeof(resDesc)); + resDesc.resType = cudaResourceTypeArray; + resDesc.res.array.array = dataArray; - // TODO: For very small sizes (roughly <=512x128) with few angles (<=180) - // not using an array is more efficient. - //cudaBindTexture2D(0, gT_FanVolumeTexture, (const void*)data, channelDesc, width, height, sizeof(float)*pitch); - cudaBindTextureToArray(gT_FanVolumeTexture, dataArray, channelDesc); + cudaTextureDesc texDesc; + memset(&texDesc, 0, sizeof(texDesc)); + texDesc.addressMode[0] = cudaAddressModeBorder; + texDesc.addressMode[1] = cudaAddressModeBorder; + texDesc.filterMode = cudaFilterModeLinear; + texDesc.readMode = cudaReadModeElementType; + texDesc.normalizedCoords = 0; - // TODO: error value? + texObj = 0; - return true; + 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, unsigned int startSlice, unsigned int startAngle, unsigned int endAngle, const SDimensions dims, float outputScale) +__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) { float* projData = (float*)D_projData; const int relDet = threadIdx.x; @@ -134,7 +136,7 @@ __global__ void FanFPhorizontal(float* D_projData, unsigned int projPitch, unsig float fV = 0.0f; for (int slice = startSlice; slice < endSlice; ++slice) { - fV += tex2D(gT_FanVolumeTexture, fX, fY); + fV += tex2D(tex, fX, fY); fY -= alpha; fX += 1.0f; } @@ -149,7 +151,7 @@ __global__ void FanFPhorizontal(float* D_projData, unsigned int projPitch, unsig // projection for angles that are roughly vertical // (detector roughly horizontal) -__global__ void FanFPvertical(float* D_projData, unsigned int projPitch, unsigned int startSlice, unsigned int startAngle, unsigned int endAngle, const SDimensions dims, float outputScale) +__global__ void FanFPvertical(float* D_projData, unsigned int projPitch, cudaTextureObject_t tex, unsigned int startSlice, unsigned int startAngle, unsigned int endAngle, const SDimensions dims, float outputScale) { const int relDet = threadIdx.x; const int relAngle = threadIdx.y; @@ -207,7 +209,7 @@ __global__ void FanFPvertical(float* D_projData, unsigned int projPitch, unsigne for (int slice = startSlice; slice < endSlice; ++slice) { - fV += tex2D(gT_FanVolumeTexture, fX, fY); + fV += tex2D(tex, fX, fY); fX -= alpha; fY += 1.0f; } @@ -227,7 +229,9 @@ bool FanFP_internal(float* D_volumeData, unsigned int volumePitch, assert(dims.iProjAngles <= g_MaxAngles); cudaArray* D_dataArray; - bindVolumeDataTexture(D_volumeData, D_dataArray, volumePitch, dims.iVolWidth, dims.iVolHeight); + cudaTextureObject_t D_texObj; + + bindVolumeDataTexture(D_volumeData, D_dataArray, D_texObj, volumePitch, dims.iVolWidth, dims.iVolHeight); // transfer angles to constant memory float* tmp = new float[dims.iProjAngles]; @@ -260,13 +264,13 @@ bool FanFP_internal(float* D_volumeData, unsigned int volumePitch, cudaStreamCreate(&stream1); streams.push_back(stream1); for (unsigned int i = 0; i < dims.iVolWidth; i += g_blockSlices) - FanFPhorizontal<<>>(D_projData, projPitch, i, blockStart, blockEnd, dims, outputScale); + FanFPhorizontal<<>>(D_projData, projPitch, D_texObj, i, blockStart, blockEnd, dims, outputScale); cudaStream_t stream2; cudaStreamCreate(&stream2); streams.push_back(stream2); for (unsigned int i = 0; i < dims.iVolHeight; i += g_blockSlices) - FanFPvertical<<>>(D_projData, projPitch, i, blockStart, blockEnd, dims, outputScale); + FanFPvertical<<>>(D_projData, projPitch, D_texObj, i, blockStart, blockEnd, dims, outputScale); bool ok = true; @@ -278,6 +282,8 @@ bool FanFP_internal(float* D_volumeData, unsigned int volumePitch, cudaFreeArray(D_dataArray); + cudaDestroyTextureObject(D_texObj); + return ok; } -- cgit v1.2.1 From 87ca44ca75a966cb6c1be88b201f9132ee176003 Mon Sep 17 00:00:00 2001 From: Willem Jan Palenstijn Date: Mon, 22 Nov 2021 14:01:19 +0100 Subject: Replace texref by texobj in par3d_fp --- cuda/3d/par3d_fp.cu | 79 +++++++++++++++++++++++++++++------------------------ 1 file changed, 44 insertions(+), 35 deletions(-) diff --git a/cuda/3d/par3d_fp.cu b/cuda/3d/par3d_fp.cu index 5daddc1..cae75f1 100644 --- a/cuda/3d/par3d_fp.cu +++ b/cuda/3d/par3d_fp.cu @@ -35,10 +35,6 @@ along with the ASTRA Toolbox. If not, see . #include -typedef texture texture3D; - -static texture3D gT_par3DVolumeTexture; - namespace astraCUDA3d { static const unsigned int g_anglesPerBlock = 4; @@ -63,21 +59,26 @@ __constant__ float gC_DetVY[g_MaxAngles]; __constant__ float gC_DetVZ[g_MaxAngles]; -static bool bindVolumeDataTexture(const cudaArray* array) +static bool bindVolumeDataTexture(cudaArray* array, cudaTextureObject_t& texObj) { - cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc(); - - gT_par3DVolumeTexture.addressMode[0] = cudaAddressModeBorder; - gT_par3DVolumeTexture.addressMode[1] = cudaAddressModeBorder; - gT_par3DVolumeTexture.addressMode[2] = cudaAddressModeBorder; - gT_par3DVolumeTexture.filterMode = cudaFilterModeLinear; - gT_par3DVolumeTexture.normalized = false; - - cudaBindTextureToArray(gT_par3DVolumeTexture, array, channelDesc); - - // TODO: error value? - - return true; + cudaChannelFormatDesc channelDesc = + cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKindFloat); + + cudaResourceDesc resDesc; + memset(&resDesc, 0, sizeof(resDesc)); + resDesc.resType = cudaResourceTypeArray; + resDesc.res.array.array = array; + + cudaTextureDesc texDesc; + memset(&texDesc, 0, sizeof(texDesc)); + texDesc.addressMode[0] = cudaAddressModeBorder; + texDesc.addressMode[1] = cudaAddressModeBorder; + texDesc.addressMode[2] = cudaAddressModeBorder; + texDesc.filterMode = cudaFilterModeLinear; + texDesc.readMode = cudaReadModeElementType; + texDesc.normalizedCoords = 0; + + return checkCuda(cudaCreateTextureObject(&texObj, &resDesc, &texDesc, NULL), "par3d_fp texture"); } @@ -89,7 +90,7 @@ struct DIR_X { __device__ float c0(float x, float y, float z) const { return x; } __device__ float c1(float x, float y, float z) const { return y; } __device__ float c2(float x, float y, float z) const { return z; } - __device__ float tex(float f0, float f1, float f2) const { return tex3D(gT_par3DVolumeTexture, f0, f1, f2); } + __device__ float tex(cudaTextureObject_t tex, float f0, float f1, float f2) const { return tex3D(tex, f0, f1, f2); } __device__ float x(float f0, float f1, float f2) const { return f0; } __device__ float y(float f0, float f1, float f2) const { return f1; } __device__ float z(float f0, float f1, float f2) const { return f2; } @@ -103,7 +104,7 @@ struct DIR_Y { __device__ float c0(float x, float y, float z) const { return y; } __device__ float c1(float x, float y, float z) const { return x; } __device__ float c2(float x, float y, float z) const { return z; } - __device__ float tex(float f0, float f1, float f2) const { return tex3D(gT_par3DVolumeTexture, f1, f0, f2); } + __device__ float tex(cudaTextureObject_t tex, float f0, float f1, float f2) const { return tex3D(tex, f1, f0, f2); } __device__ float x(float f0, float f1, float f2) const { return f1; } __device__ float y(float f0, float f1, float f2) const { return f0; } __device__ float z(float f0, float f1, float f2) const { return f2; } @@ -117,7 +118,7 @@ struct DIR_Z { __device__ float c0(float x, float y, float z) const { return z; } __device__ float c1(float x, float y, float z) const { return x; } __device__ float c2(float x, float y, float z) const { return y; } - __device__ float tex(float f0, float f1, float f2) const { return tex3D(gT_par3DVolumeTexture, f1, f2, f0); } + __device__ float tex(cudaTextureObject_t tex, float f0, float f1, float f2) const { return tex3D(tex, f1, f2, f0); } __device__ float x(float f0, float f1, float f2) const { return f1; } __device__ float y(float f0, float f1, float f2) const { return f2; } __device__ float z(float f0, float f1, float f2) const { return f0; } @@ -145,6 +146,7 @@ struct SCALE_NONCUBE { template __global__ void par3D_FP_t(float* D_projData, unsigned int projPitch, + cudaTextureObject_t tex, unsigned int startSlice, unsigned int startAngle, unsigned int endAngle, const SDimensions3D dims, @@ -210,7 +212,7 @@ __global__ void par3D_FP_t(float* D_projData, unsigned int projPitch, for (int s = startSlice; s < endSlice; ++s) { - fVal += c.tex(f0, f1, f2); + fVal += c.tex(tex, f0, f1, f2); f0 += 1.0f; f1 += a1; f2 += a2; @@ -225,6 +227,7 @@ __global__ void par3D_FP_t(float* D_projData, unsigned int projPitch, // Supersampling version template __global__ void par3D_FP_SS_t(float* D_projData, unsigned int projPitch, + cudaTextureObject_t tex, unsigned int startSlice, unsigned int startAngle, unsigned int endAngle, const SDimensions3D dims, int iRaysPerDetDim, @@ -301,7 +304,7 @@ __global__ void par3D_FP_SS_t(float* D_projData, unsigned int projPitch, for (int s = startSlice; s < endSlice; ++s) { - fVal += c.tex(f0, f1, f2); + fVal += c.tex(tex, f0, f1, f2); f0 += 1.0f; f1 += a1; f2 += a2; @@ -415,7 +418,9 @@ __global__ void par3D_FP_SumSqW_t(float* D_projData, unsigned int projPitch, bool Par3DFP_Array_internal(cudaPitchedPtr D_projData, - const SDimensions3D& dims, unsigned int angleCount, const SPar3DProjection* angles, + cudaTextureObject_t D_texObj, + const SDimensions3D& dims, + unsigned int angleCount, const SPar3DProjection* angles, const SProjectorParams3D& params) { // transfer angles to constant memory @@ -520,29 +525,29 @@ bool Par3DFP_Array_internal(cudaPitchedPtr D_projData, for (unsigned int i = 0; i < dims.iVolX; i += g_blockSlices) if (params.iRaysPerDetDim == 1) if (cube) - par3D_FP_t<<>>((float*)D_projData.ptr, D_projData.pitch/sizeof(float), i, blockStart, blockEnd, dims, scube); + par3D_FP_t<<>>((float*)D_projData.ptr, D_projData.pitch/sizeof(float), D_texObj, i, blockStart, blockEnd, dims, scube); else - par3D_FP_t<<>>((float*)D_projData.ptr, D_projData.pitch/sizeof(float), i, blockStart, blockEnd, dims, snoncubeX); + par3D_FP_t<<>>((float*)D_projData.ptr, D_projData.pitch/sizeof(float), D_texObj,i, blockStart, blockEnd, dims, snoncubeX); else - par3D_FP_SS_t<<>>((float*)D_projData.ptr, D_projData.pitch/sizeof(float), i, blockStart, blockEnd, dims, params.iRaysPerDetDim, snoncubeX); + par3D_FP_SS_t<<>>((float*)D_projData.ptr, D_projData.pitch/sizeof(float), D_texObj,i, blockStart, blockEnd, dims, params.iRaysPerDetDim, snoncubeX); } else if (blockDirection == 1) { for (unsigned int i = 0; i < dims.iVolY; i += g_blockSlices) if (params.iRaysPerDetDim == 1) if (cube) - par3D_FP_t<<>>((float*)D_projData.ptr, D_projData.pitch/sizeof(float), i, blockStart, blockEnd, dims, scube); + par3D_FP_t<<>>((float*)D_projData.ptr, D_projData.pitch/sizeof(float), D_texObj,i, blockStart, blockEnd, dims, scube); else - par3D_FP_t<<>>((float*)D_projData.ptr, D_projData.pitch/sizeof(float), i, blockStart, blockEnd, dims, snoncubeY); + par3D_FP_t<<>>((float*)D_projData.ptr, D_projData.pitch/sizeof(float), D_texObj,i, blockStart, blockEnd, dims, snoncubeY); else - par3D_FP_SS_t<<>>((float*)D_projData.ptr, D_projData.pitch/sizeof(float), i, blockStart, blockEnd, dims, params.iRaysPerDetDim, snoncubeY); + par3D_FP_SS_t<<>>((float*)D_projData.ptr, D_projData.pitch/sizeof(float), D_texObj,i, blockStart, blockEnd, dims, params.iRaysPerDetDim, snoncubeY); } else if (blockDirection == 2) { for (unsigned int i = 0; i < dims.iVolZ; i += g_blockSlices) if (params.iRaysPerDetDim == 1) if (cube) - par3D_FP_t<<>>((float*)D_projData.ptr, D_projData.pitch/sizeof(float), i, blockStart, blockEnd, dims, scube); + par3D_FP_t<<>>((float*)D_projData.ptr, D_projData.pitch/sizeof(float), D_texObj,i, blockStart, blockEnd, dims, scube); else - par3D_FP_t<<>>((float*)D_projData.ptr, D_projData.pitch/sizeof(float), i, blockStart, blockEnd, dims, snoncubeZ); + par3D_FP_t<<>>((float*)D_projData.ptr, D_projData.pitch/sizeof(float), D_texObj,i, blockStart, blockEnd, dims, snoncubeZ); else - par3D_FP_SS_t<<>>((float*)D_projData.ptr, D_projData.pitch/sizeof(float), i, blockStart, blockEnd, dims, params.iRaysPerDetDim, snoncubeZ); + par3D_FP_SS_t<<>>((float*)D_projData.ptr, D_projData.pitch/sizeof(float), D_texObj,i, blockStart, blockEnd, dims, params.iRaysPerDetDim, snoncubeZ); } } @@ -569,10 +574,12 @@ bool Par3DFP(cudaPitchedPtr D_volumeData, const SDimensions3D& dims, const SPar3DProjection* angles, const SProjectorParams3D& params) { + cudaTextureObject_t D_texObj; + // transfer volume to array cudaArray* cuArray = allocateVolumeArray(dims); transferVolumeToArray(D_volumeData, cuArray, dims); - bindVolumeDataTexture(cuArray); + bindVolumeDataTexture(cuArray, D_texObj); bool ret; @@ -584,7 +591,7 @@ bool Par3DFP(cudaPitchedPtr D_volumeData, cudaPitchedPtr D_subprojData = D_projData; D_subprojData.ptr = (char*)D_projData.ptr + iAngle * D_projData.pitch; - ret = Par3DFP_Array_internal(D_subprojData, + ret = Par3DFP_Array_internal(D_subprojData, D_texObj, dims, iEndAngle - iAngle, angles + iAngle, params); if (!ret) @@ -593,6 +600,8 @@ bool Par3DFP(cudaPitchedPtr D_volumeData, cudaFreeArray(cuArray); + cudaDestroyTextureObject(D_texObj); + return ret; } -- cgit v1.2.1 From 26fd23c53f35e374618b660a82d9de1755490a62 Mon Sep 17 00:00:00 2001 From: Willem Jan Palenstijn Date: Mon, 22 Nov 2021 14:01:25 +0100 Subject: Replace texref by texobj in cone_fp --- cuda/3d/cone_fp.cu | 78 +++++++++++++++++++++++++++++------------------------- 1 file changed, 42 insertions(+), 36 deletions(-) diff --git a/cuda/3d/cone_fp.cu b/cuda/3d/cone_fp.cu index 2c3d1f6..e49ea24 100644 --- a/cuda/3d/cone_fp.cu +++ b/cuda/3d/cone_fp.cu @@ -35,10 +35,6 @@ along with the ASTRA Toolbox. If not, see . #include -typedef texture texture3D; - -static texture3D gT_coneVolumeTexture; - namespace astraCUDA3d { static const unsigned int g_anglesPerBlock = 4; @@ -63,21 +59,26 @@ __constant__ float gC_DetVY[g_MaxAngles]; __constant__ float gC_DetVZ[g_MaxAngles]; -bool bindVolumeDataTexture(const cudaArray* array) +bool bindVolumeDataTexture(cudaArray* array, cudaTextureObject_t& texObj) { - cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc(); - - gT_coneVolumeTexture.addressMode[0] = cudaAddressModeBorder; - gT_coneVolumeTexture.addressMode[1] = cudaAddressModeBorder; - gT_coneVolumeTexture.addressMode[2] = cudaAddressModeBorder; - gT_coneVolumeTexture.filterMode = cudaFilterModeLinear; - gT_coneVolumeTexture.normalized = false; - - cudaBindTextureToArray(gT_coneVolumeTexture, array, channelDesc); - - // TODO: error value? - - return true; + cudaChannelFormatDesc channelDesc = + cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKindFloat); + + cudaResourceDesc resDesc; + memset(&resDesc, 0, sizeof(resDesc)); + resDesc.resType = cudaResourceTypeArray; + resDesc.res.array.array = array; + + cudaTextureDesc texDesc; + memset(&texDesc, 0, sizeof(texDesc)); + texDesc.addressMode[0] = cudaAddressModeBorder; + texDesc.addressMode[1] = cudaAddressModeBorder; + texDesc.addressMode[2] = cudaAddressModeBorder; + texDesc.filterMode = cudaFilterModeLinear; + texDesc.readMode = cudaReadModeElementType; + texDesc.normalizedCoords = 0; + + return checkCuda(cudaCreateTextureObject(&texObj, &resDesc, &texDesc, NULL), "cone_fp texture"); } @@ -89,7 +90,7 @@ struct DIR_X { __device__ float c0(float x, float y, float z) const { return x; } __device__ float c1(float x, float y, float z) const { return y; } __device__ float c2(float x, float y, float z) const { return z; } - __device__ float tex(float f0, float f1, float f2) const { return tex3D(gT_coneVolumeTexture, f0, f1, f2); } + __device__ float tex(cudaTextureObject_t tex, float f0, float f1, float f2) const { return tex3D(tex, f0, f1, f2); } __device__ float x(float f0, float f1, float f2) const { return f0; } __device__ float y(float f0, float f1, float f2) const { return f1; } __device__ float z(float f0, float f1, float f2) const { return f2; } @@ -103,7 +104,7 @@ struct DIR_Y { __device__ float c0(float x, float y, float z) const { return y; } __device__ float c1(float x, float y, float z) const { return x; } __device__ float c2(float x, float y, float z) const { return z; } - __device__ float tex(float f0, float f1, float f2) const { return tex3D(gT_coneVolumeTexture, f1, f0, f2); } + __device__ float tex(cudaTextureObject_t tex, float f0, float f1, float f2) const { return tex3D(tex, f1, f0, f2); } __device__ float x(float f0, float f1, float f2) const { return f1; } __device__ float y(float f0, float f1, float f2) const { return f0; } __device__ float z(float f0, float f1, float f2) const { return f2; } @@ -117,7 +118,7 @@ struct DIR_Z { __device__ float c0(float x, float y, float z) const { return z; } __device__ float c1(float x, float y, float z) const { return x; } __device__ float c2(float x, float y, float z) const { return y; } - __device__ float tex(float f0, float f1, float f2) const { return tex3D(gT_coneVolumeTexture, f1, f2, f0); } + __device__ float tex(cudaTextureObject_t tex, float f0, float f1, float f2) const { return tex3D(tex, f1, f2, f0); } __device__ float x(float f0, float f1, float f2) const { return f1; } __device__ float y(float f0, float f1, float f2) const { return f2; } __device__ float z(float f0, float f1, float f2) const { return f0; } @@ -144,6 +145,7 @@ struct SCALE_NONCUBE { template __global__ void cone_FP_t(float* D_projData, unsigned int projPitch, + cudaTextureObject_t tex, unsigned int startSlice, unsigned int startAngle, unsigned int endAngle, const SDimensions3D dims, @@ -208,7 +210,7 @@ __global__ void cone_FP_t(float* D_projData, unsigned int projPitch, for (int s = startSlice; s < endSlice; ++s) { - fVal += c.tex(f0, f1, f2); + fVal += c.tex(tex, f0, f1, f2); f0 += 1.0f; f1 += a1; f2 += a2; @@ -222,6 +224,7 @@ __global__ void cone_FP_t(float* D_projData, unsigned int projPitch, template __global__ void cone_FP_SS_t(float* D_projData, unsigned int projPitch, + cudaTextureObject_t tex, unsigned int startSlice, unsigned int startAngle, unsigned int endAngle, const SDimensions3D dims, int iRaysPerDetDim, @@ -295,7 +298,7 @@ __global__ void cone_FP_SS_t(float* D_projData, unsigned int projPitch, for (int s = startSlice; s < endSlice; ++s) { - fVal += c.tex(f0, f1, f2); + fVal += c.tex(tex, f0, f1, f2); f0 += 1.0f; f1 += a1; f2 += a2; @@ -313,7 +316,9 @@ __global__ void cone_FP_SS_t(float* D_projData, unsigned int projPitch, bool ConeFP_Array_internal(cudaPitchedPtr D_projData, - const SDimensions3D& dims, unsigned int angleCount, const SConeProjection* angles, + cudaTextureObject_t D_texObj, + const SDimensions3D& dims, + unsigned int angleCount, const SConeProjection* angles, const SProjectorParams3D& params) { // transfer angles to constant memory @@ -419,29 +424,29 @@ bool ConeFP_Array_internal(cudaPitchedPtr D_projData, for (unsigned int i = 0; i < dims.iVolX; i += g_blockSlices) if (params.iRaysPerDetDim == 1) if (cube) - cone_FP_t<<>>((float*)D_projData.ptr, D_projData.pitch/sizeof(float), i, blockStart, blockEnd, dims, scube); + cone_FP_t<<>>((float*)D_projData.ptr, D_projData.pitch/sizeof(float), D_texObj, i, blockStart, blockEnd, dims, scube); else - cone_FP_t<<>>((float*)D_projData.ptr, D_projData.pitch/sizeof(float), i, blockStart, blockEnd, dims, snoncubeX); + cone_FP_t<<>>((float*)D_projData.ptr, D_projData.pitch/sizeof(float), D_texObj, i, blockStart, blockEnd, dims, snoncubeX); else - cone_FP_SS_t<<>>((float*)D_projData.ptr, D_projData.pitch/sizeof(float), i, blockStart, blockEnd, dims, params.iRaysPerDetDim, snoncubeX); + cone_FP_SS_t<<>>((float*)D_projData.ptr, D_projData.pitch/sizeof(float), D_texObj, i, blockStart, blockEnd, dims, params.iRaysPerDetDim, snoncubeX); } else if (blockDirection == 1) { for (unsigned int i = 0; i < dims.iVolY; i += g_blockSlices) if (params.iRaysPerDetDim == 1) if (cube) - cone_FP_t<<>>((float*)D_projData.ptr, D_projData.pitch/sizeof(float), i, blockStart, blockEnd, dims, scube); + cone_FP_t<<>>((float*)D_projData.ptr, D_projData.pitch/sizeof(float), D_texObj, i, blockStart, blockEnd, dims, scube); else - cone_FP_t<<>>((float*)D_projData.ptr, D_projData.pitch/sizeof(float), i, blockStart, blockEnd, dims, snoncubeY); + cone_FP_t<<>>((float*)D_projData.ptr, D_projData.pitch/sizeof(float), D_texObj, i, blockStart, blockEnd, dims, snoncubeY); else - cone_FP_SS_t<<>>((float*)D_projData.ptr, D_projData.pitch/sizeof(float), i, blockStart, blockEnd, dims, params.iRaysPerDetDim, snoncubeY); + cone_FP_SS_t<<>>((float*)D_projData.ptr, D_projData.pitch/sizeof(float), D_texObj, i, blockStart, blockEnd, dims, params.iRaysPerDetDim, snoncubeY); } else if (blockDirection == 2) { for (unsigned int i = 0; i < dims.iVolZ; i += g_blockSlices) if (params.iRaysPerDetDim == 1) if (cube) - cone_FP_t<<>>((float*)D_projData.ptr, D_projData.pitch/sizeof(float), i, blockStart, blockEnd, dims, scube); + cone_FP_t<<>>((float*)D_projData.ptr, D_projData.pitch/sizeof(float), D_texObj, i, blockStart, blockEnd, dims, scube); else - cone_FP_t<<>>((float*)D_projData.ptr, D_projData.pitch/sizeof(float), i, blockStart, blockEnd, dims, snoncubeZ); + cone_FP_t<<>>((float*)D_projData.ptr, D_projData.pitch/sizeof(float), D_texObj, i, blockStart, blockEnd, dims, snoncubeZ); else - cone_FP_SS_t<<>>((float*)D_projData.ptr, D_projData.pitch/sizeof(float), i, blockStart, blockEnd, dims, params.iRaysPerDetDim, snoncubeZ); + cone_FP_SS_t<<>>((float*)D_projData.ptr, D_projData.pitch/sizeof(float), D_texObj, i, blockStart, blockEnd, dims, params.iRaysPerDetDim, snoncubeZ); } } @@ -469,11 +474,12 @@ bool ConeFP(cudaPitchedPtr D_volumeData, const SDimensions3D& dims, const SConeProjection* angles, const SProjectorParams3D& params) { - // transfer volume to array + cudaTextureObject_t D_texObj; + // transfer volume to array cudaArray* cuArray = allocateVolumeArray(dims); transferVolumeToArray(D_volumeData, cuArray, dims); - bindVolumeDataTexture(cuArray); + bindVolumeDataTexture(cuArray, D_texObj); bool ret; @@ -485,7 +491,7 @@ bool ConeFP(cudaPitchedPtr D_volumeData, cudaPitchedPtr D_subprojData = D_projData; D_subprojData.ptr = (char*)D_projData.ptr + iAngle * D_projData.pitch; - ret = ConeFP_Array_internal(D_subprojData, + ret = ConeFP_Array_internal(D_subprojData, D_texObj, dims, iEndAngle - iAngle, angles + iAngle, params); if (!ret) -- cgit v1.2.1 From 1902175e8c10ee6e7175c6b0051e524e954f6da9 Mon Sep 17 00:00:00 2001 From: Willem Jan Palenstijn Date: Mon, 22 Nov 2021 14:01:41 +0100 Subject: Replace texref by texobj in par3d_bp --- cuda/3d/par3d_bp.cu | 67 +++++++++++++++++++++++++++++------------------------ 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/cuda/3d/par3d_bp.cu b/cuda/3d/par3d_bp.cu index 1dc75ce..748086e 100644 --- a/cuda/3d/par3d_bp.cu +++ b/cuda/3d/par3d_bp.cu @@ -35,10 +35,6 @@ along with the ASTRA Toolbox. If not, see . #include -typedef texture texture3D; - -static texture3D gT_par3DProjTexture; - namespace astraCUDA3d { static const unsigned int g_volBlockZ = 6; @@ -58,26 +54,31 @@ __constant__ DevPar3DParams gC_C[g_MaxAngles]; __constant__ float gC_scale[g_MaxAngles]; -static bool bindProjDataTexture(const cudaArray* array) +static bool bindProjDataTexture(cudaArray* array, cudaTextureObject_t& texObj) { - cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc(); - - gT_par3DProjTexture.addressMode[0] = cudaAddressModeBorder; - gT_par3DProjTexture.addressMode[1] = cudaAddressModeBorder; - gT_par3DProjTexture.addressMode[2] = cudaAddressModeBorder; - gT_par3DProjTexture.filterMode = cudaFilterModeLinear; - gT_par3DProjTexture.normalized = false; - - cudaBindTextureToArray(gT_par3DProjTexture, array, channelDesc); - - // TODO: error value? - - return true; + cudaChannelFormatDesc channelDesc = + cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKindFloat); + + cudaResourceDesc resDesc; + memset(&resDesc, 0, sizeof(resDesc)); + resDesc.resType = cudaResourceTypeArray; + resDesc.res.array.array = array; + + cudaTextureDesc texDesc; + memset(&texDesc, 0, sizeof(texDesc)); + texDesc.addressMode[0] = cudaAddressModeBorder; + texDesc.addressMode[1] = cudaAddressModeBorder; + texDesc.addressMode[2] = cudaAddressModeBorder; + texDesc.filterMode = cudaFilterModeLinear; + texDesc.readMode = cudaReadModeElementType; + texDesc.normalizedCoords = 0; + + return checkCuda(cudaCreateTextureObject(&texObj, &resDesc, &texDesc, NULL), "par3d_bp texture"); } template -__global__ void dev_par3D_BP(void* D_volData, unsigned int volPitch, int startAngle, int angleOffset, const SDimensions3D dims, float fOutputScale) +__global__ void dev_par3D_BP(void* D_volData, unsigned int volPitch, cudaTextureObject_t tex, int startAngle, int angleOffset, const SDimensions3D dims, float fOutputScale) { float* volData = (float*)D_volData; @@ -125,7 +126,7 @@ __global__ void dev_par3D_BP(void* D_volData, unsigned int volPitch, int startAn for (int idx = 0; idx < ZSIZE; ++idx) { - float fVal = tex3D(gT_par3DProjTexture, fU, fAngle, fV); + float fVal = tex3D(tex, fU, fAngle, fV); Z[idx] += fVal * fS; fU += fCu.z; @@ -144,7 +145,7 @@ __global__ void dev_par3D_BP(void* D_volData, unsigned int volPitch, int startAn } // supersampling version -__global__ void dev_par3D_BP_SS(void* D_volData, unsigned int volPitch, int startAngle, int angleOffset, const SDimensions3D dims, int iRaysPerVoxelDim, float fOutputScale) +__global__ void dev_par3D_BP_SS(void* D_volData, unsigned int volPitch, cudaTextureObject_t tex, int startAngle, int angleOffset, const SDimensions3D dims, int iRaysPerVoxelDim, float fOutputScale) { float* volData = (float*)D_volData; @@ -206,7 +207,7 @@ __global__ void dev_par3D_BP_SS(void* D_volData, unsigned int volPitch, int star const float fU = fCu.w + fXs * fCu.x + fYs * fCu.y + fZs * fCu.z; const float fV = fCv.w + fXs * fCv.x + fYs * fCv.y + fZs * fCv.z; - fVal += tex3D(gT_par3DProjTexture, fU, fAngle, fV) * fS; + fVal += tex3D(tex, fU, fAngle, fV) * fS; fZs += fSubStep; } fYs += fSubStep; @@ -259,18 +260,21 @@ bool Par3DBP_Array(cudaPitchedPtr D_volumeData, const SDimensions3D& dims, const SPar3DProjection* angles, const SProjectorParams3D& params) { - bindProjDataTexture(D_projArray); + cudaTextureObject_t D_texObj; + bindProjDataTexture(D_projArray, D_texObj); float fOutputScale = params.fOutputScale * params.fVolScaleX * params.fVolScaleY * params.fVolScaleZ; + bool ok = true; + for (unsigned int th = 0; th < dims.iProjAngles; th += g_MaxAngles) { unsigned int angleCount = g_MaxAngles; if (th + angleCount > dims.iProjAngles) angleCount = dims.iProjAngles - th; - bool ok = transferConstants(angles, angleCount, params); + ok = transferConstants(angles, angleCount, params); if (!ok) - return false; + break; dim3 dimBlock(g_volBlockX, g_volBlockY); @@ -283,23 +287,26 @@ bool Par3DBP_Array(cudaPitchedPtr D_volumeData, // printf("Calling BP: %d, %dx%d, %dx%d to %p\n", i, dimBlock.x, dimBlock.y, dimGrid.x, dimGrid.y, (void*)D_volumeData.ptr); if (params.iRaysPerVoxelDim == 1) { if (dims.iVolZ == 1) { - dev_par3D_BP<1><<>>(D_volumeData.ptr, D_volumeData.pitch/sizeof(float), i, th, dims, fOutputScale); + dev_par3D_BP<1><<>>(D_volumeData.ptr, D_volumeData.pitch/sizeof(float), D_texObj, i, th, dims, fOutputScale); } else { - dev_par3D_BP<<>>(D_volumeData.ptr, D_volumeData.pitch/sizeof(float), i, th, dims, fOutputScale); + dev_par3D_BP<<>>(D_volumeData.ptr, D_volumeData.pitch/sizeof(float), D_texObj, i, th, dims, fOutputScale); } } else - dev_par3D_BP_SS<<>>(D_volumeData.ptr, D_volumeData.pitch/sizeof(float), i, th, dims, params.iRaysPerVoxelDim, fOutputScale); + dev_par3D_BP_SS<<>>(D_volumeData.ptr, D_volumeData.pitch/sizeof(float), D_texObj, i, th, dims, params.iRaysPerVoxelDim, fOutputScale); } // TODO: Consider not synchronizing here, if possible. - if (!checkCuda(cudaThreadSynchronize(), "cone_bp")) - return false; + ok = checkCuda(cudaThreadSynchronize(), "cone_bp"); + if (!ok) + break; angles = angles + angleCount; // printf("%f\n", toc(t)); } + cudaDestroyTextureObject(D_texObj); + return true; } -- cgit v1.2.1 From 3aa424220e6c64099b64ab15e6489d296431f542 Mon Sep 17 00:00:00 2001 From: Willem Jan Palenstijn Date: Mon, 22 Nov 2021 14:01:47 +0100 Subject: Replace texref by texobj in cone_bp --- cuda/3d/cone_bp.cu | 76 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 42 insertions(+), 34 deletions(-) diff --git a/cuda/3d/cone_bp.cu b/cuda/3d/cone_bp.cu index e265304..3092467 100644 --- a/cuda/3d/cone_bp.cu +++ b/cuda/3d/cone_bp.cu @@ -35,10 +35,6 @@ along with the ASTRA Toolbox. If not, see . #include -typedef texture texture3D; - -static texture3D gT_coneProjTexture; - namespace astraCUDA3d { static const unsigned int g_volBlockZ = 6; @@ -57,28 +53,35 @@ struct DevConeParams { __constant__ DevConeParams gC_C[g_MaxAngles]; -bool bindProjDataTexture(const cudaArray* array) +bool bindProjDataTexture(cudaArray* array, cudaTextureObject_t& texObj) { - cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc(); - - gT_coneProjTexture.addressMode[0] = cudaAddressModeBorder; - gT_coneProjTexture.addressMode[1] = cudaAddressModeBorder; - gT_coneProjTexture.addressMode[2] = cudaAddressModeBorder; - gT_coneProjTexture.filterMode = cudaFilterModeLinear; - gT_coneProjTexture.normalized = false; - - cudaBindTextureToArray(gT_coneProjTexture, array, channelDesc); - - // TODO: error value? - - return true; + cudaChannelFormatDesc channelDesc = + cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKindFloat); + + cudaResourceDesc resDesc; + memset(&resDesc, 0, sizeof(resDesc)); + resDesc.resType = cudaResourceTypeArray; + resDesc.res.array.array = array; + + cudaTextureDesc texDesc; + memset(&texDesc, 0, sizeof(texDesc)); + texDesc.addressMode[0] = cudaAddressModeBorder; + texDesc.addressMode[1] = cudaAddressModeBorder; + texDesc.addressMode[2] = cudaAddressModeBorder; + texDesc.filterMode = cudaFilterModeLinear; + texDesc.readMode = cudaReadModeElementType; + texDesc.normalizedCoords = 0; + + return checkCuda(cudaCreateTextureObject(&texObj, &resDesc, &texDesc, NULL), "cone_bp texture"); } //__launch_bounds__(32*16, 4) template -__global__ void dev_cone_BP(void* D_volData, unsigned int volPitch, int startAngle, - int angleOffset, const astraCUDA3d::SDimensions3D dims, +__global__ void dev_cone_BP(void* D_volData, unsigned int volPitch, + cudaTextureObject_t tex, + int startAngle, int angleOffset, + const astraCUDA3d::SDimensions3D dims, float fOutputScale) { float* volData = (float*)D_volData; @@ -133,7 +136,7 @@ __global__ void dev_cone_BP(void* D_volData, unsigned int volPitch, int startAng fr = __fdividef(1.0f, fDen); fU = fUNum * fr; fV = fVNum * fr; - float fVal = tex3D(gT_coneProjTexture, fU, fAngle, fV); + float fVal = tex3D(tex, fU, fAngle, fV); Z[idx] += fr*fr*fVal; fUNum += fCu.z; @@ -154,7 +157,7 @@ __global__ void dev_cone_BP(void* D_volData, unsigned int volPitch, int startAng // supersampling version -__global__ void dev_cone_BP_SS(void* D_volData, unsigned int volPitch, int startAngle, int angleOffset, const SDimensions3D dims, int iRaysPerVoxelDim, float fOutputScale) +__global__ void dev_cone_BP_SS(void* D_volData, unsigned int volPitch, cudaTextureObject_t tex, int startAngle, int angleOffset, const SDimensions3D dims, int iRaysPerVoxelDim, float fOutputScale) { float* volData = (float*)D_volData; @@ -220,7 +223,7 @@ __global__ void dev_cone_BP_SS(void* D_volData, unsigned int volPitch, int start const float fU = fUNum * fr; const float fV = fVNum * fr; - fVal += tex3D(gT_coneProjTexture, fU, fAngle, fV) * fr * fr; + fVal += tex3D(tex, fU, fAngle, fV) * fr * fr; fZs += fSubStep; } @@ -313,7 +316,8 @@ bool ConeBP_Array(cudaPitchedPtr D_volumeData, const SDimensions3D& dims, const SConeProjection* angles, const SProjectorParams3D& params) { - bindProjDataTexture(D_projArray); + cudaTextureObject_t D_texObj; + bindProjDataTexture(D_projArray, D_texObj); float fOutputScale; if (params.bFDKWeighting) { @@ -323,14 +327,16 @@ bool ConeBP_Array(cudaPitchedPtr D_volumeData, fOutputScale = params.fOutputScale * (params.fVolScaleX * params.fVolScaleY * params.fVolScaleZ); } + bool ok = true; + for (unsigned int th = 0; th < dims.iProjAngles; th += g_MaxAngles) { unsigned int angleCount = g_MaxAngles; if (th + angleCount > dims.iProjAngles) angleCount = dims.iProjAngles - th; - bool ok = transferConstants(angles, angleCount, params); + ok = transferConstants(angles, angleCount, params); if (!ok) - return false; + break; dim3 dimBlock(g_volBlockX, g_volBlockY); @@ -343,31 +349,33 @@ bool ConeBP_Array(cudaPitchedPtr D_volumeData, // printf("Calling BP: %d, %dx%d, %dx%d to %p\n", i, dimBlock.x, dimBlock.y, dimGrid.x, dimGrid.y, (void*)D_volumeData.ptr); if (params.bFDKWeighting) { if (dims.iVolZ == 1) { - dev_cone_BP<<>>(D_volumeData.ptr, D_volumeData.pitch/sizeof(float), i, th, dims, fOutputScale); + dev_cone_BP<<>>(D_volumeData.ptr, D_volumeData.pitch/sizeof(float), D_texObj, i, th, dims, fOutputScale); } else { - dev_cone_BP<<>>(D_volumeData.ptr, D_volumeData.pitch/sizeof(float), i, th, dims, fOutputScale); + dev_cone_BP<<>>(D_volumeData.ptr, D_volumeData.pitch/sizeof(float), D_texObj, i, th, dims, fOutputScale); } } else if (params.iRaysPerVoxelDim == 1) { if (dims.iVolZ == 1) { - dev_cone_BP<<>>(D_volumeData.ptr, D_volumeData.pitch/sizeof(float), i, th, dims, fOutputScale); + dev_cone_BP<<>>(D_volumeData.ptr, D_volumeData.pitch/sizeof(float), D_texObj, i, th, dims, fOutputScale); } else { - dev_cone_BP<<>>(D_volumeData.ptr, D_volumeData.pitch/sizeof(float), i, th, dims, fOutputScale); + dev_cone_BP<<>>(D_volumeData.ptr, D_volumeData.pitch/sizeof(float), D_texObj, i, th, dims, fOutputScale); } } else - dev_cone_BP_SS<<>>(D_volumeData.ptr, D_volumeData.pitch/sizeof(float), i, th, dims, params.iRaysPerVoxelDim, fOutputScale); + dev_cone_BP_SS<<>>(D_volumeData.ptr, D_volumeData.pitch/sizeof(float), D_texObj, i, th, dims, params.iRaysPerVoxelDim, fOutputScale); } // TODO: Consider not synchronizing here, if possible. - if (!checkCuda(cudaThreadSynchronize(), "cone_bp")) - return false; + ok = checkCuda(cudaThreadSynchronize(), "cone_bp"); + if (!ok) + break; angles = angles + angleCount; // printf("%f\n", toc(t)); } + cudaDestroyTextureObject(D_texObj); - return true; + return ok; } bool ConeBP(cudaPitchedPtr D_volumeData, -- cgit v1.2.1 From 64d42baf1bd15df8e0ecd00d89bb5c7be787cac2 Mon Sep 17 00:00:00 2001 From: Willem Jan Palenstijn Date: Mon, 22 Nov 2021 14:12:26 +0100 Subject: Remove unnecessary includes --- src/CudaFDKAlgorithm3D.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/CudaFDKAlgorithm3D.cpp b/src/CudaFDKAlgorithm3D.cpp index cc23df9..228da3e 100644 --- a/src/CudaFDKAlgorithm3D.cpp +++ b/src/CudaFDKAlgorithm3D.cpp @@ -37,9 +37,6 @@ along with the ASTRA Toolbox. If not, see . #include "astra/Logging.h" #include "astra/Filters.h" -#include "astra/cuda/3d/astra3d.h" -#include "astra/cuda/3d/util3d.h" - using namespace std; using namespace astraCUDA3d; -- cgit v1.2.1 From d4645875801d29402b1e3f2a42a5d9902a37a718 Mon Sep 17 00:00:00 2001 From: Willem Jan Palenstijn Date: Mon, 22 Nov 2021 14:14:55 +0100 Subject: De-duplicate 3D texture object creation --- cuda/3d/cone_bp.cu | 26 ++------------------------ cuda/3d/cone_fp.cu | 32 ++++++-------------------------- cuda/3d/par3d_bp.cu | 26 ++------------------------ cuda/3d/par3d_fp.cu | 31 ++++++------------------------- cuda/3d/util3d.cu | 22 ++++++++++++++++++++++ include/astra/cuda/3d/util3d.h | 2 ++ 6 files changed, 40 insertions(+), 99 deletions(-) diff --git a/cuda/3d/cone_bp.cu b/cuda/3d/cone_bp.cu index 3092467..41d781c 100644 --- a/cuda/3d/cone_bp.cu +++ b/cuda/3d/cone_bp.cu @@ -53,29 +53,6 @@ struct DevConeParams { __constant__ DevConeParams gC_C[g_MaxAngles]; -bool bindProjDataTexture(cudaArray* array, cudaTextureObject_t& texObj) -{ - cudaChannelFormatDesc channelDesc = - cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKindFloat); - - cudaResourceDesc resDesc; - memset(&resDesc, 0, sizeof(resDesc)); - resDesc.resType = cudaResourceTypeArray; - resDesc.res.array.array = array; - - cudaTextureDesc texDesc; - memset(&texDesc, 0, sizeof(texDesc)); - texDesc.addressMode[0] = cudaAddressModeBorder; - texDesc.addressMode[1] = cudaAddressModeBorder; - texDesc.addressMode[2] = cudaAddressModeBorder; - texDesc.filterMode = cudaFilterModeLinear; - texDesc.readMode = cudaReadModeElementType; - texDesc.normalizedCoords = 0; - - return checkCuda(cudaCreateTextureObject(&texObj, &resDesc, &texDesc, NULL), "cone_bp texture"); -} - - //__launch_bounds__(32*16, 4) template __global__ void dev_cone_BP(void* D_volData, unsigned int volPitch, @@ -317,7 +294,8 @@ bool ConeBP_Array(cudaPitchedPtr D_volumeData, const SProjectorParams3D& params) { cudaTextureObject_t D_texObj; - bindProjDataTexture(D_projArray, D_texObj); + if (!createTextureObject3D(D_projArray, D_texObj)) + return false; float fOutputScale; if (params.bFDKWeighting) { diff --git a/cuda/3d/cone_fp.cu b/cuda/3d/cone_fp.cu index e49ea24..2ef58ee 100644 --- a/cuda/3d/cone_fp.cu +++ b/cuda/3d/cone_fp.cu @@ -59,29 +59,6 @@ __constant__ float gC_DetVY[g_MaxAngles]; __constant__ float gC_DetVZ[g_MaxAngles]; -bool bindVolumeDataTexture(cudaArray* array, cudaTextureObject_t& texObj) -{ - cudaChannelFormatDesc channelDesc = - cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKindFloat); - - cudaResourceDesc resDesc; - memset(&resDesc, 0, sizeof(resDesc)); - resDesc.resType = cudaResourceTypeArray; - resDesc.res.array.array = array; - - cudaTextureDesc texDesc; - memset(&texDesc, 0, sizeof(texDesc)); - texDesc.addressMode[0] = cudaAddressModeBorder; - texDesc.addressMode[1] = cudaAddressModeBorder; - texDesc.addressMode[2] = cudaAddressModeBorder; - texDesc.filterMode = cudaFilterModeLinear; - texDesc.readMode = cudaReadModeElementType; - texDesc.normalizedCoords = 0; - - return checkCuda(cudaCreateTextureObject(&texObj, &resDesc, &texDesc, NULL), "cone_fp texture"); -} - - // x=0, y=1, z=2 struct DIR_X { __device__ float nSlices(const SDimensions3D& dims) const { return dims.iVolX; } @@ -474,12 +451,15 @@ bool ConeFP(cudaPitchedPtr D_volumeData, const SDimensions3D& dims, const SConeProjection* angles, const SProjectorParams3D& params) { - cudaTextureObject_t D_texObj; - // transfer volume to array cudaArray* cuArray = allocateVolumeArray(dims); transferVolumeToArray(D_volumeData, cuArray, dims); - bindVolumeDataTexture(cuArray, D_texObj); + + cudaTextureObject_t D_texObj; + if (!createTextureObject3D(cuArray, D_texObj)) { + cudaFreeArray(cuArray); + return false; + } bool ret; diff --git a/cuda/3d/par3d_bp.cu b/cuda/3d/par3d_bp.cu index 748086e..27d95fe 100644 --- a/cuda/3d/par3d_bp.cu +++ b/cuda/3d/par3d_bp.cu @@ -54,29 +54,6 @@ __constant__ DevPar3DParams gC_C[g_MaxAngles]; __constant__ float gC_scale[g_MaxAngles]; -static bool bindProjDataTexture(cudaArray* array, cudaTextureObject_t& texObj) -{ - cudaChannelFormatDesc channelDesc = - cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKindFloat); - - cudaResourceDesc resDesc; - memset(&resDesc, 0, sizeof(resDesc)); - resDesc.resType = cudaResourceTypeArray; - resDesc.res.array.array = array; - - cudaTextureDesc texDesc; - memset(&texDesc, 0, sizeof(texDesc)); - texDesc.addressMode[0] = cudaAddressModeBorder; - texDesc.addressMode[1] = cudaAddressModeBorder; - texDesc.addressMode[2] = cudaAddressModeBorder; - texDesc.filterMode = cudaFilterModeLinear; - texDesc.readMode = cudaReadModeElementType; - texDesc.normalizedCoords = 0; - - return checkCuda(cudaCreateTextureObject(&texObj, &resDesc, &texDesc, NULL), "par3d_bp texture"); -} - - template __global__ void dev_par3D_BP(void* D_volData, unsigned int volPitch, cudaTextureObject_t tex, int startAngle, int angleOffset, const SDimensions3D dims, float fOutputScale) { @@ -261,7 +238,8 @@ bool Par3DBP_Array(cudaPitchedPtr D_volumeData, const SProjectorParams3D& params) { cudaTextureObject_t D_texObj; - bindProjDataTexture(D_projArray, D_texObj); + if (!createTextureObject3D(D_projArray, D_texObj)) + return false; float fOutputScale = params.fOutputScale * params.fVolScaleX * params.fVolScaleY * params.fVolScaleZ; diff --git a/cuda/3d/par3d_fp.cu b/cuda/3d/par3d_fp.cu index cae75f1..b2178ec 100644 --- a/cuda/3d/par3d_fp.cu +++ b/cuda/3d/par3d_fp.cu @@ -59,29 +59,6 @@ __constant__ float gC_DetVY[g_MaxAngles]; __constant__ float gC_DetVZ[g_MaxAngles]; -static bool bindVolumeDataTexture(cudaArray* array, cudaTextureObject_t& texObj) -{ - cudaChannelFormatDesc channelDesc = - cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKindFloat); - - cudaResourceDesc resDesc; - memset(&resDesc, 0, sizeof(resDesc)); - resDesc.resType = cudaResourceTypeArray; - resDesc.res.array.array = array; - - cudaTextureDesc texDesc; - memset(&texDesc, 0, sizeof(texDesc)); - texDesc.addressMode[0] = cudaAddressModeBorder; - texDesc.addressMode[1] = cudaAddressModeBorder; - texDesc.addressMode[2] = cudaAddressModeBorder; - texDesc.filterMode = cudaFilterModeLinear; - texDesc.readMode = cudaReadModeElementType; - texDesc.normalizedCoords = 0; - - return checkCuda(cudaCreateTextureObject(&texObj, &resDesc, &texDesc, NULL), "par3d_fp texture"); -} - - // x=0, y=1, z=2 struct DIR_X { __device__ float nSlices(const SDimensions3D& dims) const { return dims.iVolX; } @@ -574,12 +551,16 @@ bool Par3DFP(cudaPitchedPtr D_volumeData, const SDimensions3D& dims, const SPar3DProjection* angles, const SProjectorParams3D& params) { - cudaTextureObject_t D_texObj; // transfer volume to array cudaArray* cuArray = allocateVolumeArray(dims); transferVolumeToArray(D_volumeData, cuArray, dims); - bindVolumeDataTexture(cuArray, D_texObj); + + cudaTextureObject_t D_texObj; + if (!createTextureObject3D(cuArray, D_texObj)) { + cudaFreeArray(cuArray); + return false; + } bool ret; diff --git a/cuda/3d/util3d.cu b/cuda/3d/util3d.cu index 71b5668..3dc915d 100644 --- a/cuda/3d/util3d.cu +++ b/cuda/3d/util3d.cu @@ -378,6 +378,28 @@ bool transferHostProjectionsToArray(const float *projData, cudaArray* array, con return checkCuda(cudaMemcpy3D(&p), "transferHostProjectionsToArray 3D"); } +bool createTextureObject3D(cudaArray* array, cudaTextureObject_t& texObj) +{ + cudaChannelFormatDesc channelDesc = + cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKindFloat); + + cudaResourceDesc resDesc; + memset(&resDesc, 0, sizeof(resDesc)); + resDesc.resType = cudaResourceTypeArray; + resDesc.res.array.array = array; + + cudaTextureDesc texDesc; + memset(&texDesc, 0, sizeof(texDesc)); + texDesc.addressMode[0] = cudaAddressModeBorder; + texDesc.addressMode[1] = cudaAddressModeBorder; + texDesc.addressMode[2] = cudaAddressModeBorder; + texDesc.filterMode = cudaFilterModeLinear; + texDesc.readMode = cudaReadModeElementType; + texDesc.normalizedCoords = 0; + + return checkCuda(cudaCreateTextureObject(&texObj, &resDesc, &texDesc, NULL), "createTextureObject3D"); +} + float dotProduct3D(cudaPitchedPtr data, unsigned int x, unsigned int y, diff --git a/include/astra/cuda/3d/util3d.h b/include/astra/cuda/3d/util3d.h index 9fa254d..210d944 100644 --- a/include/astra/cuda/3d/util3d.h +++ b/include/astra/cuda/3d/util3d.h @@ -60,6 +60,8 @@ bool zeroVolumeArray(cudaArray* array, const SDimensions3D& dims); cudaArray* allocateProjectionArray(const SDimensions3D& dims); cudaArray* allocateVolumeArray(const SDimensions3D& dims); +bool createTextureObject3D(cudaArray* array, cudaTextureObject_t& texObj); + float dotProduct3D(cudaPitchedPtr data, unsigned int x, unsigned int y, unsigned int z); int calcNextPowerOfTwo(int _iValue); -- cgit v1.2.1 From 887f3c919c02bbbfc3c757478742f6961a30963a Mon Sep 17 00:00:00 2001 From: Willem Jan Palenstijn Date: Mon, 22 Nov 2021 14:28:33 +0100 Subject: Replace texref by texobj in fan_bp --- cuda/2d/fan_bp.cu | 76 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 49 insertions(+), 27 deletions(-) diff --git a/cuda/2d/fan_bp.cu b/cuda/2d/fan_bp.cu index 2068d03..ea2d23b 100644 --- a/cuda/2d/fan_bp.cu +++ b/cuda/2d/fan_bp.cu @@ -58,24 +58,35 @@ struct DevFanParams { __constant__ DevFanParams gC_C[g_MaxAngles]; -static bool bindProjDataTexture(float* data, unsigned int pitch, unsigned int width, unsigned int height, cudaTextureAddressMode mode = cudaAddressModeBorder) +static bool bindProjDataTexture(float* data, cudaTextureObject_t& texObj, unsigned int pitch, unsigned int width, unsigned int height, cudaTextureAddressMode mode = cudaAddressModeBorder) { - cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc(); - - gT_FanProjTexture.addressMode[0] = mode; - gT_FanProjTexture.addressMode[1] = mode; - gT_FanProjTexture.filterMode = cudaFilterModeLinear; - gT_FanProjTexture.normalized = false; - - cudaBindTexture2D(0, gT_FanProjTexture, (const void*)data, channelDesc, width, height, sizeof(float)*pitch); - - // TODO: error value? - - return true; + 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, unsigned int startAngle, const SDimensions dims, float fOutputScale) +__global__ void devFanBP(float* D_volData, unsigned int volPitch, cudaTextureObject_t tex, unsigned int startAngle, const SDimensions dims, float fOutputScale) { const int relX = threadIdx.x; const int relY = threadIdx.y; @@ -115,7 +126,7 @@ __global__ void devFanBP(float* D_volData, unsigned int volPitch, unsigned int s const float fr = __fdividef(1.0f, fDen); const float fT = fNum * fr; - fVal += tex2D(gT_FanProjTexture, fT, fA) * (FBPWEIGHT ? fr * fr : fr); + fVal += tex2D(tex, fT, fA) * (FBPWEIGHT ? fr * fr : fr); fA += 1.0f; } @@ -123,7 +134,7 @@ __global__ void devFanBP(float* D_volData, unsigned int volPitch, unsigned int s } // supersampling version -__global__ void devFanBP_SS(float* D_volData, unsigned int volPitch, unsigned int startAngle, const SDimensions dims, float fOutputScale) +__global__ void devFanBP_SS(float* D_volData, unsigned int volPitch, cudaTextureObject_t tex, unsigned int startAngle, const SDimensions dims, float fOutputScale) { const int relX = threadIdx.x; const int relY = threadIdx.y; @@ -169,7 +180,7 @@ __global__ void devFanBP_SS(float* D_volData, unsigned int volPitch, unsigned in const float fr = __fdividef(1.0f, fDen); const float fT = fNum * fr; - fVal += tex2D(gT_FanProjTexture, fT, fA) * fr; + fVal += tex2D(tex, fT, fA) * fr; fY -= fSubStep; } fX += fSubStep; @@ -184,7 +195,7 @@ __global__ void devFanBP_SS(float* D_volData, unsigned int volPitch, unsigned in // BP specifically for SART. // It includes (free) weighting with voxel weight. // It assumes the proj texture is set up _without_ padding, unlike regular BP. -__global__ void devFanBP_SART(float* D_volData, unsigned int volPitch, const SDimensions dims, float fOutputScale) +__global__ void devFanBP_SART(float* D_volData, unsigned int volPitch, cudaTextureObject_t tex, const SDimensions dims, float fOutputScale) { const int relX = threadIdx.x; const int relY = threadIdx.y; @@ -213,7 +224,7 @@ __global__ void devFanBP_SART(float* D_volData, unsigned int volPitch, const SDi const float fr = __fdividef(1.0f, fDen); const float fT = fNum * fr; // NB: The scale constant in devBP is cancelled out by the SART weighting - const float fVal = tex2D(gT_FanProjTexture, fT, 0.5f); + const float fVal = tex2D(tex, fT, 0.5f); volData[Y*volPitch+X] += fVal * fOutputScale; } @@ -303,7 +314,8 @@ bool FanBP_internal(float* D_volumeData, unsigned int volumePitch, { assert(dims.iProjAngles <= g_MaxAngles); - bindProjDataTexture(D_projData, projPitch, dims.iProjDets, dims.iProjAngles); + cudaTextureObject_t D_texObj; + bindProjDataTexture(D_projData, D_texObj, projPitch, dims.iProjDets, dims.iProjAngles); bool ok = transferConstants(angles, dims.iProjAngles, false); if (!ok) @@ -318,15 +330,17 @@ bool FanBP_internal(float* D_volumeData, unsigned int volumePitch, for (unsigned int i = 0; i < dims.iProjAngles; i += g_anglesPerBlock) { if (dims.iRaysPerPixelDim > 1) - devFanBP_SS<<>>(D_volumeData, volumePitch, i, dims, fOutputScale); + devFanBP_SS<<>>(D_volumeData, volumePitch, D_texObj, i, dims, fOutputScale); else - devFanBP<<>>(D_volumeData, volumePitch, i, dims, fOutputScale); + devFanBP<<>>(D_volumeData, volumePitch, D_texObj, i, dims, fOutputScale); } ok = checkCuda(cudaStreamSynchronize(stream), "FanBP"); cudaStreamDestroy(stream); + cudaDestroyTextureObject(D_texObj); + return ok; } @@ -337,7 +351,8 @@ bool FanBP_FBPWeighted_internal(float* D_volumeData, unsigned int volumePitch, { assert(dims.iProjAngles <= g_MaxAngles); - bindProjDataTexture(D_projData, projPitch, dims.iProjDets, dims.iProjAngles); + cudaTextureObject_t D_texObj; + bindProjDataTexture(D_projData, D_texObj, projPitch, dims.iProjDets, dims.iProjAngles); bool ok = transferConstants(angles, dims.iProjAngles, true); if (!ok) @@ -351,13 +366,15 @@ bool FanBP_FBPWeighted_internal(float* D_volumeData, unsigned int volumePitch, cudaStreamCreate(&stream); for (unsigned int i = 0; i < dims.iProjAngles; i += g_anglesPerBlock) { - devFanBP<<>>(D_volumeData, volumePitch, i, dims, fOutputScale); + devFanBP<<>>(D_volumeData, volumePitch, D_texObj, i, dims, fOutputScale); } ok = checkCuda(cudaStreamSynchronize(stream), "FanBP_FBPWeighted"); cudaStreamDestroy(stream); + cudaDestroyTextureObject(D_texObj); + return ok; } @@ -369,7 +386,8 @@ bool FanBP_SART(float* D_volumeData, unsigned int volumePitch, float fOutputScale) { // only one angle - bindProjDataTexture(D_projData, projPitch, dims.iProjDets, 1, cudaAddressModeClamp); + cudaTextureObject_t D_texObj; + bindProjDataTexture(D_projData, D_texObj, projPitch, dims.iProjDets, 1, cudaAddressModeClamp); bool ok = transferConstants(angles + angle, 1, false); if (!ok) @@ -379,9 +397,13 @@ bool FanBP_SART(float* D_volumeData, unsigned int volumePitch, dim3 dimGrid((dims.iVolWidth+g_blockSlices-1)/g_blockSlices, (dims.iVolHeight+g_blockSliceSize-1)/g_blockSliceSize); - devFanBP_SART<<>>(D_volumeData, volumePitch, dims, fOutputScale); + devFanBP_SART<<>>(D_volumeData, volumePitch, D_texObj, dims, fOutputScale); + + bool ok = checkCuda(cudaThreadSynchronize(), "FanBP_SART"); - return checkCuda(cudaThreadSynchronize(), "FanBP_SART"); + cudaDestroyTextureObject(D_texObj); + + return ok; } bool FanBP(float* D_volumeData, unsigned int volumePitch, -- cgit v1.2.1 From c11ee1cffd53bdf4530eb4381bf484cd1a1ce9cb Mon Sep 17 00:00:00 2001 From: Willem Jan Palenstijn Date: Mon, 22 Nov 2021 14:28:37 +0100 Subject: Replace texref by texobj in par_bp --- cuda/2d/par_bp.cu | 69 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 44 insertions(+), 25 deletions(-) diff --git a/cuda/2d/par_bp.cu b/cuda/2d/par_bp.cu index d7c3ab0..ee94de8 100644 --- a/cuda/2d/par_bp.cu +++ b/cuda/2d/par_bp.cu @@ -51,24 +51,35 @@ __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, unsigned int pitch, unsigned int width, unsigned int height, cudaTextureAddressMode mode = cudaAddressModeBorder) +static bool bindProjDataTexture(float* data, cudaTextureObject_t& texObj, unsigned int pitch, unsigned int width, unsigned int height, cudaTextureAddressMode mode = cudaAddressModeBorder) { - cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc(); - - gT_projTexture.addressMode[0] = mode; - gT_projTexture.addressMode[1] = mode; - gT_projTexture.filterMode = cudaFilterModeLinear; - gT_projTexture.normalized = false; - - cudaBindTexture2D(0, gT_projTexture, (const void*)data, channelDesc, width, height, sizeof(float)*pitch); - - // TODO: error value? - - return true; + 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, unsigned int startAngle, const SDimensions dims, float fOutputScale) +__global__ void devBP(float* D_volData, unsigned int volPitch, cudaTextureObject_t tex, unsigned int startAngle, const SDimensions dims, float fOutputScale) { const int relX = threadIdx.x; const int relY = threadIdx.y; @@ -98,7 +109,7 @@ __global__ void devBP(float* D_volData, unsigned int volPitch, unsigned int star const float scale = gC_angle_scale[angle]; const float fT = fX * scaled_cos_theta - fY * scaled_sin_theta + TOffset; - fVal += tex2D(gT_projTexture, fT, fA) * scale; + fVal += tex2D(tex, fT, fA) * scale; fA += 1.0f; } @@ -106,7 +117,7 @@ __global__ void devBP(float* D_volData, unsigned int volPitch, unsigned int star } // supersampling version -__global__ void devBP_SS(float* D_volData, unsigned int volPitch, unsigned int startAngle, const SDimensions dims, float fOutputScale) +__global__ void devBP_SS(float* D_volData, unsigned int volPitch, cudaTextureObject_t tex, unsigned int startAngle, const SDimensions dims, float fOutputScale) { const int relX = threadIdx.x; const int relY = threadIdx.y; @@ -145,7 +156,7 @@ __global__ void devBP_SS(float* D_volData, unsigned int volPitch, unsigned int s float fTy = fT; fT += fSubStep * cos_theta; for (int iSubY = 0; iSubY < dims.iRaysPerPixelDim; ++iSubY) { - fVal += tex2D(gT_projTexture, fTy, fA) * scale; + fVal += tex2D(tex, fTy, fA) * scale; fTy -= fSubStep * sin_theta; } } @@ -155,7 +166,7 @@ __global__ void devBP_SS(float* D_volData, unsigned int volPitch, unsigned int s volData[Y*volPitch+X] += fVal * fOutputScale; } -__global__ void devBP_SART(float* D_volData, unsigned int volPitch, float offset, float angle_sin, float angle_cos, const SDimensions dims, float fOutputScale) +__global__ void devBP_SART(float* D_volData, unsigned int volPitch, cudaTextureObject_t tex, float offset, float angle_sin, float angle_cos, const SDimensions dims, float fOutputScale) { const int relX = threadIdx.x; const int relY = threadIdx.y; @@ -170,7 +181,7 @@ __global__ void devBP_SART(float* D_volData, unsigned int volPitch, float offset const float fY = ( Y - 0.5f*dims.iVolHeight + 0.5f ); const float fT = fX * angle_cos - fY * angle_sin + offset; - const float fVal = tex2D(gT_projTexture, fT, 0.5f); + const float fVal = tex2D(tex, fT, 0.5f); // NB: The 'scale' constant in devBP is cancelled out by the SART weighting @@ -190,7 +201,8 @@ bool BP_internal(float* D_volumeData, unsigned int volumePitch, float* angle_offset = new float[dims.iProjAngles]; float* angle_scale = new float[dims.iProjAngles]; - bindProjDataTexture(D_projData, projPitch, dims.iProjDets, 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; @@ -227,15 +239,17 @@ bool BP_internal(float* D_volumeData, unsigned int volumePitch, for (unsigned int i = 0; i < dims.iProjAngles; i += g_anglesPerBlock) { if (dims.iRaysPerPixelDim > 1) - devBP_SS<<>>(D_volumeData, volumePitch, i, dims, fOutputScale); + devBP_SS<<>>(D_volumeData, volumePitch, D_texObj, i, dims, fOutputScale); else - devBP<<>>(D_volumeData, volumePitch, i, dims, fOutputScale); + devBP<<>>(D_volumeData, volumePitch, D_texObj, i, dims, fOutputScale); } bool ok = checkCuda(cudaStreamSynchronize(stream), "par_bp"); cudaStreamDestroy(stream); + cudaDestroyTextureObject(D_texObj); + return ok; } @@ -269,7 +283,8 @@ bool BP_SART(float* D_volumeData, unsigned int volumePitch, // Only one angle. // We need to Clamp to the border pixels instead of to zero, because // SART weights with ray length. - bindProjDataTexture(D_projData, projPitch, dims.iProjDets, 1, cudaAddressModeClamp); + cudaTextureObject_t D_texObj; + bindProjDataTexture(D_projData, D_texObj, projPitch, dims.iProjDets, 1, cudaAddressModeClamp); double d = angles[angle].fDetUX * angles[angle].fRayY - angles[angle].fDetUY * angles[angle].fRayX; float angle_scaled_cos = angles[angle].fRayY / d; @@ -282,9 +297,13 @@ bool BP_SART(float* D_volumeData, unsigned int volumePitch, dim3 dimGrid((dims.iVolWidth+g_blockSlices-1)/g_blockSlices, (dims.iVolHeight+g_blockSliceSize-1)/g_blockSliceSize); - devBP_SART<<>>(D_volumeData, volumePitch, angle_offset, angle_scaled_sin, angle_scaled_cos, dims, fOutputScale); + devBP_SART<<>>(D_volumeData, volumePitch, D_texObj, angle_offset, angle_scaled_sin, angle_scaled_cos, dims, fOutputScale); + + bool ok = checkCuda(cudaThreadSynchronize(), "BP_SART"); - return checkCuda(cudaThreadSynchronize(), "BP_SART"); + cudaDestroyTextureObject(D_texObj); + + return ok; } -- cgit v1.2.1 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 From 7cad7b813838ed2ddb65a4c9ea1c08c625c50043 Mon Sep 17 00:00:00 2001 From: Willem Jan Palenstijn Date: Mon, 22 Nov 2021 14:44:50 +0100 Subject: Fix memleak in error handling --- cuda/2d/fan_fp.cu | 2 +- cuda/2d/par_fp.cu | 2 +- cuda/2d/util.cu | 17 +++++++++++++---- include/astra/cuda/2d/util.h | 2 +- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/cuda/2d/fan_fp.cu b/cuda/2d/fan_fp.cu index ed99e08..5aa9674 100644 --- a/cuda/2d/fan_fp.cu +++ b/cuda/2d/fan_fp.cu @@ -201,7 +201,7 @@ bool FanFP_internal(float* D_volumeData, unsigned int volumePitch, cudaArray* D_dataArray; cudaTextureObject_t D_texObj; - if (!createTextureObject2D(D_volumeData, D_dataArray, D_texObj, volumePitch, dims.iVolWidth, dims.iVolHeight)) + if (!createArrayAndTextureObject2D(D_volumeData, D_dataArray, D_texObj, volumePitch, dims.iVolWidth, dims.iVolHeight)) return false; // transfer angles to constant memory diff --git a/cuda/2d/par_fp.cu b/cuda/2d/par_fp.cu index 6035e0c..3d9e842 100644 --- a/cuda/2d/par_fp.cu +++ b/cuda/2d/par_fp.cu @@ -238,7 +238,7 @@ bool FP_simple_internal(float* D_volumeData, unsigned int volumePitch, cudaArray* D_dataArray; cudaTextureObject_t D_texObj; - if (!createTextureObject2D(D_volumeData, D_dataArray, D_texObj, volumePitch, dims.iVolWidth, dims.iVolHeight)) + if (!createArrayAndTextureObject2D(D_volumeData, D_dataArray, D_texObj, volumePitch, dims.iVolWidth, dims.iVolHeight)) return false; diff --git a/cuda/2d/util.cu b/cuda/2d/util.cu index 2ad3c0f..4a58880 100644 --- a/cuda/2d/util.cu +++ b/cuda/2d/util.cu @@ -126,7 +126,7 @@ 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) +bool createArrayAndTextureObject2D(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. @@ -135,8 +135,12 @@ bool createTextureObject2D(float* data, cudaArray*& dataArray, cudaTextureObject 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); + if (!checkCuda(cudaMallocArray(&dataArray, &channelDesc, width, height), "createTextureObject2D malloc")) + return false; + if (!checkCuda(cudaMemcpy2DToArray(dataArray, 0, 0, data, pitch*sizeof(float), width*sizeof(float), height, cudaMemcpyDeviceToDevice), "createTextureObject2D memcpy")) { + cudaFreeArray(dataArray); + return false; + } cudaResourceDesc resDesc; memset(&resDesc, 0, sizeof(resDesc)); @@ -153,7 +157,12 @@ bool createTextureObject2D(float* data, cudaArray*& dataArray, cudaTextureObject texObj = 0; - return checkCuda(cudaCreateTextureObject(&texObj, &resDesc, &texDesc, NULL), "createTextureObject2D"); + if (!checkCuda(cudaCreateTextureObject(&texObj, &resDesc, &texDesc, NULL), "createTextureObject2D")) { + cudaFreeArray(dataArray); + return false; + } + + return true; } bool createTextureObjectPitch2D(float* data, cudaTextureObject_t& texObj, unsigned int pitch, unsigned int width, unsigned int height, cudaTextureAddressMode mode) diff --git a/include/astra/cuda/2d/util.h b/include/astra/cuda/2d/util.h index cacf0a9..2cf0639 100644 --- a/include/astra/cuda/2d/util.h +++ b/include/astra/cuda/2d/util.h @@ -66,7 +66,7 @@ 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 createArrayAndTextureObject2D(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); -- cgit v1.2.1