Browse Source

Tests the OpenCL part again

master
Joshua Moerman 10 years ago
parent
commit
9cf9887a09
  1. 144
      src/main.cpp

144
src/main.cpp

@ -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(){
App a;
NSAppWrapper app;
app.create_window({
[&](ContextParameters){
a.initialize();
},
[&](ContextParameters){
a.draw();
},
nullptr
});
app.run();
auto context = cl::Context::getDefault();
cout << context << endl;
// transfer data into buffers
cl::Buffer input(context, input_vector.begin(), input_vector.end(), false, true);
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;
int r = 20, g = 20, b = 20;
// grab the kernel
KernelOp kernel(program, "square", &err);
check(err);
// DO IT (in place)
for(int i = 0; i < r; ++i){
check(kernel(queue, W, H, input, W, input));
}
// create a queue
cl::CommandQueue queue(context, context.getInfo<CL_CONTEXT_DEVICES>().front());
// read back
queue.finish();
// 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);
auto red = input_vector;
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));
// DO IT (in place)
for(int i = 0; i < g; ++i){
check(kernel(queue, W, H, input, W, input));
}
}
// transfer data into buffers
cl::Buffer input(context, input_vector.begin(), input_vector.end(), false, true);
// read back
queue.finish();
int r = 80, g = 40, b = 20;
auto green = input_vector;
// DO IT (in place)
for(int i = 0; i < r; ++i){
check(kernel(queue, W, H, input, W, input));
}
// DO IT (in place)
for(int i = 0; i < b; ++i){
check(kernel(queue, W, H, input, W, input));
}
// read back
queue.finish();
// read back
queue.finish();
auto red = input_vector;
auto& blue = input_vector;
// DO IT (in place)
for(int i = 0; i < g; ++i){
check(kernel(queue, W, H, input, W, input));
// 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;
}
};
// 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));
}
int main() {
App a;
CLApp b;
// read back
queue.finish();
NSAppWrapper app;
auto& blue = input_vector;
app.create_window({
[&](ContextParameters){
a.initialize();
b.initialize();
},
[&](ContextParameters){
a.draw();
},
nullptr
});
// 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;
app.run();
}