diff --git a/cs677/pa2/threaded.cc b/cs677/pa2/threaded.cc index 018f09f..fb88e33 100644 --- a/cs677/pa2/threaded.cc +++ b/cs677/pa2/threaded.cc @@ -32,6 +32,34 @@ void usage(char * prog) exit(42); } +/* + * taskAllocate() will divide a set of total_tasks tasks into + * total_workers groups, as evenly as possible + * Parameters: + * total_tasks : IN : the total number of tasks to divide up + * total_workers : IN : the total number of workers to allocate tasks to (>0) + * this_id : IN : the id (base 0) of the task calling us for work + * first_task_id : OUT : the id (base 0) of the first task for this worker + * num : OUT : the number of tasks assigned to this worker + */ +inline void taskAllocate(int total_tasks, int total_workers, int this_id, + int * first_task_id, int * num) +{ + int l_num; + int leftovers = total_tasks % total_workers; /* num of "leftover" tasks */ + if (this_id < leftovers) + { + l_num = total_tasks / total_workers + 1; /* do one of the leftovers */ + *first_task_id = l_num * this_id; + } + else + { + l_num = total_tasks / total_workers; + *first_task_id = l_num * this_id + leftovers; + } + *num = l_num; +} + int main(int argc, char * argv[]) { vector files[2];