Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[trace] when destroying a container, remove its references from allContainers
[simgrid.git] / examples / msg / tracing / tasks.c
1 /* Copyright (c) 2002,2003,2004 Arnaud Legrand. All rights reserved.        */
2
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. */
5
6 #include <stdio.h>
7 #include "msg/msg.h"
8 #include "xbt/sysdep.h"         /* calloc, printf */
9
10 /* Create a log channel to have nice outputs. */
11 #include "xbt/log.h"
12 #include "xbt/asserts.h"
13 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
14                              "Messages specific for this msg example");
15
16 /** Emitter function  */
17 int master(int argc, char *argv[])
18 {
19   long number_of_tasks = atol(argv[1]);
20   double task_comp_size = atof(argv[2]);
21   double task_comm_size = atof(argv[3]);
22   long slaves_count = atol(argv[4]);
23   XBT_INFO("master %ld %f %f %ld", number_of_tasks, task_comp_size,
24         task_comm_size, slaves_count);
25
26   int i;
27   for (i = 0; i < number_of_tasks; i++) {
28     char task_name[100];
29     snprintf (task_name, 100, "task-%d", i);
30     m_task_t task = NULL;
31     task = MSG_task_create(task_name, task_comp_size, task_comm_size, NULL);
32
33     //setting the category of task to "compute"
34     //the category of a task must be defined before it is sent or executed
35     TRACE_msg_set_task_category(task, "compute");
36     MSG_task_send(task, "master_mailbox");
37   }
38
39   for (i = 0; i < slaves_count; i++) {
40     char task_name[100];
41     snprintf (task_name, 100, "task-%d", i);
42     m_task_t finalize = MSG_task_create(task_name, 0, 0, xbt_strdup("finalize"));
43     TRACE_msg_set_task_category(finalize, "finalize");
44     MSG_task_send(finalize, "master_mailbox");
45   }
46
47   return 0;
48 }
49
50 /** Receiver function  */
51 int slave(int argc, char *argv[])
52 {
53   m_task_t task = NULL;
54   MSG_error_t res;
55
56   while (1) {
57     MSG_task_receive(&(task), "master_mailbox");
58
59     char *data = MSG_task_get_data(task);
60     if (data && !strcmp(data, "finalize")) {
61       free(data);
62       MSG_task_destroy(task);
63       break;
64     }
65
66     XBT_INFO("Executing task %f", MSG_task_get_compute_duration(task));
67     MSG_task_execute(task);
68     XBT_INFO("End of execution");
69     MSG_task_destroy(task);
70     task = NULL;
71   }
72   return 0;
73 }
74
75 /** Main function */
76 int main(int argc, char *argv[])
77 {
78   MSG_global_init(&argc, argv);
79   if (argc < 3) {
80     printf("Usage: %s platform_file deployment_file\n", argv[0]);
81     exit(1);
82   }
83
84   char *platform_file = argv[1];
85   char *deployment_file = argv[2];
86   MSG_create_environment(platform_file);
87
88   TRACE_category_with_color ("compute", "1 0 0");  //compute is red
89   TRACE_category_with_color ("finalize", "0 1 0"); //finalize is green
90
91   MSG_function_register("master", master);
92   MSG_function_register("slave", slave);
93   MSG_launch_application(deployment_file);
94
95   MSG_main();
96   MSG_clean();
97   return 0;
98 }