本文整理汇总了C++中CommandQueue::enqueueReadBuffer方法的典型用法代码示例。如果您正苦于以下问题:C++ CommandQueue::enqueueReadBuffer方法的具体用法?C++ CommandQueue::enqueueReadBuffer怎么用?C++ CommandQueue::enqueueReadBuffer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CommandQueue
的用法示例。
在下文中一共展示了CommandQueue::enqueueReadBuffer方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main(int argc, char ** argv) {
// Load image
SIPL::Image<float> * image = new SIPL::Image<float>("images/sunset.jpg");
// Create OpenCL context
Context context = createCLContextFromArguments(argc, argv);
// Compile OpenCL code
Program program = buildProgramFromSource(context, "gaussian_blur.cl");
// Select device and create a command queue for it
VECTOR_CLASS<Device> devices = context.getInfo<CL_CONTEXT_DEVICES>();
CommandQueue queue = CommandQueue(context, devices[0]);
// Create an OpenCL Image / texture and transfer data to the device
Image2D clImage = Image2D(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, ImageFormat(CL_R, CL_FLOAT), image->getWidth(), image->getHeight(), 0, (void*)image->getData());
// Create a buffer for the result
Buffer clResult = Buffer(context, CL_MEM_WRITE_ONLY, sizeof(float)*image->getWidth()*image->getHeight());
// Create Gaussian mask
int maskSize;
float * mask = createBlurMask(10.0f, &maskSize);
// Create buffer for mask and transfer it to the device
Buffer clMask = Buffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(float)*(maskSize*2+1)*(maskSize*2+1), mask);
// Run Gaussian kernel
Kernel gaussianBlur = Kernel(program, "gaussian_blur");
gaussianBlur.setArg(0, clImage);
gaussianBlur.setArg(1, clMask);
gaussianBlur.setArg(2, clResult);
gaussianBlur.setArg(3, maskSize);
queue.enqueueNDRangeKernel(
gaussianBlur,
NullRange,
NDRange(image->getWidth(), image->getHeight()),
NullRange
);
// Transfer image back to host
float* data = new float[image->getWidth()*image->getHeight()];
queue.enqueueReadBuffer(clResult, CL_TRUE, 0, sizeof(float)*image->getWidth()*image->getHeight(), data);
image->setData(data);
// Save image to disk
image->save("images/result.jpg", "jpeg");
image->display();
}
示例2: main
int main(int argc, char * argv[])
{
const std::string hw("Hello WorldCL\n");
char * outH = new char [hw.length()+1];
ImageCL i;
i.create_cl_context();
Buffer out_buffer;
int opts = CL_MEM_WRITE_ONLY | CL_MEM_USE_HOST_PTR;
i.create_cl_kernel_buffer(out_buffer, opts, hw.length()+1);
// device handles
vector<Device> devices;
devices = i.get_devices_list();
// load kernel code
Kernel k = i.load_cl_kernel_file("image.cl", "hello", out_buffer);
CommandQueue q = i.run_kernel(k, devices[0], hw.length()+1); // blocks until finished
q.enqueueReadBuffer(out_buffer, CL_TRUE, 0, hw.length()+1, (void*)&hw);
std::cout << hw;
return 0;
}
示例3: main
int main(int argc, char **argv)
{
srand((unsigned)time(NULL));
Kernel kernel;
CommandQueue queue;
Context context;
{
std::vector<Platform> platformList;
Platform::get(&platformList);
clog << "Platform number is: " << platformList.size() << endl;
std::string platformVendor;
platformList[0].getInfo((cl_platform_info)CL_PLATFORM_VENDOR, &platformVendor);
clog << "Platform is by: " << platformVendor << "\n";
cl_context_properties cprops[] = {
CL_CONTEXT_PLATFORM, (cl_context_properties) platformList[0](),
0
};
context = Context(GET_TARGET_PLATFORM, cprops);
std::vector<Device> devices = context.getInfo<CL_CONTEXT_DEVICES>();
queue = CommandQueue(context, devices[0]);
std::string sourceCode = "#include \"es.cl\"\n";
Program::Sources source(1, std::make_pair(sourceCode.c_str(), sourceCode.length()+1));
Program program = Program(context, source);
try
{
program.build(devices, "-I.");
}
catch (Error &)
{
std::string errors;
program.getBuildInfo(devices[0], CL_PROGRAM_BUILD_LOG, &errors);
std::cerr << "Build log: " << endl << errors << endl;
return 1;
}
kernel = Kernel(program, "es");
}
individual *individuals = new individual[LAMBDA];
for (int i = 0; i < LAMBDA; i++)
{
for (int j = 0; j < DIM; ++j)
{
individuals[i].x[j] = (rand()/((float)RAND_MAX)) * (XMAX-XMIN) + XMIN;
individuals[i].s[j] = (XMAX-XMIN) / 6.f;
}
for (int j = 0; j < DIM_A; ++j)
{
individuals[i].a[j] = (rand()/((float)RAND_MAX)) * (2*PI) - PI;
}
individuals[i].fitness = 0;
}
float gbest = std::numeric_limits<float>::infinity(), xbest[DIM];
Buffer esBuffer = Buffer(context, 0, INDIVIDUALS_SIZE);
Event ev;
queue.enqueueMapBuffer(esBuffer, CL_TRUE, CL_MAP_READ | CL_MAP_WRITE, 0, INDIVIDUALS_SIZE);
for (int i = 0; i < 1000; i++)
{
queue.enqueueWriteBuffer(esBuffer, CL_TRUE, 0, INDIVIDUALS_SIZE, individuals);
kernel.setArg(1, (cl_ulong)rand());
kernel.setArg(0, esBuffer);
queue.enqueueNDRangeKernel(kernel, NullRange, NDRange(LAMBDA), NDRange(1), NULL, &ev);
ev.wait();
queue.enqueueReadBuffer(esBuffer, CL_TRUE, 0, INDIVIDUALS_SIZE, individuals);
std::sort(individuals, individuals + LAMBDA, individual_comp);
individual mean = get_mean(individuals);
for (int j = 0; j < LAMBDA; ++j)
{
individuals[j] = mean;
}
}
gbest = individuals[0].fitness;
for (int i = 0; i < DIM; ++i) xbest[i] = individuals[0].x[i];
clog << "Best value " << gbest << " found at (";
for (int i = 0; i < DIM; ++i) clog << xbest[i] << (i == DIM-1 ? ")" : ", ");
clog << "\n";
clog << "Our computation estemates it: f(" << xbest[0] << ", ..., " << xbest[DIM-1] << ") = " << es_f(xbest) << endl;
delete[] individuals;
return 0;
}
示例4: main
//.........这里部分代码省略.........
K.setArg(0, *bufs[0]);
K.setArg(1, 0);
QdspOO->enqueueTask(K);
K.setArg(1, elements);
clock_gettime(CLOCK_MONOTONIC, &tp_start);
/*------------------------------------------------------------------------
* Iterate for as many tasks as there are
*-----------------------------------------------------------------------*/
for (int i = 0; i < tasks; ++i)
{
/*---------------------------------------------------------------------
* Variables to ensure that this iteration is using the correct circular
* resources: i.e. buffers and arrays.
*--------------------------------------------------------------------*/
int circIdx = i % inflight;
Buffer &buf(*bufs[circIdx]);
int *ary(arys [circIdx]);
Event nullEv;
/*---------------------------------------------------------------------
* Native kernels are only passed a single pointer, so define a structure
* that contains the actual arguments, populate that and then create
* a C++ binding native argument class that has the pointer and a size.
*--------------------------------------------------------------------*/
arguments_t proArgs = { ary, elements, i, i };
arguments_t conArgs = { ary, elements, i+1, i };
native_arg_t proNargs(&proArgs, sizeof(proArgs));
native_arg_t conNargs(&conArgs, sizeof(conArgs));
K.setArg(0, buf);
/*---------------------------------------------------------------------
* Since we are reusing N sets of buffers in this loop, we need to make
* sure than iteration I does not start until after iteration I-N
* completes. Iterations < N can start immediately.
*--------------------------------------------------------------------*/
vecEv *start_waits = (i < inflight) ? 0 : &evt[circIdx][CNS];
evt[circIdx][PRD][0] = nullEv;
evt[circIdx][WRT][0] = nullEv;
evt[circIdx][CMP][0] = nullEv;
evt[circIdx][RD ][0] = nullEv;
QcpuOO->enqueueNativeKernel(cpu_produce, proNargs, 0, 0,
start_waits, &evt[circIdx][PRD][0]);
evt[circIdx][CNS][0] = nullEv;
QdspOO->enqueueWriteBuffer (buf, CL_FALSE, 0, size, ary,
&evt[circIdx][PRD], &evt[circIdx][WRT][0]);
QdspOO->enqueueTask (K,
&evt[circIdx][WRT], &evt[circIdx][CMP][0]);
QdspOO->enqueueReadBuffer (buf, CL_FALSE, 0, size, ary,
&evt[circIdx][CMP], &evt[circIdx][RD ][0]);
QcpuIO->enqueueNativeKernel(cpu_consume, conNargs, 0, 0,
&evt[circIdx][RD ], &evt[circIdx][CNS][0]);
}
/*------------------------------------------------------------------------
* Only need to wait for the CPU In Order queue to finish, since all all
* other enqueue events must finish before the CPU IO queue can finish
*-----------------------------------------------------------------------*/
QcpuIO->finish();
delete QcpuIO;
delete QcpuOO;
delete QdspOO;
clock_gettime(CLOCK_MONOTONIC, &tp_end);
double elapsed = clock_diff (&tp_start, &tp_end);
printf("Elapsed : %8.6f secs\n", elapsed);
/*------------------------------------------------------------------------
* After the running is complete, report timing for each step
*-----------------------------------------------------------------------*/
#if PROFILE
cl_ulong ref;
evt[0][0][0].getProfilingInfo(CL_PROFILING_COMMAND_QUEUED, &ref);
for (int i = 0; i < inflight; ++i)
{
for (int s = 0; s < STAGES; ++s)
ocl_relative_times(evt[i][s][0], stage_names[s], ref);
cout << endl;
}
#endif
}
catch (Error err)
{
cerr << "ERROR: " << err.what() << "("
<< ocl_decode_error(err.err()) << ")"
<< endl;
incorrect_results = true;
}
if (incorrect_results) return -1;
}