commented a bit more

git-svn-id: svn://anubis/gvsu@179 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
josh 2008-09-27 15:44:31 +00:00
parent ba71ee6707
commit ab4c97e4e1

View File

@ -73,6 +73,7 @@ int main(int argc, char * argv[])
num_threads = 1; num_threads = 1;
int file_to_read = 0; int file_to_read = 0;
/* Process command-line arguments */
for (int i = 1; i < argc; i++) for (int i = 1; i < argc; i++)
{ {
if ( ! strcmp("-n", argv[i]) ) if ( ! strcmp("-n", argv[i]) )
@ -101,6 +102,7 @@ int main(int argc, char * argv[])
matrix = new int[(files[0].size() + 1) * (files[1].size() + 1)]; matrix = new int[(files[0].size() + 1) * (files[1].size() + 1)];
pthread_barrier_init(&barrier, NULL, num_threads); pthread_barrier_init(&barrier, NULL, num_threads);
/* Create num_threads worker threads */
for (int i = 0; i < num_threads; i++) for (int i = 0; i < num_threads; i++)
{ {
int * arg = new int; int * arg = new int;
@ -113,6 +115,7 @@ int main(int argc, char * argv[])
} }
} }
/* Wait for the worker threads to exit and accumulate their results */
int max_val = 0, max_i = 0, max_j = 0; 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++)
{ {
@ -171,6 +174,13 @@ void * calcSimMatrixThread(void * arg)
int s_size = s->size(); int s_size = s->size();
int t_size = t->size(); int t_size = t->size();
int last_step = s_size + t_size; int last_step = s_size + t_size;
/* Create F as a pointer to a two-dimensional array of size
* s_size+1 X t_size+1
* This allows us to keep the similarity matrix in a stored area
* but still to access it using two-dimensional array syntax so the
* compiler does the math for us of calculating the offsets into
* the array based on s_size and 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_i = 0, max_j = 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;
@ -180,32 +190,30 @@ void * calcSimMatrixThread(void * arg)
taskAllocate(s_size+1, num_threads, id, &first_task_id, &num_tasks); taskAllocate(s_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++)
(*F)[idx][0] = 0; /* set first column to 0's */ (*F)[idx][0] = 0; /* set first column to 0's */
pthread_barrier_wait(&barrier); pthread_barrier_wait(&barrier); /* Wait for all threads to finish */
for (int step = 2; step <= last_step; step++) for (int step = 2; step <= last_step; step++)
{ {
int first_i = step - 1; int first_i = step - 1;
int first_j = 1; int first_j = 1;
int last_i = 1; int last_i = 1;
int last_j = step - 1; int last_j = step - 1;
if (first_i > s_size) if (first_i > s_size) /* Adjust if past bottom of matrix */
{ {
first_j += (first_i - s_size); first_j += (first_i - s_size);
first_i = s_size; first_i = s_size;
} }
if (last_j > t_size) if (last_j > t_size) /* Adjust if past right of matrix */
{ {
last_i += (last_j - t_size); last_i += (last_j - t_size);
last_j = t_size; last_j = t_size;
} }
num_tasks = (last_j - first_j) + 1; num_tasks = (last_j - first_j) + 1; /* first through last inclusive */
taskAllocate(num_tasks, num_threads, id, &first_task_id, &num_tasks); taskAllocate(num_tasks, num_threads, id, &first_task_id, &num_tasks);
for (int i = first_i - first_task_id, for (int i = first_i - first_task_id, /* this thread starting i */
j = first_j + first_task_id, j = first_j + first_task_id, /* this thread starting j */
task_id = 0; task_id = 0;
task_id < num_tasks; task_id < num_tasks;
i--, i--, j++, task_id++) /* loop diagonally num_tasks times */
j++,
task_id++)
{ {
/* Compute the value for the matrix */ /* Compute the value for the matrix */
(*F)[i][j] = (*F)[i][j] =
@ -238,6 +246,7 @@ void * calcSimMatrixThread(void * arg)
} }
} }
} }
/* Wait for all threads to proceed to the next step together */
pthread_barrier_wait(&barrier); pthread_barrier_wait(&barrier);
} }