Xmipp  v3.23.11-Nereus
correlation_computer.cpp
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 #include "correlation_computer.h"
27 
28 template<typename T>
29 template<bool NORMALIZE>
31  const auto &s = this->getSettings();
32  const size_t n = s.otherDims.n();
33  const size_t z = s.otherDims.z();
34  const size_t y = s.otherDims.y();
35  const size_t x = s.otherDims.x();
36 
37  auto &res = this->getFiguresOfMerit();
38  res.resize(n); // allocate all positions, so that we can access it our of order
39  auto workload = [&](int id, size_t signalId){
40  auto ref = MultidimArray<T>(1, z, y, x, const_cast<T*>(m_ref)); // removing const, but data should not be changed
41  T * address = others + signalId * s.otherDims.sizeSingle();
42  auto other = MultidimArray<T>(1, z, y, x, address);
43  if (NORMALIZE) {
44  res.at(signalId) = correlationIndex(ref, other);
45  } else {
46  res.at(signalId) = fastCorrelation(ref, other);
47  }
48  };
49 
50  auto futures = std::vector<std::future<void>>();
51  for (size_t i = 0; i < n; ++i) {
52  futures.emplace_back(m_threadPool.push(workload, i));
53  }
54  for (auto &f : futures) {
55  f.get();
56  }
57 }
58 
59 template<typename T>
61  bool isReady = this->isInitialized() && this->isRefLoaded();
62  if ( ! isReady) {
63  REPORT_ERROR(ERR_LOGIC_ERROR, "Not ready to execute. Call init() and load reference");
64  }
65  const auto &s = this->getSettings();
66  switch(s.type) {
67  case MeritType::OneToN: {
68  if (s.normalizeResult) {
69  computeOneToN<true>(others);
70  } else {
71  computeOneToN<false>(others);
72  }
73  break;
74  }
75  default:
76  REPORT_ERROR(ERR_NOT_IMPLEMENTED, "This case is not implemented");
77  }
78 }
79 
80 template<typename T>
82  m_ref = ref; // just copy pointer
83  this->setIsRefLoaded(nullptr != ref);
84 }
85 
86 template<typename T>
87 void CorrelationComputer<T>::initialize(bool doAllocation) {
88  const auto &s = this->getSettings();
89  release();
90 
91  for (auto &hw : s.hw) {
92  if ( ! dynamic_cast<CPU*>(hw)) {
93  REPORT_ERROR(ERR_LOGIC_ERROR, "Instance of CPU is expected");
94  }
95  }
96 // m_threadPool.resize(s.hw.size()); // FIXME DS set to requested number of thread
97  m_threadPool.resize(CPU::findCores());
98 }
99 
100 template<typename T>
102  setDefault();
103 }
104 
105 template<typename T>
107  m_ref = nullptr;
108  m_threadPool.resize(0);
109 }
110 
111 template<typename T>
113  bool result = true;
114  if ( ! this->isInitialized()) {
115  return false;
116  }
117  auto &sOrig = this->getSettings();
118  result = result && sOrig.type == s.type;
119  result = result && (sOrig.otherDims.size() >= s.otherDims.size()); // previously, we needed more space
120 
121  return result;
122 }
123 
124 template<typename T>
126  // so far nothing to do
127 }
128 
129 // explicit instantiation
130 template class CorrelationComputer<float>;
131 template class CorrelationComputer<double>;
Case or algorithm not implemented yet.
Definition: xmipp_error.h:177
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
static double * y
double correlationIndex(const MultidimArray< T > &x, const MultidimArray< T > &y, const MultidimArray< int > *mask=NULL, MultidimArray< double > *Contributions=NULL)
char * address
static unsigned findCores()
Definition: cpu.h:41
doublereal * x
#define i
MeritType type
double * f
void compute(T *others) override
double z
Dimensions otherDims
T fastCorrelation(const MultidimArray< T > &x, const MultidimArray< T > &y)
Definition: filters.h:328
constexpr size_t size() const
Definition: dimensions.h:104
void loadReference(const T *ref) override
int * n
Some logical error in the pipeline.
Definition: xmipp_error.h:147