Advertisement
elsemTim

AlltoallvYkobi3d

May 5th, 2017
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #include <mpi.h>
  4.  
  5. #define WIDTH 5
  6. #define HEIGHT 6
  7. #define DEPTH 7
  8. #define ITERS 1
  9.  
  10. int main(int argc, char** argv) {
  11. MPI_Init(&argc, &argv);
  12.  
  13. int proc_number = 0; //номер процесса
  14. int proc_count = 0; //кол. проц.
  15. MPI_Comm_rank(MPI_COMM_WORLD, &proc_number);
  16. MPI_Comm_size(MPI_COMM_WORLD, &proc_count);
  17.  
  18. if (proc_count > WIDTH) {
  19. printf("Too many proccess for WIDTH of %d", WIDTH);
  20. MPI_Finalize();
  21. return 0;
  22. }
  23.  
  24. int width = 0;
  25. int height = HEIGHT;
  26. int depth = DEPTH;
  27. width = WIDTH / proc_count;
  28. if (proc_number < WIDTH % proc_count) {
  29. width += 1;
  30. }
  31. width += 2;
  32. height += 2;
  33. depth +=2;
  34. //---
  35.  
  36. double arr[width][height][depth];
  37. double arrNew[width][height][depth];
  38.  
  39. int i, j, t;
  40. for (i = 0; i < width; i++) {
  41. for (j = 0; j < height; j++) {
  42. for (t = 0; t < depth; t++) {
  43. if ((proc_number == 0 && i == 0) || (j == 0) || (t == 0) ||
  44. (proc_number == proc_count - 1 && i == width - 1) || (j == height - 1) || (t == depth - 1)) {
  45. arr[i][j][t] = 1;
  46. arrNew[i][j][t] = 1;
  47. } else {
  48. arr[i][j][t] = 0;
  49. arrNew[i][j][t] = 0;
  50. }
  51. }
  52. }
  53.  
  54. //---
  55.  
  56. int iter;
  57. for (iter = 0; iter < ITERS; iter++) {
  58. for (i = 1; i < width - 1; i++) {
  59. for (j = 1; j < height - 1; j++) {
  60. for (t = 1; t < depth - 1; t++) {
  61. arrNew[i][j][t] = arr[i][j - 1][t] + arr[i][j + 1][t];
  62. arrNew[i][j][t] += arr[i - 1][j][t] + arr[i + 1][j][t];
  63. arrNew[i][j][t] += arr[i][j][t - 1] + arr[i][j][t + 1];
  64. arrNew[i][j][t] /= 6.0;
  65. }
  66. }
  67. }
  68.  
  69.  
  70. //---
  71. int tt;
  72. int displs[proc_count];
  73. if (proc_number > 0) {
  74. int counts[proc_count];
  75. for (tt = 0; tt < proc_count; tt++) {
  76. counts[tt] = 0;
  77. }
  78. counts[proc_number - 1] = depth - 2;
  79. for (tt = 0; tt < proc_count; tt++) {
  80. displs[tt] = 0;
  81. }
  82. for (i = 1; i < height - 1; i++){
  83. MPI_Alltoallv(&arrNew[1][i][1], counts, displs, MPI_DOUBLE,
  84. &arr[0][i][1], counts, displs, MPI_DOUBLE, MPI_COMM_WORLD);
  85. }
  86. }
  87.  
  88. if (proc_number < proc_count - 1) {
  89. int counts[proc_count];
  90. for (tt = 0; tt < proc_count; tt++) {
  91. counts[tt] = 0;
  92. }
  93. counts[proc_number + 1] = depth - 2;
  94. int displs[proc_count];
  95. for (tt = 0; tt < proc_count; tt++) {
  96. displs[tt] = 0;
  97. }
  98. for (i = 1; i < height -1; i++) {
  99. MPI_Alltoallv(&arrNew[width - 2][i][1], counts, displs, MPI_DOUBLE,
  100. &arr[width - 1][i][1], counts, displs, MPI_DOUBLE, MPI_COMM_WORLD);
  101. }
  102. }
  103.  
  104.  
  105. for (i = 1 ; i < width - 1; i++)
  106. for (j = 1 ; j < height - 1; j++)
  107. for (t = 1; t < depth - 1; t++)
  108. arr[i][j][t]=arrNew[i][j][t];
  109.  
  110. }
  111.  
  112. //---
  113.  
  114. if (proc_number == 0) {
  115. fclose(fopen("output.txt", "w"));
  116. }
  117.  
  118. int k;
  119. for (k = 0; k < proc_count; k++) {
  120. MPI_Barrier(MPI_COMM_WORLD);
  121.  
  122. if (k == proc_number) {
  123. FILE* out = fopen("output.txt", "a");
  124.  
  125. int start = 1;
  126. if (proc_number == 0) {
  127. start = 0;
  128. }
  129. int end = width - 1;
  130. if (proc_number == proc_count - 1) {
  131. end = width;
  132. }
  133. for (i = start; i < end; i++) {
  134. for (j = 0; j < height; j++) {
  135. for (t = 0; t < depth; t++) {
  136. fprintf(out, "%lf ", arr[i][j][t]);
  137. }
  138. fprintf(out, "\n");
  139. }
  140. }
  141. //fprintf(out, "\n---\n\n");
  142.  
  143. fclose(out);
  144. }
  145. }
  146.  
  147. MPI_Finalize();
  148. return 0;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement