Skip to content

Commit

Permalink
Add an IndexSet class and basic functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
pratikvn committed Dec 15, 2020
1 parent f00f92d commit 655d9c7
Show file tree
Hide file tree
Showing 21 changed files with 1,002 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ target_sources(ginkgo
base/combination.cpp
base/composition.cpp
base/executor.cpp
base/index_set.cpp
base/mtx_io.cpp
base/perturbation.cpp
base/version.cpp
Expand Down
95 changes: 95 additions & 0 deletions core/base/index_set.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*******************************<GINKGO LICENSE>******************************
Copyright (c) 2017-2020, the Ginkgo authors
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************<GINKGO LICENSE>*******************************/

#include <ginkgo/core/base/index_set.hpp>


#include <algorithm>
#include <iostream>
#include <mutex>
#include <vector>


#include <ginkgo/core/base/exception_helpers.hpp>
#include <ginkgo/core/base/types.hpp>


#include "core/base/index_set_kernels.hpp"


namespace gko {
namespace index_set {


GKO_REGISTER_OPERATION(populate_subsets, index_set::populate_subsets);


} // namespace index_set


template <typename IndexType>
void IndexSet<IndexType>::populate_subsets(const gko::Array<IndexType> &indices)
{
auto exec = this->get_executor();

auto num_indices = static_cast<IndexType>(indices.get_num_elems());

exec->run(index_set::make_populate_subsets(
this->index_space_size_, this->num_stored_indices_,
indices.get_const_data(), num_indices, this->subsets_begin_.get_data(),
this->subsets_end_.get_data(),
this->superset_cumulative_indices_.get_data()));
}


template <typename IndexType>
bool IndexSet<IndexType>::is_element(const IndexType index) const
{}


template <typename IndexType>
void IndexSet<IndexType>::get_global_index(const IndexType &local_index,
IndexType &global_index) const
{}


template <typename IndexType>
void IndexSet<IndexType>::get_local_index(const IndexType &global_index,
IndexType &local_index) const
{}


#define GKO_DECLARE_INDEX_SET(_type) class IndexSet<_type>
GKO_INSTANTIATE_FOR_EACH_INDEX_TYPE(GKO_DECLARE_INDEX_SET);


} // namespace gko
111 changes: 111 additions & 0 deletions core/base/index_set_kernels.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*******************************<GINKGO LICENSE>******************************
Copyright (c) 2017-2020, the Ginkgo authors
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************<GINKGO LICENSE>*******************************/

#ifndef GKO_CORE_BASE_INDEX_SET_KERNELS_HPP_
#define GKO_CORE_BASE_INDEX_SET_KERNELS_HPP_


#include <ginkgo/core/base/index_set.hpp>


#include <memory>


namespace gko {
namespace kernels {


#define GKO_DECLARE_INDEX_SET_POPULATE_KERNEL(IndexType) \
void populate_subsets(std::shared_ptr<const DefaultExecutor> exec, \
const IndexType &index_space_size, \
IndexType &num_elems, const IndexType *indices, \
IndexType &num_indices, IndexType *subset_begin, \
IndexType *subset_end, IndexType *superset_indices)


#define GKO_DECLARE_ALL_AS_TEMPLATES \
template <typename IndexType> \
GKO_DECLARE_INDEX_SET_POPULATE_KERNEL(IndexType)


namespace omp {
namespace index_set {

GKO_DECLARE_ALL_AS_TEMPLATES;

} // namespace index_set
} // namespace omp


namespace cuda {
namespace index_set {

GKO_DECLARE_ALL_AS_TEMPLATES;

} // namespace index_set
} // namespace cuda


namespace reference {
namespace index_set {

GKO_DECLARE_ALL_AS_TEMPLATES;

} // namespace index_set
} // namespace reference


namespace hip {
namespace index_set {

GKO_DECLARE_ALL_AS_TEMPLATES;

} // namespace index_set
} // namespace hip


namespace dpcpp {
namespace index_set {

GKO_DECLARE_ALL_AS_TEMPLATES;

} // namespace index_set
} // namespace dpcpp


#undef GKO_DECLARE_ALL_AS_TEMPLATES


} // namespace kernels
} // namespace gko

#endif // GKO_CORE_BASE_INDEX_SET_KERNELS_HPP_
13 changes: 13 additions & 0 deletions core/device_hooks/common_kernels.inc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <ginkgo/core/base/exception_helpers.hpp>


#include "core/base/index_set_kernels.hpp"
#include "core/components/absolute_array.hpp"
#include "core/components/fill_array.hpp"
#include "core/components/precision_conversion.hpp"
Expand Down Expand Up @@ -111,6 +112,18 @@ GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE(GKO_DECLARE_OUTPLACE_ABSOLUTE_ARRAY_KERNEL);
} // namespace components


namespace index_set {


template <typename IndexType>
GKO_DECLARE_INDEX_SET_POPULATE_KERNEL(IndexType)
GKO_NOT_COMPILED(GKO_HOOK_MODULE);
GKO_INSTANTIATE_FOR_EACH_INDEX_TYPE(GKO_DECLARE_INDEX_SET_POPULATE_KERNEL);


} // namespace index_set


namespace dense {


Expand Down
1 change: 1 addition & 0 deletions core/test/base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ ginkgo_create_test(exception)
ginkgo_create_test(exception_helpers)
ginkgo_create_test(extended_float)
ginkgo_create_test(executor)
ginkgo_create_test(index_set)
ginkgo_create_test(iterator_factory)
ginkgo_create_test(lin_op)
ginkgo_create_test(math)
Expand Down
143 changes: 143 additions & 0 deletions core/test/base/index_set.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*******************************<GINKGO LICENSE>******************************
Copyright (c) 2017-2020, the Ginkgo authors
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************<GINKGO LICENSE>*******************************/

#include <ginkgo/core/base/index_set.hpp>


#include <ginkgo/core/base/executor.hpp>
#include <ginkgo/core/base/range.hpp>
#include <ginkgo/core/matrix/dense.hpp>


#include "core/test/utils.hpp"


namespace {


template <typename T>
class IndexSet : public ::testing::Test {
protected:
using value_type = T;
IndexSet() : exec(gko::ReferenceExecutor::create()) {}

void TearDown()
{
if (exec != nullptr) {
// ensure that previous calls finished and didn't throw an error
ASSERT_NO_THROW(exec->synchronize());
}
}

static void assert_equal_to_original(gko::IndexSet<T> &a)
{
ASSERT_EQ(a.get_size(), 10);
}

static void assert_equal_index_sets(gko::IndexSet<T> &a,
gko::IndexSet<T> &b)
{
ASSERT_EQ(a.get_size(), b.get_size());
ASSERT_EQ(a.get_num_subsets(), b.get_num_subsets());
if (a.get_num_subsets() > 0) {
for (auto i = 0; i < a.get_num_subsets(); ++i) {
ASSERT_EQ(a.get_subsets_begin()[i], b.get_subsets_begin()[i]);
ASSERT_EQ(a.get_subsets_end()[i], b.get_subsets_end()[i]);
ASSERT_EQ(a.get_superset_indices()[i],
b.get_superset_indices()[i]);
}
}
}


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

TYPED_TEST_SUITE(IndexSet, gko::test::IndexTypes);


TYPED_TEST(IndexSet, CanBeEmpty)
{
auto empty = gko::IndexSet<TypeParam>{};
ASSERT_EQ(empty.get_size(), 0);
ASSERT_EQ(empty.get_num_subsets(), 0);
}


TYPED_TEST(IndexSet, CanBeConstructedWithSize)
{
auto idx_set = gko::IndexSet<TypeParam>{this->exec, 10};
ASSERT_EQ(idx_set.get_size(), 10);
ASSERT_EQ(idx_set.get_num_subsets(), 0);
}


TYPED_TEST(IndexSet, CanBeCopyConstructed)
{
auto idx_set = gko::IndexSet<TypeParam>{this->exec, 10};
auto idx_set2(idx_set);
this->assert_equal_index_sets(idx_set2, idx_set);
}


TYPED_TEST(IndexSet, CanBeMoveConstructed)
{
auto idx_set = gko::IndexSet<TypeParam>{this->exec, 10};
auto idx_set2(std::move(idx_set));
this->assert_equal_to_original(idx_set2);
}


TYPED_TEST(IndexSet, CanBeCopyAssigned)
{
auto idx_set = gko::IndexSet<TypeParam>{this->exec, 10};
auto idx_set2 = idx_set;
this->assert_equal_index_sets(idx_set2, idx_set);
}


TYPED_TEST(IndexSet, CanBeMoveAssigned)
{
auto idx_set = gko::IndexSet<TypeParam>{this->exec, 10};
auto idx_set2 = std::move(idx_set);
this->assert_equal_to_original(idx_set2);
}


TYPED_TEST(IndexSet, KnowsItsSize)
{
auto idx_set = gko::IndexSet<TypeParam>{this->exec, 10};
ASSERT_EQ(idx_set.get_size(), 10);
}


} // namespace
Loading

0 comments on commit 655d9c7

Please sign in to comment.