summaryrefslogtreecommitdiff
blob: 907f7112570da1321f05eb478a3c3b9d14074320 (plain)
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
//=====================================================
// Copyright (C) 2012 Andrea Arteaga <andyspiros@gmail.com>
//=====================================================
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//

/* Check whether we are included by NumeriInterface.hpp or not */
#ifndef NI_INCLUDE_OK
#  error CInterface.hpp should never be included directly: include NumericInterface.hpp instead!
#endif

#define CBLASFUNC(F) CAT(cblas_, CAT(NI_SCALARPREFIX, F))
#define LAPACKEFUNC(F) CAT(LAPACKE_, CAT(NI_SCALARPREFIX, F))

#ifndef NI_NAME
#  define NI_NAME CAT(CAT(CInterface,$),NI_SCALAR)
#endif


template<>
class NumericInterface<NI_SCALAR>
{
public:
    typedef NI_SCALAR Scalar;

public:
    static std::string name()
    {
        std::string name = MAKE_STRING(NI_NAME);
        return name;
    }



    /****************
     * LEVEL 1 BLAS *
     ****************/

    static void rot(const int& N, Scalar* x, Scalar* y,
                    const Scalar& cosine, const Scalar& sine)
    {
        CBLASFUNC(rot)(N, x, 1, y, 1, cosine, sine);
    }


    static void axpy(const int& N, const Scalar& alpha,
                     const Scalar* x, Scalar* y)
    {
        CBLASFUNC(axpy)(N, alpha, x, 1, y, 1);
    }

    static Scalar dot(const int& N, const Scalar* x, const Scalar* y)
    {
        return CBLASFUNC(dot)(N, x, 1, y, 1);
    }

    static Scalar norm(const int& N, const Scalar* x)
    {
        return CBLASFUNC(nrm2)(N, x, 1);
    }



    /****************
     * LEVEL 2 BLAS *
     ****************/

    static void MatrixVector(const bool& trans, const int& M, const int& N,
            const Scalar& alpha, const Scalar* A, const Scalar* x,
            const Scalar& beta, Scalar* y)
    {
        const int LDA = trans ? N : M;
        const int tA = trans ? CblasTrans : CblasNoTrans;
        CBLASFUNC(gemv)(CblasColMajor, tA, M, N, alpha, A, LDA,
                        x, 1, beta, y, 1);
    }

    static void SymMatrixVector(const char& uplo, const int& N,
            const Scalar& alpha, const Scalar* A, const Scalar* x,
            const Scalar& beta, Scalar* y)
    {
        int uplo_ = -1;
        if (uplo == 'u' || uplo == 'U')
            uplo_ = CblasUpper;
        else if (uplo == 'l' || uplo == 'L')
            uplo_ = CblasLower;

        CBLASFUNC(symv)(CblasColMajor, uplo_, N, alpha, A, N, x, 1, beta, y, 1);
    }

    static void TriMatrixVector(const char& uplo, const int& N,
            const Scalar* A, Scalar* x)
    {
        int uplo_ = -1;
        if (uplo == 'u' || uplo == 'U')
            uplo_ = CblasUpper;
        else if (uplo == 'l' || uplo == 'L')
            uplo_ = CblasLower;

        CBLASFUNC(trmv)(CblasColMajor, uplo_, CblasNoTrans, CblasNonUnit, N,
                        A, N, x, 1);
    }

    static void TriSolveVector(const char& uplo,
            const int& N, const Scalar* A, Scalar* x)
    {
        int uplo_ = -1;
        if (uplo == 'u' || uplo == 'U')
            uplo_ = CblasUpper;
        else if (uplo == 'l' || uplo == 'L')
            uplo_ = CblasLower;

        CBLASFUNC(trsv)(CblasColMajor, uplo_, CblasNoTrans, CblasNonUnit, N,
                A, N, x, 1);
    }

    static void Rank1Update(const int& M, const int& N, const Scalar& alpha,
            const Scalar* x, const Scalar* y, Scalar* A)
    {
        CBLASFUNC(ger)(CblasColMajor, M, N, alpha, x, 1, y, 1, A, M);
    }

    static void Rank2Update(const char& uplo, const int& N, const Scalar& alpha,
            const Scalar* x, const Scalar* y, Scalar* A)
    {
        int uplo_ = -1;
        if (uplo == 'u' || uplo == 'U')
            uplo_ = CblasUpper;
        else if (uplo == 'l' || uplo == 'L')
            uplo_ = CblasLower;

        CBLASFUNC(syr2)(CblasColMajor, uplo_, N, alpha, x, 1, y, 1, A, N);
    }



    /****************
     * LEVEL 3 BLAS *
     ****************/

    static void MatrixMatrix(const bool& transA, const bool& transB,
            const int& M, const int& N, const int& K,
            const Scalar& alpha, const Scalar* A, const Scalar* B,
            const Scalar& beta, Scalar* C)
    {
        int LDA = M, LDB = K;
        int tA = CblasNoTrans, tB = CblasNoTrans;

        if (transA) {
            LDA = K;
            tA = CblasTrans;
        }
        if (transB) {
            LDB = N;
            tB = CblasTrans;
        }

        CBLASFUNC(gemm)(CblasColMajor, tA, tB, M, N, K, alpha, A, LDA, B, LDB,
                        beta, C, M);
    }

    static void TriMatrixMatrix(const char& uplo,
            const int& M, const int& N, const Scalar* A, Scalar* B)
    {
        int uplo_ = -1;
        if (uplo == 'u' || uplo == 'U')
            uplo_ = CblasUpper;
        else if (uplo == 'l' || uplo == 'L')
            uplo_ = CblasLower;

        CBLASFUNC(trmm)(CblasColMajor, CblasLeft, uplo_, CblasNoTrans,
                        CblasNonUnit, M, N, 1., A, M, B, M);
    }

    static void TriSolveMatrix(const char& uplo,
            const int& M, const int& N, const Scalar* A, Scalar *B)
    {
        int uplo_ = -1;
        if (uplo == 'u' || uplo == 'U')
            uplo_ = CblasUpper;
        else if (uplo == 'l' || uplo == 'L')
            uplo_ = CblasLower;

        CBLASFUNC(trsm)(CblasColMajor, CblasLeft, uplo_, CblasNoTrans,
                CblasNonUnit, M, N, 1., A, M, B, M);
    }



    /*******************
     * LAPACKE SOLVERS *
     *******************/

    static void GeneralSolve(const int& N, Scalar *A, Scalar *b, int *ipiv)
    {
        LAPACKEFUNC(gesv)(CblasColMajor, N, 1, A, N, ipiv, b, N);
    }

    static void LeastSquaresSolve(const int& N, Scalar *A, Scalar *b)
    {
        LAPACKEFUNC(gels)(CblasColMajor, 'N', N, N, 1, A, N, b, N);
    }



    /**************************
     * LAPACKE decompositions *
     **************************/

    static void LUdecomp(const int& N, Scalar* A, int* ipiv)
    {
        LAPACKEFUNC(getrf)(CblasColMajor, N, N, A, N, ipiv);
    }

    static void Choleskydecomp(const char& uplo, const int& N, Scalar* A)
    {
        LAPACKEFUNC(potrf)(CblasColMajor, uplo, N, A, N);
    }

    static void QRdecomp(const int& N, Scalar* A, int* jpiv, Scalar* tau)
    {
        LAPACKEFUNC(geqp3)(CblasColMajor, N, N, A, N, jpiv, tau);
    }
};