From 744e2ad392b3c83b0cc5efba7207b9fd1d5f9c09 Mon Sep 17 00:00:00 2001 From: josh Date: Fri, 26 Sep 2008 17:18:49 +0000 Subject: [PATCH] added taskAllocate() function to assign tasks to workers based on id git-svn-id: svn://anubis/gvsu@168 45c1a28c-8058-47b2-ae61-ca45b979098e --- cs677/pa2/threaded.cc | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) 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];