Hi all,
I began to programming with cuda last week, and the first problem i saw was the struct in array.
First the other element don’t match, then I add align(16) to struct, then the indices did not match…
Bored of write random code, and too lazy to search the rightly problem. I have converted all my array of struct in 2Darray, and I have writ this support class:
[codebox]
template
class SmartDeviceArray
{
public:
void *values;
int nValues;
size_t pitch;
SmartDeviceArray() { values=0; }
~SmartDeviceArray()
{
if(values)
{
cudaFree(values);
values = 0;
}
}
void Init(T* src,int nVals)
{
nValues = nVals;
cudaError_t r;
r = cudaMallocPitch(&values, &pitch,sizeof(T),nValues);
MyTest(r,"Allocating");
cudaMemcpy2D(values,pitch,src,sizeof(T),sizeof(T),nValues,cu
daMemcpyHostToDevice) ;
MyTest(r,"Coping");
}
};
template
host device
T* GetValue(void *ptr,size_t pitch,int idx)
{
return (T*)(&(((char*)ptr)[idx*pitch]));
}
[/codebox]
and later, in device code, i call
MyStruct *pElement = GetValue<MyStruct>(pArray,iArrayPitch,idx);
unfortunately I can not pass the the class to global function, i do this:
#define PASS_SMART(T,p) (T*)p.values,p.nValues,p.pitch
MyGlobal<<<Db,Dg>>>(otherParam,
PASS_SMART(MyStrict,MyVar) );
I think it can be useful to other programmer, and i want share it with all you.
I know it is not perfect, and probably it is not the best way to do this, but it is what i found.
Perry