Repository navigation
kokkos - add /gpu/cuda/kokkos backend with Kokkos CeedVector - #2135
pradeep80555 wants to merge 17 commits into
Conversation
…-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
…oy and HasValidArray
…alize Kokkos at exit
| // 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 |
There was a problem hiding this comment.
| CEED_BACKEND(CeedRegister_Kokkos, 1, "/gpu/cuda/kokkos") | |
| CEED_BACKEND(CeedRegister_Kokkos, 1, "/gpu/cuda/kokkos") | |
minor
| $(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))) |
There was a problem hiding this comment.
| $(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 |
There was a problem hiding this comment.
| 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 |
There was a problem hiding this comment.
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
- CeedVectorSetArray a user provided pointer
- Do some work in libCEED with that pointer
- CeedVectorTakeArray that user provided pointer
- 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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Thanks, that makes sense. I understand both the issues :
- creating a DualView from two views that don't actually match (DropBorrowed / USE_POINTER)
- 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.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Yup, that sounds accurate. Thanks
Purpose:
Step 1 of #2065 : a new
/gpu/cuda/kokkosbackend. It delegates to/gpu/cuda/reffor all the non-implemented aspects and replaces all the CeedVector backend functions with kokkos versions (Kokkos::DualView + handwritten parallel_for / parallel_reduceonDefaultExecutionSpaceas discussed in the issue thread).Build :
Kokkos built with CUDA, then
make KOKKOS_DIR=.... , CUDA_DIR=.... , CUDA_ARCH=sm_xx.CUDA_ARCHmatters: 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 okmake prove search=t: 273/273 pass on /gpu/cuda/kokkosSome design choices :
Kokkos::filnalize()is called withatexit.IndexType<CeedSize>) CUDA's default is a 32-bit int.-MMD -MPare filtered out (nvcc_wrapperpasses
-MPalone to the host compiler, which gives errors), andCUDA_ARCHis 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.