-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtramNonSmp.h
297 lines (227 loc) · 7.93 KB
/
tramNonSmp.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#ifndef __TRAM_NON_SMP_H__
#define __TRAM_NON_SMP_H__
#include "tramNonSmp.decl.h"
template <typename T, typename SingletonClass>
class singleton
{
public:
using value_type = T;
using class_type = SingletonClass;
// Non-copyable, non-movable
singleton(singleton const&) = delete;
singleton(singleton&&) = delete;
singleton& operator=(singleton const&) = delete;
singleton& operator=(singleton&&) = delete;
static const std::unique_ptr<value_type>& instance()
{
static std::unique_ptr<value_type> inst{new value_type()};
return inst;
}
protected:
singleton() = default;
};
#define TRAM_GENERATE_SINGLETON(type, name) \
class name : public singleton<type, name> \
{ \
private: \
name() = default; \
}
#define TRAM_ACCESS_SINGLETON(name) (*name::instance())
using buffer_t = int;
TRAM_GENERATE_SINGLETON(buffer_t, payload_buffer_size);
TRAM_GENERATE_SINGLETON(double, flush_timer);
template <typename T>
struct tramNonSmpMsg : public CMessage_tramNonSmpMsg<T> {
using value_type = T;
tramNonSmpMsg() : next(0) {}
tramNonSmpMsg(int size, value_type* buf) : next(size) {
std::copy(buf, buf + size, payload_buffer);
}
int next;
value_type* payload_buffer;
};
template <typename T>
tramNonSmpMsg<T>* make_tram_msg(int size_) {
auto* msg = new (&size_) tramNonSmpMsg<T>();
return msg;
}
template <typename T>
tramNonSmpMsg<T>* make_tram_msg(int size_, tramNonSmp<T>* buffer_) {
auto* msg = new (&size_) tramNonSmpMsg<T>(size_, buffer_);
return msg;
}
template <typename T>
class tramNonSmp : public CBase_tramNonSmp<T> {
private:
using value_type = T;
using function_ptr = void (*)(void*, value_type);
using buff_function_ptr = void(*)(void*, tramNonSmpMsg<value_type>*);
function_ptr func_ptr;
buff_function_ptr buff_func_ptr;
void* obj_ptr;
tramNonSmpMsg<value_type> **msgBuffers;
bool enable_flushing;
double time_to_flush;
int flush_counter = -1;
bool is_itemized;
public:
tramNonSmp(CkMigrateMessage* msg);
tramNonSmp();
tramNonSmp(int);
tramNonSmp(int, double);
tramNonSmp(CkGroupID, int, bool, double, bool);
// Locally accessed function
void set_func_ptr(function_ptr fptr, void* optr);
void set_buffered_func_ptr(buff_function_ptr fptr, void* optr);
void set_itemized(bool value);
void register_flush();
// Entry methods
void insertValue(value_type const& value, int dest_pe);
void tflush();
void periodic_flush();
void timed_flush();
void receive(tramNonSmpMsg<value_type>* msg);
void num_flushes();
};
template <typename T>
tramNonSmp<T>::tramNonSmp(CkMigrateMessage* msg) {}
template <typename T>
tramNonSmp<T>::tramNonSmp()
: func_ptr(nullptr)
, obj_ptr(nullptr)
, is_itemized(true)
, enable_flushing(false)
, time_to_flush(std::numeric_limits<double>::max()) {
buffer_t& buffer_size = TRAM_ACCESS_SINGLETON(payload_buffer_size);
buffer_size = 1024;
// Question: Does this also needs to be double pointer? I think not.
msgBuffers = new tramNonSmpMsg<value_type>*[CkNumPes()];
for (int i = 0; i != CkNumPes(); ++i)
msgBuffers[i] = make_tram_msg<value_type>(buffer_size);
}
template <typename T>
tramNonSmp<T>::tramNonSmp(int buffer_size_)
: func_ptr(nullptr)
, obj_ptr(nullptr)
, is_itemized(true)
, enable_flushing(false)
, time_to_flush(std::numeric_limits<double>::max()) {
buffer_t& buffer_size = TRAM_ACCESS_SINGLETON(payload_buffer_size);
buffer_size = buffer_size_;
// Question: Does this also needs to be double pointer? I think not.
msgBuffers = new tramNonSmpMsg<value_type>*[CkNumPes()];
for (int i = 0; i != CkNumPes(); ++i)
msgBuffers[i] = make_tram_msg<value_type>(buffer_size);
}
template <typename T>
tramNonSmp<T>::tramNonSmp(int buffer_size_, double time_in_ms)
: func_ptr(nullptr)
, obj_ptr(nullptr)
, is_itemized(true)
, enable_flushing(true) {
buffer_t& buffer_size = TRAM_ACCESS_SINGLETON(payload_buffer_size);
buffer_size = buffer_size_;
time_to_flush = time_in_ms;
register_flush();
// CcdCallFnAfter()
// Question: Does this also needs to be double pointer? I think not.
msgBuffers = new tramNonSmpMsg<value_type>*[CkNumPes()];
for (int i = 0; i != CkNumPes(); ++i)
msgBuffers[i] = make_tram_msg<value_type>(buffer_size);
}
template <typename T>
tramNonSmp<T>::tramNonSmp(CkGroupID gid, int buffer_size_, bool enable_buffer_flushing, double time_in_ms, bool ret_item)
: func_ptr(nullptr)
, obj_ptr(nullptr)
, is_itemized(true)
, enable_flushing(enable_buffer_flushing)
, time_to_flush(std::numeric_limits<double>::max()) {
buffer_t& buffer_size = TRAM_ACCESS_SINGLETON(payload_buffer_size);
buffer_size = buffer_size_;
if (enable_flushing) {
time_to_flush = time_in_ms;
register_flush();
}
// CcdCallFnAfter()
// Question: Does this also needs to be double pointer? I think not.
msgBuffers = new tramNonSmpMsg<value_type>*[CkNumPes()];
for (int i = 0; i != CkNumPes(); ++i)
msgBuffers[i] = make_tram_msg<value_type>(buffer_size);
// contribute(start_cb);
}
template <typename T>
void periodic_progress(void *htram_obj, double time) {
tramNonSmp<T> *proper_obj = static_cast<tramNonSmp<T>*>(htram_obj);
proper_obj->periodic_flush();
proper_obj->register_flush();
}
template <typename T>
void tramNonSmp<T>::num_flushes() {
ckout << "[PE:" << CkMyPe() << "] Flush Counter: " << flush_counter << endl;
}
template <typename T>
void tramNonSmp<T>::register_flush() {
CcdCallFnAfter(periodic_progress<T>, (void *) this, time_to_flush);
// TODO: Count the number of flushes here
++flush_counter;
}
template <typename T>
void tramNonSmp<T>::set_func_ptr(function_ptr fptr, void* optr) {
func_ptr = fptr;
obj_ptr = optr;
}
template <typename T>
void tramNonSmp<T>::set_buffered_func_ptr(buff_function_ptr fptr, void* optr) {
buff_func_ptr = fptr;
obj_ptr = optr;
}
template <typename T>
void tramNonSmp<T>::set_itemized(bool value) {
is_itemized = value;
}
template <typename T>
void tramNonSmp<T>::insertValue(value_type const& value, int dest_pe) {
// Buffer the message
tramNonSmpMsg<value_type>* destMsg = msgBuffers[dest_pe];
destMsg->payload_buffer[destMsg->next] = value;
destMsg->next++;
buffer_t& buffer_size = TRAM_ACCESS_SINGLETON(payload_buffer_size);
if (destMsg->next == TRAM_ACCESS_SINGLETON(payload_buffer_size)) {
// Flush message to destination PE if its filled
this->thisProxy[dest_pe].receive(destMsg);
msgBuffers[dest_pe] = make_tram_msg<value_type>(buffer_size);
}
}
template <typename T>
void tramNonSmp<T>::periodic_flush() {
buffer_t& buffer_size = TRAM_ACCESS_SINGLETON(payload_buffer_size);
for (int i = 0; i < CkNumPes(); ++i) {
if (msgBuffers[i]->next) {
this->thisProxy[i].receive(msgBuffers[i]);
msgBuffers[i] = make_tram_msg<value_type>(buffer_size);
}
}
}
template <typename T>
void tramNonSmp<T>::tflush() {
buffer_t& buffer_size = TRAM_ACCESS_SINGLETON(payload_buffer_size);
for (int i = 0; i < CkNumPes(); ++i) {
this->thisProxy[i].receive(msgBuffers[i]);
msgBuffers[i] = make_tram_msg<value_type>(buffer_size);
}
}
template <typename T>
void tramNonSmp<T>::receive(tramNonSmpMsg<T>* msg) {
// Call the callback function
int limit = msg->next;
if (is_itemized)
for (int i = 0; i != limit; ++i) {
func_ptr(obj_ptr, msg->payload_buffer[i]);
}
else
buff_func_ptr(obj_ptr, msg);
}
#define CK_TEMPLATES_ONLY
#include "tramNonSmp.def.h"
#undef CK_TEMPLATES_ONLY
#endif