Logo AND Algorithmique Numérique Distribuée

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