<commit_before>// MIT License
//
// Copyright (c) 2017 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.

#include <iostream>
#include <vector>

// Google Test
#include <gtest/gtest.h>
// rocPRIM API
#include <rocprim/rocprim.hpp>

#include "test_utils.hpp"
#include "test_utils_types.hpp"

#define HIP_CHECK(error) ASSERT_EQ(error, hipSuccess)

namespace rp = rocprim;

// ---------------------------------------------------------
// Test for scan ops taking single input value
// ---------------------------------------------------------

template<class Params>
class RocprimBlockScanSingleValueTests : public ::testing::Test
{
public:
    using type = typename Params::input_type;
    static constexpr unsigned int block_size = Params::block_size;
};

TYPED_TEST_CASE(RocprimBlockScanSingleValueTests, BlockParams);

template<
    int Method,
    unsigned int BlockSize,
    rocprim::block_scan_algorithm Algorithm,
    class T
>
__global__
auto scan_kernel(T* device_output, T* device_output_b, T init)
-> typename std::enable_if<Method == 0>::type
{
    (void)init;
    (void)device_output_b;
    const unsigned int index = (hipBlockIdx_x * BlockSize) + hipThreadIdx_x;
    T value = device_output[index];
    rp::block_scan<T, BlockSize, Algorithm> bscan;
    bscan.inclusive_scan(value, value);
    device_output[index] = value;
}

template<
    int Method,
    unsigned int BlockSize,
    rocprim::block_scan_algorithm Algorithm,
    class T
>
__global__
auto scan_kernel(T* device_output, T* device_output_b, T init)
-> typename std::enable_if<Method == 1>::type
{
    (void)init;
    const unsigned int index = (hipBlockIdx_x * BlockSize) + hipThreadIdx_x;
    T value = device_output[index];
    T reduction;
    rp::block_scan<T, BlockSize, Algorithm> bscan;
    bscan.inclusive_scan(value, value, reduction);
    device_output[index] = value;
    if(hipThreadIdx_x == 0)
    {
        device_output_b[hipBlockIdx_x] = reduction;
    }
}

template<
    int Method,
    unsigned int BlockSize,
    rocprim::block_scan_algorithm Algorithm,
    class T
>
__global__
auto scan_kernel(T* device_output, T* device_output_b, T init)
-> typename std::enable_if<Method == 2>::type
{
    const unsigned int index = (hipBlockIdx_x * BlockSize) + hipThreadIdx_x;
    T prefix_value = init;
    auto prefix_callback = [&prefix_value](T reduction)
    {
        T prefix = prefix_value;
        prefix_value = prefix_value + reduction;
        return prefix;
    };

    T value = device_output[index];

    using bscan_t = rp::block_scan<T, BlockSize, Algorithm>;
    __shared__ typename bscan_t::storage_type storage;
    bscan_t().inclusive_scan(value, value, storage, prefix_callback, rp::plus<T>());

    device_output[index] = value;
    if(hipThreadIdx_x == 0)
    {
        device_output_b[hipBlockIdx_x] = prefix_value;
    }
}

template<
    int Method,
    unsigned int BlockSize,
    rocprim::block_scan_algorithm Algorithm,
    class T
>
__global__
auto scan_kernel(T* device_output, T* device_output_b, T init)
-> typename std::enable_if<Method == 3>::type
{
    (void)device_output_b;
    const unsigned int index = (hipBlockIdx_x * BlockSize) + hipThreadIdx_x;
    T value = device_output[index];
    rp::block_scan<T, BlockSize, Algorithm> bscan;
    bscan.exclusive_scan(value, value, init);
    device_output[index] = value;
}

template<
    int Method,
    unsigned int BlockSize,
    rocprim::block_scan_algorithm Algorithm,
    class T
>
__global__
auto scan_kernel(T* device_output, T* device_output_b, T init)
-> typename std::enable_if<Method == 4>::type
{
    const unsigned int index = (hipBlockIdx_x * BlockSize) + hipThreadIdx_x;
    T value = device_output[index];
    T reduction;
    rp::block_scan<T, BlockSize, Algorithm> bscan;
    bscan.exclusive_scan(value, value, init, reduction);
    device_output[index] = value;
    if(hipThreadIdx_x == 0)
    {
        device_output_b[hipBlockIdx_x] = reduction;
    }
}

template<
    int Method,
    unsigned int BlockSize,
    rocprim::block_scan_algorithm Algorithm,
    class T
>
__global__
auto scan_kernel(T* device_output, T* device_output_b, T init)
-> typename std::enable_if<Method == 5>::type
{
    const unsigned int index = (hipBlockIdx_x * BlockSize) + hipThreadIdx_x;
    T prefix_value = init;
    auto prefix_callback = [&prefix_value](T reduction)
    {
        T prefix = prefix_value;
        prefix_value = prefix_value + reduction;
        return prefix;
    };

    T value = device_output[index];

    using bscan_t = rp::block_scan<T, BlockSize, Algorithm>;
    __shared__ typename bscan_t::storage_type storage;
    bscan_t().exclusive_scan(value, value, storage, prefix_callback, rp::plus<T>());

    device_output[index] = value;
    if(hipThreadIdx_x == 0)
    {
        device_output_b[hipBlockIdx_x] = prefix_value;
    }
}

template <
    class T,
    unsigned int BlockSize,
    rp::block_scan_algorithm Algorithm,
    int Method
