TrioCFD 1.9.8
TrioCFD documentation
Loading...
Searching...
No Matches
Simd_MatrixArray_template.h
1/****************************************************************************
2* Copyright (c) 2022, CEA
3* All rights reserved.
4*
5* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
9*
10* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
11* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
12* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
13*
14*****************************************************************************/
15
16#ifndef Simd_MatrixArray_template_included
17#define Simd_MatrixArray_template_included
18
19#include <Simd_template.h>
20#include <FixedVector.h>
21
22// Simd_floatMatrixArray<N,Nlines,Ncolumns> stores N matrices of size Nlines * Ncolumns,
23// arranged for Simd operation (one simd load or store accesses one particular
24// (i,j) element of the matrix for n consecutive matrices).
25// N must be a multiple of the simd vector size
26// It is a fixed size storage that should be used for efficient processing
27// of local data (that fit in the L1 cache)
28template<typename _TYPE_,int N, int Nlines, int Ncolumns>
30{
31public:
32 _TYPE_& operator()(int matrix_index, int line, int col)
33 {
34 assert(line >= 0 && line < Nlines && col >= 0 && col < Ncolumns && matrix_index >= 0 && matrix_index < N);
35 return data_[line][col][matrix_index];
36 }
37
38 const _TYPE_& operator()(int matrix_index, int line, int col) const
39 {
40 assert(line >= 0 && line < Nlines && col >= 0 && col < Ncolumns && matrix_index >= 0 && matrix_index < N);
41 return data_[line][col][matrix_index];
42 }
43
44 Simd_template<_TYPE_> SimdGet(int matrix_index, int line, int col) const
45 {
46 assert(line >= 0 && line < Nlines && col >= 0 && col < Ncolumns && matrix_index >= 0 && matrix_index < N);
47 return SimdGet(data_[line][col] + matrix_index);
48 }
49
50 void SimdPut(int matrix_index, int line, int col, const Simd_template<_TYPE_>& x)
51 {
52 assert(line >= 0 && line < Nlines && col >= 0 && col < Ncolumns && matrix_index >= 0 && matrix_index < N);
53 SimdPut(data_[line][col] + matrix_index, x);
54 }
55
56protected:
57 _TYPE_ data_[Nlines][Ncolumns][N];
58};
59
60template<int N, int Nlines, int Ncolumns> using Simd_floatMatrixArray = Simd_MatrixArray_template<float,N,Nlines,Ncolumns>;
61template<int N, int Nlines, int Ncolumns> using Simd_doubleMatrixArray = Simd_MatrixArray_template<double,N,Nlines,Ncolumns>;
62
63// Computes the inverse of all matrices in the array with simd instructions tab and resu must be properly aligned for simd.
64// Determinant of the matrices must not be zero (undefined behavior or crash...)
65template<typename _TYPE_,int N>
66inline void Simd_Matrix33_inverse_template(const Simd_MatrixArray_template<_TYPE_,N,3,3>& tab, Simd_MatrixArray_template<_TYPE_,N,3,3>& resu)
67{
68 // 51 x vector size operations (count 4 x vector size operations for division)
69 // Runs at 13 Gflops on Nehalem 3Ghz with icc -O
70 const int vsize = Simd_template<_TYPE_>::size();
71 assert(N%vsize == 0);
72 for (int i = 0; i < N; i += vsize)
73 {
74 const Simd_template<_TYPE_> a00 = tab.SimdGet(i,0,0), a01 = tab.SimdGet(i,0,1), a02 = tab.SimdGet(i,0,2);
75 const Simd_template<_TYPE_> a10 = tab.SimdGet(i,1,0), a11 = tab.SimdGet(i,1,1), a12 = tab.SimdGet(i,1,2);
76 const Simd_template<_TYPE_> a20 = tab.SimdGet(i,2,0), a21 = tab.SimdGet(i,2,1), a22 = tab.SimdGet(i,2,2);
77 // calcul de valeurs temporaires pour optimisation
78 const Simd_template<_TYPE_> t4 = a00*a11, t6 = a00*a12, t8 = a01*a10;
79 const Simd_template<_TYPE_> t10 = a02*a10, t12 = a01*a20, t14 = a02*a20;
80 const Simd_template<_TYPE_> t = t4*a22-t6*a21-t8*a22+t10*a21+t12*a12-t14*a11;
81 const Simd_template<_TYPE_> t17 = SimdReciprocalMed(t);
82
83 //calcul de la matrice inverse
84 resu.SimdPut(i,0,0, (a11*a22-a12*a21)*t17);
85 resu.SimdPut(i,0,1, (a02*a21-a01*a22)*t17);
86 resu.SimdPut(i,0,2, (a01*a12-a02*a11)*t17);
87 resu.SimdPut(i,1,0, (a12*a20-a10*a22)*t17);
88 resu.SimdPut(i,1,1, (a00*a22-t14)*t17);
89 resu.SimdPut(i,1,2, (t10-t6)*t17);
90 resu.SimdPut(i,2,0, (a10*a21-a11*a20)*t17);
91 resu.SimdPut(i,2,1, (t12-a00*a21)*t17);
92 resu.SimdPut(i,2,2, (t4-t8)*t17);
93 }
94}
95
96
97#endif /* Simd_MatrixArray_template_included */
const _TYPE_ & operator()(int matrix_index, int line, int col) const
_TYPE_ & operator()(int matrix_index, int line, int col)
Simd_template< _TYPE_ > SimdGet(int matrix_index, int line, int col) const
void SimdPut(int matrix_index, int line, int col, const Simd_template< _TYPE_ > &x)
This class provides a generic access to simd operations on x86, x86 AMD and ARM architectures.
static int size()