Advertisement
Guest User

Untitled

a guest
Dec 21st, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #include <mpi.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include <signal.h>
  6.  
  7. #define MYTAG 1
  8. int myid, j;
  9. double startwtime = 0.0, endwtime;
  10.  
  11. int main(int argc, char *argv[]) {
  12. int rank, from, to;
  13. MPI_Status status;
  14. int fact = 0;
  15. int myid;
  16. MPI_Init(&argc, &argv);
  17. MPI_Comm_size(MPI_COMM_WORLD, &rank);
  18. MPI_Comm_rank(MPI_COMM_WORLD, &myid);
  19.  
  20. if (myid == 0) { //для 0 процесса
  21.  
  22. from = rank - 1; // от кого принимаем
  23. to = 1; // кому отдаем
  24. fact = fact + myid; //число увеличивающееся на 1 при передаче
  25. MPI_Send(&fact, 1, MPI_INT, to, 0, MPI_COMM_WORLD);//отправка
  26. MPI_Recv(&fact, 1, MPI_INT, from, 0, MPI_COMM_WORLD, &status);//прием
  27. }
  28. else{
  29. if (myid == rank - 1) { // для 1 процесса
  30. from = rank - 2;
  31. to = 0;
  32. MPI_Recv(&fact, 1, MPI_INT, from, 0, MPI_COMM_WORLD, &status);
  33. fact += myid;
  34. MPI_Send(&fact, 1, MPI_INT, to, 0, MPI_COMM_WORLD);
  35.  
  36.  
  37. } else { // для остальных
  38. from = myid - 1;
  39. to = myid + 1;
  40. MPI_Recv(&fact, 1, MPI_INT, from, 0, MPI_COMM_WORLD, &status);
  41. fact += myid;
  42. MPI_Send(&fact, 1, MPI_INT, to, 0, MPI_COMM_WORLD);
  43. }
  44. }
  45. if (myid == 0) // в 0 процессе выводим
  46. printf("fact = %d", fact);
  47. MPI_Finalize();
  48. return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement