diff --git a/cs677/pa2/Makefile b/cs677/pa2/Makefile index e792a6d..ed7875b 100644 --- a/cs677/pa2/Makefile +++ b/cs677/pa2/Makefile @@ -11,7 +11,7 @@ test: all ./sequential e1.* ./threaded e1.* ./sequential e2.* - ./sequential e2.* + ./threaded e2.* clean: -rm -f $(PROGS) *~ *.o diff --git a/cs677/pa2/threaded.cc b/cs677/pa2/threaded.cc index 3f47e51..f530c88 100644 --- a/cs677/pa2/threaded.cc +++ b/cs677/pa2/threaded.cc @@ -143,7 +143,7 @@ void * calcSimMatrixThread(void * arg) int s_size = s->size(); int t_size = t->size(); int last_step = s_size + t_size; - int (*F)[s_size][t_size] = (int (*) [s_size][t_size]) 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 first_task_id, num_tasks; taskAllocate(t_size+1, num_threads, id, &first_task_id, &num_tasks); @@ -153,13 +153,32 @@ void * calcSimMatrixThread(void * arg) for (int i = 0, idx = first_task_id; i < num_tasks; i++, idx++) (*F)[idx][0] = 0; /* set first column to 0's */ pthread_barrier_wait(&barrier); - for (int step = 2; i <= last_step; i++) + for (int step = 2; step <= last_step; step++) { - /* TODO: figure out how many tasks there are in this step */ - /* TODO: call taskAllocate() to get mine */ - /* TODO: loop over tasks assigned by taskAllocate() */ + int first_i = step - 1; + int first_j = 1; + int last_i = 1; + int last_j = step - 1; + if (first_i > s_size) + { + first_j += (first_i - s_size); + first_i = s_size; + } + if (last_j > t_size) + { + last_i += (last_j - t_size); + last_j = t_size; + } + num_tasks = (last_j - first_j) + 1; + taskAllocate(num_tasks, num_threads, id, &first_task_id, &num_tasks); + for (int i = first_i - first_task_id, + j = first_j + first_task_id, + task_id = 0; + task_id < num_tasks; + i--, + j++, + task_id++) { - /* TODO: figure out i, j from step and task */ /* Compute the value for the matrix */ (*F)[i][j] = max( @@ -194,6 +213,17 @@ void * calcSimMatrixThread(void * arg) pthread_barrier_wait(&barrier); } +#if 0 + cout << "Matrix: " << s_size+1 << " x " << t_size+1 << endl; + for (int i = 0; i <= s_size; i++) + { + for (int j = 0; j <= t_size; j++) + { + printf("%2d ", (*F)[i][j]); + } + printf("\n"); + } +#endif cout << "Maximum value is " << max_val << " at position (" << max_x << ", " << max_y << ")" << endl;