fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <mpi.h>
  4.  
  5. #define SIZE 8
  6.  
  7. int main(int argc, char *argv[]) {
  8. int rank, size;
  9. int numbers[SIZE][3];
  10. int lsum = 0, gsum = 0; //local_sum and global_sum
  11. int count_per_process;
  12. int *recvbuf;
  13.  
  14. // MPI environment
  15. MPI_Init(&argc, &argv);
  16. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  17. MPI_Comm_size(MPI_COMM_WORLD, &size);
  18.  
  19. // Process 0 reads the input numbers
  20. if (rank == 0) {
  21. printf("Enter the numbers:\n"); //inputs 3 integers
  22. for (int i = 0; i < SIZE; i++) {
  23. scanf("%d %d %d", &numbers[i][0], &numbers[i][1], &numbers[i][2]);
  24. }
  25. }
  26.  
  27. MPI_Bcast(numbers, SIZE * 3, MPI_INT, 0, MPI_COMM_WORLD);
  28.  
  29. count_per_process = SIZE / size;
  30.  
  31. // Each process calculates its local sum
  32. for (int i = rank * count_per_process; i < (rank + 1) * count_per_process; i++) {
  33. for (int j = 0; j < 3; j++) {
  34. lsum += numbers[i][j];
  35. }
  36. printf("Process %d partial sum of set %d: %d\n", rank, i, lsum);
  37. }
  38.  
  39. // Reduce local sums to global sum at process 0
  40. MPI_Reduce(&lsum, &gsum, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
  41.  
  42. //prints the global sum
  43. if (rank == 0) {
  44. printf("Global sum: %d\n", gsum);
  45. }
  46.  
  47. MPI_Finalize();
  48. return 0;
  49. }
  50.  
Success #stdin #stdout #stderr 0.27s 40508KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected symbol in "int main"
Execution halted