/** The guy we will move from host to host. It move alone and then is moved by policeman back */ static int emigrant(int argc, char *argv[]) { msg_task_t task; XBT_INFO ("I'll look for a new job on another machine where the grass is greener."); MSG_process_migrate(MSG_process_self(), MSG_get_host_by_name("Boivin")); XBT_INFO("Yeah, found something to do"); task = MSG_task_create("job", 98095000, 0, NULL); MSG_task_execute(task); MSG_task_destroy(task); MSG_process_sleep(2); XBT_INFO("Moving back home after work"); MSG_process_migrate(MSG_process_self(), MSG_get_host_by_name("Jacquelin")); MSG_process_migrate(MSG_process_self(), MSG_get_host_by_name("Boivin")); MSG_process_sleep(4); xbt_mutex_acquire(mutex); process_to_migrate = MSG_process_self(); xbt_cond_broadcast(cond); xbt_mutex_release(mutex); MSG_process_suspend(MSG_process_self()); msg_host_t h = MSG_process_get_host(MSG_process_self()); XBT_INFO("I've been moved on this new host: %s", MSG_host_get_name(h)); XBT_INFO("Uh, nothing to do here. Stopping now"); return 0; } /* end_of_emigrant */
static int par_task(int /*argc*/, char* /*argv*/ []) { double * computation_amount = new double[2] {10E7, 10E7}; double * communication_amount = new double[4] {1E6, 1E6, 1E6, 1E6}; double progress; std::vector<msg_host_t> hosts_to_use = std::vector<msg_host_t>(); hosts_to_use.push_back(MSG_get_host_by_name("Tremblay")); hosts_to_use.push_back(MSG_get_host_by_name("Jupiter")); msg_task_t task = MSG_parallel_task_create("ptask", 2, hosts_to_use.data(), computation_amount, communication_amount, NULL); tasks.push_back(task); XBT_INFO("get the progress of %s before the task starts", task->name); progress = MSG_task_get_remaining_work_ratio(task); xbt_assert(progress == 1.0, "Progress should be 1.0 not %f", progress); XBT_INFO("Executing task: \"%s\"", task->name); MSG_parallel_task_execute(task); XBT_INFO("get the progress of %s after the task finishes", task->name); progress = MSG_task_get_remaining_work_ratio(task); xbt_assert(progress == 0.0, "Progress should be equal to 0.0 not %f", progress); MSG_task_destroy(task); delete[] computation_amount; delete[] communication_amount; XBT_INFO("Goodbye now!"); return 0; }
int main(int argc, char *argv[]) { MSG_init(&argc, argv); xbt_assert(argc > 1, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]); MSG_create_environment(argv[1]); MSG_process_create("simple_func", process_function, NULL, MSG_get_host_by_name("Tremblay")); MSG_process_create("simple_func", process_function, NULL, MSG_get_host_by_name("Fafard")); MSG_main(); return 0; }
int main(int argc, char *argv[]) { msg_error_t res = MSG_OK; MSG_init(&argc, argv); MSG_create_environment(argv[1]); MSG_process_create("send", sender_fun, NULL, MSG_get_host_by_name("Tremblay")); MSG_process_create("receive", receiver_fun, NULL, MSG_get_host_by_name("Tremblay")); res = MSG_main(); return res != MSG_OK; }
int main(int argc, char *argv[]) { MSG_init(&argc, argv); xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]); MSG_create_environment(argv[1]); MSG_process_create("master", master, NULL, MSG_get_host_by_name("Tremblay")); MSG_process_create("worker", worker, NULL, MSG_get_host_by_name("Jupiter")); msg_error_t res = MSG_main(); XBT_INFO("Simulation time %g", MSG_get_clock()); return res != MSG_OK; }
// FIXME: don't mess with MSG internals here, use MSG_process_create_with_arguments() static void rb_process_create_with_args(VALUE fct_name, VALUE arguments, VALUE properties, VALUE ht_name) { VALUE ruby_process = rb_process_instance(fct_name, arguments, properties); m_process_t process = NULL; // Native Process to Create const char *name; // Name of C Native Processs if (!fct_name) rb_raise(rb_eRuntimeError, "Internal error: Process name cannot be NULL"); name = RSTRING_PTR(fct_name); XBT_DEBUG("Create native process %s", name); char **argv = xbt_new(char *, 2); argv[0] = bprintf("%s@%s", name, RSTRING_PTR(ht_name)); argv[1] = NULL; // Allocate the data for the simulation process = MSG_process_create_with_arguments(name, (xbt_main_func_t) ruby_process, process, MSG_get_host_by_name(RSTRING_PTR(ht_name)), 1, argv); // Bind The Ruby Process instance to The Native Process rb_process_bind(ruby_process, process); }
/** * \ingroup msg_file_management * \brief Copy a file to another location on a remote host. * \param file : the file to move * \param host : the remote host where the file has to be copied * \param fullpath : the complete path destination on the remote host * \return If successful, the function returns MSG_OK. Otherwise, it returns * MSG_TASK_CANCELED. */ msg_error_t MSG_file_rcopy (msg_file_t file, msg_host_t host, const char* fullpath) { msg_file_priv_t file_priv = MSG_file_priv(file); sg_size_t read_size; /* Find the host where the file is physically located and read it */ msg_storage_t storage_src =(msg_storage_t) xbt_lib_get_elm_or_null(storage_lib, file_priv->storageId); msg_storage_priv_t storage_priv_src = MSG_storage_priv(storage_src); msg_host_t attached_host = MSG_get_host_by_name(storage_priv_src->hostname); MSG_file_seek(file, 0, SEEK_SET); read_size = simcall_file_read(file_priv->simdata->smx_file, file_priv->size, attached_host); /* Find the real host destination where the file will be physically stored */ xbt_dict_cursor_t cursor = NULL; char *mount_name, *storage_name, *file_mount_name, *host_name_dest; msg_storage_t storage_dest = NULL; msg_host_t host_dest; size_t longest_prefix_length = 0; xbt_dict_t storage_list = simcall_host_get_mounted_storage_list(host); xbt_dict_foreach(storage_list,cursor,mount_name,storage_name){ file_mount_name = (char *) xbt_malloc ((strlen(mount_name)+1)); strncpy(file_mount_name,fullpath,strlen(mount_name)+1); file_mount_name[strlen(mount_name)] = '\0'; if(!strcmp(file_mount_name,mount_name) && strlen(mount_name)>longest_prefix_length){ /* The current mount name is found in the full path and is bigger than the previous*/ longest_prefix_length = strlen(mount_name); storage_dest = (msg_storage_t) xbt_lib_get_elm_or_null(storage_lib, storage_name); } free(file_mount_name); }
/** Emitter function */ int master(int argc, char *argv[]) { double task_comp_size = 5E7; double task_comm_size = 1E6; char mailbox[256]; msg_task_t task = NULL; msg_host_t jupiter = MSG_get_host_by_name("Jupiter"); sprintf(mailbox, "jupi"); task = MSG_task_create("task on", task_comp_size, task_comm_size, NULL); XBT_INFO("Sending \"%s\"", task->name); if (MSG_task_send_with_timeout(task, mailbox, 1) != MSG_OK) MSG_task_destroy(task); MSG_process_sleep(1); MSG_host_off(jupiter); task = MSG_task_create("task off", task_comp_size, task_comm_size, NULL); XBT_INFO("Sending \"%s\"", task->name); if (MSG_task_send_with_timeout(task, mailbox, 1) != MSG_OK) MSG_task_destroy(task); MSG_host_on(jupiter); xbt_swag_t jupi_processes = MSG_host_get_process_list(jupiter); void *process; xbt_swag_foreach(process, jupi_processes) { MSG_process_kill(process); }
int main(int argc, char* argv[]) { MSG_init(&argc, argv); xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]); MSG_create_environment(argv[1]); MSG_process_create("dvfs_test", dvfs, NULL, MSG_get_host_by_name("MyHost1")); MSG_process_create("dvfs_test", dvfs, NULL, MSG_get_host_by_name("MyHost2")); msg_error_t res = MSG_main(); XBT_INFO("Total simulation time: %e", MSG_get_clock()); return res != MSG_OK; }
/** Main function */ int main(int argc, char *argv[]) { MSG_init(&argc, argv); MSG_function_register("app", &app); MSG_create_environment(argv[1]); MSG_process_create("app", app, NULL, MSG_get_host_by_name("Tremblay")); return MSG_main(); }
/** The guy we will move from host to host. It move alone and then is moved by policeman back */ static int emigrant(int argc, char *argv[]) { m_task_t task; XBT_INFO ("I'll look for a new job on another machine where the grass is greener."); MSG_process_change_host(MSG_get_host_by_name("Boivin")); XBT_INFO("Yeah, found something to do"); task = MSG_task_create("job", 98095000, 0, NULL); MSG_task_execute(task); MSG_task_destroy(task); MSG_process_sleep(2); XBT_INFO("Moving back home after work"); MSG_process_change_host(MSG_get_host_by_name("Jacquelin")); MSG_process_change_host(MSG_get_host_by_name("Boivin")); MSG_process_sleep(4); XBT_INFO("Uh, nothing to do here. Stopping now"); return 0; } /* end_of_emigrant */
int execute(int argc, char *argv[]) { char buffer[32]; int i, j; msg_host_t *m_host_list = NULL; msg_task_t task = NULL; int host_list_size; double *computation_duration = NULL; double *communication_table = NULL; double communication_amount = 0; double computation_amount = 0; double execution_time; host_list_size = argc - 3; XBT_DEBUG("host_list_size=%d", host_list_size); m_host_list = calloc(host_list_size, sizeof(msg_host_t)); for (i = 1; i <= host_list_size; i++) { m_host_list[i - 1] = MSG_get_host_by_name(argv[i]); xbt_assert(m_host_list[i - 1] != NULL, "Unknown host %s. Stopping Now! ", argv[i]); } _XBT_GNUC_UNUSED int read; read = sscanf(argv[argc - 2], "%lg", &computation_amount); xbt_assert(read, "Invalid argument %s\n", argv[argc - 2]); read = sscanf(argv[argc - 1], "%lg", &communication_amount); xbt_assert(read, "Invalid argument %s\n", argv[argc - 1]); computation_duration = (double *) calloc(host_list_size, sizeof(double)); communication_table = (double *) calloc(host_list_size * host_list_size, sizeof(double)); for (i = 0; i < host_list_size; i++) { computation_duration[i] = computation_amount / host_list_size; for (j = 0; j < host_list_size; j++) communication_table[i * host_list_size + j] = communication_amount / (host_list_size * host_list_size); } sprintf(buffer, "redist#0\n"); task = MSG_parallel_task_create(buffer, host_list_size, m_host_list, computation_duration, communication_table, NULL); execution_time = MSG_get_clock(); MSG_parallel_task_execute(task); MSG_task_destroy(task); xbt_free(m_host_list); execution_time = MSG_get_clock() - execution_time; XBT_INFO("execution_time=%g ", execution_time); return 0; }
/* This function creates a MSG process. It has the prototype enforced by SIMIX_function_register_process_create */ void MSG_process_create_from_SIMIX(smx_process_t* process, const char *name, xbt_main_func_t code, void *data, const char *hostname, int argc, char **argv, xbt_dict_t properties) { m_host_t host = MSG_get_host_by_name(hostname); m_process_t p = MSG_process_create_with_environment(name, code, data, host, argc, argv, properties); *((m_process_t*) process) = p; }
int main(int argc, char *argv[]) { MSG_init(&argc, argv); MSG_config("host/model", "ptask_L07"); xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s ../examples/platforms/two_hosts.xml\n", argv[0], argv[0]); MSG_create_environment(argv[1]); MSG_process_create("sequential", seq_task, NULL, MSG_get_host_by_name("Tremblay")); MSG_process_create("parallel", par_task, NULL, MSG_get_host_by_name("Tremblay")); // Create a process to test in progress task MSG_process_create("get_progress", get_progress, NULL, MSG_get_host_by_name("Tremblay")); msg_error_t res = MSG_main(); XBT_INFO("Simulation time %g", MSG_get_clock()); return res != MSG_OK; }
/** 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; xbt_assert(argc==4,"Strange number of arguments expected 3 got %d", argc - 1); XBT_DEBUG ("Master started"); /* 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, 100*task_comm_size, 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; } { /* Process organisation */ MSG_get_host_by_name(slavename); } count_finished++; timer_start = 1 ; /* time measurement */ sprintf(id_alias, "%d", id); start_time = MSG_get_clock(); //MSG_task_execute(todo); MSG_task_send(todo, id_alias); end_time = MSG_get_clock(); XBT_DEBUG ("Finished"); return 0; } /* end_of_master */
/** \ingroup msg_file_management * \brief Unlink the file pointed by fd * * \param fd is the file descriptor (#msg_file_t) * \return 0 on success or 1 on error */ msg_error_t MSG_file_unlink(msg_file_t fd) { msg_file_priv_t file_priv = MSG_file_priv(fd); /* Find the host where the file is physically located (remote or local)*/ msg_storage_t storage_src = (msg_storage_t) xbt_lib_get_elm_or_null(storage_lib, file_priv->storageId); msg_storage_priv_t storage_priv_src = MSG_storage_priv(storage_src); msg_host_t attached_host = MSG_get_host_by_name(storage_priv_src->hostname); int res = simcall_file_unlink(file_priv->simdata->smx_file, attached_host); return res; }
int dvfs(int argc, char *argv[]) { msg_host_t host = NULL; msg_task_t task1 = NULL; double task_time = 0; host = MSG_get_host_by_name("MyHost1"); double current_peak = MSG_get_host_current_power_peak(host); XBT_INFO("Current power peak=%lf", current_peak); double consumed_energy = MSG_get_host_consumed_energy(host); XBT_INFO("Total energy (Joules): %lf", consumed_energy); // Run a task task1 = MSG_task_create ("t1", 100E6, 0, NULL); MSG_task_execute (task1); MSG_task_destroy(task1); task_time = MSG_get_clock(); XBT_INFO("Task1 simulation time: %le", task_time); consumed_energy = MSG_get_host_consumed_energy(host); XBT_INFO("Total energy (Joules): %lf", consumed_energy); // ========= Change power peak ========= int peak_index=2; double peak_at = MSG_get_host_power_peak_at(host, peak_index); XBT_INFO("=========Changing power peak value to %lf (at index %d)", peak_at, peak_index); MSG_set_host_power_peak_at(host, peak_index); // Run a second task task1 = MSG_task_create ("t2", 100E6, 0, NULL); MSG_task_execute (task1); MSG_task_destroy(task1); task_time = MSG_get_clock() - task_time; XBT_INFO("Task2 simulation time: %le", task_time); consumed_energy = MSG_get_host_consumed_energy(host); XBT_INFO("Total energy (Joules): %lf", consumed_energy); MSG_process_sleep(3); task_time = MSG_get_clock() - task_time; XBT_INFO("Task3 (sleep) simulation time: %le", task_time); consumed_energy = MSG_get_host_consumed_energy(host); XBT_INFO("Total energy (Joules): %lf", consumed_energy); return 0; }
JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Host_getByName(JNIEnv * env, jclass cls, jstring jname) { msg_host_t host; /* native host */ jobject jhost; /* global reference to the java host instance returned */ /* get the C string from the java string */ const char *name = (*env)->GetStringUTFChars(env, jname, 0); if (name == NULL) { jxbt_throw_null(env,bprintf("No host can have a null name")); return NULL; } /* get the host by name (the hosts are created during the grid resolution) */ host = MSG_get_host_by_name(name); if (!host) { /* invalid name */ jxbt_throw_host_not_found(env, name); (*env)->ReleaseStringUTFChars(env, jname, name); return NULL; } (*env)->ReleaseStringUTFChars(env, jname, name); if (!MSG_host_get_data(host)) { /* native host not associated yet with java host */ /* Instantiate a new java host */ 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, "new global ref allocation failed"); return NULL; } /* Sets the java host name */ (*env)->SetObjectField(env, jhost, jhost_field_Host_name, jname); /* bind the java host and the native host */ jhost_bind(jhost, host, env); /* the native host data field is set with the global reference to the * java host returned by this function */ MSG_host_set_data(host, (void *) jhost); } /* return the global reference to the java host instance */ return (jobject) MSG_host_get_data(host); }
/* This function move the emigrant on Jacquelin */ static int policeman(int argc, char *argv[]) { xbt_mutex_acquire(mutex); XBT_INFO("Wait a bit before migrating the emigrant."); while (process_to_migrate == NULL) xbt_cond_wait(cond, mutex); MSG_process_migrate(process_to_migrate, MSG_get_host_by_name("Jacquelin")); XBT_INFO("I moved the emigrant"); MSG_process_resume(process_to_migrate); xbt_mutex_release(mutex); return 0; } /* end_of_policeman */
int host(int argc, char *argv[]){ msg_file_t file = NULL; const char* filename; sg_size_t read, write; file = MSG_file_open(argv[1], NULL); filename = MSG_file_get_name(file); XBT_INFO("Opened file '%s'",filename); MSG_file_dump(file); XBT_INFO("Try to read %llu from '%s'",MSG_file_get_size(file),filename); read = MSG_file_read(file, MSG_file_get_size(file)); XBT_INFO("Have read %llu from '%s'. Offset is now at: %llu",read,filename, MSG_file_tell(file)); XBT_INFO("Seek back to the begining of the stream..."); MSG_file_seek(file, 0, SEEK_SET); XBT_INFO("Offset is now at: %llu", MSG_file_tell(file)); MSG_file_close(file); if (argc > 5){ file = MSG_file_open(argv[2], NULL); filename = MSG_file_get_name(file); XBT_INFO("Opened file '%s'",filename); XBT_INFO("Try to write %llu MiB to '%s'", MSG_file_get_size(file)/1024, filename); write = MSG_file_write(file, MSG_file_get_size(file)*1024); XBT_INFO("Have written %llu bytes to '%s'.",write,filename); msg_host_t src, dest; src= MSG_host_self(); dest = MSG_get_host_by_name(argv[3]); if (atoi(argv[5])){ XBT_INFO("Move '%s' (of size %llu) from '%s' to '%s'", filename, MSG_file_get_size(file), MSG_host_get_name(src), argv[3]); MSG_file_rmove(file, dest, argv[4]); } else { XBT_INFO("Copy '%s' (of size %llu) from '%s' to '%s'", filename, MSG_file_get_size(file), MSG_host_get_name(src), argv[3]); MSG_file_rcopy(file, dest, argv[4]); MSG_file_close(file); } } return 0; }
/* This function creates a MSG process. It has the prototype enforced by SIMIX_function_register_process_create */ void MSG_process_create_from_SIMIX(smx_process_t* process, const char *name, xbt_main_func_t code, void *data, const char *hostname, double kill_time, int argc, char **argv, xbt_dict_t properties, int auto_restart) { msg_host_t host = MSG_get_host_by_name(hostname); msg_process_t p = MSG_process_create_with_environment(name, code, data, host, argc, argv, properties); if (p) { MSG_process_set_kill_time(p,kill_time); MSG_process_auto_restart_set(p,auto_restart); } *((msg_process_t*) process) = p; }
/** 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; }
/** * \brief Returns a host given its name. * \param L a Lua state * \return number of values returned to Lua * * - Argument 1 (string): name of a host * - Return value (host): the corresponding host */ static int l_host_get_by_name(lua_State * L) { const char *name = luaL_checkstring(L, 1); XBT_DEBUG("Getting Host from name..."); msg_host_t msg_host = MSG_get_host_by_name(name); if (!msg_host) { luaL_error(L, "null Host : MSG_get_host_by_name failed"); } lua_newtable(L); /* create a table, put the userdata on top of it */ msg_host_t *lua_host = (msg_host_t *) lua_newuserdata(L, sizeof(msg_host_t)); *lua_host = msg_host; luaL_getmetatable(L, HOST_MODULE_NAME); lua_setmetatable(L, -2); lua_setfield(L, -2, "__simgrid_host"); /* put the userdata as field of the table */ /* remove the args from the stack */ lua_remove(L, 1); return 1; }
/** Emitter function */ int sender(int argc, char *argv[]) { msg_host_t host = NULL; double time; msg_task_t task_la = NULL; msg_task_t task_bw = NULL; char sprintf_buffer_la[64]; char sprintf_buffer_bw[64]; XBT_INFO("sender"); /*host = xbt_new0(msg_host_t,1); */ XBT_INFO("host = %s", argv[1]); host = MSG_get_host_by_name(argv[1]); if (host == NULL) { XBT_INFO("Unknown host %s. Stopping Now! ", argv[1]); abort(); } /* Latency */ time = MSG_get_clock(); sprintf(sprintf_buffer_la, "latency task"); task_la = MSG_task_create(sprintf_buffer_la, 0.0, task_comm_size_lat, NULL); task_la->data = xbt_new(double, 1); *(double *) task_la->data = time; XBT_INFO("task_la->data = %le", *((double *) task_la->data)); MSG_task_send(task_la, argv[1]); /* Bandwidth */ time = MSG_get_clock(); sprintf(sprintf_buffer_bw, "bandwidth task"); task_bw = MSG_task_create(sprintf_buffer_bw, 0.0, task_comm_size_bw, NULL); task_bw->data = xbt_new(double, 1); *(double *) task_bw->data = time; XBT_INFO("task_bw->data = %le", *((double *) task_bw->data)); MSG_task_send(task_bw, argv[1]); return 0; } /* end_of_client */
/** The guy we will move from host to host. It move alone and then is moved by policeman back */ static int emigrant(int argc, char *argv[]) { msg_task_t task = NULL; char *destination = NULL; MSG_process_sleep(2); while (1){ // I am an eternal emigrant MSG_task_receive(&(task), "master_mailbox"); destination = (char*)MSG_task_get_data (task); MSG_task_destroy (task); if (!destination) break; //there is no destination, die MSG_process_migrate(MSG_process_self(), MSG_get_host_by_name(destination)); MSG_process_sleep(2); // I am tired, have to sleep for 2 seconds free (destination); task = NULL; } return 0; }
/** \ingroup msg_file_management * \brief Write into a file (local or remote) * * \param size of the file to write * \param fd is a the file descriptor * \return the number of bytes successfully write or -1 if an error occurred */ sg_size_t MSG_file_write(msg_file_t fd, sg_size_t size) { msg_file_priv_t file_priv = MSG_file_priv(fd); sg_size_t write_size, offset; /* Find the host where the file is physically located (remote or local)*/ msg_storage_t storage_src =(msg_storage_t) xbt_lib_get_elm_or_null(storage_lib, file_priv->storageId); msg_storage_priv_t storage_priv_src = MSG_storage_priv(storage_src); msg_host_t attached_host = MSG_get_host_by_name(storage_priv_src->hostname); if(strcmp(storage_priv_src->hostname, MSG_host_get_name(MSG_host_self()))){ /* the file is hosted on a remote host, initiate a communication between src and dest hosts for data transfer */ XBT_DEBUG("File is on %s remote host, initiate data transfer of %llu bytes.", storage_priv_src->hostname, size); msg_host_t *m_host_list = NULL; m_host_list = calloc(2, sizeof(msg_host_t)); m_host_list[0] = MSG_host_self(); m_host_list[1] = attached_host; double flops_amount[] = { 0, 0 }; double bytes_amount[] = { 0, (double)size, 0, 0 }; msg_task_t task = MSG_parallel_task_create("file transfer for write", 2, m_host_list, flops_amount, bytes_amount, NULL); msg_error_t transfer = MSG_parallel_task_execute(task); MSG_task_destroy(task); free(m_host_list); if(transfer != MSG_OK){ if (transfer == MSG_HOST_FAILURE) XBT_WARN("Transfer error, %s remote host just turned off!", MSG_host_get_name(attached_host)); if (transfer == MSG_TASK_CANCELED) XBT_WARN("Transfer error, task has been canceled!"); return -1; } } /* Write file on local or remote host */ offset = simcall_file_tell(file_priv->simdata->smx_file); write_size = simcall_file_write(file_priv->simdata->smx_file, size, attached_host); file_priv->size = offset+write_size; return write_size; }
int test_launcher(int argc, char *argv[]) { int test = 0; char **argvF; argvF = xbt_new(char*, 2); argvF[0] = xbt_strdup("process_daemon"); msg_host_t jupiter = MSG_get_host_by_name("Jupiter"); test = 1; // Create a process running a simple task on a host and turn the host off during the execution of the process. if (xbt_dynar_search_or_negative(tests, &test)!=-1){ XBT_INFO("Test 1:"); XBT_INFO(" Create a process on Jupiter"); argvF = xbt_new(char*, 2); argvF[0] = xbt_strdup("process_daemon"); MSG_process_create_with_arguments("process_daemon", process_daemon, NULL, jupiter, 1, argvF); MSG_process_sleep(3); XBT_INFO(" Turn off Jupiter"); MSG_host_off(jupiter); MSG_process_sleep(10); XBT_INFO("Test 1 seems ok, cool !(number of Process : %d, it should be 1 (i.e. the Test one))", MSG_process_get_number()); }
int master(int argc, char *argv[]) { int slaves_count = 0; msg_host_t *slaves = NULL; int number_of_tasks = 0; double task_comp_size = 0; double task_comm_size = 0; int i; number_of_tasks = TASK_COUNT_PER_HOST*argc; task_comp_size = TASK_COMP_SIZE; task_comm_size = TASK_COMM_SIZE; { /* Process organization */ slaves_count = argc; slaves = xbt_new0(msg_host_t, slaves_count); for (i = 0; i < argc; i++) { slaves[i] = MSG_get_host_by_name(argv[i]); if (slaves[i] == 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("%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++) { msg_task_t task = MSG_task_create("Task", task_comp_size, task_comm_size, xbt_new0(double, 1)); int a; *((double *) task->data) = MSG_get_clock(); a = MSG_task_send_with_timeout(task,MSG_host_get_name(slaves[i % slaves_count]),10.0); if (a == MSG_OK) { XBT_INFO("Send completed"); } else if (a == MSG_HOST_FAILURE) { XBT_INFO ("Gloups. The cpu on which I'm running just turned off!. See you!"); free(task->data); MSG_task_destroy(task); free(slaves); return 0; } else if (a == MSG_TRANSFER_FAILURE) { XBT_INFO ("Mmh. Something went wrong with '%s'. Nevermind. Let's keep going!", MSG_host_get_name(slaves[i % slaves_count])); free(task->data); MSG_task_destroy(task); } else if (a == MSG_TIMEOUT) { XBT_INFO ("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!", MSG_host_get_name(slaves[i % slaves_count])); free(task->data); MSG_task_destroy(task); } else { XBT_INFO("Hey ?! What's up ? "); xbt_die( "Unexpected behavior"); } } XBT_INFO ("All tasks have been dispatched. Let's tell everybody the computation is over."); for (i = 0; i < slaves_count; i++) { msg_task_t task = MSG_task_create("finalize", 0, 0, FINALIZE); int a = MSG_task_send_with_timeout(task,MSG_host_get_name(slaves[i]),1.0); if (a == MSG_OK) continue; if (a == MSG_HOST_FAILURE) { XBT_INFO ("Gloups. The cpu on which I'm running just turned off!. See you!"); MSG_task_destroy(task); free(slaves); return 0; } else if (a == MSG_TRANSFER_FAILURE) { XBT_INFO("Mmh. Can't reach '%s'! Nevermind. Let's keep going!", MSG_host_get_name(slaves[i])); MSG_task_destroy(task); } else if (a == MSG_TIMEOUT) { XBT_INFO ("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!", MSG_host_get_name(slaves[i % slaves_count])); MSG_task_destroy(task); } else { XBT_INFO("Hey ?! What's up ? "); xbt_die("Unexpected behavior with '%s': %d", MSG_host_get_name(slaves[i]), a); } } XBT_INFO("Goodbye now!"); free(slaves); 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 organisation */ slaves_count = argc - 4; slaves = xbt_new0(msg_host_t, slaves_count); for (i = 4; i < argc; i++) { slaves[i - 4] = MSG_get_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 */
XBT_INFO("Putting it in surf..."); platf_generate(); XBT_INFO("Let's get the available hosts and dispatch work:"); unsigned int i; msg_host_t host = NULL; msg_host_t host_master = NULL; msg_process_t process = NULL; xbt_dynar_t host_dynar = MSG_hosts_as_dynar(); char** hostname_list = xbt_malloc(sizeof(char*) * xbt_dynar_length(host_dynar)); xbt_dynar_foreach(host_dynar, i, host) { process = MSG_process_create("slave", slave, NULL, host); MSG_process_auto_restart_set(process, TRUE); hostname_list[i] = (char*) MSG_host_get_name(host); } host_master = MSG_get_host_by_name("host_master"); MSG_process_create_with_arguments("master", master, NULL, host_master, xbt_dynar_length(host_dynar), hostname_list); res = MSG_main(); if (res == MSG_OK) return 0; else return 1; } /* end_of_main */