OpenCL called correctly, but not yet giving result

This commit is contained in:
karl 2020-12-16 11:29:59 +01:00
parent 62ce8f1b06
commit fddb092a56
3 changed files with 42 additions and 17 deletions

View File

@ -1,5 +1,5 @@
CXX = g++
CXXFLAGS = -fopenmp -Wall -O3
CXXFLAGS = -fopenmp -Wall -O3 -g
gol: main.o Timing.o
$(CXX) $(CXXFLAGS) -o gol main.o Timing.o -lOpenCL

18
gol.cl
View File

@ -1,5 +1,5 @@
void kernel generation(global const int *previous, global int *new, global const int *size) {
int ID, Nthreads, n, ratio, start, stop;"
int ID, Nthreads, n, ratio, start, stop, x, y, left, right, up, down, neighbors, i;
ID = get_global_id(0);
Nthreads = get_global_size(0);
@ -9,18 +9,18 @@ void kernel generation(global const int *previous, global int *new, global const
start = ratio * ID;
stop = ratio * (ID + 1);
for (int i=start; i<stop; i++)
int x = i % size[1];
int y = i / size[0];
for (i = start; i < stop; i++)
x = i % size[1];
y = i / size[0];
int left = x - 1;
int right = (x + 1) % size[0];
left = x - 1;
right = (x + 1) % size[0];
int up = (y - 1 + size[1]) % size[1];
int down = (y + 1) % size[1];
up = (y - 1 + size[1]) % size[1];
down = (y + 1) % size[1];
// Get the number of neighbors
int neighbors =
neighbors =
previous[size[0] * up + left]
+ previous[size[0] * up + x]
+ previous[size[0] * up + right]

View File

@ -173,7 +173,7 @@ void print_usage() {
std::cerr << "Usage: gol --mode seq|omp|ocl [--threads number] [--device cpu|gpu] --load infile.gol --save outfile.gol --generations number [--measure]" << std::endl;
}
void main_opencl(std::string infile, int num_generations) {
void main_opencl(std::string infile, std::string outfile, int num_generations) {
// get all platforms (drivers), e.g. NVIDIA
std::vector<cl::Platform> all_platforms;
cl::Platform::get(&all_platforms);
@ -235,7 +235,7 @@ void main_opencl(std::string infile, int num_generations) {
int size_x = std::stoi(x_str);
int size_y = std::stoi(y_str);
int world[size_x * size_y];
bool *world = new bool[size_x * size_y];
// Set the data
for (int y = 0; y < size_y; y++) {
@ -257,16 +257,16 @@ void main_opencl(std::string infile, int num_generations) {
int n = size_x * size_y;
// create buffers on device (allocate space on GPU)
cl::Buffer buffer_previous(context, CL_MEM_READ_WRITE, sizeof(int) * n);
cl::Buffer buffer_new(context, CL_MEM_READ_WRITE, sizeof(int) * n);
cl::Buffer buffer_previous(context, CL_MEM_READ_WRITE, sizeof(bool) * n);
cl::Buffer buffer_new(context, CL_MEM_READ_WRITE, sizeof(bool) * n);
cl::Buffer buffer_size(context, CL_MEM_READ_WRITE, sizeof(int) * 2);
// create a queue (a queue of commands that the GPU will execute)
cl::CommandQueue queue(context, default_device);
// push write commands to queue
queue.enqueueWriteBuffer(buffer_previous, CL_TRUE, 0, sizeof(int) * n, world);
queue.enqueueWriteBuffer(buffer_new, CL_TRUE, 0, sizeof(int) * n, world); // TODO: pass empty array
queue.enqueueWriteBuffer(buffer_previous, CL_TRUE, 0, sizeof(bool) * n, world);
queue.enqueueWriteBuffer(buffer_new, CL_TRUE, 0, sizeof(bool) * n, world); // TODO: pass empty array
queue.enqueueWriteBuffer(buffer_size, CL_TRUE, 0, sizeof(int) * 2, size); // TODO: pass empty array
// RUN ZE KERNEL
@ -278,7 +278,27 @@ void main_opencl(std::string infile, int num_generations) {
queue.finish();
// read result from GPU to here
queue.enqueueReadBuffer(buffer_new, CL_TRUE, 0, sizeof(int)*n, world); // TODO: pass different empty?
queue.enqueueReadBuffer(buffer_new, CL_TRUE, 0, sizeof(bool) * n, world); // TODO: pass different empty?
// Write the result
std::ofstream result_file;
result_file.open(outfile);
result_file << size_x << "," << size_y << '\n';
for (int y = 0; y < size_y; y++) {
std::string line;
getline(world_file, line);
for (int x = 0; x < size_x; x++) {
// Convert 1 and 0 to 'x' and '.' again
line += world[y * size_x + x] ? 'x' : '.';
}
result_file << line << '\n';
}
result_file.close();
}
int main(int argc, char* argv[]) {
@ -381,6 +401,11 @@ int main(int argc, char* argv[]) {
std::cout << "Running OpenCL version" << std::endl;
}
if (mode == Mode::OCL) {
main_opencl(infile, outfile, num_generations);
return 0;
}
// Read in the start state
std::ifstream world_file;
world_file.open(infile);