Skip to content

Commit

Permalink
Add other utility kernels for reference
Browse files Browse the repository at this point in the history
+ Local to global index mapping.
+ Global to local index mapping.
+ Move kernel tests to reference/
  • Loading branch information
pratikvn committed Dec 16, 2020
1 parent f84fb2c commit 1d14d4d
Show file tree
Hide file tree
Showing 6 changed files with 306 additions and 61 deletions.
41 changes: 38 additions & 3 deletions core/base/index_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,40 @@ void IndexSet<IndexType>::populate_subsets(const gko::Array<IndexType> &indices)
}


template <typename IndexType>
IndexType IndexSet<IndexType>::get_global_index(const IndexType &index) const
{
auto exec = this->get_executor();
auto loc_idx =
Array<IndexType>(exec, std::initializer_list<IndexType>{index});
auto glob_idx =
Array<IndexType>(exec, std::initializer_list<IndexType>{index});

GKO_ASSERT(this->get_num_subsets() >= 1);
exec->run(index_set::make_local_to_global(
this->index_space_size_, &this->subsets_begin_, &this->subsets_end_,
&this->superset_cumulative_indices_, &loc_idx, &glob_idx));
return glob_idx.get_data()[0];
}


template <typename IndexType>
IndexType IndexSet<IndexType>::get_local_index(const IndexType &index) const
{
auto exec = this->get_executor();
auto loc_idx =
Array<IndexType>(exec, std::initializer_list<IndexType>{index});
auto glob_idx =
Array<IndexType>(exec, std::initializer_list<IndexType>{index});

GKO_ASSERT(this->get_num_subsets() >= 1);
exec->run(index_set::make_global_to_local(
this->index_space_size_, &this->subsets_begin_, &this->subsets_end_,
&this->superset_cumulative_indices_, &glob_idx, &loc_idx));
return loc_idx.get_data()[0];
}


