-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathmain.cpp
345 lines (307 loc) · 9.88 KB
/
main.cpp
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/**********************************************************************
** This program is part of 'MOOSE', the
** Messaging Object Oriented Simulation Environment.
** Copyright (C) 2003-2009 Upinder S. Bhalla. and NCBS
** It is made available under the terms of the
** GNU Lesser General Public License version 2.1
** See the file COPYING.LIB for the full notice.
**********************************************************************/
#include "header.h"
#include "SparseMatrix.h"
#include <time.h>
#include <math.h>
#include <queue>
#if defined(_WIN32)
#include "getopt.h"
#else
#include <unistd.h> // for getopt
#endif
#include "../scheduling/Clock.h"
#include "../msg/DiagonalMsg.h"
#include "../msg/SparseMsg.h"
#include "../mpi/PostMaster.h"
#ifdef USE_MPI
#include <mpi.h>
#endif
#include "../shell/Shell.h"
#ifdef MACOSX
#include <sys/sysctl.h>
#endif // MACOSX
#ifdef DO_UNIT_TESTS
#include <iomanip>
int testIndex = 0;
#define MOOSE_TEST(name, f ) \
cout << "[TEST " << setw(2) << testIndex << "] " << name; \
testIndex += 1; \
f; \
cout << std::right << " [DONE]" << endl; \
extern void testAsync();
extern void testSyncArray( unsigned int size, unsigned int method );
extern void testShell();
extern void testScheduling();
extern void testSchedulingProcess();
extern void testBuiltins();
extern void testSynapse();
extern void testBuiltinsProcess();
extern void testMpiScheduling();
extern void testMpiBuiltins();
extern void testMpiShell();
extern void testMsg();
extern void testMpiMsg();
// extern void testKinetics();
extern void testKsolve();
extern void testKsolveProcess();
extern void testBiophysics();
extern void testBiophysicsProcess();
extern void testDiffusion();
extern void testHSolve();
// extern void testKineticsProcess();
// extern void testGeom();
extern void testMesh();
// extern void testSimManager();
extern void testSigNeur();
extern void testSigNeurProcess();
extern unsigned int initMsgManagers();
extern void destroyMsgManagers();
// void regressionTests();
#endif // DO_UNIT_TESTS
extern void speedTestMultiNodeIntFireNetwork(unsigned int size, unsigned int runsteps);
#ifdef USE_SMOLDYN
extern void testSmoldyn();
#endif
// bool benchmarkTests( int argc, char** argv );
//////////////////////////////////////////////////////////////////
// System-dependent function here
//////////////////////////////////////////////////////////////////
unsigned int getNumCores()
{
unsigned int numCPU = 0;
#ifdef LINUX
numCPU = sysconf( _SC_NPROCESSORS_ONLN );
#endif
#ifdef MACOSX
int mib[4];
size_t len = sizeof(numCPU);
/* set the mib for hw.ncpu */
mib[0] = CTL_HW;
mib[1] = HW_AVAILCPU; // alternatively, try HW_NCPU;
/* get the number of CPUs from the system */
sysctl(mib, 2, &numCPU, &len, NULL, 0);
if( numCPU < 1 )
{
mib[1] = HW_NCPU;
sysctl( mib, 2, &numCPU, &len, NULL, 0 );
}
#endif
if ( numCPU < 1 )
{
cout << "No CPU information available. Assuming single core." << endl;
numCPU = 1;
}
return numCPU;
}
bool quitFlag = 0;
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
void checkChildren( Id parent, const string& info )
{
vector< Id > ret;
Neutral::children( parent.eref(), ret );
cout << info << " checkChildren of " <<
parent.element()->getName() << ": " <<
ret.size() << " children\n";
for ( vector< Id >::iterator i = ret.begin(); i != ret.end(); ++i )
{
cout << i->element()->getName() << endl;
}
}
Id init(int argc, char** argv, bool& doUnitTests)
{
unsigned int numCores = getNumCores();
int numNodes = 1;
int myNode = 0;
bool isInfinite = 0;
int opt;
Cinfo::rebuildOpIndex();
#ifdef USE_MPI
MPI_Init( &argc, &argv );
MPI_Comm_size( MPI_COMM_WORLD, &numNodes );
MPI_Comm_rank( MPI_COMM_WORLD, &myNode );
#endif
/**
* Here we allow the user to override the automatic identification
* of processor configuration
*/
while ( ( opt = getopt( argc, argv, "hiqun:b:B:" ) ) != -1 )
{
switch ( opt )
{
case 'i' : // infinite loop, used for multinode debugging, to give gdb something to attach to.
isInfinite = 1;
break;
case 'n': // Multiple nodes
numNodes = (unsigned int)atoi( optarg );
break;
case 'B': // Benchmark plus dump data: handle later.
break;
case 'u': // Do unit tests, pass back.
doUnitTests = true;
break;
case 'q': // quit immediately after completion.
quitFlag = 1;
break;
case 'h': // help
default:
cout << "Usage: moose -help -infiniteLoop -unit_tests -quit -n numNodes\n";
exit( 1 );
}
}
if ( myNode == 0 )
{
#ifndef QUIET_MODE
cout << "on node " << myNode << ", numNodes = "
<< numNodes << ", numCores = " << numCores << endl;
#endif
}
Id shellId;
Element* shelle =
new GlobalDataElement( shellId, Shell::initCinfo(), "root", 1 );
Id clockId = Id::nextId();
assert( clockId.value() == 1 );
Id classMasterId = Id::nextId();
Id postMasterId = Id::nextId();
Shell* s = reinterpret_cast< Shell* >( shellId.eref().data() );
s->setShellElement( shelle );
s->setHardware( numCores, numNodes, myNode );
s->loadBalance();
/// Sets up the Elements that represent each class of Msg.
unsigned int numMsg = Msg::initMsgManagers();
new GlobalDataElement( clockId, Clock::initCinfo(), "clock", 1 );
new GlobalDataElement( classMasterId, Neutral::initCinfo(), "classes", 1);
new GlobalDataElement( postMasterId, PostMaster::initCinfo(), "postmaster", 1 );
assert ( shellId == Id() );
assert( clockId == Id( 1 ) );
assert( classMasterId == Id( 2 ) );
assert( postMasterId == Id( 3 ) );
Shell::adopt( shellId, clockId, numMsg++ );
Shell::adopt( shellId, classMasterId, numMsg++ );
Shell::adopt( shellId, postMasterId, numMsg++ );
assert( numMsg == 10 ); // Must be the same on all nodes.
Cinfo::makeCinfoElements( classMasterId );
while ( isInfinite ) // busy loop for debugging under gdb and MPI.
;
return shellId;
}
/**
* These tests are meant to run on individual nodes, and should
* not invoke MPI calls. They should not be run when MPI is running.
* These tests do not use the threaded/MPI event loop and are the most
* basic of the set.
*/
void nonMpiTests( Shell* s )
{
#ifdef DO_UNIT_TESTS
if ( Shell::myNode() == 0 )
{
unsigned int numNodes = s->numNodes();
unsigned int numCores = s->numCores();
if ( numCores > 0 )
s->setHardware( 1, 1, 0 );
MOOSE_TEST("testAsync", testAsync());
MOOSE_TEST("testMsg", testMsg());
MOOSE_TEST("testShell", testShell());
MOOSE_TEST("testScheduling", testScheduling());
MOOSE_TEST("testBuiltints", testBuiltins());
//MOOSE_TEST("testKinetics", testKinetics());
MOOSE_TEST("testKsolve", testKsolve());
//MOOSE_TEST("testKsolveProcess", testKsolveProcess());
MOOSE_TEST("testBiophysics", testBiophysics());
MOOSE_TEST("testDiffusion", testDiffusion());
MOOSE_TEST("testHsolve", testHSolve());
//MOOSE_TEST("testGeom", testGeom());
MOOSE_TEST("testMesh", testMesh());
MOOSE_TEST("testSynapse", testSynapse());
MOOSE_TEST( "testSigneur", testSigNeur());
s->setHardware( numCores, numNodes, 0 );
}
#endif
}
/**
* These tests involve the threaded/MPI process loop and are the next
* level of tests.
*/
void processTests( Shell* s )
{
#ifdef DO_UNIT_TESTS
MOOSE_TEST( "testSchedulingProcess", testSchedulingProcess());
MOOSE_TEST( "testBuiltinsProcess", testBuiltinsProcess());
MOOSE_TEST( "testBiophysicsProcess", testBiophysicsProcess());
MOOSE_TEST( "testSigNeurProcess", testSigNeurProcess());
#endif
}
/**
* These are tests that are MPI safe. They should also run
* properly on single nodes.
*/
void mpiTests()
{
#ifdef DO_UNIT_TESTS
MOOSE_TEST( "testMpiMsg", testMpiMsg());
MOOSE_TEST( "testMpiShell", testMpiShell());
MOOSE_TEST( "testMpiBuiltins", testMpiBuiltins());
MOOSE_TEST( "testMpiScheduling", testMpiScheduling());
#endif
}
#if ! defined(PYMOOSE) && ! defined(MOOSE_LIB)
int main( int argc, char** argv )
{
bool doUnitTests = 0;
bool doRegressionTests = 0;
unsigned int benchmark = 0;
// This reorders the OpFunc to Fid mapping to ensure it is node and
// compiler independent.
Id shellId = init(argc, argv, doUnitTests);
// Note that the main loop remains the parser loop, though it may
// spawn a lot of other stuff.
Element* shelle = shellId.element();
Shell* s = reinterpret_cast< Shell* >( shelle->data( 0 ) );
if ( doUnitTests )
nonMpiTests( s ); // These tests do not need the process loop.
if ( Shell::myNode() == 0 )
{
if ( Shell::numNodes() > 1 )
{
// Use the last clock for the postmaster, so that it is called
// after everything else has been processed and all messages
// are ready to send out.
s->doUseClock( "/postmaster", "process", 9 );
s->doSetClock( 9, 1.0 ); // Use a sensible default.
}
#ifdef DO_UNIT_TESTS
if ( doUnitTests )
{
mpiTests();
processTests( s );
}
#endif
// Here we set off a little event loop to poll user input.
// It deals with the doQuit call too.
if(! quitFlag)
Shell::launchParser();
}
else
{
PostMaster* p = reinterpret_cast< PostMaster* >( ObjId( 3 ).data());
while ( Shell::keepLooping() )
{
p->clearPending();
}
}
Msg::clearAllMsgs();
Id::clearAllElements();
#ifdef USE_MPI
MPI_Finalize();
#endif
return 0;
}
#endif