How to read back a CUDA Texture for testing?

前端 未结 2 1305
再見小時候
再見小時候 2020-12-15 01:03

Ok, so far, I can create an array on the host computer (of type float), and copy it to the gpu, then bring it back to the host as another array (to test if the copy was succ

2条回答
  •  伪装坚强ぢ
    2020-12-15 01:57

    Briefly:

    ------------- in your main.cu ---------------------------------------------------------------------------------------

    -1. Define the texture as a globlal variable

    
           texture refTexture; // global variable !
           // meaning: address the texture with (x,y) (2D) and get an unsinged int
    

    In the main function:

    -2. Use arrays combined with texture

        cudaArray* myArray; // declar.
        // ask for memory
        cudaMallocArray (   &myArray,
    &refTex.channelDesc, /* with this you don't need to fill a channel descriptor */ width,
    height);

    -3. copy data from CPU to GPU (to the array)

     cudaMemcpyToArray ( arrayCudaEntrada, // destination: the array
    0, 0, // offsets sourceData, // pointer uint* widthheightsizeof(uint), // total amount of bytes to be copied cudaMemcpyHostToDevice);

    -4. bind texture and array

        cudaBindTextureToArray( refTex,arrayCudaEntrada)
    

    -5. change some parameters in the texture


    refTextura_In.normalized = false; // don't automatically convert fetched data to [0,1[ refTextura_In.addressMode[0] = cudaAddressModeClamp; // if my indexing is out of bounds: automatically use a valid indexing (0 if negative index, last if too great index) refTextura_In.addressMode[1] = cudaAddressModeClamp;

    ---------- in the kernel --------------------------------------------------------

        // find out indexes (f,c) to process by this thread
         uint f = (blockIdx.x * blockDim.x) + threadIdx.x;
         uint c = (blockIdx.y * blockDim.y) + threadIdx.y;

      // this is curious and necessary: indexes for reading from a texture
      // are floats !. Even if you are certain to access (4,5) you have
      // match the "center" this is (4.5, 5.5)
      uint read = tex2D( refTex, c+0.5f, f+0.5f); // texRef is a global variable
    

    Now You process read and write the results to other zone of the device global memory, not to the texture itself !

提交回复
热议问题