Xmipp  v3.23.11-Nereus
amerit_computer.h
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: David Strelak (davidstrelak@gmail.com)
4  *
5  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20  * 02111-1307 USA
21  *
22  * All comments concerning this program package may be sent to the
23  * e-mail address 'xmipp@cnb.csic.es'
24  ***************************************************************************/
25 
26 #ifndef LIBRARIES_RECONSTRUCTION_AMERIT_COMPUTER_H_
27 #define LIBRARIES_RECONSTRUCTION_AMERIT_COMPUTER_H_
28 
29 #include "data/dimensions.h"
30 #include "data/hw.h"
31 #include "core/xmipp_error.h"
32 #include <vector>
33 
37 enum class MeritType {
38  OneToN // we have one reference item and many others to compare
39 };
40 
42 public:
43  std::vector<HW*> hw;
44  Dimensions refDims = Dimensions(0);
45  Dimensions otherDims = Dimensions(0);
48 
49  void check() const {
50  if (0 == hw.size()) {
51  REPORT_ERROR(ERR_VALUE_INCORRECT, "HW contains zero (0) devices");
52  }
53  if ( ! refDims.isValid()) {
54  REPORT_ERROR(ERR_LOGIC_ERROR, "'Ref' dimensions are invalid (contain 0)");
55  }
56  if ( ! otherDims.isValid()) {
57  REPORT_ERROR(ERR_LOGIC_ERROR, "'Other' dimensions are invalid (contain 0)");
58  }
59  if ( ! refDims.equalExceptNPadded(otherDims)) {
60  REPORT_ERROR(ERR_LOGIC_ERROR, "Dimensions of the reference and other signals differ");
61  }
62  if ((MeritType::OneToN == type)
63  && (1 != refDims.n())) {
64  REPORT_ERROR(ERR_LOGIC_ERROR, "More than one reference specified for type 1:N");
65  }
66  }
67 };
68 
69 template<typename T>
71 public:
73  m_isInit(false),
74  m_isRefLoaded(false){};
75  virtual ~AMeritComputer() = default;
76 
77  // no reference on purpose, we store a copy anyway
78  void init(const MeritSettings &s, bool reuse) {
79  s.check();
80  bool skipAllocation = reuse && m_isInit && canBeReused(s);
81  m_settings = s;
82  this->initialize( ! skipAllocation);
83  this->check();
84  m_isInit = true;
85  }
86 
87  inline const MeritSettings &getSettings() const {
88  return m_settings;
89  }
90 
91  virtual void loadReference(const T *ref) = 0;
92 
93  virtual void compute(T *others) = 0;
94 
95  inline const std::vector<float> &getFiguresOfMerit() const {
96  return m_figures;
97  }
98 
99 protected:
100  virtual void check() = 0;
101  virtual bool canBeReused(const MeritSettings &s) const = 0;
102  virtual void initialize(bool allocate) = 0;
103 
104  inline constexpr bool isInitialized() const {
105  return m_isInit;
106  }
107 
108  inline constexpr bool isRefLoaded() const {
109  return m_isRefLoaded;
110  }
111 
112  void setIsRefLoaded(bool status) {
113  m_isRefLoaded = status;
114  }
115 
116  inline std::vector<float> &getFiguresOfMerit() {
117  return m_figures;
118  }
119 
120 private:
121  MeritSettings m_settings;
122  std::vector<float> m_figures;
123 
124  // flags
125  bool m_isInit;
126  bool m_isRefLoaded;
127 };
129 #endif /* LIBRARIES_RECONSTRUCTION_AMERIT_COMPUTER_H_ */
void init(const MeritSettings &s, bool reuse)
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
void check() const
void setIsRefLoaded(bool status)
std::vector< float > & getFiguresOfMerit()
MeritType type
std::vector< HW * > hw
constexpr bool equalExceptNPadded(const Dimensions &b) const
Definition: dimensions.h:124
const std::vector< float > & getFiguresOfMerit() const
constexpr bool isInitialized() const
CUDA_HD constexpr size_t n() const
Definition: dimensions.h:78
constexpr bool isValid() const
Definition: dimensions.h:138
MeritType
const MeritSettings & getSettings() const
constexpr bool isRefLoaded() const
Incorrect value received.
Definition: xmipp_error.h:195
check(nparam, nf, nfsr, &Linfty, nineq, nineqn, neq, neqn, ncsrl, ncsrn, mode, &modem, eps, bgbnd, param)
Some logical error in the pipeline.
Definition: xmipp_error.h:147