Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <mpi.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5. #define RESULT 1
  6. #define FINISH 2
  7. #define DEBUG
  8. #define DATA 0
  9.  
  10. #define TABLESIZE 10
  11. #define CHUNKS 3
  12.  
  13. void merge2(int arr[], int l, int m, int r)
  14. {
  15. int i, j, k;
  16. int n1 = m - l + 1;
  17. int n2 = r - m;
  18.  
  19. int L[n1], R[n2];
  20.  
  21. for(i = 0; i < n1; i++)
  22. L[i] = arr[l + i];
  23. for(j = 0; j <= n2; j++)
  24. R[j] = arr[m + 1+ j];
  25.  
  26. i = 0;
  27. j = 0;
  28. k = l;
  29. while (i < n1 && j < n2)
  30. {
  31. if (L[i] <= R[j])
  32. {
  33. arr[k] = L[i];
  34. i++;
  35. }
  36. else
  37. {
  38. arr[k] = R[j];
  39. j++;
  40. }
  41. k++;
  42. }
  43.  
  44. while (i < n1)
  45. {
  46. arr[k] = L[i];
  47. i++;
  48. k++;
  49. }
  50.  
  51. while (j < n2)
  52. {
  53. arr[k] = R[j];
  54. j++;
  55. k++;
  56. }
  57. }
  58.  
  59. void mergeSort2(int arr[], int l, int r)
  60. {
  61. if (l < r)
  62. {
  63. int m = l+(r-l)/2; //Same as (l+r)/2, but avoids overflow for large l and h
  64. mergeSort2(arr, l, m);
  65. mergeSort2(arr, m+1, r);
  66. merge2(arr, l, m, r);
  67. }
  68. }
  69.  
  70. int compare4qsort(const void *a, const void *b){
  71. return (*(int*)a-*(int*)b);
  72. }
  73.  
  74. int main(int argc, char** argv) {
  75.  
  76. int myrank,proccount;
  77. int i,j,k,l,z;
  78. int size, chunks;
  79. int tobesorted[TABLESIZE];
  80. MPI_Status status;
  81. int *temp2send;
  82. int *receivedBuffer;
  83. int amountToSend=0;
  84. int chunksSent=0;
  85. int amountSortedAlready=0;
  86.  
  87. MPI_Init(NULL,NULL);
  88. MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
  89. MPI_Comm_size(MPI_COMM_WORLD, &proccount);
  90.  
  91. if (proccount<2){
  92. printf("Run with at least 2 processes");
  93. MPI_Finalize();
  94. return -1;
  95. }
  96.  
  97. if (myrank == 0){ // MASTER
  98.  
  99.  
  100. printf("\nTablica z %d elementami, liczba paczek, to %d\n\n", TABLESIZE, CHUNKS);
  101. printf("Przed posortowaniem: ");
  102. fflush(stdout);
  103.  
  104.  
  105. srand(time(NULL));
  106. for (i=0;i<TABLESIZE;i++){
  107. tobesorted[i]=rand()%40;
  108. printf("%d, ", tobesorted[i]);
  109. }
  110. // printf("\n");
  111.  
  112. int amountPerChunk = TABLESIZE / CHUNKS;
  113. int beginIndex=0;
  114. int endIndex=0;
  115. int sentcount=0;
  116. // FIRST SENDING
  117. for (j=1; j<proccount;j++) {
  118. endIndex = beginIndex+amountPerChunk-1;
  119. /* if (j==proccount-1) {
  120. endIndex = endIndex + (TABLESIZE%CHUNKS);
  121. }*/
  122.  
  123.  
  124. printf("proc %d: indeks elementu poczatkowego=%d indeks elementu koncowego=%d\n", j,beginIndex,endIndex);
  125. fflush(stdout);
  126.  
  127. amountToSend = endIndex-beginIndex+1;
  128. // send it to process j
  129. MPI_Send((void*)(tobesorted+sentcount+1),amountToSend,MPI_INT,j,DATA,MPI_COMM_WORLD);
  130. beginIndex = beginIndex + amountToSend;
  131. sentcount=sentcount+amountToSend;
  132. chunksSent++;
  133.  
  134. printf("\n Paczka [%d] wyslana do procesu (%d)! Z calkowitej liczby [%d]\n", chunksSent, j, sentcount);
  135. fflush(stdout);
  136.  
  137.  
  138. }
  139. //
  140. int *sortedBuffer = &tobesorted;
  141. // process results from slaves
  142. while (chunksSent<CHUNKS-1) {
  143. int left=0;
  144. int right=0;
  145. int resultReceivedAmount=0;
  146. MPI_Probe(MPI_ANY_SOURCE,MPI_ANY_TAG,MPI_COMM_WORLD,&status);
  147. MPI_Get_count(&status,MPI_INT,&resultReceivedAmount);
  148.  
  149. //int *receivedResult = malloc(resultReceivedAmount * sizeof(int));
  150. MPI_Recv((void*)(sortedBuffer+amountSortedAlready),resultReceivedAmount,MPI_INT,MPI_ANY_SOURCE,RESULT,MPI_COMM_WORLD,&status);
  151.  
  152. //mergeSort2(sortedBuffer,0,amountSortedAlready);
  153.  
  154. MPI_Send((void*)(tobesorted+sentcount+1),amountPerChunk,MPI_INT,status.MPI_SOURCE,DATA,MPI_COMM_WORLD);
  155. amountSortedAlready=amountSortedAlready+resultReceivedAmount;
  156. chunksSent++;
  157. sentcount=sentcount+amountPerChunk;
  158. }
  159. // LAST CHUNK
  160. amountToSend = TABLESIZE-sentcount;
  161. MPI_Send((void*)(tobesorted+sentcount+1),amountToSend,MPI_INT,status.MPI_SOURCE,DATA,MPI_COMM_WORLD);
  162. int lastResultReceivedAmount=0;
  163. MPI_Probe(MPI_ANY_SOURCE,MPI_ANY_TAG,MPI_COMM_WORLD,&status);
  164. MPI_Get_count(&status,MPI_INT,&lastResultReceivedAmount);
  165.  
  166. MPI_Recv((void*)(sortedBuffer+amountSortedAlready),lastResultReceivedAmount,MPI_INT,MPI_ANY_SOURCE,RESULT,MPI_COMM_WORLD,&status);
  167.  
  168. mergeSort2(sortedBuffer,0,TABLESIZE);
  169. //shutdown the slaves
  170. for (j=1;j<proccount;j++){
  171. MPI_Send(NULL,0,MPI_INT,j,FINISH,MPI_COMM_WORLD);
  172. }
  173.  
  174. printf("Tablica koncowa: \n");
  175. for(i=0;i<TABLESIZE;i++){
  176. printf("%d > ", sortedBuffer[i]);
  177. //printf("\n final sorted table size = %d", )
  178. }
  179. printf("\n");
  180. fflush(stdout);
  181. //
  182. } else { // SLAVE NOW
  183.  
  184. do {
  185. int receivedAmount=0;
  186. MPI_Probe(0,MPI_ANY_TAG,MPI_COMM_WORLD,&status);
  187. MPI_Get_count(&status,MPI_INT,&receivedAmount);
  188.  
  189. receivedBuffer = malloc(receivedAmount * sizeof(int));
  190. if (status.MPI_TAG==DATA){
  191. MPI_Recv(receivedBuffer,receivedAmount, MPI_INT,MPI_ANY_SOURCE,DATA,MPI_COMM_WORLD,&status);
  192.  
  193. qsort(receivedBuffer,receivedAmount, sizeof(int), compare4qsort);
  194. //mergeSort2(receivedBuffer,0,receivedAmount);
  195.  
  196. MPI_Send(receivedBuffer,receivedAmount,MPI_INT,0,RESULT,MPI_COMM_WORLD);
  197. }
  198. } while (status.MPI_TAG!=FINISH);
  199. }
  200. MPI_Finalize();
  201. return 0;
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement