Skip to content

Commit ac41501

Browse files
committed
Use raw string literals for source definitions in cccl.c tests
1 parent 67fd8a7 commit ac41501

File tree

7 files changed

+167
-184
lines changed

7 files changed

+167
-184
lines changed

c/parallel/test/test_merge_sort.cpp

Lines changed: 43 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -244,16 +244,15 @@ struct item_pair
244244
struct DeviceMergeSort_SortPairsCopy_CustomType_Fixture_Tag;
245245
C2H_TEST("DeviceMergeSort:SortPairsCopy works with custom types", "[merge_sort]")
246246
{
247-
const size_t num_items = GENERATE_COPY(take(2, random(1, 100000)), values({5, 10000, 100000}));
248-
operation_t op = make_operation(
249-
"op",
250-
"struct key_pair { short a; size_t b; };\n"
251-
"extern \"C\" __device__ void op(void* lhs_ptr, void* rhs_ptr, bool* out_ptr) {\n"
252-
" key_pair* lhs = static_cast<key_pair*>(lhs_ptr);\n"
253-
" key_pair* rhs = static_cast<key_pair*>(rhs_ptr);\n"
254-
" bool* out = static_cast<bool*>(out_ptr);\n"
255-
" *out = lhs->a == rhs->a ? lhs->b < rhs->b : lhs->a < rhs->a;\n"
256-
"}");
247+
const size_t num_items = GENERATE_COPY(take(2, random(1, 100000)), values({5, 10000, 100000}));
248+
operation_t op = make_operation("op",
249+
R"(struct key_pair { short a; size_t b; };
250+
extern "C" __device__ void op(void* lhs_ptr, void* rhs_ptr, bool* out_ptr) {
251+
key_pair* lhs = static_cast<key_pair*>(lhs_ptr);
252+
key_pair* rhs = static_cast<key_pair*>(rhs_ptr);
253+
bool* out = static_cast<bool*>(out_ptr);
254+
*out = lhs->a == rhs->a ? lhs->b < rhs->b : lhs->a < rhs->a;
255+
})");
257256
const std::vector<short> a = generate<short>(num_items);
258257
const std::vector<size_t> b = generate<size_t>(num_items);
259258
std::vector<key_pair> input_keys(num_items);
@@ -301,16 +300,15 @@ C2H_TEST("DeviceMergeSort:SortPairsCopy works with custom types", "[merge_sort]"
301300
struct DeviceMergeSort_SortPairsCopy_CustomType_WellKnown_Fixture_Tag;
302301
C2H_TEST("DeviceMergeSort:SortPairsCopy works with custom types with well-known predicates", "[merge_sort][well_known]")
303302
{
304-
const size_t num_items = GENERATE_COPY(take(2, random(1, 100000)), values({5, 10000, 100000}));
305-
operation_t op_state = make_operation(
306-
"op",
307-
"struct key_pair { short a; size_t b; };\n"
308-
"extern \"C\" __device__ void op(void* lhs_ptr, void* rhs_ptr, bool* out_ptr) {\n"
309-
" key_pair* lhs = static_cast<key_pair*>(lhs_ptr);\n"
310-
" key_pair* rhs = static_cast<key_pair*>(rhs_ptr);\n"
311-
" bool* out = static_cast<bool*>(out_ptr);\n"
312-
" *out = lhs->a == rhs->a ? lhs->b < rhs->b : lhs->a < rhs->a;\n"
313-
"}");
303+
const size_t num_items = GENERATE_COPY(take(2, random(1, 100000)), values({5, 10000, 100000}));
304+
operation_t op_state = make_operation("op",
305+
R"(struct key_pair { short a; size_t b; };
306+
extern "C" __device__ void op(void* lhs_ptr, void* rhs_ptr, bool* out_ptr) {
307+
key_pair* lhs = static_cast<key_pair*>(lhs_ptr);
308+
key_pair* rhs = static_cast<key_pair*>(rhs_ptr);
309+
bool* out = static_cast<bool*>(out_ptr);
310+
*out = lhs->a == rhs->a ? lhs->b < rhs->b : lhs->a < rhs->a;
311+
})");
314312
cccl_op_t op = op_state;
315313
op.type = cccl_op_kind_t::CCCL_LESS;
316314
const std::vector<short> a = generate<short>(num_items);
@@ -432,17 +430,17 @@ C2H_TEST("DeviceMergeSort::SortKeys works with output iterators", "[merge_sort]"
432430
make_iterator<TestType, random_access_iterator_state_t>(
433431
{"random_access_iterator_state_t", "struct random_access_iterator_state_t { int* d_input; };\n"},
434432
{"advance",
435-
"extern \"C\" __device__ void advance(void* state, const void* offset) {\n"
436-
" auto* typed_state = static_cast<random_access_iterator_state_t*>(state);\n"
437-
" auto offset_val = *static_cast<const unsigned long long*>(offset);\n"
438-
" typed_state->d_input += offset_val;\n"
439-
"}"},
433+
R"(extern "C" __device__ void advance(void* state, const void* offset) {
434+
auto* typed_state = static_cast<random_access_iterator_state_t*>(state);
435+
auto offset_val = *static_cast<const unsigned long long*>(offset);
436+
typed_state->d_input += offset_val;
437+
})"},
440438
{"dereference",
441-
"extern \"C\" __device__ void dereference(void* state, const void* x) {\n"
442-
" auto* typed_state = static_cast<random_access_iterator_state_t*>(state);\n"
443-
" auto x_val = *static_cast<const int*>(x);\n"
444-
" *typed_state->d_input = x_val;\n"
445-
"}"});
439+
R"(extern "C" __device__ void dereference(void* state, const void* x) {
440+
auto* typed_state = static_cast<random_access_iterator_state_t*>(state);
441+
auto x_val = *static_cast<const int*>(x);
442+
*typed_state->d_input = x_val;
443+
})"});
446444
std::vector<TestType> input_keys = make_shuffled_key_ranks_vector<TestType>(num_items);
447445
std::vector<TestType> expected_keys = input_keys;
448446

