Nek5000
SEM for Incompressible NS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
tensor.c
Go to the documentation of this file.
1 #include "c99.h"
2 #include "name.h"
3 #include "types.h"
4 
5 #if !defined(USE_CBLAS)
6 
7 #define tensor_dot PREFIXED_NAME(tensor_dot )
8 #define tensor_mtxm PREFIXED_NAME(tensor_mtxm)
9 
10 /* Matrices are always column-major (FORTRAN style) */
11 
12 double tensor_dot(const double *a, const double *b, uint n)
13 {
14  double sum = 0;
15  for(;n;--n) sum += *a++ * *b++;
16  return sum;
17 }
18 
19 # if defined(USE_NAIVE_BLAS)
20 # define tensor_mxv PREFIXED_NAME(tensor_mxv )
21 # define tensor_mtxv PREFIXED_NAME(tensor_mtxv)
22 # define tensor_mxm PREFIXED_NAME(tensor_mxm )
23 
24 /* y = A x */
25 void tensor_mxv(
26  double *restrict y, uint ny,
27  const double *restrict A, const double *restrict x, uint nx)
28 {
29  uint i;
30  for(i=0;i<ny;++i) y[i]=0;
31  for(;nx;--nx) {
32  const double xk = *x++;
33  for(i=0;i<ny;++i) y[i] += (*A++)*xk;
34  }
35 }
36 
37 /* y = A^T x */
38 void tensor_mtxv(
39  double *restrict y, uint ny,
40  const double *restrict A, const double *restrict x, uint nx)
41 {
42  for(;ny;--ny) {
43  const double *restrict xp = x;
44  uint n = nx;
45  double sum = *A++ * *xp++;
46  for(--n;n;--n) sum += *A++ * *xp++;
47  *y++ = sum;
48  }
49 }
50 
51 /* C = A * B */
52 void tensor_mxm(
53  double *restrict C, uint nc,
54  const double *restrict A, uint na, const double *restrict B, uint nb)
55 {
56  uint i,j,k;
57  for(i=0;i<nc*nb;++i) C[i]=0;
58  for(j=0;j<nb;++j,C+=nc) {
59  const double *restrict A_ = A;
60  for(k=0;k<na;++k) {
61  const double b = *B++;
62  for(i=0;i<nc;++i) C[i] += (*A_++) * b;
63  }
64  }
65 }
66 
67 # endif
68 
69 /* C = A^T * B */
71  double *restrict C, uint nc,
72  const double *restrict A, uint na, const double *restrict B, uint nb)
73 {
74  uint i,j;
75  for(j=0;j<nb;++j,B+=na) {
76  const double *restrict A_ = A;
77  for(i=0;i<nc;++i,A_+=na) *C++ = tensor_dot(A_,B,na);
78  }
79 }
80 
81 #endif
82 
#define uint
Definition: types.h:70
static double sum(struct xxt *data, double v, uint n, uint tag)
Definition: xxt.c:400
static void tensor_mxv(double *y, uint ny, const double *A, const double *x, uint nx)
Definition: tensor.h:58
static void tensor_mtxv(double *y, uint ny, const double *A, const double *x, uint nx)
Definition: tensor.h:63
n
Definition: xxt_test.m:73
static void tensor_mxm(double *C, uint nc, const double *A, uint na, const double *B, uint nb)
Definition: tensor.h:53
#define x
ulong A[NUM][SI]
Definition: sort_test.c:17
uint B[NUM][SI]
Definition: sort_test.c:18
#define tensor_dot
Definition: tensor.c:7
#define tensor_mtxm
Definition: tensor.c:8
#define restrict
Definition: c99.h:11
for i
Definition: xxt_test.m:74
establishes some macros to establish naming conventions
static double y[NR *NS *NT *N]
Definition: obbox_test.c:31
const uint nx[3]
Definition: xxt_test.c:41