Advertisement
Guest User

Untitled

a guest
Nov 16th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. #include "pch.h"
  2. #include "mpi.h"
  3. #include <stdio.h>
  4. #include <iostream>
  5. #include <math.h>
  6. #include <ctime>
  7. #include <cmath>
  8. #include <vector>
  9. #include <string>
  10. using namespace std;
  11.  
  12. bool IsRingTopology(MPI_Comm comm) {
  13. int status;
  14. MPI_Topo_test(comm, &status);
  15. if (status != MPI_CART)
  16. return false;
  17.  
  18. int ndims;
  19. MPI_Cartdim_get(comm, &ndims);
  20. if (ndims != 1)
  21. return false;
  22.  
  23. std::vector<int> dims(ndims), periods(ndims), coords(ndims);
  24. MPI_Cart_get(comm, ndims, dims.data(), periods.data(), coords.data());
  25. if (periods[0] != 1)
  26. return false;
  27.  
  28. return true;
  29. }
  30.  
  31. std::vector<int> Send(MPI_Comm ringcomm, int source, int dest,
  32. std::vector <int> message, int mess_size) {
  33. int size, rank;
  34. MPI_Comm_size(ringcomm, &size);
  35. MPI_Comm_rank(ringcomm, &rank);
  36. std::vector<int> result;
  37.  
  38. if (source > (size - 1) || source < 0 || dest >(size - 1) || dest < 0)
  39. throw - 1;
  40.  
  41. if (((rank >= source) && (rank <= dest)) ||
  42. ((dest - source < 0) && ((rank >= source) || (rank <= dest)))) {
  43. if (dest == source)
  44. return message;
  45.  
  46. int curr_dest, curr_source;
  47. MPI_Status status;
  48.  
  49. MPI_Cart_shift(ringcomm, 0, 1, &curr_source, &curr_dest);
  50.  
  51. if (rank == source) {
  52. MPI_Send(&message[0], mess_size, MPI_INT, curr_dest, 1, ringcomm);
  53. }
  54. else if (rank == dest) {
  55. result.resize(mess_size);
  56. MPI_Recv(&result[0], mess_size, MPI_INT, curr_source, 1, ringcomm, &status);
  57. }
  58. else {
  59. std::vector<int> *tmp = new std::vector<int>;
  60. tmp->resize(mess_size);
  61.  
  62. MPI_Recv(&((*tmp)[0]), mess_size, MPI_INT, curr_source, 1, ringcomm, &status);
  63. MPI_Send(&((*tmp)[0]), mess_size, MPI_INT, curr_dest, 1, ringcomm);
  64.  
  65. delete tmp;
  66. }
  67. }
  68. return result;
  69. }
  70.  
  71. int main(int argc, char* argv[]) {
  72. MPI_Comm oldcomm = MPI_COMM_WORLD;
  73. MPI_Comm ringcomm;
  74. MPI_Init(&argc, &argv); //СЮДА ЕБАТЬ?
  75. std::vector<int> dims(1), periods(1);
  76.  
  77. MPI_Comm_size(oldcomm, &dims[0]);
  78. periods[0] = 1;
  79.  
  80. MPI_Cart_create(oldcomm, 1, dims.data(), periods.data(), 0, &ringcomm);
  81.  
  82. bool IsW = IsRingTopology(ringcomm);
  83. cout << IsW;
  84. int res = (ringcomm, -1, 0, 1024, 3);
  85. cout<< res;
  86. MPI_Finalize(); //СЮДА ЕБАТЬ?
  87. return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement