Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add an Isend, just the prototype for the barrier action
[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
30 /* My actions */
31 static void send(xbt_dynar_t action)
32 {
33   char *name = xbt_str_join(action, " ");
34   char *to = xbt_dynar_get_as(action, 2, char *);
35   char *size = xbt_dynar_get_as(action, 3, char *);
36   INFO2("Send: %s (size: %lg)", name, parse_double(size));
37   MSG_task_send(MSG_task_create(name, 0, parse_double(size), NULL), to);
38   INFO1("Sent %s", name);
39   free(name);
40 }
41
42 static int spawned_send(int argc, char *argv[])
43 {
44   xbt_dynar_t action= (xbt_dynar_t) MSG_process_get_data(MSG_process_self());
45   char *name = xbt_str_join(action, " ");
46   char *to = xbt_dynar_get_as(action, 2, char *);
47   char *size = xbt_dynar_get_as(action, 3, char *);
48   
49   INFO1("Sending on %s", name);
50   MSG_task_send(MSG_task_create(name, 0, parse_double(size), NULL), to);
51   INFO1("Sent %s", name);
52   free(name);
53   return 0;
54 }
55
56 static void Isend(xbt_dynar_t action)
57 {
58   char *name = xbt_str_join(action, " ");
59   m_process_t comm_helper;
60
61   INFO1("Isend on %s: spawn process ", 
62          MSG_process_get_name(MSG_process_self()));
63
64   sprintf(name,"%s_wait",MSG_process_self()->name);
65   comm_helper = MSG_process_create(name,spawned_send,
66                                    (void *) action,
67                                    MSG_host_self());
68   free(name);
69 }
70
71
72 static void recv(xbt_dynar_t action)
73 {
74   char *name = xbt_str_join(action, " ");
75   m_task_t task = NULL;
76   INFO1("Receiving: %s", name);
77   //FIXME: argument of action ignored so far; semantic not clear
78   //char *from=xbt_dynar_get_as(action,2,char*);
79   MSG_task_receive(&task, MSG_process_get_name(MSG_process_self()));
80   INFO1("Received %s", MSG_task_get_name(task));
81   MSG_task_destroy(task);
82   free(name);
83 }
84
85 static int spawned_recv(int argc, char *argv[])
86 {
87   m_task_t task = NULL;
88   char* name = (char *) MSG_process_get_data(MSG_process_self());
89   INFO1("Receiving on %s", name);
90   MSG_task_receive(&task, name);
91   INFO1("Received %s", MSG_task_get_name(task));
92   MSG_task_send(MSG_task_create("waiter",0,0,NULL),MSG_process_self()->name); 
93   
94   MSG_task_destroy(task);
95   return 0;
96 }
97
98
99 static void Irecv(xbt_dynar_t action)
100 {
101   char *name = xbt_str_join(action, " ");
102   m_process_t comm_helper;
103
104   INFO1("Irecv on %s: spawn process ", 
105          MSG_process_get_name(MSG_process_self()));
106
107   sprintf(name,"%s_wait",MSG_process_self()->name);
108   comm_helper = MSG_process_create(name,spawned_recv,
109                  (void *) MSG_process_get_name(MSG_process_self()),
110                  MSG_host_self());
111
112
113   free(name);
114 }
115
116
117 static void wait(xbt_dynar_t action)
118 {
119   char *name = xbt_str_join(action, " ");
120   char task_name[80];
121   m_task_t task = NULL;
122   
123   INFO1("wait: %s", name);
124   sprintf(task_name,"%s_wait",MSG_process_self()->name);
125   MSG_task_receive(&task,task_name);
126   INFO1("waited: %s", name);
127   free(name);
128 }
129
130 static void barrier (xbt_dynar_t action)
131 {
132   char *name = xbt_str_join(action, " ");
133   INFO1("barrier: %s", name);
134   
135
136   free(name);
137
138 }
139 static void sleep(xbt_dynar_t action)
140 {
141   char *name = xbt_str_join(action, " ");
142   char *duration = xbt_dynar_get_as(action, 2, char *);
143   INFO1("sleep: %s", name);
144   MSG_process_sleep(parse_double(duration));
145   INFO1("sleept: %s", name);
146   free(name);
147 }
148
149 static void compute(xbt_dynar_t action)
150 {
151   char *name = xbt_str_join(action, " ");
152   char *amout = xbt_dynar_get_as(action, 2, char *);
153   m_task_t task = MSG_task_create(name, parse_double(amout), 0, NULL);
154   INFO1("computing: %s", name);
155   MSG_task_execute(task);
156   MSG_task_destroy(task);
157   INFO1("computed: %s", name);
158   free(name);
159 }
160
161 /** Main function */
162 int main(int argc, char *argv[])
163 {
164   MSG_error_t res = MSG_OK;
165
166   /* Check the given arguments */
167   MSG_global_init(&argc, argv);
168   if (argc < 4) {
169     printf("Usage: %s platform_file deployment_file action_files\n", argv[0]);
170     printf("example: %s msg_platform.xml msg_deployment.xml actions\n",
171            argv[0]);
172     exit(1);
173   }
174
175   /*  Simulation setting */
176   MSG_create_environment(argv[1]);
177
178   /* No need to register functions as in classical MSG programs: the actions get started anyway */
179   MSG_launch_application(argv[2]);
180
181   /*   Action registration */
182   MSG_action_register("send", send);
183   MSG_action_register("Isend", Isend);
184   MSG_action_register("recv", recv);
185   MSG_action_register("Irecv", Irecv);
186   MSG_action_register("wait", wait);
187   MSG_action_register("barrier", barrier);
188   MSG_action_register("sleep", sleep);
189   MSG_action_register("compute", compute);
190
191
192   /* Actually do the simulation using MSG_action_trace_run */
193   res = MSG_action_trace_run(argv[3]);
194
195   INFO1("Simulation time %g", MSG_get_clock());
196   xbt_dynar_free_container(&comm_world);
197   MSG_clean();
198
199   if (res == MSG_OK)
200     return 0;
201   else
202     return 1;
203 }                               /* end_of_main */