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 min_local, min_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. // Each process generates random numbers
  17. srand(rank);
  18. for (int i = 0; i < ARRAY_SIZE; i++) {
  19. numbers[i] = rand() % 1000;
  20. }
  21.  
  22. // Find local minimum
  23. min_local = numbers[0];
  24. for (int i = 1; i < ARRAY_SIZE; i++) {
  25. if (numbers[i] < min_local) {
  26. min_local = numbers[i];
  27. }
  28. }
  29.  
  30. // Reduce to find global minimum
  31. MPI_Reduce(&min_local, &min_global, 1, MPI_INT, MPI_MIN, 0, MPI_COMM_WORLD);
  32.  
  33. // Process 0 prints the result
  34. if (rank == 0) {
  35. printf("Minimum number: %d\n", min_global);
  36. }
  37.  
  38. MPI_Finalize();
  39.  
  40. return 0;
  41. }
  42.  
Success #stdin #stdout #stderr 0.3s 40776KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected symbol in "int main"
Execution halted