Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. #include <math.h>
  2. #include <mpi.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. #define n 16
  7.  
  8. int main(int argc, char *argv[])
  9. {
  10. MPI_Init(&argc, &argv);
  11. MPI_Comm comm = MPI_COMM_WORLD;
  12. int p,r;
  13. MPI_Comm_size(comm, &p);
  14. MPI_Comm_rank(comm, &r);
  15. int *arr;
  16. arr = NULL;
  17. if (r == 0){
  18. arr = (int *) malloc(n * n * sizeof(int));
  19. for (int i = 0; i < n * n; i++) arr[i] = i;
  20. for (int i = 0; i < n; i++){
  21. printf("n");
  22. for (int j = 0; j < n; j++)
  23. printf("%4d", arr[i * n + j]);
  24. }
  25. }
  26. printf("n");
  27. int ps = sqrt(p);
  28. int ns = n / ps;
  29.  
  30. if (r == 0) {
  31. printf("ps: %d ns: %dn", ps, ns);
  32. }
  33. /* create datatype */
  34. MPI_Datatype block;
  35. MPI_Type_vector(ns, ns, n, MPI_INT, &block);
  36. int blocks[ps];
  37. MPI_Aint displs[ps];
  38. for (int i = 0; i < ps; i++) {
  39. blocks[i] = 1;
  40. displs[i] = i * sizeof(int);
  41. }
  42. MPI_Datatype types[ps];
  43. //for (int i = 0; i < ps - 1; i++) types[i] = block;
  44. //types[ps - 1] = MPI_UB;
  45. types[0] = block;
  46. for (int i = 1; i < ps; i++) types[i] = MPI_UB;
  47. //types[0] = block;
  48. //types[1] = MPI_UB;
  49. if (r == 0) {
  50. printf("displs:n");
  51. for(int i = 0; i < ps; i++) printf("%3ld", displs[i]);
  52. printf("n");
  53. }
  54.  
  55. MPI_Datatype row;
  56. MPI_Type_struct(ps, blocks, displs, types, &row);
  57. MPI_Type_commit(&row);
  58.  
  59. /* prepare scatter */
  60. int sdispl[p]; int sendcounts[p];
  61. for (int i = 0; i < p; i++) {
  62. sdispl[i] = (i % ps) + (i / ps) * (ns * ps);
  63. sendcounts[i] = 1;
  64. }
  65. if (r == 0) {
  66. printf("sdispl: n");
  67. for (int i = 0; i < 4; i++) printf("%3d", sdispl[i]);
  68. printf("n");
  69. }
  70.  
  71. int rcv[ns * ns];
  72. MPI_Scatterv(arr, sendcounts, sdispl, row, rcv, ns * ns, MPI_INT, 0, comm);
  73.  
  74. int result = 1;
  75. if (r == result) {
  76. printf("result for %d:n", result);
  77. for (int i = 0; i < ns * ns; i++) {
  78. printf("%4d", rcv[i]);
  79. if ((i+1) % ns == 0) printf("n");
  80. }
  81. }
  82.  
  83. if (arr != NULL) free(arr);
  84. MPI_Finalize();
  85. return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement