OpenCL* for CPU
Ask questions and share information on Intel® SDK for OpenCL™ Applications and OpenCL™ implementations for Intel® CPU.
This forum covers OpenCL* for CPU only. OpenCL* for GPU questions can be asked in the GPU Compute Software forum. Intel® FPGA SDK for OpenCL™ questions can be ask in the FPGA Intel® High Level Design forum.
1719 Discussions

How to Find & Write to Device Address of OpenCL Kernel Buffer


Preface: We have an accelerator application written in OpenCL where the final output of the accelerator is written into an OpenCL Buffer (on the board's DDR3 memory). We can then read from the buffer on the host side and extract the output.

Body: For our use case, in addition to the accelerator data output, we also need to know the exact device memory address that this final output is written to in the board's DDR3 memory. I understand that OpenCL prefers to hide the raw memory address grittiness from the developer and instead lets them access device memory through OpenCL buffer structures, but for our use case we do need to know the exact device memory address. Once we have found this address, we would then like to write an OpenCL kernel that writes data to this raw memory address on the device. Again, I understand that this is not the recommended OpenCL flow, but for our case we have to do it this way.

My Attempt: I found a similar forum thread with the following kernel code, but I'm not sure that it functions as I intend. My goal with this kernel is to store the Target Buffer Pointer in a different Buffer that I can then read from on the host side, but I have to set the kernel arguments from the host side so I am concerned that I am actually passing the Host's memory address of the buffer instead of the Device's memory address.


  • How can a developer find the device memory address of an OpenCL buffer?
  • How would you write an OpenCL kernel that writes data to a given raw memory address on the device?

Kernel Code: 

// Kernel used to obtain pointer from target buffer
__kernel void mem_ptr(__global char * target_buffer, __global ulong * ptr)
    ptr[0] = &target_buffer[0];


Host Code:

// Variable Decleration
cl_kernel kernel_mem_ptr;
cl_mem ptr_buffer;
cl_ulong buffer_ptrs;

// Create Kernel
knl_mem_ptr = clCreateKernel(program, knl_name_mem_ptr, &status);
checkError(status, "Failed to create mem_ptr kernel");

//  Buffer that we want the device pointer from (data_buf) 
//  and a place to store it (ptr_buffer).
ptr_buffer = clCreateBuffer(context, CL_MEM_READ_WRITE, 1 * sizeof(cl_ulong), NULL, &ret);

// Setup our kernel arguments from the host...
ret = clSetKernelArg(kernel_mem_ptr, 0, sizeof(cl_mem), (void *)&data_buf);
ret = clSetKernelArg(kernel_mem_ptr, 1, sizeof(cl_mem), (void *)&ptr_buffer);

// Enqueue Buffer Getter
ret = clEnqueueTask(que_lrn[i], kernel_mem_ptr, 0, NULL, NULL);

// Read Pointer from Buffer 
ret = clEnqueueReadBuffer(que_lrn[i], ptr_buffer, CL_TRUE, 0, 1 * sizeof(cl_ulong), (void*)buffer_ptrs, 0, NULL, NULL);


Any help is appreciated, thanks!


Labels (1)
0 Kudos
0 Replies