Skip to content

Commit 08d339e

Browse files
committed
#1: API: refactor API for defining tasks, edges, and shared blocks
1 parent bfec011 commit 08d339e

File tree

5 files changed

+223
-35
lines changed

5 files changed

+223
-35
lines changed

src/vt-lb/model/Communication.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,28 @@
4848

4949
namespace vt_lb::model {
5050

51-
// todo
51+
struct Edge {
52+
Edge() = default;
53+
Edge(TaskType from, TaskType to, BytesType volume)
54+
: from_(from), to_(to), volume_(volume)
55+
{}
56+
57+
TaskType getFrom() const { return from_; }
58+
TaskType getTo() const { return to_; }
59+
BytesType getVolume() const { return volume_; }
60+
61+
template <typename Serializer>
62+
void serialize(Serializer& s) {
63+
s | from_;
64+
s | to_;
65+
s | volume_;
66+
}
67+
68+
private:
69+
TaskType from_ = invalid_task;
70+
TaskType to_ = invalid_task;
71+
BytesType volume_ = 0.0;
72+
};
5273

5374
} /* end namespace vt_lb::model */
5475

src/vt-lb/model/PhaseData.h

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
//@HEADER
3+
// *****************************************************************************
4+
//
5+
// PhaseData.h
6+
// DARMA/vt-lb => Virtual Transport/Load Balancers
7+
//
8+
// Copyright 2019-2024 National Technology & Engineering Solutions of Sandia, LLC
9+
// (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S.
10+
// Government retains certain rights in this software.
11+
//
12+
// Redistribution and use in source and binary forms, with or without
13+
// modification, are permitted provided that the following conditions are met:
14+
//
15+
// * Redistributions of source code must retain the above copyright notice,
16+
// this list of conditions and the following disclaimer.
17+
//
18+
// * Redistributions in binary form, must reproduce the above copyright notice,
19+
// this list of conditions and the following disclaimer in the documentation
20+
// and/or other materials provided with the distribution.
21+
//
22+
// * Neither the name of the copyright holder nor the names of its
23+
// contributors may be used to endorse or promote products derived from this
24+
// software without specific prior written permission.
25+
//
26+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27+
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28+
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29+
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30+
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31+
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32+
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33+
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34+
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35+
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36+
// POSSIBILITY OF SUCH DAMAGE.
37+
//
38+
// Questions? Contact [email protected]
39+
//
40+
// *****************************************************************************
41+
//@HEADER
42+
*/
43+
44+
#if !defined INCLUDED_VT_LB_MODEL_PHASE_DATA_H
45+
#define INCLUDED_VT_LB_MODEL_PHASE_DATA_H
46+
47+
#include "types.h"
48+
#include "SharedBlock.h"
49+
#include "Task.h"
50+
#include "Communication.h"
51+
#include <unordered_map>
52+
53+
namespace vt_lb::model {
54+
55+
struct PhaseData {
56+
PhaseData() = default;
57+
explicit PhaseData(RankType rank) : rank_(rank) {}
58+
59+
void addTask(Task const& t) { tasks_.emplace(t.getId(), t); }
60+
void addCommunication(Edge const& e) { communications_.push_back(e); }
61+
void addSharedBlock(SharedBlock const& b) { shared_blocks_.emplace(b.getId(), b); }
62+
63+
RankType getRank() const { return rank_; }
64+
65+
Task const* getTask(TaskType id) const {
66+
auto it = tasks_.find(id);
67+
return it != tasks_.end() ? &it->second : nullptr;
68+
}
69+
bool hasTask(TaskType id) const { return tasks_.find(id) != tasks_.end(); }
70+
void eraseTask(TaskType id) { tasks_.erase(id); }
71+
72+
SharedBlock const* getSharedBlock(SharedBlockType id) const {
73+
auto it = shared_blocks_.find(id);
74+
return it != shared_blocks_.end() ? &it->second : nullptr;
75+
}
76+
bool hasSharedBlock(SharedBlockType id) const { return shared_blocks_.find(id) != shared_blocks_.end(); }
77+
void eraseSharedBlock(SharedBlockType id) { shared_blocks_.erase(id); }
78+
79+
std::unordered_map<TaskType, Task> const& getTasksMap() const { return tasks_; }
80+
std::vector<Edge> const& getCommunications() const { return communications_; }
81+
std::unordered_map<SharedBlockType, SharedBlock> const& getSharedBlocksMap() const { return shared_blocks_; }
82+
83+
void clear() {
84+
tasks_.clear();
85+
communications_.clear();
86+
shared_blocks_.clear();
87+
}
88+
89+
template <typename Serializer>
90+
void serialize(Serializer& s) {
91+
s | rank_;
92+
s | tasks_;
93+
s | communications_;
94+
s | shared_blocks_;
95+
}
96+
97+
private:
98+
RankType rank_ = invalid_node;
99+
std::unordered_map<TaskType, Task> tasks_;
100+
std::vector<Edge> communications_;
101+
std::unordered_map<SharedBlockType, SharedBlock> shared_blocks_;
102+
};
103+
104+
} /* end namespace vt_lb::model */
105+
106+
#endif /*INCLUDED_VT_LB_MODEL_PHASE_DATA_H*/

src/vt-lb/model/SharedBlock.h

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,33 @@
4444
#if !defined INCLUDED_VT_LB_MODEL_BLOCK_H
4545
#define INCLUDED_VT_LB_MODEL_BLOCK_H
4646

