Advertisement
kevlinsky

Untitled

Nov 28th, 2020
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <mpi.h>
  3. #include <cmath>
  4.  
  5. int main(int argc, char *argv[]) {
  6. int size = 2;
  7. int N = 3;
  8. int groupOfProcesses1[N], groupOfProcesses2[N], groupOfProcesses3[N], groupOfProcesses4[N];
  9.  
  10. int world_rank;
  11. int world_size;
  12.  
  13. MPI_Init(&argc, &argv);
  14. MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
  15. MPI_Comm_size(MPI_COMM_WORLD, &world_size);
  16.  
  17. MPI_Group worldGroup;
  18. MPI_Group newGroup1, newGroup2, newGroup3, newGroup4;
  19. MPI_Comm_group(MPI_COMM_WORLD, &worldGroup);
  20.  
  21. int index1 = 0, index2 = 0, index3 = 0, index4 = 0;
  22. for (int i = 0; i < world_size; ++i) {
  23. if (i >= 0 && i < 3){
  24. groupOfProcesses1[index1] = i;
  25. index1++;
  26. }
  27. if (i >= 3 && i < 6){
  28. groupOfProcesses2[index2] = i;
  29. index2++;
  30. }
  31. if (i >= 6 && i < 9){
  32. groupOfProcesses3[index3] = i;
  33. index3++;
  34. }
  35. if (i >= 9 && i < 12){
  36. groupOfProcesses4[index4] = i;
  37. index4++;
  38. }
  39. }
  40.  
  41. MPI_Group_incl(worldGroup, N, groupOfProcesses1, &newGroup1);
  42. MPI_Group_incl(worldGroup, N, groupOfProcesses2, &newGroup2);
  43. MPI_Group_incl(worldGroup, N, groupOfProcesses3, &newGroup3);
  44. MPI_Group_incl(worldGroup, N, groupOfProcesses4, &newGroup4);
  45.  
  46. MPI_Comm newComm1, newComm2, newComm3, newComm4;
  47. MPI_Comm_create(MPI_COMM_WORLD, newGroup1, &newComm1);
  48. MPI_Comm_create(MPI_COMM_WORLD, newGroup2, &newComm2);
  49. MPI_Comm_create(MPI_COMM_WORLD, newGroup3, &newComm3);
  50. MPI_Comm_create(MPI_COMM_WORLD, newGroup4, &newComm4);
  51.  
  52. int a[size];
  53. int a_received[size * N];
  54.  
  55. int newRank;
  56.  
  57. if (newComm1 != MPI_COMM_NULL) {
  58. MPI_Comm_rank(newComm1, &newRank);
  59. for (int i = 0; i < size; ++i) {
  60. a[i] = 1;
  61. }
  62. MPI_Gather(&a, size, MPI_INT, a_received, size, MPI_INT, 0, newComm1);
  63. }
  64.  
  65. if (newComm2 != MPI_COMM_NULL) {
  66. MPI_Comm_rank(newComm2, &newRank);
  67. for (int i = 0; i < size; ++i) {
  68. a[i] = 2;
  69. }
  70. MPI_Gather(&a, size, MPI_INT, a_received, size, MPI_INT, 0, newComm2);
  71. }
  72.  
  73. if (newComm3 != MPI_COMM_NULL) {
  74. MPI_Comm_rank(newComm3, &newRank);
  75. for (int i = 0; i < size; ++i) {
  76. a[i] = 3;
  77. }
  78. MPI_Gather(&a, size, MPI_INT, a_received, size, MPI_INT, 0, newComm3);
  79. }
  80.  
  81. if (newComm4 != MPI_COMM_NULL) {
  82. MPI_Comm_rank(newComm4, &newRank);
  83. for (int i = 0; i < size; ++i) {
  84. a[i] = 4;
  85. }
  86. MPI_Gather(&a, size, MPI_INT, a_received, size, MPI_INT, 0, newComm4);
  87. }
  88.  
  89. if (newRank == 0 && newComm1 != MPI_COMM_NULL) {
  90. printf("Thread 0 from NEW_COMM_1 received a[i] = ");
  91. for (int i = 0; i < size * N; ++i) {
  92. printf("%d ", a_received[i]);
  93. }
  94. printf("\n");
  95. }
  96.  
  97. if (newRank == 0 && newComm2 != MPI_COMM_NULL) {
  98. printf("Thread 0 from NEW_COMM_2 received a[i] = ");
  99. for (int i = 0; i < size * N; ++i) {
  100. printf("%d ", a_received[i]);
  101. }
  102. printf("\n");
  103. }
  104.  
  105. if (newRank == 0 && newComm3 != MPI_COMM_NULL) {
  106. printf("Thread 0 from NEW_COMM_3 received a[i] = ");
  107. for (int i = 0; i < size * N; ++i) {
  108. printf("%d ", a_received[i]);
  109. }
  110. printf("\n");
  111. }
  112.  
  113. if (newRank == 0 && newComm4 != MPI_COMM_NULL) {
  114. printf("Thread 0 from NEW_COMM_4 received a[i] = ");
  115. for (int i = 0; i < size * N; ++i) {
  116. printf("%d ", a_received[i]);
  117. }
  118. printf("\n");
  119. }
  120.  
  121. MPI_Comm interComm12, interComm21, interComm34, interComm43;
  122. if (newComm1 != MPI_COMM_NULL) MPI_Intercomm_create(MPI_COMM_SELF, 0, MPI_COMM_WORLD, 3, 0, &interComm12);
  123. if (newComm2 != MPI_COMM_NULL) MPI_Intercomm_create(MPI_COMM_SELF, 0, MPI_COMM_WORLD, 0, 0, &interComm21);
  124. if (newComm3 != MPI_COMM_NULL) MPI_Intercomm_create(MPI_COMM_SELF, 0, MPI_COMM_WORLD, 9, 1, &interComm34);
  125. if (newComm4 != MPI_COMM_NULL) MPI_Intercomm_create(MPI_COMM_SELF, 0, MPI_COMM_WORLD, 6, 1, &interComm43);
  126.  
  127. int interCommArray[size * N];
  128.  
  129. if (newComm1 != MPI_COMM_NULL && newRank == 0){
  130. MPI_Status status;
  131. MPI_Send(a_received, size * N, MPI_INT, 0, 0, interComm12);
  132. MPI_Recv(interCommArray, size * N, MPI_INT, 0, 0, interComm12, &status);
  133. printf("Thread from NEW_COMM_1 received from thread from NEW_COMM_2 through interComm12 a[i] = ");
  134. for (int i = 0; i < size * N; ++i) {
  135. printf("%d ", interCommArray[i]);
  136. }
  137. printf("\n");
  138. }
  139.  
  140. if (newComm2 != MPI_COMM_NULL && newRank == 0){
  141. MPI_Status status;
  142. MPI_Send(a_received, size * N, MPI_INT, 0, 0, interComm21);
  143. MPI_Recv(interCommArray, size * N, MPI_INT, 0, 0, interComm21, &status);
  144. printf("Thread from NEW_COMM_2 received from thread from NEW_COMM_1 through interComm21 a[i] = ");
  145. for (int i = 0; i < size * N; ++i) {
  146. printf("%d ", interCommArray[i]);
  147. }
  148. printf("\n");
  149. }
  150.  
  151. if (newComm3 != MPI_COMM_NULL && newRank == 0){
  152. MPI_Status status;
  153. MPI_Send(a_received, size * N, MPI_INT, 0, 1, interComm34);
  154. MPI_Recv(interCommArray, size * N, MPI_INT, 0, 1, interComm34, &status);
  155. printf("Thread from NEW_COMM_3 received from thread from NEW_COMM_4 through interComm34 a[i] = ");
  156. for (int i = 0; i < size * N; ++i) {
  157. printf("%d ", interCommArray[i]);
  158. }
  159. printf("\n");
  160. }
  161.  
  162. if (newComm4 != MPI_COMM_NULL && newRank == 0){
  163. MPI_Status status;
  164. MPI_Send(a_received, size * N, MPI_INT, 0, 1, interComm43);
  165. MPI_Recv(interCommArray, size * N, MPI_INT, 0, 1, interComm43, &status);
  166. printf("Thread from NEW_COMM_4 received from thread from NEW_COMM_3 through interComm43 a[i] = ");
  167. for (int i = 0; i < size * N; ++i) {
  168. printf("%d ", interCommArray[i]);
  169. }
  170. printf("\n");
  171. }
  172.  
  173. if (newComm1 != MPI_COMM_NULL) {
  174. MPI_Comm_free(&newComm1);
  175. }
  176.  
  177. if (newComm2 != MPI_COMM_NULL) {
  178. MPI_Comm_free(&newComm2);
  179. }
  180.  
  181. if (newComm3 != MPI_COMM_NULL) {
  182. MPI_Comm_free(&newComm3);
  183. }
  184.  
  185. if (newComm4 != MPI_COMM_NULL) {
  186. MPI_Comm_free(&newComm4);
  187. }
  188.  
  189. MPI_Group_free(&newGroup1);
  190. MPI_Group_free(&newGroup2);
  191. MPI_Group_free(&newGroup3);
  192. MPI_Group_free(&newGroup4);
  193. MPI_Finalize();
  194. return 0;
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement