Sign in to follow this  

Simple opencl question

This topic is 474 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi

 

I have a beginner question regarding changing a global variable from the host.

 

I have a "cl_float3 center" on the host.

 

I pass it to my kernel like so: error = clSetKernelArg(kernel, 3, sizeof(cl_float3), &center);

 

How can i up date the value of center on the device global memory from the host each frame?

 

I am assuming it is the memcopy function but i dont know how to generate the pointer to it.

 

Any help would be great.

 

Thanks :)

 

 

 

Share this post


Link to post
Share on other sites

This is what I have so far.

//Global on host
cl_mem centerBuf;
cl_float3 center;

//create buffer
centerBuf = clCreateBuffer(context,
		CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
		sizeof(cl_float3),
		&center, &error);

//set buffer as kernel arg
error = clSetKernelArg(kernel, 3, sizeof(centerBuf), &centerBuf);

////update center vector
move code


//get pointer to buffer
cl_float3 *CENTERptr = (cl_float3 *)clEnqueueMapBuffer(queue,
		centerBuf,
		CL_TRUE,
		CL_MAP_READ,
		0,
		sizeof(cl_float3),
		0, NULL, NULL, NULL);

//copy center from host to global memory on gpu (I assume thats what this does)
memcpy(CENTERptr, &center, sizeof(cl_float3));

//inside the kerenel
__kernel void __main(..., __global float3* center,...) {
//store centers x y and z inside the debug buffer
debugBuf[id] = (float4)(center[0].x,center[0].y,center[0].z,id);
}

This doesn't work.

 

Am i using memcpy wrong? Am i accessing the buffer inside the kernel wrong?

 

Any tips would be great.

Thanks

Share this post


Link to post
Share on other sites

I haven't done much OpenCL yet but I am slated to learn it so I'll give it a shot.

 

The memcpy should work but it's not necessary for one variable.
You could just assign it, *CENTERptr = center;

 

 

Check your error values and print out status about them.

Edited by Shannon Barber

Share this post


Link to post
Share on other sites

First of all, CL_MEM_COPY_HOST_PTR will only copy the initial data given to the host-allocated backing store (allocated by OpenCL). Secondly, when you map the buffer to get a pointer you can write to it, yes (assuming you set the flags correctly) but you need to unmap it when you're done for OpenCL to transfer all that back to the GPU. And you need to get your flags right.

 

Also consider that for small variables that change all the time you might be better off with a constant variable rather than a global buffer, but you can do that later, first get it working.

Share this post


Link to post
Share on other sites

Thanks a lot guys.

 

I put the following after memcpy

clEnqueueUnmapMemObject(queue, &center, CENTERptr, 0, 0, 0);

Still doesnt work.

 

I have tried everything.

 

Changed some flags, read the specs on each function, no luck.

Share this post


Link to post
Share on other sites

Thanks again guys, got it working. Changed this line:

clEnqueueUnmapMemObject(queue, &center, CENTERptr, 0, 0, 0);

I put the center vector in instead of the buffer :P

 

She works.

 

I knew it was going to be faster but not this fast. Its like watching an opengl raster sphere floating around only raytraced.

 

Insane.

Share this post


Link to post
Share on other sites

This topic is 474 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this