Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d60901c2ef03b65b6626452f5b7e56607b539617
[simgrid.git] / examples / msg / actions / actions.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2009. The SimGrid team. All rights reserved.               */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include "msg/msg.h"            /* Yeah! If you want to use msg, you need to include msg/msg.h */
11 #include "xbt.h"                /* calloc, printf */
12
13
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
15                  "Messages specific for this msg example");
16
17
18 /* Helper function */
19 static double parse_double(const char *string) {
20   double value;
21   char *endptr;
22
23   value=strtod(string, &endptr);
24   if (*endptr != '\0')
25           THROW1(unknown_error, 0, "%s is not a double", string);
26   return value;
27 }
28
29 /* My actions */
30 static void send(xbt_dynar_t action)
31 {
32   char *name = xbt_str_join(action, " ");
33   char *to = xbt_dynar_get_as(action, 2, char *);
34   char *size = xbt_dynar_get_as(action, 3, char *);
35   INFO2("Send: %s (size: %lg)", name, parse_double(size));
36   MSG_task_send(MSG_task_create(name, 0, parse_double(size), NULL), to);
37   INFO1("Sent %s", name);
38   free(name);
39 }
40
41 static void recv(xbt_dynar_t action)
42 {
43   char *name = xbt_str_join(action, " ");
44   m_task_t task = NULL;
45   INFO1("Receiving: %s", name);
46   //FIXME: argument of action ignored so far; semantic not clear
47   //char *from=xbt_dynar_get_as(action,2,char*);
48   MSG_task_receive(&task, MSG_process_get_name(MSG_process_self()));
49   INFO1("Received %s", MSG_task_get_name(task));
50   MSG_task_destroy(task);
51   free(name);
52 }
53
54 static int spawned_recv(int argc, char *argv[])
55 {
56   m_task_t task = NULL;
57   char* name = (char *) MSG_process_get_data(MSG_process_self());
58   INFO1("Receiving on %s", name);
59   MSG_task_receive(&task, name);
60   INFO1("Received %s", MSG_task_get_name(task));
61   MSG_task_send(MSG_task_create("waiter",0,0,NULL),MSG_process_self()->name); 
62   
63   MSG_task_destroy(task);
64   return 0;
65 }
66
67 static void Irecv(xbt_dynar_t action)
68 {
69   char *name = xbt_str_join(action, " ");
70   m_process_t comm_helper;
71
72   INFO1("Irecv on %s: spawn process ", 
73          MSG_process_get_name(MSG_process_self()));
74
75   sprintf(name,"%s_wait",MSG_process_self()->name);
76   comm_helper = MSG_process_create(name,spawned_recv,
77                  (void *) MSG_process_get_name(MSG_process_self()),
78                  MSG_host_self());
79
80
81   free(name);
82 }
83
84 static void wait(xbt_dynar_t action)
85 {
86   char *name = xbt_str_join(action, " ");
87   char task_name[80];
88   m_task_t task = NULL;
89   
90   INFO1("wait: %s", name);
91   sprintf(task_name,"%s_wait",MSG_process_self()->name);
92   MSG_task_receive(&task,task_name);
93   INFO1("waited: %s", name);
94   free(name);
95 }
96
97 static void sleep(xbt_dynar_t action)
98 {
99   char *name = xbt_str_join(action, " ");
100   char *duration = xbt_dynar_get_as(action, 2, char *);
101   INFO1("sleep: %s", name);
102   MSG_process_sleep(parse_double(duration));
103   INFO1("sleept: %s", name);
104   free(name);
105 }
106
107 static void compute(xbt_dynar_t action)
108 {
109   char *name = xbt_str_join(action, " ");
110   char *amout = xbt_dynar_get_as(action, 2, char *);
111   m_task_t task = MSG_task_create(name, parse_double(amout), 0, NULL);
112   INFO1("computing: %s", name);
113   MSG_task_execute(task);
114   MSG_task_destroy(task);
115   INFO1("computed: %s", name);
116   free(name);
117 }
118
119 /** Main function */
120 int main(int argc, char *argv[])
121 {
122   MSG_error_t res = MSG_OK;
123
124   /* Check the given arguments */
125   MSG_global_init(&argc, argv);
126   if (argc < 4) {
127     printf("Usage: %s platform_file deployment_file action_files\n", argv[0]);
128     printf("example: %s msg_platform.xml msg_deployment.xml actions\n",
129            argv[0]);
130     exit(1);
131   }
132
133   /*  Simulation setting */
134   MSG_create_environment(argv[1]);
135
136   /* No need to register functions as in classical MSG programs: the actions get started anyway */
137   MSG_launch_application(argv[2]);
138
139   /*   Action registration */
140   MSG_action_register("send", send);
141   MSG_action_register("recv", recv);
142   MSG_action_register("Irecv", Irecv);
143   MSG_action_register("wait", wait);
144   MSG_action_register("sleep", sleep);
145   MSG_action_register("compute", compute);
146
147   /* Actually do the simulation using MSG_action_trace_run */
148   res = MSG_action_trace_run(argv[3]);
149
150   INFO1("Simulation time %g", MSG_get_clock());
151   MSG_clean();
152
153   if (res == MSG_OK)
154     return 0;
155   else
156     return 1;
157 }                               /* end_of_main */