You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
2.6 KiB

  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cmath>
  4. #include "Ttiming.h"
  5. #include <mpi.h>
  6. #include <stdio.h>
  7. using namespace std;
  8. void array_alloc(long **&arr, long arrsize)
  9. {
  10. try
  11. {
  12. arr = new long* [arrsize];
  13. arr[0] = new long [arrsize*arrsize];
  14. for(long i = 1; i < arrsize; ++i)
  15. arr[i] = arr[i-1] + arrsize;
  16. }
  17. catch (bad_alloc& ex)
  18. {
  19. cerr << "Could not allocate memory for array" << endl;
  20. exit(1);
  21. }
  22. }
  23. void vect_alloc(long *&arr, long arrsize)
  24. {
  25. try
  26. {
  27. arr = new long [arrsize];
  28. }
  29. catch (bad_alloc& ex)
  30. {
  31. cerr << "Could not allocate memory for vector" << endl;
  32. exit(1);
  33. }
  34. }
  35. void array_destroy(long **arr)
  36. {
  37. delete [] arr[0];
  38. delete [] arr;
  39. }
  40. int main(int argc, char *argv[])
  41. {
  42. long **A=NULL, **B=NULL, **C=NULL, *B_rot=NULL, *vect=NULL;
  43. long rozmiar=0;
  44. char *endptr;
  45. TTiming tt;
  46. long i, j;//,k;
  47. MPI::Status status;
  48. if (argc < 2)
  49. {
  50. cerr << "Usage: " << argv[0] << " <size>" << endl;
  51. exit(1);
  52. }
  53. rozmiar = strtol(argv[1], &endptr, 10);
  54. if (*endptr)
  55. {
  56. cerr << "Invalid array size format" << endl;
  57. exit(1);
  58. }
  59. if (rozmiar <= 0 || rozmiar > 2000)
  60. {
  61. cerr << "The number of matrix dimension must be in range [1,2000]" << endl;
  62. exit(1);
  63. }
  64. MPI::Init(argc, argv);
  65. int taskid = MPI::COMM_WORLD.Get_rank();
  66. int ntasks = MPI::COMM_WORLD.Get_size();
  67. printf("ntasks= %d : taskid= %d : Hello World!\n",ntasks,taskid);
  68. //alokacja macierzy
  69. array_alloc(A, rozmiar);
  70. if (taskid == 0)
  71. {
  72. array_alloc(B, rozmiar);
  73. vect_alloc(B_rot, rozmiar*rozmiar);
  74. }
  75. array_alloc(C, rozmiar);
  76. vect_alloc(vect, rozmiar);
  77. if (taskid == 0)
  78. {
  79. //wypelnienie macierzy A liczbami "losowymi"
  80. for (long i=0; i<rozmiar; ++i)
  81. for (long j=0; j<rozmiar; ++j)
  82. A[i][j] = (long)(sin(i) * i * j) % 10;
  83. //wypelnienie macierzy B liczbami "losowymi"
  84. for (long i=0; i<rozmiar; ++i)
  85. for (long j=0; j<rozmiar; ++j)
  86. B[i][j] = (long)(cos(j) *(i+j)) % 10;
  87. //dokonaj obracania macierzy
  88. for (long i=0; i<rozmiar; ++i)
  89. for (long j=0; j<rozmiar; ++j)
  90. B_rot[i*rozmiar+j] = B[j][i];
  91. }
  92. //wysyłanie macierzy A do wszystkich
  93. MPI::COMM_WORLD.Bcast(&A[0][0], rozmiar*rozmiar, MPI::LONG, 0);
  94. MPI::COMM_WORLD.Scatter(&B_rot[0], rozmiar, MPI::LONG, &vect[0], rozmiar, MPI::LONG, 0);
  95. /*
  96. tt.Begin();
  97. for (i=0; i<rozmiar; ++i)
  98. for (j=0; j<rozmiar; ++j)
  99. {
  100. C[i][j] = 0;
  101. for (long k=0; k<rozmiar; ++k)
  102. C[i][j] += A[i][k]*B[k][j];
  103. }
  104. long elapsed = tt.End();
  105. cout << "Time: " << elapsed << " ms" << endl;
  106. array_destroy(A);
  107. array_destroy(B);
  108. array_destroy(C);
  109. */
  110. cout << taskid << ": " << vect[2] << endl;
  111. MPI::Finalize();
  112. exit(0);
  113. }