fork download
  1.  
  2. #include <mpi.h>
  3. #include <iostream>
  4. #include <cstdlib>
  5. #include <ctime>
  6. #include <vector>
  7.  
  8. using namespace std;
  9.  
  10. int main(int argc, char* argv[]) {
  11. MPI_Init(&argc, &argv);
  12. int world_rank;
  13. MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
  14. int world_size;
  15. MPI_Comm_size(MPI_COMM_WORLD, &world_size);
  16.  
  17. int m = 100;
  18. srand(time(NULL) + world_rank); // Seed the random number generator with the process rank
  19.  
  20. // Generate m random numbers
  21. vector<int> numbers(m);
  22. for (int i = 0; i < m; i++) {
  23. numbers[i] = rand() % 901 + 100;
  24. }
  25.  
  26. // Print the random numbers to the console
  27. cout << "Process " << world_rank << " random numbers: ";
  28. for (int i = 0; i < m; i++) {
  29. cout << numbers[i] << " ";
  30. }
  31. cout << endl;
  32.  
  33. // Compute the sum of the random numbers
  34. int sum = 0;
  35. for (int i = 0; i < m; i++) {
  36. sum += numbers[i];
  37. }
  38.  
  39. // Print the sum to the console
  40. cout << "Process " << world_rank << " sum: " << sum << endl;
  41.  
  42. // Determine the time it takes for the process to complete the job
  43. double start_time = MPI_Wtime();
  44.  
  45. // Reduce all sums to the root process
  46. int global_sum;
  47. MPI_Reduce(&sum, &global_sum, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
  48.  
  49. // Print the time to the console
  50. double end_time = MPI_Wtime();
  51. cout << "Process " << world_rank << " time: " << (end_time - start_time) << " seconds" << endl;
  52.  
  53. // Print the global sum to the console
  54. if (world_rank == 0) {
  55. cout << "Global sum: " << global_sum << endl;
  56. }
  57.  
  58. MPI_Finalize();
  59. return 0;
  60. }
  61.  
  62.  
Success #stdin #stdout #stderr 0.36s 40472KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected symbol in "using namespace"
Execution halted