fork download
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <array>
  4. #include <mpi.h>
  5. using namespace std;
  6. const int arrSize = 100;
  7.  
  8. void SumMatrices()
  9. {
  10.  
  11. }
  12.  
  13. int main(int argc, char* argv[])
  14. {
  15.  
  16. int MB[arrSize][arrSize], MC[arrSize][arrSize], MA[arrSize][arrSize];
  17. int buffer;
  18. for (int i = 0; i < arrSize; ++i)
  19. {
  20. for (int j = 0; j < arrSize; ++j)
  21. {
  22. MB[i][j] = rand() % 100 + 10;
  23. MC[i][j] = rand() % 100 + 10;
  24. }
  25. }
  26.  
  27. int procCount, rank;
  28.  
  29. MPI_Init(&argc, &argv);
  30. MPI_Comm_size(MPI_COMM_WORLD, &procCount);
  31. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  32. MPI_Status status;
  33. int H = arrSize / procCount;
  34. int beginInd = rank * H;
  35. int endInd = beginInd + H - 1;
  36. int min = INT32_MAX;
  37.  
  38. cout << "Process with rank " << rank << " has started matrix addition\n";
  39.  
  40. while(beginInd <= endInd)
  41. {
  42. for(int i = 0; i < arrSize; ++i)
  43. {
  44. int sum = MB[beginInd][i] + MC[beginInd][i];
  45. MA[beginInd][i] = sum;
  46. if (min > sum) min = sum;
  47. }
  48.  
  49. ++beginInd;
  50. }
  51.  
  52. if(rank == 0)
  53. {
  54. cout << "Proc 0 min value is " << min << "\n";
  55. for(int i = 1; i < procCount; ++i)
  56. {
  57. MPI_Recv(&buffer, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
  58. if (min > buffer) min = buffer;
  59. }
  60.  
  61. cout << "Min value is " << min;
  62. }
  63. else
  64. {
  65. buffer = min;
  66. cout << "Process " << rank << " passing message '" << min <<"' to process 0\n";
  67. MPI_Send(&buffer, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
  68. }
  69.  
  70. MPI_Finalize();
  71. return 0;
  72. }
  73.  
Success #stdin #stdout #stderr 0.26s 40768KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected symbol in "using namespace"
Execution halted