text: /*******************************<GINKGO LICENSE>******************************
  Copyright 2017-2018
  
  Karlsruhe Institute of Technology
  Universitat Jaume I
  University of Tennessee
  
  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>*******************************/
  
  /*****************************<COMPILATION>***********************************
  The easiest way to build the example solver is to use the script provided:
  ./build.sh <PATH_TO_GINKGO_BUILD_DIR>
  
  Ginkgo should be compiled with `-DBUILD_REFERENCE=on` option.
  
  Alternatively, you can setup the configuration manually:
  
  Go to the <PATH_TO_GINKGO_BUILD_DIR> directory and copy the shared
  libraries located in the following subdirectories:
  
      + core/
      + core/device_hooks/
      + reference/
      + omp/
      + cuda/
  
  to this directory.
  
  Then compile the file with the following command line:
  
  c++ -std=c++11 -o ranges ranges.cpp -I../.. \
      -L. -lginkgo -lginkgo_reference -lginkgo_omp -lginkgo_cuda
  
  (if ginkgo was built in debug mode, append 'd' to every library name)
  
  Now you should be able to run the program using:
  
  env LD_LIBRARY_PATH=.:${LD_LIBRARY_PATH} ./ranges
  
  *****************************<COMPILATION>**********************************/
  
  #include <ginkgo/ginkgo.hpp>
  #include <iomanip>
  #include <iostream>
  
  
  // LU factorization implementation using Ginkgo ranges
  // For simplicity, we only consider square matrices, and no pivoting.
  template <typename Accessor>
  void factorize(const gko::range<Accessor> &A)
  // note: const means that the range (i.e. the data handler) is constant,
  //       not that the underlying data is constant!
  {
      using gko::span;
      assert(A.length(0) == A.length(1));
      for (gko::size_type i = 0; i < A.length(0) - 1; ++i) {
          const auto trail = span{i + 1, A.length(0)};
          // note: neither of the lines below need additional memory to store
          //       intermediate arrays, all computation is done at the point of
          //       assignment
          A(trail, i) = A(trail, i) / A(i, i);
          // caveat: operator * is element-wise multiplication, mmul is matrix
          //         multiplication
          A(trail, trail) = A(trail, trail) - mmul(A(trail, i), A(i, trail));
      }
  }
  
  
  // a utility function for printing the factorization on screen
  template <typename Accessor>
  void print_lu(const gko::range<Accessor> &A)
  {
      std::cout << std::setprecision(2) << std::fixed;
      std::cout << "L = [";
      for (int i = 0; i < A.length(0); ++i) {
          std::cout << "\n  ";
          for (int j = 0; j < A.length(1); ++j) {
              std::cout << (i > j ? A(i, j) : (i == j) * 1.) << " ";
          }
      }
      std::cout << "\n]\n\nU = [";
      for (int i = 0; i < A.length(0); ++i) {
          std::cout << "\n  ";
          for (int j = 0; j < A.length(1); ++j) {
              std::cout << (i <= j ? A(i, j) : 0.) << " ";
          }
      }
      std::cout << "\n]" << std::endl;
  }
  
  
  int main(int argc, char *argv[])
  {
      // Print version information
      std::cout << gko::version_info::get() << std::endl;
  
      // Create some test data, add some padding just to demonstrate how to use it
      // with ranges.
      // clang-format off
      double data[] = {
          2.,  4.,  5., -1.0,
          4., 11., 12., -1.0,
          6., 24., 24., -1.0
      };
      // clang-format on
  
      // Create a 3-by-3 range, with a 2D row-major accessor using data as the
      // underlying storage. Set the stride (a.k.a. "LDA") to 4.
      auto A = gko::range<gko::accessor::row_major<double, 2>>(data, 3u, 3u, 4u);
  
      // use the LU factorization routine defined above to factorize the matrix
      factorize(A);
  
      // print the factorization on screen
      print_lu(A);
  }
  