Skip to content

Instantly share code, notes, and snippets.

@gnail737
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save gnail737/1e91be1a4d6be7f7255d to your computer and use it in GitHub Desktop.

Select an option

Save gnail737/1e91be1a4d6be7f7255d to your computer and use it in GitHub Desktop.
Temp code -- unique_ptr is so beautiful`
#include <jni.h>
#include "OCLRuntime.h"
#define ARRAY_SIZE 256
using std::unique_ptr;
using std::vector;
/* allocating resource objects on heap, so they can be released in ad-hoc fashion*/
/* standard OpenCL resource, platform, context, device, membuffer, commandqueue */
static unique_ptr<std::vector<cl::Platform>> platforms;
static unique_ptr<cl::Context> hybridContext;
static unique_ptr<std::vector<cl::Device>> devices;
static unique_ptr<cl::Program> program;
/*assuming only one kernel at this time */
static unique_ptr<cl::Kernel> kernel;
static unique_ptr<float[]> buff1, buff2;
static unique_ptr<cl::Buffer> input_buff1;
static unique_ptr<cl::Buffer> input_buff2;
static unique_ptr<std::vector<cl::CommandQueue>> queues;
static bool readyToRun = false;
static const char * helloStr = "\n";
void destroyContext() {
hybridContext = nullptr;
devices = nullptr;
program = nullptr;
kernel = nullptr;
buff1 = nullptr;
buff2 = nullptr;
input_buff1 = nullptr;
input_buff2 = nullptr;
}
extern "C" void
Java_com_example_testcamerapreview_TestCameraPreview_initRuntime( JNIEnv* env, jobject thiz ){
/*allocating objects on heap intialize them to proper values */
cl_int err = CL_SUCCESS;
try {
platforms = unique_ptr<vector<cl::Platform>>{new vector<cl::Platform>};
cl::Platform::get(platforms.get());
if (platforms->size() == 0) {
LOGI("Platform size is zero");
throw cl::Error(5000, "You got error!");
}
cl_context_properties prop[] =
{CL_CONTEXT_PLATFORM, (cl_context_properties)((*platforms)[0])(), 0};
hybridContext = unique_ptr<cl::Context>{new cl::Context(CL_DEVICE_TYPE_ALL, prop)};
//cl::Context context_gpu(CL_DEVICE_TYPE_GPU, prop);
devices = unique_ptr<std::vector<cl::Device>>{new vector<cl::Device>(hybridContext->getInfo<CL_CONTEXT_DEVICES>())};
cl::Program::Sources l_src(1, std::make_pair(helloStr, strlen(helloStr)));
program = unique_ptr<cl::Program>{new cl::Program(*hybridContext, l_src)};
program->build(*devices);
kernel = unique_ptr<cl::Kernel>{new cl::Kernel(*program, "hello", &err)};
if (err != 0) throw cl::Error(5000, "You got error!");
buff1 = unique_ptr<float[]>{new float[ARRAY_SIZE]};
buff2 = unique_ptr<float[]>{new float[ARRAY_SIZE]};
for (int i=0; i<ARRAY_SIZE; ++i) {
buff1[i] = i*2;
buff2[i] = i;
}
input_buff1 = unique_ptr<cl::Buffer>{ new cl::Buffer(*hybridContext,
CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR,
ARRAY_SIZE*sizeof(float), buff1.get())};
input_buff2 = unique_ptr<cl::Buffer>{ new cl::Buffer(*hybridContext,
CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR,
ARRAY_SIZE*sizeof(float), buff2.get())};
//std::vector<cl::Event> event_list;
queues = unique_ptr<std::vector<cl::CommandQueue>>{new std::vector<cl::CommandQueue>()};
for (cl::Device device : *devices ) {
cl::CommandQueue queue(*hybridContext, device, 0, &err);
if (err != 0) throw cl::Error(5000, "You got error!");
queues->push_back(queue);
}
//running command on all queues
for (cl::CommandQueue queue : *queues){
cl::Event event;
queue.enqueueNDRangeKernel(*kernel, cl::NullRange,
cl::NDRange(4, 4),
cl::NullRange,
NULL, NULL);
queue.finish();
//event_list.push_back(event);
}
/*
for (cl::Event event : event_list)
event.wait(); */
}
catch(cl::Error err) {
LOGE("Error happened \n what - %s \n error - %d \n", err.what(), err.err());
destroyContext();
}
readyToRun = true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment