comparison OrthancStone/Sources/Toolbox/AlignedMatrix.h @ 2068:22a83fb9dd23 deep-learning

added AlignedMatrix and TimerLogger
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 17 May 2023 17:30:52 +0200
parents
children
comparison
equal deleted inserted replaced
2067:20222330cdf6 2068:22a83fb9dd23
1 /**
2 * Stone of Orthanc
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
7 *
8 * This program is free software: you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation, either version 3 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this program. If not, see
20 * <http://www.gnu.org/licenses/>.
21 **/
22
23
24 #pragma once
25
26 #include "SimdIncludes.h"
27
28 #if (ORTHANC_HAS_AVX2 == 1 || ORTHANC_HAS_SSE2 == 1 || ORTHANC_HAS_WASM_SIMD == 1)
29 # define ORTHANC_HAS_MATRIX_PRODUCT_TRANSPOSED_VECTORIZED 1
30 #else
31 # define ORTHANC_HAS_MATRIX_PRODUCT_TRANSPOSED_VECTORIZED 0
32 #endif
33
34
35 #include <boost/noncopyable.hpp>
36 #include <cassert>
37
38 namespace OrthancStone
39 {
40 /**
41 * 2D matrix whose rows are aligned for the largest SIMD
42 * instructions that are available.
43 **/
44 class AlignedMatrix : public boost::noncopyable
45 {
46 private:
47 class ProductTransposedVectorizedContext;
48
49 unsigned int rows_;
50 unsigned int cols_;
51 size_t pitch_;
52 size_t pitchFloatPointer_;
53 float* content_;
54
55 void Setup(unsigned int rows,
56 unsigned int cols);
57
58 public:
59 AlignedMatrix(unsigned int rows,
60 unsigned int cols)
61 {
62 Setup(rows, cols);
63 }
64
65 ~AlignedMatrix();
66
67 unsigned int GetRows() const
68 {
69 return rows_;
70 }
71
72 unsigned int GetColumns() const
73 {
74 return cols_;
75 }
76
77 unsigned int GetPitch() const
78 {
79 return pitch_;
80 }
81
82 float* GetRowPointer(unsigned int row)
83 {
84 assert(row < rows_);
85 return content_ + row * pitchFloatPointer_;
86 }
87
88 const float* GetRowPointer(unsigned int row) const
89 {
90 assert(row < rows_);
91 return content_ + row * pitchFloatPointer_;
92 }
93
94 size_t GetIndex(unsigned int row,
95 unsigned int col) const
96 {
97 assert(row < rows_ && col < cols_);
98 return row * pitchFloatPointer_ + col;
99 }
100
101 float GetValue(unsigned int row,
102 unsigned int col) const
103 {
104 return content_[GetIndex(row, col)];
105 }
106
107 void SetValue(unsigned int row,
108 unsigned int col,
109 float value) const
110 {
111 content_[GetIndex(row, col)] = value;
112 }
113
114 void AddValue(unsigned int row,
115 unsigned int col,
116 float value)
117 {
118 content_[GetIndex(row, col)] += value;
119 }
120
121 void FillZeros();
122
123 // Computes "C = A * B" without SIMD operations
124 static void ProductPlain(AlignedMatrix& c,
125 const AlignedMatrix& a,
126 const AlignedMatrix& b);
127
128 #if ORTHANC_HAS_MATRIX_PRODUCT_TRANSPOSED_VECTORIZED == 1
129 // Computes "C = A * B^T" using SIMD operations
130 static void ProductTransposedVectorized(AlignedMatrix& c,
131 const AlignedMatrix& a,
132 const AlignedMatrix& bt);
133 #endif
134 };
135 }