Skip to content

Commit b70b473

Browse files
committed
Migrate Evaluator state to View; auto-bind in allocate
1 parent 0b4ab46 commit b70b473

108 files changed

Lines changed: 1258 additions & 929 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

GridKit/LinearAlgebra/MemoryUtils.hpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <algorithm>
34
#include <iostream>
45

56
namespace GridKit
@@ -206,8 +207,7 @@ namespace GridKit
206207
template <typename I, typename T>
207208
int allocateArrayOnHost(T** v, I n)
208209
{
209-
std::size_t arraysize = static_cast<std::size_t>(n) * sizeof(T);
210-
*v = new T[arraysize];
210+
*v = new T[static_cast<std::size_t>(n)]();
211211
return *v == nullptr ? 1 : 0;
212212
}
213213

@@ -222,16 +222,14 @@ namespace GridKit
222222
template <typename I, typename T>
223223
int copyArrayHostToHost(T* dst, const T* src, I n)
224224
{
225-
std::size_t arraysize = static_cast<std::size_t>(n) * sizeof(T);
226-
memcpy(dst, src, arraysize);
225+
std::copy_n(src, static_cast<std::size_t>(n), dst);
227226
return 0;
228227
}
229228

230229
template <typename I, typename T>
231230
int setZeroArrayOnHost(T* v, I n)
232231
{
233-
std::size_t arraysize = static_cast<std::size_t>(n) * sizeof(T);
234-
memset(v, 0, arraysize);
232+
std::fill_n(v, static_cast<std::size_t>(n), T{});
235233
return 0;
236234
}
237235

GridKit/LinearAlgebra/Vector/Vector.cpp

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <cassert>
2-
#include <cstring>
2+
#include <utility>
33

4+
#include <GridKit/AutomaticDifferentiation/DependencyTracking/Variable.hpp>
45
#include <GridKit/LinearAlgebra/Vector/Vector.hpp>
56
#include <GridKit/Utilities/Logger/Logger.hpp>
67

