Created
October 13, 2009 16:57
-
-
Save jsoffer/209369 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment