Skip to content

Commit 7a41231

Browse files
UCS/DATASTRUCT: Add ucs_array_for_each_index
Signed-off-by: Guy Ealey Morag <gealeymorag@nvidia.com>
1 parent efcc834 commit 7a41231

6 files changed

Lines changed: 71 additions & 10 deletions

File tree

.clang-format

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ ForEachMacros: ['UCS_STATIC_BITMAP_FOR_EACH_BIT',
7272
'UCS_STATIC_BITMAP_FOR_EACH_BIT',
7373
'FOR_EACH_ENTITY',
7474
'ucs_carray_for_each',
75+
'ucs_carray_for_each_index',
7576
'kh_foreach',
7677
'kh_foreach_key',
7778
'kh_foreach_value',
@@ -81,6 +82,7 @@ ForEachMacros: ['UCS_STATIC_BITMAP_FOR_EACH_BIT',
8182
'ucp_proto_perf_segment_foreach',
8283
'ucp_proto_perf_segment_foreach_range',
8384
'ucs_array_for_each',
85+
'ucs_array_for_each_index',
8486
'UCS_BITMAP_FOR_EACH_BIT',
8587
'ucs_for_each_bit',
8688
'ucs_for_each_submask',

src/ucp/proto/proto_select.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,7 @@ static ucs_status_t ucp_proto_thresholds_next_range(
7575
ucs_dynamic_bitmap_reset_all(proto_mask);
7676
ucs_dynamic_bitmap_init(&disabled_proto_mask);
7777

78-
for (proto_idx = 0; proto_idx < ucs_array_length(&proto_init->protocols);
79-
++proto_idx) {
80-
proto = &ucs_array_elem(&proto_init->protocols, proto_idx);
78+
ucs_array_for_each_index(proto, proto_idx, &proto_init->protocols) {
8179
range = ucp_proto_flat_perf_find_lb(proto->flat_perf, msg_length);
8280
if (range == NULL) {
8381
ucs_trace("skipping proto %s for msg_length %zu",

src/ucs/datastruct/array.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright (c) NVIDIA CORPORATION & AFFILIATES, 2020. ALL RIGHTS RESERVED.
2+
* Copyright (c) NVIDIA CORPORATION & AFFILIATES, 2020-2026. ALL RIGHTS RESERVED.
33
*
44
* See file LICENSE for terms.
55
*/
@@ -456,6 +456,18 @@ ucs_array_old_buffer_set_null(void **old_buffer_p)
456456
ucs_array_length(_array))
457457

458458

459+
/**
460+
* Iterate over array elements and track the current index
461+
*
462+
* @param _elem Pointer variable to the current array element
463+
* @param _idx Variable containing the current array index
464+
* @param _array Array to iterate over
465+
*/
466+
#define ucs_array_for_each_index(_elem, _idx, _array) \
467+
ucs_carray_for_each_index(_elem, _idx, ucs_array_begin(_array), \
468+
ucs_array_length(_array))
469+
470+
459471
/* Internal helper function */
460472
ucs_status_t ucs_array_grow(void **buffer_p, size_t *capacity_p,
461473
size_t min_capacity, size_t max_capacity,

src/ucs/datastruct/callbackq.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright (c) NVIDIA CORPORATION & AFFILIATES, 2001-2016. ALL RIGHTS RESERVED.
2+
* Copyright (c) NVIDIA CORPORATION & AFFILIATES, 2001-2026. ALL RIGHTS RESERVED.
33
* Copyright (C) ARM Ltd. 2016-2017. ALL RIGHTS RESERVED.
44
*
55
* See file LICENSE for terms.
@@ -295,9 +295,7 @@ static void ucs_callbackq_spill_elems_purge(ucs_callbackq_t *cbq)
295295
* always be equal to dst_idx, so nothing will be actually copied/moved.
296296
*/
297297
dst_idx = 0;
298-
for (src_idx = 0; src_idx < ucs_array_length(&priv->spill_elems);
299-
++src_idx) {
300-
src_elem = &ucs_array_elem(&priv->spill_elems, src_idx);
298+
ucs_array_for_each_index(src_elem, src_idx, &priv->spill_elems) {
301299
if (src_elem->id != UCS_CALLBACKQ_ID_NULL) {
302300
ucs_assert(dst_idx <= src_idx);
303301
if (dst_idx != src_idx) {

src/ucs/sys/compiler_def.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright (c) NVIDIA CORPORATION & AFFILIATES, 2001-2017. ALL RIGHTS RESERVED.
2+
* Copyright (c) NVIDIA CORPORATION & AFFILIATES, 2001-2026. ALL RIGHTS RESERVED.
33
* Copyright (C) UT-Battelle, LLC. 2015. ALL RIGHTS RESERVED.
44
* Copyright (C) Arm, Ltd. 2021. ALL RIGHTS RESERVED.
55
* Copyright (C) Advanced Micro Devices, Inc. 2024. ALL RIGHTS RESERVED.
@@ -250,6 +250,12 @@
250250
#define ucs_carray_for_each(_elem, _array, _length) \
251251
for ((_elem) = (_array); (_elem) < ((_array) + (_length)); ++(_elem))
252252

253+
/*
254+
* Iterate over all elements of a C-array and track the current index
255+
*/
256+
#define ucs_carray_for_each_index(_elem, _idx, _array, _length) \
257+
for ((_idx) = 0, (_elem) = (_array); (_idx) < (_length); ++(_idx), ++(_elem))
258+
253259
/*
254260
* Swap two variables values
255261
*/

test/gtest/ucs/test_datatype.cc

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright (c) NVIDIA CORPORATION & AFFILIATES, 2001-2014. ALL RIGHTS RESERVED.
2+
* Copyright (c) NVIDIA CORPORATION & AFFILIATES, 2001-2026. ALL RIGHTS RESERVED.
33
* Copyright (C) UT-Battelle, LLC. 2014. ALL RIGHTS RESERVED.
44
* Copyright (C) Huawei Technologies Co., Ltd. 2020. ALL RIGHTS RESERVED.
55
* See file LICENSE for terms.
@@ -1115,6 +1115,51 @@ class test_array : public test_datatype {
11151115
void cleanup_array_of_linked_lists(test_list_links_array_t *test_array);
11161116
};
11171117

1118+
UCS_TEST_F(test_array, carray_for_each_index) {
1119+
const int values[] = {3, 5, 7};
1120+
const int *elem;
1121+
size_t idx;
1122+
1123+
ucs_carray_for_each_index(elem, idx, values, 0) {
1124+
ADD_FAILURE() << "iterated over an empty array";
1125+
}
1126+
EXPECT_EQ(0, idx);
1127+
1128+
ucs_carray_for_each_index(elem, idx, values,
1129+
ucs_static_array_size(values)) {
1130+
EXPECT_EQ(&values[idx], elem);
1131+
EXPECT_EQ(values[idx], *elem);
1132+
}
1133+
EXPECT_EQ(ucs_static_array_size(values), idx);
1134+
}
1135+
1136+
UCS_TEST_F(test_array, array_for_each_index) {
1137+
constexpr size_t NUM_ELEMENTS = 3;
1138+
test_1int_t test_array;
1139+
int *elem;
1140+
size_t idx;
1141+
1142+
ucs_array_init_dynamic(&test_array);
1143+
1144+
ucs_array_for_each_index(elem, idx, &test_array) {
1145+
ADD_FAILURE() << "iterated over an empty array";
1146+
}
1147+
EXPECT_EQ(0, idx);
1148+
1149+
for (idx = 0; idx < NUM_ELEMENTS; ++idx) {
1150+
*ucs_array_append(&test_array,
1151+
FAIL()) = static_cast<int>(idx * idx + 1);
1152+
}
1153+
1154+
ucs_array_for_each_index(elem, idx, &test_array) {
1155+
EXPECT_EQ(&ucs_array_elem(&test_array, idx), elem);
1156+
EXPECT_EQ(idx * idx + 1, static_cast<size_t>(*elem));
1157+
}
1158+
EXPECT_EQ(NUM_ELEMENTS, idx);
1159+
1160+
ucs_array_cleanup_dynamic(&test_array);
1161+
}
1162+
11181163
/* generate a list of numbers in a certain size */
11191164
void test_array::generate_linked_list(int size, simple_elem_t *head)
11201165
{

0 commit comments

Comments
 (0)