16#include "fpmas/synchro/synchro.h"
20namespace fpmas {
namespace model {
41 template<
typename AgentType>
86 operator AgentType*()
const {
87 return static_cast<AgentType*
>(_agent->
get());
96 return static_cast<AgentType*
>(_agent->
get());
105 return *
static_cast<AgentType*
>(_agent->
get()); }
124 if(_agent ==
nullptr)
126 return static_cast<AgentType*
>(_agent->
get());
142 template<
typename AgentType,
typename Compare = std::less<AgentType>>
184 template<
typename AgentType>
224 template<
typename AgentType>
227 typedef typename std::vector<Neighbor<AgentType>>::iterator iterator;
228 typedef typename std::vector<Neighbor<AgentType>>::const_iterator const_iterator;
229 std::vector<Neighbor<AgentType>> neighbors;
240 : neighbors(neighbors) {}
248 return neighbors.begin();
256 return neighbors.begin();
265 return neighbors.end();
273 const_iterator
end()
const {
274 return neighbors.end();
283 return neighbors.size();
292 return neighbors.empty();
305 return neighbors.at(i);
306 }
catch(
const std::out_of_range&) {
316 return neighbors.at(i);
317 }
catch(
const std::out_of_range&) {
348 template<
typename Gen>
350 std::shuffle(neighbors.begin(), neighbors.end(), gen);
397 template<
typename Compare = std::less<AgentType>>
400 neighbors.begin(), neighbors.end(),
416 template<
typename Filter>
420 neighbors.begin(), neighbors.end(),
433 template<
typename Gen>
437 return neighbors.at(index(gen));
459 template<
typename AgentType>
462 std::vector<void(AgentType::*)()> _behaviors;
464 template<
typename T,
typename Enable=
void>
465 struct AutoAgentCast {
467 return dynamic_cast<T*
>(agent);
472 struct AutoAgentCast<T, typename std::enable_if<
473 std::is_base_of<api::model::Agent, T>::value
476 return static_cast<T*
>(agent);
531 template<
typename ...T>
533 : _behaviors({behaviors...}){
537 for(
auto behavior : this->_behaviors)
538 (AutoAgentCast<AgentType>::cast(agent)->*behavior)();
551 template<
typename AgentType,
typename ... Args>
554 void (AgentType::* behavior)(Args...);
555 std::tuple<Args...> args;
559 template<
int ...>
struct seq {};
560 template<
int N,
int ...S>
struct gens {
562 typedef typename gens<N-1, N-1, S...>::type type;
564 template<
int ...S>
struct gens<0, S...>{
typedef seq<S...> type; };
570 (
dynamic_cast<AgentType*
>(agent)->*behavior)(std::get<S>(args) ...);
582 void (AgentType::* behavior)(Args...),
584 : behavior(behavior), args(args...) {}
593 return call_fn_internal(agent,
typename gens<
sizeof...(Args)>::type());
701 template<
typename AgentInterface,
typename AgentType,
typename TypeIdBase = AgentType>
715 std::vector<api::model::GroupId> group_ids;
716 std::vector<api::model::AgentGroup*> _groups;
720 std::unordered_map<api::model::GroupId, api::model::AgentTask*> _tasks;
801 if(group_ids.size() > 0)
802 return group_ids.back();
808 std::vector<GroupId>
groupIds()
const override {
return group_ids;}
813 group_ids.push_back(
id);}
818 group_ids.push_back(
id);
825 group_ids.erase(std::remove(group_ids.begin(), group_ids.end(),
id));
832 if(_groups.size() > 0)
833 return _groups.back();
839 std::vector<const api::model::AgentGroup*>
groups()
const override {
840 return {_groups.begin(), _groups.end()};
850 std::vector<api::model::AgentGroup*>
groups()
override {
return _groups;}
856 _groups.push_back(
group);
862 _groups.push_back(
group);
869 _groups.erase(std::remove(_groups.begin(), _groups.end(),
group));
878 std::list<api::model::Agent*>::iterator pos
880 group_pos[gid] = pos;
887 return group_pos.find(gid)->second;
898 return new AgentType(*
static_cast<const AgentType*
>(
this));
906 *
static_cast<AgentType*
>(
this) = *
static_cast<const AgentType*
>(agent);
924 *
static_cast<AgentType*
>(
this) = std::move(*
static_cast<AgentType*
>(agent));
957 return _tasks.at(this->
groupId());
963 return _tasks.at(this->
groupId());
976 assert(_tasks.count(
id) > 0);
977 return _tasks.at(
id);
983 return _tasks.at(
id);}
995 const std::unordered_map<api::model::GroupId, api::model::AgentTask*>&
1005 virtual void act()
override {}
1021 std::vector<Neighbor<NeighborAgentType>> out;
1024 if(NeighborAgentType* neighbor =
dynamic_cast<NeighborAgentType*
>(_node->
data().get())) {
1025 out.push_back({&_node->
data(), _edge});
1045 std::vector<Neighbor<NeighborAgentType>> out;
1048 if(NeighborAgentType* neighbor =
dynamic_cast<NeighborAgentType*
>(_node->
data().get())) {
1049 out.push_back({&_node->
data(), _edge});
1070 std::vector<Neighbor<NeighborAgentType>> in;
1073 if(NeighborAgentType* neighbor =
dynamic_cast<NeighborAgentType*
>(_node->
data().get())) {
1074 in.push_back({&_node->
data(), _edge});
1094 std::vector<Neighbor<NeighborAgentType>> in;
1097 if(NeighborAgentType* neighbor =
dynamic_cast<NeighborAgentType*
>(_node->
data().get())) {
1098 in.push_back({&_node->
data(), _edge});
1111 template<
typename AgentInterface,
typename AgentType,
typename TypeIdBase>
1123 template<
typename AgentType,
typename TypeIdBase = AgentType>
1142 std::vector<api::model::Agent*> agents;
1160 agents.push_back(agent);
1182 return agents.size();
1240 std::size_t agent_count,
1288 std::size_t agent_count,
1313 template<
template<
typename>
class SyncMode>
1365 std::string file_format;
1366 std::fstream stream;
1374 ), std::ios_base::out);
1375 breakpoint.
dump(stream, model);
1402 std::string file_format,
1405 : file_format(file_format), breakpoint(breakpoint), model(model) {
1417namespace fpmas {
namespace io {
namespace json {
1515namespace fpmas {
namespace io {
namespace datapack {
1516 using api::model::AgentPtr;
1712namespace fpmas {
namespace synchro {
1758 std::set<api::model::GroupId> local_ids;
1759 for(
auto id : local_agent->groupIds())
1760 local_ids.insert(
id);
1761 std::set<api::model::GroupId> updated_ids;
1762 for(
auto id : updated_agent->groupIds())
1763 updated_ids.insert(
id);
1765 std::vector<api::model::GroupId> new_groups;
1766 for(
auto id : updated_ids)
1767 if(local_ids.count(
id) == 0)
1768 new_groups.push_back(
id);
1770 std::vector<api::model::GroupId> obsolete_groups;
1771 for(
auto id : local_ids)
1772 if(updated_ids.count(
id) == 0)
1773 obsolete_groups.push_back(
id);
1775 local_agent = std::move(updated_agent);
1781 for(
auto id : new_groups)
1782 local_agent->model()->getGroup(
id).add(local_agent.
get());
1783 for(
auto id : obsolete_groups)
1784 local_agent->model()->getGroup(
id).remove(local_agent.
get());
1800 struct adl_serializer<
fpmas::api::model::AgentPtr> {
Definition: communication.h:251
virtual int getRank() const =0
Definition: distributed_edge.h:91
Definition: distributed_graph.h:169
Definition: distributed_id.h:89
Definition: distributed_node.h:28
Definition: graph_builder.h:23
Definition: breakpoint.h:26
virtual void dump(std::ostream &stream, const T &object)=0
virtual GroupId groupId() const =0
virtual api::runtime::Runtime & runtime()=0
virtual api::communication::MpiCommunicator & getMpiCommunicator()=0
virtual Date currentDate() const =0
Definition: scheduler.h:135
T * get()
Definition: ptr_wrapper.h:58
Definition: clustered_graph_builder.h:349
Definition: graph_builder.h:23
Definition: random_load_balancing.h:27
Definition: ring_graph_builder.h:55
Definition: breakpoint.h:26
Definition: datapack.h:275
std::size_t nodeCount() override
Definition: model.h:1181
AgentNodeBuilder(api::model::AgentGroup &group)
Definition: model.h:1150
void push(api::model::Agent *agent)
Definition: model.h:1159
api::model::AgentNode * buildNode(api::graph::DistributedGraph< AgentPtr > &) override
Definition: model.cpp:22
AutoBreakpoint(std::string file_format, api::io::Breakpoint< api::model::Model > &breakpoint, api::model::Model &model)
Definition: model.h:1401
const api::scheduler::Job & job() const
Definition: model.h:1413
void execute(api::model::Agent *agent) const override
Definition: model.h:592
BehaviorWithArgs(void(AgentType::*behavior)(Args...), Args... args)
Definition: model.h:581
Behavior(T... behaviors)
Definition: model.h:532
void execute(api::model::Agent *agent) const
Definition: model.h:536
CompareNeighbors(const Compare &compare)
Definition: model.h:152
bool operator()(const Neighbor< AgentType > &n1, const Neighbor< AgentType > &n2) const
Definition: model.h:158
api::model::AgentNode * buildDistantNode(api::graph::DistributedId id, int location, api::graph::DistributedGraph< AgentPtr > &graph) override
Definition: model.cpp:67
api::model::AgentNode * buildNode(api::graph::DistributedGraph< AgentPtr > &graph) override
Definition: model.cpp:57
DistributedAgentNodeBuilder(api::model::AgentGroup &group, std::size_t agent_count, std::function< api::model::Agent *()> allocator, api::communication::MpiCommunicator &comm)
Definition: model.cpp:32
Neighbor(AgentPtr *agent, AgentEdge *edge)
Definition: model.h:80
AgentType * operator->() const
Definition: model.h:95
AgentEdge * edge() const
Definition: model.h:113
AgentType & operator*() const
Definition: model.h:104
AgentType * agent() const
Definition: model.h:123
Neighbor()
Definition: model.h:61
const Neighbor< AgentType > & at(std::size_t i) const
Definition: model.h:314
Neighbor< AgentType > & operator[](std::size_t i)
Definition: model.h:328
bool empty() const
Definition: model.h:291
Neighbors & shuffle(Gen &gen)
Definition: model.h:349
const_iterator end() const
Definition: model.h:273
iterator begin()
Definition: model.h:247
Neighbor< AgentType > random()
Definition: model.h:448
Neighbor< AgentType > random(Gen &gen)
Definition: model.h:434
std::size_t count() const
Definition: model.h:282
Neighbor< AgentType > & at(std::size_t i)
Definition: model.h:303
Neighbors & sort(Compare comp=Compare())
Definition: model.h:398
Neighbors & shuffle()
Definition: model.h:362
const Neighbor< AgentType > & operator[](std::size_t i) const
Definition: model.h:335
iterator end()
Definition: model.h:264
Neighbors & filter(Filter _filter)
Definition: model.h:417
Neighbors(const std::vector< Neighbor< AgentType > > &neighbors)
Definition: model.h:239
const_iterator begin() const
Definition: model.h:255
api::model::AgentTask * task(api::model::GroupId id) override
Definition: model.h:975
void copyAssign(api::model::Agent *agent) override
Definition: model.h:904
static const api::model::TypeId TYPE_ID
Definition: model.h:704
AgentBase & operator=(AgentBase &&)
Definition: model.h:793
Neighbors< NeighborAgentType > inNeighbors() const
Definition: model.h:1069
void setNode(api::model::AgentNode *node) override
Definition: model.h:938
void removeGroupId(api::model::GroupId id) override
Definition: model.h:824
const std::unordered_map< api::model::GroupId, api::model::AgentTask * > & tasks() override
Definition: model.h:996
void setGroupPos(api::model::GroupId gid, std::list< api::model::Agent * >::iterator pos) override
Definition: model.h:876
api::model::TypeId typeId() const override
Definition: model.h:893
api::model::AgentGroup * group() override
Definition: model.h:831
api::model::Agent * copy() const override
Definition: model.h:897
GroupId groupId() const override
Definition: model.h:800
const api::model::AgentNode * node() const override
Definition: model.h:934
void addGroupId(api::model::GroupId id) override
Definition: model.h:817
AgentBase(AgentBase &&)=default
std::vector< const api::model::AgentGroup * > groups() const override
Definition: model.h:839
void setModel(api::model::Model *model) override
Definition: model.h:951
std::vector< api::model::AgentGroup * > groups() override
Definition: model.h:850
api::model::AgentNode * node() override
Definition: model.h:930
api::model::Model * model() override
Definition: model.h:943
Neighbors< NeighborAgentType > inNeighbors(api::graph::LayerId layer) const
Definition: model.h:1093
std::vector< GroupId > groupIds() const override
Definition: model.h:808
const api::model::AgentTask * task() const override
Definition: model.h:962
Neighbors< NeighborAgentType > outNeighbors(api::graph::LayerId layer) const
Definition: model.h:1044
void removeGroup(api::model::AgentGroup *group) override
Definition: model.h:868
void setTask(api::model::AgentTask *task) override
Definition: model.h:968
void setGroup(api::model::AgentGroup *group) override
Definition: model.h:855
std::list< api::model::Agent * >::iterator getGroupPos(api::model::GroupId gid) const override
Definition: model.h:886
AgentType FinalAgentType
Definition: model.h:712
api::model::AgentTask * task() override
Definition: model.h:956
const api::model::AgentTask * task(api::model::GroupId id) const override
Definition: model.h:982
void moveAssign(api::model::Agent *agent) override
Definition: model.h:912
Neighbors< NeighborAgentType > outNeighbors() const
Definition: model.h:1020
void setTask(api::model::GroupId id, api::model::AgentTask *task) override
Definition: model.h:988
const api::model::AgentGroup * group() const override
Definition: model.h:846
AgentBase & operator=(const AgentBase &agent)=default
void setGroupId(api::model::GroupId id) override
Definition: model.h:812
void addGroup(api::model::AgentGroup *group) override
Definition: model.h:861
AgentBase(const AgentBase &agent)=default
virtual void act() override
Definition: model.h:1005
const api::model::Model * model() const override
Definition: model.h:947
Definition: generator.h:322
Generator_t::result_type result_type
Definition: generator.h:345
Definition: distribution.h:24
Definition: scheduler.h:169
Definition: scheduler.h:78
int LayerId
Definition: edge.h:13
int GroupId
Definition: model.h:78
api::utils::PtrWrapper< api::model::Agent > WeakAgentPtr
Definition: model.h:160
std::type_index TypeId
Definition: model.h:82
api::graph::DistributedEdge< AgentPtr > AgentEdge
Definition: model.h:49
nlohmann::basic_json< std::map, std::vector, std::string, bool, std::int64_t, std::uint64_t, double, std::allocator, light_serializer > light_json
Definition: json.h:14
io::Breakpoint< api::model::Model > Breakpoint
Definition: model.h:1355
api::utils::Callback< AgentNode * > AgentNodeCallback
Definition: model.h:1129
bool operator<(const Neighbor< AgentType > &n1, const Neighbor< AgentType > &n2)
Definition: model.h:185
detail::DefaultModel< SyncMode > Model
Definition: model.h:1314
bool is_agent_in_group(api::model::Agent *agent, api::model::GroupId group_id)
Definition: model.cpp:5
std::string format(std::string input, int rank)
Definition: format.cpp:4
static std::size_t size(const LightObjectPack &pack, const AgentPtr &ptr)
static void to_datapack(LightObjectPack &pack, const AgentPtr &pointer)
static AgentPtr from_datapack(const LightObjectPack &pack)
static std::size_t size(const LightObjectPack &pack, const WeakAgentPtr &ptr)
static void to_datapack(LightObjectPack &pack, const WeakAgentPtr &ptr)
static WeakAgentPtr from_datapack(const LightObjectPack &pack)
Definition: datapack.h:1382
static void to_datapack(ObjectPack &pack, const AgentPtr &pointer)
static AgentPtr from_datapack(const ObjectPack &pack)
static std::size_t size(const ObjectPack &p, const AgentPtr &ptr)
static std::size_t size(const ObjectPack &pack, const WeakAgentPtr &ptr)
static WeakAgentPtr from_datapack(const ObjectPack &pack)
static void to_datapack(ObjectPack &pack, const WeakAgentPtr &ptr)
Definition: datapack.h:55
static void to_json(light_json &j, const fpmas::api::model::AgentPtr &pointer)
static fpmas::api::model::AgentPtr from_json(const light_json &j)
static fpmas::api::model::WeakAgentPtr from_json(const light_json &j)
static void to_json(light_json &j, const fpmas::api::model::WeakAgentPtr &pointer)
static random::DistributedGenerator rd
Definition: model.h:203
static void seed(random::DistributedGenerator<>::result_type seed)
Definition: model.cpp:17
static void update(api::model::AgentPtr &local_agent, api::model::AgentPtr &&updated_agent)
Definition: model.h:1749
static fpmas::api::model::AgentPtr from_json(const json &j)
static void to_json(json &j, const fpmas::api::model::AgentPtr &pointer)
static fpmas::api::model::WeakAgentPtr from_json(const json &j)
static void to_json(json &j, const fpmas::api::model::WeakAgentPtr &pointer)