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 XBT_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 xbt_datadesc_type_t matrix_type, request_type;
21 matrix_type = xbt_datadesc_matrix(xbt_datadesc_by_name("double"),
24 xbt_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 xbt_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[])
58 xbt_socket_t sock = NULL;
61 /* 1. Init the GRAS infrastructure */
62 gras_init(&argc, argv);
64 /* 2. Get the port I should listen on from the command line, if specified */
69 /* 3. Create my master socket */
70 XBT_INFO("Launch server (port=%d)", port);
72 sock = gras_socket_server(port);
75 RETHROWF("Unable to establish a server socket: %s");
78 /* 4. Register the known messages and payloads. */
79 mmrpc_register_messages();
81 /* 5. Register my callback */
82 gras_cb_register("request", &server_cb_request_handler);
84 /* 6. Wait up to 10 minutes for an incoming message to handle */
85 gras_msg_handle(600.0);
87 /* 7. Free the allocated resources, and shut GRAS down */
88 gras_socket_close(sock);
96 int client(int argc, char *argv[])
98 xbt_socket_t toserver = NULL; /* peer */
101 xbt_matrix_t request[2], answer;
105 const char *host = "127.0.0.1";
108 /* 1. Init the GRAS's infrastructure */
109 gras_init(&argc, argv);
111 /* 2. Get the server's address. The command line override defaults when specified */
114 port = atoi(argv[2]);
117 XBT_INFO("Launch client (server on %s:%d)", host, port);
119 /* 3. Wait for the server startup */
122 /* 4. Create a socket to speak to the server */
124 toserver = gras_socket_client(host, port);
127 RETHROWF("Unable to connect to the server: %s");
129 XBT_INFO("Connected to %s:%d.", host, port);
132 /* 5. Register the messages (before use) */
133 mmrpc_register_messages();
135 /* 6. Keep the user informed of what's going on */
136 XBT_INFO(">>>>>>>> Connected to server which is on %s:%d <<<<<<<<",
137 xbt_socket_peer_name(toserver), xbt_socket_peer_port(toserver));
139 /* 7. Prepare and send the request to the server */
141 request[0] = xbt_matrix_double_new_id(MATSIZE, MATSIZE);
142 request[1] = xbt_matrix_double_new_rand(MATSIZE, MATSIZE);
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);
149 gras_msg_send(toserver, "request", &request);
151 xbt_matrix_free(request[0]);
153 XBT_INFO(">>>>>>>> Request sent to %s:%d <<<<<<<<",
154 xbt_socket_peer_name(toserver), xbt_socket_peer_port(toserver));
156 /* 8. Wait for the answer from the server, and deal with issues */
157 gras_msg_wait(6000, "answer", &from, &answer);
160 xbt_matrix_dump(answer,"C:answer",0,xbt_matrix_dump_display_double);
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));
170 /* 9. Keep the user informed of what's going on, again */
171 XBT_INFO(">>>>>>>> Got answer from %s:%d (values are right) <<<<<<<<",
172 xbt_socket_peer_name(from), xbt_socket_peer_port(from));
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);
181 } /* end_of_client */