Is this memory allocation fine?

Hi,

struct MyStr
{
int a;
int* iPtr;
long l;
float* fPtr
}

void HostFun( MyStr* str )
{

cudaMalloc( (void**)&str->iPtr, str->a );
cudaMalloc( (void**)&str->fPtr, str->l );

}

global void GloFun( int* iPtr, float* fPtr )
{
// I’m doing something here…
}

main()
{

MyStr str;
str.a = 10;
str.l = 10;
HostFun( &str );
GloFun<<<1,1>>>( str.iPtr, str.fPtr )
cudaFree( str.iPtr);
cudaFree( str.fPtr);

}

see the above code. and please answer

  • I’m declaring a structure variable (str) in main(). so str is in host memory. Is it Right or not?
  • I’m allocating the device memory for the members of str in host function HostFun(). but str is in host memory. Is this fine or not?
  • I’m passing the pointers in global function as arguments. when I’m accessing or doing something using those pointers will do anything? or it just works fine?
  • I’m freeing the memory in main() [ host ] function. Is this fine?
  • I’m declaring a structure variable (str) in main(). so str is in host memory. Is it Right or not?
    yes, its host memory.

  • I’m allocating the device memory for the members of str in host function HostFun(). but str is in host memory. Is this fine or not?
    yes. You get a pointer to device mem and you can hold it on host mem. However your function should look like this

void HostFun( MyStr* str ) {
cudaMalloc( (void**)&str->iPtr, str->a * sizeof(int));
cudaMalloc( (void**)&str->fPtr, str->l * sizeof(float));
}

  • I’m passing the pointers in global function as arguments. when I’m accessing or doing something using those pointers will do anything? or it just works fine?
    works fine, but you should pass also the dimensions.

  • I’m freeing the memory in main() [ host ] function. Is this fine?
    yes. Should be there.

Great Thanks Noel :)