Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. /*
  2. ============================================================================
  3. Name : Laboratorul4.c
  4. Author : Author
  5. Version :
  6. Copyright : Your copyright notice
  7. Description : Hello MPI World in C
  8. ============================================================================
  9. */
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include "mpi.h"
  13.  
  14. int main(int argc, char* argv[]){
  15. int my_rank; /* rank of process */
  16. int p; /* number of processes */
  17. int source; /* rank of sender */
  18. int dest; /* rank of receiver */
  19. int tag=0; /* tag for messages */
  20. char message[100]; /* storage for message */
  21.  
  22. int d = 3;
  23. int k;
  24. int source_virtual;
  25. int masca = (1<<d)-1;
  26.  
  27. source = 8;
  28. //dest = 7;
  29.  
  30. MPI_Status status ; /* return status for receive */
  31.  
  32. /* start up MPI */
  33.  
  34. MPI_Init(&argc, &argv);
  35.  
  36. /* find out process rank */
  37. MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
  38.  
  39. /* find out number of processes */
  40. MPI_Comm_size(MPI_COMM_WORLD, &p);
  41.  
  42. int id_virtual = source ^ my_rank;
  43.  
  44.  
  45.  
  46. for(k = d - 1; k >= 0; k--)
  47. {
  48. masca = masca ^ 1 << k;
  49. if((id_virtual & masca) == 0)
  50. {
  51. if((id_virtual & 1<<k) == 0)
  52. {
  53. dest = id_virtual ^ 1<<k ;
  54. dest = dest ^ source;
  55. printf("Mesaj\n");
  56. sprintf(message, "Hello");
  57. MPI_Send(message, strlen(message)+1, MPI_CHAR,
  58. dest, tag, MPI_COMM_WORLD);
  59. }
  60. else
  61. {
  62. source_virtual = id_virtual ^ 1<<k;
  63. source_virtual = source_virtual ^ source;
  64. MPI_Recv(message, 100, MPI_CHAR, source_virtual , tag,
  65. MPI_COMM_WORLD, &status);
  66. printf("%d sent %s to %d\n",source_virtual, message, my_rank);
  67. }
  68. }
  69. MPI_Barrier(MPI_COMM_WORLD);
  70. }
  71. /* shut down MPI */
  72. MPI_Finalize();
  73.  
  74.  
  75. return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement