Logo AND Algorithmique Numérique Distribuée

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