Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
try to port the gras simulation side to the new smx_network infrastructure (not yet...
[simgrid.git] / src / gras / Transport / sg_transport.c
1 /* $Id$ */
2
3 /* sg_transport - SG specific functions for transport                       */
4
5 /* Copyright (c) 2004 Martin Quinson. All rights reserved.                  */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include "xbt/ex.h"
11 #include "gras/Transport/transport_private.h"
12 #include "gras/Virtu/virtu_sg.h"
13 #include "simix/private.h"
14
15 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(gras_trp);
16
17 /* check transport_private.h for an explanation of this variable; this just need to be defined to NULL in SG */
18 gras_socket_t _gras_lastly_selected_socket = NULL;
19
20 /**     
21  * gras_trp_select:
22  *
23  * Returns the next socket to service having a message awaiting.
24  *
25  * if timeout<0, we ought to implement the adaptative timeout (FIXME)
26  *
27  * if timeout=0, do not wait for new message, only handle the ones already there.
28  *
29  * if timeout>0 and no message there, wait at most that amount of time before giving up.
30  */
31 gras_socket_t gras_trp_select(double timeout) {
32   static int warned=0;
33   if (timeout>=0 && !warned) {
34     warned=1;
35     WARN0("Timed select not implemented in SG. Switching to blocking select");
36   }
37   gras_trp_procdata_t pd =
38     (gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id);
39
40   gras_socket_t sock_iter, active_socket;
41   gras_trp_sg_sock_data_t *active_socket_data;
42   smx_comm_t comm;
43   unsigned int cursor;
44
45
46   /* FIXME: make sure that the ongoing comm is canceled&destroyed when the corresponding socket is closed */
47   xbt_assert(xbt_dynar_length(pd->sockets)==xbt_dynar_length(pd->comms));
48
49   /* Wait for the first terminating comm object */
50   int rank = SIMIX_network_waitany(pd->comms);
51
52   /* Don't wait on this socket until the comm object is recreated by gras_msg_recv */
53   comm = NULL;
54   xbt_dynar_set(pd->comms,rank,&comm);
55
56   /* Ok, got something. Open a socket back to the expeditor */
57   active_socket = xbt_dynar_get_as(pd->sockets,rank,gras_socket_t);
58   active_socket_data = (gras_trp_sg_sock_data_t *) active_socket->data;
59
60   /* Try to reuse an already opened socket to that expeditor */
61   DEBUG1("Open sockets size %lu", xbt_dynar_length(pd->sockets));
62   xbt_dynar_foreach(pd->sockets, cursor, sock_iter) {
63     gras_trp_sg_sock_data_t *sock_data;
64     DEBUG1("Consider %p as outgoing socket to expeditor", sock_iter);
65
66     if (sock_iter->meas || !sock_iter->outgoing)
67       continue;
68     sock_data = ((gras_trp_sg_sock_data_t *) sock_iter->data);
69
70     if ((sock_data->to_socket == active_socket) &&
71         (sock_data->to_host ==
72          SIMIX_process_get_host(active_socket_data->from_process))) {
73       xbt_dynar_cursor_unlock(pd->sockets);
74       return sock_iter;
75     }
76   }
77
78   /* Socket to expeditor not created yet */
79   DEBUG0("Create a socket to the expeditor");
80
81
82
83   return sock_iter;
84
85 #if 0 /* KILLME */
86   gras_socket_t res;
87   gras_trp_procdata_t pd =
88     (gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id);
89   gras_trp_sg_sock_data_t *sockdata;
90   gras_trp_plugin_t trp;
91   gras_socket_t active_socket = NULL;
92   gras_trp_sg_sock_data_t *active_socket_data;
93   gras_socket_t sock_iter;      /* iterating over all sockets */
94   unsigned int cursor;
95
96   DEBUG3("select on %s@%s with timeout=%f",
97          SIMIX_process_get_name(SIMIX_process_self()),
98          SIMIX_host_get_name(SIMIX_host_self()), timeout);
99   if (timeout >= 0) {
100     xbt_queue_shift_timed(pd->msg_selectable_sockets,
101                           &active_socket, timeout);
102   } else {
103     xbt_queue_shift(pd->msg_selectable_sockets, &active_socket);
104   }
105
106   if (active_socket == NULL) {
107     DEBUG0("TIMEOUT");
108     THROW0(timeout_error, 0, "Timeout");
109   }
110   active_socket_data = (gras_trp_sg_sock_data_t *) active_socket->data;
111
112   /* Ok, got something. Open a socket back to the expeditor */
113
114   /* Try to reuse an already openned socket to that expeditor */
115   DEBUG1("Open sockets size %lu", xbt_dynar_length(pd->sockets));
116   xbt_dynar_foreach(pd->sockets, cursor, sock_iter) {
117     gras_trp_sg_sock_data_t *sock_data;
118     DEBUG1("Consider %p as outgoing socket to expeditor", sock_iter);
119
120     if (sock_iter->meas || !sock_iter->outgoing)
121       continue;
122     sock_data = ((gras_trp_sg_sock_data_t *) sock_iter->data);
123
124     if ((sock_data->to_socket == active_socket) &&
125         (sock_data->to_host ==
126          SIMIX_process_get_host(active_socket_data->from_process))) {
127       xbt_dynar_cursor_unlock(pd->sockets);
128       return sock_iter;
129     }
130   }
131
132   /* Socket to expeditor not created yet */
133   DEBUG0("Create a socket to the expeditor");
134
135   trp = gras_trp_plugin_get_by_name("sg");
136
137   gras_trp_socket_new(1, &res);
138   res->plugin = trp;
139
140   res->incoming = 1;
141   res->outgoing = 1;
142   res->is_master = 0;
143   res->sd = -1;
144
145   res->port = -1;
146
147   /* initialize the ports */
148   //res->peer_port = active_socket->port;
149   res->port = active_socket->peer_port;
150
151   /* create sockdata */
152   sockdata = xbt_new(gras_trp_sg_sock_data_t, 1);
153   sockdata->from_process = SIMIX_process_self();
154   sockdata->to_process = active_socket_data->from_process;
155
156   res->peer_port = ((gras_trp_procdata_t)
157                     gras_libdata_by_name_from_remote("gras_trp",
158                                                      sockdata->to_process))->
159     myport;
160   sockdata->to_socket = active_socket;
161   /*update the peer to_socket  variable */
162   active_socket_data->to_socket = res;
163   sockdata->cond = SIMIX_cond_init();
164   sockdata->mutex = SIMIX_mutex_init();
165
166   sockdata->to_host =
167     SIMIX_process_get_host(active_socket_data->from_process);
168
169   res->data = sockdata;
170   res->peer_name = strdup(SIMIX_host_get_name(sockdata->to_host));
171
172   gras_trp_buf_init_sock(res);
173
174   DEBUG4("Create socket to process:%s(Port %d) from process: %s(Port %d)",
175          SIMIX_process_get_name(sockdata->from_process),
176          res->peer_port,
177          SIMIX_process_get_name(sockdata->to_process), res->port);
178
179   return res;
180 #endif
181 }
182
183
184 /* dummy implementations of the functions used in RL mode */
185
186 void gras_trp_tcp_setup(gras_trp_plugin_t plug)
187 {
188   THROW0(mismatch_error, 0, NULL);
189 }
190
191 void gras_trp_file_setup(gras_trp_plugin_t plug)
192 {
193   THROW0(mismatch_error, 0, NULL);
194 }
195
196 void gras_trp_iov_setup(gras_trp_plugin_t plug)
197 {
198   THROW0(mismatch_error, 0, NULL);
199 }
200
201 gras_socket_t gras_trp_buf_init_sock(gras_socket_t sock)
202 {
203   return sock;
204 }