fml  0.1-0
Fused Matrix Library
vecops.hh
1 // This file is part of fml which is released under the Boost Software
2 // License, Version 1.0. See accompanying file LICENSE or copy at
3 // https://www.boost.org/LICENSE_1_0.txt
4 
5 #ifndef FML_CPU_INTERNALS_VECOPS_H
6 #define FML_CPU_INTERNALS_VECOPS_H
7 #pragma once
8 
9 
10 #include <cmath>
11 
12 #include "../../_internals/types.hh"
13 
14 
15 namespace fml
16 {
17  namespace vecops
18  {
19  namespace cpu
20  {
21  template <typename REAL>
22  static inline void sum(const len_t len, const REAL *x, REAL &s)
23  {
24  s = 0;
25 
26  #pragma omp simd reduction(+:s)
27  for (len_t i=0; i<len; i++)
28  s += x[i];
29  }
30 
31 
32 
33  template <typename REAL>
34  static inline void sweep_add(const REAL c, const len_t len, REAL *x)
35  {
36  #pragma omp for simd
37  for (len_t i=0; i<len; i++)
38  x[i] += c;
39  }
40 
41 
42 
43  template <typename REAL>
44  static inline void sweep_mul(const REAL c, const len_t len, REAL *x)
45  {
46  #pragma omp for simd
47  for (len_t i=0; i<len; i++)
48  x[i] *= c;
49  }
50 
51 
52 
53  template <typename REAL>
54  static inline void var(const len_t n, const REAL *x, REAL &mean, REAL &var)
55  {
56  mean = 0;
57  var = 0;
58 
59  for (len_t i=0; i<n; i++)
60  {
61  REAL dt = x[i] - mean;
62  mean += dt/((REAL) i+1);
63  var += dt * (x[i] - mean);
64  }
65 
66  var = sqrt(var / ((REAL) n-1));
67  }
68  }
69  }
70 }
71 
72 
73 #endif
fml
Core namespace.
Definition: dimops.hh:10