3 /* GridRPC - Fake Grid RPC thingy doing matrix multiplications (as expected)*/
5 /* Copyright (c) 2005 Martin Quinson. All rights reserved. */
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. */
10 #define GRAS_DEFINE_TYPE_EXTERN
13 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(MatMult);
16 static int server_cb_request_handler(gras_msg_cb_ctx_t ctx, void *payload_data) {
17 gras_socket_t expeditor=gras_msg_cb_ctx_from(ctx);
19 /* 1. Get the payload into the data variable */
20 matrix_t *data=(matrix_t*)payload_data;
24 /* 2. Make some room to return the result */
25 result.lines = data[0].lines;
26 result.rows = data[1].rows;
27 result.ctn = xbt_malloc0(sizeof(double) * result.lines * result.rows);
29 /* 3. Do the computation */
30 for (i=0; i<result.lines; i++)
31 for (j=0; j<result.rows; j++)
32 for (k=0; k<data[1].lines; k++)
33 result.ctn[i*result.rows + j] += data[0].ctn[i*result.rows +k] *data[1].ctn[k*result.rows +j];
35 /* 4. Send it back as payload of a pong message to the expeditor */
36 gras_msg_send(expeditor, gras_msgtype_by_name("answer"), &result);
42 gras_socket_close(expeditor);
45 } /* end_of_server_cb_request_handler */
47 int server (int argc,char *argv[]) {
49 gras_socket_t sock=NULL;
52 /* 1. Init the GRAS infrastructure */
53 gras_init(&argc,argv);
55 /* 2. Get the port I should listen on from the command line, if specified */
60 /* 3. Create my master socket */
61 INFO1("Launch server (port=%d)", port);
63 sock = gras_socket_server(port);
65 RETHROW0("Unable to establish a server socket: %s");
68 /* 4. Register the known messages and payloads. */
69 mmrpc_register_messages();
71 /* 5. Register my callback */
72 gras_cb_register(gras_msgtype_by_name("request"),&server_cb_request_handler);
74 /* 6. Wait up to 10 minutes for an incomming message to handle */
75 gras_msg_handle(600.0);
77 /* 7. Free the allocated resources, and shut GRAS down */
78 gras_socket_close(sock);