Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cosmetics
[simgrid.git] / examples / gras / mmrpc / mmrpc_server.c
1 /* $Id$ */
2
3 /* GridRPC - Fake Grid RPC thingy doing matrix multiplications (as expected)*/
4
5 /* Copyright (c) 2005 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 #define GRAS_DEFINE_TYPE_EXTERN
11 #include "mmrpc.h"
12
13 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(MatMult);
14
15
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);
18                              
19   /* 1. Get the payload into the data variable */
20   matrix_t *data=(matrix_t*)payload_data;
21   matrix_t result;
22   int i,j,k;
23    
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);
28
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];
34
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);
37
38   /* 5. Cleanups */
39   free(data[0].ctn); 
40   free(data[1].ctn);
41   free(result.ctn);
42   gras_socket_close(expeditor);
43    
44   return 1;
45 } /* end_of_server_cb_request_handler */
46
47 int server (int argc,char *argv[]) {
48   xbt_ex_t e; 
49   gras_socket_t sock=NULL;
50   int port = 4000;
51   
52   /* 1. Init the GRAS infrastructure */
53   gras_init(&argc,argv);
54    
55   /* 2. Get the port I should listen on from the command line, if specified */
56   if (argc == 2) {
57     port=atoi(argv[1]);
58   }
59
60   /* 3. Create my master socket */
61   INFO1("Launch server (port=%d)", port);
62   TRY {
63     sock = gras_socket_server(port);
64   } CATCH(e) {
65     RETHROW0("Unable to establish a server socket: %s");
66   }
67
68   /* 4. Register the known messages and payloads. */
69   mmrpc_register_messages();
70    
71   /* 5. Register my callback */
72   gras_cb_register(gras_msgtype_by_name("request"),&server_cb_request_handler);
73
74   /* 6. Wait up to 10 minutes for an incomming message to handle */
75   gras_msg_handle(600.0);
76    
77   /* 7. Free the allocated resources, and shut GRAS down */
78   gras_socket_close(sock);
79   gras_exit();
80    
81   INFO0("Done.");
82   return 0;
83 } /* end_of_server */