This repository was archived by the owner on Aug 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMWTask.h
220 lines (170 loc) · 4.6 KB
/
MWTask.h
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//
// MWTask.h
//
#ifndef __MWTask_h
#define __MWTask_h
#include "MWAbstractDriver.h"
#include "MWWorkerID.h"
#include "MWGroup.h"
//
// Forward declaration
//
template <class CommType>
class MWTask;
/** This class represents a unit of work for a MWWorker.
The task consits of two main components. The "work" to be done,
and the "result" of the completed task. In order to create an
application, the user must specify methods for packing and unpacking
both the "work" and "result" portions of the task.
When the task is being serviced by a worker, it contains a link
to the ID of that instance of the worker.
@see MWAbstractDriver
@see MWWorker
@author Mike Yoder, modified by Jeff Linderoth and Jean-Pierre Goux
*/
template <typename CommType>
class MWTask
{
friend class MWAbstractDriver<CommType>;
public:
///
typedef enum { NORMAL=0, NWS=1, NUMTASKTYPES=2 } TaskType;
/// Default constructor
MWTask(MWAbstractDriver<CommType>* master_, CommType* RMC_);
/// Default Destructor
virtual ~MWTask();
/// The task's number
int number;
/// The task's type.
TaskType taskType;
/** @name Time and usage data */
//@{
/** The time (wall clock) that it takes to run the 'execute_task'
function on the worker's side. */
double working_time;
/** The amount of user+system time taken by this task, measured
from start to finish of the 'execute_task' function. */
double cpu_time;
/// A status flag. If true, then this task is finished.
bool status;
//@}
/**@name Packing and Unpacking
The user must pack and unpack the contents of the class
so that it can be sent to the worker and back. These
functions will make RMC->pack() and RMC->unpack() calls.
*/
//@{
/// Pack the work portion of the task
virtual void pack_work( void ) = 0;
/// Unpack the work portion of the task
virtual void unpack_work( void ) = 0;
/// Pack the result portion of the task
virtual void pack_results( void ) = 0;
/// Unpack the result portion of the task
virtual void unpack_results( void ) = 0;
/// Dump this task to the screen
virtual void printself( int level = 60 )
{MWprintf ( level, " Task %d\n", number);}
//@}
/**@name Checkpointing Utilities
These two functions are used when checkpointing. Simply put,
they write/read the state of the task to this file pointer
provided.
*/
//@{
/// Write the state of this task out to an output stream
virtual void write_ckpt_info(ostream& os) {};
/** Read the state of this task from an input stream (overwriting
any existing state */
virtual void read_ckpt_info(istream& is) {};
///
void write_group_info (ostream& os);
///
void read_group_info (istream& is);
//@}
///
void initGroups ( int num )
{group.init(num);}
///
void addGroup ( int num );
///
void deleteGroup ( int num );
///
bool doesBelong ( int num )
{ return group.belong ( num ); }
///
MWGroup& getGroup ( )
{ return group; }
///
MWGroup group;
///
MWAbstractDriver<CommType>* master;
/// A pointer to the worker ID executing this task (NULL if none)
MWWorkerID<CommType> *worker;
///
CommType* RMC;
};
/**
template <class CommType>
bool less<MWTask<CommType>*>::operator()(const MWTask<CommType>*& __x, const MWTask<CommType>*& __y) const
{
return __x->number < __y->number;
}
**/
template <class CommType>
MWTask<CommType>::MWTask(MWAbstractDriver<CommType>* master_, CommType* RMC_)
: number(-1),
taskType(NORMAL),
working_time(0.0),
cpu_time(0.0),
status(false),
group(1),
worker(NULL),
RMC(RMC_)
{
assert(RMC_ != 0);
if (master)
initGroups(master_->workGroupTaskNum.size());
else
initGroups(0);
}
template <class CommType>
MWTask<CommType>::~MWTask()
{
if ( worker )
if ( worker->runningtask )
worker->runningtask = NULL;
}
template <class CommType>
void MWTask<CommType>::addGroup ( int num )
{
if ( doesBelong ( num ) )
return;
if (master)
master->workGroupTaskNum[num]++;
group.join( num );
}
template <class CommType>
void MWTask<CommType>::deleteGroup ( int num )
{
if ( !doesBelong ( num ) )
return;
if (master)
master->workGroupTaskNum[num]--;
group.leave ( num );
}
template <class CommType>
void MWTask<CommType>::write_group_info (ostream& os)
{ group.write_checkpoint ( os ); }
template <class CommType>
void MWTask<CommType>::read_group_info (istream& is )
{
int num = (master ? master->workGroupTaskNum.size() : 1);
initGroups(num);
group.read_checkpoint ( is );
if (master)
for ( int i = 0; i < num; i++ )
if ( doesBelong ( i ) )
master->workGroupTaskNum[num]++;
}
#endif