Debian Codegraph @

tools/physicalModeling/mesh2faust/vega/libraries/matrix/matrixBLASOptimized.cpp

raw symbols SCIP
1 /*************************************************************************
2 * *
3 * Vega FEM Simulation Library Version 3.1 *
4 * *
5 * "matrix" library , Copyright (C) 2007 CMU, 2009 MIT *
6 * All rights reserved. *
7 * *
8 * Code author: Jernej Barbic *
9 * http://www.jernejbarbic.com/code *
10 * Research: Jernej Barbic, Doug L. James, Jovan Popovic *
11 * Funding: NSF, Link Foundation, Singapore-MIT GAMBIT Game Lab *
12 * *
13 * This library is free software; you can redistribute it and/or *
14 * modify it under the terms of the BSD-style license that is *
15 * included with this library in the file LICENSE.txt *
16 * *
17 * This library is distributed in the hope that it will be useful, *
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file *
20 * LICENSE.TXT for more details. *
21 * *
22 *************************************************************************/
23
24 #include "matrixMacros.h"
25 #include "matrixBLAS.h"
26 #include "lapack-headers.h"
27
28 //void cblas_scopy(const int N, const float *X, const int incX, float *Y, const int incY);
29 template<bool C>
30 class _xcopy {};
31
32 template<>
33 class _xcopy<true>
34 {
35 public:
36 inline static void f(const int N, float * X, const int incX, float * Y, const int incY)
37 {
38 cblas_scopy(N,X,incX,Y,incY);
39 }
40 };
41
42 template<>
43 class _xcopy<false>
44 {
45 public:
46 inline static void f(const int N, double * X, const int incX, double * Y, const int incY)
47 {
48 cblas_dcopy(N,X,incX,Y,incY);
49 }
50 };
51
52 //void cblas_saxpy(const int N, const float alpha, const float *X, const int incX, float *Y, const int incY);
53 template<bool C>
54 class _xaxpy {};
55
56 template<>
57 class _xaxpy<true>
58 {
59 public:
60 inline static void f(const int N, float alpha, const float * X, const int incX, float * Y, const int incY)
61 {
62 cblas_saxpy(N,alpha,X,incX,Y,incY);
63 }
64 };
65
66 template<>
67 class _xaxpy<false>
68 {
69 public:
70 inline static void f(const int N, double alpha, const double * X, const int incX, double * Y, const int incY)
71 {
72 cblas_daxpy(N,alpha,X,incX,Y,incY);
73 }
74 };
75
76 //void cblas_sscal(const int N, const float alpha, float *X, const int incX);
77 template<bool C>
78 class _xscal {};
79
80 template<>
81 class _xscal<true>
82 {
83 public:
84 inline static void f(const int N, const float alpha, float * X, const int incX)
85 {
86 cblas_sscal(N,alpha,X,incX);
87 }
88 };
89
90 template<>
91 class _xscal<false>
92 {
93 public:
94 inline static void f(const int N, const double alpha, double * X, const int incX)
95 {
96 cblas_dscal(N,alpha,X,incX);
97 }
98 };
99
100 //void cblas_snrm2(const int N, float * X, const int incX);
101 template<bool C>
102 class _xnrm2 {};
103
104 template<>
105 class _xnrm2<true>
106 {
107 public:
108 inline static float f(const int N, const float * X, const int incX)
109 {
110 return cblas_snrm2(N,X,incX);
111 }
112 };
113
114 template<>
115 class _xnrm2<false>
116 {
117 public:
118 inline static double f(const int N, const double * X, const int incX)
119 {
120 return cblas_dnrm2(N,X,incX);
121 }
122 };
123
124 //void cblas_sgemm(const enum CBLAS_ORDER Order, const enum CBLAS_TRANSPOSE TransA, const enum CBLAS_TRANSPOSE TransB, const int M, const int N, const int K, const float alpha, const float *A, const int lda, const float *B, const int ldb, const float beta, float *C, const int ldc);
125 template<bool C>
126 class _xgemm {};
127
128 template<>
129 class _xgemm<true>
130 {
131 public:
132 inline static void f(const CBLAS_ORDER Order, const CBLAS_TRANSPOSE TransA, const CBLAS_TRANSPOSE TransB, const int M, const int N, const int K, const float alpha, const float *A, const int lda, const float *B, const int ldb, const float beta, float *C, const int ldc)
133 {
134 cblas_sgemm(Order, TransA, TransB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
135 }
136 };
137
138 template<>
139 class _xgemm<false>
140 {
141 public:
142 inline static void f(const CBLAS_ORDER Order, const CBLAS_TRANSPOSE TransA, const CBLAS_TRANSPOSE TransB, const int M, const int N, const int K, const double alpha, const double *A, const int lda, const double *B, const int ldb, const double beta, double *C, const int ldc)
143 {
144 cblas_dgemm(Order, TransA, TransB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
145 }
146 };
147
148 template<class real>
149 real * SumMatrices(int m, int n, real * mtx1, real * mtx2, real * output)
150 {
151 real * target = output;
152 if (target == NULL)
153 target = (real*) malloc (sizeof(real) * m * n);
154
155 _xcopy<sizeof(real)==sizeof(float)>::f(m*n, mtx1, 1, target, 1);
156 _xaxpy<sizeof(real)==sizeof(float)>::f(m*n, 1.0, mtx2, 1, target, 1);
157
158 /*
159 for(int i=0; i<m*n; i++)
160 target[i] = mtx1[i] + mtx2[i];
161 */
162 return target;
163 }
164
165 template<class real>
166 real * SubtractMatrices(int m, int n, real * mtx1, real * mtx2, real * output)
167 {
168 real * target = output;
169 if (target == NULL)
170 target = (real*) malloc (sizeof(real) * m * n);
171
172 _xcopy<sizeof(real)==sizeof(float)>::f(m*n, mtx1, 1, target, 1);
173 _xaxpy<sizeof(real)==sizeof(float)>::f(m*n, -1.0, mtx2, 1, target, 1);
174
175 /*
176 for(int i=0; i<m*n; i++)
177 target[i] = mtx1[i] - mtx2[i];
178 */
179 return target;
180 }
181
182 template<class real>
183 real * MultiplyMatrices(int m, int p, int n, real * mtx1, real * mtx2, real * output)
184 {
185 real * target = output;
186 if (target == NULL)
187 target = (real*) malloc (sizeof(real) * m * n);
188
189 _xgemm<sizeof(real)==sizeof(float)>::f(CblasColMajor, CblasNoTrans, CblasNoTrans, m, n, p, 1.0, mtx1, m, mtx2, p, 0.0, target, m);
190
191 /*
192 for(int i=0; i<m; i++)
193 for(int j=0; j<n; j++)
194 {
195 real entry = 0;
196 for(int k=0; k<p; k++)
197 entry += mtx1[ELT(m,i,k)] * mtx2[ELT(p, k, j)];
198 target[ELT(m, i, j)] = entry;
199 }
200 */
201 return target;
202 }
203
204 template<class real>
205 real * MultiplyMatricesT(int m, int p, int n, real * mtx1, real * mtx2, real * output)
206 {
207 real * target = output;
208 if (target == NULL)
209 target = (real*) malloc (sizeof(real) * n * m);
210
211 _xgemm<sizeof(real)==sizeof(float)>::f(CblasColMajor, CblasTrans, CblasNoTrans, m, n, p, 1.0, mtx1, p, mtx2, p, 0.0, target, m);
212
213 /*
214 for(int i=0; i<m; i++)
215 for(int j=0; j<n; j++)
216 {
217 real entry = 0;
218 for(int k=0; k<p; k++)
219 entry += mtx1[ELT(p, k, i)] * mtx2[ELT(p, k, j)];
220 target[ELT(m, i, j)] = entry;
221 }
222 */
223 return target;
224 }
225
226 template<class real>
227 real * ScalarMultiplyMatrix(int m, int n, real alpha, real * mtx, real * output)
228 {
229 real * target = output;
230 if (target == NULL)
231 target = (real*) malloc (sizeof(real) * m * n);
232
233 _xcopy<sizeof(real)==sizeof(float)>::f(m*n, mtx, 1, target, 1);
234 _xscal<sizeof(real)==sizeof(float)>::f(m*n, alpha, target, 1);
235
236 /*
237 for(int i=0; i<m*n; i++)
238 target[i] = alpha * mtx[i];
239 */
240
241 return target;
242 }
243
244 // computes Euclidean norm of a vector
245 template <class real>
246 real VectorNorm(int m, real * vec)
247 {
248 return _xnrm2<sizeof(real)==sizeof(float)>::f(m, vec, 1);
249 }
250