Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
upsj committed Nov 27, 2023
1 parent 0775637 commit 6e74674
Show file tree
Hide file tree
Showing 13 changed files with 458 additions and 180 deletions.
117 changes: 117 additions & 0 deletions core/matrix/coo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,123 @@ GKO_REGISTER_OPERATION(outplace_absolute_array,
} // namespace coo


template <typename ValueType, typename IndexType>
std::unique_ptr<Coo<ValueType, IndexType>> Coo<ValueType, IndexType>::create(
std::shared_ptr<const Executor> exec, const dim<2>& size,
size_type num_nonzeros)
{
return std::unique_ptr<Coo>(new Coo{exec, size, num_nonzeros});
}


template <typename ValueType, typename IndexType>
std::unique_ptr<Coo<ValueType, IndexType>> Coo<ValueType, IndexType>::create(
std::shared_ptr<const Executor> exec, const dim<2>& size,
array<value_type> values, array<index_type> col_idxs,
array<index_type> row_idxs)
{
return std::unique_ptr<Coo>(new Coo{exec, size, std::move(values),
std::move(col_idxs),
std::move(row_idxs)});
}


template <typename ValueType, typename IndexType>
std::unique_ptr<const Coo<ValueType, IndexType>>
Coo<ValueType, IndexType>::create_const(
std::shared_ptr<const Executor> exec, const dim<2>& size,
gko::detail::const_array_view<ValueType>&& values,
gko::detail::const_array_view<IndexType>&& col_idxs,
gko::detail::const_array_view<IndexType>&& row_idxs)
{
// cast const-ness away, but return a const object afterwards,
// so we can ensure that no modifications take place.
return std::unique_ptr<const Coo>(
new Coo{exec, size, gko::detail::array_const_cast(std::move(values)),
gko::detail::array_const_cast(std::move(col_idxs)),
gko::detail::array_const_cast(std::move(row_idxs))});
}


template <typename ValueType, typename IndexType>
Coo<ValueType, IndexType>::Coo(std::shared_ptr<const Executor> exec,
const dim<2>& size, size_type num_nonzeros)
: EnableLinOp<Coo>(exec, size),
values_(exec, num_nonzeros),
col_idxs_(exec, num_nonzeros),
row_idxs_(exec, num_nonzeros)
{}


template <typename ValueType, typename IndexType>
Coo<ValueType, IndexType>::Coo(std::shared_ptr<const Executor> exec,
const dim<2>& size, array<value_type> values,
array<index_type> col_idxs,
array<index_type> row_idxs)
: EnableLinOp<Coo>(exec, size),
values_{exec, std::move(values)},
col_idxs_{exec, std::move(col_idxs)},
row_idxs_{exec, std::move(row_idxs)}
{
GKO_ASSERT_EQ(values_.get_num_elems(), col_idxs_.get_num_elems());
GKO_ASSERT_EQ(values_.get_num_elems(), row_idxs_.get_num_elems());
}


template <typename ValueType, typename IndexType>
LinOp* Coo<ValueType, IndexType>::apply2(ptr_param<const LinOp> b,
ptr_param<LinOp> x)
{
this->validate_application_parameters(b.get(), x.get());
auto exec = this->get_executor();
this->apply2_impl(make_temporary_clone(exec, b).get(),
make_temporary_clone(exec, x).get());
return this;
}


template <typename ValueType, typename IndexType>
const LinOp* Coo<ValueType, IndexType>::apply2(ptr_param<const LinOp> b,
ptr_param<LinOp> x) const
{
this->validate_application_parameters(b.get(), x.get());
auto exec = this->get_executor();
this->apply2_impl(make_temporary_clone(exec, b).get(),
make_temporary_clone(exec, x).get());
return this;
}


template <typename ValueType, typename IndexType>
LinOp* Coo<ValueType, IndexType>::apply2(ptr_param<const LinOp> alpha,
ptr_param<const LinOp> b,
ptr_param<LinOp> x)
{
this->validate_application_parameters(b.get(), x.get());
GKO_ASSERT_EQUAL_DIMENSIONS(alpha, dim<2>(1, 1));
auto exec = this->get_executor();
this->apply2_impl(make_temporary_clone(exec, alpha).get(),
make_temporary_clone(exec, b).get(),
make_temporary_clone(exec, x).get());
return this;
}


template <typename ValueType, typename IndexType>
const LinOp* Coo<ValueType, IndexType>::apply2(ptr_param<const LinOp> alpha,
ptr_param<const LinOp> b,
ptr_param<LinOp> x) const
{
this->validate_application_parameters(b.get(), x.get());
GKO_ASSERT_EQUAL_DIMENSIONS(alpha, dim<2>(1, 1));
auto exec = this->get_executor();
this->apply2_impl(make_temporary_clone(exec, alpha).get(),
make_temporary_clone(exec, b).get(),
make_temporary_clone(exec, x).get());
return this;
}


