Simple opencl question

Started by
4 comments, last by BadEggGames 7 years, 7 months ago

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 :)

Advertisement

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

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.

- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

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.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

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.

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.

This topic is closed to new replies.

Advertisement