Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create submatrix from Index sets #964

Merged
merged 20 commits into from
Mar 30, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
141 changes: 129 additions & 12 deletions include/ginkgo/core/base/index_set.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,25 @@ class index_set {
*/
using index_type = IndexType;

/**
* Creates an empty Array not tied to any executor.
*/
index_set() noexcept
: exec_(nullptr), index_space_size_{0}, num_stored_indices_{0}
{}

/**
* Creates an empty index_set tied to the specified Executor.
*
* @param exec the Executor where the index_set data is allocated
*/
index_set(std::shared_ptr<const Executor> exec)
: exec_(std::move(exec)), index_space_size_{0}, num_stored_indices_{0}
explicit index_set(std::shared_ptr<const Executor> exec) noexcept
: exec_(std::move(exec)),
index_space_size_{0},
num_stored_indices_{0},
subsets_begin_{Array<index_type>(exec_)},
subsets_end_{Array<index_type>(exec_)},
superset_cumulative_indices_{Array<index_type>(exec_)}
{}

/**
Expand Down Expand Up @@ -144,20 +156,125 @@ class index_set {
}

/**
* Creates a copy of another index_set on a different executor.
* Creates a copy of the input index_set on a different executor.
*
* @param exec the executor where the new index_set will be created
* @param other the index_set to copy from
*/
index_set(std::shared_ptr<const Executor> exec, const index_set& other)
: index_set(exec)
{
*this = other;
}

/**
* Creates a copy of the input index_set.
*
* @param other the index_set to copy from
*/
index_set(const index_set& other) : index_set(other.get_executor(), other)
{}

/**
* Moves the input index_set to a different executor.
*
* @param exec the executor where the new index_set will be moved to
* @param other the index_set to move from
*/
index_set(std::shared_ptr<const Executor> exec, index_set&& other)
: index_set(exec)
{
*this = std::move(other);
}

/**
* Moves the input index_set.
*
* @param other the index_set to move from
*/
index_set(index_set&& other)
: index_set(other.get_executor(), std::move(other))
{}

/**
* Copies data from another index_set
*
* The executor of this is preserved. In case this does not have an assigned
* executor, it will inherit the executor of other.
*
* @param other the index_set to copy from
*
* @return this
*/
index_set& operator=(const index_set& other)
{
if (&other == this) {
return *this;
}
if (other.get_executor() == nullptr) {
this->clear();
return *this;
}
if (exec_ == nullptr) {
this->exec_ = other.get_executor();
}
this->index_space_size_ = other.index_space_size_;
this->num_stored_indices_ = other.num_stored_indices_;
this->subsets_begin_ = other.subsets_begin_;
this->subsets_end_ = other.subsets_end_;
this->superset_cumulative_indices_ = other.superset_cumulative_indices_;

return *this;
}

/**
* Moves data from another index_set
*
* The executor of this is preserved. In case this does not have an assigned
* executor, it will inherit the executor of other.
*
* @param other the index_set to move from
*
* @return this
*/
index_set& operator=(index_set&& other)
{
if (&other == this) {
return *this;
}
if (other.get_executor() == nullptr) {
this->clear();
return *this;
}
if (exec_ == nullptr) {
this->exec_ = other.get_executor();
}
this->index_space_size_ = other.index_space_size_;
this->num_stored_indices_ = other.num_stored_indices_;
subsets_begin_ = gko::Array<index_type>(exec, other.subsets_begin_);
subsets_end_ = gko::Array<index_type>(exec, other.subsets_end_);
superset_cumulative_indices_ =
gko::Array<index_type>(exec, other.superset_cumulative_indices_);
other.index_space_size_ = 0;
other.num_stored_indices_ = 0;
this->subsets_begin_ = std::move(other.subsets_begin_);
this->subsets_end_ = std::move(other.subsets_end_);
this->superset_cumulative_indices_ =
std::move(other.superset_cumulative_indices_);

return *this;
}

/**
* Deallocates all data used by the index_set.
*
* The index_set is left in a valid, but empty state, so the same index_set
* can be used to allocate new memory. Calls to
* index_set::get_subsets_begin() will return a `nullptr`.
*/
void clear() noexcept
{
this->index_space_size_ = 0;
this->num_stored_indices_ = 0;
this->subsets_begin_.clear();
this->subsets_end_.clear();
this->superset_cumulative_indices_.clear();
}

/**
Expand Down Expand Up @@ -345,11 +462,11 @@ class index_set {
const bool is_sorted);

std::shared_ptr<const Executor> exec_;
index_type index_space_size_;
index_type num_stored_indices_;
gko::Array<index_type> subsets_begin_;
gko::Array<index_type> subsets_end_;
gko::Array<index_type> superset_cumulative_indices_;
index_type index_space_size_{};
index_type num_stored_indices_{};
gko::Array<index_type> subsets_begin_{};
gko::Array<index_type> subsets_end_{};
gko::Array<index_type> superset_cumulative_indices_{};
};


Expand Down
4 changes: 2 additions & 2 deletions reference/test/base/index_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ TYPED_TEST(index_set, CanBeMoveConstructed)
gko::index_set<TypeParam> idx_set2(std::move(idx_set));

ASSERT_EQ(idx_set2.get_executor(), this->exec);
ASSERT_EQ(idx_set.get_executor(), nullptr);
ASSERT_EQ(idx_set.get_size(), 0);
ASSERT_EQ(idx_set2.get_size(), 10);
}

Expand Down Expand Up @@ -163,7 +163,7 @@ TYPED_TEST(index_set, CanBeMoveAssigned)
gko::index_set<TypeParam> idx_set2 = std::move(idx_set);

ASSERT_EQ(idx_set2.get_executor(), this->exec);
ASSERT_EQ(idx_set.get_executor(), nullptr);
ASSERT_EQ(idx_set.get_size(), 0);
ASSERT_EQ(idx_set2.get_size(), 10);
}

Expand Down