Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* foldl en C con hilos */
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <unistd.h>
- #include <math.h>
- #define MAXHILOS 8
- #undef DEBUG
- int n = 0;
- int shift = 1;
- int * from = NULL;
- int * to = NULL;
- int * tmp = NULL;
- pthread_barrier_t * barrera;
- pthread_t hilo[MAXHILOS];
- void proc1(void * xp);
- /* hace 'zipWith (+) xs (drop shift xs)' y guarda el resultado en ys */
- void zipWithPlus(void){
- int i = 0;
- printf("init\n");
- pthread_barrier_init(barrera, NULL, (MAXHILOS - shift));
- printf("post barrera\n");
- for(i = 0; i < (MAXHILOS - shift); ++i){
- printf("en ciclo\n");
- pthread_create(&hilo[i],NULL,(void *)&proc1, (void *) i);
- }
- printf("pre destruir\n");
- pthread_barrier_destroy(barrera);
- printf("post destruir\n");
- /* siguiente vuelta */
- /* swap */
- tmp = from;
- from = to;
- to = tmp;
- ++n;
- shift = pow (2,n);
- }
- void proc1(void * xp) {
- int x = (int) xp;
- *(to+x+shift) = *(from+x) + *(from+x+shift);
- pthread_exit(0);
- }
- int main() {
- int i;
- from = malloc(sizeof(int) * MAXHILOS);
- to = malloc(sizeof(int) * MAXHILOS);
- barrera = malloc(sizeof(pthread_barrier_t));
- for(i = 0; i < MAXHILOS; ++i){
- *(from+i) = *(to+i) = i;
- }
- zipWithPlus();
- zipWithPlus();
- zipWithPlus();
- for(i = 0; i < MAXHILOS; ++i){
- if(i < (MAXHILOS / 2)){
- printf(" %d", *(to+i));
- } else {
- printf(" %d", *(from+i));
- }
- }
- printf("\n");
- free(from);
- free(to);
- free(barrera);
- return 0;
- }
Add Comment
Please, Sign In to add comment