Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge remote-tracking branch 'origin/master'
[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 /** @addtogroup MSG_examples
18  * 
19  * - <b>msg/icomms/peer2.c</b>: demonstrates the @ref MSG_comm_waitall function
20  */
21
22 int sender(int argc, char *argv[]);
23 int receiver(int argc, char *argv[]);
24
25 MSG_error_t test_all(const char *platform_file,
26                      const char *application_file);
27
28 /** Sender function  */
29 int sender(int argc, char *argv[])
30 {
31   long number_of_tasks = atol(argv[1]);
32   double task_comp_size = atof(argv[2]);
33   double task_comm_size = atof(argv[3]);
34   long receivers_count = atol(argv[4]);
35
36   msg_comm_t *comm = xbt_new(msg_comm_t, number_of_tasks + receivers_count);
37   int i;
38   m_task_t task = NULL;
39   for (i = 0; i < number_of_tasks; i++) {
40     char mailbox[256];
41     char sprintf_buffer[256];
42     sprintf(mailbox, "receiver-%ld", i % receivers_count);
43     sprintf(sprintf_buffer, "Task_%d", i);
44     task =
45         MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
46                         NULL);
47     comm[i] = MSG_task_isend(task, mailbox);
48     XBT_INFO("Send to receiver-%ld Task_%d", i % receivers_count, i);
49   }
50   for (i = 0; i < receivers_count; i++) {
51     char mailbox[80];
52     sprintf(mailbox, "receiver-%ld", i % receivers_count);
53     task = MSG_task_create("finalize", 0, 0, 0);
54     comm[i + number_of_tasks] = MSG_task_isend(task, mailbox);
55     XBT_INFO("Send to receiver-%ld finalize", i % receivers_count);
56
57   }
58   /* Here we are waiting for the completion of all communications */
59   MSG_comm_waitall(comm, (number_of_tasks + receivers_count), -1);
60
61   XBT_INFO("Goodbye now!");
62   xbt_free(comm);
63   return 0;
64 }                               /* end_of_sender */
65
66 /** Receiver function  */
67 int receiver(int argc, char *argv[])
68 {
69   m_task_t task = NULL;
70   _XBT_GNUC_UNUSED MSG_error_t res;
71   int id = -1;
72   char mailbox[80];
73   msg_comm_t res_irecv;
74   _XBT_GNUC_UNUSED int read;
75   read = sscanf(argv[1], "%d", &id);
76   xbt_assert(read, "Invalid argument %s\n", argv[1]);
77   MSG_process_sleep(10);
78   sprintf(mailbox, "receiver-%d", id);
79   while (1) {
80     res_irecv = MSG_task_irecv(&(task), mailbox);
81     XBT_INFO("Wait to receive a task");
82     res = MSG_comm_wait(res_irecv, -1);
83     xbt_assert(res == MSG_OK, "MSG_task_get failed");
84     XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
85     if (!strcmp(MSG_task_get_name(task), "finalize")) {
86       MSG_task_destroy(task);
87       break;
88     }
89
90     XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
91     MSG_task_execute(task);
92     XBT_INFO("\"%s\" done", MSG_task_get_name(task));
93     MSG_task_destroy(task);
94     task = NULL;
95   }
96   XBT_INFO("I'm done. See you!");
97   return 0;
98 }                               /* end_of_receiver */
99
100 /** Test function */
101 MSG_error_t test_all(const char *platform_file,
102                      const char *application_file)
103 {
104   MSG_error_t res = MSG_OK;
105
106   /* MSG_config("workstation/model","KCCFLN05"); */
107   {                             /*  Simulation setting */
108     MSG_create_environment(platform_file);
109   }
110   {                             /*   Application deployment */
111     MSG_function_register("sender", sender);
112     MSG_function_register("receiver", receiver);
113     MSG_launch_application(application_file);
114   }
115   res = MSG_main();
116
117   XBT_INFO("Simulation time %g", MSG_get_clock());
118   return res;
119 }                               /* end_of_test_all */
120
121
122 /** Main function */
123 int main(int argc, char *argv[])
124 {
125   MSG_error_t res = MSG_OK;
126
127   MSG_init(&argc, argv);
128   if (argc < 3) {
129     printf("Usage: %s platform_file deployment_file\n", argv[0]);
130     printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
131     exit(1);
132   }
133   res = test_all(argv[1], argv[2]);
134   MSG_clean();
135
136   if (res == MSG_OK)
137     return 0;
138   else
139     return 1;
140 }                               /* end_of_main */