FAQ  •  Login

Passing Struct to Kernel?

Moderators: Dan Negrut, ME964 Spring 2012

<<

S12hatt

Newbie
Newbie

Posts: 47

Joined: Mon Jan 23, 2012 9:07 pm

Unread post Fri May 04, 2012 1:06 pm

Passing Struct to Kernel?

Anyone doing this? Specifically, I want to pass a single struct (not a struct*) that has float pointers as member variables.

I wanted to see of anyone else is doing this because I'm not sure the proper way to do it and I think its gonna be annoying to figure out the right way on my own.
<<

S12klhaney

Newbie
Newbie

Posts: 32

Joined: Mon Jan 23, 2012 9:07 pm

Unread post Mon May 07, 2012 12:49 pm

Re: Passing Struct to Kernel?

If you look back at the lecture notes for February 16th there is a matrix multiplication example using structs.

Essentially, you need to allocate the memory on the GPU for the struct member variables instead of the struct itself:
  Code:
Matrix AllocateDeviceMatrix(const Matrix M) {
    Matrix Mdevice = M;
    int size = M.width * M.height * sizeof(float);
    cudaMalloc((void**)&Mdevice.elements, size);
    return Mdevice;
}


Then pass it to the kernel by value:
  Code:
__global__ void MatrixMulKernel(Matrix M, Matrix N, Matrix P) { .. }


And then deallocate the memory on the GPU for the struct member variables once you are done:
  Code:
void FreeDeviceMatrix(Matrix M) {
    cudaFree(M.elements);
}


If I remember correctly the kernel function arguments are saved in the shared memory, so as long as your struct isn't a memory hog it should be fine to simply pass it by value instead of by pointer. Hope that helps! Lecture notes has the full details if you need more information.

Regards,
Kerry
<<

Dan Negrut

Global Moderator
Global Moderator

Posts: 833

Joined: Wed Sep 03, 2008 12:24 pm

Unread post Thu May 10, 2012 10:28 pm

Re: Passing Struct to Kernel?

Kerry - thanks for your post.
Indeed, the arguments used to be passed through shared memory. i don't know if this is going to be the case in the future though...
Dan

Return to ME964 Spring 2012: High Performance Computing

Who is online

Users browsing this forum: No registered users and 1 guest

cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software.