Skip to content

Commit 9c61a08

Browse files
committed
remove uncessary changes to Vector class
1 parent d2636cc commit 9c61a08

8 files changed

Lines changed: 69 additions & 179 deletions

File tree

GridKit/LinearAlgebra/Vector/Vector.cpp

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include <cassert>
2-
#include <utility>
32

43
#include <GridKit/AutomaticDifferentiation/DependencyTracking/Variable.hpp>
54
#include <GridKit/LinearAlgebra/Vector/Vector.hpp>
@@ -53,48 +52,6 @@ namespace GridKit
5352
*/
5453
template <typename ScalarT, typename IdxT>
5554
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()
9855
{
9956
if (owns_cpu_data_ && h_data_)
10057
{

GridKit/LinearAlgebra/Vector/Vector.hpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ namespace GridKit
4242
Vector(IdxT n, IdxT k);
4343
~Vector();
4444

45-
Vector(const Vector&) = delete;
46-
Vector(Vector&& other) noexcept;
45+
Vector(const Vector&) = delete;
46+
Vector(Vector&&) = delete;
4747
Vector& operator=(const Vector&) = delete;
48-
Vector& operator=(Vector&& other) noexcept;
48+
Vector& operator=(Vector&&) = delete;
4949

5050
int copyFromExternal(const ScalarT* source,
5151
memory::MemorySpace memspaceIn = memory::HOST,
@@ -64,11 +64,6 @@ namespace GridKit
6464
return static_cast<std::size_t>(getSize());
6565
}
6666

67-
bool empty() const
68-
{
69-
return size() == 0;
70-
}
71-
7267
ScalarT* data(memory::MemorySpace memspace = memory::HOST)
7368
{
7469
return getData(memspace);
@@ -115,7 +110,6 @@ namespace GridKit
115110
memory::MemorySpace memspaceDst = memory::HOST);
116111

117112
private:
118-
void release();
119113
void setHostUpdated(bool is_updated);
120114
void setDeviceUpdated(bool is_updated);
121115

GridKit/Model/Evaluator.hpp

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ namespace GridKit
126126
/**
127127
* @brief Get the absolute tolerance for each variable in the model
128128
*
129-
* @return a view of the absolute tolerance vector.
129+
* @return the absolute tolerance vector.
130130
*
131131
* @pre `setAbsoluteTolerance` must have been called first.
132132
*/
@@ -138,7 +138,7 @@ namespace GridKit
138138
/**
139139
* @brief Get the absolute tolerance for each variable in the model
140140
*
141-
* @return a const view of the absolute tolerance vector.
141+
* @return the absolute tolerance vector.
142142
*
143143
* @pre `setAbsoluteTolerance` must have been called first.
144144
*/
@@ -225,26 +225,27 @@ namespace GridKit
225225
virtual void bind(VectorT& y, VectorT& yp, VectorT& f, VectorT& tag, VectorT& abs_tol, IdxT offset)
226226
{
227227
const IdxT n = size();
228-
assert(static_cast<std::size_t>(offset) + static_cast<std::size_t>(n) <= y.size());
229-
assert(static_cast<std::size_t>(offset) + static_cast<std::size_t>(n) <= yp.size());
230-
assert(static_cast<std::size_t>(offset) + static_cast<std::size_t>(n) <= f.size());
231-
assert(static_cast<std::size_t>(offset) + static_cast<std::size_t>(n) <= tag.size());
232-
assert(static_cast<std::size_t>(offset) + static_cast<std::size_t>(n) <= abs_tol.size());
228+
assert(!allocated_);
229+
assert(offset + n <= y.getSize());
230+
assert(offset + n <= yp.getSize());
231+
assert(offset + n <= f.getSize());
232+
assert(offset + n <= tag.getSize());
233+
assert(offset + n <= abs_tol.getSize());
233234

234-
y_ = VectorT(n);
235-
y_.setData(y.data() + offset);
235+
y_.resize(n);
236+
y_.setData(y.getData(memory::HOST) + offset);
236237

237-
yp_ = VectorT(n);
238-
yp_.setData(yp.data() + offset);
238+
yp_.resize(n);
239+
yp_.setData(yp.getData(memory::HOST) + offset);
239240

240-
f_ = VectorT(n);
241-
f_.setData(f.data() + offset);
241+
f_.resize(n);
242+
f_.setData(f.getData(memory::HOST) + offset);
242243

243-
tag_ = VectorT(n);
244-
tag_.setData(tag.data() + offset);
244+
tag_.resize(n);
245+
tag_.setData(tag.getData(memory::HOST) + offset);
245246

246-
abs_tol_ = VectorT(n);
247-
abs_tol_.setData(abs_tol.data() + offset);
247+
abs_tol_.resize(n);
248+
abs_tol_.setData(abs_tol.getData(memory::HOST) + offset);
248249

249250
offset_ = offset;
250251
allocated_ = true;
@@ -256,25 +257,25 @@ namespace GridKit
256257
*/
257258
void allocateVectors(IdxT n)
258259
{
259-
y_ = VectorT(n);
260+
y_.resize(n);
260261
y_.allocate(memory::HOST);
261-
y_.setDataUpdated(memory::HOST);
262+
y_.setToZero(memory::HOST);
262263

263-
yp_ = VectorT(n);
264+
yp_.resize(n);
264265
yp_.allocate(memory::HOST);
265-
yp_.setDataUpdated(memory::HOST);
266+
yp_.setToZero(memory::HOST);
266267

267-
f_ = VectorT(n);
268+
f_.resize(n);
268269
f_.allocate(memory::HOST);
269-
f_.setDataUpdated(memory::HOST);
270+
f_.setToZero(memory::HOST);
270271

271-
tag_ = VectorT(n);
272+
tag_.resize(n);
272273
tag_.allocate(memory::HOST);
273-
tag_.setDataUpdated(memory::HOST);
274+
tag_.setToZero(memory::HOST);
274275

275-
abs_tol_ = VectorT(n);
276+
abs_tol_.resize(n);
276277
abs_tol_.allocate(memory::HOST);
277-
abs_tol_.setDataUpdated(memory::HOST);
278+
abs_tol_.setToZero(memory::HOST);
278279

279280
offset_ = 0;
280281
allocated_ = true;

GridKit/Model/PowerFlow/Bus/BusPQ.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,20 @@ namespace GridKit
6969
template <class ScalarT, typename IdxT>
7070
int BusPQ<ScalarT, IdxT>::allocate()
7171
{
72-
using VectorT = typename ModelEvaluatorImpl<ScalarT, IdxT>::VectorT;
72+
using VectorT = typename ModelEvaluatorImpl<ScalarT, IdxT>::VectorT;
73+
auto allocate_host_vector = [](VectorT& vector, IdxT n)
74+
{
75+
vector.resize(n);
76+
vector.allocate(GridKit::memory::HOST);
77+
vector.setDataUpdated(GridKit::memory::HOST);
78+
};
7379

7480
// std::cout << "Allocate PQ bus ..." << std::endl;
7581
this->allocateVectors(size_);
7682

77-
fB_ = VectorT(size_);
78-
fB_.allocate(GridKit::memory::HOST);
79-
fB_.setDataUpdated(GridKit::memory::HOST);
80-
81-
yB_ = VectorT(size_);
82-
yB_.allocate(GridKit::memory::HOST);
83-
yB_.setDataUpdated(GridKit::memory::HOST);
84-
85-
ypB_ = VectorT(size_);
86-
ypB_.allocate(GridKit::memory::HOST);
87-
ypB_.setDataUpdated(GridKit::memory::HOST);
83+
allocate_host_vector(fB_, size_);
84+
allocate_host_vector(yB_, size_);
85+
allocate_host_vector(ypB_, size_);
8886

8987
return 0;
9088
}

GridKit/Model/PowerFlow/Bus/BusPV.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,20 @@ namespace GridKit
6767
template <class ScalarT, typename IdxT>
6868
int BusPV<ScalarT, IdxT>::allocate()
6969
{
70-
using VectorT = typename ModelEvaluatorImpl<ScalarT, IdxT>::VectorT;
70+
using VectorT = typename ModelEvaluatorImpl<ScalarT, IdxT>::VectorT;
71+
auto allocate_host_vector = [](VectorT& vector, IdxT n)
72+
{
73+
vector.resize(n);
74+
vector.allocate(GridKit::memory::HOST);
75+
vector.setDataUpdated(GridKit::memory::HOST);
76+
};
7177

7278
// std::cout << "Allocate PV bus ..." << std::endl;
7379
this->allocateVectors(size_);
7480

75-
fB_ = VectorT(size_);
76-
fB_.allocate(GridKit::memory::HOST);
77-
fB_.setDataUpdated(GridKit::memory::HOST);
78-
79-
yB_ = VectorT(size_);
80-
yB_.allocate(GridKit::memory::HOST);
81-
yB_.setDataUpdated(GridKit::memory::HOST);
82-
83-
ypB_ = VectorT(size_);
84-
ypB_.allocate(GridKit::memory::HOST);
85-
ypB_.setDataUpdated(GridKit::memory::HOST);
81+
allocate_host_vector(fB_, size_);
82+
allocate_host_vector(yB_, size_);
83+
allocate_host_vector(ypB_, size_);
8684

8785
return 0;
8886
}

GridKit/Model/PowerFlow/SystemModel.hpp

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -104,37 +104,21 @@ namespace GridKit
104104
// Allocate global vectors
105105
this->allocateVectors(size_);
106106

107-
yB_ = VectorT(size_);
108-
yB_.allocate(GridKit::memory::HOST);
109-
yB_.setDataUpdated(GridKit::memory::HOST);
110-
111-
ypB_ = VectorT(size_);
112-
ypB_.allocate(GridKit::memory::HOST);
113-
ypB_.setDataUpdated(GridKit::memory::HOST);
114-
115-
fB_ = VectorT(size_);
116-
fB_.allocate(GridKit::memory::HOST);
117-
fB_.setDataUpdated(GridKit::memory::HOST);
118-
119-
g_ = VectorT(size_quad_);
120-
g_.allocate(GridKit::memory::HOST);
121-
g_.setDataUpdated(GridKit::memory::HOST);
122-
123-
gB_ = VectorT(size_quad_ * size_opt_);
124-
gB_.allocate(GridKit::memory::HOST);
125-
gB_.setDataUpdated(GridKit::memory::HOST);
126-
127-
param_ = VectorT(size_opt_);
128-
param_.allocate(GridKit::memory::HOST);
129-
param_.setDataUpdated(GridKit::memory::HOST);
130-
131-
param_lo_ = VectorT(size_opt_);
132-
param_lo_.allocate(GridKit::memory::HOST);
133-
param_lo_.setDataUpdated(GridKit::memory::HOST);
134-
135-
param_up_ = VectorT(size_opt_);
136-
param_up_.allocate(GridKit::memory::HOST);
137-
param_up_.setDataUpdated(GridKit::memory::HOST);
107+
auto allocate_host_vector = [](VectorT& vector, IdxT n)
108+
{
109+
vector.resize(n);
110+
vector.allocate(GridKit::memory::HOST);
111+
vector.setDataUpdated(GridKit::memory::HOST);
112+
};
113+
114+
allocate_host_vector(yB_, size_);
115+
allocate_host_vector(ypB_, size_);
116+
allocate_host_vector(fB_, size_);
117+
allocate_host_vector(g_, size_quad_);
118+
allocate_host_vector(gB_, size_quad_ * size_opt_);
119+
allocate_host_vector(param_, size_opt_);
120+
allocate_host_vector(param_lo_, size_opt_);
121+
allocate_host_vector(param_up_, size_opt_);
138122

139123
assert(size_quad_ == 1 or size_quad_ == 0);
140124

tests/UnitTests/LinearAlgebra/Vector/VectorTests.hpp

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -108,46 +108,6 @@ namespace GridKit
108108
return status.report(__func__);
109109
}
110110

111-
/**
112-
* @brief Test non-owning Vector storage over host vector data.
113-
*
114-
* @param[in] N Number of elements in the vector.
115-
* @return TestOutcome indicating success or failure of the test.
116-
*/
117-
TestOutcome externalData(IdxT N)
118-
{
119-
TestStatus status;
120-
status = true;
121-
122-
Vector<ScalarT, IdxT> x(N);
123-
x.allocate(memory::HOST);
124-
x.setToConst(ScalarT{}, memory::HOST);
125-
126-
Vector<ScalarT, IdxT> alias(N);
127-
alias.setData(x.data());
128-
129-
status *= (alias.size() == static_cast<std::size_t>(N));
130-
status *= (!alias.empty());
131-
status *= (alias.data() == x.data());
132-
status *= (alias.size() == static_cast<std::size_t>(N));
133-
134-
for (IdxT i = 0; i < N; ++i)
135-
{
136-
alias[static_cast<std::size_t>(i)] = static_cast<ScalarT>(i);
137-
}
138-
139-
const auto& const_alias = alias;
140-
status *= (const_alias.size() == alias.size());
141-
status *= (const_alias.data() == alias.data());
142-
for (IdxT i = 0; i < N; ++i)
143-
{
144-
status *= isEqual(x[static_cast<std::size_t>(i)], static_cast<ScalarT>(i));
145-
status *= isEqual(const_alias[static_cast<std::size_t>(i)], static_cast<ScalarT>(i));
146-
}
147-
148-
return status.report(__func__);
149-
}
150-
151111
/**
152112
* @brief Test setting data in a vector from array.
153113
*
@@ -231,7 +191,7 @@ namespace GridKit
231191
z.allocate(memory::HOST);
232192
z.copyFromExternal(y, memspace_, memory::HOST);
233193

234-
const ScalarT* z_data = z.data();
194+
const ScalarT* z_data = z.getData(memory::HOST);
235195

236196
if (z_data == nullptr)
237197
{
@@ -421,11 +381,11 @@ namespace GridKit
421381
for (IdxT i = 0; i < x.getSize(); ++i)
422382
{
423383
// std::cout << x->getData("cpu")[i] << "\n";
424-
if (!isEqual(x.data()[i], answer))
384+
if (!isEqual(x.getData(memory::HOST)[i], answer))
425385
{
426386
std::cout << std::setprecision(16);
427387
success = false;
428-
std::cout << "Solution vector element x[" << i << "] = " << x.data()[i]
388+
std::cout << "Solution vector element x[" << i << "] = " << x.getData(memory::HOST)[i]
429389
<< ", expected: " << answer << "\n";
430390
break;
431391
}

tests/UnitTests/LinearAlgebra/Vector/runVectorTests.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ int main(int, char**)
2121
// result += test.copyToExternal(50);
2222

2323
result += test.resize(100, 50);
24-
result += test.externalData(50);
25-
2624
result += test.setToConst(50);
2725
result += test.setToConst(50);
2826
}

0 commit comments

Comments
 (0)