fork download
  1. #include <stdio.h>
  2. #include <mpi.h>
  3. int main(int argc, char **argv) {
  4. // Initialize MPI
  5. MPI_Init(&argc, &argv);
  6. // Get the total number of processes and the rank of the current process
  7. int world_size, world_rank;
  8. MPI_Comm_size(MPI_COMM_WORLD, &world_size);
  9. MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
  10. // Define the range of numbers to sum
  11. int N = 100;
  12. int local_sum = 0;
  13. int global_sum;
  14. // Calculate the local sum for each process
  15. for (int i = world_rank + 1; i <= N; i += world_size) {
  16. local_sum += i;
  17. }
  18. // Perform a global sum using MPI_Reduce
  19. MPI_Reduce(&local_sum, &global_sum, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
  20. // Output the result
  21. if (world_rank == 0) {
  22. printf("The sum of numbers from 1 to %d is: %d\n", N, global_sum);
  23. }
  24. // Finalize MPI
  25. MPI_Finalize();
  26. return 0;
  27. }
  28.  
  29.  
Success #stdin #stdout #stderr 0.28s 40856KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected symbol in "int main"
Execution halted