BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
anonymous_namespace{Simulation.cpp} Namespace Reference

Functions

bool detHasSameDimensions (const IDetector &detector, const OutputData< double > &data)
 
size_t getIndexStep (size_t total_size, size_t n_handlers)
 
size_t getStartIndex (size_t n_handlers, size_t current_handler, size_t n_elements)
 
size_t getNumberOfElements (size_t n_handlers, size_t current_handler, size_t n_elements)
 
void runComputations (std::vector< std::unique_ptr< IComputation >> computations)
 

Function Documentation

◆ detHasSameDimensions()

bool anonymous_namespace{Simulation.cpp}::detHasSameDimensions ( const IDetector detector,
const OutputData< double > &  data 
)

Definition at line 33 of file Simulation.cpp.

34 {
35  if (data.getRank() != detector.dimension())
36  return false;
37 
38  for (size_t i = 0; i < detector.dimension(); ++i)
39  if (data.getAxis(i).size() != detector.getAxis(i).size())
40  return false;
41 
42  return true;
43 }
virtual size_t size() const =0
retrieve the number of bins
size_t dimension() const
Returns actual dimensionality of the detector (number of defined axes)
Definition: IDetector.cpp:44
const IAxis & getAxis(size_t index) const
Definition: IDetector.cpp:54
size_t getRank() const
Returns number of dimensions.
Definition: OutputData.h:59
const IAxis & getAxis(size_t serial_number) const
returns axis with given serial number
Definition: OutputData.h:314

References IDetector::dimension(), IDetector::getAxis(), OutputData< T >::getAxis(), OutputData< T >::getRank(), and IAxis::size().

Referenced by Simulation::convertData().

Here is the call graph for this function:

◆ getIndexStep()

size_t anonymous_namespace{Simulation.cpp}::getIndexStep ( size_t  total_size,
size_t  n_handlers 
)

Definition at line 45 of file Simulation.cpp.

46 {
47  ASSERT(total_size > 0);
48  ASSERT(n_handlers > 0);
49  size_t result = total_size / n_handlers;
50  return total_size % n_handlers ? ++result : result;
51 }
#define ASSERT(condition)
Definition: Assert.h:26

References ASSERT.

Referenced by getNumberOfElements(), and getStartIndex().

◆ getStartIndex()

size_t anonymous_namespace{Simulation.cpp}::getStartIndex ( size_t  n_handlers,
size_t  current_handler,
size_t  n_elements 
)

Definition at line 53 of file Simulation.cpp.

54 {
55  const size_t handler_size = getIndexStep(n_elements, static_cast<size_t>(n_handlers));
56  const size_t start_index = current_handler * handler_size;
57  if (start_index >= n_elements)
58  return n_elements;
59  return start_index;
60 }
size_t getIndexStep(size_t total_size, size_t n_handlers)
Definition: Simulation.cpp:45

References getIndexStep().

Referenced by Simulation::runSimulation(), and Simulation::runSingleSimulation().

Here is the call graph for this function:

◆ getNumberOfElements()

size_t anonymous_namespace{Simulation.cpp}::getNumberOfElements ( size_t  n_handlers,
size_t  current_handler,
size_t  n_elements 
)

Definition at line 62 of file Simulation.cpp.

63 {
64  const size_t handler_size = getIndexStep(n_elements, static_cast<size_t>(n_handlers));
65  const size_t start_index = current_handler * handler_size;
66  if (start_index >= n_elements)
67  return 0;
68  return std::min(handler_size, n_elements - start_index);
69 }

References getIndexStep().

Referenced by Simulation::runSimulation(), and Simulation::runSingleSimulation().

Here is the call graph for this function:

◆ runComputations()

void anonymous_namespace{Simulation.cpp}::runComputations ( std::vector< std::unique_ptr< IComputation >>  computations)

Definition at line 71 of file Simulation.cpp.

72 {
73  ASSERT(!computations.empty());
74 
75  if (computations.size() == 1) { // Running computation in current thread
76  auto& computation = computations.front();
77  computation->run();
78  if (computation->isCompleted())
79  return;
80  std::string message = computation->errorMessage();
81  throw Exceptions::RuntimeErrorException("Error in runComputations: Simulation has "
82  "terminated unexpectedly with following error: "
83  "message.\n"
84  + message);
85  }
86 
87  // Running computations in several threads.
88  // The number of threads is equal to the number of computations.
89 
90  std::vector<std::unique_ptr<std::thread>> threads;
91 
92  // Run simulations in n threads.
93  for (auto& comp : computations)
94  threads.emplace_back(new std::thread([&comp]() { comp->run(); }));
95 
96  // Wait for threads to complete.
97  for (auto& thread : threads)
98  thread->join();
99 
100  // Check successful completion.
101  std::vector<std::string> failure_messages;
102  for (auto& comp : computations)
103  if (!comp->isCompleted())
104  failure_messages.push_back(comp->errorMessage());
105 
106  if (failure_messages.empty())
107  return;
109  "Error in runComputations: "
110  "At least one simulation thread has terminated unexpectedly.\n"
111  "Messages: "
112  + StringUtils::join(failure_messages, " --- "));
113 }
std::string join(const std::vector< std::string > &joinable, const std::string &joint)
Returns string obtain by joining vector elements.
Definition: StringUtils.cpp:71

References ASSERT, and StringUtils::join().

Referenced by Simulation::runSingleSimulation().

Here is the call graph for this function: