Skip to content

Instantly share code, notes, and snippets.

@xatier
Last active January 2, 2016 07:18
Show Gist options
  • Save xatier/8268759 to your computer and use it in GitHub Desktop.
Save xatier/8268759 to your computer and use it in GitHub Desktop.
////////////////////////////////////////////
// file: ~xatier/work/lib/clwrapper.c
////////////////////////////////////////////
// openCL doc
// http://www.khronos.org/registry/cl/specs/opencl-2.0.pdf
// http://www.khronos.org/registry/cl/sdk/1.2/docs/man/xhtml/
// page 164
void * clSVMAlloc (cl_context context, cl_svm_mem_flags flags, size_t size, unsigned int alignment) {
// flgs <- flags (some trnasform)
// cl_mem clCreateBuffer ( cl_context context, cl_mem_flags flags,
// size_t size, void *host_ptr, cl_int *errcode_ret)
if (flags & CL_MEM_SVM_FINE_GRAIN_BUFFER) {
// page 165, under Table 5.13, flags
// If CL_MEM_SVM_FINE_GRAIN_BUFFER is not specified, the buffer can be created as a coarse grained SVM allocation.
// ignore?
}
if (flags & CL_MEM_SVM_ATOMICS) {
// grained SVM allocation. Similarly, if CL_MEM_SVM_ATOMICS is not specified, the buffer can
// be created without support for the OpenCL 2.0 SVM atomic operations
}
void *svm_pointer = malloc(size);
// XXX: our memory management here
void *bufferObject = clCreateBuffer(context, flags, size, h_pointer, NULL);
// XXX: fill the svm_pointer <-> real buffer object table
return svm_pointer;
}
cl_mem svm_pointer2buffer (vouid *svm_pointer) {
// need a look up from svm pointer to buffer
return ...;
}
// page 167
void clSVMFree (cl_context context, void * svm_pointer) {
// XXX: free host pointer in our memory pool
// buffer object pointer
void *bufferObject = svm_pointer2buffer(svm_pointer);
// XXX: delete the svm_pointer <-> real buffer object table
clReleaseMemObject(bufferObject);
}
// page 168
// XXX: do we need to implement this?
cl_int clEnqueuesSVMFree () {
}
// page 169
// enqueues a command to do a memcpy operation.
cl_int clEnqueueSVMMemcpy (cl_command_queue command_queue,
cl_bool blocking_copy,
void *dst_ptr,
const void *src_ptr,
size_t size,
cl_uint num_events_in_wait_list,
const cl_event *event_wait_list,
cl_event *event) {
// XXX: pure forwarding?
// enqueues a command to copy a buffer object identified by src_buffer to another buffer object identified by dst_buffer.
return clEnqueueCopyBuffer(command_queue,
src_buffer,
dst_buffer,
src_offset,
dst_offset,
size,
num_events_in_wait_list,
event_wait_list,
event);
}
// page 171
// enqueues a command to fill a region in memory with a pattern of a given pattern size.
cl_int clEnqueueSVMMemFill (cl_command_queue command_queue,
void *svm_ptr,
const void *pattern,
size_t pattern_size,
size_t size,
cl_uint num_events_in_wait_list,
const cl_event *event_wait_list,
cl_event *event) {
void *bufferObject = svm_pointer2buffer(svm_pointer);
// enqueues a command to fill a buffer object with a pattern of a given pattern size.
clEnqueueFillBuffer(command_queue,
bufferObject,
// XXX: the following parameters are needed to be fixed
const void *pattern,
size_t pattern_size,
size_t offset,
size_t size,
cl_uint num_events_in_wait_list,
const cl_event *event_wait_list,
cl_event *event)
}
// page 173
// enqueues a command that will allow the host to update a region of a SVM buffer.
// Note that since we are enqueuing a command with a SVM buffer, the region is already mapped in the host address space.
cl_int clEnqueueSVMMap (cl_command_queue command_queue,
cl_bool blocking_map,
cl_map_flags map_flags,
void *svm_ptr,
size_t size,
cl_uint num_events_in_wait_list,
const cl_event *event_wait_list,
cl_event *event) {
void *bufferObject = svm_pointer2buffer(svm_pointer);
// enqueues a command to map a region of the buffer object given by buffer into the
// host address space and returns a pointer to this mapped region.
return clEnqueueMapBuffer(command_queue,
bufferObject,
// XXX: the following parameters are needed to be fixed
cl_bool blocking_map,
cl_map_flags map_flags,
size_t offset,
size_t size,
cl_uint num_events_in_wait_list,
const cl_event *event_wait_list,
cl_event *event,
cl_int *errcode_ret);
}
// page 174
// enqueues a command to indicate that the host has completed updating the region given by
// svm_ptr and which was specified in a previous call to clEnqueueSVMMap.
cl_int clEnqueueSVMUnmap (cl_command_queue command_queue,
void *svm_ptr,
cl_uint num_events_in_wait_list,
const cl_event *event_wait_list,
cl_event *event) {
void *bufferObject = svm_pointer2buffer(svm_pointer);
return clEnqueueUnmapMemObject(command_queue ,
bufferObject,
void *mapped_ptr, // XXX: maybe the host pointer?
num_events_in_wait_list,
event_wait_list,
event );
}
@xatier
Copy link
Author

xatier commented Mar 17, 2014

  • User's data structure / algorithm (how do user write their openCL 2.0 applications?)
  • pointer, alias analysis
  • how to translate address space (Offline kernel rewriting or dynamically table lookup)
  • memory management (synchronization / consistence)/ memory layout on device

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment