Due to the nature of the algorithm I am programming I need to write/fill a 3D matrix with some specific maths and then read from that matrix (in a separate kernel) as a 3D linearly interpolated texture.
As texture is a reading mode, I am assuming I can somehow write in the global memory bind to the texture, and in a separate read from it, without the need of double memory and copying the values from the write to the read matrix. However I don't seem to figure out how to do this.
- How can I use 3D texture memory as read and write (in separate kernels) ?
My problem is that I don't know how to define this global read/write array. In the sample below, I have created a 3D texture, but this is using code with cudaExtent
and cudaArray
. But I don't seem to be able to use this types to write on them, neither I seem to be able to create them with float*
or the likes.
I may not be able to do this and need a memcpy
somewhere in the middle, but as these arrays are generally big, I'd like to save memory.
Sample code (doesn't compile, but clearly defines the structure of what I am trying to do). Uses 100x100x100 3D memory as default because yes.
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <cuda_runtime_api.h>
#include <cuda.h>
#define MAXTREADS 1024
cudaError_t addWithCuda(int *c, const int *a, const int *b, unsigned int size);
texture<float, cudaTextureType3D, cudaReadModeElementType> tex;
__global__ void readKernel(float* imageend )
{
int indY = blockIdx.y * blockDim.y + threadIdx.y;
int indX = blockIdx.x * blockDim.x + threadIdx.x;
int indZ = blockIdx.z * blockDim.z + threadIdx.z;
//Make sure we dont go out of bounds
size_t idx = indZ * 100 * 100 + indY * 100 + indX;
if (indX >= 100 | indY >= 100 | indZ >= 100)
return;
imageend[idx] = tex3D(tex, indX + 0.5, indY + 0.5, indZ + 0.5);
}
__global__ void writeKernel(float* imageaux){
int indY = blockIdx.y * blockDim.y + threadIdx.y;
int indX = blockIdx.x * blockDim.x + threadIdx.x;
int indZ = blockIdx.z * blockDim.z + threadIdx.z;
//Make sure we dont go out of bounds
size_t idx = indZ * 100 * 100 + indY * 100 + indX;
if (indX >= 100 | indY >= 100 | indZ >= 100)
return;
imageaux[idx] = (float)idx;
}
int main()
{
cudaArray *d_image_aux= 0;
const cudaExtent extent = make_cudaExtent(100, 100, 100);
cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc<float>();
cudaMalloc3DArray(&d_image_aux, &channelDesc, extent);
// Configure texture options
tex.normalized = false;
tex.filterMode = cudaFilterModeLinear;
tex.addressMode[0] = cudaAddressModeBorder;
tex.addressMode[1] = cudaAddressModeBorder;
tex.addressMode[2] = cudaAddressModeBorder;
cudaBindTextureToArray(tex, d_image_aux, channelDesc);
float *d_image_end = 0;
size_t num_bytes = 100 * 100 * 100 * sizeof(float);
cudaMalloc((void**)&d_image_end, num_bytes);
cudaMemset(d_image_end, 0, num_bytes);
int divx, divy, divz; //Irrelevant for the demo, important for the main code
divx = 32;
divy = 32;
divz = 1;
dim3 grid((100 + divx - 1) / divx,
(100 + divy - 1) / divy,
(100 + divz - 1) / divz);
dim3 block(divx, divy, divz);
// Kernels
writeKernel << <grid, block >> >(d_image_aux);
readKernel << <grid, block >> >(d_image_end);
cudaUnbindTexture(tex);
cudaFree(d_image_aux);
cudaFree(d_image_end);
return 0;
}
NOTE: I am aware that I can not write "interpolated" or whatever that would be. The write operation will always be in integer indexes, while the read operation needs to use trilinear interpolation.