summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWillem Jan Palenstijn <Willem.Jan.Palenstijn@cwi.nl>2021-11-22 14:43:07 +0100
committerWillem Jan Palenstijn <wjp@usecode.org>2021-11-26 12:09:09 +0100
commitbe2da43a560a7241c56e727fb481f1389e9f7fdf (patch)
tree318df874ff0efb63d01749006f09318289d0844b
parentc11ee1cffd53bdf4530eb4381bf484cd1a1ce9cb (diff)
downloadastra-be2da43a560a7241c56e727fb481f1389e9f7fdf.tar.gz
astra-be2da43a560a7241c56e727fb481f1389e9f7fdf.tar.bz2
astra-be2da43a560a7241c56e727fb481f1389e9f7fdf.tar.xz
astra-be2da43a560a7241c56e727fb481f1389e9f7fdf.zip
De-duplicate 2D texture object creation
-rw-r--r--cuda/2d/fan_bp.cu51
-rw-r--r--cuda/2d/fan_fp.cu33
-rw-r--r--cuda/2d/par_bp.cu37
-rw-r--r--cuda/2d/par_fp.cu39
-rw-r--r--cuda/2d/util.cu60
-rw-r--r--include/astra/cuda/2d/util.h4
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<bool FBPWEIGHT>
__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<<<dimGrid, dimBlock>>>(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 <unsigned int blockSize>
__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,