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

Common cleanup #1431

Merged
merged 5 commits into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions example/gmres/ex_real_A.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
*/

#include <math.h>
#include "KokkosKernels_IOUtils.hpp"
#include "KokkosSparse_IOUtils.hpp"
#include <Kokkos_Core.hpp>
#include <Kokkos_Random.hpp>
#include <KokkosBlas.hpp>
Expand Down Expand Up @@ -117,7 +117,7 @@ int main(int argc, char* argv[]) {
{
// Read in a matrix Market file and use it to test the Kokkos Operator.
KokkosSparse::CrsMatrix<ST, OT, EXSP> A =
KokkosKernels::Impl::read_kokkos_crst_matrix<
KokkosSparse::Impl::read_kokkos_crst_matrix<
Copy link
Contributor

@brian-kelley brian-kelley Jun 3, 2022

Choose a reason for hiding this comment

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

Since you're moving this into a different namespace anyway, maybe it should just be in KokkosSparse::? I think it's pretty common enough for external users to need a MatrixMarket reader.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, that is how it should be, I thought I had changed the namespace but maybe I forgot...
I should really re-import everything back and mark as deprecated but that will be pretty messy : (

KokkosSparse::CrsMatrix<ST, OT, EXSP>>(filename.c_str());

int n = A.numRows();
Expand Down
3 changes: 2 additions & 1 deletion example/gmres/test_cmplx_A.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

#include <math.h>
#include "KokkosKernels_IOUtils.hpp"
#include "KokkosSparse_IOUtils.hpp"
#include <Kokkos_Core.hpp>
#include <Kokkos_Random.hpp>
#include <KokkosBlas.hpp>
Expand Down Expand Up @@ -77,7 +78,7 @@ int main(int /*argc*/, char** /*argv[]*/) {
{
// Read in a matrix Market file and use it to test the Kokkos Operator.
KokkosSparse::CrsMatrix<ST, OT, EXSP> A =
KokkosKernels::Impl::read_kokkos_crst_matrix<
KokkosSparse::Impl::read_kokkos_crst_matrix<
KokkosSparse::CrsMatrix<ST, OT, EXSP>>(filename.c_str());

int n = A.numRows();
Expand Down
5 changes: 3 additions & 2 deletions example/gmres/test_prec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include <Kokkos_Random.hpp>
#include <KokkosBlas.hpp>
#include <KokkosSparse_spmv.hpp>
#include "KokkosSparse_IOUtils.hpp"

int main(int argc, char* argv[]) {
typedef double ST;
Expand Down Expand Up @@ -114,13 +115,13 @@ int main(int argc, char* argv[]) {
{
// Generate a diagonal matrix with entries 1, 2, ...., 1000 and its inverse.
KokkosSparse::CrsMatrix<ST, OT, EXSP> A =
KokkosKernels::Impl::kk_generate_diag_matrix<
KokkosSparse::Impl::kk_generate_diag_matrix<
KokkosSparse::CrsMatrix<ST, OT, EXSP>>(n);
KokkosSparse::Experimental::MatrixPrec<ST, Kokkos::LayoutLeft, EXSP, OT>*
myPrec =
new KokkosSparse::Experimental::MatrixPrec<ST, Kokkos::LayoutLeft,
EXSP, OT>(
KokkosKernels::Impl::kk_generate_diag_matrix<
KokkosSparse::Impl::kk_generate_diag_matrix<
KokkosSparse::CrsMatrix<ST, OT, EXSP>>(n, true));

ViewVectorType X(Kokkos::view_alloc(Kokkos::WithoutInitializing, "X"),
Expand Down
3 changes: 2 additions & 1 deletion example/gmres/test_real_A.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

#include <math.h>
#include "KokkosKernels_IOUtils.hpp"
#include "KokkosSparse_IOUtils.hpp"
#include <Kokkos_Core.hpp>
#include <Kokkos_Random.hpp>
#include <KokkosBlas.hpp>
Expand Down Expand Up @@ -89,7 +90,7 @@ int main(int /*argc*/, char** /*argv[]*/) {
cOT diagDominance = 1;
nnz = 10 * numRows;
sp_matrix_type A =
KokkosKernels::Impl::kk_generate_diagonally_dominant_sparse_matrix<
KokkosSparse::Impl::kk_generate_diagonally_dominant_sparse_matrix<
sp_matrix_type>(numRows, numCols, nnz, 0, ncOT(0.01 * numRows),
diagDominance);

Expand Down
100 changes: 54 additions & 46 deletions example/wiki/sparse/KokkosSparse_wiki_gauss_seidel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,88 +2,96 @@
#include "KokkosKernels_default_types.hpp"
#include "KokkosKernels_Handle.hpp"
#include "KokkosKernels_IOUtils.hpp"
#include "KokkosSparse_IOUtils.hpp"
#include "KokkosSparse_spmv.hpp"
#include "KokkosSparse_CrsMatrix.hpp"
#include "KokkosSparse_gauss_seidel.hpp"
#include "KokkosBlas1_nrm2.hpp"

//Parallel Gauss-Seidel Preconditioner/Smoother
// Parallel Gauss-Seidel Preconditioner/Smoother
// -Uses graph coloring to find independent row sets,
// and applies GS to each set in parallel
// -Here, use to solve a diagonally dominant linear system directly.

//Helper to print out colors in the shape of the grid
int main()
{
using Scalar = default_scalar;
using Mag = Kokkos::ArithTraits<Scalar>::mag_type;
using Ordinal = default_lno_t;
using Offset = default_size_type;
// Helper to print out colors in the shape of the grid
int main() {
using Scalar = default_scalar;
using Mag = Kokkos::ArithTraits<Scalar>::mag_type;
using Ordinal = default_lno_t;
using Offset = default_size_type;
using ExecSpace = Kokkos::DefaultExecutionSpace;
using MemSpace = typename ExecSpace::memory_space;
using Device = Kokkos::Device<ExecSpace, MemSpace>;
using Handle = KokkosKernels::Experimental::
KokkosKernelsHandle<Offset, Ordinal, default_scalar, ExecSpace, MemSpace, MemSpace>;
using Matrix = KokkosSparse::CrsMatrix<Scalar, Ordinal, Device, void, Offset>;
using Vector = typename Matrix::values_type;
using MemSpace = typename ExecSpace::memory_space;
using Device = Kokkos::Device<ExecSpace, MemSpace>;
using Handle = KokkosKernels::Experimental::KokkosKernelsHandle<
Offset, Ordinal, default_scalar, ExecSpace, MemSpace, MemSpace>;
using Matrix = KokkosSparse::CrsMatrix<Scalar, Ordinal, Device, void, Offset>;
using Vector = typename Matrix::values_type;
constexpr Ordinal numRows = 10000;
const Scalar one = Kokkos::ArithTraits<Scalar>::one();
const Mag magOne = Kokkos::ArithTraits<Mag>::one();
//Solve tolerance
const Scalar one = Kokkos::ArithTraits<Scalar>::one();
const Mag magOne = Kokkos::ArithTraits<Mag>::one();
// Solve tolerance
const Mag tolerance = 1e-6 * magOne;
Kokkos::initialize();
{
//Generate a square, strictly diagonally dominant, but nonsymmetric matrix on which Gauss-Seidel should converge.
//Get approx. 20 entries per row
//Diagonals are 2x the absolute sum of all other entries.
// Generate a square, strictly diagonally dominant, but nonsymmetric matrix
// on which Gauss-Seidel should converge. Get approx. 20 entries per row
// Diagonals are 2x the absolute sum of all other entries.
Offset nnz = numRows * 20;
Matrix A = KokkosKernels::Impl::kk_generate_diagonally_dominant_sparse_matrix<Matrix>(numRows, numRows, nnz, 2, 100, 1.05 * one);
std::cout << "Generated a matrix with " << numRows << " rows/cols, and " << nnz << " entries.\n";
//Create a kernel handle, then a Gauss-Seidel handle with the default algorithm
Matrix A =
KokkosSparse::Impl::kk_generate_diagonally_dominant_sparse_matrix<
Matrix>(numRows, numRows, nnz, 2, 100, 1.05 * one);
std::cout << "Generated a matrix with " << numRows << " rows/cols, and "
<< nnz << " entries.\n";
// Create a kernel handle, then a Gauss-Seidel handle with the default
// algorithm
Handle handle;
handle.create_gs_handle(KokkosSparse::GS_DEFAULT);
//Set up Gauss-Seidel for the graph (matrix sparsity pattern)
KokkosSparse::Experimental::gauss_seidel_symbolic(&handle, numRows, numRows, A.graph.row_map, A.graph.entries, false);
//Set up Gauss-Seidel for the matrix values (numeric)
//Another matrix with the same sparsity pattern could re-use the handle and symbolic phase, and only call numeric.
KokkosSparse::Experimental::gauss_seidel_numeric(&handle, numRows, numRows, A.graph.row_map, A.graph.entries, A.values, false);
//Now, preconditioner is ready to use. Set up an unknown vector (uninitialized) and randomized right-hand-side vector.
// Set up Gauss-Seidel for the graph (matrix sparsity pattern)
KokkosSparse::Experimental::gauss_seidel_symbolic(
&handle, numRows, numRows, A.graph.row_map, A.graph.entries, false);
// Set up Gauss-Seidel for the matrix values (numeric)
// Another matrix with the same sparsity pattern could re-use the handle and
// symbolic phase, and only call numeric.
KokkosSparse::Experimental::gauss_seidel_numeric(
&handle, numRows, numRows, A.graph.row_map, A.graph.entries, A.values,
false);
// Now, preconditioner is ready to use. Set up an unknown vector
// (uninitialized) and randomized right-hand-side vector.
Vector x(Kokkos::view_alloc(Kokkos::WithoutInitializing, "x"), numRows);
Vector b(Kokkos::view_alloc(Kokkos::WithoutInitializing, "b"), numRows);
Vector res(Kokkos::view_alloc(Kokkos::WithoutInitializing, "res"), numRows);
auto bHost = Kokkos::create_mirror_view(b);
for(Ordinal i = 0; i < numRows; i++)
for (Ordinal i = 0; i < numRows; i++)
bHost(i) = 3 * ((one * rand()) / RAND_MAX);
Kokkos::deep_copy(b, bHost);
//Measure initial residual norm ||Ax - b||, where x is 0
Mag initialRes = KokkosBlas::nrm2(b);
// Measure initial residual norm ||Ax - b||, where x is 0
Mag initialRes = KokkosBlas::nrm2(b);
Mag scaledResNorm = magOne;
bool firstIter = true;
//Iterate until reaching the tolerance
bool firstIter = true;
// Iterate until reaching the tolerance
int numIters = 0;
while(scaledResNorm > tolerance)
{
//Run one sweep of forward Gauss-Seidel (SOR with omega = 1.0)
//If this is the first iteration, tell apply:
while (scaledResNorm > tolerance) {
// Run one sweep of forward Gauss-Seidel (SOR with omega = 1.0)
// If this is the first iteration, tell apply:
// * to zero out x (it was uninitialized)
// * that b has changed since the previous apply (since there was no previous apply)
// * that b has changed since the previous apply (since there was no
// previous apply)
KokkosSparse::Experimental::forward_sweep_gauss_seidel_apply(
&handle, numRows, numRows,
A.graph.row_map, A.graph.entries, A.values,
&handle, numRows, numRows, A.graph.row_map, A.graph.entries, A.values,
x, b, firstIter, firstIter, one, 1);
firstIter = false;
//Now, compute the new residual norm using SPMV
// Now, compute the new residual norm using SPMV
Kokkos::deep_copy(res, b);
//Compute res := Ax - res (since res is now equal to b, this is Ax - b)
// Compute res := Ax - res (since res is now equal to b, this is Ax - b)
KokkosSparse::spmv("N", one, A, x, -one, res);
//Recompute the scaled norm
// Recompute the scaled norm
scaledResNorm = KokkosBlas::nrm2(res) / initialRes;
numIters++;
std::cout << "Iteration " << numIters << " scaled residual norm: " << scaledResNorm << '\n';
std::cout << "Iteration " << numIters
<< " scaled residual norm: " << scaledResNorm << '\n';
}
std::cout << "SUCCESS: converged in " << numIters << " iterations.\n";
}
Kokkos::finalize();
return 0;
}

7 changes: 3 additions & 4 deletions perf_test/graph/KokkosGraph_color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
#include "KokkosKernels_TestParameters.hpp"
#include "KokkosGraph_Distance1Color.hpp"
#include "KokkosKernels_TestUtils.hpp"
#include "KokkosSparse_IOUtils.hpp"

void print_options(std::ostream &os, const char *app_name,
unsigned int indent = 0) {
Expand Down Expand Up @@ -376,16 +377,14 @@ void run_multi_mem_experiment(Parameters params) {
if (params.a_mem_space == 1) {
fast_crstmat_t a_fast_crsmat;
a_fast_crsmat =
KokkosKernels::Impl::read_kokkos_crst_matrix<fast_crstmat_t>(
a_mat_file);
KokkosSparse::Impl::read_kokkos_crst_matrix<fast_crstmat_t>(a_mat_file);
a_fast_crsgraph = a_fast_crsmat.graph;
num_cols = a_fast_crsmat.numCols();

} else {
slow_crstmat_t a_slow_crsmat;
a_slow_crsmat =
KokkosKernels::Impl::read_kokkos_crst_matrix<slow_crstmat_t>(
a_mat_file);
KokkosSparse::Impl::read_kokkos_crst_matrix<slow_crstmat_t>(a_mat_file);
a_slow_crsgraph = a_slow_crsmat.graph;
num_cols = a_slow_crsmat.numCols();
}
Expand Down
3 changes: 2 additions & 1 deletion perf_test/graph/KokkosGraph_color_d2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
#include <KokkosGraph_Distance2Color.hpp>
#include "KokkosKernels_default_types.hpp"
#include "KokkosKernels_TestUtils.hpp"
#include "KokkosSparse_IOUtils.hpp"

using namespace KokkosGraph;

Expand Down Expand Up @@ -595,7 +596,7 @@ void experiment_driver(const D2Parameters& params) {
using graph_t = typename crsMat_t::StaticCrsGraphType;

crsMat_t A =
KokkosKernels::Impl::read_kokkos_crst_matrix<crsMat_t>(params.mtx_file);
KokkosSparse::Impl::read_kokkos_crst_matrix<crsMat_t>(params.mtx_file);
graph_t Agraph = A.graph;
int num_cols = A.numCols();

Expand Down
3 changes: 2 additions & 1 deletion perf_test/graph/KokkosGraph_mis_d2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
#include "KokkosGraph_MIS2.hpp"
#include "KokkosKernels_default_types.hpp"
#include "KokkosKernels_TestUtils.hpp"
#include "KokkosSparse_IOUtils.hpp"

using namespace KokkosGraph;

Expand Down Expand Up @@ -253,7 +254,7 @@ void run_mis2(const MIS2Parameters& params) {

Kokkos::Timer t;
crsMat_t A_in =
KokkosKernels::Impl::read_kokkos_crst_matrix<crsMat_t>(params.mtx_file);
KokkosSparse::Impl::read_kokkos_crst_matrix<crsMat_t>(params.mtx_file);
std::cout << "I/O time: " << t.seconds() << " s\n";
t.reset();
// Symmetrize the matrix just in case
Expand Down
5 changes: 3 additions & 2 deletions perf_test/sparse/KokkosSparse_gs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#include <KokkosBlas1_nrm2.hpp>
#include <KokkosKernels_config.h>
#include "KokkosKernels_default_types.hpp"
#include "KokkosSparse_IOUtils.hpp"
#include <iostream>
#include <random>
#include <vector>
Expand Down Expand Up @@ -177,7 +178,7 @@ crsMat_t generateLongRowMatrix(const GS_Parameters& params) {
rowmap.data(), numRows + 1));
crsMat_t A("A", numRows, numRows, totalEntries, valuesView, rowmapView,
entriesView);
A = KokkosKernels::sort_and_merge_matrix(A);
A = KokkosSparse::sort_and_merge_matrix(A);
if (params.graph_symmetric) {
// Symmetrize on host, rather than relying on the parallel versions (those
// can be tested for symmetric=false)
Expand All @@ -203,7 +204,7 @@ void runGS(const GS_Parameters& params) {
typedef typename crsMat_t::values_type::non_const_type scalar_view_t;
crsMat_t A;
if (params.matrix_path)
A = KokkosKernels::Impl::read_kokkos_crst_matrix<crsMat_t>(
A = KokkosSparse::Impl::read_kokkos_crst_matrix<crsMat_t>(
params.matrix_path);
else
A = generateLongRowMatrix<crsMat_t>(params);
Expand Down
5 changes: 3 additions & 2 deletions perf_test/sparse/KokkosSparse_kk_spmv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
#include <Kokkos_Core.hpp>
#include <KokkosSparse_CrsMatrix.hpp>
#include <KokkosKernels_IOUtils.hpp>
#include <KokkosSparse_IOUtils.hpp>
#include <KokkosSparse_spmv.hpp>
#include "KokkosKernels_default_types.hpp"

Expand All @@ -74,11 +75,11 @@ void run_spmv(Ordinal numRows, Ordinal numCols, const char* filename, int loop,
srand(17312837);
matrix_type A;
if (filename)
A = KokkosKernels::Impl::read_kokkos_crst_matrix<matrix_type>(filename);
A = KokkosSparse::Impl::read_kokkos_crst_matrix<matrix_type>(filename);
else {
Offset nnz = 10 * numRows;
// note: the help text says the bandwidth is fixed at 0.01 * numRows
A = KokkosKernels::Impl::kk_generate_sparse_matrix<matrix_type>(
A = KokkosSparse::Impl::kk_generate_sparse_matrix<matrix_type>(
numRows, numCols, nnz, 0, 0.01 * numRows);
}
numRows = A.numRows();
Expand Down
21 changes: 9 additions & 12 deletions perf_test/sparse/KokkosSparse_multimem_spgemm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

#include "KokkosSparse_CrsMatrix.hpp"
#include "KokkosSparse_run_spgemm.hpp"
#include "KokkosSparse_IOUtils.hpp"

namespace KokkosKernels {

Expand Down Expand Up @@ -74,12 +75,10 @@ void run_multi_mem_spgemm(Parameters params) {

if (params.a_mem_space == 1) {
a_fast_crsmat =
KokkosKernels::Impl::read_kokkos_crst_matrix<fast_crstmat_t>(
a_mat_file);
KokkosSparse::Impl::read_kokkos_crst_matrix<fast_crstmat_t>(a_mat_file);
} else {
a_slow_crsmat =
KokkosKernels::Impl::read_kokkos_crst_matrix<slow_crstmat_t>(
a_mat_file);
KokkosSparse::Impl::read_kokkos_crst_matrix<slow_crstmat_t>(a_mat_file);
}

if ((b_mat_file == NULL || strcmp(b_mat_file, a_mat_file) == 0) &&
Expand All @@ -90,13 +89,11 @@ void run_multi_mem_spgemm(Parameters params) {
} else if (params.b_mem_space == 1) {
if (b_mat_file == NULL) b_mat_file = a_mat_file;
b_fast_crsmat =
KokkosKernels::Impl::read_kokkos_crst_matrix<fast_crstmat_t>(
b_mat_file);
KokkosSparse::Impl::read_kokkos_crst_matrix<fast_crstmat_t>(b_mat_file);
} else {
if (b_mat_file == NULL) b_mat_file = a_mat_file;
b_slow_crsmat =
KokkosKernels::Impl::read_kokkos_crst_matrix<slow_crstmat_t>(
b_mat_file);
KokkosSparse::Impl::read_kokkos_crst_matrix<slow_crstmat_t>(b_mat_file);
}

if (params.a_mem_space == 1) {
Expand Down Expand Up @@ -222,18 +219,18 @@ void run_multi_mem_spgemm(Parameters params) {

if (c_mat_file != NULL) {
if (params.c_mem_space == 1) {
KokkosKernels::sort_crs_matrix(c_fast_crsmat);
KokkosSparse::sort_crs_matrix(c_fast_crsmat);

KokkosKernels::Impl::write_graph_bin(
KokkosSparse::Impl::write_graph_bin(
(lno_t)(c_fast_crsmat.numRows()),
(size_type)(c_fast_crsmat.graph.entries.extent(0)),
c_fast_crsmat.graph.row_map.data(),
c_fast_crsmat.graph.entries.data(), c_fast_crsmat.values.data(),
c_mat_file);
} else {
KokkosKernels::sort_crs_matrix(c_slow_crsmat);
KokkosSparse::sort_crs_matrix(c_slow_crsmat);

KokkosKernels::Impl::write_graph_bin(
KokkosSparse::Impl::write_graph_bin(
(lno_t)c_slow_crsmat.numRows(),
(size_type)c_slow_crsmat.graph.entries.extent(0),
c_slow_crsmat.graph.row_map.data(),
Expand Down
Loading