Memosa-FVM  0.2
SpikeStorage.cpp
Go to the documentation of this file.
1 // This file os part of FVM
2 // Copyright (c) 2012 FVM Authors
3 // See LICENSE file for terms.
4 
5 #ifdef FVM_PARALLEL
6 #include <mpi.h>
7 #endif
8 
9 #include "CRConnectivity.h"
10 #include "SpikeStorage.h"
11 #include "Field.h"
12 #include <iostream>
13 
14 using namespace std;
15 
16 
17 SpikeStorage::SpikeStorage(const CRConnectivity& conn, int semi_bandwidth):
18 _conn(conn), _bandwidth(semi_bandwidth)
19 {
20  //logCtor();
21 
22  init();
23 }
24 
26 {
27  //logDtor();
28 }
29 
30  //PRIVATE
31 void
33 {
34  //reserve minumum space for data structures
35  _LSPK_INTERIOR.reserve(_bandwidth);
36  _LSPK_GHOST.reserve (_bandwidth);
37  _LSPK_OFFD_PTR.reserve(_bandwidth);
38  _RSPK_INTERIOR.reserve(_bandwidth);
39  _RSPK_GHOST.reserve (_bandwidth);
40  _RSPK_OFFD_PTR.reserve(_bandwidth);
41  _LSPK_I.reserve(_bandwidth);
42  _LSPK_J.reserve(_bandwidth);
43  _RSPK_I.reserve(_bandwidth);
44  _RSPK_J.reserve(_bandwidth);
45  //keeping left and right spike matrices counter, we will know if there is more than one ghost cell
46  int ncells = _conn.getRowSite().getSelfCount();
47  _LSPKCountGhost.resize(ncells,0);
48  _RSPKCountGhost.resize(ncells,0);
49 
50 
51 #ifdef FVM_PARALLEL
52  _cellSelfCounts.resize( MPI::COMM_WORLD.Get_size() );
53  _procID = MPI::COMM_WORLD.Get_rank();
54 #endif
55  //get size of cells from other process
57  //syn operation to get neighbour local indices to this ghost cells
58  syncCellIDs();
59  //create local array to store global indices
61  //spk_offd_ptr
62  setOffDiagPtr();
63 }
64 
65 //gather cells size
66 void
68 {
69  const StorageSite& cellSite = _conn.getRowSite();
70  _localCellSelfCount = cellSite.getSelfCount();
71 #ifdef FVM_PARALLEL
72  MPI::COMM_WORLD.Allgather(&_localCellSelfCount, 1, MPI::INT, &_cellSelfCounts[0], 1, MPI::INT);
73 #endif
74 }
75 //sync operation to get cellIDs
76 void
78 {
79  shared_ptr<Field> cellIndicesField( new Field("cellID") );
80 
81  const StorageSite& cells = _conn.getRowSite();
82  const int cellCount = cells.getCount();
83  shared_ptr< Array<int> > indPtr( new Array<int>(cellCount) );
84  Array<int>& indices = *indPtr;
85  Array<int> indicesOld( cellCount ); //keep original indices
86  //zeroing
87  indices.zero();
88  //filling indices
89  for ( int n = 0; n < cells.getCount(); n++ ){
90  indices[n] = n;
91  indicesOld[n] = n;
92  }
93  //volume add
94  cellIndicesField->addArray(cells, indPtr);
95  //synLocal to get neighbourhood
96  cellIndicesField->syncLocal();
97  //create mapping between old and new ghost ids
98  int ibeg = cells.getSelfCount();
99  int iend = cells.getCount();
100  for ( int i = ibeg; i < iend; i++ ){
101  _ghostMap[ indicesOld[i] ] = indices[i];
102  }
103 
104 
105 }
106 
107 //create local array to store global indices
108 void
110 {
111  int indx_base = 0;
112  for ( int i = 1; i <= _procID; i++)
113  indx_base += _cellSelfCounts[i-1];
114 
115  const StorageSite& cellSite = _conn.getRowSite();
116  const StorageSite::GatherMap& gatherMap = cellSite.getGatherMap();
117  //loop over gather maps
118  foreach( const StorageSite::GatherMap::value_type& mpos, gatherMap ){
119  const StorageSite& oSite = *mpos.first;
120  const int oRank = oSite.getGatherProcID();
121  const Array<int>& ghostIndices = *(mpos.second);
122  //get inner indices
123  Array<int> innerIndices( ghostIndices.getLength() );
124  for ( int n = 0; n < ghostIndices.getLength(); n++ ){
125  innerIndices[n] = _conn( ghostIndices[n], 0 );
126  }
127  //finding base for neighbour
128  int indx_base_other = 0;
129  for ( int i = 1; i <= oRank; i++)
130  indx_base_other += _cellSelfCounts[i-1];
131 
132  //check
133  for ( int n = 0; n < ghostIndices.getLength(); n++ ){
134  const int iGlbIndx = indx_base + innerIndices[n];
135  const int jGlbIndx = indx_base_other + _ghostMap[ ghostIndices[n] ];
136  //check left or right
137  if ( (iGlbIndx > jGlbIndx) && ((iGlbIndx - jGlbIndx) <= _bandwidth) ){
138  _LSPK_INTERIOR.push_back( innerIndices[n] );
139  _LSPK_GHOST.push_back ( ghostIndices[n] );
140  _LSPK_I.push_back( innerIndices[n] );
141  _LSPK_J.push_back( _bandwidth + jGlbIndx - indx_base );
142  _LSPKCountGhost[innerIndices[n]] += 1;
143  } else if ( (iGlbIndx < jGlbIndx) && (jGlbIndx - iGlbIndx) <= _bandwidth ) {
144  _RSPK_INTERIOR.push_back( innerIndices[n] );
145  _RSPK_GHOST.push_back ( ghostIndices[n] );
146  _RSPK_I.push_back( innerIndices[n] );
147  _RSPK_J.push_back( jGlbIndx - indx_base_other );
148  _RSPKCountGhost[innerIndices[n]] += 1;
149  }
150  }
151  }
152 
153 }
154 //compute RSP_OFFD_PTR and LSPK_OFFD_PTR
155 void
157 {
158  //loop over LSPK
159  const Array<int>& row = _conn.getRow();
160  const Array<int>& col = _conn.getCol();
161  for( unsigned int i = 0; i < _LSPK_INTERIOR.size(); i++ ){
162  const int rowIndx = _LSPK_INTERIOR[i];
163  for ( int j = row[rowIndx]; j < row[rowIndx+1]; j++ ){
164  const int neighCellID = col[j];
165  if ( neighCellID == _LSPK_GHOST[i] )
166  _LSPK_OFFD_PTR.push_back( j );
167  }
168  }
169  //loop over RSPK
170  for( unsigned int i = 0; i < _RSPK_INTERIOR.size(); i++ ){
171  const int rowIndx = _RSPK_INTERIOR[i];
172  for ( int j = row[rowIndx]; j < row[rowIndx+1]; j++ ){
173  const int neighCellID = col[j];
174  if ( neighCellID == _RSPK_GHOST[i] )
175  _RSPK_OFFD_PTR.push_back( j );
176  }
177  }
178 
179 
180 }
const Array< int > & getCol() const
virtual void zero()
Definition: Array.h:281
const Array< int > & getRow() const
int getSelfCount() const
Definition: StorageSite.h:40
vector< int > _LSPK_J
Definition: SpikeStorage.h:64
vector< int > _LSPK_I
Definition: SpikeStorage.h:63
Definition: Field.h:14
void setOffDiagPtr()
vector< int > _RSPK_I
Definition: SpikeStorage.h:65
vector< int > _RSPK_GHOST
Definition: SpikeStorage.h:61
vector< int > _LSPK_OFFD_PTR
Definition: SpikeStorage.h:59
vector< int > _LSPKCountGhost
Definition: SpikeStorage.h:68
vector< int > _RSPK_OFFD_PTR
Definition: SpikeStorage.h:62
void syncCellIDs()
vector< int > _LSPK_GHOST
Definition: SpikeStorage.h:58
vector< int > _RSPKCountGhost
Definition: SpikeStorage.h:69
vector< int > _RSPK_INTERIOR
Definition: SpikeStorage.h:60
int getGatherProcID() const
Definition: StorageSite.h:83
const CRConnectivity & _conn
Definition: SpikeStorage.h:49
int _localCellSelfCount
Definition: SpikeStorage.h:53
vector< int > _RSPK_J
Definition: SpikeStorage.h:66
map< int, int > _ghostMap
Definition: SpikeStorage.h:54
SpikeStorage(const CRConnectivity &conn, int semi_bandwidth)
const GatherMap & getGatherMap() const
Definition: StorageSite.h:59
void setGlobalIndices()
int getCount() const
Definition: StorageSite.h:39
vector< int > _cellSelfCounts
Definition: SpikeStorage.h:56
map< const StorageSite *, shared_ptr< Array< int > > > GatherMap
Definition: StorageSite.h:24
const StorageSite & getRowSite() const
vector< int > _LSPK_INTERIOR
Definition: SpikeStorage.h:57
void gatherCellSizes()
int getLength() const
Definition: Array.h:87