From 34c7c09ca4fc01d4940ed284d2ce9ba18cb3badc Mon Sep 17 00:00:00 2001 From: fredrikr79 Date: Mon, 22 Sep 2025 17:20:04 +0200 Subject: [PATCH] ex3: task 3 --- exercise3/wave_1d_parallel.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/exercise3/wave_1d_parallel.c b/exercise3/wave_1d_parallel.c index ef9847c..5feac2f 100644 --- a/exercise3/wave_1d_parallel.c +++ b/exercise3/wave_1d_parallel.c @@ -19,7 +19,11 @@ typedef double real_t; // TASK: T1b // Declare variables each MPI process will need // BEGIN: T1b -; +int comm_size; +int comm_rank; +int_t n; + +#define I(i) ((i) + n * comm_rank) // END: T1b // Simulation parameters: size, step count, and how often to save the state. @@ -64,12 +68,13 @@ void domain_save(int_t step) { // and set the time step. void domain_initialize(void) { // BEGIN: T3 - buffers[0] = malloc((N + 2) * sizeof(real_t)); - buffers[1] = malloc((N + 2) * sizeof(real_t)); - buffers[2] = malloc((N + 2) * sizeof(real_t)); + const int_t bufsize = (comm_rank == ROOT ? N : n + 2); + buffers[0] = malloc(bufsize * sizeof(real_t)); + buffers[1] = malloc(bufsize * sizeof(real_t)); + buffers[2] = malloc(bufsize * sizeof(real_t)); - for (int_t i = 0; i < N; i++) { - U_prv(i) = U(i) = cos(2 * M_PI * i / (real_t)N); + for (int_t i = 0; i < n; i++) { + U_prv(i) = U(i) = cos(2 * M_PI * (i + offset) / (real_t)N); } // END: T3