Advertisement
Guest User

Untitled

a guest
Jul 6th, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <time.h>
  4.  
  5. #include "mpi.h"
  6. #include "omp.h"
  7.  
  8. #define SIZE 10
  9. #define NUMBER_TO_FIND 1
  10. #define RANGE 3
  11.  
  12. void create_array(int **array, int size)
  13. {
  14. *array = (int*) malloc(size * sizeof(int));
  15. for (int i = 0; i < size; ++i)
  16. (*array)[i] = -1;
  17. }
  18.  
  19. void fill_array(int *array, int size) {
  20. for (int i = 0; i < SIZE; ++i)
  21. array[i] = rand() % RANGE;
  22. }
  23.  
  24. void print_array(int* array, int size)
  25. {
  26. for (int i = 0; i < size; ++i)
  27. printf("%d ", array[i]);
  28. printf("\n");
  29. }
  30.  
  31. void print_result(int* array, int size)
  32. {
  33. for (int i = 0; i < size; ++i)
  34. if (array[i] != -1)
  35. printf("%d ", array[i]);
  36. printf("\n");
  37. }
  38.  
  39. void find(int* array, int* result, int num, int starting_point, int size)
  40. {
  41. int pos = -1;
  42. #pragma omp paralel for shared(array, result) reduction(+:pos)
  43. for (int i = 0; i < size; ++i)
  44. if (array[i] == num) {
  45. pos += 1;
  46. result[pos] = starting_point + i;
  47. }
  48. }
  49.  
  50. int main(int argc, char *argv[])
  51. {
  52. srand(time(NULL));
  53.  
  54. int rank, comm_size;
  55.  
  56. MPI_Init(&argc, &argv);
  57.  
  58. MPI_Comm_size(MPI_COMM_WORLD, &comm_size);
  59. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  60.  
  61. int *global_array, *global_result;
  62. int *local_array, *local_result;
  63. int local_size = SIZE / comm_size;
  64.  
  65. if (rank == comm_size-1)
  66. local_size += SIZE % comm_size;
  67.  
  68. global_array = NULL;
  69. global_result = NULL;
  70.  
  71. if (rank == 0)
  72. {
  73. printf("Array\n");
  74. create_array(&global_array, SIZE);
  75. fill_array(global_array, SIZE);
  76. print_array(global_array, SIZE);
  77.  
  78. create_array(&global_result, SIZE);
  79. }
  80.  
  81. create_array(&local_array, local_size);
  82. create_array(&local_result, local_size);
  83.  
  84. int counts[comm_size];
  85. int displacements[comm_size];
  86.  
  87. if (rank == 0)
  88. {
  89. for (int i = 0; i < comm_size; ++i)
  90. {
  91. if (i == comm_size-1)
  92. counts[i] = local_size + (SIZE % comm_size);
  93. else
  94. counts[i] = local_size;
  95.  
  96. if (i == 0)
  97. displacements[i] = 0;
  98. else
  99. displacements[i] = displacements[i-1] + counts[i-1];
  100. }
  101. }
  102.  
  103. MPI_Scatterv( global_array, counts, displacements, MPI_INT,
  104. &(local_array[0]), local_size, MPI_INT,
  105. 0, MPI_COMM_WORLD );
  106.  
  107. // for (int i = 0; i < comm_size; ++i)
  108. // {
  109. // if (rank == i){
  110. // printf("RANK: %d\n", rank);
  111. // print_array(local_array, local_size);
  112. // }
  113. // MPI_Barrier(MPI_COMM_WORLD);
  114. // }
  115. // MPI_Barrier(MPI_COMM_WORLD);
  116.  
  117. int starting_point;
  118. MPI_Scatter(displacements, 1, MPI_INT, &starting_point, 1, MPI_INT, 0, MPI_COMM_WORLD);
  119.  
  120. // printf("RANK: %d, ST: %d\n", rank, starting_point);
  121. // MPI_Barrier(MPI_COMM_WORLD);
  122.  
  123. find(local_array, local_result, NUMBER_TO_FIND, starting_point, local_size);
  124. MPI_Barrier(MPI_COMM_WORLD);
  125.  
  126. MPI_Gatherv( &(local_result[0]), local_size, MPI_INT,
  127. global_result, counts, displacements, MPI_INT,
  128. 0, MPI_COMM_WORLD );
  129.  
  130. if (rank == 0){
  131. printf("%d found at positions:\n", NUMBER_TO_FIND);
  132. print_result(global_result, SIZE);
  133. }
  134.  
  135. MPI_Finalize();
  136.  
  137. return 0;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement