Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add more checking to inputs of SMPI functions
[simgrid.git] / examples / gras / mmrpc / mmrpc_client.c
1 /* GridRPC - Fake Grid RPC thingy doing matrix multiplications (as expected)*/
2
3 /* Copyright (c) 2006, 2007, 2009, 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 XBT_DEFINE_TYPE_EXTERN
10 #include "xbt/matrix.h"
11 #include "mmrpc.h"
12
13 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(MatMult);
14
15 static xbt_socket_t try_gras_socket_client(const char *host, int port)
16 {
17   volatile xbt_socket_t sock = NULL;
18   xbt_ex_t e;
19   TRY {
20     sock = gras_socket_client(host, port);
21   }
22   CATCH(e) {
23     if (e.category != system_error)
24       RETHROWF("Unable to connect to the server: %s");
25     xbt_ex_free(e);
26   }
27   return sock;
28 }
29
30 int client(int argc, char *argv[])
31 {
32   xbt_socket_t toserver = NULL;        /* peer */
33
34   xbt_socket_t from;
35   xbt_matrix_t request[2], answer;
36
37   int i;
38   _XBT_GNUC_UNUSED int j;
39
40   const char *host = "127.0.0.1";
41   int port = 4000;
42
43   /* 1. Init the GRAS's infrastructure */
44   gras_init(&argc, argv);
45
46   /* 2. Get the server's address. The command line override defaults when specified */
47   if (argc == 3) {
48     host = argv[1];
49     port = atoi(argv[2]);
50   }
51
52   XBT_INFO("Launch client (server on %s:%d)", host, port);
53
54   /* 3. Register the messages (before use) */
55   mmrpc_register_messages();
56
57   /* 4. Create a socket to speak to the server */
58   while (!(toserver = try_gras_socket_client(host, port)))
59     gras_os_sleep(0.05);
60   XBT_INFO("Connected to %s:%d.", host, port);
61
62   /* 5. Keep the user informed of what's going on */
63   XBT_INFO(">>>>>>>> Connected to server which is on %s:%d <<<<<<<<",
64         xbt_socket_peer_name(toserver), xbt_socket_peer_port(toserver));
65
66   /* 6. Prepare and send the request to the server */
67
68   request[0] = xbt_matrix_double_new_id(MATSIZE, MATSIZE);
69   request[1] = xbt_matrix_double_new_rand(MATSIZE, MATSIZE);
70
71   /*
72      xbt_matrix_dump(request[0],"C:sent0",0,xbt_matrix_dump_display_double);
73      xbt_matrix_dump(request[1],"C:sent1",0,xbt_matrix_dump_display_double);
74    */
75
76   gras_msg_send(toserver, "request", &request);
77
78   xbt_matrix_free(request[0]);
79
80   XBT_INFO(">>>>>>>> Request sent to %s:%d <<<<<<<<",
81         xbt_socket_peer_name(toserver), xbt_socket_peer_port(toserver));
82
83   /* 7. Wait for the answer from the server, and deal with issues */
84   gras_msg_wait(6000, "answer", &from, &answer);
85
86   /*
87      xbt_matrix_dump(answer,"C:answer",0,xbt_matrix_dump_display_double);
88    */
89   for (i = 0; i < MATSIZE; i++)
90     for (j = 0; i < MATSIZE; i++)
91       xbt_assert(xbt_matrix_get_as(answer, i, j, double) ==
92                   xbt_matrix_get_as(request[1], i, j, double),
93                   "Answer does not match expectations. Found %f at cell %d,%d instead of %f",
94                   xbt_matrix_get_as(answer, i, j, double), i, j,
95                   xbt_matrix_get_as(request[1], i, j, double));
96
97   /* 8. Keep the user informed of what's going on, again */
98   XBT_INFO(">>>>>>>> Got answer from %s:%d (values are right) <<<<<<<<",
99         xbt_socket_peer_name(from), xbt_socket_peer_port(from));
100
101   /* 9. Free the allocated resources, and shut GRAS down */
102   xbt_matrix_free(request[1]);
103   xbt_matrix_free(answer);
104   gras_socket_close(toserver);
105   gras_exit();
106   return 0;
107 }                               /* end_of_client */