template <typename ValueType, typename IndexType>
void Coo<ValueType, IndexType>::apply_impl(const LinOp* b, LinOp* x) const
{
Expand Down
147 changes: 147 additions & 0 deletions core/matrix/csr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,153 @@ GKO_REGISTER_OPERATION(check_diagonal_entries,
} // namespace csr


template <typename ValueType, typename IndexType>
std::unique_ptr<const Csr<ValueType, IndexType>>
Csr<ValueType, IndexType>::create_const(
std::shared_ptr<const Executor> exec, const dim<2>& size,
gko::detail::const_array_view<ValueType>&& values,
gko::detail::const_array_view<IndexType>&& col_idxs,
gko::detail::const_array_view<IndexType>&& row_ptrs,
std::shared_ptr<strategy_type> strategy)
{
// cast const-ness away, but return a const object afterwards,
// so we can ensure that no modifications take place.
return std::unique_ptr<const Csr>(
new Csr{exec, size, gko::detail::array_const_cast(std::move(values)),
gko::detail::array_const_cast(std::move(col_idxs)),
gko::detail::array_const_cast(std::move(row_ptrs)), strategy});
}


template <typename ValueType, typename IndexType>
std::unique_ptr<const Csr<ValueType, IndexType>>
Csr<ValueType, IndexType>::create_const(
std::shared_ptr<const Executor> exec, const dim<2>& size,
gko::detail::const_array_view<ValueType>&& values,
gko::detail::const_array_view<IndexType>&& col_idxs,
gko::detail::const_array_view<IndexType>&& row_ptrs)
{
return Csr::create_const(exec, size, std::move(values), std::move(col_idxs),
std::move(row_ptrs),
Csr::make_default_strategy(exec));
}


template <typename ValueType, typename IndexType>
std::unique_ptr<Csr<ValueType, IndexType>> Csr<ValueType, IndexType>::create(
std::shared_ptr<const Executor> exec,
std::shared_ptr<strategy_type> strategy)
{
return std::unique_ptr<Csr>(new Csr{exec, std::move(strategy)});
}


template <typename ValueType, typename IndexType>
std::unique_ptr<Csr<ValueType, IndexType>> Csr<ValueType, IndexType>::create(
std::shared_ptr<const Executor> exec, const dim<2>& size,
size_type num_nonzeros, std::shared_ptr<strategy_type> strategy)
{
return std::unique_ptr<Csr>(
new Csr{exec, size, num_nonzeros, std::move(strategy)});
}


template <typename ValueType, typename IndexType>
std::unique_ptr<Csr<ValueType, IndexType>> Csr<ValueType, IndexType>::create(
std::shared_ptr<const Executor> exec, const dim<2>& size,
size_type num_nonzeros)
{
return std::unique_ptr<Csr>(new Csr{exec, size, num_nonzeros});
}


template <typename ValueType, typename IndexType>
std::unique_ptr<Csr<ValueType, IndexType>> Csr<ValueType, IndexType>::create(
std::shared_ptr<const Executor> exec, const dim<2>& size,
array<value_type> values, array<index_type> col_idxs,
array<index_type> row_ptrs, std::shared_ptr<strategy_type> strategy)
{
return std::unique_ptr<Csr>(
new Csr{exec, size, std::move(values), std::move(col_idxs),
std::move(row_ptrs), std::move(strategy)});
}


template <typename ValueType, typename IndexType>
std::unique_ptr<Csr<ValueType, IndexType>> Csr<ValueType, IndexType>::create(
std::shared_ptr<const Executor> exec, const dim<2>& size,
array<value_type> values, array<index_type> col_idxs,
array<index_type> row_ptrs)
{
return std::unique_ptr<Csr>(new Csr{exec, size, std::move(values),
std::move(col_idxs),
std::move(row_ptrs)});
}


template <typename ValueType, typename IndexType>
Csr<ValueType, IndexType>::Csr(std::shared_ptr<const Executor> exec,
std::shared_ptr<strategy_type> strategy)
: Csr(std::move(exec), dim<2>{}, {}, std::move(strategy))
{}


template <typename ValueType, typename IndexType>
Csr<ValueType, IndexType>::Csr(std::shared_ptr<const Executor> exec,
const dim<2>& size, size_type num_nonzeros,
std::shared_ptr<strategy_type> strategy)
: EnableLinOp<Csr>(exec, size),
values_(exec, num_nonzeros),
col_idxs_(exec, num_nonzeros),
row_ptrs_(exec, size[0] + 1),
srow_(exec, strategy->clac_size(num_nonzeros)),
strategy_(strategy->copy())
{
row_ptrs_.fill(0);
this->make_srow();
}


template <typename ValueType, typename IndexType>
Csr<ValueType, IndexType>::Csr(std::shared_ptr<const Executor> exec,
const dim<2>& size, size_type num_nonzeros)
: Csr{exec, size, num_nonzeros, Csr::make_default_strategy(exec)}
{}


template <typename ValueType, typename IndexType>
Csr<ValueType, IndexType>::Csr(std::shared_ptr<const Executor> exec,
const dim<2>& size, array<value_type> values,
array<index_type> col_idxs,
array<index_type> row_ptrs,
std::shared_ptr<strategy_type> strategy)
: EnableLinOp<Csr>(exec, size),
values_{exec, std::move(values)},
col_idxs_{exec, std::move(col_idxs)},
row_ptrs_{exec, std::move(row_ptrs)},
srow_(exec),
strategy_(strategy->copy())
{
GKO_ASSERT_EQ(values_.get_num_elems(), col_idxs_.get_num_elems());
GKO_ASSERT_EQ(this->get_size()[0] + 1, row_ptrs_.get_num_elems());
this->make_srow();
}


template <typename ValueType, typename IndexType>
Csr<ValueType, IndexType>::Csr(std::shared_ptr<const Executor> exec,
const dim<2>& size, array<value_type> values,
array<index_type> col_idxs,
array<index_type> row_ptrs)
: Csr{exec,
size,
std::move(values),
std::move(col_idxs),
std::move(row_ptrs),
Csr::make_default_strategy(exec)}
{}


template <typename ValueType, typename IndexType>
Csr<ValueType, IndexType>& Csr<ValueType, IndexType>::operator=(
const Csr<ValueType, IndexType>& other)
Expand Down
5 changes: 2 additions & 3 deletions include/ginkgo/core/base/batch_multi_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,12 +378,11 @@ class MultiVector
* the wrong executor, an internal copy will be created, and the
* original array data will not be used in the vector.
*/
template <typename ValuesArray>
MultiVector(std::shared_ptr<const Executor> exec, const batch_dim<2>& size,
ValuesArray&& values)
array<value_type> values)
: EnablePolymorphicObject<MultiVector<ValueType>>(exec),
batch_size_(size),
values_{exec, std::forward<ValuesArray>(values)}
values_{exec, std::move(values)}
{
// Ensure that the values array has the correct size
auto num_elems = compute_num_elems(size);
Expand Down
12 changes: 5 additions & 7 deletions include/ginkgo/core/base/device_matrix_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,13 @@ class device_matrix_data {
* @param col_idxs the array containing the matrix column indices
* @param row_idxs the array containing the matrix row indices
*/
template <typename ValueArray, typename RowIndexArray,
typename ColIndexArray>
device_matrix_data(std::shared_ptr<const Executor> exec, dim<2> size,
RowIndexArray&& row_idxs, ColIndexArray&& col_idxs,
ValueArray&& values)
array<index_type> row_idxs, array<index_type> col_idxs,
array<value_type> values)
: size_{size},
row_idxs_{exec, std::forward<RowIndexArray>(row_idxs)},
col_idxs_{exec, std::forward<ColIndexArray>(col_idxs)},
values_{exec, std::forward<ValueArray>(values)}
row_idxs_{exec, std::move(row_idxs)},
col_idxs_{exec, std::move(col_idxs)},
values_{exec, std::move(values)}
{
GKO_ASSERT_EQ(values_.get_num_elems(), row_idxs_.get_num_elems());
GKO_ASSERT_EQ(values_.get_num_elems(), col_idxs_.get_num_elems());
Expand Down
6 changes: 2 additions & 4 deletions include/ginkgo/core/matrix/batch_dense.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,11 +302,9 @@ class Dense final : public EnableBatchLinOp<Dense<ValueType>>,
* the wrong executor, an internal copy will be created, and the
* original array data will not be used in the matrix.
*/
template <typename ValuesArray>
Dense(std::shared_ptr<const Executor> exec, const batch_dim<2>& size,
ValuesArray&& values)
: EnableBatchLinOp<Dense>(exec, size),
values_{exec, std::forward<ValuesArray>(values)}
array<value_type> values)
: EnableBatchLinOp<Dense>(exec, size), values_{exec, std::move(values)}
{
// Ensure that the values array has the correct size
auto num_elems = compute_num_elems(size);
Expand Down
Loading

0 comments on commit 6e74674

Please sign in to comment.