diff --git a/exercise7/wave_2d_parallel.cu b/exercise7/wave_2d_parallel.cu index dacfefd..ffdf273 100644 --- a/exercise7/wave_2d_parallel.cu +++ b/exercise7/wave_2d_parallel.cu @@ -1,12 +1,12 @@ +#include +#include +#include +#include #include #include #include -#include -#include -#include #include -#include -#include +#include // TASK: T1 // Include the cooperative groups library @@ -14,11 +14,9 @@ ; // END: T1 - // Convert 'struct timeval' into seconds in double prec. floating point #define WALLTIME(t) ((double)(t).tv_sec + 1e-6 * (double)(t).tv_usec) - // Option to change numerical precision typedef int64_t int_t; typedef double real_t; @@ -36,7 +34,7 @@ int_t // Wave equation parameters, time step is derived from the space step const real_t - c = 1.0, + c = 1.0, dx = 1.0, dy = 1.0; real_t @@ -46,213 +44,182 @@ real_t real_t *buffers[3] = { NULL, NULL, NULL }; -#define U_prv(i,j) buffers[0][((i)+1)*(N+2)+(j)+1] -#define U(i,j) buffers[1][((i)+1)*(N+2)+(j)+1] -#define U_nxt(i,j) buffers[2][((i)+1)*(N+2)+(j)+1] +#define U_prv(i, j) buffers[0][((i) + 1) * (N + 2) + (j) + 1] +#define U(i, j) buffers[1][((i) + 1) * (N + 2) + (j) + 1] +#define U_nxt(i, j) buffers[2][((i) + 1) * (N + 2) + (j) + 1] // END: T1b -#define cudaErrorCheck(ans) { gpuAssert((ans), __FILE__, __LINE__); } -inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort=true) -{ - if (code != cudaSuccess) { - fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line); - if (abort) exit(code); - } +#define cudaErrorCheck(ans) \ + { \ + gpuAssert((ans), __FILE__, __LINE__); \ + } +inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort = true) { + if (code != cudaSuccess) { + fprintf(stderr, "GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line); + if (abort) + exit(code); + } } - // Rotate the time step buffers. -void move_buffer_window ( void ) -{ - real_t *temp = buffers[0]; - buffers[0] = buffers[1]; - buffers[1] = buffers[2]; - buffers[2] = temp; +void move_buffer_window(void) { + real_t *temp = buffers[0]; + buffers[0] = buffers[1]; + buffers[1] = buffers[2]; + buffers[2] = temp; } - // Save the present time step in a numbered file under 'data/' -void domain_save ( int_t step ) -{ - char filename[256]; +void domain_save(int_t step) { + char filename[256]; - // Ensure output directory exists (ignore error if it already exists) - if (mkdir("data", 0755) != 0 && errno != EEXIST) { - perror("mkdir data"); - exit(EXIT_FAILURE); + // Ensure output directory exists (ignore error if it already exists) + if (mkdir("data", 0755) != 0 && errno != EEXIST) { + perror("mkdir data"); + exit(EXIT_FAILURE); + } + + snprintf(filename, sizeof(filename), "data/%05" PRId64 ".dat", step); + + FILE *out = fopen(filename, "wb"); + if (out == NULL) { + perror("fopen output file"); + fprintf(stderr, "Failed to open '%s' for writing.\n", filename); + exit(EXIT_FAILURE); + } + + for (int_t i = 0; i < M; ++i) { + size_t written = fwrite(&U(i, 0), sizeof(real_t), (size_t)N, out); + if (written != (size_t)N) { + perror("fwrite"); + fclose(out); + exit(EXIT_FAILURE); } + } - snprintf(filename, sizeof(filename), "data/%05" PRId64 ".dat", step); - - FILE *out = fopen(filename, "wb"); - if (out == NULL) { - perror("fopen output file"); - fprintf(stderr, "Failed to open '%s' for writing.\n", filename); - exit(EXIT_FAILURE); - } - - for ( int_t i = 0; i < M; ++i ) { - size_t written = fwrite ( &U(i,0), sizeof(real_t), (size_t)N, out ); - if ( written != (size_t)N ) { - perror("fwrite"); - fclose(out); - exit(EXIT_FAILURE); - } - } - - if ( fclose(out) != 0 ) { - perror("fclose"); - exit(EXIT_FAILURE); - } + if (fclose(out) != 0) { + perror("fclose"); + exit(EXIT_FAILURE); + } } - // TASK: T4 // Get rid of all the memory allocations -void domain_finalize ( void ) -{ -// BEGIN: T4 - free ( buffers[0] ); - free ( buffers[1] ); - free ( buffers[2] ); -// END: T4 +void domain_finalize(void) { + // BEGIN: T4 + free(buffers[0]); + free(buffers[1]); + free(buffers[2]); + // END: T4 } - // TASK: T6 // Neumann (reflective) boundary condition // BEGIN: T6 -void boundary_condition ( void ) -{ - for ( int_t i=0; i