I’m relatively new with CUDA. I don’t know may be my question is too naive but I spent all the day trying to solve this issue!
First of all there is a very low amount of information about the Driver API functions and how to use it. even the programming guide give a very low amount of information about some of them and the others remain without any information at all!
I’m trying to copy a 2D array from my host memory to a 2D cuda array and associate it with a 2D Texture. There is no error returned from any of the functions but when I’m trying to retrieve data back from the texture it returns just zero. I tried everything but it’s always zero.
In the cu file I have the following code:
[codebox]
texture<float, 2, cudaReadModeElementType> tex; // 2D texture
global void
GetTextureValue(float x,float y,float* sample)
{
(*sample) = tex2D(tex, x,y);
}
[/codebox]
In my CPP file the code is:
[codebox]
CUresult CudaRes;
//Allocate CUDA Array
CUDA_ARRAY_DESCRIPTOR ArrDesc;
ArrDesc.Format = CU_AD_FORMAT_FLOAT;
ArrDesc.NumChannels = 1;
ArrDesc.Width = 200;
ArrDesc.Height = 200;
CudaRes= cuArrayCreate(&GPUArr, &ArrDesc);
CUDA_MEMCPY2D copyParam;
memset(©Param, 0, sizeof(copyParam));
float* Data=new float[400];
for(int i=0;i<400;i++)
Data[i]=i+1;
memset(©Param, 0, sizeof(copyParam));
copyParam.dstMemoryType = CU_MEMORYTYPE_ARRAY;
copyParam.dstArray = GPUArr;
copyParam.srcMemoryType = CU_MEMORYTYPE_HOST;
copyParam.srcHost = (void*)Data;
copyParam.srcPitch = 200 * sizeof(float);
copyParam.WidthInBytes = copyParam.srcPitch;
copyParam.Height = 200;
CudaRes= cuMemcpy2D(©Param);
CudaRes=cuModuleGetTexRef(&DataTex, cuModule, “tex”);
CudaRes=cuTexRefSetAddressMode(DataTex, 0, CU_TR_ADDRESS_MODE_WRAP);
CudaRes=cuTexRefSetAddressMode(DataTex, 1, CU_TR_ADDRESS_MODE_WRAP);
CudaRes=cuTexRefSetFilterMode(DataTex, CU_TR_FILTER_MODE_LINEAR);
CudaRes=cuTexRefSetFormat(DataTex, CU_AD_FORMAT_FLOAT, 1);
CudaRes=cuTexRefSetArray(DataTex, GPUArr, CU_TRSA_OVERRIDE_FORMAT);
CudaRes=cuFuncSetBlockShape(GetTextureValue,1,1,1);
CudaRes= cuMemAlloc(&TextureLookupOutput,sizeof(float));
for(int i=0;i<200;i++)
{
for(int j=0;j<200;j++)
{
int offset = 0;
ALIGN_OFFSET(offset,__alignof(float));
CudaRes=cuParamSetf(GetTextureValue, offset, i);
offset += sizeof(float);
ALIGN_OFFSET(offset,__alignof(float));
CudaRes=cuParamSetf(GetTextureValue, offset, j);
offset += sizeof(float);
ALIGN_OFFSET(offset,__alignof(void*));
CudaRes=cuParamSeti(GetTextureValue,offset,TextureLookupOutp
ut);
offset += sizeof(TextureLookupOutput);
CudaRes=cuParamSetSize(GetTextureValue,offset);
CudaRes=cuLaunchGrid(GetTextureValue,1,1);
float GPUValue;
CudaRes=cuMemcpyDtoH(&GPUValue,TextureLookupOutput,sizeof(float));
}
}
[/codebox]
Can any one know what’s the error , or even what Can I do more to know it?