Skip to content

Commit 8e1fb03

Browse files
committed
#2099: Types: Add basic arithmetic operations and use them across codebase
1 parent 5a3e388 commit 8e1fb03

10 files changed

Lines changed: 116 additions & 33 deletions

File tree

src/vt/collective/scatter/scatter.impl.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ void Scatter::scatter(
5959
std::size_t const& total_size, std::size_t const& max_proc_size,
6060
FuncSizeType size_fn, FuncDataType data_fn
6161
) {
62-
auto const& num_nodes = theContext()->getNumNodes();
63-
auto const& elm_size = max_proc_size;
64-
auto const& combined_size = num_nodes * elm_size;
62+
auto const num_nodes = theContext()->getNumNodes();
63+
auto const elm_size = max_proc_size;
64+
auto const combined_size = static_cast<size_t>(num_nodes * elm_size);
6565
auto scatter_msg =
6666
makeMessageSz<ScatterMsg>(combined_size, combined_size, elm_size);
6767
vtAssert(total_size == combined_size, "Sizes must be consistent");

src/vt/group/region/group_range.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ Range::Range(Range const& in_other, BoundType in_remove_extent)
134134
for (auto split = 0; split < num_splits; split++) {
135135
auto const& child_size = size / num_splits;
136136
auto hi_bound = split == num_splits - 1 ?
137-
hi_ : std::min(static_cast<int>(hi_), cur_lo + child_size*stride_);
137+
hi_ : std::min(hi_, cur_lo + child_size*stride_);
138138
auto r1 = std::make_unique<Range>(cur_lo, BoundType{hi_bound}, stride_);
139139
apply(std::move(r1));
140140
cur_lo += BoundType{child_size*stride_};

src/vt/utils/strong/strong_type.h

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,89 @@ struct Strong {
205205
return ThisType{v_ / in.v_};
206206
}
207207

208+
/**
209+
* \brief Addition assignment operator
210+
*
211+
* \param[in] in the other value
212+
*/
213+
template <typename OtherType>
214+
ThisType& operator+=(const OtherType& in) {
215+
v_ += static_cast<Type>(in);
216+
return *this;
217+
}
218+
219+
/**
220+
* \brief Division assignment operator
221+
*
222+
* \param[in] in the other value
223+
*/
224+
template <typename OtherType>
225+
ThisType& operator/=(const OtherType& in) {
226+
v_ /= static_cast<Type>(in);
227+
return *this;
228+
}
229+
230+
/**
231+
* \brief Multiplication assignment operator
232+
*
233+
* \param[in] in the other value
234+
*/
235+
template <typename OtherType>
236+
ThisType& operator*=(const OtherType& in) {
237+
v_ *= static_cast<Type>(in);
238+
return *this;
239+
}
240+
241+
/**
242+
* \brief Addition operator
243+
*
244+
* \param[in] in the other value
245+
*/
246+
template <typename OtherType>
247+
ThisType operator+(const OtherType& in) const {
248+
return ThisType{v_ + static_cast<Type>(in)};
249+
}
250+
251+
/**
252+
* \brief Subtraction operator
253+
*
254+
* \param[in] in the other value
255+
*/
256+
template <typename OtherType>
257+
ThisType operator-(const OtherType& in) const {
258+
return ThisType{v_ - static_cast<Type>(in)};
259+
}
260+
261+
/**
262+
* \brief Modulo operator
263+
*
264+
* \param[in] in the other value
265+
*/
266+
template <typename OtherType>
267+
ThisType operator%(const OtherType& in) const {
268+
return ThisType{v_ % static_cast<Type>(in)};
269+
}
270+
271+
/**
272+
* \brief Multiplication operator
273+
*
274+
* \param[in] in the other value
275+
*/
276+
template <typename OtherType>
277+
ThisType operator*(const OtherType& in) const {
278+
return ThisType{v_ * static_cast<Type>(in)};
279+
}
280+
281+
/**
282+
* \brief Division operator
283+
*
284+
* \param[in] in the other value
285+
*/
286+
template <typename OtherType>
287+
ThisType operator/(const OtherType& in) const {
288+
return ThisType{v_ / static_cast<Type>(in)};
289+
}
290+
208291
/**
209292
* \brief Pre-increment operator
210293
*/

tests/unit/collection/test_destroy.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ static constexpr int32_t const num_elms_per_node = 8;
107107

108108
TEST_F(TestDestroy, test_destroy_1) {
109109
auto const& this_node = theContext()->getNode();
110-
auto const& num_nodes = theContext()->getNumNodes();
110+
auto const& num_nodes = theContext()->getNumNodes().get();
111111

112112
vt::runInEpochCollective([&]{
113113
if (this_node == vt::NodeT{0}) {

tests/unit/collection/test_insert.extended.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ static constexpr int32_t const num_elms_per_node = 8;
9292

9393
TEST_F(TestInsert, test_insert_dense_1) {
9494
auto const this_node = theContext()->getNode();
95-
auto const num_nodes = theContext()->getNumNodes();
95+
auto const num_nodes = theContext()->getNumNodes().get();
9696

9797
auto const range = Index1D(num_nodes * num_elms_per_node);
9898
auto proxy = vt::makeCollection<InsertTest>("test_insert_dense_1")
@@ -133,7 +133,7 @@ TEST_F(TestInsert, test_insert_dense_1) {
133133

134134
TEST_F(TestInsert, test_insert_sparse_1) {
135135
auto const this_node = theContext()->getNode();
136-
auto const num_nodes = theContext()->getNumNodes();
136+
auto const num_nodes = theContext()->getNumNodes().get();
137137

138138
auto const range = Index1D(num_nodes * num_elms_per_node * 16);
139139
auto proxy = vt::makeCollection<InsertTest>("test_insert_sparse_1")
@@ -158,7 +158,7 @@ TEST_F(TestInsert, test_insert_sparse_1) {
158158

159159
TEST_F(TestInsert, test_insert_dense_node_1) {
160160
auto const this_node = theContext()->getNode();
161-
auto const num_nodes = theContext()->getNumNodes();
161+
auto const num_nodes = theContext()->getNumNodes().get();
162162

163163
auto const range = Index1D(num_nodes * num_elms_per_node);
164164
auto proxy = vt::makeCollection<InsertTest>("test_insert_dense_node_1")
@@ -185,7 +185,7 @@ TEST_F(TestInsert, test_insert_dense_node_1) {
185185

186186
TEST_F(TestInsert, test_insert_sparse_node_1) {
187187
auto const this_node = theContext()->getNode();
188-
auto const num_nodes = theContext()->getNumNodes();
188+
auto const num_nodes = theContext()->getNumNodes().get();
189189

190190
auto const range = Index1D(num_nodes * num_elms_per_node * 16);
191191
auto proxy = vt::makeCollection<InsertTest>("test_insert_sparse_node_1")
@@ -212,7 +212,7 @@ TEST_F(TestInsert, test_insert_sparse_node_1) {
212212

213213
TEST_F(TestInsert, test_insert_send_dense_node_1) {
214214
auto const this_node = theContext()->getNode();
215-
auto const num_nodes = theContext()->getNumNodes();
215+
auto const num_nodes = theContext()->getNumNodes().get();
216216

217217
auto const range = Index1D(num_nodes * num_elms_per_node);
218218
auto proxy = vt::makeCollection<InsertTest>("test_insert_send_dense_node_1")
@@ -250,7 +250,7 @@ TEST_F(TestInsert, test_insert_send_dense_node_1) {
250250

251251
TEST_F(TestInsert, test_insert_send_sparse_node_1) {
252252
auto const this_node = theContext()->getNode();
253-
auto const num_nodes = theContext()->getNumNodes();
253+
auto const num_nodes = theContext()->getNumNodes().get();
254254

255255
auto const range = Index1D(num_nodes * num_elms_per_node * 16);
256256
auto proxy = vt::makeCollection<InsertTest>("test_insert_send_sparse_node_1")

tests/unit/collection/test_lb.extended.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ struct MyCol2 : vt::Collection<MyCol2,vt::Index1D> {};
229229
using TestLoadBalancerNoWork = TestParallelHarness;
230230

231231
TEST_F(TestLoadBalancerNoWork, test_load_balancer_no_work) {
232-
auto const num_nodes = theContext()->getNumNodes();
232+
auto const num_nodes = theContext()->getNumNodes().get();
233233
auto const range = Index1D(num_nodes * 8);
234234
theCollection()->constructCollective<MyCol2>(
235235
range, [](vt::Index1D) { return std::make_unique<MyCol2>(); },
@@ -705,8 +705,8 @@ getJsonStringForPhase(
705705
TEST_P(TestDumpUserdefinedData, test_dump_userdefined_json) {
706706
bool should_dump = GetParam();
707707

708-
auto this_node = vt::theContext()->getNode();
709-
auto num_nodes = vt::theContext()->getNumNodes();
708+
auto this_node = vt::theContext()->getNode().get();
709+
auto num_nodes = vt::theContext()->getNumNodes().get();
710710

711711
vt::vrt::collection::CollectionProxy<MyCol> proxy;
712712
auto const range = vt::Index1D(num_nodes * 1);

tests/unit/collection/test_list_insert.cc

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ TEST_F(TestListInsert, test_bounded_list_insert_1) {
124124
num_inserted = 0;
125125
num_work = 0;
126126

127-
auto const num_nodes = theContext()->getNumNodes();
127+
auto const num_nodes = theContext()->getNumNodes().get();
128128

129129
auto const range = Index1D(num_nodes * num_elms_per_node);
130130
std::vector<Index1D> list_insert;
@@ -151,7 +151,7 @@ TEST_F(TestListInsert, test_bounded_list_insert_no_default_constructor) {
151151
num_inserted = 0;
152152
num_work = 0;
153153

154-
auto const num_nodes = theContext()->getNumNodes();
154+
auto const num_nodes = theContext()->getNumNodes().get();
155155

156156
auto const range = Index1D(num_nodes * num_elms_per_node);
157157
std::vector<Index1D> list_insert;
@@ -192,7 +192,7 @@ TEST_F(TestListInsert, test_unbounded_list_insert_2) {
192192
num_inserted = 0;
193193
num_work = 0;
194194

195-
auto const num_nodes = theContext()->getNumNodes();
195+
auto const num_nodes = theContext()->getNumNodes().get();
196196

197197
auto const range = Index1D(num_nodes * num_elms_per_node);
198198
std::vector<Index1D> list_insert;
@@ -219,7 +219,7 @@ TEST_F(TestListInsert, test_unbounded_list_insert_no_default_constructor) {
219219
num_inserted = 0;
220220
num_work = 0;
221221

222-
auto const num_nodes = theContext()->getNumNodes();
222+
auto const num_nodes = theContext()->getNumNodes().get();
223223

224224
auto const range = Index1D(num_nodes * num_elms_per_node);
225225
std::vector<Index1D> list_insert;
@@ -247,7 +247,7 @@ TEST_F(TestListInsert, test_bounded_list_insert_here_3) {
247247
num_inserted = 0;
248248
num_work = 0;
249249

250-
auto const num_nodes = theContext()->getNumNodes();
250+
auto const num_nodes = theContext()->getNumNodes().get();
251251
auto const this_node = theContext()->getNode();
252252
auto const range = Index1D(num_nodes * num_elms_per_node);
253253

@@ -281,7 +281,7 @@ TEST_F(TestListInsert, test_bounded_list_insert_here_no_default_constructor) {
281281
num_inserted = 0;
282282
num_work = 0;
283283

284-
auto const num_nodes = theContext()->getNumNodes();
284+
auto const num_nodes = theContext()->getNumNodes().get();
285285
auto const this_node = theContext()->getNode();
286286
auto const range = Index1D(num_nodes * num_elms_per_node);
287287

@@ -314,7 +314,7 @@ TEST_F(TestListInsert, test_unbounded_list_insert_here_4) {
314314
num_inserted = 0;
315315
num_work = 0;
316316

317-
auto const num_nodes = theContext()->getNumNodes();
317+
auto const num_nodes = theContext()->getNumNodes().get();
318318
auto const this_node = theContext()->getNode();
319319
auto const range = Index1D(num_nodes * num_elms_per_node);
320320

@@ -348,7 +348,7 @@ TEST_F(TestListInsert, test_unbounded_list_insert_here_no_default_constructor) {
348348
num_inserted = 0;
349349
num_work = 0;
350350

351-
auto const num_nodes = theContext()->getNumNodes();
351+
auto const num_nodes = theContext()->getNumNodes().get();
352352
auto const this_node = theContext()->getNode();
353353
auto const range = Index1D(num_nodes * num_elms_per_node);
354354

@@ -426,7 +426,7 @@ TEST_F(TestListInsert, test_bounded_bulk_insert_no_default_constructor) {
426426
num_inserted = 0;
427427
num_work = 0;
428428

429-
auto const num_nodes = theContext()->getNumNodes();
429+
auto const num_nodes = theContext()->getNumNodes().get();
430430
auto const range = Index1D(num_nodes * num_elms_per_node);
431431

432432
auto proxy = vt::makeCollection<NonDefaultConstructibleStruct>("test_bounded_bulk_insert_no_default_constructor")
@@ -450,7 +450,7 @@ TEST_F(TestListInsert, test_bounded_mix_list_insert_no_default_constructor) {
450450
num_inserted = 0;
451451
num_work = 0;
452452

453-
auto const num_nodes = theContext()->getNumNodes();
453+
auto const num_nodes = theContext()->getNumNodes().get();
454454
auto const this_node = theContext()->getNode();
455455
auto const range = Index1D(num_nodes * num_elms_per_node);
456456

tests/unit/collection/test_mapping.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,20 +100,20 @@ std::vector<T> kFactors(T n, int8_t k) {
100100
T const orig_n = n;
101101
std::vector<T> factors;
102102
while (n % 2 == 0) {
103-
factors.push_back(2);
103+
factors.push_back(T{2});
104104
n /= 2;
105105
}
106-
for (T i = 3; i*i <= n; i += 2) {
106+
for (T i = T{3}; i*i <= n; i += 2) {
107107
while (n % i == 0) {
108-
factors.push_back(i);
108+
factors.push_back(T{i});
109109
n /= i;
110110
}
111111
}
112112
if (n > 2) {
113-
factors.push_back(n);
113+
factors.push_back(T{n});
114114
}
115115
while (factors.size() < static_cast<std::size_t>(k)) {
116-
factors.push_back(1);
116+
factors.push_back(T{1});
117117
}
118118

119119
std::vector<T> output = factors;
@@ -133,7 +133,7 @@ std::vector<T> kFactors(T n, int8_t k) {
133133
output = compressed;
134134
}
135135

136-
T total = 1;
136+
T total = T{1};
137137
std::string buf = "";
138138
for (auto&& elm : output) {
139139
buf += fmt::format("{}, ", elm);

tests/unit/collection/test_query_context.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ TEST_F(TestQueryContext, test_query_context_broadcast_1) {
7676
auto const& this_node = theContext()->getNode();
7777
auto const& num_nodes = theContext()->getNumNodes();
7878
if (this_node == vt::NodeT{0}) {
79-
auto const& range = Index1D(num_nodes * num_elms_per_node);
79+
auto const& range = Index1D(num_nodes.get() * num_elms_per_node);
8080
auto proxy = theCollection()->construct<QueryTest>(
8181
range, "test_query_context_broadcast_1"
8282
);
@@ -88,7 +88,7 @@ TEST_F(TestQueryContext, test_query_context_broadcast_1) {
8888

8989
TEST_F(TestQueryContext, test_query_context_send_1) {
9090
auto const& this_node = theContext()->getNode();
91-
auto const& num_nodes = theContext()->getNumNodes();
91+
auto const& num_nodes = theContext()->getNumNodes().get();
9292
if (this_node == vt::NodeT{0}) {
9393
auto const& range = Index1D(num_nodes * num_elms_per_node);
9494
auto proxy = theCollection()->construct<QueryTest>(

tests/unit/collection/test_storage.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ struct TestCollectionStorage : TestParallelHarness {
109109
};
110110

111111
TEST_F(TestCollectionStorage, test_collection_storage_1) {
112-
auto const num_nodes = theContext()->getNumNodes();
112+
auto const num_nodes = theContext()->getNumNodes().get();
113113
auto const num_elms = Index1D{num_nodes*16};
114114

115115
using MsgType = typename TestCol::TestMsg;

0 commit comments

Comments
 (0)