Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
plug a leak
[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(result.ctn);
82   gras_socket_close(expeditor);
83    
84   return 1;
85 } /* end_of_server_cb_request_handler */
86
87 int server (int argc,char *argv[]) {
88   xbt_ex_t e; 
89   gras_socket_t sock=NULL;
90   int port = 4000;
91   
92   /* 1. Init the GRAS infrastructure */
93   gras_init(&argc,argv);
94    
95   /* 2. Get the port I should listen on from the command line, if specified */
96   if (argc == 2) {
97     port=atoi(argv[1]);
98   }
99
100   /* 3. Create my master socket */
101   INFO1("Launch server (port=%d)", port);
102   TRY {
103     sock = gras_socket_server(port);
104   } CATCH(e) {
105     RETHROW0("Unable to establish a server socket: %s");
106   }
107
108   /* 4. Register the known messages and payloads. */
109   register_messages();
110    
111   /* 5. Register my callback */
112   gras_cb_register(gras_msgtype_by_name("request"),&server_cb_request_handler);
113
114   /* 6. Wait up to 10 minutes for an incomming message to handle */
115   gras_msg_handle(600.0);
116    
117   /* 7. Free the allocated resources, and shut GRAS down */
118   gras_socket_close(sock);
119   gras_exit();
120    
121   INFO0("Done.");
122   return 0;
123 } /* end_of_server */
124
125 /* **********************************************************************
126  * Client code
127  * **********************************************************************/
128
129 /* Function prototypes */
130
131 int client(int argc,char *argv[]) {
132   xbt_ex_t e; 
133   gras_socket_t toserver=NULL; /* peer */
134
135   gras_socket_t from;
136   matrix_t request[2], answer;
137
138   int i,j;
139
140   const char *host = "127.0.0.1";
141         int   port = 4000;
142
143   /* 1. Init the GRAS's infrastructure */
144   gras_init(&argc, argv);
145    
146   /* 2. Get the server's address. The command line override defaults when specified */
147   if (argc == 3) {
148     host=argv[1];
149     port=atoi(argv[2]);
150   } 
151
152   INFO2("Launch client (server on %s:%d)",host,port);
153    
154   /* 3. Wait for the server startup */
155   gras_os_sleep(1);
156    
157   /* 4. Create a socket to speak to the server */
158   TRY {
159     toserver=gras_socket_client(host,port);
160   } CATCH(e) {
161     RETHROW0("Unable to connect to the server: %s");
162   }
163   INFO2("Connected to %s:%d.",host,port);    
164
165
166   /* 5. Register the messages (before use) */
167   register_messages();
168
169   /* 6. Keep the user informed of what's going on */
170   INFO2(">>>>>>>> Connected to server which is on %s:%d <<<<<<<<", 
171         gras_socket_peer_name(toserver),gras_socket_peer_port(toserver));
172
173   /* 7. Prepare and send the request to the server */
174
175   request[0].rows=request[0].cols=request[1].rows=request[1].cols=MATSIZE;
176
177   request[0].ctn=xbt_malloc0(sizeof(double)*MATSIZE*MATSIZE);
178   request[1].ctn=xbt_malloc0(sizeof(double)*MATSIZE*MATSIZE);
179
180   for (i=0; i<MATSIZE; i++) {
181     request[0].ctn[i*MATSIZE+i] = 1;
182     for (j=0; j<MATSIZE; j++)
183       request[1].ctn[i*MATSIZE+j] = i*MATSIZE+j;
184   }
185   /*  mat_dump(&request[0],"C:sent0");*/
186   /*  mat_dump(&request[1],"C:sent1");*/
187
188   gras_msg_send(toserver, gras_msgtype_by_name("request"), &request);
189
190   free(request[0].ctn);
191   free(request[1].ctn);
192
193   INFO2(">>>>>>>> Request sent to %s:%d <<<<<<<<",
194         gras_socket_peer_name(toserver),gras_socket_peer_port(toserver));
195
196   /* 8. Wait for the answer from the server, and deal with issues */
197   gras_msg_wait(6000,gras_msgtype_by_name("answer"),&from,&answer);
198
199   /*  mat_dump(&answer,"C:answer");*/
200   for (i=0; i<MATSIZE*MATSIZE; i++) 
201     xbt_assert(answer.ctn[i]==i);
202
203   /* 9. Keep the user informed of what's going on, again */
204   INFO2(">>>>>>>> Got answer from %s:%d <<<<<<<<", 
205         gras_socket_peer_name(from),gras_socket_peer_port(from));
206
207   /* 10. Free the allocated resources, and shut GRAS down */
208   free(answer.ctn);
209   gras_socket_close(toserver);
210   gras_exit();
211   INFO0("Done.");
212   return 0;
213 } /* end_of_client */