+static void reduce(xbt_dynar_t action)
+{
+ int i;
+ char *name;
+ char task_name[80];
+ char spawn_name[80];
+ char *comm_size = xbt_dynar_get_as(action, 2, char *);
+ char *comp_size = xbt_dynar_get_as(action, 3, char *);
+ m_process_t comm_helper=NULL;
+ m_task_t task=NULL, comp_task=NULL;
+ const char* process_name;
+
+ coll_ctr counters = (coll_ctr) MSG_process_get_data(MSG_process_self());
+
+ xbt_assert0(communicator_size, "Size of Communicator is not defined"
+ ", can't use collective operations");
+
+ MSG_process_self()->data=NULL;
+
+ process_name = MSG_process_self()->name;
+
+ if (!counters){
+ DEBUG0("Initialize the counters");
+ counters = (coll_ctr) calloc (1, sizeof(struct coll_ctr_t));
+ }
+
+ name = bprintf("reduce_%d", counters->reduce_counter++);
+
+ if (!strcmp(process_name, "process0")){
+ INFO2("%s: %s is the Root",name, process_name);
+ for(i=1;i<communicator_size;i++){
+ sprintf(spawn_name,"%s_process%d", name, i);
+ sprintf(task_name,"%s_wait", spawn_name);
+ comm_helper =
+ MSG_process_create(task_name, spawned_recv,
+ (void *) xbt_strdup(spawn_name),
+ MSG_host_self());
+ }
+
+ for(i=1;i<communicator_size;i++){
+ sprintf(task_name,"%s_process%d_wait", name, i);
+ MSG_task_receive(&task,task_name);
+ MSG_task_destroy(task);
+ task=NULL;
+ }
+
+ comp_task =
+ MSG_task_create("reduce_comp", parse_double(comp_size), 0, NULL);
+ INFO1("%s: computing 'reduce_comp'", name);
+ MSG_task_execute(comp_task);
+ MSG_task_destroy(comp_task);
+ INFO1("%s: computed", name);
+ } else {
+ INFO2("%s: %s sends", name, process_name);
+ sprintf(task_name,"%s_%s", name, process_name);
+ INFO1("put on %s", task_name);
+ MSG_task_send(MSG_task_create(name, 0, parse_double(comm_size), NULL),
+ task_name);
+ }
+
+ MSG_process_set_data(MSG_process_self(), (void*)counters);
+ free(name);
+}
+