fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<mpi.h>
  4.  
  5. #define ARRAY_SIZE 1000
  6.  
  7. int main(int argc, char *argv[]) {
  8. int rank, size;
  9. int numbers[ARRAY_SIZE];
  10. int max_local, max_global;
  11.  
  12. MPI_Init(&argc, &argv)
  13. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  14. MPI_Comm_size(MPI_COMM_WORLD, &size);
  15.  
  16. srand(rank + 1);
  17.  
  18. for(i = 0; i < ARRAY_SIZE; i++) {
  19. numbers[i] = rand();
  20.  
  21. }
  22.  
  23.  
  24. max_local = numbers[0];
  25. for (int i = 1; i < ARRAY_SIZE; i++) {
  26. if (numbers[i] > max_local) {
  27. max_local = numbers[i];
  28. }
  29. }
  30.  
  31.  
  32. MPI_Reduce(&max_local, &max_global, 1, MPI_INT, MPI_MAX, 0, MPI_COMM_WORLD);
  33.  
  34.  
  35. if (rank == 0) {
  36. printf("Global maximum number: %d\n", max_global);
  37. }
  38.  
  39. MPI_Finalize();
  40.  
  41. return 0;
  42. }
Success #stdin #stdout #stderr 0.32s 40624KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected symbol in "int main"
Execution halted