lapack_function.cc 5.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//   Copyright (c) 2021 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.

#include "paddle/fluid/operators/math/lapack_function.h"
16
#include "paddle/fluid/platform/complex.h"
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#include "paddle/fluid/platform/dynload/lapack.h"

namespace paddle {
namespace operators {
namespace math {

// LU (for example)
template <>
void lapackLu<double>(int m, int n, double *a, int lda, int *ipiv, int *info) {
  platform::dynload::dgetrf_(&m, &n, a, &lda, ipiv, info);
}

template <>
void lapackLu<float>(int m, int n, float *a, int lda, int *ipiv, int *info) {
  platform::dynload::sgetrf_(&m, &n, a, &lda, ipiv, info);
}

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
// eigh
template <>
void lapackEigh<float>(char jobz, char uplo, int n, float *a, int lda, float *w,
                       float *work, int lwork, float *rwork, int lrwork,
                       int *iwork, int liwork, int *info) {
  (void)rwork;   // unused
  (void)lrwork;  // unused
  platform::dynload::ssyevd_(&jobz, &uplo, &n, a, &lda, w, work, &lwork, iwork,
                             &liwork, info);
}

template <>
void lapackEigh<double>(char jobz, char uplo, int n, double *a, int lda,
                        double *w, double *work, int lwork, double *rwork,
                        int lrwork, int *iwork, int liwork, int *info) {
  (void)rwork;   // unused
  (void)lrwork;  // unused
  platform::dynload::dsyevd_(&jobz, &uplo, &n, a, &lda, w, work, &lwork, iwork,
                             &liwork, info);
}

template <>
void lapackEigh<platform::complex<float>, float>(
    char jobz, char uplo, int n, platform::complex<float> *a, int lda, float *w,
    platform::complex<float> *work, int lwork, float *rwork, int lrwork,
    int *iwork, int liwork, int *info) {
  platform::dynload::cheevd_(&jobz, &uplo, &n,
                             reinterpret_cast<std::complex<float> *>(a), &lda,
                             w, reinterpret_cast<std::complex<float> *>(work),
                             &lwork, rwork, &lrwork, iwork, &liwork, info);
}

template <>
void lapackEigh<platform::complex<double>, double>(
    char jobz, char uplo, int n, platform::complex<double> *a, int lda,
    double *w, platform::complex<double> *work, int lwork, double *rwork,
    int lrwork, int *iwork, int liwork, int *info) {
  platform::dynload::zheevd_(&jobz, &uplo, &n,
                             reinterpret_cast<std::complex<double> *>(a), &lda,
                             w, reinterpret_cast<std::complex<double> *>(work),
                             &lwork, rwork, &lrwork, iwork, &liwork, info);
}

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
// Eig
template <>
void lapackEig<double>(char jobvl, char jobvr, int n, double *a, int lda,
                       double *w, double *vl, int ldvl, double *vr, int ldvr,
                       double *work, int lwork, double *rwork, int *info) {
  double *wr = w;
  double *wi = w + n;
  (void)rwork;  // unused
  platform::dynload::dgeev_(&jobvl, &jobvr, &n, a, &lda, wr, wi, vl, &ldvl, vr,
                            &ldvr, work, &lwork, info);
}

template <>
void lapackEig<float>(char jobvl, char jobvr, int n, float *a, int lda,
                      float *w, float *vl, int ldvl, float *vr, int ldvr,
                      float *work, int lwork, float *rwork, int *info) {
  float *wr = w;
  float *wi = w + n;
  (void)rwork;  // unused
  platform::dynload::sgeev_(&jobvl, &jobvr, &n, a, &lda, wr, wi, vl, &ldvl, vr,
                            &ldvr, work, &lwork, info);
}

template <>
void lapackEig<platform::complex<double>, double>(
    char jobvl, char jobvr, int n, platform::complex<double> *a, int lda,
    platform::complex<double> *w, platform::complex<double> *vl, int ldvl,
    platform::complex<double> *vr, int ldvr, platform::complex<double> *work,
    int lwork, double *rwork, int *info) {
  platform::dynload::zgeev_(
      &jobvl, &jobvr, &n, reinterpret_cast<std::complex<double> *>(a), &lda,
      reinterpret_cast<std::complex<double> *>(w),
      reinterpret_cast<std::complex<double> *>(vl), &ldvl,
      reinterpret_cast<std::complex<double> *>(vr), &ldvr,
      reinterpret_cast<std::complex<double> *>(work), &lwork, rwork, info);
}

template <>
void lapackEig<platform::complex<float>, float>(
    char jobvl, char jobvr, int n, platform::complex<float> *a, int lda,
    platform::complex<float> *w, platform::complex<float> *vl, int ldvl,
    platform::complex<float> *vr, int ldvr, platform::complex<float> *work,
    int lwork, float *rwork, int *info) {
  platform::dynload::cgeev_(
      &jobvl, &jobvr, &n, reinterpret_cast<std::complex<float> *>(a), &lda,
      reinterpret_cast<std::complex<float> *>(w),
      reinterpret_cast<std::complex<float> *>(vl), &ldvl,
      reinterpret_cast<std::complex<float> *>(vr), &ldvr,
      reinterpret_cast<std::complex<float> *>(work), &lwork, rwork, info);
}

128 129 130
}  // namespace math
}  // namespace operators
}  // namespace paddle