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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
/* ************************************************************************
* Copyright (C) 2016-2024 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 <hipblas/hipblas.h>
#include <stdbool.h>
#ifdef __cplusplus
#include "complex.hpp"
#include <cmath>
#include <cstdio>
#include <iostream>
#include <random>
#include <type_traits>
#include <vector>
#endif
#include <stdio.h>
#include <stdlib.h>
#ifdef __cplusplus
// Return true if value is NaN
template <typename T>
inline bool hipblas_isnan(T)
{
return false;
}
inline bool hipblas_isnan(double arg)
{
return std::isnan(arg);
}
inline bool hipblas_isnan(float arg)
{
return std::isnan(arg);
}
inline bool hipblas_isnan(hipblasHalf arg)
{
auto half_data = static_cast<unsigned short>(arg);
return (~(half_data)&0x7c00) == 0 && (half_data & 0x3ff) != 0;
}
inline bool hipblas_isnan(hipblasComplex arg)
{
return std::isnan(arg.real()) || std::isnan(arg.imag());
}
inline bool hipblas_isnan(hipblasDoubleComplex arg)
{
return std::isnan(arg.real()) || std::isnan(arg.imag());
}
inline hipblasHalf float_to_half(float val)
{
#ifdef HIPBLAS_USE_HIP_HALF
return __float2half(val);
#else
uint16_t a = _cvtss_sh(val, 0);
return a;
#endif
}
inline float bfloat16_to_float(hipblasBfloat16 bf)
{
union
{
uint32_t int32;
float fp32;
} u = {uint32_t(bf.data) << 16};
return u.fp32;
}
inline hipblasBfloat16 float_to_bfloat16(float f)
{
hipblasBfloat16 rv;
union
{
float fp32;
uint32_t int32;
} u = {f};
if(~u.int32 & 0x7f800000)
{
u.int32 += 0x7fff + ((u.int32 >> 16) & 1); // Round to nearest, round to even
}
else if(u.int32 & 0xffff)
{
u.int32 |= 0x10000; // Preserve signaling NaN
}
rv.data = uint16_t(u.int32 >> 16);
return rv;
}
inline float half_to_float(hipblasHalf val)
{
#ifdef HIPBLAS_USE_HIP_HALF
return __half2float(val);
#else
return _cvtsh_ss(val);
#endif
}
/* =============================================================================================== */
/* Absolute values */
// template <typename T>
// inline T hipblas_abs(const T& x)
// {
// return x < 0 ? -x : x;
// }
// template <>
// inline double hipblas_abs(const hipblasHalf& x)
// {
// return std::abs(half_to_float(x));
// }
// template <>
// inline double hipblas_abs(const hipblasBfloat16& x)
// {
// return std::abs(bfloat16_to_float(x));
// }
// rocblas_bfloat16 is handled specially
inline hipblasBfloat16 hipblas_abs(hipblasBfloat16 x)
{
x.data &= 0x7fff;
return x;
}
// rocblas_half
inline hipblasHalf hipblas_abs(hipblasHalf x)
{
union
{
hipblasHalf x;
uint16_t data;
} t = {x};
t.data &= 0x7fff;
return t.x;
}
inline double hipblas_abs(const double& x)
{
return x < 0 ? -x : x;
}
inline float hipblas_abs(const float& x)
{
return x < 0 ? -x : x;
}
inline double hipblas_abs(const hipblasComplex& x)
{
return std::abs(reinterpret_cast<const std::complex<float>&>(x));
}
inline double hipblas_abs(const hipblasDoubleComplex& x)
{
return std::abs(reinterpret_cast<const std::complex<double>&>(x));
}
inline int hipblas_abs(const int& x)
{
return x < 0 ? -x : x;
}
inline int64_t hipblas_abs(const int64_t& x)
{
return x < 0 ? -x : x;
}
/* =============================================================================================== */
/* Complex / real helpers. */
template <typename T>
static constexpr bool is_complex = false;
template <>
HIPBLAS_CLANG_STATIC inline constexpr bool is_complex<hipblasComplex> = true;
template <>
HIPBLAS_CLANG_STATIC inline constexpr bool is_complex<hipblasDoubleComplex> = true;
// Get base types from complex types.
template <typename T, typename = void>
struct real_t_impl
{
using type = T;
};
template <typename T>
struct real_t_impl<T, std::enable_if_t<is_complex<T>>>
{
using type = decltype(T{}.real());
};
template <typename T>
struct real_t_impl<std::complex<T>>
{
using type = T;
};
template <typename T>
using real_t = typename real_t_impl<T>::type;
// Conjugate a value. For most types, simply return argument; for
// hipblasComplex and hipblasDoubleComplex, return std::conj(z)
template <typename T, std::enable_if_t<!is_complex<T>, int> = 0>
__device__ __host__ inline T hipblas_conjugate(const T& z)
{
return z;
}
template <typename T, std::enable_if_t<is_complex<T>, int> = 0>
__device__ __host__ inline T hipblas_conjugate(const T& z)
{
return std::conj(z);
}
// hipblasComplex and hipblasDoubleComplex, return real
template <typename T, std::enable_if_t<!is_complex<T>, int> = 0>
__device__ __host__ inline T hipblas_real(const T& z)
{
return z;
}
template <typename T, std::enable_if_t<is_complex<T>, int> = 0>
__device__ __host__ inline T hipblas_real(const T& z)
{
return std::real(z);
}
#endif // __cplusplus
|