fork download
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <mpi.h>
  5.  
  6. #define ARRAY_SIZE 1000
  7.  
  8. int main(int argc, char** argv) {
  9. int rank, size;
  10. int numbers[ARRAY_SIZE];
  11. int local_min, global_min;
  12.  
  13. MPI_Init(&argc, &argv);
  14. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  15. MPI_Comm_size(MPI_COMM_WORLD, &size);
  16.  
  17. // Each process generates random numbers
  18. srand(rank); // Seed with process rank for different random numbers
  19. for (int i = 0; i < ARRAY_SIZE; ++i) {
  20. numbers[i] = rand();
  21. }
  22.  
  23. // Each process finds its local minimum
  24. local_min = numbers[0];
  25. for (int i = 1; i < ARRAY_SIZE; ++i) {
  26. if (numbers[i] < local_min) {
  27. local_min = numbers[i];
  28. }
  29. }
  30.  
  31. // Reduce local minimums to find the global minimum
  32. MPI_Reduce(&local_min, &global_min, 1, MPI_INT, MPI_MIN, 0, MPI_COMM_WORLD);
  33.  
  34. // Print the global minimum
  35. if (rank == 0) {
  36. printf("Global minimum: %d\n", global_min);
  37. }
  38.  
  39. MPI_Finalize();
  40. return 0;
  41. }
  42.  
  43.  
Success #stdin #stdout #stderr 0.27s 40732KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected symbol in "int main"
Execution halted