@@ -475,17 +473,17 @@ C2H_TEST("DeviceMergeSort::SortPairs works with output iterators for items", "[m
475473
make_iterator<TestType, item_random_access_iterator_state_t>(
476474
"struct item_random_access_iterator_state_t { int* d_input; };\n",
477475
{"advance",
478-
"extern \"C\" __device__ void advance(void* state, const void* offset) {\n"
479-
" auto* typed_state = static_cast<item_random_access_iterator_state_t*>(state);\n"
480-
" auto offset_val = *static_cast<const unsigned long long*>(offset);\n"
481-
" typed_state->d_input += offset_val;\n"
482-
"}"},
476+
R"(extern "C" __device__ void advance(void* state, const void* offset) {
477+
auto* typed_state = static_cast<item_random_access_iterator_state_t*>(state);
478+
auto offset_val = *static_cast<const unsigned long long*>(offset);
479+
typed_state->d_input += offset_val;
480+
})"},
483481
{"dereference",
484-
"extern \"C\" __device__ void dereference(void* state, const void* x) {\n"
485-
" auto* typed_state = static_cast<item_random_access_iterator_state_t*>(state);\n"
486-
" auto x_val = *static_cast<const int*>(x);\n"
487-
" *typed_state->d_input = x_val;\n"
488-
"}"});
482+
R"(extern "C" __device__ void dereference(void* state, const void* x) {
483+
auto* typed_state = static_cast<item_random_access_iterator_state_t*>(state);
484+
auto x_val = *static_cast<const int*>(x);
485+
*typed_state->d_input = x_val;
486+
})"});
489487

