Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include "stdafx.h"
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <mpi.h>
  6. #include <math.h>
  7.  
  8. #define n 5
  9. #define k 10
  10. #define m 7
  11.  
  12. struct Data {
  13. int masn[n];
  14. double masd[n];
  15. double masdd[m];
  16. };
  17.  
  18. int main(int arc, char **argv) {
  19.  
  20. int size, rank, msgtag = 12;
  21. MPI_Status status;
  22. int position;
  23.  
  24. //шапочка
  25.  
  26. if (MPI_Init(&arc, &argv) != MPI_SUCCESS) return 1;
  27.  
  28. if (MPI_Comm_size(MPI_COMM_WORLD, &size) != MPI_SUCCESS) {
  29.  
  30. MPI_Finalize();
  31.  
  32. return 2;
  33.  
  34. }
  35.  
  36. if (MPI_Comm_rank(MPI_COMM_WORLD, &rank) != MPI_SUCCESS) {
  37.  
  38. MPI_Finalize();
  39.  
  40. return 3;
  41.  
  42. }
  43. Data data[k];
  44.  
  45. int len = sizeof(data);
  46.  
  47. if (!rank) {
  48. void *buf = malloc(len);
  49.  
  50. for (int i = 0; i < k; i++) {
  51. for (int j = 0; j < n; j++) {
  52. data[i].masn[j] = j*j;
  53. data[i].masd[j] = (double)(j*j + 0.5);
  54. }
  55. for (int t = 0; t < m; t++) {
  56. data[i].masdd[t] = (double)(t*t + 0.1);
  57. }
  58. }
  59.  
  60. position = 0;
  61.  
  62. for (int i = 0; i < k; i++) {
  63. MPI_Pack(data[i].masn, n, MPI_INT, buf, len, &position, MPI_COMM_WORLD);
  64. MPI_Pack(data[i].masd, n, MPI_DOUBLE, buf, len, &position, MPI_COMM_WORLD);
  65. MPI_Pack(data[i].masdd, m, MPI_DOUBLE, buf, len, &position, MPI_COMM_WORLD);
  66. }
  67.  
  68. MPI_Send(&position, 1, MPI_INT, 1, msgtag, MPI_COMM_WORLD);
  69. MPI_Send(buf, position, MPI_PACKED, 1, msgtag, MPI_COMM_WORLD);
  70. free(buf);
  71. }
  72.  
  73. if (rank == 1) {
  74. int pos = 0;
  75. void *mas = malloc(len);
  76. MPI_Recv(&position, 1, MPI_INT, 0, msgtag, MPI_COMM_WORLD, &status);
  77. MPI_Recv(mas, position, MPI_PACKED, 0, msgtag, MPI_COMM_WORLD, &status);
  78.  
  79. for (int i = 0; i < k; i++) {
  80. MPI_Unpack(mas, position, &pos, data[i].masn, n, MPI_INT, MPI_COMM_WORLD);
  81. MPI_Unpack(mas, position, &pos, data[i].masd, n, MPI_DOUBLE, MPI_COMM_WORLD);
  82. MPI_Unpack(mas, position, &pos, data[i].masdd, m, MPI_DOUBLE, MPI_COMM_WORLD);
  83. }
  84. free(mas);
  85.  
  86. for (int i = 0; i < k; i++) {
  87. for (int j = 0; j < n; j++) {
  88. fprintf(stderr, "%d ", data[i].masn[j]);
  89. }
  90. fprintf(stderr, "\n");
  91.  
  92. for (int t = 0; t < n; t++) {
  93. fprintf(stderr, "%f ", data[i].masd[t]);
  94. }
  95. fprintf(stderr, "\n");
  96.  
  97. for (int l = 0; l < n; l++) {
  98. fprintf(stderr, "%f ", data[i].masdd[l]);
  99. }
  100. fprintf(stderr, "\n");
  101. fprintf(stderr, "\n");
  102. fprintf(stderr, "\n");
  103. }
  104. fprintf(stderr, "%d\n", data[1].masn[1]);
  105. }
  106.  
  107.  
  108.  
  109. //тапочки
  110. MPI_Finalize();
  111. return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement