Guest User

Untitled

a guest
Mar 14th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. /* foldl en C con hilos */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <pthread.h>
  6.  
  7. #include <unistd.h>
  8.  
  9. #include <math.h>
  10.  
  11. #define MAXHILOS 8
  12. #undef DEBUG
  13.  
  14. int n = 0;
  15. int shift = 1;
  16.  
  17. int * from = NULL;
  18. int * to = NULL;
  19. int * tmp = NULL;
  20.  
  21. pthread_barrier_t * barrera;
  22. pthread_t hilo[MAXHILOS];
  23.  
  24. void proc1(void * xp);
  25.  
  26. /* hace 'zipWith (+) xs (drop shift xs)' y guarda el resultado en ys */
  27.  
  28. void zipWithPlus(void){
  29. int i = 0;
  30.  
  31. printf("init\n");
  32.  
  33. pthread_barrier_init(barrera, NULL, (MAXHILOS - shift));
  34.  
  35. printf("post barrera\n");
  36.  
  37. for(i = 0; i < (MAXHILOS - shift); ++i){
  38. printf("en ciclo\n");
  39. pthread_create(&hilo[i],NULL,(void *)&proc1, (void *) i);
  40. }
  41.  
  42. printf("pre destruir\n");
  43.  
  44. pthread_barrier_destroy(barrera);
  45.  
  46. printf("post destruir\n");
  47.  
  48. /* siguiente vuelta */
  49.  
  50. /* swap */
  51. tmp = from;
  52. from = to;
  53. to = tmp;
  54.  
  55. ++n;
  56. shift = pow (2,n);
  57. }
  58.  
  59. void proc1(void * xp) {
  60. int x = (int) xp;
  61.  
  62. *(to+x+shift) = *(from+x) + *(from+x+shift);
  63.  
  64. pthread_exit(0);
  65. }
  66.  
  67.  
  68. int main() {
  69.  
  70. int i;
  71.  
  72. from = malloc(sizeof(int) * MAXHILOS);
  73. to = malloc(sizeof(int) * MAXHILOS);
  74.  
  75. barrera = malloc(sizeof(pthread_barrier_t));
  76.  
  77. for(i = 0; i < MAXHILOS; ++i){
  78. *(from+i) = *(to+i) = i;
  79. }
  80.  
  81. zipWithPlus();
  82. zipWithPlus();
  83. zipWithPlus();
  84.  
  85. for(i = 0; i < MAXHILOS; ++i){
  86. if(i < (MAXHILOS / 2)){
  87. printf(" %d", *(to+i));
  88. } else {
  89. printf(" %d", *(from+i));
  90. }
  91. }
  92.  
  93. printf("\n");
  94.  
  95. free(from);
  96. free(to);
  97.  
  98. free(barrera);
  99.  
  100. return 0;
  101. }
Add Comment
Please, Sign In to add comment