-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RFC] New version of CudaCompat #428
base: CMSSW_11_0_X_Patatrack
Are you sure you want to change the base?
Changes from 23 commits
f761369
466b1ae
69b6daa
f398b45
f7ec457
332cb45
0696159
34f3704
f8d97ab
c87c860
b26b7f4
e89d886
a7d242e
3a530f1
324667e
0740845
9391df8
70d3656
51f5342
04465cf
36b8343
6928c39
d685dfe
cf20a55
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#ifndef HeterogeneousCore_CUDAUtilities_interface_cpu_unique_ptr_h | ||
#define HeterogeneousCore_CUDAUtilities_interface_cpu_unique_ptr_h | ||
|
||
#include <memory> | ||
#include <functional> | ||
|
||
#include <cstdlib> | ||
#include <cuda_runtime.h> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. from a look at the file, I think |
||
|
||
namespace cudautils { | ||
namespace cpu { | ||
namespace impl { | ||
// Additional layer of types to distinguish from device:: and host::unique_ptr | ||
class CPUDeleter { | ||
public: | ||
CPUDeleter() = default; | ||
|
||
void operator()(void *ptr) { | ||
::free(ptr); | ||
} | ||
}; | ||
} // namespace impl | ||
|
||
template <typename T> | ||
using unique_ptr = std::unique_ptr<T, impl::CPUDeleter>; | ||
|
||
namespace impl { | ||
template <typename T> | ||
struct make_cpu_unique_selector { | ||
using non_array = cudautils::cpu::unique_ptr<T>; | ||
}; | ||
template <typename T> | ||
struct make_cpu_unique_selector<T[]> { | ||
using unbounded_array = cudautils::cpu::unique_ptr<T[]>; | ||
}; | ||
template <typename T, size_t N> | ||
struct make_cpu_unique_selector<T[N]> { | ||
struct bounded_array {}; | ||
}; | ||
} // namespace impl | ||
} // namespace cpu | ||
|
||
template <typename T> | ||
typename cpu::impl::make_cpu_unique_selector<T>::non_array make_cpu_unique(cudaStream_t) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. trying to better understand this: calling |
||
static_assert(std::is_trivially_constructible<T>::value, | ||
"Allocating with non-trivial constructor on the cpu memory is not supported"); | ||
void *mem = ::malloc(sizeof(T)); | ||
return typename cpu::impl::make_cpu_unique_selector<T>::non_array{reinterpret_cast<T *>(mem), | ||
cpu::impl::CPUDeleter()}; | ||
} | ||
|
||
template <typename T> | ||
typename cpu::impl::make_cpu_unique_selector<T>::unbounded_array make_cpu_unique(size_t n, cudaStream_t) { | ||
using element_type = typename std::remove_extent<T>::type; | ||
static_assert(std::is_trivially_constructible<element_type>::value, | ||
"Allocating with non-trivial constructor on the cpu memory is not supported"); | ||
void *mem = ::malloc(n * sizeof(element_type)); | ||
return typename cpu::impl::make_cpu_unique_selector<T>::unbounded_array{reinterpret_cast<element_type *>(mem), | ||
cpu::impl::CPUDeleter()}; | ||
} | ||
|
||
template <typename T, typename... Args> | ||
typename cpu::impl::make_cpu_unique_selector<T>::bounded_array make_cpu_unique(Args &&...) = delete; | ||
|
||
// No check for the trivial constructor, make it clear in the interface | ||
template <typename T> | ||
typename cpu::impl::make_cpu_unique_selector<T>::non_array make_cpu_unique_uninitialized(cudaStream_t) { | ||
void *mem = ::malloc(sizeof(T)); | ||
return typename cpu::impl::make_cpu_unique_selector<T>::non_array{reinterpret_cast<T *>(mem), | ||
cpu::impl::CPUDeleter()}; | ||
} | ||
|
||
template <typename T> | ||
typename cpu::impl::make_cpu_unique_selector<T>::unbounded_array make_cpu_unique_uninitialized(size_t n, cudaStream_t) { | ||
using element_type = typename std::remove_extent<T>::type; | ||
void *mem = ::malloc(n * sizeof(element_type)); | ||
return typename cpu::impl::make_cpu_unique_selector<T>::unbounded_array{reinterpret_cast<element_type *>(mem), | ||
cpu::impl::CPUDeleter()}; | ||
} | ||
|
||
template <typename T, typename... Args> | ||
typename cpu::impl::make_cpu_unique_selector<T>::bounded_array make_cpu_unique_uninitialized(Args &&...) = | ||
delete; | ||
} // namespace cudautils | ||
|
||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,10 +94,14 @@ namespace cudautils { | |
} // namespace detail | ||
|
||
// wrappers for cudaLaunchKernel | ||
|
||
inline | ||
fwyzard marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will add the |
||
void launch(void (*kernel)(), LaunchParameters config) { | ||
#ifdef CUDA_KERNELS_ON_CPU | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but I really, really do not want to add a dependency on #ifdefs etc. here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will find a less intrusive solution |
||
kernel(); | ||
#else | ||
cudaCheck(cudaLaunchKernel( | ||
(const void*)kernel, config.gridDim, config.blockDim, nullptr, config.sharedMem, config.stream)); | ||
#endif | ||
} | ||
|
||
template <typename F, typename... Args> | ||
|
@@ -107,6 +111,9 @@ namespace cudautils { | |
std::enable_if_t<std::is_void<std::result_of_t<F && (Args && ...)> >::value> | ||
#endif | ||
launch(F* kernel, LaunchParameters config, Args&&... args) { | ||
#ifdef CUDA_KERNELS_ON_CPU | ||
kernel(args...); | ||
#else | ||
using function_type = detail::kernel_traits<F>; | ||
typename function_type::argument_type_tuple args_copy(args...); | ||
|
||
|
@@ -116,10 +123,11 @@ namespace cudautils { | |
detail::pointer_setter<size>()(pointers, args_copy); | ||
cudaCheck(cudaLaunchKernel( | ||
(const void*)kernel, config.gridDim, config.blockDim, (void**)pointers, config.sharedMem, config.stream)); | ||
#endif | ||
} | ||
|
||
// wrappers for cudaLaunchCooperativeKernel | ||
|
||
inline | ||
fwyzard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
void launch_cooperative(void (*kernel)(), LaunchParameters config) { | ||
cudaCheck(cudaLaunchCooperativeKernel( | ||
(const void*)kernel, config.gridDim, config.blockDim, nullptr, config.sharedMem, config.stream)); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am curious if using
cudaStreamDefault
was giving problems ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no,to me nullptr makes more sense for a pointer (even if they are all == 0 )