File: comm-wait.c

package info (click to toggle)
simgrid 4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 38,980 kB
  • sloc: cpp: 123,583; ansic: 66,779; python: 8,358; java: 6,406; fortran: 6,079; f90: 5,123; xml: 4,587; sh: 2,337; perl: 1,436; makefile: 105; lisp: 49; javascript: 7; sed: 6
file content (104 lines) | stat: -rw-r--r-- 4,038 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* Copyright (c) 2010-2025. The SimGrid Team. All rights reserved.          */

/* This program is free software; you can redistribute it and/or modify it
 * under the terms of the license (GNU LGPL) which comes with this package. */

#include "simgrid/actor.h"
#include "simgrid/comm.h"
#include "simgrid/engine.h"
#include "simgrid/mailbox.h"

#include "xbt/log.h"
#include "xbt/str.h"
#include "xbt/sysdep.h"

#include <stdio.h>

XBT_LOG_NEW_DEFAULT_CATEGORY(comm_wait, "Messages specific for this example");

/* Main function of the Sender actor */
static void sender(int argc, char* argv[])
{
  xbt_assert(argc == 5, "The sender function expects 4 arguments from the XML deployment file");
  long messages_count     = xbt_str_parse_int(argv[1], "Invalid amount of messages");  /* - number of messages */
  long message_size       = xbt_str_parse_int(argv[2], "Invalid message size");        /* - communication cost */
  double sleep_start_time = xbt_str_parse_double(argv[3], "Invalid sleep start time"); /* - start time */
  double sleep_test_time  = xbt_str_parse_double(argv[4], "Invalid test time");        /* - test time */

  XBT_INFO("sleep_start_time : %f , sleep_test_time : %f", sleep_start_time, sleep_test_time);
  sg_mailbox_t mailbox = sg_mailbox_by_name("receiver");

  sg_actor_sleep_for(sleep_start_time);
  for (int i = 0; i < messages_count; i++) {
    char* payload = bprintf("Message %d", i);

    /* This actor first sends a message asynchronously with @ref sg_mailbox_put_async. Then, if: */
    sg_comm_t comm = sg_mailbox_put_async(mailbox, payload, message_size);
    XBT_INFO("Send '%s' to 'receiver'", payload);

    if (sleep_test_time > 0) {          /* - "test_time" is set to 0, wait on @ref sg_comm_wait */
      while (sg_comm_test(comm) == 0) { /* - Call @ref sg_comm_test every "sleep_test_time" otherwise */
        sg_actor_sleep_for(sleep_test_time);
      }
    } else {
      sg_comm_wait(comm);
    }
  }

  XBT_INFO("Send 'finalize' to 'receiver'");
  sg_mailbox_put(mailbox, xbt_strdup("finalize"), 0);
}

/* Receiver actor expects 3 arguments: */
static void receiver(int argc, char* argv[])
{
  xbt_assert(argc == 3, "The relay_runner function does not accept any parameter from the XML deployment file");
  double sleep_start_time = xbt_str_parse_double(argv[1], "Invalid sleep start parameter"); /* - start time */
  double sleep_test_time  = xbt_str_parse_double(argv[2], "Invalid sleep test parameter");  /* - test time */
  XBT_INFO("sleep_start_time : %f , sleep_test_time : %f", sleep_start_time, sleep_test_time);

  sg_actor_sleep_for(sleep_start_time); /* This actor first sleeps for "start time" seconds.  */

  sg_mailbox_t mailbox = sg_mailbox_by_name("receiver");
  void* received       = NULL;

  XBT_INFO("Wait for my first message");
  while (1) {
    /* Then it posts asynchronous receives (@ref sg_mailbox_get_async) and*/
    sg_comm_t comm = sg_mailbox_get_async(mailbox, &received);

    if (sleep_test_time > 0) {          /* - if "test_time" is set to 0, wait on @ref sg_comm_wait */
      while (sg_comm_test(comm) == 0) { /* - Call @ref sg_comm_test every "sleep_test_time" otherwise */
        sg_actor_sleep_for(sleep_test_time);
      }
    } else {
      sg_comm_wait(comm);
    }
    XBT_INFO("I got a '%s'.", (char*)received);

    if (strcmp((char*)received, "finalize") == 0) { /* If the received task is "finalize", the actor ends */
      free(received);
      break;
    }
    free(received);
  }
}

int main(int argc, char* argv[])
{
  simgrid_init(&argc, argv);
  xbt_assert(argc > 2,
             "Usage: %s platform_file deployment_file\n"
             "\tExample: %s platform.xml deployment.xml\n",
             argv[0], argv[0]);

  simgrid_load_platform(argv[1]); /* - Load the platform description */

  simgrid_register_function("sender", sender);
  simgrid_register_function("receiver", receiver);
  simgrid_load_deployment(argv[2]); /* - Deploy the sender and receiver actors */

  simgrid_run(); /* - Run the simulation */

  return 0;
}