Skip to content

[BUG] c.parallel: Stateful ops don't work with transform (and some other algorithms) #7387

Closed

Description

@shwina

Stateful ops don't work with transform (and other algorithms that take the kernels/operators.h path as opposed to jit_templates).

I was able to vibe code a solution; I'll open a PR for feedback.

struct transform_stateful_counter_state_t
{
  int* d_counter;
};

C2H_TEST("Transform works with stateful unary operators", "[transform]")
{
  const std::size_t num_items = GENERATE(0, 42, take(4, random(1 << 12, 1 << 16)));
  pointer_t<int> counter(1);
  stateful_operation_t<transform_stateful_counter_state_t> op = make_operation(
    "op",
    R"(struct transform_stateful_counter_state_t { int* d_counter; };
extern "C" __device__ void op(void* state_ptr, void* x_ptr, void* out_ptr) {
  auto* state = static_cast<transform_stateful_counter_state_t*>(state_ptr);
  atomicAdd(state->d_counter, 1);
  int x = *static_cast<int*>(x_ptr);
  *static_cast<int*>(out_ptr) = x * 2;
})",
    transform_stateful_counter_state_t{counter.ptr});

  const std::vector<int> input = generate<int>(num_items);
  const std::vector<int> output(num_items, 0);
  pointer_t<int> input_ptr(input);
  pointer_t<int> output_ptr(output);

  std::optional<transform_build_cache_t> build_cache = std::nullopt;
  std::optional<std::string> test_key                = std::nullopt;

  unary_transform(input_ptr, output_ptr, num_items, op, build_cache, test_key);

  std::vector<int> expected(num_items, 0);
  std::transform(input.begin(), input.end(), expected.begin(), [](int x) {
    return x * 2;
  });

  if (num_items > 0)
  {
    REQUIRE(expected == std::vector<int>(output_ptr));
    REQUIRE(counter[0] == static_cast<int>(num_items));
  }
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cccl.c.parallel.test.transform is a Catch2 v3.8.1 host application.
Run with -? for options

-------------------------------------------------------------------------------
"Transform works with stateful unary operators"(0) - types_668 - 0
-------------------------------------------------------------------------------
/home/ashwin/workspace/cccl/c/parallel/test/test_transform.cpp:668
...............................................................................

/home/ashwin/workspace/cccl/c/parallel/test/test_util.h:712: FAILED:
  REQUIRE( cudaSuccess == cudaFree(ptr) )
with expansion:
  0 (no error) == 716 (misaligned address)

terminate called after throwing an instance of 'Catch::TestFailureException'
/home/ashwin/workspace/cccl/c/parallel/test/test_util.h:712: FAILED:
  {Unknown expression after the reported line}
due to a fatal error condition:
  SIGABRT - Abort (abnormal termination) signal

===============================================================================
test cases:  1 |  0 passed | 1 failed
assertions: 36 | 34 passed | 2 failed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

  • Status
    Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions