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_socket_t expeditor, void *payload_data) {
59 /* 1. Get the payload into the data variable */
60 matrix_t *data=(matrix_t*)payload_data;
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);
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];
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);
82 gras_socket_close(expeditor);
85 } /* end_of_server_cb_request_handler */
87 int server (int argc,char *argv[]) {
89 gras_socket_t sock=NULL;
92 /* 1. Init the GRAS infrastructure */
93 gras_init(&argc,argv);
95 /* 2. Get the port I should listen on from the command line, if specified */
100 /* 3. Create my master socket */
101 INFO1("Launch server (port=%d)", port);
103 sock = gras_socket_server(port);
105 RETHROW0("Unable to establish a server socket: %s");
108 /* 4. Register the known messages and payloads. */
111 /* 5. Register my callback */
112 gras_cb_register(gras_msgtype_by_name("request"),&server_cb_request_handler);
114 /* 6. Wait up to 10 minutes for an incomming message to handle */
115 gras_msg_handle(600.0);
117 /* 7. Free the allocated resources, and shut GRAS down */
118 gras_socket_close(sock);
123 } /* end_of_server */
125 /* **********************************************************************
127 * **********************************************************************/
129 /* Function prototypes */
131 int client(int argc,char *argv[]) {
133 gras_socket_t toserver=NULL; /* peer */
136 matrix_t request[2], answer;
140 const char *host = "127.0.0.1";
143 /* 1. Init the GRAS's infrastructure */
144 gras_init(&argc, argv);
146 /* 2. Get the server's address. The command line override defaults when specified */
152 INFO2("Launch client (server on %s:%d)",host,port);
154 /* 3. Wait for the server startup */
157 /* 4. Create a socket to speak to the server */
159 toserver=gras_socket_client(host,port);
161 RETHROW0("Unable to connect to the server: %s");
163 INFO2("Connected to %s:%d.",host,port);
166 /* 5. Register the messages (before use) */
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));
173 /* 7. Prepare and send the request to the server */
175 request[0].rows=request[0].cols=request[1].rows=request[1].cols=MATSIZE;
177 request[0].ctn=xbt_malloc0(sizeof(double)*MATSIZE*MATSIZE);
178 request[1].ctn=xbt_malloc0(sizeof(double)*MATSIZE*MATSIZE);
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;
185 // mat_dump(&request[0],"C:sent0");
186 // mat_dump(&request[1],"C:sent1");
188 gras_msg_send(toserver, gras_msgtype_by_name("request"), &request);
190 free(request[0].ctn);
191 free(request[1].ctn);
193 INFO2(">>>>>>>> Request sent to %s:%d <<<<<<<<",
194 gras_socket_peer_name(toserver),gras_socket_peer_port(toserver));
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);
199 // mat_dump(&answer,"C:answer");
200 for (i=0; i<MATSIZE*MATSIZE; i++)
201 xbt_assert(answer.ctn[i]==i);
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));
207 /* 10. Free the allocated resources, and shut GRAS down */
208 gras_socket_close(toserver);
212 } /* end_of_client */