-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_mapped_buffer.cpp
More file actions
64 lines (52 loc) · 1.92 KB
/
Copy pathmemory_mapped_buffer.cpp
File metadata and controls
64 lines (52 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <boost/compute.hpp>
#include <CL/cl.h>
#include <chrono>
#include <cstring>
namespace compute = ::boost::compute;
int main () {
auto gpu = compute::system::default_device();
std::cout << "Using Default Device: " << gpu.name() << "\n";
auto ctx = compute::context(gpu);
auto cmd_queue = compute::command_queue(ctx, gpu, CL_QUEUE_PROFILING_ENABLE);
const auto size = 256;
compute::buffer device_buffer(ctx, size * sizeof(cl_int));
cl_int err = 0;
int* dev_ptr = (int*)clEnqueueMapBuffer(cmd_queue.get(),
device_buffer.get(),
CL_TRUE,
CL_MAP_READ | CL_MAP_WRITE,
0,
sizeof(cl_int) * size,
0,
NULL,
NULL,
&err);
if (!dev_ptr) {
std::cerr << "Error in Mapping" << "\n";
}
std::memset(dev_ptr, 0, sizeof(cl_int) * size);
auto program = compute::program::create_with_source_file("increment.cl", ctx);
try {
program.build();
} catch (compute::opencl_error) {
std::cout << program.build_log() << "\n";
}
auto increment_kernel = compute::kernel(program, "increment");
std::cout << "Running: " << increment_kernel.name() << "\n";
increment_kernel.set_arg(0, device_buffer);
auto finish_event = cmd_queue.enqueue_1d_range_kernel(increment_kernel, 0, size, 128);
compute::wait_list wait(finish_event);
cmd_queue.flush();
cmd_queue.finish();
for (int i=1; i<size+1; ++i) {
std::cout << dev_ptr[i-1] << " ";
if (i % 32 == 0) {
std::cout << "\n";
}
}
clEnqueueUnmapMemObject(cmd_queue.get(), device_buffer.get(), dev_ptr, 0, NULL, NULL);
auto start = finish_event.get_profiling_info<cl_long>(CL_PROFILING_COMMAND_START);
auto end = finish_event.get_profiling_info<cl_long>(CL_PROFILING_COMMAND_END);
std::cout << "\nFinsihed Duration: " << end - start << " ns\n";
}