/*! \file */
/* ************************************************************************
 * Copyright (C) 2022 Advanced Micro Devices, Inc. All rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * ************************************************************************ */

#pragma once

#include "common.h"

static ROCSPARSE_DEVICE_ILF void record_data_status(rocsparse_data_status* data_status,
                                                    rocsparse_data_status  status)
{
    if(status != rocsparse_data_status_success)
    {
        *data_status = status;
    }
}

// Shift CSR offsets
template <unsigned int BLOCKSIZE, typename I, typename J>
__launch_bounds__(BLOCKSIZE) ROCSPARSE_KERNEL
    void shift_offsets_kernel(J size, const I* __restrict__ in, I* __restrict__ out)
{
    J gid = hipBlockIdx_x * BLOCKSIZE + hipThreadIdx_x;

    if(gid >= size)
    {
        return;
    }

    out[gid] = in[gid] - in[0];
}

template <unsigned int BLOCKSIZE, typename I, typename J>
__launch_bounds__(BLOCKSIZE) ROCSPARSE_KERNEL
    void check_row_ptr_array(J m,
                             const I* __restrict__ csr_row_ptr,
                             rocsparse_data_status* data_status)
{
    I gid = hipBlockIdx_x * BLOCKSIZE + hipThreadIdx_x;

    if(gid < m)
    {
        I start = csr_row_ptr[gid] - csr_row_ptr[0];
        I end   = csr_row_ptr[gid + 1] - csr_row_ptr[0];

        if(start < 0 || end < 0)
        {
            record_data_status(data_status, rocsparse_data_status_invalid_offset_ptr);
            return;
        }

        if(end < start)
        {
            record_data_status(data_status, rocsparse_data_status_invalid_offset_ptr);
            return;
        }
    }
}

template <unsigned int BLOCKSIZE, unsigned int WF_SIZE, typename T, typename I, typename J>
__launch_bounds__(BLOCKSIZE) ROCSPARSE_KERNEL
    void check_matrix_csr_device(J m,
                                 J n,
                                 I nnz,
                                 const T* __restrict__ csr_val,
                                 const I* __restrict__ csr_row_ptr,
                                 const J*               csr_col_ind,
                                 const J*               csr_col_ind_sorted,
                                 rocsparse_index_base   idx_base,
                                 rocsparse_matrix_type  matrix_type,
                                 rocsparse_fill_mode    uplo,
                                 rocsparse_storage_mode storage,
                                 rocsparse_data_status* data_status)
{
    int tid = hipThreadIdx_x;
    J   gid = hipBlockIdx_x * BLOCKSIZE + tid;
    int lid = gid & (WF_SIZE - 1);

    J row = gid / WF_SIZE;

    if(row >= m)
    {
        return;
    }

    I start = csr_row_ptr[row] - csr_row_ptr[0];
    I end   = csr_row_ptr[row + 1] - csr_row_ptr[0];

    if(start < 0 || end < 0)
    {
        record_data_status(data_status, rocsparse_data_status_invalid_offset_ptr);
        return;
    }

    if(end < start)
    {
        record_data_status(data_status, rocsparse_data_status_invalid_offset_ptr);
        return;
    }

    for(I j = start + lid; j < end; j += WF_SIZE)
    {
        J col = csr_col_ind[j] - idx_base;

        // Check columns are in range [0...n)
        if(col < 0 || col >= n)
        {
            record_data_status(data_status, rocsparse_data_status_invalid_index);
            return;
        }

        // Check that there are no duplicate columns
        if(j >= start + 1)
        {
            J scol      = csr_col_ind_sorted[j] - idx_base;
            J prev_scol = csr_col_ind_sorted[j - 1] - idx_base;

            if(scol == prev_scol && (prev_scol >= 0 && prev_scol < n))
            {
                record_data_status(data_status, rocsparse_data_status_duplicate_entry);
                return;
            }
        }

        // check if values are inf or nan
        T val = csr_val[j];
        if(rocsparse_is_inf(val))
        {
            record_data_status(data_status, rocsparse_data_status_inf);
            return;
        }

        if(rocsparse_is_nan(val))
        {
            record_data_status(data_status, rocsparse_data_status_nan);
            return;
        }

        // Check matrix type and fill mode is correct
        if(matrix_type != rocsparse_matrix_type_general)
        {
            if(uplo == rocsparse_fill_mode_lower)
            {
                if(row < col)
                {
                    record_data_status(data_status, rocsparse_data_status_invalid_fill);
                    return;
                }
            }
            else
            {
                if(row > col)
                {
                    record_data_status(data_status, rocsparse_data_status_invalid_fill);
                    return;
                }
            }
        }

        // Check sorting is correct
        if(storage == rocsparse_storage_mode_sorted)
        {
            if(j >= start + 1)
            {
                J prev_col = csr_col_ind[j - 1] - idx_base;

                if(col <= prev_col && (prev_col >= 0 && prev_col < n))
                {
                    record_data_status(data_status, rocsparse_data_status_invalid_sorting);
                    return;
                }
            }
        }
    }
}
