Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Modify the all2all to support all data Message Size
[simgrid.git] / examples / gras / all2all / all2all.c
1 /* $Id$ */
2
3 /* ALL2ALL - all2all of GRAS features                                       */
4
5 /* Copyright (c) 2006 Ahmed Harbaoui. 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 #include "xbt/ex.h"
12
13 XBT_LOG_NEW_DEFAULT_CATEGORY(all2all,"Messages specific to this example");
14
15 /* register data which may be sent (common to client and server) */
16 static void register_messages(void) {
17 }
18
19 /* Function prototypes */
20 int receiver (int argc,char *argv[]);
21 int sender (int argc,char *argv[]);
22
23
24 /* **********************************************************************
25  * Receiver code
26  * **********************************************************************/
27 int receiver (int argc,char *argv[]) {
28
29   int myport; /* port on which I receive stuff */
30   int todo; /* amount of messages I should get */
31   char *data; /* message content */
32  
33   gras_socket_t mysock;  /* socket on which other people contact me */
34   gras_socket_t expeditor;  /* to notice who wrote me */
35  
36   /* Init the GRAS infrastructure and declare my globals */
37   gras_init(&argc,argv);
38  
39   /* Get my settings from the command line */
40   myport=atoi(argv[1]);
41   todo=atoi(argv[2]);
42
43   /* Create my master socket */
44   mysock = gras_socket_server(myport);
45
46   /* Register the known messages */
47   gras_msgtype_declare("data", gras_datadesc_by_name("string"));
48
49   /* Get the data */
50
51   INFO2("Listening on port %d (expecting %d messages)",
52         gras_socket_my_port(mysock),
53         todo);
54   while (todo>0) {
55      gras_msg_wait(60 /* wait up to one minute */,
56                    gras_msgtype_by_name("data"),
57                    &expeditor,
58                    &data);
59      todo--;
60      
61      INFO3("Got Data from %s:%d (still %d to go)",
62           /* data,*/
63            gras_socket_peer_name(expeditor), gras_socket_peer_port(expeditor),
64            todo);
65
66   }
67
68   /* Free the allocated resources, and shut GRAS down */
69   gras_socket_close(mysock);
70
71   gras_exit();
72   return 0;
73 } /* end_of_receiver */
74
75 /* **********************************************************************
76  * Sender code
77  * **********************************************************************/
78
79 int sender (int argc,char *argv[]) {
80
81   int i; /* iterator */
82   char *data; /* data exchanged */
83   int datasize; /* size of message */
84   xbt_host_t h; /* iterator */
85   
86   gras_socket_t peer;  /* socket to node */
87   
88  
89   /* xbt_dynar for hosts */
90   xbt_dynar_t hosts = xbt_dynar_new(sizeof(xbt_host_t),&xbt_host_free_voidp);
91  
92   /* Init the GRAS infrastructure and declare my globals */
93   gras_init(&argc,argv);
94  
95   /* Get the node location from argc/argv */
96   for (i=1; i<argc-1; i++){
97     xbt_host_t host = xbt_host_from_string(argv[i]);
98     xbt_dynar_push(hosts,&host);
99   }
100   
101   datasize=atoi(argv[argc-1]);
102   
103   data=(char *) malloc(datasize+1);  // allocation of datasize octets
104   memset(data, 32, datasize);
105   data[datasize] = '\0';
106
107   INFO0("Launch current node");
108
109   /* Register the known messages */
110   gras_msgtype_declare("data", gras_datadesc_by_name("string"));
111
112
113   /* Wait for receivers to startup */
114   gras_os_sleep(1);
115
116   /* write 'em */
117   xbt_dynar_foreach(hosts,i,h) {
118      
119      peer = gras_socket_client(h->name,h->port);
120      gras_msg_send(peer,gras_msgtype_by_name("data"),&data);
121      INFO2("  Sent Data from %s to %s",
122            /*data,*/gras_os_myname(),h->name);
123      gras_socket_close(peer);
124   }
125
126   /* Free the allocated resources, and shut GRAS down */
127   xbt_dynar_free(&hosts);
128      
129   gras_exit();
130   return 0;
131 } /* end_of_sender */