// SPDX-License-Identifier: MIT // Copyright (c) 2018-2022, Advanced Micro Devices, Inc. All rights reserved. #pragma once #include "ck/ck.hpp" #include "ck/utility/data_type.hpp" #include "ck/utility/type.hpp" namespace ck { namespace reduce { // Every binary operator used in reduction is represented by a templated functor class. Each functor // class must provide at least // three members: // 1) GetIdentityValue() -- the interface to return the "identity element" for the binary // operator, "identity element" is the unique // element in the algebraic space that doesn't affect the value of other elements // when operated against them, and the concept is similar to zero vector in // vector space // (http://pages.cs.wisc.edu/~matthewb/pages/notes/pdf/linearalgebra/VectorSpaces.pdf). // 2) IsCompatibleInMemoryDataOperation() -- return true if the reduction task corresponding to this // operator can use the InMemoryDataOperation to finalize, or else it return false 3) operator() -- // the first argument of the operator must be both an input & output, and the corresponding variable // usually stores // the accumulated result of many operator() calls; the second argument is only an // input. For indexable binary // operator, the second version of operator() has third argument (which is an // output) to indicate whether the // accumulated value (the first argument) has changed, in which case the recorded // accumulated index also need be // changed. struct Add { template __host__ __device__ static constexpr T GetIdentityValue() { return type_convert(0.0f); }; __host__ __device__ static constexpr bool IsCompatibleInMemoryDataOperation(InMemoryDataOperationEnum operation) { return operation == InMemoryDataOperationEnum::AtomicAdd || operation == InMemoryDataOperationEnum::Set; }; template __host__ __device__ inline constexpr void operator()(T& a, T b) const { static_assert(is_same::value || is_same::value || is_same::value, "The data type is not supported by the Add accumulator!"); a = a + b; } }; struct SquaredAdd { template __host__ __device__ static constexpr T GetIdentityValue() { return type_convert(0.0f); }; __host__ __device__ static constexpr bool IsCompatibleInMemoryDataOperation(InMemoryDataOperationEnum operation) { return operation == InMemoryDataOperationEnum::AtomicAdd || operation == InMemoryDataOperationEnum::Set; }; template __host__ __device__ inline constexpr void operator()(T& a, T b) const { static_assert(is_same::value || is_same::value || is_same::value || is_same::value || is_same::value, "The data type is not supported by the SquaredAdd accumulator!"); a = a + b * b; } }; struct Mul { template __host__ __device__ static constexpr T GetIdentityValue() { return type_convert(1.0f); }; __host__ __device__ static constexpr bool IsCompatibleInMemoryDataOperation(InMemoryDataOperationEnum operation) { return operation == InMemoryDataOperationEnum::Set; }; template __host__ __device__ inline constexpr void operator()(T& a, T b) const { static_assert(is_same::value || is_same::value || is_same::value, "The data type is not supported by the Mul accumulator!"); a = a * b; } }; struct Max { template __host__ __device__ static constexpr T GetIdentityValue() { return NumericLimits::Lowest(); }; __host__ __device__ static constexpr bool IsCompatibleInMemoryDataOperation(InMemoryDataOperationEnum operation) { // ToChange: atomic_max to be added return operation == InMemoryDataOperationEnum::Set; }; template __host__ __device__ inline constexpr void operator()(T& a, T b) const { static_assert(is_same::value || is_same::value || is_same::value || is_same::value || is_same::value, "The data type is not supported by the Max accumulator!"); if(a < b) a = b; } template __host__ __device__ inline constexpr void operator()(T& a, T b, bool& changed) const { static_assert(is_same::value || is_same::value || is_same::value || is_same::value || is_same::value, "The data type is not supported by the Max accumulator!"); if(a < b) { a = b; changed = true; } } }; struct Min { template __host__ __device__ static constexpr T GetIdentityValue() { return NumericLimits::Max(); }; __host__ __device__ static constexpr bool IsCompatibleInMemoryDataOperation(InMemoryDataOperationEnum operation) { // ToChange: atomic_min to be added return operation == InMemoryDataOperationEnum::Set; }; template __host__ __device__ inline constexpr void operator()(T& a, T b) const { static_assert(is_same::value || is_same::value || is_same::value || is_same::value || is_same::value, "The data type is not supported by the Min accumulator!"); if(a > b) a = b; } template __host__ __device__ inline constexpr void operator()(T& a, T b, bool& changed) const { static_assert(is_same::value || is_same::value || is_same::value || is_same::value || is_same::value, "The data type is not supported by the Min accumulator!"); if(a > b) { a = b; changed = true; } } }; struct AMax { template __host__ __device__ static constexpr T GetIdentityValue() { return type_convert(0.0f); }; __host__ __device__ static constexpr bool IsCompatibleInMemoryDataOperation(InMemoryDataOperationEnum operation) { // ToChange: atomic_max to be added return operation == InMemoryDataOperationEnum::Set; }; template __host__ __device__ inline constexpr void operator()(T& a, T b) const { static_assert(is_same::value || is_same::value || is_same::value || is_same::value || is_same::value, "The data type is not supported by the AMax accumulator!"); if(a < b) a = b; } template __host__ __device__ inline constexpr void operator()(T& a, T b, bool& changed) const { static_assert(is_same::value || is_same::value || is_same::value || is_same::value || is_same::value, "The data type is not supported by the AMax accumulator!"); if(a < b) { a = b; changed = true; } } }; template constexpr T GetIdentityValueForInMemoryDataOperation(InMemoryDataOperationEnum operation) { T result = ck::type_convert(0.0f); if(operation == InMemoryDataOperationEnum::AtomicMax) result = ck::NumericLimits::Lowest(); return (result); }; template struct InMemoryDataOperatonSupportedOnDataType { static constexpr bool value = false; }; template struct InMemoryDataOperatonSupportedOnDataType { static constexpr bool value = is_same::value || is_same::value; }; template struct InMemoryDataOperatonSupportedOnDataType { static constexpr bool value = is_same::value || is_same::value; }; template struct InMemoryDataOperatonSupportedOnDataType { static constexpr bool value = is_same::value || is_same::value || is_same::value || is_same::value || is_same::value || is_same::value; }; template struct InMemoryDataOperatonSupportedOnDataType { static constexpr bool value = is_same::value || is_same::value || is_same::value || is_same::value || is_same::value; }; } // namespace reduce } // namespace ck