|
3 | 3 | // This file implements the PARALLEL construct |
4 | 4 |
|
5 | 5 | // Declaration of array for storing pthread identifier from pthread_create function |
6 | | -pthread_t *miniomp_threads; |
| 6 | +miniomp_thread_t *miniomp_threads; |
7 | 7 |
|
8 | 8 | // Global variable for parallel descriptor |
9 | 9 | miniomp_parallel_t *miniomp_parallel; |
10 | 10 |
|
11 | 11 | // Declaration of per-thread specific key |
12 | 12 | pthread_key_t miniomp_specifickey; |
13 | 13 |
|
| 14 | +void miniomp_thread_init(miniomp_thread_t * thread, unsigned int id, void (*fn) (void *), miniomp_parallel_t *region){ |
| 15 | + thread->id = id; |
| 16 | + thread->region = region; |
| 17 | + pthread_setspecific(miniomp_specifickey, thread); |
| 18 | + pthread_create(&(thread->pthread), NULL, fn, thread); |
| 19 | +} |
| 20 | + |
| 21 | + |
14 | 22 | // This is the prototype for the Pthreads starting function |
15 | | -void *worker(void *args) { |
16 | | - miniomp_parallel_t *parallel_t = args; |
17 | | - int id = parallel_t->id; |
| 23 | +void worker(void *args) { |
| 24 | + miniomp_thread_t *thread = args; |
| 25 | + miniomp_parallel_t *parallel = thread->region; |
| 26 | + int id = thread->id; |
18 | 27 | printf("Thread %d initialized\n", id); |
19 | | - void (*fn) (void *) = parallel_t->fn; |
20 | | - void * data = parallel_t->fn_data; |
21 | | - pthread_setspecific(miniomp_specifickey, parallel_t); |
| 28 | + void (*fn) (void *) = parallel->fn; |
| 29 | + void * data = parallel->fn_data; |
| 30 | + //printf("Thread %d fn and data done\n", id); |
22 | 31 | fn(data); |
| 32 | + //printf("Executed fn in thread %d", id); |
23 | 33 | // insert all necessary code here for: |
24 | 34 | // 1) save thread-specific data |
25 | 35 | // 2) invoke the per-threads instance of function encapsulating the parallel region |
26 | 36 | // 3) exit the function |
27 | | - return(NULL); |
28 | 37 | } |
29 | 38 |
|
30 | 39 | void |
31 | 40 | GOMP_parallel (void (*fn) (void *), void *data, unsigned num_threads, unsigned int flags) { |
32 | 41 | if(!num_threads) num_threads = omp_get_num_threads(); |
33 | 42 | printf("Starting a parallel region using %d threads\n", num_threads); |
34 | | - miniomp_threads = malloc(num_threads*sizeof(pthread_t)); |
35 | | - miniomp_parallel = malloc(sizeof(miniomp_parallel_t)*num_threads); |
| 43 | + miniomp_threads = malloc(num_threads*sizeof(miniomp_thread_t)); |
| 44 | + miniomp_parallel = malloc(sizeof(miniomp_parallel_t)); |
| 45 | + miniomp_parallel->fn = fn; |
| 46 | + miniomp_parallel->fn_data = data; |
| 47 | + miniomp_parallel->id = 0; |
| 48 | + pthread_mutex_init(&(miniomp_parallel->mutex), NULL); |
36 | 49 | for (int i=0; i<num_threads; i++){ |
37 | | - //pthread_mutex_init(&miniomp_parallel[i].mutex, NULL); |
38 | | - miniomp_parallel[i].fn = fn; |
39 | | - miniomp_parallel[i].fn_data = data; |
40 | | - miniomp_parallel[i].id = i; |
41 | | - pthread_create(&miniomp_threads[i], NULL, worker, &miniomp_parallel[i]); |
| 50 | + miniomp_thread_init(&miniomp_threads[i], i, worker, miniomp_parallel); |
42 | 51 | } |
43 | 52 | for (int i=0; i < num_threads; i++){ |
44 | | - pthread_join(miniomp_threads[i], NULL); |
45 | | - //pthread_mutex_destroy(&miniomp_parallel[i].mutex); |
| 53 | + pthread_join(miniomp_threads[i].pthread, NULL); |
46 | 54 | } |
47 | 55 | free(miniomp_threads); |
48 | 56 | free(miniomp_parallel); |
|
0 commit comments