Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
prevent tasks to share resources when they don't have to.
[simgrid.git] / examples / msg / parallel_task / parallel_task.c
1 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <stdio.h>
8 #include "msg/msg.h"            /* Yeah! If you want to use msg, you need to include msg/msg.h */
9 #include "xbt/sysdep.h"         /* calloc, printf */
10
11 /* Create a log channel to have nice outputs. */
12 #include "xbt/log.h"
13 #include "xbt/asserts.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
15                              "Messages specific for this msg example");
16
17 int test(int argc, char *argv[]);
18 MSG_error_t test_all(const char *platform_file);
19
20 /** Emitter function  */
21 int test(int argc, char *argv[])
22 {
23   xbt_dynar_t slaves_dynar;
24   int slaves_count = 0;
25   m_host_t *slaves = NULL;
26   double task_comp_size = 100000;
27   double task_comm_size = 10000;
28   double *computation_amount = NULL;
29   double *communication_amount = NULL;
30   m_task_t ptask = NULL;
31   int i, j;
32
33   slaves_dynar = MSG_hosts_as_dynar();
34   slaves_count = xbt_dynar_length(slaves_dynar);
35   slaves = xbt_dynar_to_array(slaves_dynar);
36   xbt_dynar_free(&slaves_dynar);
37
38   computation_amount = xbt_new0(double, slaves_count);
39   communication_amount = xbt_new0(double, slaves_count * slaves_count);
40
41   for (i = 0; i < slaves_count; i++)
42     computation_amount[i] = task_comp_size;
43
44   for (i = 0; i < slaves_count; i++)
45     for (j = i + 1; j < slaves_count; j++)
46       communication_amount[i * slaves_count + j] = task_comm_size;
47
48   ptask = MSG_parallel_task_create("parallel task",
49                                    slaves_count, slaves,
50                                    computation_amount,
51                                    communication_amount, NULL);
52   MSG_parallel_task_execute(ptask);
53
54   MSG_task_destroy(ptask);
55   /* There is no need to free that! */
56 /*   free(communication_amount); */
57 /*   free(computation_amount); */
58
59   XBT_INFO("Goodbye now!");
60   free(slaves);
61   return 0;
62 }
63
64 /** Test function */
65 MSG_error_t test_all(const char *platform_file)
66 {
67   MSG_error_t res = MSG_OK;
68   xbt_dynar_t all_hosts;
69   m_host_t first_host;
70
71   MSG_config("workstation/model", "ptask_L07");
72   MSG_create_environment(platform_file);
73
74   all_hosts = MSG_hosts_as_dynar();
75   first_host = xbt_dynar_pop_as(all_hosts,m_host_t);
76   MSG_process_create("test", test, NULL, first_host);
77   res = MSG_main();
78   xbt_dynar_free(&all_hosts);
79
80   XBT_INFO("Simulation time %g", MSG_get_clock());
81   return res;
82 }
83
84 int main(int argc, char *argv[])
85 {
86   MSG_error_t res = MSG_OK;
87
88   MSG_global_init(&argc, argv);
89   if (argc < 2) {
90     printf("Usage: %s platform_file\n", argv[0]);
91     printf("example: %s msg_platform.xml\n", argv[0]);
92     exit(1);
93   }
94   res = test_all(argv[1]);
95   MSG_clean();
96
97   if (res == MSG_OK)
98     return 0;
99   else
100     return 1;
101 }