From ba71ee6707dda7485c9dcf1a03cee54c1d382bd8 Mon Sep 17 00:00:00 2001 From: josh Date: Sat, 27 Sep 2008 15:37:25 +0000 Subject: [PATCH] added return value information to threads and synchronized maximum values in main thread after joining worker threads git-svn-id: svn://anubis/gvsu@178 45c1a28c-8058-47b2-ae61-ca45b979098e --- cs677/pa2/threaded.cc | 53 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 11 deletions(-) diff --git a/cs677/pa2/threaded.cc b/cs677/pa2/threaded.cc index f530c88..028e48d 100644 --- a/cs677/pa2/threaded.cc +++ b/cs677/pa2/threaded.cc @@ -25,6 +25,13 @@ int * matrix; vector * s; vector * t; +struct retval_t +{ + int max_val; + int max_i; + int max_j; +} retval; + /* Print basic usage information */ void usage(char * prog) { @@ -106,13 +113,34 @@ int main(int argc, char * argv[]) } } + int max_val = 0, max_i = 0, max_j = 0; for (int i = 0; i < num_threads; i++) { - pthread_join(threads[i], NULL); + struct retval_t * retval; + pthread_join(threads[i], (void **) &retval); + if (retval->max_val == max_val) + { + if ( (retval->max_i + retval->max_j) > (max_i + max_j) ) + { + max_i = retval->max_i; + max_j = retval->max_j; + } + } + else if (retval->max_val > max_val) + { + max_val = retval->max_val; + max_i = retval->max_i; + max_j = retval->max_j; + } + delete retval; } - pthread_barrier_destroy(&barrier); + /* Print the maximum value and position */ + cout << "Maximum value is " << max_val << " at position (" + << max_i << ", " << max_j << ")" << endl; + /* Clean up after ourselves */ + pthread_barrier_destroy(&barrier); delete[] matrix; delete[] threads; return 0; @@ -144,7 +172,7 @@ void * calcSimMatrixThread(void * arg) int t_size = t->size(); int last_step = s_size + t_size; int (*F)[s_size+1][t_size+1] = (int (*) [s_size+1][t_size+1]) matrix; - int max_x = 0, max_y = 0, max_val = 0; + int max_i = 0, max_j = 0, max_val = 0; int first_task_id, num_tasks; taskAllocate(t_size+1, num_threads, id, &first_task_id, &num_tasks); for (int i = 0, idx = first_task_id; i < num_tasks; i++, idx++) @@ -196,17 +224,17 @@ void * calcSimMatrixThread(void * arg) if ((*F)[i][j] > max_val) { max_val = (*F)[i][j]; - max_x = i; - max_y = j; + max_i = i; + max_j = j; } else if ((*F)[i][j] == max_val) { /* If we found a value the same as our current maximum * value, see if it has a greater i+j value */ - if ( (i + j) > (max_x + max_y) ) + if ( (i + j) > (max_i + max_j) ) { - max_x = i; - max_y = j; + max_i = i; + max_j = j; } } } @@ -224,9 +252,12 @@ void * calcSimMatrixThread(void * arg) printf("\n"); } #endif - cout << "Maximum value is " << max_val << " at position (" - << max_x << ", " << max_y << ")" << endl; + + struct retval_t * retval = new struct retval_t; + retval->max_val = max_val; + retval->max_i = max_i; + retval->max_j = max_j; delete realarg; - return NULL; + return retval; }