/*
 * Copyright 2020 NVIDIA Corporation.  All rights reserved.
 *
 * NOTICE TO LICENSEE:
 *
 * This source code and/or documentation ("Licensed Deliverables") are
 * subject to NVIDIA intellectual property rights under U.S. and
 * international Copyright laws.
 *
 * These Licensed Deliverables contained herein is PROPRIETARY and
 * CONFIDENTIAL to NVIDIA and is being provided under the terms and
 * conditions of a form of NVIDIA software license agreement by and
 * between NVIDIA and Licensee ("License Agreement") or electronically
 * accepted by Licensee.  Notwithstanding any terms or conditions to
 * the contrary in the License Agreement, reproduction or disclosure
 * of the Licensed Deliverables to any third party without the express
 * written consent of NVIDIA is prohibited.
 *
 * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE
 * LICENSE AGREEMENT, NVIDIA MAKES NO REPRESENTATION ABOUT THE
 * SUITABILITY OF THESE LICENSED DELIVERABLES FOR ANY PURPOSE.  IT IS
 * PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND.
 * NVIDIA DISCLAIMS ALL WARRANTIES WITH REGARD TO THESE LICENSED
 * DELIVERABLES, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY,
 * NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE.
 * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE
 * LICENSE AGREEMENT, IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY
 * SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, OR ANY
 * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
 * OF THESE LICENSED DELIVERABLES.
 *
 * U.S. Government End Users.  These Licensed Deliverables are a
 * "commercial item" as that term is defined at 48 C.F.R. 2.101 (OCT
 * 1995), consisting of "commercial computer software" and "commercial
 * computer software documentation" as such terms are used in 48
 * C.F.R. 12.212 (SEPT 1995) and is provided to the U.S. Government
 * only as a commercial end item.  Consistent with 48 C.F.R.12.212 and
 * 48 C.F.R. 227.7202-1 through 227.7202-4 (JUNE 1995), all
 * U.S. Government End Users acquire the Licensed Deliverables with
 * only those rights set forth herein.
 *
 * Any use of the Licensed Deliverables in individual and commercial
 * software must include, in the user documentation and internal
 * comments to the code, the above Disclaimer and U.S. Government End
 * Users Notice.
 */

#include <sycl/sycl.hpp>
#include <dpct/dpct.hpp>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <oneapi/mkl.hpp>
#include <dpct/blas_utils.hpp>

#include "cublas_utils.h"

using data_type = double;

int main(int argc, char *argv[]) try {
    dpct::device_ext &dev_ct1 = dpct::get_current_device();
    sycl::queue &q_ct1 = dev_ct1.default_queue();
    sycl::queue *cublasH = NULL;
    dpct::queue_ptr stream = &q_ct1;

    /*
     *   A = 2.10
     *   B = 1.20
     */

    data_type A = 2.1;
    data_type B = 1.2;
    data_type c = 2.1;
    data_type s = 1.2;

    printf("A\n");
    std::printf("%0.2f\n", A);
    printf("=====\n");

    printf("B\n");
    std::printf("%0.2f\n", B);
    printf("=====\n");

    /* step 1: create cublas handle, bind a stream */
    /*
    DPCT1003:78: Migrated API does not return error code. (*, 0) is inserted.
    You may need to rewrite this code.
    */
    CUBLAS_CHECK((cublasH = &q_ct1, 0));

    /*
    DPCT1003:79: Migrated API does not return error code. (*, 0) is inserted.
    You may need to rewrite this code.
    */
    /*
    DPCT1025:80: The SYCL queue is created ignoring the flag and priority
    options.
    */
    CUDA_CHECK((stream = dev_ct1.create_queue(), 0));
    /*
    DPCT1003:81: Migrated API does not return error code. (*, 0) is inserted.
    You may need to rewrite this code.
    */
    CUBLAS_CHECK((cublasH = stream, 0));

    /* step 3: compute */
    /*
    DPCT1034:82: Migrated API does not return error code. 0 is returned in the
    lambda. You may need to rewrite this code.
    */
  CUBLAS_CHECK([&]() {
    double *a_ct9 = &A;
    double *b_ct10 = &B;
    double *c_ct11 = &c;
    double *s_ct12 = &s;
    if (sycl::get_pointer_type(&A, cublasH->get_context()) !=
            sycl::usm::alloc::device &&
        sycl::get_pointer_type(&A, cublasH->get_context()) !=
            sycl::usm::alloc::shared) {
      a_ct9 = sycl::malloc_shared<double>(4, dpct::get_default_queue());
      b_ct10 = a_ct9 + 1;
      c_ct11 = a_ct9 + 2;
      s_ct12 = a_ct9 + 3;
      *a_ct9 = A;
      *b_ct10 = B;
      *c_ct11 = c;
      *s_ct12 = s;
    }
    oneapi::mkl::blas::column_major::rotg(*cublasH, a_ct9, b_ct10, c_ct11,
                                          s_ct12);
    if (sycl::get_pointer_type(&A, cublasH->get_context()) !=
            sycl::usm::alloc::device &&
        sycl::get_pointer_type(&A, cublasH->get_context()) !=
            sycl::usm::alloc::shared) {
      cublasH->wait();
      A = *a_ct9;
      B = *b_ct10;
      c = *c_ct11;
      s = *s_ct12;
      sycl::free(a_ct9, dpct::get_default_queue());
    }
    return 0;
  }());

    /*
    DPCT1003:83: Migrated API does not return error code. (*, 0) is inserted.
    You may need to rewrite this code.
    */
    CUDA_CHECK((stream->wait(), 0));

    /*
     *   A = 2.42
     *   B = 0.50
     */

     printf("A\n");
     std::printf("%0.2f\n", A);
     printf("=====\n");
 
     printf("B\n");
     std::printf("%0.2f\n", B);
     printf("=====\n");

    /* free resources */
    /*
    DPCT1003:84: Migrated API does not return error code. (*, 0) is inserted.
    You may need to rewrite this code.
    */
    CUBLAS_CHECK((cublasH = nullptr, 0));

    CUDA_CHECK((dev_ct1.destroy_queue(stream), 0));

    CUDA_CHECK((dev_ct1.reset(), 0));

    return EXIT_SUCCESS;
}
catch (sycl::exception const &exc) {
  std::cerr << exc.what() << "Exception caught at file:" << __FILE__
            << ", line:" << __LINE__ << std::endl;
  std::exit(1);
}
