/* Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #pragma once #include #include "paddle/fluid/platform/complex128.h" #include "paddle/fluid/platform/complex64.h" #include "paddle/fluid/platform/hostdevice.h" namespace paddle { namespace operators { namespace math { template struct cond { static constexpr bool value = B; using type = T; }; template struct eval_if { using type = typename TrueF::type; }; template struct eval_if { using type = typename FalseF::type; }; template using eval_if_t = typename eval_if::type; template struct select { using type = eval_if_t>; }; template using select_t = typename select::type; template using Real = select_t::value, float>, cond::value, double>, T>; template using Complex = typename std::enable_if::value>::type; // There are no NoComplex cases now, implement later if needed template using NoComplex = typename std::enable_if::value>::type; template struct RealFunctor; template struct RealFunctor>> { public: RealFunctor(const T* input, Real* output, int64_t numel) : input_(input), output_(output), numel_(numel) {} HOSTDEVICE void operator()(int64_t idx) const { output_[idx] = input_[idx].real; } private: const T* input_; Real* output_; int64_t numel_; }; template struct ImagFunctor; template struct ImagFunctor>> { ImagFunctor(const T* input, Real* output, int64_t numel) : input_(input), output_(output), numel_(numel) {} HOSTDEVICE void operator()(int64_t idx) const { output_[idx] = input_[idx].imag; } const T* input_; Real* output_; int64_t numel_; }; template struct RealToComplexFunctor; template struct RealToComplexFunctor>> { RealToComplexFunctor(const Real* input, T* output, int64_t numel) : input_(input), output_(output), numel_(numel) {} HOSTDEVICE void operator()(int64_t idx) const { output_[idx].real = input_[idx]; output_[idx].imag = 0; } const Real* input_; T* output_; int64_t numel_; }; template struct ImagToComplexFunctor; template struct ImagToComplexFunctor>> { ImagToComplexFunctor(const Real* input, T* output, int64_t numel) : input_(input), output_(output), numel_(numel) {} HOSTDEVICE void operator()(int64_t idx) const { output_[idx].real = 0; output_[idx].imag = input_[idx]; } const Real* input_; T* output_; int64_t numel_; }; } // namespace math } // namespace operators } // namespace paddle