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 max_local, max_global;
  10. int *data = NULL;
  11. int i;
  12.  
  13. MPI_Init(&argc, &argv);
  14. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  15. MPI_Comm_size(MPI_COMM_WORLD, &size);
  16.  
  17. // Allocate memory for data array
  18. data = (int*)malloc(sizeof(int) * ARRAY_SIZE);
  19.  
  20. // Generate random numbers
  21. srand(rank); // seed based on rank for different numbers in each process
  22. for (i = 0; i < ARRAY_SIZE; i++) {
  23. data[i] = rand();
  24. }
  25.  
  26. // Find local max
  27. max_local = data[0];
  28. for (i = 1; i < ARRAY_SIZE; i++) {
  29. if (data[i] > max_local) {
  30. max_local = data[i];
  31. }
  32. }
  33.  
  34. // Reduce all local max values to find global max
  35. MPI_Reduce(&max_local, &max_global, 1, MPI_INT, MPI_MAX, 0, MPI_COMM_WORLD);
  36.  
  37. // Print result
  38. if (rank == 0) {
  39. printf("The maximum number is: %d\n", max_global);
  40. }
  41.  
  42. // Cleanup
  43. free(data);
  44. MPI_Finalize();
  45.  
  46. return 0;
  47. }
  48.  
Success #stdin #stdout #stderr 0.32s 40404KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected symbol in "int main"
Execution halted