Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Let it compile properly
[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 #include "gras.h"
11
12 #define MATSIZE 128
13
14 XBT_LOG_NEW_DEFAULT_CATEGORY(MatMult,"Messages specific to this example");
15
16 GRAS_DEFINE_TYPE(s_matrix,
17 struct s_matrix {
18   int rows;
19   int cols;
20   double *ctn GRAS_ANNOTE(size, rows*cols);
21 };);
22 typedef struct s_matrix matrix_t;
23
24 static void mat_dump(matrix_t *mat, const char* name) {
25   int i,j;
26
27   printf(">>> Matrix %s dump (%d x %d)\n",name,mat->rows,mat->cols);
28   for (i=0; i<mat->rows; i++) {
29     printf("  ");
30     for (j=0; j<mat->cols; j++)
31       printf(" %.2f",mat->ctn[i*mat->cols + j]);
32     printf("\n");
33   }
34   printf("<<< end of matrix %s dump\n",name);
35 }
36
37 /* register messages which may be sent and their payload
38    (common to client and server) */
39 static void register_messages(void) {
40   gras_datadesc_type_t matrix_type, request_type;
41
42   matrix_type=gras_datadesc_by_symbol(s_matrix);
43   request_type=gras_datadesc_array_fixed("matrix_t[2]",matrix_type,2);
44   
45   gras_msgtype_declare("answer", matrix_type);
46   gras_msgtype_declare("request", request_type);
47 }
48
49 /* Function prototypes */
50 int server (int argc,char *argv[]);
51 int client (int argc,char *argv[]);
52
53 /* **********************************************************************
54  * Server code
55  * **********************************************************************/
56
57 static int server_cb_request_handler(gras_socket_t expeditor, void *payload_data) {
58                              
59   /* 1. Get the payload into the data variable */
60   matrix_t *data=(matrix_t*)payload_data;
61   matrix_t result;
62   int i,j,k;
63    
64   /* 2. Make some room to return the result */
65   result.rows = data[0].rows;
66   result.cols = data[1].cols;
67   result.ctn = xbt_malloc0(sizeof(double) * result.rows * result.cols);
68
69   /* 3. Do the computation */
70   for (i=0; i<result.rows; i++) 
71     for (j=0; j<result.cols; j++) 
72       for (k=0; k<data[1].rows; k++) 
73         result.ctn[i*result.cols + j] +=  data[0].ctn[i*result.cols +k] *data[1].ctn[k*result.cols +j];
74
75   /* 4. Send it back as payload of a pong message to the expeditor */
76   gras_msg_send(expeditor, gras_msgtype_by_name("answer"), &result);
77
78   /* 5. Cleanups */
79   free(data[0].ctn); 
80   free(data[1].ctn);
81   free(data);
82   free(result.ctn);
83   gras_socket_close(expeditor);
84    
85   return 1;
86 } /* end_of_server_cb_request_handler */
87
88 int server (int argc,char *argv[]) {
89   xbt_ex_t e; 
90   gras_socket_t sock=NULL;
91   int port = 4000;
92   
93   /* 1. Init the GRAS infrastructure */
94   gras_init(&argc,argv);
95    
96   /* 2. Get the port I should listen on from the command line, if specified */
97   if (argc == 2) {
98     port=atoi(argv[1]);
99   }
100
101   /* 3. Create my master socket */
102   INFO1("Launch server (port=%d)", port);
103   TRY {
104     sock = gras_socket_server(port);
105   } CATCH(e) {
106     RETHROW0("Unable to establish a server socket: %s");
107   }
108
109   /* 4. Register the known messages and payloads. */
110   register_messages();
111    
112   /* 5. Register my callback */
113   gras_cb_register(gras_msgtype_by_name("request"),&server_cb_request_handler);
114
115   /* 6. Wait up to 10 minutes for an incomming message to handle */
116   gras_msg_handle(600.0);
117    
118   /* 7. Free the allocated resources, and shut GRAS down */
119   gras_socket_close(sock);
120   gras_exit();
121    
122   INFO0("Done.");
123   return 0;
124 } /* end_of_server */
125
126 /* **********************************************************************
127  * Client code
128  * **********************************************************************/
129
130 /* Function prototypes */
131
132 int client(int argc,char *argv[]) {
133   xbt_ex_t e; 
134   gras_socket_t toserver=NULL; /* peer */
135
136   gras_socket_t from;
137   matrix_t request[2], answer;
138
139   int i,j;
140
141   const char *host = "127.0.0.1";
142         int   port = 4000;
143
144   /* 1. Init the GRAS's infrastructure */
145   gras_init(&argc, argv);
146    
147   /* 2. Get the server's address. The command line override defaults when specified */
148   if (argc == 3) {
149     host=argv[1];
150     port=atoi(argv[2]);
151   } 
152
153   INFO2("Launch client (server on %s:%d)",host,port);
154    
155   /* 3. Wait for the server startup */
156   gras_os_sleep(1);
157    
158   /* 4. Create a socket to speak to the server */
159   TRY {
160     toserver=gras_socket_client(host,port);
161   } CATCH(e) {
162     RETHROW0("Unable to connect to the server: %s");
163   }
164   INFO2("Connected to %s:%d.",host,port);    
165
166
167   /* 5. Register the messages (before use) */
168   register_messages();
169
170   /* 6. Keep the user informed of what's going on */
171   INFO2(">>>>>>>> Connected to server which is on %s:%d <<<<<<<<", 
172         gras_socket_peer_name(toserver),gras_socket_peer_port(toserver));
173
174   /* 7. Prepare and send the request to the server */
175
176   request[0].rows=request[0].cols=request[1].rows=request[1].cols=MATSIZE;
177
178   request[0].ctn=xbt_malloc0(sizeof(double)*MATSIZE*MATSIZE);
179   request[1].ctn=xbt_malloc0(sizeof(double)*MATSIZE*MATSIZE);
180
181   for (i=0; i<MATSIZE; i++) {
182     request[0].ctn[i*MATSIZE+i] = 1;
183     for (j=0; j<MATSIZE; j++)
184       request[1].ctn[i*MATSIZE+j] = i*MATSIZE+j;
185   }
186   //  mat_dump(&request[0],"C:sent0");
187   //  mat_dump(&request[1],"C:sent1");
188
189   gras_msg_send(toserver, gras_msgtype_by_name("request"), &request);
190
191   free(request[0].ctn);
192   free(request[1].ctn);
193
194   INFO2(">>>>>>>> Request sent to %s:%d <<<<<<<<",
195         gras_socket_peer_name(toserver),gras_socket_peer_port(toserver));
196
197   /* 8. Wait for the answer from the server, and deal with issues */
198   gras_msg_wait(6000,gras_msgtype_by_name("answer"),&from,&answer);
199
200   //  mat_dump(&answer,"C:answer");
201   for (i=0; i<MATSIZE*MATSIZE; i++) 
202     xbt_assert(answer.ctn[i]==i);
203
204   /* 9. Keep the user informed of what's going on, again */
205   INFO2(">>>>>>>> Got answer from %s:%d <<<<<<<<", 
206         gras_socket_peer_name(from),gras_socket_peer_port(from));
207
208   /* 10. Free the allocated resources, and shut GRAS down */
209   gras_socket_close(toserver);
210   gras_exit();
211   INFO0("Done.");
212   return 0;
213 } /* end_of_client */