490488
pointer_t<TestType> input_keys_it(input_keys);
491489
pointer_t<item_t> input_items_it(input_items);
@@ -657,12 +655,12 @@ C2H_TEST("MergeSort works with C++ source operations using custom headers", "[me
657655
/* C2H_TEST("DeviceMergeSort:SortPairsCopy fails to build for large types due to no vsmem", "[merge_sort]")
658656
{
659657
const size_t num_items = 1;
660-
operation_t op = make_operation(
658+
operation_t op = make_operation(
661659
"op",
662-
"struct large_key_pair { int a; char c[100]; };\n"
663-
"extern \"C\" __device__ bool op(large_key_pair lhs, large_key_pair rhs) {\n"
664-
" return lhs.a < rhs.a;\n"
665-
"}");
660+
R"(struct large_key_pair { int a; char c[100]; };
661+
extern "C" __device__ bool op(large_key_pair lhs, large_key_pair rhs) {
662+
return lhs.a < rhs.a;
663+
})");
666664
const std::vector<int> a = generate<int>(num_items);
667665
std::vector<large_key_pair> input_keys(num_items);
668666
for (std::size_t i = 0; i < num_items; ++i)

c/parallel/test/test_reduce.cpp

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,14 @@ C2H_TEST("Reduce works with custom types", "[reduce]")
180180
{
181181
const std::size_t num_items = GENERATE(0, 42, take(4, random(1 << 12, 1 << 24)));
182182

183-
operation_t op = make_operation(
184-
"op",
185-
"struct pair { short a; size_t b; };\n"
186-
"extern \"C\" __device__ void op(void* lhs_ptr, void* rhs_ptr, void* out_ptr) {\n"
187-
" pair* lhs = static_cast<pair*>(lhs_ptr);\n"
188-
" pair* rhs = static_cast<pair*>(rhs_ptr);\n"
189-
" pair* out = static_cast<pair*>(out_ptr);\n"
190-
" *out = pair{ lhs->a + rhs->a, lhs->b + rhs->b };\n"
191-
"}");
183+
operation_t op = make_operation("op",
184+
R"(struct pair { short a; size_t b; };
185+
extern "C" __device__ void op(void* lhs_ptr, void* rhs_ptr, void* out_ptr) {
186+
pair* lhs = static_cast<pair*>(lhs_ptr);
187+
pair* rhs = static_cast<pair*>(rhs_ptr);
188+
pair* out = static_cast<pair*>(out_ptr);
189+
*out = pair{ lhs->a + rhs->a, lhs->b + rhs->b };
190+
})");
192191
const std::vector<short> a = generate<short>(num_items);
193192
const std::vector<size_t> b = generate<size_t>(num_items);
194193
std::vector<pair> input(num_items);
@@ -218,15 +217,14 @@ C2H_TEST("Reduce works with custom types with well-known operations", "[reduce][
218217
{
219218
const std::size_t num_items = GENERATE(0, 42, take(4, random(1 << 12, 1 << 24)));
220219

221-
operation_t op_state = make_operation(
222-
"op",
223-
"struct pair { short a; size_t b; };\n"
224-
"extern \"C\" __device__ void op(void* lhs_ptr, void* rhs_ptr, void* out_ptr) {\n"
225-
" pair* lhs = static_cast<pair*>(lhs_ptr);\n"
226-
" pair* rhs = static_cast<pair*>(rhs_ptr);\n"
227-
" pair* out = static_cast<pair*>(out_ptr);\n"
228-
" *out = pair{ lhs->a + rhs->a, lhs->b + rhs->b };\n"
229-
"}");
220+
operation_t op_state = make_operation("op",
221+
R"(struct pair { short a; size_t b; };
222+
extern "C" __device__ void op(void* lhs_ptr, void* rhs_ptr, void* out_ptr) {
223+
pair* lhs = static_cast<pair*>(lhs_ptr);
224+
pair* rhs = static_cast<pair*>(rhs_ptr);
225+
pair* out = static_cast<pair*>(out_ptr);
226+
*out = pair{ lhs->a + rhs->a, lhs->b + rhs->b };
227+
})");
230228
cccl_op_t op = op_state;
231229
op.type = cccl_op_kind_t::CCCL_PLUS;
232230
const std::vector<short> a = generate<short>(num_items);
@@ -371,14 +369,14 @@ C2H_TEST("Reduce works with stateful operators", "[reduce]")
371369
pointer_t<int> counter(1);
372370
stateful_operation_t<invocation_counter_state_t> op = make_operation(
373371
"op",
374-
"struct invocation_counter_state_t { int* d_counter; };\n"
375-
"extern \"C\" __device__ void op(void* state_ptr, void* a_ptr, void* b_ptr, void* out_ptr) {\n"
376-
" invocation_counter_state_t* state = static_cast<invocation_counter_state_t*>(state_ptr);\n"
377-
" atomicAdd(state->d_counter, 1);\n"
378-
" int a = *static_cast<int*>(a_ptr);\n"
379-
" int b = *static_cast<int*>(b_ptr);\n"
380-
" *static_cast<int*>(out_ptr) = a + b;\n"
381-
"}",
372+
R"(struct invocation_counter_state_t { int* d_counter; };
373+
extern "C" __device__ void op(void* state_ptr, void* a_ptr, void* b_ptr, void* out_ptr) {
374+
invocation_counter_state_t* state = static_cast<invocation_counter_state_t*>(state_ptr);
375+
atomicAdd(state->d_counter, 1);
376+
int a = *static_cast<int*>(a_ptr);
377+
int b = *static_cast<int*>(b_ptr);
378+
*static_cast<int*>(out_ptr) = a + b;
379+
})",
382380
invocation_counter_state_t{counter.ptr});
383381

384382
const std::vector<int> input = generate<int>(num_items);

c/parallel/test/test_scan.cpp

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -398,15 +398,14 @@ C2H_TEST("Scan works with custom types", "[scan]")
398398
{
399399
const std::size_t num_items = GENERATE(0, 42, take(4, random(1 << 12, 1 << 24)));
400400

401-
operation_t op = make_operation(
402-
"op",
403-
"struct pair { short a; size_t b; };\n"
404-
"extern \"C\" __device__ void op(void* lhs_ptr, void* rhs_ptr, void* out_ptr) {\n"
405-
" pair* lhs = static_cast<pair*>(lhs_ptr);\n"
406-
" pair* rhs = static_cast<pair*>(rhs_ptr);\n"
407-
" pair* out = static_cast<pair*>(out_ptr);\n"
408-
" *out = pair{ lhs->a + rhs->a, lhs->b + rhs->b };\n"
409-
"}");
401+
operation_t op = make_operation("op",
402+
R"(struct pair { short a; size_t b; };
403+
extern "C" __device__ void op(void* lhs_ptr, void* rhs_ptr, void* out_ptr) {
404+
pair* lhs = static_cast<pair*>(lhs_ptr);
405+
pair* rhs = static_cast<pair*>(rhs_ptr);
406+
pair* out = static_cast<pair*>(out_ptr);
407+
*out = pair{ lhs->a + rhs->a, lhs->b + rhs->b };
408+
})");
410409
const std::vector<short> a = generate<short>(num_items);
411410
const std::vector<size_t> b = generate<size_t>(num_items);
412411
std::vector<pair> input(num_items);
@@ -439,15 +438,14 @@ C2H_TEST("Scan works with custom types with well-known operations", "[scan][well
439438
{
440439
const std::size_t num_items = GENERATE(0, 42, take(4, random(1 << 12, 1 << 24)));
441440

442-
operation_t op_state = make_operation(
443-
"op",
444-
"struct pair { short a; size_t b; };\n"
445-
"extern \"C\" __device__ void op(void* lhs_ptr, void* rhs_ptr, void* out_ptr) {\n"
446-
" pair* lhs = static_cast<pair*>(lhs_ptr);\n"
447-
" pair* rhs = static_cast<pair*>(rhs_ptr);\n"
448-
" pair* out = static_cast<pair*>(out_ptr);\n"
449-
" *out = pair{ lhs->a + rhs->a, lhs->b + rhs->b };\n"
450-
"}");
441+
operation_t op_state = make_operation("op",
442+
R"(struct pair { short a; size_t b; };
443+
extern "C" __device__ void op(void* lhs_ptr, void* rhs_ptr, void* out_ptr) {
444+
pair* lhs = static_cast<pair*>(lhs_ptr);
445+
pair* rhs = static_cast<pair*>(rhs_ptr);
446+
pair* out = static_cast<pair*>(out_ptr);
447+
*out = pair{ lhs->a + rhs->a, lhs->b + rhs->b };
448+
})");
451449
cccl_op_t op = op_state;
452450
op.type = cccl_op_kind_t::CCCL_PLUS;
453451
const std::vector<short> a = generate<short>(num_items);

c/parallel/test/test_three_way_partition.cpp

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -329,21 +329,21 @@ C2H_TEST("ThreeWayPartition works with stateful operations", "[three_way_partiti
329329
selector_state_t op_state = {21};
330330
stateful_operation_t<selector_state_t> less_op = make_operation(
331331
"less_op",
332-
"struct selector_state_t { int comparison_value; };\n"
333-
"extern \"C\" __device__ void less_op(void* state_ptr, void* x_ptr, void* out_ptr) {\n"
334-
" selector_state_t* state = static_cast<selector_state_t*>(state_ptr);\n"
335-
" *static_cast<int*>(x_ptr) < state->comparison_value;\n"
336-
" *static_cast<bool*>(out_ptr) = *static_cast<int*>(x_ptr) < state->comparison_value;\n"
337-
"}",
332+
R"(struct selector_state_t { int comparison_value; };
333+
extern "C" __device__ void less_op(void* state_ptr, void* x_ptr, void* out_ptr) {
334+
selector_state_t* state = static_cast<selector_state_t*>(state_ptr);
335+
*static_cast<int*>(x_ptr) < state->comparison_value;
336+
*static_cast<bool*>(out_ptr) = *static_cast<int*>(x_ptr) < state->comparison_value;
337+
})",
338338
op_state);
339339
stateful_operation_t<selector_state_t> greater_or_equal_op = make_operation(
340340
"greater_or_equal_op",
341-
"struct selector_state_t { int comparison_value; };\n"
342-
"extern \"C\" __device__ void greater_or_equal_op(void* state_ptr, void* x_ptr, void* out_ptr) {\n"
343-
" selector_state_t* state = static_cast<selector_state_t*>(state_ptr);\n"
344-
" *static_cast<int*>(x_ptr) >= state->comparison_value;\n"
345-
" *static_cast<bool*>(out_ptr) = *static_cast<int*>(x_ptr) >= state->comparison_value;\n"
346-
"}",
341+
R"(struct selector_state_t { int comparison_value; };
342+
extern "C" __device__ void greater_or_equal_op(void* state_ptr, void* x_ptr, void* out_ptr) {
343+
selector_state_t* state = static_cast<selector_state_t*>(state_ptr);
344+
*static_cast<int*>(x_ptr) >= state->comparison_value;
345+
*static_cast<bool*>(out_ptr) = *static_cast<int*>(x_ptr) >= state->comparison_value;
346+
})",
347347
op_state);
348348

349349
const std::size_t num_items = GENERATE(0, 42, take(4, random(1 << 12, 1 << 20)));
@@ -409,21 +409,21 @@ C2H_TEST("ThreeWayPartition works with custom types", "[three_way_partition]")
409409

410410
operation_t less_op = make_operation(
411411
"less_op",
412-
std::format("struct pair_type {{ int a; size_t b; }};"
413-
"extern \"C\" __device__ void less_op(void* x_ptr, void* out_ptr) {{ "
414-
" pair_type* x = static_cast<pair_type*>(x_ptr); "
415-
" bool* out = static_cast<bool*>(out_ptr); "
416-
" *out = x->a < {0}; "
417-
"}}",
412+
std::format(R"(struct pair_type {{ int a; size_t b; }};
413+
extern "C" __device__ void less_op(void* x_ptr, void* out_ptr) {{
414+
pair_type* x = static_cast<pair_type*>(x_ptr);
415+
bool* out = static_cast<bool*>(out_ptr);
416+
*out = x->a < {0};
417+
}})",
418418
comparison_value));
419419
operation_t greater_or_equal_op = make_operation(
420420
"greater_or_equal_op",
421-
std::format("struct pair_type {{ int a; size_t b; }};"
422-
"extern \"C\" __device__ void greater_or_equal_op(void* x_ptr, void* out_ptr) {{ "
423-
" pair_type* x = static_cast<pair_type*>(x_ptr); "
424-
" bool* out = static_cast<bool*>(out_ptr); "
425-
" *out = x->a >= {0}; "
426-
"}}",
421+
std::format(R"(struct pair_type {{ int a; size_t b; }};
422+
extern "C" __device__ void greater_or_equal_op(void* x_ptr, void* out_ptr) {{
423+
pair_type* x = static_cast<pair_type*>(x_ptr);
424+
bool* out = static_cast<bool*>(out_ptr);
425+
*out = x->a >= {0};
426+
}})",
427427
comparison_value));
428428

429429
const std::size_t num_items = GENERATE(0, 42, take(4, random(1 << 12, 1 << 20)));

0 commit comments

Comments
 (0)