template <typename IndexType>
Array<IndexType> IndexSet<IndexType>::get_global_indices_from_local(
const Array<IndexType> &local_indices) const
Expand All @@ -78,7 +112,7 @@ Array<IndexType> IndexSet<IndexType>::get_global_indices_from_local(
gko::Array<IndexType>(exec, local_indices.get_num_elems());

GKO_ASSERT(this->get_num_subsets() >= 1);
exec->run(index_set::make_global_to_local(
exec->run(index_set::make_local_to_global(
this->index_space_size_, &this->subsets_begin_, &this->subsets_end_,
&this->superset_cumulative_indices_, &local_indices, &global_indices));
return std::move(global_indices);
Expand All @@ -90,10 +124,11 @@ Array<IndexType> IndexSet<IndexType>::get_local_indices_from_global(
const Array<IndexType> &global_indices) const
{
auto exec = this->get_executor();
auto local_indices = gko::Array<IndexType>(exec);
auto local_indices =
gko::Array<IndexType>(exec, global_indices.get_num_elems());

GKO_ASSERT(this->get_num_subsets() >= 1);
exec->run(index_set::make_local_to_global(
exec->run(index_set::make_global_to_local(
this->index_space_size_, &this->subsets_begin_, &this->subsets_end_,
&this->superset_cumulative_indices_, &global_indices, &local_indices));
return std::move(local_indices);
Expand Down
54 changes: 0 additions & 54 deletions core/test/base/index_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,6 @@ class IndexSet : public ::testing::Test {
}
}

static void assert_equal_arrays(const T num_elems, const T *a, const T *b)
{
if (num_elems > 0) {
for (auto i = 0; i < num_elems; ++i) {
ASSERT_EQ(a[i], b[i]);
}
}
}


std::shared_ptr<const gko::Executor> exec;
};

Expand Down Expand Up @@ -149,48 +139,4 @@ TYPED_TEST(IndexSet, KnowsItsSize)
}


TYPED_TEST(IndexSet, CanBeConstructedFromIndices)
{
auto idx_arr = gko::Array<TypeParam>{this->exec, {0, 1, 2, 4, 6, 7, 8, 9}};
auto begin_comp = gko::Array<TypeParam>{this->exec, {0, 4, 6}};
auto end_comp = gko::Array<TypeParam>{this->exec, {3, 5, 10}};
auto superset_comp = gko::Array<TypeParam>{this->exec, {0, 3, 4, 8}};

auto idx_set = gko::IndexSet<TypeParam>{this->exec, 10, idx_arr};

ASSERT_EQ(idx_set.get_size(), 10);
ASSERT_EQ(idx_set.get_num_subsets(), 3);
ASSERT_EQ(idx_set.get_num_subsets(), begin_comp.get_num_elems());
auto num_elems = idx_set.get_num_subsets();
this->assert_equal_arrays(num_elems, idx_set.get_subsets_begin(),
begin_comp.get_data());
this->assert_equal_arrays(num_elems, idx_set.get_subsets_end(),
end_comp.get_data());
this->assert_equal_arrays(num_elems, idx_set.get_superset_indices(),
superset_comp.get_data());
}


TYPED_TEST(IndexSet, CanBeConstructedFromNonSortedIndices)
{
auto idx_arr = gko::Array<TypeParam>{this->exec, {9, 1, 4, 2, 6, 8, 0, 7}};
auto begin_comp = gko::Array<TypeParam>{this->exec, {0, 4, 6}};
auto end_comp = gko::Array<TypeParam>{this->exec, {3, 5, 10}};
auto superset_comp = gko::Array<TypeParam>{this->exec, {0, 3, 4, 8}};

auto idx_set = gko::IndexSet<TypeParam>{this->exec, 10, idx_arr};

ASSERT_EQ(idx_set.get_size(), 10);
ASSERT_EQ(idx_set.get_num_subsets(), 3);
ASSERT_EQ(idx_set.get_num_subsets(), begin_comp.get_num_elems());
auto num_elems = idx_set.get_num_subsets();
this->assert_equal_arrays(num_elems, idx_set.get_subsets_begin(),
begin_comp.get_data());
this->assert_equal_arrays(num_elems, idx_set.get_subsets_end(),
end_comp.get_data());
this->assert_equal_arrays(num_elems, idx_set.get_superset_indices(),
superset_comp.get_data());
}


} // namespace
4 changes: 4 additions & 0 deletions include/ginkgo/core/base/index_set.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ class IndexSet {

index_type get_num_elems() const { return this->num_stored_indices_; };

index_type get_global_index(const index_type &local_index) const;

index_type get_local_index(const index_type &global_index) const;

Array<index_type> get_global_indices_from_local(
const Array<index_type> &local_indices) const;

Expand Down
57 changes: 53 additions & 4 deletions reference/base/index_set_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,16 @@ void populate_subsets(std::shared_ptr<const DefaultExecutor> exec,
tmp_subset_begin.push_back(tmp_indices.get_data()[0]);
tmp_subset_superset_index.push_back(0);
for (auto i = 1; i < num_indices; ++i) {
if (tmp_indices.get_data()[i] == (tmp_indices.get_data()[i - 1]) + 1) {
if ((tmp_indices.get_data()[i] ==
(tmp_indices.get_data()[i - 1] + 1)) ||
(tmp_indices.get_data()[i] == tmp_indices.get_data()[i - 1])) {
continue;
} else {
tmp_subset_end.push_back(tmp_indices.get_data()[i - 1] + 1);
tmp_subset_superset_index.push_back(
tmp_subset_superset_index.back() + tmp_subset_end.back() -
tmp_subset_begin.back());
if (i + 1 < num_indices) {
if (i < num_indices) {
tmp_subset_begin.push_back(tmp_indices.get_data()[i]);
}
}
Expand Down Expand Up @@ -126,7 +128,32 @@ void global_to_local(std::shared_ptr<const DefaultExecutor> exec,
const Array<IndexType> *superset_indices,
const Array<IndexType> *global_indices,
Array<IndexType> *local_indices)
{}
{
for (auto i = 0; i < global_indices->get_num_elems(); ++i) {
auto index = global_indices->get_const_data()[i];
GKO_ASSERT(index < index_space_size);
auto bucket = std::distance(
subset_begin->get_const_data(),
std::lower_bound(
subset_begin->get_const_data(),
subset_begin->get_const_data() + subset_begin->get_num_elems(),
index, [](const IndexType &sub_idx, const IndexType idx) {
return sub_idx <= idx;
}));
auto shifted_bucket = bucket == 0 ? 0 : (bucket - 1);
if (subset_end->get_const_data()[shifted_bucket] <= index) {
local_indices->get_data()[i] = -1;
} else {
local_indices->get_data()[i] =
index - subset_begin->get_const_data()[shifted_bucket] +
superset_indices->get_const_data()[shifted_bucket];
}
std::cout << " g index " << index << " bucket " << bucket << " l idx "
<< local_indices->get_data()[i] << " subset begin "
<< subset_begin->get_const_data()[shifted_bucket]
<< std::endl;
}
}

GKO_INSTANTIATE_FOR_EACH_INDEX_TYPE(
GKO_DECLARE_INDEX_SET_GLOBAL_TO_LOCAL_KERNEL);
Expand All @@ -140,7 +167,29 @@ void local_to_global(std::shared_ptr<const DefaultExecutor> exec,
const Array<IndexType> *superset_indices,
const Array<IndexType> *local_indices,
Array<IndexType> *global_indices)
{}
{
for (auto i = 0; i < local_indices->get_num_elems(); ++i) {
auto index = local_indices->get_const_data()[i];
GKO_ASSERT(
index <=
(superset_indices
->get_const_data()[superset_indices->get_num_elems() - 1] -
1));
auto bucket = std::distance(
superset_indices->get_const_data(),
std::lower_bound(superset_indices->get_const_data(),
superset_indices->get_const_data() +
superset_indices->get_num_elems(),
index,
[](const IndexType &sup_idx, const IndexType idx) {
return sup_idx <= idx;
}));
auto shifted_bucket = bucket == 0 ? 0 : (bucket - 1);
global_indices->get_data()[i] =
subset_begin->get_const_data()[shifted_bucket] + index -
superset_indices->get_const_data()[shifted_bucket];
}
}

GKO_INSTANTIATE_FOR_EACH_INDEX_TYPE(
GKO_DECLARE_INDEX_SET_LOCAL_TO_GLOBAL_KERNEL);
Expand Down
1 change: 1 addition & 0 deletions reference/test/base/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
ginkgo_create_test(combination)
ginkgo_create_test(composition)
ginkgo_create_test(index_set)
ginkgo_create_test(perturbation)
ginkgo_create_test(utils)
Loading

0 comments on commit 1d14d4d

Please sign in to comment.