ex3: done

This commit is contained in:
2025-09-22 17:42:36 +02:00
parent a43b80dbce
commit 68b665bb91

View File

@@ -74,6 +74,12 @@ void domain_initialize(void) {
buffers[1] = malloc(bufsize * sizeof(real_t));
buffers[2] = malloc(bufsize * sizeof(real_t));
int_t offset = 0;
for (int i = 0; i < comm_rank; i++) {
int proc_n = N / comm_size + (i < N % comm_size);
offset += proc_n;
}
for (int_t i = 0; i < n; i++) {
U_prv(i) = U(i) = cos(2 * M_PI * (i + offset) / (real_t)N);
}
@@ -125,6 +131,8 @@ void boundary_condition(void) {
// Communicate the border between processes.
void border_exchange(void) {
// BEGIN: T5
if (comm_size == 1)
return;
int left = (comm_size + comm_rank - 1) % comm_size;
int right = (comm_rank + 1) % comm_size;
@@ -143,6 +151,8 @@ void border_exchange(void) {
// to root and assemble it in the root buffer
void send_data_to_root() {
// BEGIN: T7
if (comm_size == 1)
return;
if (comm_rank != ROOT) {
MPI_Send(&U(0), n, MPI_DOUBLE, ROOT, 0, MPI_COMM_WORLD);
} else {
@@ -197,6 +207,7 @@ int main(int argc, char **argv) {
// BEGIN: T2
if (comm_rank == ROOT)
gettimeofday(&t_start, NULL);
simulate();
MPI_Barrier(MPI_COMM_WORLD);