Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. #include<mpi.h>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<math.h>
  5. #include<string.h>
  6. #define N 10
  7.  
  8. const int data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  9.  
  10. int main(int argc, char * argv[]) {
  11. int rank;
  12. int nProcesses;
  13.  
  14. int *v = (int*)malloc(sizeof(int)*N);
  15. int *aux = (int*)calloc(N, sizeof(int));
  16.  
  17. v = (int*)memcpy(v, data, sizeof(data));
  18.  
  19. MPI_Init(&argc, &argv);
  20.  
  21. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  22. MPI_Comm_size(MPI_COMM_WORLD, &nProcesses);
  23. printf("Hello from %i/%i\n", rank, nProcesses);
  24.  
  25. int i;
  26. int *local = (int*)malloc(sizeof(int) * N / nProcesses);
  27.  
  28. MPI_Scatter(aux, N / nProcesses, MPI_INT, local, N / nProcesses, MPI_INT, 0, MPI_COMM_WORLD);
  29.  
  30. int local_startIndex = rank * N / nProcesses;
  31. int local_stopIndex = (rank + 1) * N / nProcesses;
  32.  
  33. printf("rank = %i startIndex = %i stopIndex = %i\n", rank, local_startIndex, local_stopIndex);
  34. int j, k;
  35. for( j = local_startIndex; j < local_stopIndex; j++ ) {
  36. for( k = 0; k < N; k++ ) {
  37. if (v[j] > v[k]) {
  38. local[j]++;
  39. // printf("local[%d] = %d\n", j, local[j]);
  40. }
  41. }
  42. }
  43.  
  44. MPI_Gather(local, N / nProcesses, MPI_INT, aux, N / nProcesses, MPI_INT, 0, MPI_COMM_WORLD);
  45.  
  46. if(rank == 0) {
  47. for(i = 0; i < N; i++) {
  48. printf("%d ", v[local[i]]);
  49. }
  50. }
  51. printf("\n");
  52.  
  53. free(aux);
  54. free(v);
  55.  
  56. MPI_Finalize();
  57. return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement