-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGlobalArray.cpp
More file actions
216 lines (166 loc) · 5.65 KB
/
GlobalArray.cpp
File metadata and controls
216 lines (166 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/***************************************************************************//**
* Project: Colony
*
* \file GlobalArray.cpp
* \author Marc Weber\n
* The SiMBioSys group (CosmoLab)\n
* Parc Científic de Barcelona\n
* Barcelona, Spain.\n
* http://www.thesimbiosys.com
* \version 1.0
* \date 11/2009
*
* Copyright 2009 by Marc Weber
******************************************************************************/
#include "GlobalArray.h"
//------------------------------------------------------------------------------
template <class T>
GlobalArray<T>::GlobalArray()
: size_(0),
nNodes_(0)
{}
//------------------------------------------------------------------------------
template <class T>
GlobalArray<T>::~GlobalArray()
{
globalArray_.free();
}
//------------------------------------------------------------------------------
template <class T>
void GlobalArray<T>::build(list< LocalArrayTarget<T> >& localArrayList)
{
// Set the number of nodes.
nNodes_ = localArrayList.size();
int nodeIndex = 0;
set<int>::iterator itset;
/*
* The 'typename' keyword is needed for the compiler to understand that
* this statement is a declaration of the variable 'itlist' with type
* 'list< LocalArrayTarget<T> >::iterator'.
*/
typename list< LocalArrayTarget<T> >::iterator itlist;
for (itlist =localArrayList.begin(), itset=nodesTable_.begin();
itlist!=localArrayList.end();
itlist++, itset++)
{
// Insert the position of the first element of the local array in the
// nodes table.
nodesTable_.insert(itset, nodeIndex); // Rem: order of these 2 instructions
nodeIndex += (*itlist).size; // is important
}
size_ = nodeIndex; // Total size of the global array.
// Resize global array (all data are garbage).
globalArray_.resize(size_);
int a = 0, b = 0;
for (itlist =localArrayList.begin(), itset=nodesTable_.begin();
itlist!=localArrayList.end();
itlist++)
{
// Make a copy of local array.
Array<T,1> localArrayCopy( (*itlist).ptr->copy() );
// Set local array to be a reference to a part of the global array.
a = (*itset);
itset++;
b = (*itset) - 1;
(*itlist).ptr->reference( globalArray_( Range(a, b) ) );
// Copy back values into local array.
(*(*itlist).ptr) = localArrayCopy;
}
}
//------------------------------------------------------------------------------
template <class T>
list<Array<T,1> > GlobalArray<T>::buildGlobalArrayRangesList
(list< LocalArrayTarget<T> >& localArrayList)
{
// Set the number of nodes.
nNodes_ = localArrayList.size();
int nodeIndex = 0;
set<int>::iterator itset;
/*
* The 'typename' keyword is needed for the compiler to understand that the
* this statement is a declaration of the variable 'itlist' with type
* 'list< LocalArrayTarget<T> >::iterator'.
*/
typename list< LocalArrayTarget<T> >::iterator itlist;
for (itlist =localArrayList.begin(), itset=nodesTable_.begin();
itlist!=localArrayList.end();
itlist++)
{
/* Insert the position of the first element of the local array in the
nodes table. */
// Insert the nodeIndex value in the nodes table.
itset = nodesTable_.insert(itset, nodeIndex);
// Update the node index with the size of the local array.
nodeIndex += (*itlist).size;
}
size_ = nodeIndex; // Total size of the global array.
// Resize global array (all data are garbage).
globalArray_.resize(size_);
// Build the list of global array parts.
list<Array<T,1> > globalArrayPartsList;
int a = 0, b = 0;
for (itlist =localArrayList.begin(), itset=nodesTable_.begin();
itlist!=localArrayList.end();
itlist++)
{
// Build the list of the parts of the global array, to be sent to the local arrays to be a referenced.
a = (*itset);
itset++;
if (itset != nodesTable_.end())
{
b = (*itset) - 1;
} else{
b = size_-1;
}
globalArrayPartsList.push_back( globalArray_( Range(a, b) ) );
}
return globalArrayPartsList;
}
//------------------------------------------------------------------------------
template <class T>
Array<T,1> GlobalArray<T>::getNode(int k)
{
if (k > nNodes_ - 1)
{
cout << "ERROR: class GlobalArray, getNode(), index k is out of bounds"
<< " k = " << k << " nNodes_ - 1 = " << nNodes_ - 1 << endl;
exit(1);
} else
{
set<int>::iterator it;
it = nodesTable_.begin();
// Jump k elements in the list.
int j;
for (j=0; j<k; j++) {it++;}
int a = *it;
it++;
int b = *it - 1;
// Array<T,1> nodeArray( globalArray_( Range(a, b) ) );
return globalArray_( Range(a, b) );
}
}
//------------------------------------------------------------------------------
template <class T>
pair<int,int> GlobalArray<T>::getLocalIndicesFromGlobal(int i) const
{
// Set an iterator pointing to the node to which belongs element i.
set<int>::iterator it;
it = nodesTable_.upper_bound(i);
it--;
// Index inside the node
int j = i - *it;
// Count the number of nodes from the beginning of the list (reverse order)
int k;
for (k = 0; it != nodesTable_.begin(); it--, k++) {} // Node number starts at 0.
pair<int,int> kj(k, j);
return kj;
}
//------------------------------------------------------------------------------
/**
* To avoid linker errors, we have to define the template class we will use.
* For more information, see the C++ FAQ:\n
* http://www.parashift.com/c++-faq-lite/templates.html#faq-35.15
*/
template class GlobalArray<double>;
template class GlobalArray<int>;
//------------------------------------------------------------------------------