Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions GridKit/LinearAlgebra/MemoryUtils.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <algorithm>
#include <iostream>

namespace GridKit
Expand Down Expand Up @@ -206,8 +207,7 @@ namespace GridKit
template <typename I, typename T>
int allocateArrayOnHost(T** v, I n)
{
std::size_t arraysize = static_cast<std::size_t>(n) * sizeof(T);
*v = new T[arraysize];
*v = new T[static_cast<std::size_t>(n)]();
return *v == nullptr ? 1 : 0;
}

Expand All @@ -222,16 +222,14 @@ namespace GridKit
template <typename I, typename T>
int copyArrayHostToHost(T* dst, const T* src, I n)
{
std::size_t arraysize = static_cast<std::size_t>(n) * sizeof(T);
memcpy(dst, src, arraysize);
std::copy_n(src, static_cast<std::size_t>(n), dst);
return 0;
}

template <typename I, typename T>
int setZeroArrayOnHost(T* v, I n)
{
std::size_t arraysize = static_cast<std::size_t>(n) * sizeof(T);
memset(v, 0, arraysize);
std::fill_n(v, static_cast<std::size_t>(n), T{});
return 0;
}

Expand Down
65 changes: 60 additions & 5 deletions GridKit/LinearAlgebra/Vector/Vector.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <cassert>
#include <cstring>
#include <utility>

#include <GridKit/AutomaticDifferentiation/DependencyTracking/Variable.hpp>
#include <GridKit/LinearAlgebra/Vector/Vector.hpp>
#include <GridKit/Utilities/Logger/Logger.hpp>

Expand Down Expand Up @@ -39,8 +40,8 @@ namespace GridKit
: n_capacity_(n),
k_(k),
n_size_(n),
gpu_updated_(new bool[k]),
cpu_updated_(new bool[k])
gpu_updated_(new bool[static_cast<std::size_t>(k)]),
cpu_updated_(new bool[static_cast<std::size_t>(k)])
{
setHostUpdated(false);
setDeviceUpdated(false);
Expand All @@ -52,13 +53,64 @@ namespace GridKit
*/
template <typename ScalarT, typename IdxT>
Vector<ScalarT, IdxT>::~Vector()
{
release();
}

template <typename ScalarT, typename IdxT>
Vector<ScalarT, IdxT>::Vector(Vector&& other) noexcept
{
*this = std::move(other);
}

template <typename ScalarT, typename IdxT>
Vector<ScalarT, IdxT>& Vector<ScalarT, IdxT>::operator=(Vector&& other) noexcept
{
if (this != &other)
{
release();

n_capacity_ = other.n_capacity_;
k_ = other.k_;
n_size_ = other.n_size_;
d_data_ = other.d_data_;
h_data_ = other.h_data_;
gpu_updated_ = other.gpu_updated_;
cpu_updated_ = other.cpu_updated_;
owns_gpu_data_ = other.owns_gpu_data_;
owns_cpu_data_ = other.owns_cpu_data_;
mem_ = std::move(other.mem_);

other.n_capacity_ = 0;
other.k_ = 0;
other.n_size_ = 0;
other.d_data_ = nullptr;
other.h_data_ = nullptr;
other.gpu_updated_ = nullptr;
other.cpu_updated_ = nullptr;
}

return *this;
}

template <typename ScalarT, typename IdxT>
void Vector<ScalarT, IdxT>::release()
{
if (owns_cpu_data_ && h_data_)
{
mem_.deleteOnHost(h_data_);
}
if (owns_gpu_data_ && d_data_)
{
mem_.deleteOnDevice(d_data_);
}
delete[] gpu_updated_;
delete[] cpu_updated_;

h_data_ = nullptr;
d_data_ = nullptr;
gpu_updated_ = nullptr;
cpu_updated_ = nullptr;
}

/**
Expand Down Expand Up @@ -999,9 +1051,12 @@ namespace GridKit
std::fill(gpu_updated_, gpu_updated_ + k_, is_updated);
}

// template class Vector<double, long int>;
template class Vector<double, long int>;
template class Vector<double, size_t>;
// template class Vector<double, int>;
template class Vector<double, int>;
template class Vector<DependencyTracking::Variable, long int>;
template class Vector<DependencyTracking::Variable, size_t>;
template class Vector<DependencyTracking::Variable, int>;

} // namespace LinearAlgebra
} // namespace GridKit
42 changes: 39 additions & 3 deletions GridKit/LinearAlgebra/Vector/Vector.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#pragma once
#include <cassert>
#include <cstddef>
#include <string>

#include <GridKit/LinearAlgebra/MemoryUtils.hpp>
Expand Down Expand Up @@ -36,10 +38,10 @@ namespace GridKit
Vector(IdxT n, IdxT k);
~Vector();

Vector(const Vector&) = delete;
Vector(Vector&&) = delete;
Vector(const Vector&) = delete;
Vector(Vector&& other) noexcept;
Vector& operator=(const Vector&) = delete;
Vector& operator=(Vector&&) = delete;
Vector& operator=(Vector&& other) noexcept;

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

std::size_t size() const
{
return static_cast<std::size_t>(getSize());
}

bool empty() const
{
return size() == 0;
}

ScalarT* data(memory::MemorySpace memspace = memory::HOST)
{
return getData(memspace);
}

const ScalarT* data(memory::MemorySpace memspace = memory::HOST) const
{
return getData(memspace);
}

ScalarT& operator[](std::size_t i)
{
assert(i < size());
return data()[i];
}

const ScalarT& operator[](std::size_t i) const
{
assert(i < size());
return data()[i];
}

IdxT getCapacity() const;
IdxT getSize() const;
IdxT getNumVectors() const;
Expand All @@ -77,6 +111,7 @@ namespace GridKit
memory::MemorySpace memspaceDst = memory::HOST);

private:
void release();
void setHostUpdated(bool is_updated);
void setDeviceUpdated(bool is_updated);

Expand All @@ -93,5 +128,6 @@ namespace GridKit

MemoryHandler mem_; ///< Device memory manager object
};

} // namespace LinearAlgebra
} // namespace GridKit
Loading
Loading