Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fixing codacy warnings on multi-core VM tests + using a dedicated platform file for...
[simgrid.git] / examples / msg / async-wait / async-wait.c
1 /* Copyright (c) 2010-2016. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "simgrid/msg.h"
7
8 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_async_wait, "Messages specific for this msg example");
9
10 /* Main function of the Sender process */
11 static int sender(int argc, char *argv[])
12 {
13   xbt_assert(argc==7, "The sender function expects 6 arguments from the XML deployment file"); 
14   long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s");        /* - number of tasks */
15   double task_comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s"); /* - computational cost */
16   double task_comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s"); /* - communication cost */
17   long receivers_count = xbt_str_parse_int(argv[4], "Invalid amount of receivers: %s");    /* - number of receivers */
18   double sleep_start_time = xbt_str_parse_double(argv[5], "Invalid sleep start time: %s"); /* - start time */
19   double sleep_test_time = xbt_str_parse_double(argv[6], "Invalid test time: %s");         /* - test time */
20
21   XBT_INFO("sleep_start_time : %f , sleep_test_time : %f", sleep_start_time, sleep_test_time);
22
23   MSG_process_sleep(sleep_start_time);
24   for (int i = 0; i < number_of_tasks; i++) {
25     char mailbox[80];
26     char taskname[80];
27
28     snprintf(mailbox,79, "receiver-%ld", i % receivers_count);
29     snprintf(taskname,79, "Task_%d", i);
30
31     /* This process first creates a task and send it asynchronously with @ref MSG_task_isend. Then, if: */
32     msg_task_t task = MSG_task_create(taskname, task_comp_size, task_comm_size, NULL);
33     msg_comm_t comm = MSG_task_isend(task, mailbox);
34     XBT_INFO("Send to receiver-%ld Task_%d", i % receivers_count, i);
35
36     if (sleep_test_time > 0) { /* - "test_time" is set to 0, wait on @ref MSG_comm_wait */
37       while (MSG_comm_test(comm) == 0) { /* - Call @ref MSG_comm_test every "test_time" otherwise */
38         MSG_process_sleep(sleep_test_time);
39       }
40     } else {
41       MSG_comm_wait(comm, -1);
42     }
43     MSG_comm_destroy(comm);
44   }
45
46   for (int i = 0; i < receivers_count; i++) {
47     char mailbox[80];
48     snprintf(mailbox,79, "receiver-%ld", i % receivers_count);
49     msg_task_t task = MSG_task_create("finalize", 0, 0, 0);
50     msg_comm_t comm = MSG_task_isend(task, mailbox);
51     XBT_INFO("Send to receiver-%ld finalize", i % receivers_count);
52     if (sleep_test_time > 0) {
53       while (MSG_comm_test(comm) == 0) {
54         MSG_process_sleep(sleep_test_time);
55       }
56     } else {
57       MSG_comm_wait(comm, -1);
58     }
59     MSG_comm_destroy(comm);
60   }
61
62   XBT_INFO("Goodbye now!");
63   return 0;
64 }
65
66 /* Receiver process expects 3 arguments: */
67 static int receiver(int argc, char *argv[])
68 {
69   xbt_assert(argc==4, "The relay_runner function does not accept any parameter from the XML deployment file");
70   int id = xbt_str_parse_int(argv[1], "Invalid id: %s");                                        /* - unique id */
71   double sleep_start_time = xbt_str_parse_double(argv[2], "Invalid sleep start parameter: %s"); /* - start time */
72   double sleep_test_time = xbt_str_parse_double(argv[3], "Invalid sleep test parameter: %s");   /* - test time */
73   XBT_INFO("sleep_start_time : %f , sleep_test_time : %f", sleep_start_time, sleep_test_time);
74
75   MSG_process_sleep(sleep_start_time); /* This process first sleeps for "start time" seconds.  */
76
77   char mailbox[80];
78   snprintf(mailbox,79, "receiver-%d", id);
79   while (1) {
80     msg_task_t task = NULL;
81     msg_comm_t comm = MSG_task_irecv(&task, mailbox); /* Then it posts asynchronous receives (@ref MSG_task_irecv) and*/
82     XBT_INFO("Wait to receive a task");
83
84     if (sleep_test_time > 0) {               /* - if "test_time" is set to 0, wait on @ref MSG_comm_wait */
85       while (MSG_comm_test(comm) == 0) {     /* - Call @ref MSG_comm_test every "test_time" otherwise */
86         MSG_process_sleep(sleep_test_time);
87       }
88     } else {
89       msg_error_t res = MSG_comm_wait(comm, -1);
90       xbt_assert(res == MSG_OK, "MSG_task_get failed");
91     }
92     MSG_comm_destroy(comm);
93
94     XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
95     if (strcmp(MSG_task_get_name(task), "finalize") == 0) { /* If the received task is "finalize", the process ends */
96       MSG_task_destroy(task);
97       break;
98     }
99
100     XBT_INFO("Processing \"%s\"", MSG_task_get_name(task)); /* Otherwise, the task is processed */
101     MSG_task_execute(task);
102     XBT_INFO("\"%s\" done", MSG_task_get_name(task));
103     MSG_task_destroy(task);
104   }
105   XBT_INFO("I'm done. See you!");
106   return 0;
107 }
108
109 int main(int argc, char *argv[])
110 {
111   MSG_init(&argc, argv);
112   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
113              "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
114
115   MSG_create_environment(argv[1]);/* - Load the platform description */
116
117   MSG_function_register("sender", sender);
118   MSG_function_register("receiver", receiver);
119   MSG_launch_application(argv[2]);/* - Deploy the sender and receiver processes */
120
121   msg_error_t res = MSG_main();  /* - Run the simulation */
122
123   XBT_INFO("Simulation time %g", MSG_get_clock());
124
125   return res != MSG_OK;
126 }