text: /*******************************<GINKGO LICENSE>******************************
  Copyright (c) 2017-2019, 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/matrix/ell.hpp>
  
  
  #include <random>
  
  
  #include <gtest/gtest.h>
  
  
  #include <core/test/utils.hpp>
  #include <ginkgo/core/base/exception.hpp>
  #include <ginkgo/core/base/exception_helpers.hpp>
  #include <ginkgo/core/base/executor.hpp>
  #include <ginkgo/core/matrix/csr.hpp>
  #include <ginkgo/core/matrix/dense.hpp>
  
  
  #include "core/matrix/ell_kernels.hpp"
  
  
  namespace {
  
  
  class Ell : public ::testing::Test {
  protected:
      using Mtx = gko::matrix::Ell<>;
      using Vec = gko::matrix::Dense<>;
  
      Ell() : rand_engine(42) {}
  
      void SetUp()
      {
          ASSERT_GT(gko::CudaExecutor::get_num_devices(), 0);
          ref = gko::ReferenceExecutor::create();
          cuda = gko::CudaExecutor::create(0, ref);
      }
  
      void TearDown()
      {
          if (cuda != nullptr) {
              ASSERT_NO_THROW(cuda->synchronize());
          }
      }
  
      std::unique_ptr<Vec> gen_mtx(int num_rows, int num_cols)
      {
          return gko::test::generate_random_matrix<Vec>(
              num_rows, num_cols, std::uniform_int_distribution<>(1, num_cols),
              std::normal_distribution<>(-1.0, 1.0), rand_engine, ref);
      }
  
      void set_up_apply_data(int num_rows = 532, int num_cols = 231,
                             int num_stored_elements_per_row = 0, int stride = 0,
                             int num_vectors = 1)
      {
          mtx = Mtx::create(ref, gko::dim<2>{}, num_stored_elements_per_row,
                            stride);
          mtx->copy_from(gen_mtx(num_rows, num_cols));
          expected = gen_mtx(num_rows, num_vectors);
          y = gen_mtx(num_cols, num_vectors);
          alpha = gko::initialize<Vec>({2.0}, ref);
          beta = gko::initialize<Vec>({-1.0}, ref);
          dmtx = Mtx::create(cuda);
          dmtx->copy_from(mtx.get());
          dresult = Vec::create(cuda);
          dresult->copy_from(expected.get());
          dy = Vec::create(cuda);
          dy->copy_from(y.get());
          dalpha = Vec::create(cuda);
          dalpha->copy_from(alpha.get());
          dbeta = Vec::create(cuda);
          dbeta->copy_from(beta.get());
      }
  
  
      std::shared_ptr<gko::ReferenceExecutor> ref;
      std::shared_ptr<const gko::CudaExecutor> cuda;
  
      std::ranlux48 rand_engine;
  
      std::unique_ptr<Mtx> mtx;
      std::unique_ptr<Vec> expected;
      std::unique_ptr<Vec> y;
      std::unique_ptr<Vec> alpha;
      std::unique_ptr<Vec> beta;
  
      std::unique_ptr<Mtx> dmtx;
      std::unique_ptr<Vec> dresult;
      std::unique_ptr<Vec> dy;
      std::unique_ptr<Vec> dalpha;
      std::unique_ptr<Vec> dbeta;
  };
  
  
  TEST_F(Ell, SimpleApplyIsEquivalentToRef)
  {
      set_up_apply_data();
  
      mtx->apply(y.get(), expected.get());
      dmtx->apply(dy.get(), dresult.get());
  
      GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14);
  }
  
  
  TEST_F(Ell, AdvancedApplyIsEquivalentToRef)
  {
      set_up_apply_data();
  
      mtx->apply(alpha.get(), y.get(), beta.get(), expected.get());
      dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get());
  
      GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14);
  }
  
  
  TEST_F(Ell, SimpleApplyWithStrideIsEquivalentToRef)
  {
      set_up_apply_data(532, 231, 300, 600);
  
      mtx->apply(y.get(), expected.get());
      dmtx->apply(dy.get(), dresult.get());
  
      GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14);
  }
  
  
  TEST_F(Ell, AdvancedApplyWithStrideIsEquivalentToRef)
  {
      set_up_apply_data(532, 231, 300, 600);
      mtx->apply(alpha.get(), y.get(), beta.get(), expected.get());
      dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get());
  
      GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14);
  }
  
  
  TEST_F(Ell, SimpleApplyWithStrideToDenseMatrixIsEquivalentToRef)
  {
      set_up_apply_data(532, 231, 300, 600, 3);
  
      mtx->apply(y.get(), expected.get());
      dmtx->apply(dy.get(), dresult.get());
  
      GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14);
  }
  
  
  TEST_F(Ell, AdvancedApplyWithStrideToDenseMatrixIsEquivalentToRef)
  {
      set_up_apply_data(532, 231, 300, 600, 3);
  
      mtx->apply(alpha.get(), y.get(), beta.get(), expected.get());
      dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get());
  
      GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14);
  }
  
  
  TEST_F(Ell, SimpleApplyByAtomicIsEquivalentToRef)
  {
      set_up_apply_data(10, 10000);
  
      mtx->apply(y.get(), expected.get());
      dmtx->apply(dy.get(), dresult.get());
  
      GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14);
  }
  
  
  TEST_F(Ell, AdvancedByAtomicApplyIsEquivalentToRef)
  {
      set_up_apply_data(10, 10000);
  
      mtx->apply(alpha.get(), y.get(), beta.get(), expected.get());
      dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get());
  
      GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14);
  }
  
  
  TEST_F(Ell, ConvertToDenseIsEquivalentToRef)
  {
      set_up_apply_data();
  
      auto dense_mtx = gko::matrix::Dense<>::create(ref);
      auto ddense_mtx = gko::matrix::Dense<>::create(cuda);
  
      mtx->convert_to(dense_mtx.get());
      dmtx->convert_to(ddense_mtx.get());
  
      GKO_ASSERT_MTX_NEAR(dense_mtx.get(), ddense_mtx.get(), 1e-14);
  }
  
  
  TEST_F(Ell, ConvertToCsrIsEquivalentToRef)
  {
      set_up_apply_data();
  
      auto csr_mtx = gko::matrix::Csr<>::create(ref);
      auto dcsr_mtx = gko::matrix::Csr<>::create(cuda);
  
      mtx->convert_to(csr_mtx.get());
      dmtx->convert_to(dcsr_mtx.get());
  
      GKO_ASSERT_MTX_NEAR(csr_mtx.get(), dcsr_mtx.get(), 1e-14);
  }
  
  
  TEST_F(Ell, CalculateNNZPerRowIsEquivalentToRef)
  {
      set_up_apply_data();
  
      gko::Array<gko::size_type> nnz_per_row;
      nnz_per_row.set_executor(ref);
      nnz_per_row.resize_and_reset(mtx->get_size()[0]);
  
      gko::Array<gko::size_type> dnnz_per_row;
      dnnz_per_row.set_executor(cuda);
      dnnz_per_row.resize_and_reset(dmtx->get_size()[0]);
  
      gko::kernels::reference::ell::calculate_nonzeros_per_row(ref, mtx.get(),
                                                               &nnz_per_row);
      gko::kernels::cuda::ell::calculate_nonzeros_per_row(cuda, dmtx.get(),
                                                          &dnnz_per_row);
  
      auto tmp = gko::Array<gko::size_type>(ref, dnnz_per_row);
      for (auto i = 0; i < nnz_per_row.get_num_elems(); i++) {
          ASSERT_EQ(nnz_per_row.get_const_data()[i], tmp.get_const_data()[i]);
      }
  }
  
  
  TEST_F(Ell, CountNNZIsEquivalentToRef)
  {
      set_up_apply_data();
  
      gko::size_type nnz;
      gko::size_type dnnz;
  
      gko::kernels::reference::ell::count_nonzeros(ref, mtx.get(), &nnz);
      gko::kernels::cuda::ell::count_nonzeros(cuda, dmtx.get(), &dnnz);
  
      ASSERT_EQ(nnz, dnnz);
  }
  
  
  }  // namespace
  