-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathArrayManager.inl
More file actions
100 lines (77 loc) · 3.22 KB
/
Copy pathArrayManager.inl
File metadata and controls
100 lines (77 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2016-19, Lawrence Livermore National Security, LLC and CHAI
// project contributors. See the COPYRIGHT file for details.
//
// SPDX-License-Identifier: BSD-3-Clause
//////////////////////////////////////////////////////////////////////////////
#ifndef CHAI_ArrayManager_INL
#define CHAI_ArrayManager_INL
#include "chai/config.hpp"
#include "chai/ArrayManager.hpp"
#include "chai/ChaiMacros.hpp"
#include <iostream>
#include "umpire/ResourceManager.hpp"
#if defined(CHAI_ENABLE_UM)
#include <cuda_runtime_api.h>
#endif
namespace chai {
template<typename T>
CHAI_INLINE
void* ArrayManager::reallocate(void* pointer, size_t elems, PointerRecord* pointer_record)
{
ExecutionSpace my_space;
for (int space = CPU; space < NUM_EXECUTION_SPACES; ++space) {
if (pointer_record->m_pointers[space] == pointer) {
my_space = static_cast<ExecutionSpace>(space);
}
}
for (int space = CPU; space < NUM_EXECUTION_SPACES; ++space) {
if(!pointer_record->m_owned[space]) {
CHAI_LOG(Debug, "Cannot reallocate unowned pointer");
return pointer_record->m_pointers[my_space];
}
}
// only copy however many bytes overlap
size_t num_bytes_to_copy = std::min(sizeof(T)*elems, pointer_record->m_size);
for (int space = CPU; space < NUM_EXECUTION_SPACES; ++space) {
void* old_ptr = pointer_record->m_pointers[space];
pointer_record->m_user_callback(ACTION_ALLOC, ExecutionSpace(space), sizeof(T) * elems);
void* new_ptr = m_allocators[space]->allocate(sizeof(T)*elems);
if (old_ptr) {
m_resource_manager.copy(new_ptr, old_ptr, num_bytes_to_copy);
}
pointer_record->m_user_callback(ACTION_FREE, ExecutionSpace(space), sizeof(T) * elems);
if (old_ptr) {
m_allocators[space]->deallocate(old_ptr);
}
pointer_record->m_pointers[space] = new_ptr;
if (old_ptr) {
m_pointer_map.erase(old_ptr);
}
m_pointer_map.insert(new_ptr, pointer_record);
}
pointer_record->m_size = sizeof(T) * elems;
return pointer_record->m_pointers[my_space];
}
#if defined(CHAI_ENABLE_PICK)
template<typename T>
CHAI_INLINE
typename ArrayManager::T_non_const<T> ArrayManager::pick(T* src_ptr, size_t index)
{
T_non_const<T> val;
m_resource_manager.registerAllocation(const_cast<T_non_const<T>*>(&val), new umpire::util::AllocationRecord{const_cast<T_non_const<T>*>(&val), sizeof(T), m_resource_manager.getAllocator("HOST").getAllocationStrategy()});
m_resource_manager.copy(const_cast<T_non_const<T>*>(&val), const_cast<T_non_const<T>*>(src_ptr+index), sizeof(T));
m_resource_manager.deregisterAllocation(&val);
return val;
}
template<typename T>
CHAI_INLINE
void ArrayManager::set(T* dst_ptr, size_t index, const T& val)
{
m_resource_manager.registerAllocation(const_cast<T_non_const<T>*>(&val), new umpire::util::AllocationRecord{const_cast<T_non_const<T>*>(&val), sizeof(T), m_resource_manager.getAllocator("HOST").getAllocationStrategy()});
m_resource_manager.copy(const_cast<T_non_const<T>*>(dst_ptr+index), const_cast<T_non_const<T>*>(&val), sizeof(T));
m_resource_manager.deregisterAllocation(const_cast<T_non_const<T>*>(&val));
}
#endif
} // end of namespace chai
#endif // CHAI_ArrayManager_INL