diff --git a/Makefile b/Makefile index 181d4cc..205ec19 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/gol.cl b/gol.cl index 93c241f..13e0dc1 100644 --- a/gol.cl +++ b/gol.cl @@ -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 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);