|
@ -110,94 +110,100 @@ struct App { |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
struct CLApp { |
|
|
|
|
|
void initialize(){ |
|
|
|
|
|
auto context = cl::Context::getDefault(); |
|
|
|
|
|
cout << context << endl; |
|
|
|
|
|
|
|
|
|
|
|
cl_int err = 0; |
|
|
|
|
|
// build the program
|
|
|
|
|
|
auto KernelSource = slurp("Kernel.cl"); |
|
|
|
|
|
cl::Program program(context, {KernelSource.c_str(), KernelSource.size()}, true, &err); |
|
|
|
|
|
check(err); |
|
|
|
|
|
cout << program << endl; |
|
|
|
|
|
|
|
|
|
|
|
// grab the kernel
|
|
|
|
|
|
KernelOp kernel(program, "square", &err); |
|
|
|
|
|
check(err); |
|
|
|
|
|
|
|
|
|
|
|
// create a queue
|
|
|
|
|
|
cl::CommandQueue queue(context, context.getInfo<CL_CONTEXT_DEVICES>().front()); |
|
|
|
|
|
|
|
|
|
|
|
// make a lot of data
|
|
|
|
|
|
constexpr size_t W = 1280 * 1; |
|
|
|
|
|
constexpr size_t H = 800 * 1; |
|
|
|
|
|
std::vector<cl_float> input_vector(W*H); |
|
|
|
|
|
|
|
|
|
|
|
for(int y = 0; y < H; ++y){ |
|
|
|
|
|
for(int x = 0; x < W; ++x){ |
|
|
|
|
|
input_vector[x + W*y] = 10 * ((x / double(W) - 0.5) + 1.3371337*(y / double(H) - 0.5)); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
int main(){ |
|
|
// transfer data into buffers
|
|
|
App a; |
|
|
cl::Buffer input(context, input_vector.begin(), input_vector.end(), false, true); |
|
|
|
|
|
|
|
|
NSAppWrapper app; |
|
|
|
|
|
|
|
|
|
|
|
app.create_window({ |
|
|
|
|
|
[&](ContextParameters){ |
|
|
|
|
|
a.initialize(); |
|
|
|
|
|
}, |
|
|
|
|
|
[&](ContextParameters){ |
|
|
|
|
|
a.draw(); |
|
|
|
|
|
}, |
|
|
|
|
|
nullptr |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
app.run(); |
|
|
|
|
|
|
|
|
|
|
|
auto context = cl::Context::getDefault(); |
|
|
|
|
|
cout << context << endl; |
|
|
|
|
|
|
|
|
|
|
|
cl_int err = 0; |
|
|
int r = 20, g = 20, b = 20; |
|
|
// build the program
|
|
|
|
|
|
auto KernelSource = slurp("Kernel.cl"); |
|
|
|
|
|
cl::Program program(context, {KernelSource.c_str(), KernelSource.size()}, true, &err); |
|
|
|
|
|
check(err); |
|
|
|
|
|
cout << program << endl; |
|
|
|
|
|
|
|
|
|
|
|
// grab the kernel
|
|
|
// DO IT (in place)
|
|
|
KernelOp kernel(program, "square", &err); |
|
|
for(int i = 0; i < r; ++i){ |
|
|
check(err); |
|
|
check(kernel(queue, W, H, input, W, input)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// create a queue
|
|
|
// read back
|
|
|
cl::CommandQueue queue(context, context.getInfo<CL_CONTEXT_DEVICES>().front()); |
|
|
queue.finish(); |
|
|
|
|
|
|
|
|
// make a lot of data
|
|
|
auto red = input_vector; |
|
|
constexpr size_t W = 1280 * 4; |
|
|
|
|
|
constexpr size_t H = 800 * 4; |
|
|
|
|
|
std::vector<cl_float> input_vector(W*H); |
|
|
|
|
|
|
|
|
|
|
|
for(int y = 0; y < H; ++y){ |
|
|
// DO IT (in place)
|
|
|
for(int x = 0; x < W; ++x){ |
|
|
for(int i = 0; i < g; ++i){ |
|
|
input_vector[x + W*y] = 10 * ((x / double(W) - 0.5) + 1.3371337*(y / double(H) - 0.5)); |
|
|
check(kernel(queue, W, H, input, W, input)); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// transfer data into buffers
|
|
|
// read back
|
|
|
cl::Buffer input(context, input_vector.begin(), input_vector.end(), false, true); |
|
|
queue.finish(); |
|
|
|
|
|
|
|
|
int r = 80, g = 40, b = 20; |
|
|
auto green = input_vector; |
|
|
|
|
|
|
|
|
// DO IT (in place)
|
|
|
// DO IT (in place)
|
|
|
for(int i = 0; i < r; ++i){ |
|
|
for(int i = 0; i < b; ++i){ |
|
|
check(kernel(queue, W, H, input, W, input)); |
|
|
check(kernel(queue, W, H, input, W, input)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// read back
|
|
|
// read back
|
|
|
queue.finish(); |
|
|
queue.finish(); |
|
|
|
|
|
|
|
|
auto red = input_vector; |
|
|
auto& blue = input_vector; |
|
|
|
|
|
|
|
|
// DO IT (in place)
|
|
|
// test
|
|
|
for(int i = 0; i < g; ++i){ |
|
|
cout << "opencl is done" << endl; |
|
|
check(kernel(queue, W, H, input, W, input)); |
|
|
png::ostream<> image(W, H, "test.png"); |
|
|
|
|
|
for(int i = 0; i < red.size(); ++i){ |
|
|
|
|
|
image << png::ostream<>::pixel(blue[i], red[i], green[i]); |
|
|
|
|
|
} |
|
|
|
|
|
cout << "png is saved" << endl; |
|
|
} |
|
|
} |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
// read back
|
|
|
|
|
|
queue.finish(); |
|
|
|
|
|
|
|
|
|
|
|
auto green = input_vector; |
|
|
|
|
|
|
|
|
|
|
|
// DO IT (in place)
|
|
|
int main() { |
|
|
for(int i = 0; i < b; ++i){ |
|
|
App a; |
|
|
check(kernel(queue, W, H, input, W, input)); |
|
|
CLApp b; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// read back
|
|
|
NSAppWrapper app; |
|
|
queue.finish(); |
|
|
|
|
|
|
|
|
|
|
|
auto& blue = input_vector; |
|
|
app.create_window({ |
|
|
|
|
|
[&](ContextParameters){ |
|
|
|
|
|
a.initialize(); |
|
|
|
|
|
b.initialize(); |
|
|
|
|
|
}, |
|
|
|
|
|
[&](ContextParameters){ |
|
|
|
|
|
a.draw(); |
|
|
|
|
|
}, |
|
|
|
|
|
nullptr |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
// test
|
|
|
app.run(); |
|
|
cout << "opencl is done" << endl; |
|
|
|
|
|
png::ostream<> image(W, H, "test.png"); |
|
|
|
|
|
for(int i = 0; i < red.size(); ++i){ |
|
|
|
|
|
image << png::ostream<>::pixel(blue[i], red[i], green[i]); |
|
|
|
|
|
} |
|
|
|
|
|
cout << "png is saved" << endl; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|