Skip to content

Commit

Permalink
add dpcpp is_sorted_by_col_idxs kernel
Browse files Browse the repository at this point in the history
  • Loading branch information
upsj committed Jun 18, 2021
1 parent c6bc571 commit 56abb9c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
26 changes: 24 additions & 2 deletions dpcpp/matrix/csr_kernels.dp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "core/base/utils.hpp"
#include "core/components/prefix_sum.hpp"
#include "core/matrix/csr_builder.hpp"
#include "core/test/utils/unsort_matrix.hpp"
#include "dpcpp/components/format_conversion.dp.hpp"


Expand Down Expand Up @@ -745,8 +746,29 @@ GKO_INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE(
template <typename ValueType, typename IndexType>
void is_sorted_by_column_index(
std::shared_ptr<const DpcppExecutor> exec,
const matrix::Csr<ValueType, IndexType> *to_check,
bool *is_sorted) GKO_NOT_IMPLEMENTED;
const matrix::Csr<ValueType, IndexType> *to_check, bool *is_sorted)
{
bool val{true};
exec->copy_from(exec->get_master(), 1, &val, is_sorted);
const auto num_rows = to_check->get_size()[0];
const auto row_ptrs = to_check->get_const_row_ptrs();
const auto cols = to_check->get_const_col_idxs();
exec->get_queue()->submit([&](sycl::handler &cgh) {
cgh.parallel_for(sycl::range<1>{num_rows}, [=](sycl::id<1> idx) {
const auto row = static_cast<size_type>(idx[0]);
const auto begin = row_ptrs[row];
const auto end = row_ptrs[row + 1];
if (*is_sorted) {
for (auto i = begin; i < end - 1; i++) {
if (cols[i] > cols[i + 1]) {
*is_sorted = false;
break;
}
}
}
});
});
};

GKO_INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE(
GKO_DECLARE_CSR_IS_SORTED_BY_COLUMN_INDEX);
Expand Down
45 changes: 45 additions & 0 deletions dpcpp/test/matrix/csr_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


#include "core/test/utils.hpp"
#include "core/test/utils/unsort_matrix.hpp"


namespace {
Expand Down Expand Up @@ -115,6 +116,24 @@ class Csr : public ::testing::Test {
dbeta->copy_from(beta.get());
}

struct matrix_pair {
std::unique_ptr<Mtx> ref;
std::unique_ptr<Mtx> dpcpp;
};

matrix_pair gen_unsorted_mtx()
{
constexpr int min_nnz_per_row{2};
auto local_mtx_ref =
gen_mtx<Mtx>(mtx_size[0], mtx_size[1], min_nnz_per_row);
gko::test::unsort_matrix(gko::lend(local_mtx_ref), rand_engine);

auto local_mtx_dpcpp = Mtx::create(dpcpp);
local_mtx_dpcpp->copy_from(local_mtx_ref.get());

return {std::move(local_mtx_ref), std::move(local_mtx_dpcpp)};
}

std::shared_ptr<gko::ReferenceExecutor> ref;
std::shared_ptr<const gko::DpcppExecutor> dpcpp;

Expand Down Expand Up @@ -239,4 +258,30 @@ TEST_F(Csr, AdvancedApplyToIdentityMatrixIsEquivalentToRef)
}


TEST_F(Csr, RecognizeSortedMatrixIsEquivalentToRef)
{
set_up_apply_data();
bool is_sorted_dpcpp{};
bool is_sorted_ref{};

is_sorted_ref = mtx->is_sorted_by_column_index();
is_sorted_dpcpp = dmtx->is_sorted_by_column_index();

ASSERT_EQ(is_sorted_ref, is_sorted_dpcpp);
}


TEST_F(Csr, RecognizeUnsortedMatrixIsEquivalentToRef)
{
auto uns_mtx = gen_unsorted_mtx();
bool is_sorted_dpcpp{};
bool is_sorted_ref{};

is_sorted_ref = uns_mtx.ref->is_sorted_by_column_index();
is_sorted_dpcpp = uns_mtx.dpcpp->is_sorted_by_column_index();

ASSERT_EQ(is_sorted_ref, is_sorted_dpcpp);
}


} // namespace

0 comments on commit 56abb9c

Please sign in to comment.