Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. //
  2. // main.cpp
  3. // Coded Computation
  4. //
  5. // Created by Pardis Malekzadeh on 6/23/17.
  6. // Copyright © 2017 Pardis Malekzadeh. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <mpi.h>
  11. #include <stdlib.h>
  12.  
  13. using namespace std;
  14.  
  15. const int n = 2;
  16. const int p = 8;
  17. const int ntasks = 4;
  18. const int nsects = p/ntasks;
  19. int req_results = 2;
  20. double F[p][n];
  21.  
  22.  
  23. void slave(int myrank){
  24.  
  25. int x[n];
  26. cout << "Im a slave, my rank is"<<myrank<<"this is part 1"<<endl;
  27.  
  28. /*MPI::COMM_WORLD.Recv(x, n, MPI::DOUBLE,0, myrank);*/
  29. MPI::COMM_WORLD.Bcast(x, n,
  30. MPI::DOUBLE, 0);
  31.  
  32. cout << "Im a slave, my rank is"<<myrank<<"this is part 2"<<endl;
  33.  
  34. /////////////////
  35. double result[nsects];
  36.  
  37. for (int i=0;i<nsects;i++){
  38. result[i]=0.0;
  39. }
  40.  
  41. for (int i=0;i<nsects;i++){
  42. for (int j=0;j<n;j++){
  43. int StartIndex = (myrank-1)*nsects;
  44. result[i]+= F[StartIndex+i][j]*x[j];
  45. }
  46. }
  47. /////////////////
  48.  
  49. MPI::COMM_WORLD.Send(result, nsects, MPI::DOUBLE, 0, myrank+ntasks);
  50.  
  51. cout << "Im a slave, my rank is"<<myrank<<"this is part 3"<<endl;
  52. }
  53.  
  54.  
  55. void master(){
  56. double x[n];
  57.  
  58. for (int i = 0; i < n; ++i)
  59. {
  60. x[i] = i;
  61. }
  62.  
  63.  
  64. /*for (int i = 0; i < n; ++i)
  65. {
  66. cin >> x[i];
  67. }*/
  68.  
  69. cout << "Im master,this is part 1"<<endl;
  70.  
  71. /*for (int rank = 1; rank <= ntasks; ++rank) {
  72. cout << "rank " << rank << endl;
  73. MPI::COMM_WORLD.Send(x, n, MPI::DOUBLE, rank, rank);
  74.  
  75.  
  76. }*/
  77.  
  78. MPI::COMM_WORLD.Bcast(x, n,
  79. MPI::DOUBLE, 0);
  80.  
  81. cout << "Im master,this is part 2"<<endl;
  82.  
  83. double result[100];
  84. for (int i = 0; i < p; ++i) {
  85. result[i] = 0;
  86. }
  87.  
  88. for (int i = 0; i < p; ++i) {
  89. cout<<result[i];
  90. }
  91. MPI::Request requests[ntasks];
  92.  
  93. for (int rank = 1; rank <= ntasks; ++rank) {
  94. cout << (rank-1)*nsects << endl;
  95. requests[rank-1] = MPI::COMM_WORLD.Irecv(&result[(rank-1)*nsects], nsects, MPI::DOUBLE, rank, rank+ntasks);
  96. }
  97.  
  98.  
  99. cout << "Im master,this is part 3"<<endl;
  100. int recv_results = 0;
  101.  
  102. do {
  103. for (int rank = 1; rank <= ntasks; ++rank) {
  104. if (requests[rank-1].Test())
  105. recv_results++;
  106. }
  107. } while (recv_results < req_results);
  108.  
  109.  
  110. cout << "Im master,this is part 4"<<endl;
  111.  
  112. }
  113.  
  114.  
  115.  
  116. int main(int argc, char* argv[]) {
  117.  
  118. cout<< "Hello!" << '\n';
  119.  
  120. for (int i = 0; i < p; ++i){
  121. for (int j = 0; j < n; ++j) {
  122. F[i][j] = rand() % 100 + 1;
  123. }
  124. }
  125.  
  126.  
  127.  
  128.  
  129. MPI::Init(argc, argv);
  130.  
  131. int myrank = MPI::COMM_WORLD.Get_rank();
  132.  
  133. int np = MPI::COMM_WORLD.Get_size();
  134.  
  135.  
  136.  
  137. if (myrank == 0) {
  138. cout << "Running on "<< np << " Processes "<< '\n';
  139. master();
  140.  
  141. } else {
  142. cout << "Greetings from process " << myrank << '\n';
  143. slave(myrank);
  144. }
  145.  
  146. MPI::Finalize();
  147. return 0;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement