fork download
  1. #include <stdio.h> // Standard I/O header file
  2. #include <mpi.h> // MPI header file
  3.  
  4. #define N 1000 // Size of array
  5.  
  6. int main() {
  7. int i, rank, size; // Loop variable, MPI rank, MPI size
  8. int array[N], local_max, global_max = 0; // Array, local max, global max
  9.  
  10. MPI_Init(NULL, NULL); // Initialize MPI environment
  11. MPI_Comm_rank(MPI_COMM_WORLD, &rank); // Get rank of current process
  12. MPI_Comm_size(MPI_COMM_WORLD, &size); // Get total number of processes
  13.  
  14. // Initialize array with rank
  15. for (i = 0; i < N; i++) {
  16. array[i] = rank * N + i; // Each process fills its portion of the array
  17. }
  18.  
  19. // Calculate local max
  20. local_max = array[0]; // Initialize local max with first element
  21. for (i = 1; i < N; i++) {
  22. if (array[i] > local_max) {
  23. local_max = array[i]; // Update local max if current element is greater
  24. }
  25. }
  26.  
  27. // Reduce local max values to find global max
  28. MPI_Reduce(&local_max, &global_max, 1, MPI_INT, MPI_MAX, 0, MPI_COMM_WORLD);
  29.  
  30. // Print global max from root process
  31. if (rank == 0) {
  32. printf("Global max: %d\n", global_max);
  33. }
  34.  
  35. MPI_Finalize(); // Finalize MPI environment
  36. return 0;
  37. }
  38.  
Success #stdin #stdout #stderr 0.27s 40924KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected symbol in "int main"
Execution halted