Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Really get the first host (fixes failing test).
[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
37   computation_amount = xbt_new0(double, slaves_count);
38   communication_amount = xbt_new0(double, slaves_count * slaves_count);
39
40   for (i = 0; i < slaves_count; i++)
41     computation_amount[i] = task_comp_size;
42
43   for (i = 0; i < slaves_count; i++)
44     for (j = i + 1; j < slaves_count; j++)
45       communication_amount[i * slaves_count + j] = task_comm_size;
46
47   ptask = MSG_parallel_task_create("parallel task",
48                                    slaves_count, slaves,
49                                    computation_amount,
50                                    communication_amount, NULL);
51   MSG_parallel_task_execute(ptask);
52
53   MSG_task_destroy(ptask);
54   /* There is no need to free that! */
55 /*   free(communication_amount); */
56 /*   free(computation_amount); */
57
58   XBT_INFO("Goodbye now!");
59   free(slaves);
60   return 0;
61 }
62
63 /** Test function */
64 MSG_error_t test_all(const char *platform_file)
65 {
66   MSG_error_t res = MSG_OK;
67   xbt_dynar_t all_hosts;
68   m_host_t first_host;
69
70   MSG_config("workstation/model", "ptask_L07");
71   MSG_create_environment(platform_file);
72
73   all_hosts = MSG_hosts_as_dynar();
74   first_host = xbt_dynar_getfirst_as(all_hosts,m_host_t);
75   MSG_process_create("test", test, NULL, first_host);
76   res = MSG_main();
77   xbt_dynar_free(&all_hosts);
78
79   XBT_INFO("Simulation time %g", MSG_get_clock());
80   return res;
81 }
82
83 int main(int argc, char *argv[])
84 {
85   MSG_error_t res = MSG_OK;
86
87   MSG_global_init(&argc, argv);
88   if (argc < 2) {
89     printf("Usage: %s platform_file\n", argv[0]);
90     printf("example: %s msg_platform.xml\n", argv[0]);
91     exit(1);
92   }
93   res = test_all(argv[1]);
94   MSG_clean();
95
96   if (res == MSG_OK)
97     return 0;
98   else
99     return 1;
100 }