fork download
  1. #include <mpi.h>
  2. #include <stdio.h>
  3.  
  4. int main(int argc, char *argv[]) {
  5. int myrank, root = 0;
  6. MPI_Comm comm = MPI_COMM_WORLD;
  7.  
  8. // Initialize MPI environment
  9. MPI_Init(&argc, &argv);
  10. MPI_Comm_rank(comm, &myrank);
  11.  
  12. // Define arrays for input and output
  13. double ain[30], aout[30];
  14. int ind[30];
  15.  
  16. // Structure to hold value and rank
  17. struct {
  18. double val;
  19. int rank;
  20. } in[30], out[30];
  21.  
  22. // Initialize input array
  23. for (int i = 0; i < 30; ++i) {
  24. ain[i] = (double)(myrank + i); // Each process has its own set of values
  25. in[i].val = ain[i];
  26. in[i].rank = myrank;
  27. }
  28.  
  29. // Perform reduction to find the maximum value and corresponding rank (MPI_MAXLOC)
  30. MPI_Reduce(in, out, 30, MPI_DOUBLE_INT, MPI_MAXLOC, root, comm);
  31.  
  32. // Process root collects and prints the result
  33. if (myrank == root) {
  34. printf("Results from process %d:\n", root);
  35. for (int i = 0; i < 30; ++i) {
  36. aout[i] = out[i].val;
  37. ind[i] = out[i].rank;
  38. printf("Max value at index %d: %f from process %d\n", i, aout[i], ind[i]);
  39. }
  40. }
  41.  
  42. // Finalize MPI environment
  43. MPI_Finalize();
  44. return 0;
  45. }
  46.  
Success #stdin #stdout #stderr 0.31s 40716KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected symbol in "int main"
Execution halted