@@ -39,8 +40,8 @@ namespace GridKit
3940
: n_capacity_(n),
4041
k_(k),
4142
n_size_(n),
42-
gpu_updated_(new bool[k]),
43-
cpu_updated_(new bool[k])
43+
gpu_updated_(new bool[static_cast<std::size_t>(k)]),
44+
cpu_updated_(new bool[static_cast<std::size_t>(k)])
4445
{
4546
setHostUpdated(false);
4647
setDeviceUpdated(false);
@@ -52,13 +53,64 @@ namespace GridKit
5253
*/
5354
template <typename ScalarT, typename IdxT>
5455
Vector<ScalarT, IdxT>::~Vector()
56+
{
57+
release();
58+
}
59+
60+
template <typename ScalarT, typename IdxT>
61+
Vector<ScalarT, IdxT>::Vector(Vector&& other) noexcept
62+
{
63+
*this = std::move(other);
64+
}
65+
66+
template <typename ScalarT, typename IdxT>
67+
Vector<ScalarT, IdxT>& Vector<ScalarT, IdxT>::operator=(Vector&& other) noexcept
68+
{
69+
if (this != &other)
70+
{
71+
release();
72+
73+
n_capacity_ = other.n_capacity_;
74+
k_ = other.k_;
75+
n_size_ = other.n_size_;
76+
d_data_ = other.d_data_;
77+
h_data_ = other.h_data_;
78+
gpu_updated_ = other.gpu_updated_;
79+
cpu_updated_ = other.cpu_updated_;
80+
owns_gpu_data_ = other.owns_gpu_data_;
81+
owns_cpu_data_ = other.owns_cpu_data_;
82+
mem_ = std::move(other.mem_);
83+
84+
other.n_capacity_ = 0;
85+
other.k_ = 0;
86+
other.n_size_ = 0;
87+
other.d_data_ = nullptr;
88+
other.h_data_ = nullptr;
89+
other.gpu_updated_ = nullptr;
90+
other.cpu_updated_ = nullptr;
91+
}
92+
93+
return *this;
94+
}
95+
96+
template <typename ScalarT, typename IdxT>
97+
void Vector<ScalarT, IdxT>::release()
5598
{
5699
if (owns_cpu_data_ && h_data_)
100+
{
57101
mem_.deleteOnHost(h_data_);
102+
}
58103
if (owns_gpu_data_ && d_data_)
104+
{
59105
mem_.deleteOnDevice(d_data_);
106+
}
60107
delete[] gpu_updated_;
61108
delete[] cpu_updated_;
109+
110+
h_data_ = nullptr;
111+
d_data_ = nullptr;
112+
gpu_updated_ = nullptr;
113+
cpu_updated_ = nullptr;
62114
}
63115

64116
/**
@@ -999,9 +1051,12 @@ namespace GridKit
9991051
std::fill(gpu_updated_, gpu_updated_ + k_, is_updated);
10001052
}
10011053

1002-
// template class Vector<double, long int>;
1054+
template class Vector<double, long int>;
10031055
template class Vector<double, size_t>;
1004-
// template class Vector<double, int>;
1056+
template class Vector<double, int>;
1057+
template class Vector<DependencyTracking::Variable, long int>;
1058+
template class Vector<DependencyTracking::Variable, size_t>;
1059+
template class Vector<DependencyTracking::Variable, int>;
10051060

10061061
} // namespace LinearAlgebra
10071062
} // namespace GridKit

GridKit/LinearAlgebra/Vector/Vector.hpp

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#pragma once
2+
#include <cassert>
3+
#include <cstddef>
24
#include <string>
35

46
#include <GridKit/LinearAlgebra/MemoryUtils.hpp>
@@ -36,10 +38,10 @@ namespace GridKit
3638
Vector(IdxT n, IdxT k);
3739
~Vector();
3840

39-
Vector(const Vector&) = delete;
40-
Vector(Vector&&) = delete;
41+
Vector(const Vector&) = delete;
42+
Vector(Vector&& other) noexcept;
4143
Vector& operator=(const Vector&) = delete;
42-
Vector& operator=(Vector&&) = delete;
44+
Vector& operator=(Vector&& other) noexcept;
4345

4446
int copyFromExternal(const ScalarT* source,
4547
memory::MemorySpace memspaceIn = memory::HOST,
@@ -53,6 +55,38 @@ namespace GridKit
5355
const ScalarT* getData(memory::MemorySpace memspace = memory::HOST) const;
5456
const ScalarT* getData(IdxT i, memory::MemorySpace memspace = memory::HOST) const;
5557

58+
std::size_t size() const
59+
{
60+
return static_cast<std::size_t>(getSize());
61+
}
62+
63+
bool empty() const
64+
{
65+
return size() == 0;
66+
}
67+
68+
ScalarT* data(memory::MemorySpace memspace = memory::HOST)
69+
{
70+
return getData(memspace);
71+
}
72+
73+
const ScalarT* data(memory::MemorySpace memspace = memory::HOST) const
74+
{
75+
return getData(memspace);
76+
}
77+
78+
ScalarT& operator[](std::size_t i)
79+
{
80+
assert(i < size());
81+
return data()[i];
82+
}
83+
84+
const ScalarT& operator[](std::size_t i) const
85+
{
86+
assert(i < size());
87+
return data()[i];
88+
}
89+
5690
IdxT getCapacity() const;
5791
IdxT getSize() const;
5892
IdxT getNumVectors() const;
@@ -77,6 +111,7 @@ namespace GridKit
77111
memory::MemorySpace memspaceDst = memory::HOST);
78112

79113
private:
114+
void release();
80115
void setHostUpdated(bool is_updated);
81116
void setDeviceUpdated(bool is_updated);
82117

@@ -93,5 +128,6 @@ namespace GridKit
93128

94129
MemoryHandler mem_; ///< Device memory manager object
95130
};
131+
96132
} // namespace LinearAlgebra
97133
} // namespace GridKit

0 commit comments

Comments
 (0)