Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
587deda1d7914f4110cdacb15a20cee93ef0624e
[simgrid.git] / examples / msg / icomms / peer2.c
1 /* Copyright (c) 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <stdio.h>
8 #include "msg/msg.h"            /* Yeah! If you want to use msg, you need to include msg/msg.h */
9 #include "xbt/sysdep.h"         /* calloc, printf */
10
11 /* Create a log channel to have nice outputs. */
12 #include "xbt/log.h"
13 #include "xbt/asserts.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
15                              "Messages specific for this msg example");
16
17 int sender(int argc, char *argv[]);
18 int receiver(int argc, char *argv[]);
19
20 MSG_error_t test_all(const char *platform_file,
21                      const char *application_file);
22
23 /** Sender function  */
24 int sender(int argc, char *argv[])
25 {
26   long number_of_tasks = atol(argv[1]);
27   double task_comp_size = atof(argv[2]);
28   double task_comm_size = atof(argv[3]);
29   long receivers_count = atol(argv[4]);
30
31   msg_comm_t *comm = xbt_new(msg_comm_t, number_of_tasks + receivers_count);
32   int i;
33   m_task_t task = NULL;
34   for (i = 0; i < number_of_tasks; i++) {
35     char mailbox[256];
36     char sprintf_buffer[256];
37     sprintf(mailbox, "receiver-%ld", i % receivers_count);
38     sprintf(sprintf_buffer, "Task_%d", i);
39     task =
40         MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
41                         NULL);
42     comm[i] = MSG_task_isend(task, mailbox);
43     INFO2("Send to receiver-%ld Task_%d", i % receivers_count, i);
44   }
45   for (i = 0; i < receivers_count; i++) {
46     char mailbox[80];
47     sprintf(mailbox, "receiver-%ld", i % receivers_count);
48     task = MSG_task_create("finalize", 0, 0, 0);
49     comm[i + number_of_tasks] = MSG_task_isend(task, mailbox);
50     INFO1("Send to receiver-%ld finalize", i % receivers_count);
51
52   }
53   /* Here we are waiting for the completion of all communications */
54   MSG_comm_waitall(comm, (number_of_tasks + receivers_count), -1);
55
56   INFO0("Goodbye now!");
57   xbt_free(comm);
58   return 0;
59 }                               /* end_of_sender */
60
61 /** Receiver function  */
62 int receiver(int argc, char *argv[])
63 {
64   m_task_t task = NULL;
65   MSG_error_t res;
66   int id = -1;
67   char mailbox[80];
68   msg_comm_t res_irecv;
69   int read = sscanf(argv[1], "%d", &id);
70   xbt_assert1(read, "Invalid argument %s\n", argv[1]);
71   MSG_process_sleep(10);
72   sprintf(mailbox, "receiver-%d", id);
73   while (1) {
74     res_irecv = MSG_task_irecv(&(task), mailbox);
75     INFO0("Wait to receive a task");
76     res = MSG_comm_wait(res_irecv, -1);
77     xbt_assert0(res == MSG_OK, "MSG_task_get failed");
78     INFO1("Received \"%s\"", MSG_task_get_name(task));
79     if (!strcmp(MSG_task_get_name(task), "finalize")) {
80       MSG_task_destroy(task);
81       break;
82     }
83
84     INFO1("Processing \"%s\"", MSG_task_get_name(task));
85     MSG_task_execute(task);
86     INFO1("\"%s\" done", MSG_task_get_name(task));
87     MSG_task_destroy(task);
88     task = NULL;
89   }
90   INFO0("I'm done. See you!");
91   return 0;
92 }                               /* end_of_receiver */
93
94 /** Test function */
95 MSG_error_t test_all(const char *platform_file,
96                      const char *application_file)
97 {
98   MSG_error_t res = MSG_OK;
99
100   /* MSG_config("workstation/model","KCCFLN05"); */
101   {                             /*  Simulation setting */
102     MSG_set_channel_number(0);
103     MSG_create_environment(platform_file);
104   }
105   {                             /*   Application deployment */
106     MSG_function_register("sender", sender);
107     MSG_function_register("receiver", receiver);
108     MSG_launch_application(application_file);
109   }
110   res = MSG_main();
111
112   INFO1("Simulation time %g", MSG_get_clock());
113   return res;
114 }                               /* end_of_test_all */
115
116
117 /** Main function */
118 int main(int argc, char *argv[])
119 {
120   MSG_error_t res = MSG_OK;
121
122   MSG_global_init(&argc, argv);
123   if (argc < 3) {
124     printf("Usage: %s platform_file deployment_file\n", argv[0]);
125     printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
126     exit(1);
127   }
128   res = test_all(argv[1], argv[2]);
129   MSG_clean();
130
131   if (res == MSG_OK)
132     return 0;
133   else
134     return 1;
135 }                               /* end_of_main */