1 /* Copyright (c) 2007-2016. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "simgrid/msg.h"
7 #include "simgrid/plugins/energy.h"
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
11 /** @addtogroup MSG_examples
13 * - <b>energy-ptask/energy-ptask.c</b>: Demonstrates the use of @ref MSG_parallel_task_create, to create special
14 * tasks that run on several hosts at the same time. The resulting simulations are very close to what can be
15 * achieved in @ref SD_API, but still allows to use the other features of MSG (it'd be cool to be able to mix
16 * interfaces, but it's not possible ATM).
19 static int runner(int argc, char *argv[])
21 /* Retrieve the list of all hosts as an array of hosts */
22 int hosts_count = MSG_get_host_number();
23 msg_host_t *hosts = xbt_dynar_to_array(MSG_hosts_as_dynar());
25 XBT_INFO("First, build a classical parallel task, with 1 Gflop to execute on each node, "
26 "and 10MB to exchange between each pair");
27 double *computation_amounts = xbt_new0(double, hosts_count);
28 double *communication_amounts = xbt_new0(double, hosts_count * hosts_count);
30 for (int i = 0; i < hosts_count; i++)
31 computation_amounts[i] = 1e9; // 1 Gflop
33 for (int i = 0; i < hosts_count; i++)
34 for (int j = i + 1; j < hosts_count; j++)
35 communication_amounts[i * hosts_count + j] = 1e7; // 10 MB
38 MSG_parallel_task_create("parallel task", hosts_count, hosts, computation_amounts, communication_amounts, NULL);
39 MSG_parallel_task_execute(ptask);
40 MSG_task_destroy(ptask);
41 /* The arrays communication_amounts and computation_amounts are not to be freed manually */
43 XBT_INFO("We can do the same with a timeout of one second enabled.");
44 computation_amounts = xbt_new0(double, hosts_count);
45 communication_amounts = xbt_new0(double, hosts_count* hosts_count);
46 for (int i = 0; i < hosts_count; i++)
47 computation_amounts[i] = 1e9; // 1 Gflop
48 for (int i = 0; i < hosts_count; i++)
49 for (int j = i + 1; j < hosts_count; j++)
50 communication_amounts[i * hosts_count + j] = 1e7; // 10 MB
52 MSG_parallel_task_create("parallel task", hosts_count, hosts, computation_amounts, communication_amounts, NULL);
53 msg_error_t errcode = MSG_parallel_task_execute_with_timeout(ptask, 1 /* timeout (in seconds)*/);
54 xbt_assert(errcode == MSG_TIMEOUT, "Woops, this did not timeout as expected... Please report that bug.");
55 MSG_task_destroy(ptask);
57 XBT_INFO("Then, build a parallel task involving only computations and no communication (1 Gflop per node)");
58 computation_amounts = xbt_new0(double, hosts_count);
59 for (int i = 0; i < hosts_count; i++)
60 computation_amounts[i] = 1e9; // 1 Gflop
61 ptask = MSG_parallel_task_create("parallel exec", hosts_count, hosts, computation_amounts, NULL/* no comm */, NULL);
62 MSG_parallel_task_execute(ptask);
63 MSG_task_destroy(ptask);
65 XBT_INFO("Then, build a parallel task with no computation nor communication (synchro only)");
66 computation_amounts = xbt_new0(double, hosts_count);
67 communication_amounts = xbt_new0(double, hosts_count * hosts_count); /* memset to 0 by xbt_new0 */
68 ptask = MSG_parallel_task_create("parallel sync", hosts_count, hosts, computation_amounts, communication_amounts, NULL);
69 MSG_parallel_task_execute(ptask);
70 MSG_task_destroy(ptask);
72 XBT_INFO("Finally, trick the ptask to do a 'remote execution', on host %s", MSG_host_get_name(hosts[1]));
73 computation_amounts = xbt_new0(double, 1);
74 computation_amounts[0] = 1e9; // 1 Gflop
75 msg_host_t *remote = xbt_new(msg_host_t,1);
77 ptask = MSG_parallel_task_create("remote exec", 1, remote, computation_amounts, NULL/* no comm */, NULL);
78 MSG_parallel_task_execute(ptask);
79 MSG_task_destroy(ptask);
82 XBT_INFO("Goodbye now!");
87 int main(int argc, char *argv[])
89 MSG_init(&argc, argv);
90 MSG_config("host/model", "ptask_L07");
92 xbt_assert(argc <= 3, "1Usage: %s <platform file> [--energy]", argv[0]);
93 xbt_assert(argc >= 2, "2Usage: %s <platform file> [--energy]", argv[0]);
95 if(argc == 3 && argv[2][2] == 'e')
96 sg_host_energy_plugin_init();
98 MSG_create_environment(argv[1]);
100 /* Pick a process, no matter which, from the platform file */
101 xbt_dynar_t all_hosts = MSG_hosts_as_dynar();
102 msg_host_t first_host = xbt_dynar_getfirst_as(all_hosts,msg_host_t);
103 xbt_dynar_free(&all_hosts);
105 MSG_process_create("test", runner, NULL, first_host);
106 msg_error_t res = MSG_main();
107 XBT_INFO("Simulation done.");
109 return res != MSG_OK;