Skip to content

kokkos - add /gpu/cuda/kokkos backend with Kokkos CeedVector - #2135

Open
pradeep80555 wants to merge 17 commits into
CEED:mainfrom
pradeep80555:pradeep/kokkos-backend
Open

pradeep80555 wants to merge 17 commits into
CEED:mainfrom
pradeep80555:pradeep/kokkos-backend

Conversation

@pradeep80555

@pradeep80555 pradeep80555 commented Dec 3, 2026 •

Copy link
Copy Markdown

Purpose:

Step 1 of #2065 : a new /gpu/cuda/kokkos backend. It delegates to /gpu/cuda/ref for all the non-implemented aspects and replaces all the CeedVector backend functions with kokkos versions (Kokkos::DualView + handwritten parallel_for / parallel_reduce on DefaultExecutionSpace as discussed in the issue thread).

Build :
Kokkos built with CUDA, then make KOKKOS_DIR=.... , CUDA_DIR=.... , CUDA_ARCH=sm_xx. CUDA_ARCH matters: coz without it nvcc_wrapper builds for sm_80 and the kernels fails with "no kernel image" on other GPUs.

Tested on a rented remote RTX 2080 Ti (sm_75, cuda 12.6, kokkos 5.3.0):

  • make test search=t1 BACKENDS=/gpu/cuda/kokkos: 55/55 ok
  • make prove search=t: 273/273 pass on /gpu/cuda/kokkos
  • no test uses SetArray with OWN_POINTER, so I checked it (host + device) with a scratch program and compute-sanitizer reports 0 errors for it and for t121, t127

