summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/examples/FaultTolerance/FLARe/DeCoRAM/src/Algorithms.cpp
blob: 3462fc5f81016297115e74a84ed297c536f6064e (plain)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// -*- C++ -*-

//=============================================================================
/**
 *  @file    Algorithms.cpp
 *
 *  $Id$
 *
 *  @author  Friedhelm Wolf (fwolf@dre.vanderbilt.edu)
 */
//=============================================================================

#include <iostream>
#include <sstream>
#include "Algorithms.h"

unsigned int extract_rank (const Taskname & taskname)
{
  unsigned int rank = 0;
  size_t first_underscore = taskname.find_first_of ("_");

  if (first_underscore != std::string::npos)
    {
      std::string rankstring = taskname.substr (first_underscore + 1);

      if (!rankstring.empty ())
        {
          rank = atoi (rankstring.c_str ());
        }
    }

  return rank;
}

PROCESSOR_LIST create_processors (unsigned long count)
{
  PROCESSOR_LIST procs;

  for (unsigned long i = 1; i <= count; ++i)
    {
      std::stringstream ss;
      ss << "P";
      if (i < 10)
        ss << "00";
      else if (i < 100)
        ss << "0";

      ss << i;
      procs.push_back (ss.str ());
    }

  return procs;
}

FTRMFF_Algorithm::~FTRMFF_Algorithm ()
{
}

CTT_Algorithm::~CTT_Algorithm ()
{
}

std::ostream & operator<< (std::ostream & ostr, const Task & t)
{
  ostr << "T["
       << t.name
       << "]("
       << t.execution_time
       << ","
       << t.period
       << ","
       << t.sync_time
       << (t.role == PRIMARY ? ",p" : ",b")
       << ")";

  return ostr;
}

std::ostream & operator<< (std::ostream & ostr, const TASK_LIST & t)
{
  if (t.empty ())
    ostr << "empty";

  for (TASK_LIST::const_iterator it = t.begin ();
       it != t.end ();
       ++it)
    {
      ostr << *it << " ";
    }

  return ostr;
}

std::ostream & operator<< (std::ostream & ostr, const PROCESSOR_LIST & p)
{
  if (p.empty ())
    ostr << "empty";

  for (PROCESSOR_LIST::const_iterator it = p.begin ();
       it != p.end ();
       ++it)
    {
      ostr << *it << " ";
    }

  return ostr;
}

std::ostream & operator<< (std::ostream & ostr, 
                           const SCHEDULE_PROGRESS_LIST & pl)
{
  for (SCHEDULE_PROGRESS_LIST::const_iterator it = 
         pl.begin ();
       it != pl.end ();
       ++it)
    {
      ostr << it->scheduled_replicas << "x" << it->task << " ";
    }

  return ostr;
}

Task
string_to_task::operator () (const std::string & s)
{
  // this method parses a string of the format:
  // T[<taskname](execution_time,period,synchronization_time)

  std::string::size_type first_comma = s.find_first_of (',');
  std::string::size_type second_comma = s.find (',', first_comma + 1);

  std::stringstream ss_ext (s.substr (s.find_first_of ('(') + 1, 
				      first_comma - 2));
  double execution_time = .0;
  ss_ext >> execution_time;

  std::stringstream ss_period (s.substr (first_comma + 1, 
                                         second_comma - 2));
  double period = .0;
  ss_period >> period;

  std::stringstream ss_synctime (s.substr (second_comma + 1,
                                           s.find_first_of (')') - 2));

  double synctime = .0;
  ss_synctime >> synctime;

  Task t;
  t.name = s.substr (s.find_first_of ('[') + 1, s.find_first_of (']') - 2);
  t.execution_time = execution_time;
  t.period = period;
  t.sync_time = synctime;
  t.role = PRIMARY;
  t.rank = 0;
  
  if (t.name.find ('_') != std::string::npos)
    {
      t.role = BACKUP;
      std::stringstream ss (t.name.substr (t.name.find ('_') + 1));
      ss >> t.rank;
    }

  return t;
}

bool
double_equal::operator () (double d1, double d2)
{
  if (d1 < d2)
    return (d2 - d1) < 0.00001;
  else
    return (d1 - d2) < 0.00001;
}

TaskNamePredicate::TaskNamePredicate (const std::string & task_name)
  : task_name_ (task_name)
{
}

bool
TaskNamePredicate::operator () (const Task & task)
{
  return task_name_.compare (task.name) == 0;
}

Taskname primary_name (const Task & task)
{
  return task.name.substr (0, task.name.find ('_'));
}

Task
PrimaryConversion::operator () (const Task & task)
{
  Task t = task;
  t.role = PRIMARY;

  return t;
}