SpikeGPU  1.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
components.h
Go to the documentation of this file.
1 #ifndef SPIKE_COMPONENTS_H
2 #define SPIKE_COMPONENTS_H
3 
4 #include <map>
5 
6 #include <cusp/array1d.h>
7 #include <thrust/sequence.h>
8 
9 
10 namespace spike {
11 
12 
13 // ----------------------------------------------------------------------------
14 // Components
15 //
16 // This structure encapsulate information about matrix component, representing
17 // decoupled diagonal blocks which can be processed independently.
18 // ----------------------------------------------------------------------------
19 struct Components
20 {
21  typedef typename cusp::array1d<int, cusp::host_memory> IntVectorH;
22 
23  Components(int n) : m_n(n) {
24  m_compIndices.resize(m_n);
25  thrust::sequence(m_compIndices.begin(), m_compIndices.end());
27  }
28 
29  int getComponentIndex(int node) {
30  if (m_compIndices[node] == node)
31  return node;
33  return m_compIndices[node];
34  }
35 
36  void combineComponents(int node1, int node2) {
37  int r1 = getComponentIndex(node1), r2 = getComponentIndex(node2);
38 
39  if (r1 != r2) {
40  m_compIndices[r1] = r2;
41  m_numComponents --;
42  }
43  }
44 
46  for (int i = 0; i < m_n; i++)
48 
49  std::map<int, int> compIndicesMapping;
50  IntVectorH compCounts(m_numComponents, 0);
51 
52  int cur_count = 0;
53  for (int i = 0; i < m_n; i++) {
54  int compIndex = m_compIndices[i];
55  if (compIndicesMapping.find(compIndex) == compIndicesMapping.end())
56  m_compIndices[i] = compIndicesMapping[compIndex] = (++cur_count);
57  else
58  m_compIndices[i] = compIndicesMapping[compIndex];
59 
60  compCounts[--m_compIndices[i]]++;
61  }
62 
63  int numComponents = m_numComponents;
64 
65  bool found = false;
66  int selected = -1;
67  for (int i = 0; i < m_numComponents; i++) {
68  if (compCounts[i] == 1) {
69  numComponents --;
70  if (! found) {
71  found = true;
72  selected = i;
73  }
74  }
75  }
76 
77  if (found) {
78  m_numComponents = numComponents + 1;
79  for (int i = 0; i < m_n; i++)
80  if (compCounts[m_compIndices[i]] == 1)
81  m_compIndices[i] = selected;
82 
83  cur_count = 0;
84  compIndicesMapping.clear();
85  for (int i = 0; i < m_n; i++) {
86  int compIndex = m_compIndices[i];
87  if (compIndicesMapping.find(compIndex) == compIndicesMapping.end())
88  m_compIndices[i] = compIndicesMapping[compIndex] = (++cur_count);
89  else
90  m_compIndices[i] = compIndicesMapping[compIndex];
91 
92  --m_compIndices[i];
93  }
94  }
95  }
96 
98  int m_n;
100 };
101 
102 
103 
104 } // namespace spike
105 
106 
107 #endif
int getComponentIndex(int node)
Definition: components.h:29
cusp::array1d< int, cusp::host_memory > IntVectorH
Definition: components.h:21
void adjustComponentIndices()
Definition: components.h:45
IntVectorH m_compIndices
Definition: components.h:97
int m_n
Definition: components.h:98
Components(int n)
Definition: components.h:23
Definition: components.h:19
int m_numComponents
Definition: components.h:99
void combineComponents(int node1, int node2)
Definition: components.h:36