3 /* GridRPC - Fake Grid RPC thingy doing matrix multiplications (as expected)*/
5 /* Copyright (c) 2005 Martin Quinson. All rights reserved. */
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. */
14 XBT_LOG_NEW_DEFAULT_CATEGORY(MatMult,"Messages specific to this example");
16 GRAS_DEFINE_TYPE(s_matrix,
20 double *ctn GRAS_ANNOTE(size, rows*cols);
22 typedef struct s_matrix matrix_t;
24 static void mat_dump(matrix_t *mat, const char* name) {
27 printf(">>> Matrix %s dump (%d x %d)\n",name,mat->rows,mat->cols);
28 for (i=0; i<mat->rows; i++) {
30 for (j=0; j<mat->cols; j++)
31 printf(" %.2f",mat->ctn[i*mat->cols + j]);
34 printf("<<< end of matrix %s dump\n",name);
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;
42 matrix_type=gras_datadesc_by_symbol(s_matrix);
43 request_type=gras_datadesc_array_fixed("matrix_t[2]",matrix_type,2);
45 gras_msgtype_declare("answer", matrix_type);
46 gras_msgtype_declare("request", request_type);
49 /* Function prototypes */
50 int server (int argc,char *argv[]);
51 int client (int argc,char *argv[]);
53 /* **********************************************************************
55 * **********************************************************************/
57 static int server_cb_request_handler(gras_msg_cb_ctx_t ctx, void *payload_data) {
58 gras_socket_t expeditor=gras_msg_cb_ctx_from(ctx);
60 /* 1. Get the payload into the data variable */
61 matrix_t *data=(matrix_t*)payload_data;
65 /* 2. Make some room to return the result */
66 result.rows = data[0].rows;
67 result.cols = data[1].cols;
68 result.ctn = xbt_malloc0(sizeof(double) * result.rows * result.cols);
70 /* 3. Do the computation */
71 for (i=0; i<result.rows; i++)
72 for (j=0; j<result.cols; j++)
73 for (k=0; k<data[1].rows; k++)
74 result.ctn[i*result.cols + j] += data[0].ctn[i*result.cols +k] *data[1].ctn[k*result.cols +j];
76 /* 4. Send it back as payload of a pong message to the expeditor */
77 gras_msg_send(expeditor, gras_msgtype_by_name("answer"), &result);
83 gras_socket_close(expeditor);
86 } /* end_of_server_cb_request_handler */
88 int server (int argc,char *argv[]) {
90 gras_socket_t sock=NULL;
93 /* 1. Init the GRAS infrastructure */
94 gras_init(&argc,argv);
96 /* 2. Get the port I should listen on from the command line, if specified */
101 /* 3. Create my master socket */
102 INFO1("Launch server (port=%d)", port);
104 sock = gras_socket_server(port);
106 RETHROW0("Unable to establish a server socket: %s");
109 /* 4. Register the known messages and payloads. */
112 /* 5. Register my callback */
113 gras_cb_register(gras_msgtype_by_name("request"),&server_cb_request_handler);
115 /* 6. Wait up to 10 minutes for an incomming message to handle */
116 gras_msg_handle(600.0);
118 /* 7. Free the allocated resources, and shut GRAS down */
119 gras_socket_close(sock);
124 } /* end_of_server */
126 /* **********************************************************************
128 * **********************************************************************/
130 /* Function prototypes */
132 int client(int argc,char *argv[]) {
134 gras_socket_t toserver=NULL; /* peer */
137 matrix_t request[2], answer;
141 const char *host = "127.0.0.1";
144 /* 1. Init the GRAS's infrastructure */
145 gras_init(&argc, argv);
147 /* 2. Get the server's address. The command line override defaults when specified */
153 INFO2("Launch client (server on %s:%d)",host,port);
155 /* 3. Wait for the server startup */
158 /* 4. Create a socket to speak to the server */
160 toserver=gras_socket_client(host,port);
162 RETHROW0("Unable to connect to the server: %s");
164 INFO2("Connected to %s:%d.",host,port);
167 /* 5. Register the messages (before use) */
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));
174 /* 7. Prepare and send the request to the server */
176 request[0].rows=request[0].cols=request[1].rows=request[1].cols=MATSIZE;
178 request[0].ctn=xbt_malloc0(sizeof(double)*MATSIZE*MATSIZE);
179 request[1].ctn=xbt_malloc0(sizeof(double)*MATSIZE*MATSIZE);
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;
186 /* mat_dump(&request[0],"C:sent0");*/
187 /* mat_dump(&request[1],"C:sent1");*/
189 gras_msg_send(toserver, gras_msgtype_by_name("request"), &request);
191 free(request[0].ctn);
192 free(request[1].ctn);
194 INFO2(">>>>>>>> Request sent to %s:%d <<<<<<<<",
195 gras_socket_peer_name(toserver),gras_socket_peer_port(toserver));
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);
200 /* mat_dump(&answer,"C:answer");*/
201 for (i=0; i<MATSIZE*MATSIZE; i++)
202 xbt_assert(answer.ctn[i]==i);
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));
208 /* 10. Free the allocated resources, and shut GRAS down */
210 gras_socket_close(toserver);
214 } /* end_of_client */