>
struct static_run_algo
{<commit_msg>Use alternative way for template compile-time switch (#88)<commit_after>// MIT License
//
// Copyright (c) 2017 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.

#include <iostream>
#include <vector>

// Google Test
#include <gtest/gtest.h>
// rocPRIM API
#include <rocprim/rocprim.hpp>

#include "test_utils.hpp"
#include "test_utils_types.hpp"

#define HIP_CHECK(error) ASSERT_EQ(error, hipSuccess)

namespace rp = rocprim;

// ---------------------------------------------------------
// Test for scan ops taking single input value
// ---------------------------------------------------------

template<class Params>
class RocprimBlockScanSingleValueTests : public ::testing::Test
{
public:
    using type = typename Params::input_type;
    static constexpr unsigned int block_size = Params::block_size;
};

TYPED_TEST_CASE(RocprimBlockScanSingleValueTests, BlockParams);

template<
    int Method,
    unsigned int BlockSize,
    rocprim::block_scan_algorithm Algorithm,
    class T,
    typename std::enable_if<Method == 0>::type* = nullptr
>
__global__
void scan_kernel(T* device_output, T* device_output_b, T init)
{
    (void)init;
    (void)device_output_b;
    const unsigned int index = (hipBlockIdx_x * BlockSize) + hipThreadIdx_x;
    T value = device_output[index];
    rp::block_scan<T, BlockSize, Algorithm> bscan;
    bscan.inclusive_scan(value, value);
    device_output[index] = value;
}

template<
    int Method,
    unsigned int BlockSize,
    rocprim::block_scan_algorithm Algorithm,
    class T,
    typename std::enable_if<Method == 1>::type* = nullptr
>
__global__
void scan_kernel(T* device_output, T* device_output_b, T init)
{
    (void)init;
    const unsigned int index = (hipBlockIdx_x * BlockSize) + hipThreadIdx_x;
    T value = device_output[index];
    T reduction;
    rp::block_scan<T, BlockSize, Algorithm> bscan;
    bscan.inclusive_scan(value, value, reduction);
    device_output[index] = value;
    if(hipThreadIdx_x == 0)
    {
        device_output_b[hipBlockIdx_x] = reduction;
    }
}

template<
    int Method,
    unsigned int BlockSize,
    rocprim::block_scan_algorithm Algorithm,
    class T,
    typename std::enable_if<Method == 2>::type* = nullptr
>
__global__
void scan_kernel(T* device_output, T* device_output_b, T init)
{
    const unsigned int index = (hipBlockIdx_x * BlockSize) + hipThreadIdx_x;
    T prefix_value = init;
    auto prefix_callback = [&prefix_value](T reduction)
    {
        T prefix = prefix_value;
        prefix_value = prefix_value + reduction;
        return prefix;
    };

    T value = device_output[index];

    using bscan_t = rp::block_scan<T, BlockSize, Algorithm>;
    __shared__ typename bscan_t::storage_type storage;
    bscan_t().inclusive_scan(value, value, storage, prefix_callback, rp::plus<T>());

    device_output[index] = value;
    if(hipThreadIdx_x == 0)
    {
        device_output_b[hipBlockIdx_x] = prefix_value;
    }
}

template<
    int Method,
    unsigned int BlockSize,
    rocprim::block_scan_algorithm Algorithm,
    class T,
    typename std::enable_if<Method == 3>::type* = nullptr
>
__global__
void scan_kernel(T* device_output, T* device_output_b, T init)
{
    (void)device_output_b;
    const unsigned int index = (hipBlockIdx_x * BlockSize) + hipThreadIdx_x;
    T value = device_output[index];
    rp::block_scan<T, BlockSize, Algorithm> bscan;
    bscan.exclusive_scan(value, value, init);
    device_output[index] = value;
}

template<
    int Method,
    unsigned int BlockSize,
    rocprim::block_scan_algorithm Algorithm,
    class T,
    typename std::enable_if<Method == 4>::type* = nullptr
>
__global__
void scan_kernel(T* device_output, T* device_output_b, T init)
{
    const unsigned int index = (hipBlockIdx_x * BlockSize) + hipThreadIdx_x;
    T value = device_output[index];
    T reduction;
    rp::block_scan<T, BlockSize, Algorithm> bscan;
    bscan.exclusive_scan(value, value, init, reduction);
    device_output[index] = value;
    if(hipThreadIdx_x == 0)
    {
        device_output_b[hipBlockIdx_x] = reduction;
    }
}

template<
    int Method,
    unsigned int BlockSize,
    rocprim::block_scan_algorithm Algorithm,
    class T,
    typename std::enable_if<Method == 5>::type* = nullptr
>
__global__
void scan_kernel(T* device_output, T* device_output_b, T init)
{
    const unsigned int index = (hipBlockIdx_x * BlockSize) + hipThreadIdx_x;
    T prefix_value = init;
    auto prefix_callback = [&prefix_value](T reduction)
    {
        T prefix = prefix_value;
        prefix_value = prefix_value + reduction;
        return prefix;
    };

    T value = device_output[index];

    using bscan_t = rp::block_scan<T, BlockSize, Algorithm>;
    __shared__ typename bscan_t::storage_type storage;
    bscan_t().exclusive_scan(value, value, storage, prefix_callback, rp::plus<T>());

    device_output[index] = value;
    if(hipThreadIdx_x == 0)
    {
        device_output_b[hipBlockIdx_x] = prefix_value;
    }
}

template <
    class T,
    unsigned int BlockSize,
    rp::block_scan_algorithm Algorithm,
    int Method
>
struct static_run_algo
{
{'fraction_non_alphanumeric': 0.11499141244786129, 'fraction_numerical': 0.0024535863253455467, 'mean_word_length': 3.5918137438978595, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1986666', 'n_tokens_mistral': 3854, 'n_tokens_neox': 3710, 'n_words': 1103}