Direct Graphical Models  v.1.7.0
Graph.cpp
1 #include "Graph.h"
2 #include "macroses.h"
3 
4 namespace DirectGraphicalModels
5 {
6  void CGraph::addNodes(const Mat &pots) {
7  for (int n = 0; n < pots.rows; n++)
8  addNode(pots.row(n).t());
9  }
10 
11  void CGraph::setNodes(size_t start_node, const Mat &pots) {
12  // Assertions
13  DGM_ASSERT_MSG(start_node + pots.rows <= getNumNodes(), "The given ranges exceed the number of nodes(%zu)", getNumNodes());
14 
15 #ifdef ENABLE_PPL
16  int size = pots.rows;
17  int rangeSize = size / (concurrency::GetProcessorCount() * 10);
18  rangeSize = MAX(1, rangeSize);
19  //printf("Processors: %d\n", concurrency::GetProcessorCount());
20  concurrency::parallel_for(0, size, rangeSize, [start_node, size, rangeSize, &pots, this](int i) {
21  for (int j = 0; (j < rangeSize) && (i + j < size); j++)
22  setNode(start_node + i + j, pots.row(i + j).t());
23  });
24 #else
25  for (int n = 0; n < pots.rows; n++)
26  setNode(start_node + n, pots.row(n).t());
27 #endif
28  }
29 
30  void CGraph::getNodes(size_t start_node, size_t num_nodes, Mat& pots) const {
31  if (!num_nodes) num_nodes = getNumNodes() - start_node;
32 
33  // Assertions
34  DGM_ASSERT_MSG(start_node + num_nodes <= getNumNodes(), "The given ranges exceed the number of nodes(%zu)", getNumNodes());
35 
36  if (pots.empty() || pots.cols != m_nStates || pots.rows != num_nodes)
37  pots = Mat(static_cast<int>(num_nodes), m_nStates, CV_32FC1);
38 
39  transpose(pots, pots);
40 
41 #ifdef ENABLE_PPL
42  int size = pots.cols;
43  int rangeSize = size / (concurrency::GetProcessorCount() * 10);
44  rangeSize = MAX(1, rangeSize);
45  //printf("Processors: %d\n", concurrency::GetProcessorCount());
46  concurrency::parallel_for(0, size, rangeSize, [start_node, size, rangeSize, &pots, this](int i) {
47  Mat pot;
48  for (int j = 0; (j < rangeSize) && (i + j < size); j++)
49  getNode(start_node + i + j, lvalue_cast(pots.col(i + j)));
50  });
51 #else
52  for (int n = 0; n < pots.cols; n++)
53  getNode(start_node + n, lvalue_cast(pots.col(n)));
54 #endif
55  transpose(pots, pots);
56  }
57 }
virtual void setNode(size_t node, const Mat &pot)=0
Sets or changes the potential of node.
virtual size_t addNode(const Mat &pot=EmptyMat)=0
Adds an additional node (with specified potentional)
virtual size_t getNumNodes(void) const =0
Returns the number of nodes in the graph.
virtual void addNodes(const Mat &pots)
Adds the graph nodes with potentials.
Definition: Graph.cpp:6
virtual void getNode(size_t node, Mat &pot) const =0
Returns the node potential.
virtual void getNodes(size_t start_node, size_t num_nodes, Mat &pots) const
Returns the node potentials.
Definition: Graph.cpp:30
byte m_nStates
The number of states (classes)
Definition: Graph.h:103
virtual void setNodes(size_t start_node, const Mat &pots)
Fills the graph nodes with new potentials.
Definition: Graph.cpp:11