Skip to content

Commit

Permalink
Implement C API for device reduction (#2256)
Browse files Browse the repository at this point in the history
* Implement C device reduce

* Format

* Fix device-specific module loading

* Don't need a context at build step now

* Address review feedback
  • Loading branch information
gevtushenko authored Aug 21, 2024
1 parent 1e1af8d commit 5a4881b
Show file tree
Hide file tree
Showing 10 changed files with 1,686 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ option(CCCL_ENABLE_THRUST "Enable the Thrust developer build." ${CCCL_TOPLEVEL_P
option(CCCL_ENABLE_TESTING "Enable CUDA C++ Core Library tests." ${CCCL_TOPLEVEL_PROJECT})
option(CCCL_ENABLE_EXAMPLES "Enable CUDA C++ Core Library examples." ${CCCL_TOPLEVEL_PROJECT})
option(CCCL_ENABLE_BENCHMARKS "Enable CUDA C++ Core Library benchmarks." OFF)
option(CCCL_ENABLE_C "Enable CUDA C Core Library." OFF)

option(CCCL_ENABLE_UNSTABLE "Enable targets and developer build options for unstable projects." OFF)

Expand Down Expand Up @@ -77,6 +78,10 @@ if (CCCL_ENABLE_UNSTABLE)
add_subdirectory(cudax)
endif()

if (CCCL_ENABLE_C)
add_subdirectory(c)
endif()

if (CCCL_ENABLE_TESTING)
add_subdirectory(test)
endif()
Expand Down
1 change: 1 addition & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"CCCL_ENABLE_TESTING": true,
"CCCL_ENABLE_EXAMPLES": true,
"CCCL_ENABLE_BENCHMARKS": true,
"CCCL_ENABLE_C": true,
"LIBCUDACXX_ENABLE_LIBCUDACXX_TESTS": true,
"CUB_ENABLE_TESTING": true,
"CUB_ENABLE_EXAMPLES": true,
Expand Down
20 changes: 20 additions & 0 deletions c/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
cmake_minimum_required(VERSION 3.30)

project(cccl.c LANGUAGES CUDA CXX)

add_library(cccl.c SHARED src/reduce.cu)
set_property(TARGET cccl.c PROPERTY POSITION_INDEPENDENT_CODE ON)
set_property(TARGET cccl.c PROPERTY CXX_STANDARD 20)
set_property(TARGET cccl.c PROPERTY CUDA_STANDARD 20)

find_package(CUDAToolkit REQUIRED)

# TODO Use static versions of cudart, nvrtc, and nvJitLink
target_link_libraries(cccl.c PRIVATE CUDA::cudart
CUDA::nvrtc
CUDA::nvJitLink
CUDA::cuda_driver)
target_compile_definitions(cccl.c PRIVATE NVRTC_GET_TYPE_NAME=1 CCCL_C_EXPERIMENTAL=1)
target_include_directories(cccl.c PUBLIC "include")

add_subdirectory(test)
59 changes: 59 additions & 0 deletions c/include/cccl/reduce.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//===----------------------------------------------------------------------===//
//
// Part of CUDA Experimental in CUDA C++ Core Libraries,
// under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//

#pragma once

#ifndef CCCL_C_EXPERIMENTAL
# warning "C exposure is experimental and subject to change. Define CCCL_C_EXPERIMENTAL to acknowledge this warning."
#else // ^^^ !CCCL_C_EXPERIMENTAL ^^^ / vvv CCCL_C_EXPERIMENTAL vvv

# include <cuda.h>

# include <cccl/types.h>

struct cccl_device_reduce_build_result_t
{
int cc;
void* cubin;
size_t cubin_size;
CUlibrary library;
CUkernel single_tile_kernel;
CUkernel single_tile_second_kernel;
CUkernel reduction_kernel;
};

// TODO return a union of nvtx/cuda/nvrtc errors or a string?
extern "C" CCCL_C_API CUresult cccl_device_reduce_build(
cccl_device_reduce_build_result_t* build,
cccl_iterator_t d_in,
cccl_iterator_t d_out,
cccl_op_t op,
cccl_value_t init,
int cc_major,
int cc_minor,
const char* cub_path,
const char* thrust_path,
const char* libcudacxx_path,
const char* ctk_path) noexcept;

extern "C" CCCL_C_API CUresult cccl_device_reduce(
cccl_device_reduce_build_result_t build,
void* d_temp_storage,
size_t* temp_storage_bytes,
cccl_iterator_t d_in,
cccl_iterator_t d_out,
unsigned long long num_items,
cccl_op_t op,
cccl_value_t init,
CUstream stream) noexcept;

extern "C" CCCL_C_API CUresult cccl_device_reduce_cleanup(cccl_device_reduce_build_result_t* bld_ptr);

#endif // CCCL_C_EXPERIMENTAL
85 changes: 85 additions & 0 deletions c/include/cccl/types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//===----------------------------------------------------------------------===//
//
// Part of CUDA Experimental in CUDA C++ Core Libraries,
// under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//

#pragma once

#ifndef CCCL_C_EXPERIMENTAL
# warning "C exposure is experimental and subject to change. Define CCCL_C_EXPERIMENTAL to acknowledge this warning."
#else // ^^^ !CCCL_C_EXPERIMENTAL ^^^ / vvv CCCL_C_EXPERIMENTAL vvv

# if defined(_WIN32)
# define CCCL_C_API __declspec(dllexport)
# else
# define CCCL_C_API __attribute__((visibility("default")))
# endif

enum class cccl_type_enum
{
INT8 = 0,
INT16 = 1,
INT32 = 2,
INT64 = 3,
UINT8 = 4,
UINT16 = 5,
UINT32 = 6,
UINT64 = 7,
FLOAT32 = 8,
FLOAT64 = 9,
STORAGE = 10
};

struct cccl_type_info
{
int size;
int alignment;
cccl_type_enum type;
};

enum class cccl_op_kind_t
{
stateless,
stateful
};

struct cccl_op_t
{
cccl_op_kind_t type;
const char* name;
const char* ltoir;
int ltoir_size;
int size;
int alignment;
void* state;
};

enum class cccl_iterator_kind_t
{
pointer,
iterator
};

struct cccl_value_t
{
cccl_type_info type;
void* state;
};

struct cccl_iterator_t
{
int size;
int alignment;
cccl_iterator_kind_t type;
cccl_op_t advance;
cccl_op_t dereference;
cccl_type_info value_type;
void* state;
};

#endif // CCCL_C_EXPERIMENTAL
Loading

0 comments on commit 5a4881b

Please sign in to comment.