Skip to content

Commit f818cc3

Browse files
liu15adayton1
andauthored
Deep copy (#303)
* Add deep copy option to avoid the need for xnack+ --------- Co-authored-by: Alan Dayton <6393677+adayton1@users.noreply.github.com>
1 parent 9556e4d commit f818cc3

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

RELEASE_NOTES.md

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ The format of this file is based on [Keep a Changelog](http://keepachangelog.com
1414

1515
## [Unreleased] - Release date YYYY-MM-DD
1616

17+
### Added
18+
- Added CARE\_DEEP\_COPY\_RAW\_PTR configuration option.
19+
1720
### Added
1821
- Added ATOMIC\_SUB, ATOMIC\_LOAD, ATOMIC\_STORE, ATOMIC\_EXCHANGE, and ATOMIC\_CAS macros.
1922
- Added TSAN\_ONLY\_ATOMIC\_\* macros to suppress tsan data race reports. Controlled by CARE\_ENABLE\_TSAN\_ONLY\_ATOMICS configuration option.

cmake/SetupOptions.cmake

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ option(CARE_ENABLE_IMPLICIT_CONVERSIONS "Enable implicit conversions to-from raw
1919
# CHAI must also be configured with the same settings for implicit conversions.
2020
set(CHAI_ENABLE_IMPLICIT_CONVERSIONS ${CARE_ENABLE_IMPLICIT_CONVERSIONS} CACHE BOOL "Enable implicit conversions to-from raw pointers")
2121
option(CARE_LEGACY_COMPATIBILITY_MODE "Enable legacy compatibility mode" OFF)
22+
option(CARE_DEEP_COPY_RAW_PTR "Use deep copy for managed array initialization from raw pointer" OFF)
2223
option(CARE_ENABLE_MANAGED_PTR "Enable managed_ptr aliases, tests, and reproducer" ON)
2324
option(CARE_DISABLE_RAJAPLUGIN "Disable use of the RAJA plugin. WILL ALSO DISABLE MEMORY MOTION." OFF)
2425
option(CARE_ENABLE_EXTERN_INSTANTIATE "Enable extern instantiation of template functions" OFF)

src/care/config.h.in

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#ifndef CARE_LEGACY_COMPATIBILITY_MODE
2020
#cmakedefine01 CARE_LEGACY_COMPATIBILITY_MODE
2121
#endif
22+
#cmakedefine CARE_DEEP_COPY_RAW_PTR
2223
#cmakedefine CARE_ENABLE_MANAGED_PTR
2324
#cmakedefine CARE_DISABLE_RAJAPLUGIN
2425
#cmakedefine CARE_ENABLE_EXTERN_INSTANTIATE

src/care/host_device_ptr.h

+22-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
// Std library headers
2424
#include <cstddef>
2525

26+
2627
namespace care {
2728
///////////////////////////////////////////////////////////////////////////
2829
/// @struct _kv
@@ -138,13 +139,20 @@ namespace care {
138139
{
139140
}
140141

141-
#if defined (CHAI_DISABLE_RM) || defined(CHAI_THIN_GPU_ALLOCATE)
142142
///
143143
/// @author Peter Robinson
144144
///
145145
/// Construct from a raw pointer, size, and name
146146
/// This is defined when the CHAI resource manager is disabled
147147
///
148+
#if defined(CARE_DEEP_COPY_RAW_PTR)
149+
host_device_ptr<T>(T* from, size_t size, const char * name)
150+
: MA(size)
151+
{
152+
std::copy_n(from, size, (T_non_const*)MA::data());
153+
}
154+
#else /* defined(CARE_DEEP_COPY_RAW_PTR) */
155+
#if defined (CHAI_DISABLE_RM) || defined(CHAI_THIN_GPU_ALLOCATE)
148156
host_device_ptr<T>(T* from, size_t size, const char * name)
149157
: MA(from, nullptr, size, nullptr)
150158
{
@@ -173,6 +181,7 @@ namespace care {
173181
}
174182
}
175183
#endif
184+
#endif /* defined(CARE_DEEP_COPY_RAW_PTR) */
176185

177186
///
178187
/// @author Peter Robinson
@@ -412,6 +421,16 @@ namespace care {
412421
void freeDeviceMemory(T_non_const ** CPU_destination,
413422
size_t elems,
414423
bool deregisterPointer=true) {
424+
#if defined(CARE_DEEP_COPY_RAW_PTR)
425+
// if there is a pointer to update ...
426+
if (CPU_destination != nullptr) {
427+
if (*CPU_destination == nullptr) {
428+
*CPU_destination = (T_non_const *) std::malloc(elems*sizeof(T));
429+
}
430+
std::copy_n(MA::cdata(), elems, *CPU_destination);
431+
}
432+
MA::free();
433+
#else /* defined(CARE_DEEP_COPY_RAW_PTR) */
415434
#if !defined(CHAI_DISABLE_RM)
416435
#if defined(CHAI_GPUCC) || CARE_ENABLE_GPU_SIMULATION_MODE
417436
if (CPU_destination != nullptr) {
@@ -441,6 +460,7 @@ namespace care {
441460
arrayManager->deregisterPointer(MA::m_pointer_record,true);
442461
CHAICallback::deregisterRecord(MA::m_pointer_record);
443462
}
463+
444464
#else // no resource manager active
445465
#if defined(CHAI_THIN_GPU_ALLOCATE) // GPU allocated thin wrapped
446466
// ... then sync to ensure data is up to date
@@ -464,6 +484,7 @@ namespace care {
464484
}
465485
}
466486
#endif
487+
#endif /* defined(CARE_DEEP_COPY_RAW_PTR) */
467488
}
468489

469490
CARE_HOST_DEVICE void pick(int idx, T_non_const& val) const {

0 commit comments

Comments
 (0)