reduction_functions_accumulate.hpp 3.56 KB
Newer Older
Umang Yadav's avatar
Umang Yadav committed
1
2
3

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Weverything"
Chao Liu's avatar
Chao Liu committed
4
// SPDX-License-Identifier: MIT
Illia Silin's avatar
Illia Silin committed
5
// Copyright (c) 2018-2023, Advanced Micro Devices, Inc. All rights reserved.
Chao Liu's avatar
Chao Liu committed
6

Chao Liu's avatar
Chao Liu committed
7
8
9
10
11
12
#pragma once

#include "ck/utility/data_type.hpp"
#include "ck/utility/math_v2.hpp"
#include "ck/utility/reduction_common.hpp"
#include "ck/utility/reduction_operator.hpp"
13
14
15
16

namespace ck {
namespace detail {

17
18
19
20
21
22
// Check for NaN; guarantee NaNs are NOT propagated to result (i.e., ignore NaNs)
template <typename ReduceOperation, typename AccDataType>
struct AccumulateWithNanIgnore
{
    __device__ static inline void Calculate(AccDataType& accuVal, AccDataType currVal)
    {
23
        if(!ck::math::isnan(currVal))
24
25
26
27
28
29
        {
            ReduceOperation{}(accuVal, currVal);
        }
    };
};

30
31
template <bool PropagateNan, typename ReduceOperation, typename AccDataType>
struct AccumulateWithNanCheck;
32

33
34
35
36
37
// Does not check for NaN; does not guarantee NaNs be propagated to result
// e.g., given that max(a, b) = a > b ? a : b
// then  max(NaN, 1) returns 1
//       max(1, NaN) returns NaN
// since any comparison involving NaNs returns false
38
39
template <typename ReduceOperation, typename AccDataType>
struct AccumulateWithNanCheck<false, ReduceOperation, AccDataType>
40
41
{
    // cppcheck-suppress constParameter
42
    __host__ __device__ static inline void Calculate(AccDataType& accuVal, AccDataType currVal)
43
44
45
46
47
    {
        ReduceOperation{}(accuVal, currVal);
    };
};

48
// Check for NaN; guarantees NaNs be propagated to result
49
50
51
template <typename ReduceOperation, typename AccDataType>
struct AccumulateWithNanCheck<true, ReduceOperation, AccDataType>
{
52
    __host__ __device__ static inline void Calculate(AccDataType& accuVal, AccDataType currVal)
53
    {
54
55
56
        using ck::math::isnan;

        if(isnan(currVal))
57
58
59
60
61
62
63
        {
            accuVal = currVal;
        }
        else
        {
            ReduceOperation{}(accuVal, currVal);
        };
64
    };
65
66
67
68
};

template <bool PropagateNan, typename ReduceOperation, typename AccDataType, typename IndexDataType>
struct AccumulateWithIndexAndNanCheck;
69

70
71
72
template <typename ReduceOperation, typename AccDataType, typename IndexDataType>
struct AccumulateWithIndexAndNanCheck<false, ReduceOperation, AccDataType, IndexDataType>
{
73
    __host__ __device__ static inline void
74
    // cppcheck-suppress constParameter
75
76
77
78
    Calculate(AccDataType& accuVal,
              AccDataType currVal,
              IndexDataType& accuIndex,
              IndexDataType currIndex)
79
80
81
    {
        bool changed = false;

82
        ReduceOperation{}(accuVal, currVal, changed);
83
84
85
86
87
88

        if(changed)
            accuIndex = currIndex;
    };
};

89
90
template <typename ReduceOperation, typename AccDataType, typename IndexDataType>
struct AccumulateWithIndexAndNanCheck<true, ReduceOperation, AccDataType, IndexDataType>
91
{
92
    // The method is called when the ReduceOperation is indexable and the user asked for indices
93
94
95
96
    __host__ __device__ static inline void Calculate(AccDataType& accuVal,
                                                     AccDataType currVal,
                                                     IndexDataType& accuIndex,
                                                     IndexDataType currIndex)
97
    {
98
99
100
        using ck::math::isnan;

        if(isnan(currVal))
101
102
103
104
105
106
107
108
        {
            accuVal   = currVal;
            accuIndex = currIndex;
        }
        else
        {
            bool changed = false;

109
            ReduceOperation{}(accuVal, currVal, changed);
110
111
112
113
114
115
116

            if(changed)
                accuIndex = currIndex;
        }
    };
};

Chao Liu's avatar
Chao Liu committed
117
118
} // namespace detail
} // namespace ck
Umang Yadav's avatar
Umang Yadav committed
119
120

#pragma clang diagnostic pop