static void vm_migrate_async(msg_vm_t vm, msg_host_t dst_pm) { const char *vm_name = MSG_vm_get_name(vm); const char *dst_pm_name = MSG_host_get_name(dst_pm); msg_host_t host = MSG_host_self(); const char *pr_name = "mig_wrk"; char **argv = xbt_new(char *, 4); argv[0] = xbt_strdup(pr_name); argv[1] = xbt_strdup(vm_name); argv[2] = xbt_strdup(dst_pm_name); argv[3] = NULL; MSG_process_create_with_arguments(pr_name, migration_worker_main, NULL, host, 3, argv); }
void peer_init(peer_t p, int argc, char *argv[]) { p->init = 0; p->prev = NULL; p->next = NULL; p->pieces = 0; p->bytes = 0; p->pending_recvs = xbt_dynar_new(sizeof(msg_comm_t), NULL); p->pending_sends = xbt_dynar_new(sizeof(msg_comm_t), NULL); /* Set mailbox name: use host number from argv or hostname if no argument given */ if (argc > 1) { p->me = bprintf("host%s", argv[1]); } else { p->me = xbt_strdup(MSG_host_get_name(MSG_host_self())); } }
/** Master expects 3+ arguments given in the XML deployment file: */ static int master(int argc, char* argv[]) { long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s"); /** - Number of tasks */ double comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s"); /** - Task compute cost */ double comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s"); /** - Task communication size */ /* Create the tasks in advance */ msg_task_t* todo = xbt_new0(msg_task_t, number_of_tasks); for (int i = 0; i < number_of_tasks; i++) { char sprintf_buffer[64]; sprintf(sprintf_buffer, "Task_%d", i); todo[i] = MSG_task_create(sprintf_buffer, comp_size, comm_size, NULL); } /* Get the info about the worker processes from my parameters */ int worker_count = argc - 4; msg_host_t* workers = xbt_new0(msg_host_t, worker_count); for (int i = 4; i < argc; i++) { workers[i - 4] = MSG_get_host_by_name(argv[i]); xbt_assert(workers[i - 4] != NULL, "Unknown host %s. Stopping Now! ", argv[i]); } XBT_INFO("Got %d workers and %ld tasks to process", worker_count, number_of_tasks); /* Dispatch the tasks */ for (int i = 0; i < number_of_tasks; i++) { XBT_INFO("Sending '%s' to '%s'", todo[i]->name, MSG_host_get_name(workers[i % worker_count])); if (MSG_host_self() == workers[i % worker_count]) { XBT_INFO("Hey ! It's me ! :)"); } MSG_task_send(todo[i], MSG_host_get_name(workers[i % worker_count])); XBT_INFO("Sent"); } XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over."); for (int i = 0; i < worker_count; i++) { msg_task_t finalize = MSG_task_create("finalize", 0, 0, FINALIZE); MSG_task_send(finalize, MSG_host_get_name(workers[i])); } XBT_INFO("Goodbye now!"); free(workers); free(todo); return 0; }
/** master */ int master(int argc, char *argv[]) { char *slavename = NULL; double task_comm_size = 0; msg_task_t todo; char id_alias[10]; //unique id to control statistics int id = -1; if (argc != 4) { XBT_INFO("Strange number of arguments expected 3 got %d", argc - 1); } /* data size */ int read; read = sscanf(argv[1], "%lg", &task_comm_size); xbt_assert(read, "Invalid argument %s\n", argv[1]); /* slave name */ slavename = argv[2]; id = atoi(argv[3]); sprintf(id_alias, "flow_%d", id); slavenames[id] = slavename; TRACE_category(id_alias); masternames[id] = MSG_host_get_name(MSG_host_self()); { /* Task creation. */ char sprintf_buffer[64] = "Task_0"; todo = MSG_task_create(sprintf_buffer, 0, task_comm_size, NULL); MSG_task_set_category(todo, id_alias); //keep track of running tasks gl_task_array[id] = todo; gl_data_size[id] = task_comm_size; } count_finished++; /* time measurement */ sprintf(id_alias, "%d", id); start_time = MSG_get_clock(); MSG_task_send(todo, id_alias); end_time = MSG_get_clock(); return 0; } /* end_of_master */
/** Receiver function */ int slave(int argc, char *argv[]) { while (1) { msg_task_t task = NULL; int a; double time1, time2; time1 = MSG_get_clock(); a = MSG_task_receive( &(task), MSG_host_get_name(MSG_host_self()) ); time2 = MSG_get_clock(); if (a == MSG_OK) { XBT_INFO("Received \"%s\"", MSG_task_get_name(task)); if (MSG_task_get_data(task) == FINALIZE) { MSG_task_destroy(task); break; } if (time1 < *((double *) task->data)) time1 = *((double *) task->data); XBT_INFO("Communication time : \"%f\"", time2 - time1); XBT_INFO("Processing \"%s\"", MSG_task_get_name(task)); a = MSG_task_execute(task); if (a == MSG_OK) { XBT_INFO("\"%s\" done", MSG_task_get_name(task)); free(task->data); MSG_task_destroy(task); } else if (a == MSG_HOST_FAILURE) { XBT_INFO ("Gloups. The cpu on which I'm running just turned off!. See you!"); return 0; } else { XBT_INFO("Hey ?! What's up ? "); xbt_die("Unexpected behavior"); } } else if (a == MSG_HOST_FAILURE) { XBT_INFO ("Gloups. The cpu on which I'm running just turned off!. See you!"); return 0; } else if (a == MSG_TRANSFER_FAILURE) { XBT_INFO("Mmh. Something went wrong. Nevermind. Let's keep going!"); } else { XBT_INFO("Hey ?! What's up ? "); xbt_die("Unexpected behavior"); } } XBT_INFO("I'm done. See you!"); return 0; } /* end_of_slave */
/** * \brief Returns the host where the current process is located. * \param L a Lua state * \return number of values returned to Lua * * - Return value (host): the current host */ static int l_host_self(lua_State * L) { /* -- */ msg_host_t host = MSG_host_self(); lua_newtable(L); /* table */ msg_host_t* lua_host = (msg_host_t*) lua_newuserdata(L, sizeof(msg_host_t)); /* table ud */ *lua_host = host; luaL_getmetatable(L, HOST_MODULE_NAME); /* table ud mt */ lua_setmetatable(L, -2); /* table ud */ lua_setfield(L, -2, "__simgrid_host"); /* table */ return 1; }
static int computation_fun(int argc, char *argv[]) { const char *pr_name = MSG_process_get_name(MSG_process_self()); const char *host_name = MSG_host_get_name(MSG_host_self()); msg_task_t task = MSG_task_create("Task", 1000000, 1000000, NULL); double clock_sta = MSG_get_clock(); MSG_task_execute(task); double clock_end = MSG_get_clock(); XBT_INFO("%s:%s task executed %g", host_name, pr_name, clock_end - clock_sta); MSG_task_destroy(task); return 0; }
static int dvfs(int argc, char *argv[]) { msg_host_t host = NULL; double task_time = 0; host = MSG_host_self(); double current_peak = MSG_get_host_current_power_peak(host); XBT_INFO("Current power peak=%f", current_peak); double consumed_energy = MSG_get_host_consumed_energy(host); XBT_INFO("Total energy (Joules): %f", consumed_energy); // Process 1 - long CPU task int argc1 = 1; char** params1 = xbt_malloc0(sizeof(char *) * argc1); params1[0] = xbt_strdup("400.0E6"); MSG_process_create_with_arguments("proc1", process_code, NULL, host, argc1, params1); // Process 2 - sleep 2 sec + CPU task int argc2 = 2; char** params2 = xbt_malloc0(sizeof(char *) * argc2); params2[0] = xbt_strdup("100.0E6"); params2[1] = xbt_strdup("2"); MSG_process_create_with_arguments("proc2", process_code, NULL, host, argc2, params2); // Process 3 - sleep 2 sec + CPU task int argc3 = 2; char** params3 = xbt_malloc0(sizeof(char *) * argc3); params3[0] = xbt_strdup("100.0E6"); params3[1] = xbt_strdup("2"); MSG_process_create_with_arguments("proc3", process_code, NULL, host, argc3, params3); // Main process MSG_process_sleep(8); task_time = MSG_get_clock() - task_time; XBT_INFO("Task simulation time: %e", task_time); consumed_energy = MSG_get_host_consumed_energy(host); XBT_INFO("Total energy (Joules): %f", consumed_energy); return 0; }
/** Create a Java org.simgrid.msg.Process with the arguments and run it */ static int java_main(int argc, char *argv[]) { JNIEnv *env = get_current_thread_env(); simgrid::java::JavaContext* context = (simgrid::java::JavaContext*) SIMIX_context_self(); //Change the "." in class name for "/". xbt_str_subst(argv[0],'.','/',0); jclass class_Process = env->FindClass(argv[0]); xbt_str_subst(argv[0],'/','.',0); //Retrieve the methodID for the constructor xbt_assert((class_Process != nullptr), "Class not found (%s). The deployment file must use the fully qualified class name, including the package. The case is important.", argv[0]); jmethodID constructor_Process = env->GetMethodID(class_Process, "<init>", "(Lorg/simgrid/msg/Host;Ljava/lang/String;[Ljava/lang/String;)V"); xbt_assert((constructor_Process != nullptr), "Constructor not found for class %s. Is there a (Host, String ,String[]) constructor in your class ?", argv[0]); //Retrieve the name of the process. jstring jname = env->NewStringUTF(argv[0]); //Build the arguments jobjectArray args = (jobjectArray)env->NewObjectArray(argc - 1, env->FindClass("java/lang/String"), env->NewStringUTF("")); int i; for (i = 1; i < argc; i++) env->SetObjectArrayElement(args,i - 1, env->NewStringUTF(argv[i])); //Retrieve the host for the process. jstring jhostName = env->NewStringUTF(MSG_host_get_name(MSG_host_self())); jobject jhost = Java_org_simgrid_msg_Host_getByName(env, nullptr, jhostName); //creates the process jobject jprocess = env->NewObject(class_Process, constructor_Process, jhost, jname, args); xbt_assert((jprocess != nullptr), "Process allocation failed."); jprocess = env->NewGlobalRef(jprocess); //bind the process to the context msg_process_t process = MSG_process_self(); context->jprocess = jprocess; /* sets the PID and the PPID of the process */ env->SetIntField(jprocess, jprocess_field_Process_pid,(jint) MSG_process_get_PID(process)); env->SetIntField(jprocess, jprocess_field_Process_ppid, (jint) MSG_process_get_PPID(process)); jprocess_bind(jprocess, process, env); run_jprocess(env, context->jprocess); return 0; }
void worker(){ worker_t p_me = MSG_host_get_data(MSG_host_self()); while(1){ m_task_t p_task = MSG_TASK_UNINITIALIZED; msg_comm_t comm = MSG_task_irecv (&p_task, p_me->channel); MSG_comm_wait(comm, -1); MSG_comm_destroy(comm); task_t p_todo_task = MSG_task_get_data(p_task); if(p_todo_task!=FINALIZE){ MSG_task_destroy(p_task); double delay = 1.0 + p_me->lf_delay*(double)rand()/(double)(RAND_MAX); if(delay<0){ delay=0; } if (p_me->ps_ms_args->uc_coarse) { p_task = MSG_task_create(p_todo_task->name, delay*p_todo_task->f_unit_cost*MSG_get_host_speed(MSG_host_self()), 0, NULL); } else{ //p_task = MSG_task_create(p_todo_task->name, delay*p_todo_task->f_unit_cost*pow(p_todo_task->ui_tile_size,3)/3.0, 0, NULL); p_task = MSG_task_create(p_todo_task->name, delay*p_todo_task->f_unit_cost*MSG_get_host_speed(MSG_host_self()), 0, NULL); } MSG_task_execute(p_task); xbt_fifo_shift(p_me->a_workqueue); p_me->p_last_task = p_todo_task; p_todo_task->uc_done = 1; if(p_todo_task->e_type == Z || p_todo_task->e_type == ZS ){ /*put the topmost line again in the triangles*/ xbt_dynar_push_as(p_me->ps_ms_args->a_NT[p_todo_task->task.Z.ui_j],unsigned int,p_todo_task->task.Z.ui_ii); } else if (p_todo_task->e_type == F) {
static int slave(int argc, char *argv[]) { msg_task_t task; XBT_ATTRIB_UNUSED int res; int id = -1; const char * mailbox = "jupi"; double start, end; while (1) { task = NULL; res = MSG_task_receive(&(task), mailbox); xbt_assert(res == MSG_OK, "MSG_task_get failed"); XBT_INFO("Handling task \"%s\"", MSG_task_get_name(task)); if (!strcmp(MSG_task_get_name(task), "finalize")) { XBT_INFO("Destroying task \"%s\"", task->name); MSG_task_destroy(task); break; } if (!strcmp(MSG_task_get_name(task), "cancel")) { MSG_process_create("worker1", worker_main, task, MSG_host_self()); MSG_process_sleep(0.1); XBT_INFO("Canceling task \"%s\"", task->name); MSG_task_cancel(task); continue; } start = MSG_get_clock(); MSG_task_execute(task); end = MSG_get_clock(); XBT_INFO("Task \"%s\" done in %f (amount %f)", MSG_task_get_name(task), end - start, MSG_task_get_flops_amount(task)); MSG_task_destroy(task); task = NULL; id--; } XBT_INFO("I'm done. See you!"); return 0; }
/** Worker does not expect any argument from XML deployment file. */ static int worker(int argc, char* argv[]) { while (1) { msg_task_t task = NULL; int res = MSG_task_receive(&(task), MSG_host_get_name(MSG_host_self())); xbt_assert(res == MSG_OK, "MSG_task_receive failed"); XBT_INFO("Received '%s'", MSG_task_get_name(task)); if (!strcmp(MSG_task_get_name(task), "finalize")) { MSG_task_destroy(task); break; } XBT_INFO("Processing \"%s\"", MSG_task_get_name(task)); MSG_task_execute(task); XBT_INFO("'%s' done", MSG_task_get_name(task)); MSG_task_destroy(task); } XBT_INFO("I'm done. See you!"); return 0; } /* end_of_worker */
int data_replicator(int argc, char* argv[]){ /** @type simgrid process Launches `uploader` processes to replicate output files. Simgrid process parameters -------------------------- Job* replica (job descriptor) -- contains all information about job parameters (type, size, etc.) */ Job* replica = (Job*) MSG_process_get_data(MSG_process_self()); size_t output_files = replica->OutputFiles.size(); for (size_t k = 0; k < output_files; ++k) { InputFile* infl; try { infl = FILES_DATABASE->at(replica->OutputFiles.at(k)); }catch (std::out_of_range& e){ continue; } const std::vector<std::string>& replica_locations = infl->Storages; for (size_t i = 0; i < replica_locations.size(); ++i) { UploadData* data = new UploadData; data->filename = replica->OutputFiles.at(k); data->dest = replica_locations.at(i); MSG_process_create("upload", uploader, data, MSG_host_self()); } } MSG_process_sleep(1.1); delete replica; return 0; }
int service_cordel_controller(int argc,char *argv[]){ int count = argc-1; char **arg=(char **)calloc(count,sizeof(char*)); int process_number = atoi(argv[1]); m_process_t process_list[process_number]; arg[0] = strdup("service_cordel"); int i; for(i=2;i<argc;i++){ arg[i-1] = strdup(argv[i]); } XBT_INFO("initializing service: %s",arg[1]); //create a number of process for(i=0;i<process_number;i++){ process_list[i] = MSG_process_create_with_arguments(arg[0],service_cordel,NULL,MSG_host_self(),count,arg); } MSG_process_sleep(MAX_TIME_SIMULATION); //kill all process in process_list for(i=0;i<process_number;i++){ MSG_process_kill(process_list[i]); } }
int broker_controller(int argc, char *argv[]){ int count = argc-1; char **arg=(char **)calloc(count,sizeof(char*)); int process_number = atoi(argv[1]); m_process_t process_list[process_number]; arg[0] = strdup("broker"); int i; for(i=2;i<argc;i++){ arg[i-1]= strdup(argv[i]); } //create a number of process for(i=0;i<process_number;i++){ process_list[i] = MSG_process_create_with_arguments(arg[0],broker,NULL,MSG_host_self(),count,arg); if ((i+1) >= atoi(argv[2]) && (((i+1) % (atoi(argv[2]))) == 0)) MSG_process_sleep(1); } MSG_process_sleep(MAX_TIME_SIMULATION); //kill all process for(i=0;i<process_number;i++){ // MSG_process_kill(process_list[i]); } }
JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Host_currentHost(JNIEnv * env, jclass cls) { jobject jhost; msg_host_t host = MSG_host_self(); if (!MSG_host_get_data(host)) { /* the native host not yet associated with the java host instance */ /* instanciate a new java host instance */ jhost = jhost_new_instance(env); if (!jhost) { jxbt_throw_jni(env, "java host instantiation failed"); return NULL; } /* get a global reference to the newly created host */ jhost = jhost_ref(env, jhost); if (!jhost) { jxbt_throw_jni(env, "global ref allocation failed"); return NULL; } /* Sets the host name */ const char *name = MSG_host_get_name(host); jobject jname = (*env)->NewStringUTF(env,name); (*env)->SetObjectField(env, jhost, jhost_field_Host_name, jname); /* Bind & store it */ jhost_bind(jhost, host, env); MSG_host_set_data(host, (void *) jhost); } else { jhost = (jobject) MSG_host_get_data(host); } return jhost; }
/** Receiver function */ int slave(int argc, char *argv[]) { while (1) { msg_task_t task = NULL; int a; double time1, time2; time1 = MSG_get_clock(); a = MSG_task_receive( &(task), MSG_host_get_name(MSG_host_self()) ); time2 = MSG_get_clock(); if (a == MSG_OK) { XBT_INFO("Received \"%s\"", MSG_task_get_name(task)); if (MSG_task_get_data(task) == FINALIZE) { MSG_task_destroy(task); break; } if (time1 < *((double *) task->data)) time1 = *((double *) task->data); XBT_INFO("Communication time : \"%f\"", time2 - time1); XBT_INFO("Processing \"%s\"", MSG_task_get_name(task)); a = MSG_task_execute(task); if (a == MSG_OK) { XBT_INFO("\"%s\" done", MSG_task_get_name(task)); free(task->data); MSG_task_destroy(task); } else { XBT_INFO("Hey ?! What's up ? "); xbt_die("Unexpected behavior"); } } else { XBT_INFO("Hey ?! What's up ? error %d", a); xbt_die("Unexpected behavior"); } } XBT_INFO("I'm done. See you!"); 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); int number_of_tasks = 0; double task_comp_size = 0; double task_comm_size = 0; char channel[1024]; int i; _XBT_GNUC_UNUSED int res = sscanf(argv[1], "%d", &number_of_tasks); 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]); { /* Task creation */ char sprintf_buffer[64]; todo = xbt_new0(msg_task_t, number_of_tasks); for (i = 0; i < number_of_tasks; i++) { sprintf(sprintf_buffer, "Task_%d", i); todo[i] = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL); } } { /* 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 %d tasks to process", workers_count, number_of_tasks); for (i = 0; i < number_of_tasks; i++) { build_channel_name(channel,master_name, MSG_host_get_name(workers[i % workers_count])); XBT_INFO("Sending \"%s\" to channel \"%s\"", todo[i]->name, channel); MSG_task_send(todo[i], channel); XBT_INFO("Sent"); } XBT_INFO ("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("Goodbye now!"); free(workers); free(todo); return 0; } /* end_of_master */
/** 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); for (i = 0; 1; i++) { char sprintf_buffer[64]; msg_task_t task = NULL; if(MSG_get_clock()>timeout) break; 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); build_channel_name(channel,master_name, MSG_host_get_name(workers[i % workers_count])); XBT_DEBUG("Sending \"%s\" to channel \"%s\"", task->name, channel); MSG_task_send(task, channel); XBT_DEBUG("Sent"); } 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 */
/** Emitter function */ static 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); char channel[1024]; int i; long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s"); /** - Number of tasks */ double comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s"); /** - Task compute cost */ double comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s"); /** - Task communication size */ { /* Task creation */ char sprintf_buffer[64]; todo = xbt_new0(msg_task_t, number_of_tasks); for (i = 0; i < number_of_tasks; i++) { sprintf(sprintf_buffer, "Task_%d", i); todo[i] = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL); } } { /* Process organization */ 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 %Ld tasks to process", workers_count, number_of_tasks); for (i = 0; i < number_of_tasks; i++) { build_channel_name(channel,master_name, MSG_host_get_name(workers[i % workers_count])); XBT_INFO("Sending \"%s\" to channel \"%s\"", todo[i]->name, channel); MSG_task_send(todo[i], channel); XBT_INFO("Sent"); } XBT_INFO ("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("Goodbye now!"); free(workers); free(todo); return 0; } /* end_of_master */
/** Receiver function */ static int worker(int argc, char *argv[]) { msg_task_t task = NULL; XBT_ATTRIB_UNUSED int res; char channel[1024]; build_channel_name(channel,MSG_process_get_data(MSG_process_self()), MSG_host_get_name(MSG_host_self())); XBT_INFO("Receiving on channel \"%s\"", channel); while (1) { res = MSG_task_receive(&(task),channel); xbt_assert(res == MSG_OK, "MSG_task_receive failed"); XBT_INFO("Received \"%s\"", MSG_task_get_name(task)); if (!strcmp(MSG_task_get_name(task), "finalize")) { MSG_task_destroy(task); break; } XBT_INFO("Processing \"%s\"", MSG_task_get_name(task)); MSG_task_execute(task); XBT_INFO("\"%s\" done", MSG_task_get_name(task)); MSG_task_destroy(task); task = NULL; } XBT_INFO("I'm done. See you!"); return 0; } /* end_of_worker */
/** Emitter function */ int master(int argc, char *argv[]) { int workers_count = 0; msg_host_t *workers = NULL; msg_task_t *todo = NULL; int number_of_tasks = 0; double task_comp_size = 0; double task_comm_size = 0; int i; XBT_ATTRIB_UNUSED int res = sscanf(argv[1], "%d", &number_of_tasks); 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]); { /* Task creation */ char sprintf_buffer[64]; todo = xbt_new0(msg_task_t, number_of_tasks); for (i = 0; i < number_of_tasks; i++) { sprintf(sprintf_buffer, "Task_%d", i); todo[i] = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL); } } { /* Process organization */ workers_count = argc - 4; workers = xbt_new0(msg_host_t, workers_count); for (i = 4; i < argc; i++) { workers[i - 4] = MSG_get_host_by_name(argv[i]); xbt_assert(workers[i - 4] != NULL, "Unknown host %s. Stopping Now! ", argv[i]); } } XBT_INFO("Got %d workers and %d tasks to process", workers_count, number_of_tasks); for (i = 0; i < number_of_tasks; i++) { XBT_INFO("Sending \"%s\" to \"%s\"", todo[i]->name, MSG_host_get_name(workers[i % workers_count])); if (MSG_host_self() == workers[i % workers_count]) { XBT_INFO("Hey ! It's me ! :)"); } MSG_task_send(todo[i], MSG_host_get_name(workers[i % workers_count])); XBT_INFO("Sent"); } XBT_INFO ("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, MSG_host_get_name(workers[i])); } XBT_INFO("Goodbye now!"); free(workers); free(todo); return 0; } /* end_of_master */
/** Emitter function */ int master(int argc, char *argv[]) { int slaves_count = 0; msg_host_t *slaves = NULL; msg_task_t *todo = NULL; int number_of_tasks = 0; double task_comp_size = 0; double task_comm_size = 0; int i; _XBT_GNUC_UNUSED int read; read = sscanf(argv[1], "%d", &number_of_tasks); xbt_assert(read, "Invalid argument %s\n", argv[1]); read = sscanf(argv[2], "%lg", &task_comp_size); xbt_assert(read, "Invalid argument %s\n", argv[2]); read = sscanf(argv[3], "%lg", &task_comm_size); xbt_assert(read, "Invalid argument %s\n", argv[3]); { /* Task creation */ char sprintf_buffer[64]; todo = xbt_new0(msg_task_t, number_of_tasks); for (i = 0; i < number_of_tasks; i++) { sprintf(sprintf_buffer, "Task_%d", i); todo[i] = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL); } } { /* Process organization */ slaves_count = argc - 4; slaves = xbt_new0(msg_host_t, slaves_count); for (i = 4; i < argc; i++) { slaves[i - 4] = MSG_host_by_name(argv[i]); if (slaves[i - 4] == NULL) { XBT_INFO("Unknown host %s. Stopping Now! ", argv[i]); abort(); } } } XBT_INFO("Got %d slave(s) :", slaves_count); for (i = 0; i < slaves_count; i++) XBT_INFO("\t %s", MSG_host_get_name(slaves[i])); XBT_INFO("Got %d task to process :", number_of_tasks); for (i = 0; i < number_of_tasks; i++) XBT_INFO("\t\"%s\"", todo[i]->name); for (i = 0; i < number_of_tasks; i++) { XBT_INFO("Sending \"%s\" to \"%s\"", todo[i]->name, MSG_host_get_name(slaves[i % slaves_count])); if (MSG_host_self() == slaves[i % slaves_count]) { XBT_INFO("Hey ! It's me ! :)"); } MSG_task_send(todo[i], MSG_host_get_name(slaves[i % slaves_count])); XBT_INFO("Send completed"); } XBT_INFO ("All tasks have been dispatched. Let's tell everybody the computation is over."); for (i = 0; i < slaves_count; i++) MSG_task_send(MSG_task_create("finalize", 0, 0, FINALIZE), MSG_host_get_name(slaves[i])); XBT_INFO("Goodbye now!"); free(slaves); free(todo); return 0; } /* end_of_master */
/** * Returns the StarsNode instance associated to the current process. */ StarsNode & getCurrentNode() { if (inSimulation) return *static_cast<StarsNode *>(MSG_host_get_data(MSG_host_self())); else return routingTable[currentNode]; }
/* - Attach some user data to disk1 */ XBT_INFO("*** Get/set data for storage element: %s ***",storage_name); data = MSG_storage_get_data(storage); XBT_INFO("Get storage data: '%s'", data); MSG_storage_set_data(storage, xbt_strdup("Some user data")); data = MSG_storage_get_data(storage); XBT_INFO("Set and get data: '%s'", data); xbt_free(data); xbt_free(storage_name); /* - Finally dump disks contents */ XBT_INFO("*** Dump content of %s ***",MSG_host_get_name(MSG_host_self())); xbt_dict_t contents = NULL; contents = MSG_host_get_storage_content(MSG_host_self()); // contents is a dict of dicts xbt_dict_cursor_t curs, curs2 = NULL; char* mountname; xbt_dict_t content; char* path; sg_size_t *size; xbt_dict_foreach(contents, curs, mountname, content){ XBT_INFO("Print the content of mount point: %s",mountname); xbt_dict_foreach(content,curs2,path,size){ XBT_INFO("%s size: %llu bytes", path,*((sg_size_t*)size)); } xbt_dict_free(&content); } xbt_dict_free(&contents);
/** Emitter function */ int master(int argc, char *argv[]) { xbt_swag_t process_list = MSG_host_get_process_list(MSG_host_self()); msg_process_t process = NULL; MSG_process_sleep(1); xbt_swag_foreach(process, process_list) { XBT_INFO("Process(pid=%d, ppid=%d, name=%s)", MSG_process_get_PID(process), MSG_process_get_PPID(process), MSG_process_get_name(process)); if (MSG_process_self_PID() != MSG_process_get_PID(process)) MSG_process_kill(process); } process = MSG_process_create("slave from master", slave, NULL, MSG_host_self()); MSG_process_sleep(2); XBT_INFO("Suspend Process(pid=%d)", MSG_process_get_PID(process)); MSG_process_suspend(process); XBT_INFO("Process(pid=%d) is %ssuspended", MSG_process_get_PID(process), (MSG_process_is_suspended(process)) ? "" : "not "); MSG_process_sleep(2); XBT_INFO("Resume Process(pid=%d)", MSG_process_get_PID(process)); MSG_process_resume(process); XBT_INFO("Process(pid=%d) is %ssuspended", MSG_process_get_PID(process),
XBT_INFO("I'm done. See you!"); return 0; } static int master(int argc, char *argv[]) { xbt_swag_t process_list = MSG_host_get_process_list(MSG_host_self()); msg_process_t process = NULL; MSG_process_sleep(1); xbt_swag_foreach(process, process_list) { XBT_INFO("Process(pid=%d, ppid=%d, name=%s)", MSG_process_get_PID(process), MSG_process_get_PPID(process), MSG_process_get_name(process)); if (MSG_process_self_PID() != MSG_process_get_PID(process)) MSG_process_kill(process); } process = MSG_process_create("slave from master", slave, NULL, MSG_host_self()); MSG_process_sleep(2); XBT_INFO("Suspend Process(pid=%d)", MSG_process_get_PID(process)); MSG_process_suspend(process); XBT_INFO("Process(pid=%d) is %ssuspended", MSG_process_get_PID(process), (MSG_process_is_suspended(process)) ? "" : "not "); MSG_process_sleep(2); XBT_INFO("Resume Process(pid=%d)", MSG_process_get_PID(process)); MSG_process_resume(process); XBT_INFO("Process(pid=%d) is %ssuspended", MSG_process_get_PID(process), (MSG_process_is_suspended(process)) ? "" : "not "); MSG_process_sleep(2);
/** 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 */