Advertisement
Guest User

mpihell

a guest
Apr 24th, 2014
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. #include<mpi.h>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<iostream>
  5. #include<conio.h>
  6. using namespace std;
  7.  
  8. int main(int argc, char** argv)
  9. {
  10. int rank,size,len,handle;
  11. char str[50],str1[50],str2[50];
  12. MPI_Status status;
  13. MPI_Init(&argc,&argv);
  14. MPI_Comm_rank(MPI_COMM_WORLD,&rank);
  15. MPI_Comm_size(MPI_COMM_WORLD,&size);
  16.  
  17. if(rank==0)
  18. {
  19. cout<<"Enter a string that is a multiple of 3\n";
  20. fflush(stdout);
  21. gets(str);
  22. fflush(stdin);
  23. len = strlen(str);
  24. handle = len/size;
  25. for(int i=1;i<size;i++)
  26. MPI_Send(&handle,1,MPI_INT,i,i,MPI_COMM_WORLD);
  27. }
  28. if(rank!=0)
  29. {
  30. MPI_Recv(&handle,1,MPI_INT,0,rank,MPI_COMM_WORLD,&status);
  31. }
  32. MPI_Scatter(&str[0],handle,MPI_CHAR,&str1[0],handle,MPI_CHAR,0,MPI_COMM_WORLD);
  33. for(int i=0;i<handle;i++)
  34. {
  35. str1[i] = str1[i] + 1;
  36. }
  37. MPI_Gather(&str1[0],handle,MPI_CHAR,&str2[0],handle,MPI_CHAR,0,MPI_COMM_WORLD);
  38. if(rank==0)
  39. {
  40. cout<<"Output is:\n";
  41. str2[len]='\0';
  42. puts(str2);
  43. fflush(stdout);
  44. }
  45. MPI_Finalize();
  46. getch();
  47. return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement