Logo AND Algorithmique Numérique Distribuée

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