-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdomain_map_v0.hpp
402 lines (360 loc) · 15.8 KB
/
domain_map_v0.hpp
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#include <hpx/include/async.hpp>
#include <hpx/include/dataflow.hpp>
#include <hpx/include/components.hpp>
#include <hpx/include/actions.hpp>
#include <hpx/include/parallel_set_operations.hpp>
#include <hpx/include/parallel_is_sorted.hpp>
#include <hpx/include/parallel_sort.hpp>
#include <hpx/include/parallel_find.hpp>
#include <boost/atomic.hpp>
#include <vector>
#include <initializer_list>
namespace domain_map
{
namespace detail
{
template<typename T>
struct local_domain_descriptor : hpx::components::simple_component_base<local_domain_descriptor<T> >
{
// local index_sets on each locality
std::vector<T> local_index_set;
std::size_t index_size;
// I think it could be good to have map like this
// local_index_set < ---- > data
//
public:
local_domain_descriptor() {} noexcept;
// functions
/*
query which locality
*/
local_domain_descriptor(const std::initializer_list<T>& to_assign)
:
local_index_set(to_assign) ,
index_size(to_assign.size() {}
/* local_domain_descriptor(std::initializer_list<T> && to_assign)
:
local_index_set(to_assign) ,
index_size(to_assign.size()) {}
*/
local_domain_descriptor(const std::vector<T>& to_assign)
:
local_index_set(to_assign) ,
index_size(to_assign.size()) {}
/////////////////////////
// accessors
bool fetch_indexes(T const& index_)
{
local_index_set.emplace_back(index);
index_size ++;
return true;
}
bool remove_index(const T & index_)
{
using namespace hpx::parallel;
auto fut_remove_index = hpx::parallel::find(par(task),
local_index_set.begin(),local_index_set.end(),index_);
auto remove_index = fut_remove_index.get();
if( remove_index != local_index_set.end())
{
local_index_set.erase(remove_index);
index_size --;
return true;
}
return false;
}
bool remove_index(T && index_)
{
using namespace hpx::parallel;
auto fut_remove_index = hpx::parallel::find(par(task),
local_index_set.begin(),local_index_set.end(),std::move(index_));
auto remove_index = fut_remove_index.get();
if( remove_index != local_index_set.end())
{
local_index_set.erase(remove_index);
index_size --;
return true;
}
return false;
}
bool add_index(const T& value)
{
local_index_set.emplace_back(value);
return true;
}
bool add_index(T&& value)
{
local_index_set.emplace_back(std::move(value));
return true;
}
HPX_DEFINE_COMPONENT_ACTION(local_domain_descriptor, fetch_indexes);
HPX_DEFINE_COMPONENT_ACTION(local_domain_descriptor, remove_index);
HPX_DEFINE_COMPONENT_ACTION(local_domain_descriptor, add_index);
};
template<>
struct local_domain_descriptor<arithmetic> : hpx::components::simple_component_base<
local_domain_descriptor<arithmetic> >
{
};
}
using namespace detail;
using base_type = hpx::components::simple_component_base<local_domain_descriptor<T> >
HPX_REGISTER_COMPONENT(base_type, local_domain_descriptor);
HPX_REGISTER_ACTION(base_type::fetch_indexes);
HPX_REGISTER_ACTION(base_type::remove_index);
HPX_REGISTER_ACTION(base_type::add_index);
template<typename T>
struct global_domain_map : simple_component_base<global_domain_map<T> >
{
// Non-specialization of non-arithmetic domains
public:
typedef T value_type;
typedef T* pointer;
typedef const T* const pointer;
typedef T& reference;
typedef const T& const_reference;
typedef unsigned int size_type;
// iterators stuff's
public:
// Constructor
global_domain_map() : localities_(hpx::find_here()) {}
global_domain_map(std::initializer_list<T>& tem ) :
index_set(tem) ,
index_size(tem.size()),
localities_(hpx::find_here()) {}
global_domain_map(std::initializer_list<T>&& tem) :
index_set(std::move(tem)),
index_size(tem.size()),
localities_(hpx::find_here()) {}
global_domain_map(const global_domain_map& other) :
localities_(other.localities_),
index_set(other.index_set),
index_size(other.index_size) {}
global_domain_map(global_domain_map&& other) :
localities_(std::move(other.localities_)),
index_set(std::move(other.index_set)),
index_size(std::move(other.index_size)) {}
~global_domain_map();
global_domain_map& operator= (const global_domain_map& other)
{
localities_ = other.localities_;
index_set = other.index_set;
localities_ = other.localities_;
return *this;
}
global_domain_map& operator= (global_domain_map&& other)
{
localities_ = std::move(other.localities_);
index_set = std::move(other.index_set);
localities_ = std::move(other.localities_);
return *this;
}
// accessors
size_type size() const noexcept
{
return index_set.size();
}
bool empty() const noexcept
{
if(index_set.size() == 0)
return true;
else
return false;
}
void clear() noexcept
{
localities_.clear();
index_set.clear();
index_size = 0;
}
//Distributive functions
/* do_cyclic() const
{
for(std::size_t i = 0; i < index_size; i++ )
{
for(auto const& id : localities_)
components_ids.emplace_back(hpx::new_<detail::local_domain_descriptor<T> >(id,index_set[i]));
}
}
*/
/*
It needs working
*/
void do_cyclic() const
{
boost::atomic<std::size_t> inc(0);
boost::atomic<std:;size_t> count(0);
if(index_size >= localities_.size())
{
for(auto const& id : localities_)
{
components_ids.emplace_back(hpx::new_<detail::local_domain_descriptor<T> >(id,index_set[inc]));
inc++; count++;
}
}
else
{
for(auto const& index : index_set)
{
components_ids.emplace_back(hpx::new_<detail::local_domain_descriptor<T> >(localities_[inc],index));
count++;
}
}
}
/*do_cyclic_block(std::size_t block_size) const
{
HPX_ASSERT(block_size < index_size);
for(std::size_t j = 0; j < index_size; j++)
{
for(auto const& id : localities_ )
{
}
}
}
*/
/*
do_block(hpx::id_type loc_ = localities_[0]) const
{
= hpx::new_<detail::local_domain_descriptor<T> >(loc_,index_set);
}
*/
//this mapping function will work only for cyclic -Distribution policy
// This fuction will be removed
hpx::id_type mapping(T& index) const
{
using namespace hpx::parallel;
auto it = hpx::parallel::find(par(task),index_set.begin(),index_set.end(),index);
auto position = it.then([](auto i)
{
return std::distance(index_set.begin() - i.get());
});
if(position <= localities_.size())
return components_ids[position];
else
{
std::size_t pos = position % localities_.size();
return components_ids[pos];
}
}
bool operator-= (const T& value)
{
using namespace hpx::parallel;
auto fut_remove_index = hpx::parallel::find(par(task),
index_set.begin(),index_set.end(),value);
auto remove_index = fut_remove_index.get();
if( remove_index != index_set.end())
{
hpx::id_type gid_ = mapping(value);
auto end = hpx::async<remove_index_action>(gid,value);
index_set.erase(remove_index);
index_size --;
if(end.get() == false)
return false;
else
return true;
}
return false;
}
bool operator-= (T&& value)
{
using namespace hpx::parallel;
auto fut_remove_index = hpx::parallel::find(par(task),
index_set.begin(),index_set.end(),std::move(value));
auto remove_index = fut_remove_index.get();
if( remove_index != index_set.end())
{
hpx::id_type gid_ = mapping(value);
auto end = hpx::async<remove_index_action>(gid,std::forward<T>(value));
index_set.erase(remove_index);
index_size --;
if(end.get() == false)
return false;
else
return true;
}
return false;
}
void operator+= (const T& value)
{
using namespace hpx::parallel;
auto available = hpx::parallel::find(par(task),
index_set.begin(),index_set.end(),value);
HPX_ASSERT(available.get() == index_set.end(),
"Duplicates not Allowed in Associative domains");
auto end = hpx::async<add_index_action>(components_ids.back(),value);
index_set.emplace_back(value);
end.get();
}
void operator+= (T&& value)
{
using namespace hpx::parallel;
auto available = hpx::parallel::find(par(task),
index_set.begin(),index_set.end(),std::move(value));
HPX_ASSERT(available.get() == index_set.end(),
"Duplicates not Allowed in Associative domains");
auto end = hpx::async<add_index_action>(components_ids.back(),std::forward<T>(value));
index_set.emplace_back(std::move(value));
end.get();
}
//Set-Associative Operations only support on non-const domains
domain | operator(domain& other)
{
using namespace hpx::parallel;
hpx::future<bool> res = hpx::parallel::is_sorted(par(task),
other.index_set.begin(), other.index_set.end());
auto te = res.then([other](hpx::future<bool> f1)
{
if(f1.get() != true){
auto f = hpx::parallel::sort(par(task),
other.index_set.begin(),other.index_set.end()).get();
return hpx::make_ready_future(1);
}
else
return hpx::make_ready_future(1);
}
);
hpx::future<bool> res_t = hpx::parallel::is_sorted(par(task),
index_set.begin(),index_set.end());
auto te_t = res_t.then([this](hpx::future<bool> f2)
{
if(f2.get() != true){
auto f = hpx::parallel::sort(par(task),
index_set.begin(),index_set.end()).get();
return hpx::make_ready_future(1);
}
else
return hpx::make_ready_future(1);
}
);
return hpx::dataflow(hpx::launch::sync,
[this,other] (auto &&value_1, auto &&value_2) -> domain
{
using namespace hpx::parallel;
std::vector<T> dest;
auto temp = hpx::parallel::set_union(par(task),
this->index_set.begin(),this->index_set.end(),
other.index_set.begin(),other.index_set.end(),
dest.begin()); // check correctness
return temp.then([&dest](auto && tem)
{
return global_domain_map(dest);
}
);
},std::move(te), std::move(te_t)
);}
private:
std::vector<hpx::id_type> localities_;
std::vector<T> index_set;
std::size_t index_size;
std::vector<hpx::future<hpx::id_type> > components_ids;
global_domain_map(const std::vector<T>& to_construct)
:
index_set(to_construct),
index_size(to_construct.size()),
localities_(hpx::find_here()) {}
};
template<>
struct global_domain_map<arithmetic> : simple_component_base<global_domain_map<arithmetic> >
{
// specialization for arithmetic domains
};