47-
#include <unordered_map>
48-
4947
#include "types.h"
5048
#include "Task.h"
5149

5250
namespace vt_lb::model {
5351

54-
struct SharedBlock {
55-
SharedBlockId id = NoSharedBlock;
56-
Bytes size = 0.0;
57-
NodeId home = InvalidNode;
58-
std::unordered_map<TaskId, Task> tasks;
59-
};
52+
struct SharedBlock {
53+
SharedBlock() = default;
54+
SharedBlock(SharedBlockType id, BytesType size, RankType home)
55+
: id_(id), size_(size), home_(home)
56+
{}
57+
58+
SharedBlockType getId() const { return id_; }
59+
BytesType getSize() const { return size_; }
60+
RankType getHome() const { return home_; }
61+
62+
template <typename Serializer>
63+
void serialize(Serializer& s) {
64+
s | id_;
65+
s | size_;
66+
s | home_;
67+
}
68+
69+
private:
70+
SharedBlockType id_ = no_shared_block;
71+
BytesType size_ = 0.0;
72+
RankType home_ = invalid_node;
73+
};
6074

6175
} /* end namespace vt_lb::model */
6276

src/vt-lb/model/Task.h

Lines changed: 65 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,72 @@
4848

4949
namespace vt_lb::model {
5050

51-
struct TaskMemory {
52-
Bytes working = 0.0;
53-
Bytes footprint = 0.0;
54-
Bytes serialized = 0.0;
55-
};
51+
struct TaskMemory {
52+
TaskMemory() = default;
53+
TaskMemory(BytesType working, BytesType footprint, BytesType serialized)
54+
: working_(working), footprint_(footprint), serialized_(serialized)
55+
{}
5656

57-
struct Task {
58-
TaskId id;
59-
NodeId home = InvalidNode;
60-
NodeId current = InvalidNode;
61-
bool migratable = true;
62-
TaskMemory memory;
63-
Load load = 0.0;
64-
};
57+
BytesType getWorking() const { return working_; }
58+
BytesType getFootprint() const { return footprint_; }
59+
BytesType getSerialized() const { return serialized_; }
60+
61+
template <typename Serializer>
62+
void serialize(Serializer& s) {
63+
s | working_;
64+
s | footprint_;
65+
s | serialized_;
66+
}
67+
68+
private:
69+
BytesType working_ = 0.0;
70+
BytesType footprint_ = 0.0;
71+
BytesType serialized_ = 0.0;
72+
};
73+
74+
struct Task {
75+
Task() = default;
76+
Task(TaskType id, RankType home, RankType current, bool migratable,
77+
TaskMemory const& memory, LoadType load)
78+
: id_(id),
79+
home_(home),
80+
current_(current),
81+
migratable_(migratable),
82+
memory_(memory),
83+
load_(load)
84+
{}
85+
86+
TaskType getId() const { return id_; }
87+
RankType getHome() const { return home_; }
88+
RankType getCurrent() const { return current_; }
89+
bool isMigratable() const { return migratable_; }
90+
TaskMemory const& getMemory() const { return memory_; }
91+
LoadType getLoad() const { return load_; }
92+
93+
template <typename Serializer>
94+
void serialize(Serializer& s) {
95+
s | id_;
96+
s | home_;
97+
s | current_;
98+
s | migratable_;
99+
s | memory_;
100+
s | load_;
101+
s | shared_blocks_;
102+
}
103+
104+
private:
105+
TaskType id_ = invalid_task;
106+
NodeType home_ = invalid_node;
107+
NodeType current_ = invalid_node;
108+
bool migratable_ = true;
109+
TaskMemory memory_;
110+
LoadType load_ = 0.0;
111+
std::unordered_set<SharedBlockType> shared_blocks_;
112+
113+
public:
114+
bool operator==(const Task& other) const { return id_ == other.id_; }
115+
bool operator!=(const Task& other) const { return !(*this == other); }
116+
};
65117

66118
} /* end namespace vt_lb::model */
67119

src/vt-lb/model/types.h

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,19 @@
4545
#define INCLUDED_VT_LB_MODEL_TYPES_H
4646

4747
#include <cstdint>
48-
#include <string>
4948
#include <functional>
5049

5150
namespace vt_lb::model {
5251

53-
using NodeId = int32_t;
54-
using SharedBlockId = int64_t;
55-
using Bytes = double;
56-
using Load = double;
52+
using RankType = int32_t;
53+
using SharedBlockType = int64_t;
54+
using BytesType = double;
55+
using LoadType = double;
56+
using TaskType = uint64_t;
5757

58-
struct TaskId {
59-
uint64_t value{0};
60-
bool operator==(const TaskId& other) const noexcept { return value == other.value; }
61-
bool operator!=(const TaskId& other) const noexcept { return !(*this == other); }
62-
};
63-
64-
constexpr SharedBlockId NoSharedBlock = -1;
65-
constexpr NodeId InvalidNode = -1;
58+
constexpr SharedBlockType no_shared_block = -1;
59+
constexpr RankType invalid_node = -1;
60+
constexpr TaskType invalid_task = static_cast<TaskType>(-1);
6661

6762
} /* end namespace vt_lb::model */
6863

0 commit comments

Comments
 (0)