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
This commit is contained in:
josh 2008-09-27 15:37:25 +00:00
parent b59e2667c1
commit ba71ee6707

View File

@ -25,6 +25,13 @@ int * matrix;
vector<char> * s; vector<char> * s;
vector<char> * t; vector<char> * t;
struct retval_t
{
int max_val;
int max_i;
int max_j;
} retval;
/* Print basic usage information */ /* Print basic usage information */
void usage(char * prog) 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++) 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[] matrix;
delete[] threads; delete[] threads;
return 0; return 0;
@ -144,7 +172,7 @@ void * calcSimMatrixThread(void * arg)
int t_size = t->size(); int t_size = t->size();
int last_step = s_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 (*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; int first_task_id, num_tasks;
taskAllocate(t_size+1, num_threads, id, &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++) 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) if ((*F)[i][j] > max_val)
{ {
max_val = (*F)[i][j]; max_val = (*F)[i][j];
max_x = i; max_i = i;
max_y = j; max_j = j;
} }
else if ((*F)[i][j] == max_val) else if ((*F)[i][j] == max_val)
{ {
/* If we found a value the same as our current maximum /* If we found a value the same as our current maximum
* value, see if it has a greater i+j value */ * 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_i = i;
max_y = j; max_j = j;
} }
} }
} }
@ -224,9 +252,12 @@ void * calcSimMatrixThread(void * arg)
printf("\n"); printf("\n");
} }
#endif #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; delete realarg;
return NULL; return retval;
} }