Skip to content

Commit

Permalink
Camilo
Browse files Browse the repository at this point in the history
  • Loading branch information
pap0012 committed Apr 24, 2019
1 parent 9904591 commit 0de1643
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/intrinsic.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ int omp_get_num_threads (void) {

int omp_get_thread_num (void) {
//printf("TBI: omp_get_thread_num still doesn't know who I am ... let's say I am 0\n");
printf("Called get_thread: \n");
//printf("Called get_thread: \n");
miniomp_thread_t *thread = pthread_getspecific(miniomp_specifickey);
printf("Casted getspecific \n");
//printf("Casted getspecific \n");
unsigned int thread_id = thread->id;
printf("%d\n", thread_id);
//printf("THREAD ID IS %d\n", thread_id);
return((int)thread_id);
}

Expand Down
14 changes: 9 additions & 5 deletions src/parallel.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ void miniomp_thread_init(miniomp_thread_t * thread, unsigned int id, void (*fn)

// This is the prototype for the Pthreads starting function
void worker(void *args) {
pthread_key_create(&miniomp_specifickey, NULL);
//pthread_key_create(&miniomp_specifickey, NULL);
miniomp_thread_t *thread = args;
pthread_setspecific(miniomp_specifickey, thread);
miniomp_parallel_t *parallel = thread->region;
int id = thread->id;
printf("Thread %d initialized\n", id);
void (*fn) (void *) = parallel->fn;
void * data = parallel->fn_data;
printf("Thread %d fn and data done\n", id);
//printf("Thread %d fn and data done\n", id);
fn(data);
printf("Executed fn in thread %d\n", id);
//printf("Executed fn in thread %d\n", id);
}

void
Expand All @@ -41,15 +41,19 @@ GOMP_parallel (void (*fn) (void *), void *data, unsigned num_threads, unsigned i
miniomp_parallel = malloc(sizeof(miniomp_parallel_t));
miniomp_parallel->fn = fn;
miniomp_parallel->fn_data = data;
miniomp_parallel->id = 0;
miniomp_parallel->id = 0;
pthread_barrier_init(&(miniomp_parallel->barrier), NULL, num_threads);
pthread_mutex_init(&(miniomp_parallel->mutex), NULL);
pthread_key_create(&miniomp_specifickey, NULL);
for (int i=0; i<num_threads; i++){
miniomp_thread_init(&miniomp_threads[i], i, worker, miniomp_parallel);
}
for (int i=0; i < num_threads; i++){
printf("Thread %i arrived to join\n", i);
//printf("Thread %i arrived to join\n", i);
pthread_join(miniomp_threads[i].pthread, NULL);
}
pthread_mutex_destroy(&(miniomp_parallel->mutex));
pthread_barrier_destroy(&(miniomp_parallel->barrier));
free(miniomp_threads);
free(miniomp_parallel);
}
Expand Down
1 change: 1 addition & 0 deletions src/parallel.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ typedef struct {
void *fn_data;
unsigned int id;
pthread_mutex_t mutex;
pthread_barrier_t barrier;
// complete the definition of parallel descriptor
} miniomp_parallel_t;

Expand Down
19 changes: 14 additions & 5 deletions src/synchronization.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,36 @@
pthread_mutex_t miniomp_default_lock;

void GOMP_critical_start (void) {
printf("TBI: Entering an unnamed critical, don't know if anyone else is alrady in. I proceed\n");
miniomp_thread_t *thread = pthread_getspecific(miniomp_specifickey);
pthread_mutex_lock(&(thread->region->mutex));
//printf("TBI: Entering an unnamed critical, don't know if anyone else is alrady in. I proceed\n");
}

void GOMP_critical_end (void) {
printf("TBI: Exiting an unnamed critical section. I can not inform anyone else, bye!\n");
miniomp_thread_t *thread = pthread_getspecific(miniomp_specifickey);
pthread_mutex_unlock(&(thread->region->mutex));
//printf("TBI: Exiting an unnamed critical section. I can not inform anyone else, bye!\n");
}

void GOMP_critical_name_start (void **pptr) {
pthread_mutex_t * plock = *pptr;
printf("TBI: Entering a named critical %p (%p), don't know if anyone else is alrady in. I proceed\n", pptr, plock);
pthread_mutex_lock(plock);
printf("TBI: Entering a named critical %p (%p)\n", pptr, plock);
// if plock is NULL it means that the lock associated to the name has not yet been allocated and initialized
}

void GOMP_critical_name_end (void **pptr) {
pthread_mutex_t * plock = *pptr;
printf("TBI: Exiting a named critical %p (%p), I can not inform anyone else, bye!\n", pptr, plock);
pthread_mutex_unlock(plock);
printf("TBI: Exiting a named critical %p (%p)\n", pptr, plock);
// if plock is still NULL something went wrong
}

pthread_barrier_t miniomp_barrier;

void GOMP_barrier() {
printf("TBI: Entering in barrier, but do not know how to wait for the rest. I proceed\n");
miniomp_thread_t *thread = pthread_getspecific(miniomp_specifickey);
printf("Thread %d waiting for signal", thread->id);
pthread_barrier_wait(&(thread->region->barrier));

}

0 comments on commit 0de1643

Please sign in to comment.