1 /* GridRPC - Fake Grid RPC thingy doing matrix multiplications (as expected)*/
3 /* Copyright (c) 2005, 2006, 2007, 2010. The SimGrid Team.
4 * All rights reserved. */
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. */
9 #define GRAS_DEFINE_TYPE_EXTERN
10 #include "xbt/matrix.h"
13 XBT_LOG_NEW_DEFAULT_CATEGORY(MatMult, "Messages specific to this example");
15 /* register messages which may be sent and their payload
16 (common to client and server) */
17 void mmrpc_register_messages(void)
19 gras_datadesc_type_t matrix_type, request_type;
21 matrix_type = gras_datadesc_matrix(gras_datadesc_by_name("double"),
24 gras_datadesc_array_fixed("s_matrix_t(double)[2]", matrix_type, 2);
26 gras_msgtype_declare("answer", matrix_type);
27 gras_msgtype_declare("request", request_type);
30 typedef xbt_matrix_t request_t[2];
31 static int server_cb_request_handler(gras_msg_cb_ctx_t ctx,
35 gras_socket_t expeditor = gras_msg_cb_ctx_from(ctx);
37 /* 1. Get the payload into the data variable */
38 xbt_matrix_t *request = (xbt_matrix_t *) payload_data;
41 /* 2. Do the computation */
42 result = xbt_matrix_double_new_mult(request[0], request[1]);
44 /* 3. Send it back as payload of a pong message to the expeditor */
45 gras_msg_send(expeditor, "answer", &result);
48 xbt_matrix_free(request[0]);
49 xbt_matrix_free(request[1]);
50 xbt_matrix_free(result);
51 gras_socket_close(expeditor);
54 } /* end_of_server_cb_request_handler */
56 int server(int argc, char *argv[])
59 gras_socket_t sock = NULL;
62 /* 1. Init the GRAS infrastructure */
63 gras_init(&argc, argv);
65 /* 2. Get the port I should listen on from the command line, if specified */
70 /* 3. Create my master socket */
71 INFO1("Launch server (port=%d)", port);
73 sock = gras_socket_server(port);
76 RETHROW0("Unable to establish a server socket: %s");
79 /* 4. Register the known messages and payloads. */
80 mmrpc_register_messages();
82 /* 5. Register my callback */
83 gras_cb_register("request", &server_cb_request_handler);
85 /* 6. Wait up to 10 minutes for an incomming message to handle */
86 gras_msg_handle(600.0);
88 /* 7. Free the allocated resources, and shut GRAS down */
89 gras_socket_close(sock);
97 int client(int argc, char *argv[])
100 gras_socket_t toserver = NULL; /* peer */
103 xbt_matrix_t request[2], answer;
107 const char *host = "127.0.0.1";
110 /* 1. Init the GRAS's infrastructure */
111 gras_init(&argc, argv);
113 /* 2. Get the server's address. The command line override defaults when specified */
116 port = atoi(argv[2]);
119 INFO2("Launch client (server on %s:%d)", host, port);
121 /* 3. Wait for the server startup */
124 /* 4. Create a socket to speak to the server */
126 toserver = gras_socket_client(host, port);
129 RETHROW0("Unable to connect to the server: %s");
131 INFO2("Connected to %s:%d.", host, port);
134 /* 5. Register the messages (before use) */
135 mmrpc_register_messages();
137 /* 6. Keep the user informed of what's going on */
138 INFO2(">>>>>>>> Connected to server which is on %s:%d <<<<<<<<",
139 gras_socket_peer_name(toserver), gras_socket_peer_port(toserver));
141 /* 7. Prepare and send the request to the server */
143 request[0] = xbt_matrix_double_new_id(MATSIZE, MATSIZE);
144 request[1] = xbt_matrix_double_new_rand(MATSIZE, MATSIZE);
147 xbt_matrix_dump(request[0],"C:sent0",0,xbt_matrix_dump_display_double);
148 xbt_matrix_dump(request[1],"C:sent1",0,xbt_matrix_dump_display_double);
151 gras_msg_send(toserver, "request", &request);
153 xbt_matrix_free(request[0]);
155 INFO2(">>>>>>>> Request sent to %s:%d <<<<<<<<",
156 gras_socket_peer_name(toserver), gras_socket_peer_port(toserver));
158 /* 8. Wait for the answer from the server, and deal with issues */
159 gras_msg_wait(6000, "answer", &from, &answer);
162 xbt_matrix_dump(answer,"C:answer",0,xbt_matrix_dump_display_double);
164 for (i = 0; i < MATSIZE; i++)
165 for (j = 0; i < MATSIZE; i++)
166 xbt_assert4(xbt_matrix_get_as(answer, i, j, double) ==
167 xbt_matrix_get_as(request[1], i, j, double),
168 "Answer does not match expectations. Found %f at cell %d,%d instead of %f",
169 xbt_matrix_get_as(answer, i, j, double), i, j,
170 xbt_matrix_get_as(request[1], i, j, double));
172 /* 9. Keep the user informed of what's going on, again */
173 INFO2(">>>>>>>> Got answer from %s:%d (values are right) <<<<<<<<",
174 gras_socket_peer_name(from), gras_socket_peer_port(from));
176 /* 10. Free the allocated resources, and shut GRAS down */
177 xbt_matrix_free(request[1]);
178 xbt_matrix_free(answer);
179 gras_socket_close(toserver);
183 } /* end_of_client */