Skip to content

Commit e7e6b07

Browse files
committed
Migrate Evaluator state to View; auto-bind in allocate
1 parent 2892ddd commit e7e6b07

99 files changed

Lines changed: 1530 additions & 710 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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
gridkit_add_library(
22
dense_vector
33
SOURCES Vector.cpp
4-
HEADERS Vector.hpp
4+
HEADERS Vector.hpp View.hpp
55
LINK_LIBRARIES GridKit::utilities_logger)

GridKit/LinearAlgebra/Vector/Vector.cpp

Lines changed: 58 additions & 4 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,11 @@ 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>;
10041056
// template class Vector<double, int>;
1057+
template class Vector<DependencyTracking::Variable, long int>;
1058+
template class Vector<DependencyTracking::Variable, size_t>;
10051059

10061060
} // namespace LinearAlgebra
10071061
} // namespace GridKit

GridKit/LinearAlgebra/Vector/Vector.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ namespace GridKit
3636
Vector(IdxT n, IdxT k);
3737
~Vector();
3838

39-
Vector(const Vector&) = delete;
40-
Vector(Vector&&) = delete;
39+
Vector(const Vector&) = delete;
40+
Vector(Vector&& other) noexcept;
4141
Vector& operator=(const Vector&) = delete;
42-
Vector& operator=(Vector&&) = delete;
42+
Vector& operator=(Vector&& other) noexcept;
4343

4444
int copyFromExternal(const ScalarT* source,
4545
memory::MemorySpace memspaceIn = memory::HOST,
@@ -77,6 +77,7 @@ namespace GridKit
7777
memory::MemorySpace memspaceDst = memory::HOST);
7878

7979
private:
80+
void release();
8081
void setHostUpdated(bool is_updated);
8182
void setDeviceUpdated(bool is_updated);
8283

@@ -93,5 +94,6 @@ namespace GridKit
9394

9495
MemoryHandler mem_; ///< Device memory manager object
9596
};
97+
9698
} // namespace LinearAlgebra
9799
} // namespace GridKit

0 commit comments

Comments
 (0)