Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update timings for 32bits too.
[simgrid.git] / examples / gras / mmrpc / mmrpc.c
1 /* GridRPC - Fake Grid RPC thingy doing matrix multiplications (as expected)*/
2
3 /* Copyright (c) 2005, 2006, 2007, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #define GRAS_DEFINE_TYPE_EXTERN
10 #include "xbt/matrix.h"
11 #include "mmrpc.h"
12
13 XBT_LOG_NEW_DEFAULT_CATEGORY(MatMult, "Messages specific to this example");
14
15 /* register messages which may be sent and their payload
16    (common to client and server) */
17 void mmrpc_register_messages(void)
18 {
19   gras_datadesc_type_t matrix_type, request_type;
20
21   matrix_type = gras_datadesc_matrix(gras_datadesc_by_name("double"),
22                                      NULL);
23   request_type =
24       gras_datadesc_array_fixed("s_matrix_t(double)[2]", matrix_type, 2);
25
26   gras_msgtype_declare("answer", matrix_type);
27   gras_msgtype_declare("request", request_type);
28 }
29
30 typedef xbt_matrix_t request_t[2];
31 static int server_cb_request_handler(gras_msg_cb_ctx_t ctx,
32                                      void *payload_data)
33 {
34
35   gras_socket_t expeditor = gras_msg_cb_ctx_from(ctx);
36
37   /* 1. Get the payload into the data variable */
38   xbt_matrix_t *request = (xbt_matrix_t *) payload_data;
39   xbt_matrix_t result;
40
41   /* 2. Do the computation */
42   result = xbt_matrix_double_new_mult(request[0], request[1]);
43
44   /* 3. Send it back as payload of a pong message to the expeditor */
45   gras_msg_send(expeditor, "answer", &result);
46
47   /* 4. Cleanups */
48   xbt_matrix_free(request[0]);
49   xbt_matrix_free(request[1]);
50   xbt_matrix_free(result);
51   gras_socket_close(expeditor);
52
53   return 0;
54 }                               /* end_of_server_cb_request_handler */
55
56 int server(int argc, char *argv[])
57 {
58   gras_socket_t sock = NULL;
59   int port = 4000;
60
61   /* 1. Init the GRAS infrastructure */
62   gras_init(&argc, argv);
63
64   /* 2. Get the port I should listen on from the command line, if specified */
65   if (argc == 2) {
66     port = atoi(argv[1]);
67   }
68
69   /* 3. Create my master socket */
70   XBT_INFO("Launch server (port=%d)", port);
71   TRY {
72     sock = gras_socket_server(port);
73   }
74   CATCH_ANONYMOUS {
75     RETHROWF("Unable to establish a server socket: %s");
76   }
77
78   /* 4. Register the known messages and payloads. */
79   mmrpc_register_messages();
80
81   /* 5. Register my callback */
82   gras_cb_register("request", &server_cb_request_handler);
83
84   /* 6. Wait up to 10 minutes for an incomming message to handle */
85   gras_msg_handle(600.0);
86
87   /* 7. Free the allocated resources, and shut GRAS down */
88   gras_socket_close(sock);
89   gras_exit();
90
91   XBT_INFO("Done.");
92   return 0;
93 }                               /* end_of_server */
94
95
96 int client(int argc, char *argv[])
97 {
98   gras_socket_t toserver = NULL;        /* peer */
99
100   gras_socket_t from;
101   xbt_matrix_t request[2], answer;
102
103   int i, j;
104
105   const char *host = "127.0.0.1";
106   int port = 4000;
107
108   /* 1. Init the GRAS's infrastructure */
109   gras_init(&argc, argv);
110
111   /* 2. Get the server's address. The command line override defaults when specified */
112   if (argc == 3) {
113     host = argv[1];
114     port = atoi(argv[2]);
115   }
116
117   XBT_INFO("Launch client (server on %s:%d)", host, port);
118
119   /* 3. Wait for the server startup */
120   gras_os_sleep(1);
121
122   /* 4. Create a socket to speak to the server */
123   TRY {
124     toserver = gras_socket_client(host, port);
125   }
126   CATCH_ANONYMOUS {
127     RETHROWF("Unable to connect to the server: %s");
128   }
129   XBT_INFO("Connected to %s:%d.", host, port);
130
131
132   /* 5. Register the messages (before use) */
133   mmrpc_register_messages();
134
135   /* 6. Keep the user informed of what's going on */
136   XBT_INFO(">>>>>>>> Connected to server which is on %s:%d <<<<<<<<",
137         gras_socket_peer_name(toserver), gras_socket_peer_port(toserver));
138
139   /* 7. Prepare and send the request to the server */
140
141   request[0] = xbt_matrix_double_new_id(MATSIZE, MATSIZE);
142   request[1] = xbt_matrix_double_new_rand(MATSIZE, MATSIZE);
143
144   /*
145      xbt_matrix_dump(request[0],"C:sent0",0,xbt_matrix_dump_display_double);
146      xbt_matrix_dump(request[1],"C:sent1",0,xbt_matrix_dump_display_double);
147    */
148
149   gras_msg_send(toserver, "request", &request);
150
151   xbt_matrix_free(request[0]);
152
153   XBT_INFO(">>>>>>>> Request sent to %s:%d <<<<<<<<",
154         gras_socket_peer_name(toserver), gras_socket_peer_port(toserver));
155
156   /* 8. Wait for the answer from the server, and deal with issues */
157   gras_msg_wait(6000, "answer", &from, &answer);
158
159   /*
160      xbt_matrix_dump(answer,"C:answer",0,xbt_matrix_dump_display_double);
161    */
162   for (i = 0; i < MATSIZE; i++)
163     for (j = 0; i < MATSIZE; i++)
164       xbt_assert(xbt_matrix_get_as(answer, i, j, double) ==
165                   xbt_matrix_get_as(request[1], i, j, double),
166                   "Answer does not match expectations. Found %f at cell %d,%d instead of %f",
167                   xbt_matrix_get_as(answer, i, j, double), i, j,
168                   xbt_matrix_get_as(request[1], i, j, double));
169
170   /* 9. Keep the user informed of what's going on, again */
171   XBT_INFO(">>>>>>>> Got answer from %s:%d (values are right) <<<<<<<<",
172         gras_socket_peer_name(from), gras_socket_peer_port(from));
173
174   /* 10. Free the allocated resources, and shut GRAS down */
175   xbt_matrix_free(request[1]);
176   xbt_matrix_free(answer);
177   gras_socket_close(toserver);
178   gras_exit();
179   XBT_INFO("Done.");
180   return 0;
181 }                               /* end_of_client */