File: example_utils.hpp

package info (click to toggle)
rocprim 5.3.3-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,656 kB
  • sloc: cpp: 60,198; python: 624; sh: 203; xml: 200; makefile: 109
file content (119 lines) | stat: -rw-r--r-- 4,370 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright (c) 2017-2019 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.

#ifndef ROCPRIM_EXAMPLE_UTILS_HPP_
#define ROCPRIM_EXAMPLE_UTILS_HPP_

#include <algorithm>
#include <vector>
#include <random>
#include <type_traits>

#include <rocprim/rocprim.hpp>

#define HIP_CHECK(condition)         \
{                                    \
    hipError_t error = condition;    \
    if(error != hipSuccess){         \
        std::cout << "HIP error: " << error << " line: " << __LINE__ << std::endl; \
        exit(error); \
    } \
}

#define OUTPUT_VALIDATION_CHECK(validation_result)               \
  {                                                              \
    if ( validation_result == false )                            \
    {                                                            \
        std::cout << "Output validation failed!" << std::endl;   \
        return;                                                  \
    }                                                            \
  }

template<class T>
inline auto get_random_data(size_t size, T min, T max)
    -> typename std::enable_if<std::is_integral<T>::value, std::vector<T>>::type
{
    std::random_device rd;
    std::default_random_engine gen(rd());
    std::uniform_int_distribution<T> distribution(min, max);
    std::vector<T> data(size);
    std::generate(data.begin(), data.end(), [&]() { return distribution(gen); });
    return data;
}

template<class T>
inline void hip_read_device_memory(std::vector<T> &host_destination, T *device_source)
{
    HIP_CHECK(
        hipMemcpy(
            host_destination.data(), device_source,
            host_destination.size() * sizeof(T),
            hipMemcpyDeviceToHost
        )
    );
}

template<class T>
inline void hip_write_device_memory(T *device_destination, std::vector<T>& host_source)
{
    HIP_CHECK(
        hipMemcpy(
            device_destination, host_source.data(),
            host_source.size() * sizeof(T),
            hipMemcpyHostToDevice
        )
    );
}

template<class T>
inline bool validate_device_output(const std::vector<T> &host_output, const std::vector<T> &expected_output)
{
    for(unsigned int index = 0; index < host_output.size(); index++)
    {
        if(host_output[index] != expected_output[index])
        {
            return false;
        }
    }
    return true;
}

// Generating expected output for block scan when using rocprim::plus as function
template<class T>
std::vector<T> get_expected_output(
    const std::vector<T> &host_input,
    const unsigned int block_size,
    const unsigned int items_per_thread = 1)
{
    unsigned int grid_size = host_input.size() / block_size;
    std::vector<T> host_expected_output(host_input.size());
    for(unsigned int block_index = 0; block_index < (grid_size / items_per_thread); block_index++)
    {
        host_expected_output[block_index * block_size] = host_input[block_index * block_size];
        for(unsigned int thread_index = 1; thread_index < (block_size * items_per_thread); thread_index++)
        {
            int index = block_index * block_size + thread_index;
            host_expected_output[index] = host_expected_output[index - 1] + host_input[index];
        }
    }
    return host_expected_output;
}

#endif // ROCPRIM_EXAMPLE_UTILS_HPP_