static int server(int argc, char *argv[]) { msg_task_t task = MSG_task_create("a", 0, 0, (char*)"Some data"); MSG_task_isend(task, "mailbox"); xbt_assert(MSG_task_listen("mailbox")); // True (1) XBT_INFO("Task listen works on regular mailboxes"); task = NULL; MSG_task_receive(&task, "mailbox"); xbt_assert(!strcmp("Some data", MSG_task_get_data(task)), "Data received: %s", (char*)MSG_task_get_data(task)); MSG_task_destroy(task); XBT_INFO("Data successfully received from regular mailbox"); MSG_mailbox_set_async("mailbox2"); task = MSG_task_create("b", 0, 0, (char*)"More data"); MSG_task_isend(task, "mailbox2"); xbt_assert(MSG_task_listen("mailbox2")); // used to break. XBT_INFO("Task listen works on asynchronous mailboxes"); task = NULL; MSG_task_receive(&task, "mailbox2"); xbt_assert(!strcmp("More data", MSG_task_get_data(task))); MSG_task_destroy(task); XBT_INFO("Data successfully received from asynchronous mailbox"); return 0; }
/** Emitter function */ int master(int argc, char *argv[]) { int workers_count = 0; msg_host_t *workers = NULL; msg_task_t *todo = NULL; msg_host_t host_self = MSG_host_self(); char *master_name = (char *) MSG_host_get_name(host_self); double task_comp_size = 0; double task_comm_size = 0; char channel[1024]; double timeout = -1; int i; TRACE_category(master_name); _XBT_GNUC_UNUSED int res = sscanf(argv[1], "%lg", &timeout); xbt_assert(res,"Invalid argument %s\n", argv[1]); res = sscanf(argv[2], "%lg", &task_comp_size); xbt_assert(res, "Invalid argument %s\n", argv[2]); res = sscanf(argv[3], "%lg", &task_comm_size); xbt_assert(res, "Invalid argument %s\n", argv[3]); { /* Process organisation */ workers_count = MSG_get_host_number(); workers = xbt_dynar_to_array(MSG_hosts_as_dynar()); for (i = 0; i < workers_count; i++) if(host_self == workers[i]) { workers[i] = workers[workers_count-1]; workers_count--; break; } for (i = 0; i < workers_count; i++) MSG_process_create("worker", worker, master_name, workers[i]); } XBT_INFO("Got %d workers and will send tasks for %g seconds!", workers_count, timeout); xbt_dynar_t idle_hosts = xbt_dynar_new(sizeof(msg_host_t), NULL); msg_host_t request_host = NULL; for (i = 0; 1;) { char sprintf_buffer[64]; msg_task_t task = NULL; msg_task_t request = NULL; while(MSG_task_listen(master_name)) { res = MSG_task_receive(&(request),master_name); xbt_assert(res == MSG_OK, "MSG_task_receive failed"); request_host = MSG_task_get_data(request); xbt_dynar_push(idle_hosts, &request_host); MSG_task_destroy(request); request = NULL; } if(MSG_get_clock()>timeout) { if(xbt_dynar_length(idle_hosts) == workers_count) break; else { MSG_process_sleep(.1); continue; } } if(xbt_dynar_length(idle_hosts)<=0) { /* No request. Let's wait... */ MSG_process_sleep(.1); continue; } sprintf(sprintf_buffer, "Task_%d", i); task = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL); MSG_task_set_category(task, master_name); xbt_dynar_shift(idle_hosts, &request_host); build_channel_name(channel,master_name, MSG_host_get_name(request_host)); XBT_DEBUG("Sending \"%s\" to channel \"%s\"", task->name, channel); MSG_task_send(task, channel); XBT_DEBUG("Sent"); i++; } int task_num = i; XBT_DEBUG ("All tasks have been dispatched. Let's tell everybody the computation is over."); for (i = 0; i < workers_count; i++) { msg_task_t finalize = MSG_task_create("finalize", 0, 0, FINALIZE); MSG_task_send(finalize, build_channel_name(channel,master_name, MSG_host_get_name(workers[i % workers_count]))); } XBT_INFO("Sent %d tasks in total!", task_num); free(workers); free(todo); return 0; } /* end_of_master */