Advertisement
Guest User

Untitled

a guest
Dec 19th, 2014
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <mpi.h>
  4. #include <math.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. double **allocMatrix(int dim) {
  8. int i;
  9. double **matrix;
  10. matrix = (double **)malloc(dim*sizeof(double *));
  11. for(i=0; i < dim; i++) {
  12. matrix[i] = (double *)malloc(dim*sizeof(double));
  13. }
  14. return matrix;
  15. }
  16. void printMatrix(double **values, int size) {
  17. int i, j;
  18.  
  19. for (i = 0; i < size; i++) {
  20. for (j = 0; j < size; j++) {
  21. printf("%10lf ", values[i][j]);
  22. }
  23. printf("n");
  24. }
  25. }
  26. int main(int argc, char* argv[]) {
  27. MPI_Init(&argc, &argv);
  28. int size, rank, i, j;
  29. int dimensions = 7;
  30. MPI_Comm_size(MPI_COMM_WORLD, &size);//number of processes
  31. MPI_Comm_rank(MPI_COMM_WORLD, &rank);//rank for each process
  32.  
  33. double **send = allocMatrix(dimensions);
  34. double **recv = allocMatrix(dimensions);
  35. int count = 0;
  36. for (i=0; i<dimensions; i++) {
  37. for (j=0; j<dimensions; j++) {
  38. if (rank == 0) {
  39. recv[i][j] = 0;
  40. } else {
  41. send[i][j] = ++count;
  42. }
  43. }
  44. }
  45.  
  46. MPI_Datatype arrType;
  47. MPI_Type_vector(1, dimensions, 0, MPI_DOUBLE, &arrType);
  48. MPI_Type_commit(&arrType);
  49. int recvCounts[size];
  50. int displs[size];
  51. recvCounts[0] = 0;
  52. displs[0] = 0;
  53. recvCounts[1] = 1;
  54. displs[1] = 0;
  55. MPI_Gatherv(&(send[0][0]), 1, arrType,
  56. &(recv[0][0]), recvCounts, displs, arrType,
  57. 0, MPI_COMM_WORLD);
  58.  
  59. if (rank == 0) {
  60. printMatrix(recv, dimensions);
  61. }
  62.  
  63.  
  64. MPI_Finalize();
  65. return 0;
  66. }
  67.  
  68. make gatherv
  69. mpicc -Wall -o gatherv gatherv.c && ./gather
  70. 1.000000 2.000000 3.000000 4.000000 5.000000 6.000000 7.000000
  71. 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
  72. 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
  73. 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
  74. 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
  75. 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
  76. 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
  77.  
  78. 1.000000 2.000000 3.000000 4.000000 5.000000 6.000000 7.000000
  79. 8.000000 9.000000 10.00000 11.00000 12.00000 13.00000 14.00000
  80. 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
  81. 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
  82. 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
  83. 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
  84. 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement