Tests the OpenCL part again
This commit is contained in:
parent
dfb1e7aebf
commit
9cf9887a09
1 changed files with 78 additions and 72 deletions
150
src/main.cpp
150
src/main.cpp
|
@ -110,15 +110,92 @@ struct App {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct CLApp {
|
||||||
|
void initialize(){
|
||||||
|
auto context = cl::Context::getDefault();
|
||||||
|
cout << context << endl;
|
||||||
|
|
||||||
int main(){
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// transfer data into buffers
|
||||||
|
cl::Buffer input(context, input_vector.begin(), input_vector.end(), false, true);
|
||||||
|
|
||||||
|
int r = 20, g = 20, b = 20;
|
||||||
|
|
||||||
|
// DO IT (in place)
|
||||||
|
for(int i = 0; i < r; ++i){
|
||||||
|
check(kernel(queue, W, H, input, W, input));
|
||||||
|
}
|
||||||
|
|
||||||
|
// read back
|
||||||
|
queue.finish();
|
||||||
|
|
||||||
|
auto red = input_vector;
|
||||||
|
|
||||||
|
// DO IT (in place)
|
||||||
|
for(int i = 0; i < g; ++i){
|
||||||
|
check(kernel(queue, W, H, input, W, input));
|
||||||
|
}
|
||||||
|
|
||||||
|
// read back
|
||||||
|
queue.finish();
|
||||||
|
|
||||||
|
auto green = input_vector;
|
||||||
|
|
||||||
|
// DO IT (in place)
|
||||||
|
for(int i = 0; i < b; ++i){
|
||||||
|
check(kernel(queue, W, H, input, W, input));
|
||||||
|
}
|
||||||
|
|
||||||
|
// read back
|
||||||
|
queue.finish();
|
||||||
|
|
||||||
|
auto& blue = input_vector;
|
||||||
|
|
||||||
|
// test
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
int main() {
|
||||||
App a;
|
App a;
|
||||||
|
CLApp b;
|
||||||
|
|
||||||
NSAppWrapper app;
|
NSAppWrapper app;
|
||||||
|
|
||||||
app.create_window({
|
app.create_window({
|
||||||
[&](ContextParameters){
|
[&](ContextParameters){
|
||||||
a.initialize();
|
a.initialize();
|
||||||
|
b.initialize();
|
||||||
},
|
},
|
||||||
[&](ContextParameters){
|
[&](ContextParameters){
|
||||||
a.draw();
|
a.draw();
|
||||||
|
@ -127,77 +204,6 @@ int main(){
|
||||||
});
|
});
|
||||||
|
|
||||||
app.run();
|
app.run();
|
||||||
|
|
||||||
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 * 4;
|
|
||||||
constexpr size_t H = 800 * 4;
|
|
||||||
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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// transfer data into buffers
|
|
||||||
cl::Buffer input(context, input_vector.begin(), input_vector.end(), false, true);
|
|
||||||
|
|
||||||
int r = 80, g = 40, b = 20;
|
|
||||||
|
|
||||||
// DO IT (in place)
|
|
||||||
for(int i = 0; i < r; ++i){
|
|
||||||
check(kernel(queue, W, H, input, W, input));
|
|
||||||
}
|
|
||||||
|
|
||||||
// read back
|
|
||||||
queue.finish();
|
|
||||||
|
|
||||||
auto red = input_vector;
|
|
||||||
|
|
||||||
// DO IT (in place)
|
|
||||||
for(int i = 0; i < g; ++i){
|
|
||||||
check(kernel(queue, W, H, input, W, input));
|
|
||||||
}
|
|
||||||
|
|
||||||
// read back
|
|
||||||
queue.finish();
|
|
||||||
|
|
||||||
auto green = input_vector;
|
|
||||||
|
|
||||||
// DO IT (in place)
|
|
||||||
for(int i = 0; i < b; ++i){
|
|
||||||
check(kernel(queue, W, H, input, W, input));
|
|
||||||
}
|
|
||||||
|
|
||||||
// read back
|
|
||||||
queue.finish();
|
|
||||||
|
|
||||||
auto& blue = input_vector;
|
|
||||||
|
|
||||||
// test
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Reference in a new issue