calculate optimal pitch value

Hi forum,

I am calculating the pitch value for the device i am working on in the following manner:


      //pull out the device info for the optimal texture pitch
      cl_uint deviceMemBaseAddressAlignmentInBits;      
            
      unsigned int deviceMemBaseAddressAlignmentInBytes;

      //get the device info
      //describes the alignment in bits of the base address of any allocated
      //memory object
      errNum = clGetDeviceInfo(cdDevices[0],
			       CL_DEVICE_MEM_BASE_ADDR_ALIGN,
			       sizeof(deviceMemBaseAddressAlignmentInBits),
			       &deviceMemBaseAddressAlignmentInBits,
			       NULL);

      if( errNum != CL_SUCCESS )
      {
	 std::cerr << "Device Memory Base Address Alignment Extraction is not successful." << std::endl;
	 return NULL;
      }
      
      //convert bits to bytes
      deviceMemBaseAddressAlignmentInBytes = deviceMemBaseAddressAlignmentInBits / 8;
      
      unsigned int remainingAlignmentBytes = (DIM*sizeof(cl_float2)) % deviceMemBaseAddressAlignmentInBytes;
      
      if( remainingAlignmentBytes != 0 )
	 tPitch = (DIM*sizeof(cl_float2)) + (deviceMemBaseAddressAlignmentInBytes - remainingAlignmentBytes);
      else
	 //returns basically the image row pitch
	 tPitch =  (DIM*sizeof(cl_float2)); // no additional bytes required.


I need your feed-back over this issue to make sure that i am doing it right.

Thanks

You computation is right. Two remarks, though:

  1. CL_DEVICE_MEM_BASE_ADDR_ALIGN gives the alignment for the largest OpenCL built-in type. This is useful if you intend to store various built-in types at different times in the same buffer because it guarantees that data will always be aligned.

If you know that your buffer will contain only float2 data, you just have to align on sizeof(float2)=8.

  1. You can make your computation simpler:

tPitch = ((DIM*sizeof(cl_float2) + deviceMemBaseAddressAlignmentInBytes - 1) / deviceMemBaseAddressAlignmentInBytes) * deviceMemBaseAddressAlignmentInBytes;

or even simpler if deviceMemBaseAddressAlignmentInBytes is a power of 2:


tPitch = (DIM*sizeof(cl_float2) + deviceMemBaseAddressAlignmentInBytes - 1) % ~(deviceMemBaseAddressAlignmentInBytes - 1);