Skip to content

Commit f1fef3e

Browse files
authored
Add C-string support in the PDI wrapper (#1095)
1 parent c7f9c64 commit f1fef3e

3 files changed

Lines changed: 82 additions & 4 deletions

File tree

src/ddc/pdi.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//
33
// SPDX-License-Identifier: MIT
44

5+
#include <cstring>
56
#include <string>
67
#include <utility>
78
#include <vector>
@@ -32,4 +33,11 @@ PdiEvent::~PdiEvent() noexcept
3233
}
3334
}
3435

36+
PdiEvent& PdiEvent::with(std::string const& name, char const* const c_string)
37+
{
38+
PDI_share(store_name(name + "_size"), store_scalar(std::strlen(c_string)), PDI_OUT);
39+
PDI_share(store_name(name), c_string, PDI_OUT);
40+
return *this;
41+
}
42+
3543
} // namespace ddc

src/ddc/pdi.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ class PdiEvent
126126
return with<default_access_v<Arithmetic>>(name, std::forward<Arithmetic>(data));
127127
}
128128

129+
/// C-string overload
130+
PdiEvent& with(std::string const& name, char const* c_string);
131+
129132
/// @}
130133
};
131134

tests/pdi/pdi.cpp

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ struct DDimY
2727

2828
extern "C" {
2929

30-
void test_ddc_expose()
30+
void test_ddc_expose_chunk()
3131
{
3232
// pdi_chunk_label_rank
3333
void* pdi_chunk_label_rank_ptr;
@@ -70,7 +70,7 @@ void test_ddc_expose()
7070

7171
TEST(Pdi, ChunkAndChunkSpan)
7272
{
73-
std::string const pdi_cfg = R"PDI_CFG(
73+
char const* const pdi_cfg = R"PDI_CFG(
7474
metadata:
7575
pdi_chunk_label_rank: size_t
7676
pdi_chunk_label_extents:
@@ -89,10 +89,10 @@ TEST(Pdi, ChunkAndChunkSpan)
8989
user_code:
9090
on_event:
9191
some_event:
92-
test_ddc_expose: {}
92+
test_ddc_expose_chunk: {}
9393
)PDI_CFG";
9494

95-
PC_tree_t pdi_conf = PC_parse_string(pdi_cfg.c_str());
95+
PC_tree_t pdi_conf = PC_parse_string(pdi_cfg);
9696
PDI_init(pdi_conf);
9797

9898
PDI_errhandler(PDI_NULL_HANDLER);
@@ -127,3 +127,70 @@ TEST(Pdi, ChunkAndChunkSpan)
127127
PDI_finalize();
128128
PC_tree_destroy(&pdi_conf);
129129
}
130+
131+
extern "C" {
132+
133+
void test_ddc_expose_c_string()
134+
{
135+
// c_string_size
136+
void* c_string_size_ptr;
137+
ASSERT_EQ(PDI_access("c_string_size", &c_string_size_ptr, PDI_IN), PDI_OK);
138+
std::size_t const* const c_string_size = static_cast<std::size_t*>(c_string_size_ptr);
139+
ASSERT_EQ(*c_string_size, 3);
140+
141+
// c_string
142+
void* c_string_ptr;
143+
ASSERT_EQ(PDI_access("c_string", &c_string_ptr, PDI_IN), PDI_OK);
144+
std::string_view const c_string(
145+
static_cast<char*>(c_string_ptr),
146+
static_cast<char*>(c_string_ptr) + *c_string_size);
147+
EXPECT_EQ(c_string, "foo");
148+
149+
EXPECT_EQ(PDI_reclaim("c_string_size"), PDI_OK);
150+
EXPECT_EQ(PDI_reclaim("c_string"), PDI_OK);
151+
152+
// nb_event_called
153+
void* nb_event_called_ptr;
154+
ASSERT_EQ(PDI_access("nb_event_called", &nb_event_called_ptr, PDI_INOUT), PDI_OK);
155+
int* const nb_event_called = static_cast<int*>(nb_event_called_ptr);
156+
*nb_event_called += 1;
157+
158+
EXPECT_EQ(PDI_reclaim("nb_event_called"), PDI_OK);
159+
}
160+
}
161+
162+
TEST(Pdi, CString)
163+
{
164+
char const* const pdi_cfg = R"PDI_CFG(
165+
metadata:
166+
c_string_size: size_t
167+
c_string: {type: array, subtype: char, size: "$c_string_size"}
168+
169+
data:
170+
nb_event_called: int
171+
172+
plugins:
173+
user_code:
174+
on_event:
175+
some_event:
176+
test_ddc_expose_c_string: {}
177+
)PDI_CFG";
178+
179+
PC_tree_t pdi_conf = PC_parse_string(pdi_cfg);
180+
PDI_init(pdi_conf);
181+
182+
PDI_errhandler(PDI_NULL_HANDLER);
183+
184+
{
185+
int nb_event_called = 0;
186+
187+
ddc::PdiEvent("some_event")
188+
.with("c_string", "foo")
189+
.with("nb_event_called", nb_event_called);
190+
191+
EXPECT_EQ(nb_event_called, 1);
192+
}
193+
194+
PDI_finalize();
195+
PC_tree_destroy(&pdi_conf);
196+
}

0 commit comments

Comments
 (0)