aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLoic Guegan <manzerbredes@mailbox.org>2021-05-20 19:23:57 +0200
committerLoic Guegan <manzerbredes@mailbox.org>2021-05-20 19:23:57 +0200
commit1124887d0fa7494f519c573e5ad44877ff10fc93 (patch)
treef34c8615c4ed9d3b6ca6c71083a6f74ce00e130f /src
parent00dfd87c8570ea97e0b7cf3cd0fd7257f7125ae0 (diff)
Cleaning code
Diffstat (limited to 'src')
-rw-r--r--src/simulator.cc96
1 files changed, 43 insertions, 53 deletions
diff --git a/src/simulator.cc b/src/simulator.cc
index d2f971f..d122a5b 100644
--- a/src/simulator.cc
+++ b/src/simulator.cc
@@ -17,6 +17,7 @@
#define MODE_TX() simgrid::s4u::this_actor::get_host()->set_pstate(3);
#define CLOCK (simgrid::s4u::Engine::get_clock())
#define CNAME (selfName.c_str())
+#define FOR(t) (t<uptime?t:uptime)
#define TRACK_UPTIME(instruction) \
{ \
double uptimeTrack=CLOCK; \
@@ -62,13 +63,14 @@ typedef unsigned int u32;
*/
class Payload{
public:
- Payload():hint(0),duration(0),HasHint(false),HisForward(false),HasData(false){}
- Payload(Payload &p):hint(p.hint),duration(p.duration),HasHint(p.HasHint),DedicatedMailbox(p.DedicatedMailbox),HisForward(p.HisForward){}
+ Payload():hint(0),duration(0),HasHint(false),HisForward(false),HasData(false),DataSize(0){}
+ Payload(Payload &p):hint(p.hint),duration(p.duration),HasHint(p.HasHint),DedicatedMailbox(p.DedicatedMailbox),HisForward(p.HisForward),HasData(p.HasData),DataSize(p.DataSize){}
double hint; // The timestamp that should be used by the receiver
double duration; // The duration that should be used by the receiver
bool HasHint;
bool HasData;
bool HisForward;
+ u32 DataSize;
std::string DedicatedMailbox; // Dedicated mailbox used by the sender/receiver
};
@@ -120,7 +122,6 @@ static void obs_node(std::vector<std::string> args) {
XBT_INFO("Deploying observation node %s",CNAME);
// Starting node
- bool isObserver=false;
u32 nWakeUp=0;
u32 nDataRcv=0;
u32 nSendFail=0;
@@ -128,8 +129,9 @@ static void obs_node(std::vector<std::string> args) {
u32 nSend=0;
u32 hint_added=0;
double totalUptime=0;
- Payload *hint_forward=NULL;
- bool init_issender=i.is_sender;
+ Payload *hint_forward=NULL; // Contains the hint to forward
+ bool is_sender=i.is_sender; // This variable might change if all receiver have received the data
+ bool isObserver=false;
while(i.ShouldContinue()){
// Start by sleeping
XBT_INFO("%s is spleeping",CNAME);
@@ -139,82 +141,82 @@ static void obs_node(std::vector<std::string> args) {
XBT_INFO("%s wakes up",CNAME);
// Doing wake up stuff
- double uptime=i.GetDuration();
- double upsince=simgrid::s4u::Engine::get_clock();
- double upuntil=i.GetTS()+i.GetDuration();
- bool forward_mode=false;
- bool onlyf=false;
- bool sendhint_mode=false;
+ double uptime=i.GetDuration(); // Store the remaining wake up duration (updated during the node uptime)
+ double upsince=CLOCK; // Store the time at which the node woke up
+ double upuntil=i.GetTS()+i.GetDuration(); // Store the time at which the node should sleep
+ bool forward_mode=false; // Turned on and off every x seconds by the receiver (to switch between forward hint mode and receiving data mode)
+ bool forward_only=false; // When observer receive a hint it switch to forward only up to the next wake up time
+ bool sendhint_mode=false; // Turned on and off every x seconds by the sender (to switch between send hint and send data)
while(uptime>0)
{
- if(i.is_sender){
- double try_for=sendhint_mode ? 0.3 : 1;
- try_for=try_for>uptime ? uptime : try_for; // Ensure we do not exceed uptime
- try_for=(i.use_hint && i.HasNext()) ? try_for: uptime; // Ensure we shoud use hint otherwise only send data
- // Send hint
+ // ---------- SENDER ----------
+ if(is_sender){
+ // Send hint if send hint mode is enable
if(i.use_hint && sendhint_mode && i.HasNext()){
Payload *p=new Payload();
- p->DedicatedMailbox="hintmailbox"+selfName;
+ p->DedicatedMailbox="hintmailbox"+selfName; // Use a dedicated mailbox
p->HasHint=true;
p->duration=i.GetNextDuration();
p->hint=i.GetNextTS();
+ p->DataSize=i.hint_size;
try {
- TRACK_UPTIME(m->put(p,0,try_for));
+ TRACK_UPTIME(m->put(p,0,FOR(0.3))); // Init connection with a receiver
simgrid::s4u::Mailbox *m_ded= simgrid::s4u::Mailbox::by_name(p->DedicatedMailbox);
MODE_TX();
- SEND(m_ded->put(p,i.hint_size,uptime));
+ SEND(m_ded->put(p,p->DataSize,uptime)); // Send the actual hint
XBT_INFO("%s sent a hint successfully",CNAME);
}
catch(...){}
}
- else{ // Send data
+ // Send data if send hint mode is disable
+ else{
Payload *p=new Payload();
p->DedicatedMailbox="datamailbox"+selfName;
p->HasData=true;
p->HasHint=false;
+ p->DataSize=i.data_size;
+ // Add hint to the data if possible
if(i.use_hint && i.HasNext()){
p->HasHint=true;
p->duration=i.GetNextDuration();
p->hint=i.GetNextTS();
+ p->DataSize+=i.hint_size; // Don't forget!!
}
+ // Send the data
try {
- TRACK_UPTIME(m->put(p,0,try_for));
+ TRACK_UPTIME(m->put(p,0,FOR(1)));
simgrid::s4u::Mailbox *m_ded= simgrid::s4u::Mailbox::by_name(p->DedicatedMailbox);
MODE_TX();
if(i.extended){
- SEND(m_ded->put(p,i.data_size));
+ SEND(m_ded->put(p,p->DataSize));
}
else{
- SEND(m_ded->put(p,i.data_size,uptime));
+ SEND(m_ded->put(p,p->DataSize,uptime));
}
XBT_INFO("%s sent data successfully",CNAME);
nSend++;
- i.is_sender=(nSend<(i.n_nodes-1));
- isObserver=!i.is_sender;
+ is_sender=(nSend<(i.n_nodes-1)); // Stop sending if all nodes received
+ isObserver=!is_sender; // Switch to observer mode if all nodes received the data
}
catch(...){}
}
- sendhint_mode=!sendhint_mode; // Switch to send hint mode
+ sendhint_mode=!sendhint_mode; // Switch back and forth between sending hint and data
}
+ // ---------- RECEIVER ----------
else if(!isObserver){
- // Here we try to forward hint for 1 sec and try to receive data for 5 secs
- double try_for=forward_mode ? 0.3 : 1;
- try_for=try_for>uptime ? uptime : try_for; // Ensure we do not exceed uptime
// Forward hint mode
if(forward_mode){
if(hint_forward!=NULL && CLOCK < hint_forward->hint){
- FORWARD_HINT(try_for); // Try forward for 5 seconds then switch to received mode
+ FORWARD_HINT(FOR(0.3)); // Try forward for 5 seconds then switch to received mode
}
}
else { // Receiving mode
Payload *p; // Received data
- Payload *hint; // To Save the received hint
- bool hintReceived=false; // In case of error during data rx this will be use to check if we could use the *hint Payload object
try {
// Get the instantaneous message
do {
- TRACK_UPTIME(p=m->get<Payload>(try_for));
+ TRACK_UPTIME(p=m->get<Payload>(FOR(1)));
if(p->HisForward){
if(hint_forward==NULL || (hint_forward !=NULL && p->hint!=hint_forward->hint)){
simgrid::s4u::Mailbox *m_ded=simgrid::s4u::Mailbox::by_name(p->DedicatedMailbox);
@@ -236,9 +238,7 @@ static void obs_node(std::vector<std::string> args) {
if(p->HasHint && !p->HasData){
TRACK_UPTIME(p=m_ded->get<Payload>(uptime));
XBT_INFO("%s received a hint successfully",CNAME);
- hint=new Payload(*p); // Save hint
hint_forward=new Payload(*p); // Enable hint forwarding
- hintReceived=true;
if(CLOCK < p->hint){
i.AddEvent(p->hint, p->duration);
hint_forward=new Payload(*p);
@@ -255,55 +255,45 @@ static void obs_node(std::vector<std::string> args) {
XBT_INFO("%s received data successfully",CNAME);
if(p->HasHint){
XBT_INFO("%s received a hint along with data successfully",CNAME);
- hint=new Payload(*p); // Save hint
hint_forward=new Payload(*p); // Enable hint forwarding
- hintReceived=true;
}
nDataRcv++;
isObserver=true;
- i.is_sender=false;
+ is_sender=false;
}
}catch(...){
XBT_INFO("%s could not receive any data",CNAME);
nRcvFail++;
- if(hintReceived){
- i.AddEvent(hint->hint, hint->duration); // Add the hint to the event list
- hint_added++;
- }
}
}
forward_mode=!forward_mode; // Toggle mode (go back and forth between receiving and forwarding)
}
+ // ---------- OBSERVER ----------
else {
XBT_INFO("%s is observing his environment...",CNAME);
MODE_ON();
if(i.use_hint){
- double try_for=forward_mode ? 1 : 1;
- try_for=try_for>uptime ? uptime : try_for;
- if(onlyf)
- forward_mode=true;
- if(forward_mode && hint_forward!=NULL && CLOCK < hint_forward->hint){
- FORWARD_HINT(try_for);
+ if((forward_mode|forward_only) && hint_forward!=NULL && CLOCK < hint_forward->hint){
+ FORWARD_HINT(FOR(1));
}
else {
Payload *p;
try {
do {
- TRACK_UPTIME(p=m->get<Payload>(try_for));
+ TRACK_UPTIME(p=m->get<Payload>(FOR(1)));
} while(p->HisForward); // Ignore forwarded hint
simgrid::s4u::Mailbox *m_ded= simgrid::s4u::Mailbox::by_name(p->DedicatedMailbox);
// Start receiving hint from sender
- onlyf=true;
+ forward_only=true;
MODE_RX();
if(p->HasHint){
TRACK_UPTIME(p=m_ded->get<Payload>(uptime));
XBT_INFO("%s received a hint successfully",CNAME);
hint_forward=new Payload(*p); // Enable hint forwarding
- //simgrid::s4u::this_actor::sleep_for(uptime); // Now sleep until the end
}
else {
- simgrid::s4u::this_actor::sleep_for(try_for);
+ simgrid::s4u::this_actor::sleep_for(FOR(1));
}
}
catch(...){
@@ -325,5 +315,5 @@ static void obs_node(std::vector<std::string> args) {
}
// Done
MODE_OFF()
- XBT_INFO("Observation node %s finished [LOG2PARSE](node:%s|isSender:%d|nSend:%d|nWakeUp:%d|nDataRcv:%d|nSendFail:%d|nRcvFail:%d|totalUptime:%f|seed:%d|hint_added:%d)",CNAME,CNAME,init_issender,nSend,nWakeUp,nDataRcv,nSendFail,nRcvFail,totalUptime,i.seed,hint_added);
+ XBT_INFO("Observation node %s finished [LOG2PARSE](node:%s|isSender:%d|nSend:%d|nWakeUp:%d|nDataRcv:%d|nSendFail:%d|nRcvFail:%d|totalUptime:%f|seed:%d|hint_added:%d)",CNAME,CNAME,i.is_sender,nSend,nWakeUp,nDataRcv,nSendFail,nRcvFail,totalUptime,i.seed,hint_added);
} \ No newline at end of file