Implement a reduction in mpi_manual_reduce.c to compute the sum of all the processor IDs using MPI send/recv routines ra

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

Implement a reduction in mpi_manual_reduce.c to compute the sum of all the processor IDs using MPI send/recv routines ra

Post by answerhappygod »

Implement a reduction in mpi_manual_reduce.c to compute the sum of all the processor IDs using MPI send/recv routines rather than the built-in MPI reduction. Your code should achieve a time conplexity of O(log n). As an example, 5 number of processors used should output a 10 (10 = 0+1+2+3+4).
#include <stdio.h>
#include <stdlib.h>
#include "mpi.h"

int main(int argc, char* argv[]) {
int numProcs, myProcID;

MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &numProcs);
MPI_Comm_rank(MPI_COMM_WORLD, &myProcID);

int local_val = myProcID;//local value

/* TODO: Implement a reduction to compute the sum of all the proc
IDs using MPI send/recv routines rather than the built-in MPI
reduction. Your implementation should achieve a time complecity of O(log n). You should not use the built-in reduce method. */


if(myProcID == 0) {//check result
int expected = 0;
for(int i = 0; i < numProcs; ++i)
expected += i;
printf("The final value is %d, expected %d\n", local_val, expected);
}

MPI_Finalize();
return 0;
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply