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

Wiki examples: tests added #737

Merged
merged 1 commit into from
Jun 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 5 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ SET(KOKKOSKERNELS_TOP_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})

IF(NOT KOKKOSKERNELS_HAS_TRILINOS)
cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
IF (Spack_WORKAROUND)
IF (Spack_WORKAROUND)
#if we are explicitly using Spack for development,
#nuke the Spack compiler
SET(SPACK_CXX $ENV{SPACK_CXX})
Expand All @@ -28,7 +28,7 @@ IF(NOT KOKKOSKERNELS_HAS_TRILINOS)
SET(KokkosKernels_VERSION_PATCH 0)
ENDIF()

IF(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.12.0")
IF(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.12.0")
MESSAGE(STATUS "Setting policy CMP0074 to use <Package>_ROOT variables")
CMAKE_POLICY(SET CMP0074 NEW)
ENDIF()
Expand Down Expand Up @@ -70,6 +70,7 @@ SET(KokkosKernels_INSTALL_TESTING OFF CACHE INTERNAL
IF (KokkosKernels_INSTALL_TESTING)
# Force testing on if we are doing intall testing
SET(KOKKOSKERNELS_ENABLE_TESTS ON)
SET(KOKKOSKERNELS_ENABLE_EXAMPLES ON)
# Don't build, load installed kernels
FIND_PACKAGE(KokkosKernels REQUIRED)
# Still need to figure out which backends
Expand All @@ -78,6 +79,7 @@ IF (KokkosKernels_INSTALL_TESTING)
KOKKOSKERNELS_ADD_TEST_DIRECTORIES(test_common)
KOKKOSKERNELS_ADD_TEST_DIRECTORIES(perf_test)
KOKKOSKERNELS_ADD_TEST_DIRECTORIES(unit_test)
KOKKOSKERNELS_ADD_EXAMPLE_DIRECTORIES(example)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When doing install testing we force testing on. Does it make sense to do the same for examples?

SET(KOKKOSKERNELS_ENABLE_EXAMPLES ON)

ELSE()
# Regular build, not install testing
# Do all the regular option processing
Expand Down Expand Up @@ -188,7 +190,7 @@ ELSE()
KOKKOSKERNELS_ADD_TEST_DIRECTORIES(test_common)
KOKKOSKERNELS_ADD_TEST_DIRECTORIES(perf_test)
KOKKOSKERNELS_ADD_TEST_DIRECTORIES(unit_test)
#KOKKOSKERNELS_ADD_EXAMPLE_DIRECTORIES(example)
KOKKOSKERNELS_ADD_EXAMPLE_DIRECTORIES(example)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would squash the two commits.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I figured I would get comments on the CMake side at least so I felt like squashing was going to be an inevitability before the PR merges ; )


KOKKOSKERNELS_PACKAGE_POSTPROCESS()
ENDIF()
12 changes: 10 additions & 2 deletions cm_generate_makefile.bash
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ KOKKOS_INSTALL_PATH=""
KOKKOS_DO_TESTS=OFF
KOKKOS_DO_EXAMPLES=OFF
KOKKOSKERNELS_DO_TESTS=ON
KOKKOSKERNELS_DO_EXAMPLES=ON

KOKKOS_MAKEINSTALL_J=4

Expand Down Expand Up @@ -428,6 +429,13 @@ do
--disable-tests)
KOKKOSKERNELS_DO_TESTS=OFF
;;
--enable-examples)
# This is the default
KOKKOSKERNELS_DO_EXAMPLES=ON
;;
--disable-examples)
KOKKOSKERNELS_DO_EXAMPLES=OFF
;;
--compiler*)
COMPILER="${key#*=}"
CNUM=$(command -v ${COMPILER} 2>&1 >/dev/null | grep "no ${COMPILER}" | wc -l)
Expand Down Expand Up @@ -668,7 +676,7 @@ cd $STORE_KOKKOSKERNELS_BUILD_PATH

# Configure kokkos-kernels
echo ""
echo cmake $COMPILER_CMD -DKokkos_DIR="${KOKKOS_FIND_PATH}" -DCMAKE_CXX_FLAGS="${KOKKOS_CXXFLAGS}" -DCMAKE_INSTALL_PREFIX="${PREFIX}" -DKokkosKernels_ENABLE_TESTS=${KOKKOSKERNELS_DO_TESTS} ${KOKKOSKERNELS_SCALARS_CMD} ${KOKKOSKERNELS_ORDINALS_CMD} ${KOKKOSKERNELS_OFFSETS_CMD} ${KOKKOSKERNELS_LAYOUTS_CMD} ${KOKKOSKERNELS_TPLS_CMD} ${KOKKOSKERNELS_USER_TPL_PATH_CMD} ${KOKKOSKERNELS_USER_TPL_LIBNAME_CMD} ${KOKKOSKERNELS_EXTRA_LINKER_FLAGS_CMD} ${KOKKOSKERNELS_BUILDTYPE_CMD} ${KOKKOSKERNELS_SPACES_CMD} ${KERNELS_DEFAULT_ETI_OPTION} ${KOKKOSKERNELS_PATH}
echo cmake $COMPILER_CMD -DKokkos_DIR="${KOKKOS_FIND_PATH}" -DCMAKE_CXX_FLAGS="${KOKKOS_CXXFLAGS}" -DCMAKE_INSTALL_PREFIX="${PREFIX}" -DKokkosKernels_ENABLE_TESTS=${KOKKOSKERNELS_DO_TESTS} -DKokkosKernels_ENABLE_EXAMPLES:BOOL=${KOKKOSKERNELS_DO_EXAMPLES} ${KOKKOSKERNELS_SCALARS_CMD} ${KOKKOSKERNELS_ORDINALS_CMD} ${KOKKOSKERNELS_OFFSETS_CMD} ${KOKKOSKERNELS_LAYOUTS_CMD} ${KOKKOSKERNELS_TPLS_CMD} ${KOKKOSKERNELS_USER_TPL_PATH_CMD} ${KOKKOSKERNELS_USER_TPL_LIBNAME_CMD} ${KOKKOSKERNELS_EXTRA_LINKER_FLAGS_CMD} ${KOKKOSKERNELS_BUILDTYPE_CMD} ${KOKKOSKERNELS_SPACES_CMD} ${KERNELS_DEFAULT_ETI_OPTION} ${KOKKOSKERNELS_PATH}
echo ""
cmake $COMPILER_CMD -DKokkos_DIR="${KOKKOS_FIND_PATH}" -DCMAKE_CXX_FLAGS="${KOKKOS_CXXFLAGS//\"}" -DCMAKE_INSTALL_PREFIX="${PREFIX}" -DKokkosKernels_ENABLE_TESTS=${KOKKOSKERNELS_DO_TESTS} ${KOKKOSKERNELS_SCALARS_CMD} ${KOKKOSKERNELS_ORDINALS_CMD} ${KOKKOSKERNELS_OFFSETS_CMD} ${KOKKOSKERNELS_LAYOUTS_CMD} ${KOKKOSKERNELS_TPLS_CMD} ${KOKKOSKERNELS_USER_TPL_PATH_CMD} ${KOKKOSKERNELS_USER_TPL_LIBNAME_CMD} ${KOKKOSKERNELS_EXTRA_LINKER_FLAGS_CMD} ${KOKKOSKERNELS_BUILDTYPE_CMD} ${KOKKOSKERNELS_SPACES_CMD} ${KERNELS_DEFAULT_ETI_OPTION} ${KOKKOSKERNELS_PATH}
cmake $COMPILER_CMD -DKokkos_DIR="${KOKKOS_FIND_PATH}" -DCMAKE_CXX_FLAGS="${KOKKOS_CXXFLAGS//\"}" -DCMAKE_INSTALL_PREFIX="${PREFIX}" -DKokkosKernels_ENABLE_TESTS=${KOKKOSKERNELS_DO_TESTS} -DKokkosKernels_ENABLE_EXAMPLES:BOOL=${KOKKOSKERNELS_DO_EXAMPLES} ${KOKKOSKERNELS_SCALARS_CMD} ${KOKKOSKERNELS_ORDINALS_CMD} ${KOKKOSKERNELS_OFFSETS_CMD} ${KOKKOSKERNELS_LAYOUTS_CMD} ${KOKKOSKERNELS_TPLS_CMD} ${KOKKOSKERNELS_USER_TPL_PATH_CMD} ${KOKKOSKERNELS_USER_TPL_LIBNAME_CMD} ${KOKKOSKERNELS_EXTRA_LINKER_FLAGS_CMD} ${KOKKOSKERNELS_BUILDTYPE_CMD} ${KOKKOSKERNELS_SPACES_CMD} ${KERNELS_DEFAULT_ETI_OPTION} ${KOKKOSKERNELS_PATH}

8 changes: 7 additions & 1 deletion example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
ADD_SUBDIRECTORY(fenl)
KOKKOSKERNELS_INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})
KOKKOSKERNELS_INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})

KOKKOSKERNELS_INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../test_common)

#ADD_SUBDIRECTORY(fenl)
#ADD_SUBDIRECTORY(graph)
ADD_SUBDIRECTORY(wiki)
1 change: 1 addition & 0 deletions example/wiki/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ADD_SUBDIRECTORY(sparse)
12 changes: 12 additions & 0 deletions example/wiki/sparse/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
KOKKOSKERNELS_INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})
KOKKOSKERNELS_INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})

KOKKOSKERNELS_ADD_EXECUTABLE_AND_TEST(
wiki_crsmatrix
SOURCES KokkosSparse_wiki_crsmatrix.cpp
)

KOKKOSKERNELS_ADD_EXECUTABLE_AND_TEST(
wiki_spmv
SOURCES KokkosSparse_wiki_spmv.cpp
)
99 changes: 99 additions & 0 deletions example/wiki/sparse/KokkosSparse_wiki_crsmatrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include <sstream>

#include "Kokkos_Core.hpp"

#include "KokkosKernels_default_types.hpp"
#include "KokkosSparse_CrsMatrix.hpp"
#include "KokkosSparse_spmv.hpp"

using Scalar = default_scalar;
using Ordinal = default_lno_t;
using Offset = default_size_type;
using Layout = default_layout;

int main(int argc, char* argv[]) {
Kokkos::initialize();

using device_type = typename Kokkos::Device<
Kokkos::DefaultExecutionSpace,
typename Kokkos::DefaultExecutionSpace::memory_space>;
using matrix_type =
typename KokkosSparse::CrsMatrix<Scalar, Ordinal, device_type, void,
Offset>;
using graph_type = typename matrix_type::staticcrsgraph_type;
using row_map_type = typename graph_type::row_map_type;
using entries_type = typename graph_type::entries_type;
using values_type = typename matrix_type::values_type;

const Scalar SC_ONE = Kokkos::ArithTraits<Scalar>::one();

Ordinal numRows = 10;

{
const Offset numNNZ = 2 + (numRows - 2) * 3 + 2;
typename row_map_type::non_const_type row_map("row pointers", numRows + 1);
typename entries_type::non_const_type entries("column indices", numNNZ);
typename values_type::non_const_type values("values", numNNZ);

{
// Build the row pointers and store numNNZ
typename row_map_type::HostMirror row_map_h =
Kokkos::create_mirror_view(row_map);
for (Ordinal rowIdx = 1; rowIdx < numRows + 1; ++rowIdx) {
if ((rowIdx == 1) || (rowIdx == numRows)) {
row_map_h(rowIdx) = row_map_h(rowIdx - 1) + 2;
} else {
row_map_h(rowIdx) = row_map_h(rowIdx - 1) + 3;
}
}
Kokkos::deep_copy(row_map, row_map_h);
if (row_map_h(numRows) != numNNZ) {
std::ostringstream error_msg;
error_msg << "error: row_map(numRows) != numNNZ, row_map_h(numRows)="
<< row_map_h(numRows) << ", numNNZ=" << numNNZ;
throw std::runtime_error(error_msg.str());
}

typename entries_type::HostMirror entries_h =
Kokkos::create_mirror_view(entries);
typename values_type::HostMirror values_h =
Kokkos::create_mirror_view(values);
for (Ordinal rowIdx = 0; rowIdx < numRows; ++rowIdx) {
if (rowIdx == 0) {
entries_h(row_map_h(rowIdx)) = rowIdx;
entries_h(row_map_h(rowIdx) + 1) = rowIdx + 1;

values_h(row_map_h(rowIdx)) = SC_ONE;
values_h(row_map_h(rowIdx) + 1) = -SC_ONE;
} else if (rowIdx == numRows - 1) {
entries_h(row_map_h(rowIdx)) = rowIdx - 1;
entries_h(row_map_h(rowIdx) + 1) = rowIdx;

values_h(row_map_h(rowIdx)) = -SC_ONE;
values_h(row_map_h(rowIdx) + 1) = SC_ONE;
} else {
entries_h(row_map_h(rowIdx)) = rowIdx - 1;
entries_h(row_map_h(rowIdx) + 1) = rowIdx;
entries_h(row_map_h(rowIdx) + 2) = rowIdx + 1;

values_h(row_map_h(rowIdx)) = -SC_ONE;
values_h(row_map_h(rowIdx) + 1) = SC_ONE + SC_ONE;
values_h(row_map_h(rowIdx) + 2) = -SC_ONE;
}
}
Kokkos::deep_copy(entries, entries_h);
Kokkos::deep_copy(values, values_h);
}

graph_type myGraph(entries, row_map);
matrix_type myMatrix("test matrix", numRows, values, myGraph);
std::cout << "myMatrix has been created successfully:" << std::endl
<< " - numRows=" << myMatrix.numRows() << std::endl
<< " - numCols=" << myMatrix.numCols() << std::endl
<< " - numNNZ= " << myMatrix.nnz() << std::endl;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The examples don't print anything at the end. Is there some performance stat or some sort of message that makes sense to print so the user knows the test is running successfully?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a fair question, at the moment if an error happens during construction the test will fail, if it reaches completion is will return 0 which ctest interprets as success.
I can add screen output that states that the matrix constructor has been called successfully, I could do the same for the graph constructor actually.

Kokkos::finalize();

return 0;
}
124 changes: 124 additions & 0 deletions example/wiki/sparse/KokkosSparse_wiki_spmv.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#include "Kokkos_Core.hpp"

#include "KokkosKernels_default_types.hpp"
#include "KokkosSparse_CrsMatrix.hpp"
#include "KokkosSparse_spmv.hpp"

using Scalar = default_scalar;
using Ordinal = default_lno_t;
using Offset = default_size_type;
using Layout = default_layout;

template <class Yvector>
struct check_spmv_functor {
Yvector y;
const Scalar SC_ONE = Kokkos::ArithTraits<Scalar>::one();

check_spmv_functor(Yvector y_) : y(y_){};

KOKKOS_INLINE_FUNCTION
void operator()(const int i, Ordinal& update) const {
if (y(i) != (SC_ONE + SC_ONE)) {
++update;
}
}
};

int main(int argc, char* argv[]) {
Kokkos::initialize();

using device_type = typename Kokkos::Device<
Kokkos::DefaultExecutionSpace,
typename Kokkos::DefaultExecutionSpace::memory_space>;
using matrix_type =
typename KokkosSparse::CrsMatrix<Scalar, Ordinal, device_type, void,
Offset>;
using graph_type = typename matrix_type::staticcrsgraph_type;
using row_map_type = typename graph_type::row_map_type;
using entries_type = typename graph_type::entries_type;
using values_type = typename matrix_type::values_type;

int return_value = 0;

{
const Scalar SC_ONE = Kokkos::ArithTraits<Scalar>::one();

Ordinal numRows = 10;

// Build the row pointers and store numNNZ
typename row_map_type::non_const_type row_map("row pointers", numRows + 1);
typename row_map_type::HostMirror row_map_h =
Kokkos::create_mirror_view(row_map);
for (Ordinal rowIdx = 1; rowIdx < numRows + 1; ++rowIdx) {
if ((rowIdx == 1) || (rowIdx == numRows)) {
row_map_h(rowIdx) = row_map_h(rowIdx - 1) + 2;
} else {
row_map_h(rowIdx) = row_map_h(rowIdx - 1) + 3;
}
}
const Offset numNNZ = row_map_h(numRows);
Kokkos::deep_copy(row_map, row_map_h);

typename entries_type::non_const_type entries("column indices", numNNZ);
typename entries_type::HostMirror entries_h =
Kokkos::create_mirror_view(entries);
typename values_type::non_const_type values("values", numNNZ);
typename values_type::HostMirror values_h =
Kokkos::create_mirror_view(values);
for (Ordinal rowIdx = 0; rowIdx < numRows; ++rowIdx) {
if (rowIdx == 0) {
entries_h(0) = rowIdx;
entries_h(1) = rowIdx + 1;

values_h(0) = SC_ONE;
values_h(1) = -SC_ONE;
} else if (rowIdx == numRows - 1) {
entries_h(row_map_h(rowIdx)) = rowIdx - 1;
entries_h(row_map_h(rowIdx) + 1) = rowIdx;

values_h(row_map_h(rowIdx)) = -SC_ONE;
values_h(row_map_h(rowIdx) + 1) = SC_ONE;
} else {
entries_h(row_map_h(rowIdx)) = rowIdx - 1;
entries_h(row_map_h(rowIdx) + 1) = rowIdx;
entries_h(row_map_h(rowIdx) + 2) = rowIdx + 1;

values_h(row_map_h(rowIdx)) = -SC_ONE;
values_h(row_map_h(rowIdx) + 1) = SC_ONE + SC_ONE;
values_h(row_map_h(rowIdx) + 2) = -SC_ONE;
}
}
Kokkos::deep_copy(entries, entries_h);
Kokkos::deep_copy(values, values_h);

graph_type myGraph(entries, row_map);
matrix_type myMatrix("test matrix", numRows, values, myGraph);

const Scalar alpha = SC_ONE;
const Scalar beta = SC_ONE;

typename values_type::non_const_type x("lhs", numRows);
typename values_type::non_const_type y("rhs", numRows);
Kokkos::deep_copy(x, SC_ONE);
Kokkos::deep_copy(y, SC_ONE + SC_ONE);

KokkosSparse::spmv("N", alpha, myMatrix, x, beta, y);

Ordinal count_errors = 0;
check_spmv_functor<values_type> check_spmv(y);
Kokkos::parallel_reduce(Kokkos::RangePolicy<Ordinal>(0, numRows),
check_spmv, count_errors);
if (count_errors > 0) {
return_value = 1;
std::cout << "Found " << count_errors << " errors in y vector!"
<< std::endl;
} else {
std::cout << "spmv was performed correctly: y = beta*y + alpha*A*x"
<< std::endl;
}
}

Kokkos::finalize();

return return_value;
}