text: /*******************************************************************************
  * Copyright 2020-2021 Intel Corporation
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
  * http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions
  * and limitations under the License.
  *
  *
  * SPDX-License-Identifier: Apache-2.0
  *******************************************************************************/
  
  #include <cstdint>
  #include <cstdlib>
  #include <iostream>
  #include <limits>
  #include <vector>
  
  #include <CL/sycl.hpp>
  #include "cblas.h"
  #include "oneapi/mkl/detail/config.hpp"
  #include "oneapi/mkl.hpp"
  #include "onemkl_blas_helper.hpp"
  #include "reference_blas_templates.hpp"
  #include "test_common.hpp"
  #include "test_helper.hpp"
  
  #include <gtest/gtest.h>
  
  using namespace cl::sycl;
  using std::vector;
  
  extern std::vector<cl::sycl::device *> devices;
  
  namespace {
  
  template <typename fp>
  int test(device *dev, oneapi::mkl::layout layout, int N, int incx, int incy, fp alpha) {
      // Prepare data.
      vector<fp> x, y, y_ref;
  
      rand_vector(x, N, incx);
      rand_vector(y, N, incy);
      y_ref = y;
  
      // Call Reference AXPY.
      using fp_ref = typename ref_type_info<fp>::type;
      const int N_ref = N, incx_ref = incx, incy_ref = incy;
  
      ::axpy(&N_ref, (fp_ref *)&alpha, (fp_ref *)x.data(), &incx_ref, (fp_ref *)y_ref.data(),
             &incy_ref);
  
      // Call DPC++ AXPY.
  
      // Catch asynchronous exceptions.
      auto exception_handler = [](exception_list exceptions) {
          for (std::exception_ptr const &e : exceptions) {
              try {
                  std::rethrow_exception(e);
              }
              catch (exception const &e) {
                  std::cout << "Caught asynchronous SYCL exception during AXPY:\n"
                            << e.what() << std::endl
                            << "OpenCL status: " << e.what() << std::endl;
              }
          }
      };
  
      queue main_queue(*dev, exception_handler);
  
      buffer<fp, 1> x_buffer = make_buffer(x);
      buffer<fp, 1> y_buffer = make_buffer(y);
  
      try {
  #ifdef CALL_RT_API
          switch (layout) {
              case oneapi::mkl::layout::column_major:
                  oneapi::mkl::blas::column_major::axpy(main_queue, N, alpha, x_buffer, incx,
                                                        y_buffer, incy);
                  break;
              case oneapi::mkl::layout::row_major:
                  oneapi::mkl::blas::row_major::axpy(main_queue, N, alpha, x_buffer, incx, y_buffer,
                                                     incy);
                  break;
              default: break;
          }
  #else
          switch (layout) {
              case oneapi::mkl::layout::column_major:
                  TEST_RUN_CT_SELECT(main_queue, oneapi::mkl::blas::column_major::axpy, N, alpha,
                                     x_buffer, incx, y_buffer, incy);
                  break;
              case oneapi::mkl::layout::row_major:
                  TEST_RUN_CT_SELECT(main_queue, oneapi::mkl::blas::row_major::axpy, N, alpha,
                                     x_buffer, incx, y_buffer, incy);
                  break;
              default: break;
          }
  #endif
      }
      catch (exception const &e) {
          std::cout << "Caught synchronous SYCL exception during AXPY:\n"
                    << e.what() << std::endl
                    << "OpenCL status: " << e.what() << std::endl;
      }
  
      catch (const oneapi::mkl::unimplemented &e) {
          return test_skipped;
      }
  
      catch (const std::runtime_error &error) {
          std::cout << "Error raised during execution of AXPY:\n" << error.what() << std::endl;
      }
  
      // Compare the results of reference implementation and DPC++ implementation.
  
      auto y_accessor = y_buffer.template get_access<access::mode::read>();
      bool good = check_equal_vector(y_accessor, y_ref, N, incy, N, std::cout);
  
      return (int)good;
  }
  
  class AxpyTests
          : public ::testing::TestWithParam<std::tuple<cl::sycl::device *, oneapi::mkl::layout>> {};
  
  TEST_P(AxpyTests, RealSinglePrecision) {
      float alpha(2.0);
      EXPECT_TRUEORSKIP(
          test<float>(std::get<0>(GetParam()), std::get<1>(GetParam()), 1357, 2, 3, alpha));
      EXPECT_TRUEORSKIP(
          test<float>(std::get<0>(GetParam()), std::get<1>(GetParam()), 1357, 1, 1, alpha));
      EXPECT_TRUEORSKIP(
          test<float>(std::get<0>(GetParam()), std::get<1>(GetParam()), 1357, -3, -2, alpha));
  }
  TEST_P(AxpyTests, RealDoublePrecision) {
      double alpha(2.0);
      EXPECT_TRUEORSKIP(
          test<double>(std::get<0>(GetParam()), std::get<1>(GetParam()), 1357, 2, 3, alpha));
      EXPECT_TRUEORSKIP(
          test<double>(std::get<0>(GetParam()), std::get<1>(GetParam()), 1357, 1, 1, alpha));
      EXPECT_TRUEORSKIP(
          test<double>(std::get<0>(GetParam()), std::get<1>(GetParam()), 1357, -3, -2, alpha));
  }
  TEST_P(AxpyTests, ComplexSinglePrecision) {
      std::complex<float> alpha(2.0, -0.5);
      EXPECT_TRUEORSKIP(test<std::complex<float>>(std::get<0>(GetParam()), std::get<1>(GetParam()),
                                                  1357, 2, 3, alpha));
      EXPECT_TRUEORSKIP(test<std::complex<float>>(std::get<0>(GetParam()), std::get<1>(GetParam()),
                                                  1357, 1, 1, alpha));
      EXPECT_TRUEORSKIP(test<std::complex<float>>(std::get<0>(GetParam()), std::get<1>(GetParam()),
                                                  1357, -3, -2, alpha));
  }
  TEST_P(AxpyTests, ComplexDoublePrecision) {
      std::complex<double> alpha(2.0, -0.5);
      EXPECT_TRUEORSKIP(test<std::complex<double>>(std::get<0>(GetParam()), std::get<1>(GetParam()),
                                                   1357, 2, 3, alpha));
      EXPECT_TRUEORSKIP(test<std::complex<double>>(std::get<0>(GetParam()), std::get<1>(GetParam()),
                                                   1357, 1, 1, alpha));
      EXPECT_TRUEORSKIP(test<std::complex<double>>(std::get<0>(GetParam()), std::get<1>(GetParam()),
                                                   1357, -3, -2, alpha));
  }
  
  INSTANTIATE_TEST_SUITE_P(AxpyTestSuite, AxpyTests,
                           ::testing::Combine(testing::ValuesIn(devices),
                                              testing::Values(oneapi::mkl::layout::column_major,
                                                              oneapi::mkl::layout::row_major)),
                           ::LayoutDeviceNamePrint());
  
  } // anonymous namespace
  