Some design choices :

  1. Kokkos kernels run on the null stream, same as cuda-ref, so ordering with cuda-ref kernels (fallback ones apart from Kokkos CeedVector) is automatic (future TODO : use Kokkos' own stream once the full backend is implemented).
  2. Kokkos is initialized by the backend if needed(respects the device_id) and if it is the backend that initialized it, Kokkos::filnalize() is called with atexit.
  3. USE_POINTER: the user's array is wrapped in an unmanaged View and becomes that side of the DualView. Each side is tracked separately like cuda-ref: COPY_VALUES copies into the existing array , OWN_POINTER/ USE_POINTER replaces only the side being set.
  4. OWN_POINTER: Copy into our memory, then free the user's array.
  5. Math kernels only run on device and fallback to cpu code in the interface.
  6. any other vectors apart from the ones our particular backend might own, are read or written via the interface's get/restore calles, so that the backend never the other vectors internals and can use the interface's locks.
  7. 64-bit RangePolicy index (IndexType<CeedSize>) CUDA's default is a 32-bit int.
  8. Makefile: Kokkos files are compiled with nvcc_wrapper: -MMD -MP are filtered out (nvcc_wrapper
    passes -MP alone to the host compiler, which gives errors), and CUDA_ARCH is passed as -arch.

Closes: step 1 of #2065

LLM/GenAI Disclosure:

Describe any LLM and GenAI usage here.
I used Claude as a tutor, reviewer. It suggested the classes and functions I could use to achive an objective. Also, took direct help with initial folder structure, Makefile changes and code for first few (3-4) memory and kernel functions in ceed-kokkos-vector.cpp . Also, some code changes during review and errors. Most of the backend is my own code ( about 60-70% ). I have reviewed, tested and can explain the part I took help for.

By submitting this PR, the author certifies to its contents as described by the Developer's Certificate of Origin.
Please follow the Contributing Guidelines for all PRs.

PRADEEP ALAPATI added 17 commits December 2, 2026 21:57
…-list-kokkos.h, backends/ceed-backend-list.h and backends/weak/ceed-kokkos-weak.c

Added a Kokkos backend registered as /gpu/cuda/kokkos (priority 120).
The backend initializes Kokkos if it is not already initialized and
delegates all objects to /gpu/cuda/ref for now.
CeedVector support
with Kokkos::DualView is next.

Also added the backend list entry and a weak registration so that
a build without Kokkos report the backend as not compiled.

Took magma code as reference for most of these.

Makefile - Added KOKKOS_DIR build support for kokkos backend

Enable /gpu/cuda/kokkos only when KOKKOS_DIR points to a Kokkos install
built with CUDA (detected via KOKKOS_ENABLE_CUDA in
KokkosCore_config.h) and CUDA is available.

Kokkos source files are compiled with nvcc_wrapper and -std=c++20
--extended-lambda (overrides KOKKOS_CXXFLAGS). Added KOKKOS_DIR to make info and the
saved build configuration.

Not yet tested with a CUDA-enabled Kokkos build yet.

Issue CEED#2065
// This will be expanded inside CeedRegisterAll() to call each registration function.
// This is also used to create weakly linked registration functions in `backends/weak/ceed-*-weak.c'.

CEED_BACKEND(CeedRegister_Kokkos, 1, "/gpu/cuda/kokkos") No newline at end of file

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
CEED_BACKEND(CeedRegister_Kokkos, 1, "/gpu/cuda/kokkos")
CEED_BACKEND(CeedRegister_Kokkos, 1, "/gpu/cuda/kokkos")

minor

Comment thread Makefile
$(info SYCL_DIR = $(SYCL_DIR)$(call backend_status,$(SYCL_BACKENDS)))
$(info MAGMA_DIR = $(MAGMA_DIR)$(call backend_status,$(MAGMA_BACKENDS)))
$(info CUTLASS_DIR = $(CUTLASS_DIR)$(call backend_status,$(CUTLASS_BACKENDS)))
$(info CUTLASS_DIR = $(CUTLASS_DIR)$(call backend_status,$(CUTLASS_BACKENDS)))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$(info CUTLASS_DIR = $(CUTLASS_DIR)$(call backend_status,$(CUTLASS_BACKENDS)))
$(info CUTLASS_DIR = $(CUTLASS_DIR)$(call backend_status,$(CUTLASS_BACKENDS)))

using CeedRangePolicy_Kokkos = Kokkos::RangePolicy<Kokkos::DefaultExecutionSpace, Kokkos::IndexType<CeedSize>>;

typedef struct {
CeedDualView_Kokkos *dv; // pointer to the DualView

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
CeedDualView_Kokkos *dv; // pointer to the DualView
CeedDualView_Kokkos *dual_view; // pointer to the DualView

minor style thing - we prefer words in variable names whenever possible

switch (mem_type) {
case CEED_MEM_HOST: {
if (!impl->h_array_borrowed) return CEED_ERROR_SUCCESS;
CeedDualView_Kokkos::t_dev d = impl->dv->view_device(); // keep the device side

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kokkos Dual Views are supposed to have both memory spaces in sync when created, so these need to be synced to be in spec.

I think that this automatic recreation of a dual view creates a bit of an issue here perf wise. Here's why

A usual workflow is

  1. CeedVectorSetArray a user provided pointer
  2. Do some work in libCEED with that pointer
  3. CeedVectorTakeArray that user provided pointer
  4. Go back to 1

So, in this case, we do not actually need (or want) to pay the cost of allocating replacement memory. We should only allocate replacement memory if usage of that memory space is requested. I think we need to switch up the design a bit to have the Vector struct hold

  Kokkos::View<CeedScalar *, Kokkos::HostSpace> view_host,
  Kokkos::View<CeedScalar *>                    view_device,
  Kokkos::DualView<CeedScalar *>                view_dual,

then, we 1) use view_dual if present, 2) use view_* if its present, 3) we [re]create the view_dual otherwise

what do you think?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That, I think, will be rather invasive in this file, so we want to make sure we have a good idea of how we want to do this before changing

@pradeep80555 pradeep80555 Dec 3, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that makes sense. I understand both the issues :

  1. creating a DualView from two views that don't actually match (DropBorrowed / USE_POINTER)
  2. allocating memory we don't need (both sides at Create, and a replacement host array on every TakeArray).

I like the 3-view idea: user arrays in view_host / view_device, our own memory in view_dual, created only when needed.

One question before I start: if the user gives only a host array (USE_POINTER) and a kernel needs it on the device, should I allocate a view_device and deep_copy by hand, or build view_dual at that point and sync through it? I'm leaning towards deep_cpoy by hand, since I can't be sure of how the user would use it.

I'll also do the dual_view rename and the two small fixes.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my understanding of Kokkos is that its preferred to deep_copy ourselves and then create the dual view. I'm not convinced its the absolute best practices, but I played around with this here: https://github.com/jeremylt/legacy-kokkos-integration-demo/blob/main/GameOfLife_Kokkos.cpp (note, this only assumes receiving host arrays, so its not a exact match to what's happening here)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice ! the demo helps . So the pattern is : Keep the view the user gave us. Only if the other side is needed, we use cretae_mirror_view_and_copy, then build the DualView from the two views (they match at that point). After that, we can maybe use sync/modify as usual. Ill do that and mirror it for the device arrays.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, that sounds accurate. Thanks

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants