Advertisement
DiegoDF1445

MS thread TEMPO

Oct 22nd, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <pthread.h>
  5. #include <sys/types.h>
  6. #include <time.h>
  7.  
  8. #define MAXN 100005
  9. #define THMAX 5
  10. #define min(x, y) (x <= y ? x : y)
  11.  
  12. //array que vai ser ordenado
  13. int arr[MAXN];
  14.  
  15. //mutex para tratar de regios criticas
  16. pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
  17. int flag = 0;
  18.  
  19. //pilha de threads disponiveis atualmente
  20. int stack[THMAX], top;
  21.  
  22. typedef struct{
  23.     int a, b, x, y, idx;
  24. }merge_range;
  25.  
  26. void* merge(void* data){
  27.     //Pego os intervalos em que eu vou juntar (merge)
  28.     merge_range *args = data;
  29.     int l = args-> a, r = args->b, i = args->x, j = args->y;
  30.     int val = args -> idx;
  31.  
  32.     //Variaveis auxiliares
  33.     int ord[MAXN], k, li = l, ls = j;
  34.  
  35.     //merge
  36.     for(k = l; l <= r && i <= j; k++){
  37.         if(arr[l] <= arr[i]) ord[k] = arr[l++];
  38.         else ord[k] = arr[i++];
  39.     }
  40.     while(l <= r) ord[k++] = arr[l++];
  41.     while(i <= j) ord[k++] = arr[i++];
  42.  
  43.     //atualizo meu array
  44.     for(; li <= ls; li++) arr[li] = ord[li];
  45.  
  46.     //região critica (variavel global), atualizo minha pilha de threads disponiveis
  47.     pthread_mutex_lock(&mtx);
  48.     stack[++top] = val;
  49.     pthread_mutex_unlock(&mtx);
  50.     return NULL;
  51. }
  52.  
  53. void merge_sort(int n){
  54.     //variaveis auxiliares
  55.     int i, j, qtd = 0, aux;
  56.     //threads
  57.     pthread_t thr[THMAX];
  58.     //array que vai guardar a task de cada thread em um instante da execução
  59.     merge_range tmp[THMAX];
  60.  
  61.     //Inicializo minha pilha de threads
  62.     for(i = 0; i < THMAX; i++) stack[i] = i;
  63.     top = THMAX - 1;
  64.  
  65.     for(i = 1; i <= n; i <<= 1){
  66.         for(j = 0; j + i <= n; j += (i << 1)  ){
  67.             //enquanto eu nao tiver threads disponiveis não faço nada
  68.             while(top < 0);
  69.             //Pego o indice da proxima thread disponivel
  70.             aux = stack[top];
  71.             //Inicializo os ranges que a thread ficara responsavel em dar o merge
  72.             tmp[ aux ].a = j; tmp[ aux ].b = j + i - 1;
  73.             tmp[ aux ].x = min(tmp[ aux ].b + 1, n - 1); tmp[aux].y = min(tmp[aux].x + i - 1, n - 1);
  74.             //Guardo o seu indice para ser recolado na pilha
  75.             tmp[aux].idx = stack[aux];
  76.  
  77.             //O trabalho começa
  78.             pthread_create( &thr[ aux ], NULL, &merge, &tmp[aux]);
  79.             pthread_join(thr[ stack[top--] ], NULL);
  80.  
  81.          }
  82.          //Enquanto todas as threads nao tiverem terminado seu trabalho, eu espero
  83.          while(top != THMAX - 1);
  84.     }
  85. }
  86.  
  87. int main(){
  88.     //Entrada e saida por arquivo
  89.     freopen("input.txt", "r", stdin);
  90.     freopen("output_WT.txt", "w", stdout);
  91.  
  92.     //Tamanho do meu array
  93.     int n;
  94.     scanf("%d", &n);
  95.  
  96.     //Entrada dos elementos do array
  97.     int i;
  98.     for(i = 0; i < n; i++){
  99.         scanf("%d", arr + i);
  100.     }
  101.  
  102.     clock_t start = clock();
  103.  
  104.     //MERGE SORT
  105.     merge_sort(n);
  106.  
  107.     //Printando array ordenado
  108.     /*for(i = 0; i < n; i++){
  109.         printf("%d ", arr[i]);
  110.     }
  111.     putchar('\n');*/
  112.  
  113.     clock_t end = clock();
  114.  
  115.     double time = (end - start) / (double)CLOCKS_PER_SEC;
  116.  
  117.     printf("%.9lf\n", time);
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement