-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMGS.tex
12372 lines (9495 loc) · 395 KB
/
MGS.tex
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\chapter{MGS}
MGS is a C++ graph-based modeling framework. The input to the system is a
scripting language called {\bf GSL} (Sect.\ref{sec:GSL-how-it-works}).
GSL : the script that helps to connect different C++ classes together in
a graph-based simulator system. (Chap.\ref{chap:MGS-GSL})
Any component in MGS needs to belong to certain category
(Sect.\ref{sec:MGS-components}). To help writing code conforms to MGS framework,
another scripting language was used MDL : the script that helps generating C++
classes that conform to MGS code standard (Chap.\ref{chap:MGS-MDL})
\section{Tutorial: Graph-based simulation}
\label{sec:graph-based-simulation}
The core of the graph-based simulation is saved in 2 folders:
\verb!gsl! and \verb!mdl!.
\begin{verbatim}
nts/
gsl/
mdl/
models/ (where developed *.mdl files reside)
graphs/ (where developed *.gsl files reside)
nti/ (code for NTS purpose)
common/ (where common utilities code reside)
datasheets/
\end{verbatim}
The software has 3 components
\begin{enumerate}
\item GSL (Sect.\ref{sec:GSL}):
This is known as MGS (Model-Graph Simulator) - \verb!gslparser! executable
file that can parse script written in GSL language to define graph
connection.
Components in a graph can be one of the following forms (Sect.\ref{sec:graph-components}).
Regardless of the form, at the code-level, they are implemented in the form of
a C++ classes. In order to help the MGS framework to be able to 'connect' them
together, i.e. mapping memory references from one instance to another,
there is a need for several helping classes; which follows a given code
design of class hierarchy.
As all of these helping classes follow a given design patterns; they can be
code-generated. A scripting language - called MDL - has been developed to
facilitate that process.
\item MDL (Sect.\ref{sec:MDL}): MDL scripting language is designed to help
generating helping classes for any C++ class to be usable as a
MGS-based graph-component.
\verb!mdlparser! executable file that can parse the script written in MDL
language to define models, and generated C++ code stub, which then can be used
in GSL.
\item NTS (Sect.\ref{sec:TissueFunctor}): a set of components implemented
using MDL and GSL that allows the incorporating .swc files into the graphs
to perform the simulation in the computational neuroscience domain.
\end{enumerate}
This section goes through a number of examples on how to use the
NTS system.
Steps for every project
\begin{enumerate}
\item write .mdl script if a new graph-based component is needed (i.e.
NodeType, VariableType, ConstantType)
\item write the .gsl script, e.g. \verb!models.gsl!, that combines all these
graph-based components in a proper way
\item run the simulation
\begin{verbatim}
mpiexec -n 8 <path-to>/gslparser models.gsl
\end{verbatim}
\end{enumerate}
\subsection{Graph's components}
\label{sec:graph-components}
We focus on the NodeType (Sect.\ref{sec:NodeType-GSL}).
Suppose nodetype \verb!AMPAReceptor! has the class
\begin{verbatim}
class CG_AMPAReceptor : public DimensionArrayProducer,
public IndexArrayProducer,
public BranchDataArrayProducer
{
}
\end{verbatim}
\subsection{How the MGS framework works?}
\label{sec:MGS-how-it-works}
MGS enables the user to define a time-step based simulation system (TSBSS).
The TSBSS is defined in a *.gsl file (Sect.\ref{sec:GSL-script}).
To understand how the simulation works, read
Sect.\ref{sec:GSL-how-it-works}.
The system is represented as a graph-based system with different components
interconnected to each other.
There are different kinds of component that can be used in a TSBSS
(Sect.\ref{sec:MDL-component}). The code for each component (e.g. Node) follows
certain pattern that can be code-generated if we can describe what kinds of data
the component expose (via \verb!Implements! keyword), and what are the phase
kernels (e.g. InitPhase, RuntimePhase), for each phase, what kinds of data to be
synced (to the proxy, if present, by passing the data as the arguments to the
phase kernel); and how it references to data on another component (via incoming
connection). All of these are described in the MDL (e.g. LifeNode), example
{\tiny
\begin{verbatim}
Interface ValueProducer {
int *value;
}
Node LifeNode Implements ValueProducer {
int value;
int publicValue;
int* [] neighbors;
InitPhase initialize;
RuntimePhase update();
RuntimePhase copy(publicValue);
ValueProducer.value << &publicValue; /* this line define what data attribute
of the node is available for other node
to access
*/
}
\end{verbatim}
}
Using the \verb!mdlparser! binary, the generated code is copied to a folder of
the same name in the \verb!./gsl/extension/<component>/!,
\begin{verbatim}
./gsl/extension/node/LifeNode
\end{verbatim}
Inside this folder, we have \verb!src! and \verb!include! subfolders which holds
a number of classes which are related to each other via a
a hierarchical structure in which two main classes are the LifeNode classs and
LifeNodeCompCategory class. The other classes are supporting classes (i.e.
code-generated classes) that can help the MGS framework to get access to the
underlying data of LifeNode and LifeNodeCompCategory.
\begin{verbatim}
./gsl/extension/node/LifeNode/src
./gsl/extension/node/LifeNode/include
\end{verbatim}
Here, we describe how the MGS framework connect different components together.
Any component in a graph-based system (e.g. Node, Variable, Constant)
communicate to each other via one's provided service. Here, a service refers to
a particular data member of a given component that can be accessed from another
component (Sect.\ref{sec:Service}).
Each component is used as a class, so its data member are exposed to MGS in the
form of service (Sect.\ref{sec:Node-data-member-as-a-service}).
To manage different Services of a graph's component, e.g. LifeNode for example,
a Publisher (Sect.\ref{sec:Publisher}) is generated for that graph's component,
e.g.
\verb!CG_LifeNodePublisher! class (Sect.\ref{sec:CG_xxxPublisher}).
If a component want their data to be accessible from another component, it
needs to expose via an interface (Sect.\ref{sec:MDL-Interface}). Then, we map
the data to the interface using \verb!<<! operator in the MDL code
\begin{verbatim}
// here it exposes the address of the data member
ValueProducer.value << &publicValue;
\end{verbatim}
Example: The generated code enables the accessing of the proper data member,
given the Interface name and the Interface's data member
{\tiny
\begin{verbatim}
std::string CG_LifeNodePublisher::getServiceNameWithInterface
(const std::string& interfaceName,
const std::string& subInterfaceName)
{
if (interfaceName == "ValueProducer") {
if (subInterfaceName == "value") {
return "publicValue";
}
}
return "";
}
\end{verbatim}
}
\subsection{-- MGS: multiple layers of abstraction}
\label{sec:MGS-multiple-layer-of-abstraction}
MULTIPLE LAYERS OF ABSTRACTION: With the different supporting classes (prefix
\verb!CG_!), it enables one class, representing a model component, to be able to
communicate with other classes, representing other model components. In
particular, the class goes through 3 or 4 layers of abstraction that coordinate
the communication between them, regardless of class's definition, and processes
on which the class's object runs, Fig.\ref{fig:NTS_code-layers}.
NOTE: The Interface component only goes through 3 layers, as it has no code body
implementation. The classes generated from 'Interface' component works as proxy
for communication between instances from two different classes.
\begin{itemize}
\item 2 layers for generated code from MDL
\item 2 layers for codes defined in the MGS frameworks to handle the
communication between generated codes at different hardware level, i.e.
shared-memory processors or distributed-memory processors.
\end{itemize}
\begin{figure}[hbt]
\centerline{\includegraphics[height=5cm,
angle=0]{./images/NTS_code-layers.eps}}
\caption{Layers of code in a model}
\label{fig:NTS_code-layers}
\end{figure}
% How about Interface? as a service (i.e. the Node's data member -
% Sect.\ref{sec:Node-data-member-as-a-service}) is exposed via the interface,
% another method is defined to enable the accessing of the proper data member,
% given the Interface name and the Interface's data member.
\subsection{-- MGS: distributed system}
\label{sec:MGS-distributed-system}
The whole world, as defined in GSL may not fit a single MPI process, i.e. only a
portion of the world is handled by the current MPI process. Such a portion is
called a granule (Sect.\ref{sec:Granule}). To help dividing the 'world' into
granule, a proper granule mapper to support granule mapping has to be used
(Sect.\ref{sec:GranuleMapper}).
Another important part of the graph-based simulation is how the different
instances of the same node-type, or from different node-type connect to each
other, i.e. access the data from each other. Then we need to use connector
functor (Sect.\ref{sec:connector-functor}).
\subsection{Game of Life}
\label{sec:nts-example-game-of-life}
Here, you want to have a 2D map of cells, each cell can get value 0 or 1,
depending upon the sum of its 4 neighbors' values.
\begin{itemize}
\item sum $\le$ sparseValue: make the cell alive, i.e. set 1
\item sum $\ge$ crowdedValue: kill the cell, i.e. set 0
\end{itemize}
Each node can live or die (i.e. represented by one data member with two
possible values 0 or 1), and this value changes after each iteration, depending
on how many alive neighbors.
\subsection{* GSL script}
Briefly, the highest level of abstraction in GSL is Repertoire
(Sect.\ref{sec:GSL-repertoire}). The system performs the simulation by looking
at first the 'root' Repertoire.
Suppose you have one or more directed graphs: how to know which one is the root
Repertoire. The context-based GSL scriptting language helps identify the root
Repertoire. Most of the time, we only have one grid in the system
(Sect.\ref{sec:Grid}).
The 2D map is represented as a Grid with certain properties, which is
represented by a class named World, and then we define an object name myworld of
this class. The GSLparser will recognize and automatically perform the
simulation, until it stops (Sect.\ref{sec:GSL-when-to-stop}).
\begin{figure}[hbt]
\centerline{
\includegraphics[height=7cm,
angle=0]{./images/game-of-life-initial-configuration.eps}}
\caption{Size of the grid (100 x 100) and initial configuration of the Game of
Life} \label{fig:game-of-life-initial-configuration}
\end{figure}
\begin{verbatim}
Grid World
{
...
}
World myworld;
\end{verbatim}
Each grid has a given dimension, defined using \verb!Dimension()! statement.
{\small
\begin{verbatim}
// A grid is a collection of layers (GridLayerDescriptors.
// The dimensions of the grid is set at construction, however each layer
// may have its own density.
// What it looks like, the main responsibility of the grid is to figure
// out how to map node coordinates to one dimensional coordinates(nodeIndex)
// and vice versa.
// It also keeps track of the maximum and the minimum densities out of
// all the layers it has.
\end{verbatim}
}
\verb!./gsl/framework/parser/include/Grid.h! file
There can be as many layers mapping on the grid as you want, using Layer()
statements. Each Layer only contains instances of a given node-type only.
Here, we have a layer that contains instances of NodeType node-type.
We want to have one layer of LifeNode node-type uniformly distributed to every
cell with 1 instance of NodeType per cell.
\begin{verbatim}
Grid World
{
Layer (nodes, LifeNode, UniformLayout(1), <nodekind="Nodes">)
}
\end{verbatim}
Here, {\it UniformLayout} is a functor (Sect.\ref{sec:UniformLayoutFunctor}).
You will uses functors a lot to modify the behavior of the graph, to help organizing the functors to know if using a
functor at one place in the script is proper or not, we try to put a category
information to them, so that it can be checked. There are currently 11 different
categories of functors (Sect.\ref{sec:functors-in-MGS-framework}).
\label{sec:GameofLife-GSL-InitNodes}
Each cell is identified by its lower-left coordinate
(x,y). The initial configuration is given in
Fig.\ref{fig:game-of-life-initial-configuration}.
{\it
\begin{verbatim}
Grid World
{
InitNodes (.[25:75,25:75].Layer(nodes), Same( Pset<LifeNode, NodeInit> ) (<value=1>) );
InitNodes (.[40:60,40:60].Layer(nodes), Same( Pset<LifeNode, NodeInit> ) (<value=0>) );
}
\end{verbatim}
}
In a directed graph, node A does not need to know which nodes it connect to, but
node B needs to know which nodes it receives the input.
\begin{verbatim}
A ------> B
\end{verbatim}
Now, we need a LifeNode nodetype, so we use MDL to help creating this LifeNode
(Sect.\ref{sec:MDL-script-game-of-life}). The basic component of a graph is a
node. However, in addition to this, there can be other components. GSL system
defines 7 different components (Sect.\ref{sec:GSL-extension}). The MDL system
(Sect.\ref{sec:MDL}) is designed to help generating code for user-defined
components belonging to one of these components using MDL script
(Sect.\ref{sec:MDL-script-structure}).
\subsection{* MDL script}
\label{sec:MDL-script-game-of-life}
\begin{enumerate}
\item write *.mdl script: File.mdl (Sect.\ref{sec:MDL-script-structure})
and put them into \verb!./models/<ProjectName>/File.mdl!
Here: Create Life.mdl file written in MDL scripting language
(Sect.\ref{sec:MDL-script})
\item compile the script
\begin{verbatim}
cd ./models/<ProjectName>/
$NTSROOT/mdl/bin/mdlparser File.mdl
\end{verbatim}
Here:
\begin{verbatim}
cd ./models/Life/
$NTSROOT/mdl/bin/mdlparser Life.mdl
\end{verbatim}
Once the script complete, it also generate a number of C++ files and folders
inside the project folder. For example: GameofLife project
(Sect.\ref{sec:nts-example-game-of-life})
\begin{verbatim}
copyModules
Extensions.mk
LifeDataCollector/
Life.mdl
LifeNode/
ValueProducer/
\end{verbatim}
and then it automatically runs the script \verb!copyModules! to copy these
folders (containing C++ generated code) to the appropriate location in the \verb!./gsl/extension/!
\begin{verbatim}
./gsl/extension/interface/ValueProducer/
./gsl/extension/node/LifeNode/
./gsl/extension/variable/LifeDataCollector/
\end{verbatim}
\item jump to the different folders where the generated codes are copied where
the C++ code for the different components in the above script are generated, and modify them accordingly
\end{enumerate}
\verb!LifeNode! will be used as a node that can connect to other \verb!LifeNode!
and share the data via the Interface \verb!ValueProducer!'s data of \verb!int!
type (here we use pointer as we want direct reference and no data copy).
All \verb!LifeNode! instances reference to some common data, which is called
Shared data.
{\tiny
\begin{verbatim}
Interface ValueProducer {
int *value;
}
/* If a node want to share it data with other components
it needs to specify these data via the form of Interface
Here, LifeNode can expose data via the ValueProducer interface
*/
Node LifeNode Implements ValueProducer {
int value;
int publicValue;
int* [] neighbors;
Shared {
int habitable;
int tooSparse;
int tooCrowded;
}
InitPhase initialize;
RuntimePhase update();
RuntimePhase copy(publicValue);
ValueProducer.value << &publicValue; /* this line define what data attribute
of the node is available for other node
to access
*/
/* This defines how a node is connected to another node
Inside () we can define the predicate, i.e. the condition on which the
connection is established (here we ignore).
And what Interface it expects from the connected node, e.g.
ValueProducer interface. With this interface, what data it will get
(i.e. 'value'), and where it will store (i.e. an element in the
array 'neighbors')
*/
Connection Pre Node () Expects ValueProducer {
ValueProducer.value >> neighbors; /* this line defines how the data from
other nodes are collected. The data
attribute to store is typically an array
as it can get from multiple connected
nodes.
The size of this array is equal to the
number of connected nodes and is
determined at the time of the simulation
setup in GSL stage. So we just use []
*/
}
}
Variable LifeDataCollector
{
string fileName;
CoordsStruct [] coordsArray;
int* [] vals;
InitPhase initialize;
TriggeredFunction dataCollection;
}
\end{verbatim}
}
The Node names and Variable names will be put into the appropriate folders
\begin{verbatim}
$NTSROOT/gsl/extensions/node/
$NTSROOT/gsl/extensions/variable/
\end{verbatim}
Example: inside these folders are generated code that you need to modify
\begin{verbatim}
$NTSROOT/gsl/extensions/node/LifeNode/
... include/
... src/
... obj/
$NTSROOT/gsl/extensions/variable/LifeDataCollector/
... include/
... src/
... obj/
\end{verbatim}
\subsection{* generated C++ code: node/compcategory}
\label{sec:MGS_user-defined-class-nodetype-compcategory}
Sec.\ref{sec:CompCategoryBase} discusses NodeCompCategoryBase.
\begin{verbatim}
LifeNodeCompCategory
|
+- CG_LifeNodeCompCategory
|
friend CG_LifeNodeCompCategory
+- NodeCompCategoryBase
|
+- DistributableCompCategoryBase
+- NodeType
|
+-
\end{verbatim}
\subsection{* generated C++ code: node}
\label{sec:MGS_user-defined-class-nodetype}
As we define a Node name \verb!LifeNode!, there will be C++ code generated for
this node.
\begin{verbatim}
LifeNode
|
+- CG_LifeNode
|
friend CG_LifeNodePublisher
friend LifeNodeCompCategory
friend CG_LifeNodeCompCategory
+- <interface classes>
+- NodeBase
|
+- Node
|
+-
NodeDescriptor,
ServiceAcceptor,
TriggerableBase
\end{verbatim}
Here, \verb!NodeBase! is the class with important methods that we often use at
C++ code level (Sect.\ref{sec:NodeBase}).
We ignore \verb!CG_*! files, and need to modify
\verb!LifeNode.h.gen! \verb!LifeNode.C.gen!
{\tiny
\begin{verbatim}
tmhoangt:/home/tmhoangt/nts>vi gsl/extensions/node/LifeNode/include/
CG_LifeNodeCompCategory.h CG_LifeNodeProxyDemarshaller.h CG_LifeNodeWorkUnitInstance.h
CG_LifeNodeFactory.h CG_LifeNodeProxy.h CG_LifeNodeWorkUnitShared.h
CG_LifeNodeGridLayerData.h CG_LifeNodePSet.h LifeNodeCompCategory.h
CG_LifeNode.h CG_LifeNodePublisher.h LifeNodeCompCategory.h.gen
CG_LifeNodeInAttrPSet.h CG_LifeNodeSharedMembers.h LifeNode.h
CG_LifeNodeNodeAccessor.h CG_LifeNodeTriggerableCaller.h LifeNode.h.gen
CG_LifeNodeOutAttrPSet.h CG_LifeNodeWorkUnitGridLayers.h
\end{verbatim}
}
{\tiny
\begin{verbatim}
tmhoangt:/home/tmhoangt/nts>vi gsl/extensions/node/LifeNode/src/
CG_LifeNode.C CG_LifeNodeProxy.C CG_LifeNodeWorkUnitShared.C
CG_LifeNodeCompCategory.C CG_LifeNodePSet.C LifeNode.C
CG_LifeNodeFactory.C CG_LifeNodePublisher.C LifeNode.C.gen
CG_LifeNodeGridLayerData.C CG_LifeNodeSharedMembers.C LifeNodeCompCategory.C
CG_LifeNodeInAttrPSet.C CG_LifeNodeTriggerableCaller.C LifeNodeCompCategory.C.gen
CG_LifeNodeNodeAccessor.C CG_LifeNodeWorkUnitGridLayers.C
CG_LifeNodeOutAttrPSet.C CG_LifeNodeWorkUnitInstance.C
\end{verbatim}
}
\subsection{* generated C++ code: variable}
\label{sec:MGS_user-defined-class-variable}
As we define a Variable name \verb!LifeDataCollector!, there will be C++ code
generated for this Variable.
We ignore \verb!CG_*! files, and need to modify
\verb!LifeDataCollector.h.gen! \verb!LifeDataCollector.C.gen!
{\tiny
\begin{verbatim}
tmhoangt:/home/tmhoangt/nts>vi gsl/extensions/variable/LifeDataCollector/include/
CG_LifeDataCollectorCompCategory.h CG_LifeDataCollectorOutAttrPSet.h CG_LifeDataCollectorPublisher.h
CG_LifeDataCollectorFactory.h CG_LifeDataCollectorProxyDemarshaller.h CG_LifeDataCollectorTriggerableCaller.h
CG_LifeDataCollector.h CG_LifeDataCollectorProxy.h CG_LifeDataCollectorWorkUnitInstance.h
CG_LifeDataCollectorInAttrPSet.h CG_LifeDataCollectorPSet.h LifeDataCollector.h
\end{verbatim}
}
\subsection{What is a trigger?}
\label{sec:Triggerable-MGS}
A Trigger-able object is an object that get called at every time-step, and
if the 'trigger'-condition meets, the trigger-event will be done.
The two common trigger-event are (1) pause the program, (2) stop the program.
To create a Trigger-able object, it needs to be an instance of a class
Triggerable class.
MGS provides the facility to help creating a Triggerable class.
MGS utilizes 4 different classes that help managing a Trigger-type class
\begin{enumerate}
\item Trigger
\item TriggerType
\item Triggerable
\item TriggerableCaller
\end{enumerate}
Class hierarchy:
\begin{verbatim}
Trigger
|
TriggerBase
|
UnsignedTrigger
IntTrigger
TriggerType
|
UnsignedTriggerDescriptor
TriggerableCaller
|
StopperEvent
PauseEvent
Triggerable
|
TriggerableBase
{ private:
DuplicatePointerArray<NDPairList> _ndPairLists;
std::vector<Trigger*> _triggers;
}
|
Stopper
{
private:
Simulation& sim;
std::list<Trigger*> _triggerList;
}
Pauser
{private:
Simulation& sim;
}
\end{verbatim}
\textcolor{red}{A user-defined Triggerable class must} define 2 classes:
\begin{enumerate}
\item derived from \verb!TriggerBase! class (Sect.\ref{sec:TriggerBase})
\item implement the \verb!conditionalFire()! method to define when the event
got triggered.
\begin{verbatim}
if (status()) {
std::vector<TriggerableCaller*>::iterator
it, end = _serialTriggerableCallers.end();
for (it = _serialTriggerableCallers.begin(); it != end; ++it) {
(*it)->event(this);
}
end = _parallelTriggerableCallers.end();
for (it = _parallelTriggerableCallers.begin(); it != end; ++it) {
(*it)->event(this);
}
}
\end{verbatim}
\item implement the \verb!status()! method
\item define a class derived from \verb!TriggerableCaller! class
(Sect.\ref{sec:TriggerableCaller})
Example: \verb!Stopper! class (Sect.\ref{sec:Stopper}), \verb!Pauser! class
(Sect.\ref{sec:Pauser}), LENSServer (Sect.\ref{sec:LENSServer}).
\item implement the \verb!event()! method
\end{enumerate}
MGS provides a set of
\subsection{CG\_.. .Publisher: expose data member as a service}
\label{sec:CG_xxxPublisher}
C++ code, \verb!GenericService! class is used:
\begin{lstlisting}
Service* CG_LifeNodePublisher::createService(const std::string& serviceRequested)
{
Service* rval = 0;
if (serviceRequested == "value") {
rval = new GenericService< int >(_data, &(_data->value));
_services.push_back(rval);
return rval;
}
if (serviceRequested == "publicValue") {
rval = new GenericService< int >(_data, &(_data->publicValue));
_services.push_back(rval);
return rval;
}
if (serviceRequested == "neighbors") {
rval = new GenericService< ShallowArray< int* > >(_data, &(_data->neighbors));
_services.push_back(rval);
return rval;
}
if (serviceRequested == "tooCrowded") {
rval = new GenericService< int >(_data, &(_data->getNonConstSharedMembers().tooCrowded));
_services.push_back(rval);
return rval;
}
if (serviceRequested == "tooSparse") {
rval = new GenericService< int >(_data, &(_data->getNonConstSharedMembers().tooSparse));
_services.push_back(rval);
return rval;
}
if (serviceRequested == "habitable") {
rval = new GenericService< int >(_data, &(_data->getNonConstSharedMembers().habitable));
_services.push_back(rval);
return rval;
}
return rval;
}
\end{lstlisting}
% \subsection{- Service}
% \label{sec:Service-MGS}
%
% This is handled by an intermediate class, called \verb!CG_...Publisher!, e.g.
% \verb!CG_LifeNodePublisher!, and the service is represented by a \verb!Service!
% class (Sect.\ref{sec:Service}).
\subsection{- Node's data member is a service}
\label{sec:Node-data-member-as-a-service}
So, for any NodeType, its' data members (including shared data members) are
treated as a service (Sect.\ref{sec:Service}). Access to any services is managed
by an instance of ServiceDescriptor class (Sect.\ref{sec:ServiceDescriptor}).
\begin{enumerate}
\item The \verb!CG_<NodeTypeName>.h! file: provide \verb!getServiceName()!
method, which return the service name (as a string), based upon the memory
reference to the data.
\item The \verb!<CG_<NodeTypeName>Publisher.C! file:
\end{enumerate}
\begin{verbatim}
// in MDL
Node LifeNode Implements ValueProducer {
int value;
int publicValue;
int* [] neighbors;
Shared {
int tooSparse;
int tooCrowded;
}
}
\end{verbatim}
then it is mapped to \verb!CG_LifeNodePublisher! class with
{\tiny
\begin{lstlisting}
class CG_LifeNodePublisher : public GeneratedPublisherBase
{
private:
CG_LifeNode * _data;
static std::vector<ServiceDescriptor> _serviceDescriptor;
}
CG_LifeNodePublisher::CG_LifeNodePublisher(Simulation& sim, CG_LifeNode* data)
: GeneratedPublisherBase(sim), _data(data)
{
if (_serviceDescriptors.size() == 0) {
_serviceDescriptors.push_back(ServiceDescriptor("value", "", "int"));
_serviceDescriptors.push_back(ServiceDescriptor("publicValue", "", "int"));
_serviceDescriptors.push_back(ServiceDescriptor("neighbors", "", "ShallowArray< int* >"));
_serviceDescriptors.push_back(ServiceDescriptor("tooCrowded", "", "int"));
_serviceDescriptors.push_back(ServiceDescriptor("tooSparse", "", "int"));
_serviceDescriptors.push_back(ServiceDescriptor("habitable", "", "int"));
}
}
\end{lstlisting}
}
NOTE: Currently, the description for the service's of all data member name is
empty. \textcolor{red}{How to passs the descriptor information???} - not sure.
An existing example: The Simulation class provide a built-in service called
\verb!Iteration! which can be accessed in GSL using \verb!::Iteration!
(Sect.\ref{sec:service-how-to-declare-in-GSL}).
\begin{enumerate}
\item \verb!Simulation.h! file:
Here, \verb!::declarator! is used as \verb!::Interation!
\item \verb!SimulationPublisher.c! file:
\end{enumerate}
\subsection{CG\_xxx}
\label{sec:CG_xxx}
The code-generated class \verb!CG_xxx! for the user-defined class \verb!xxx! is designed for a purpose
\begin{enumerate}
\item contains method for the instance of the class \verb!xxx! to connect with a different instance of the same or a different class
\begin{verbatim}
addPreVariable(...)
addPostVariable(...)
...
\end{verbatim}
\item \verb!CG_get_<interfaceClassName>_<data_member_name>!: code-generated
methods that return reference to individual data member
\item \verb!getServiceName(...)!: code-generated method that return reference
to the desired data member, based on the string-name representation of the
data member
NOTE: Some service has a string descriptor, which can be retrived using
\verb!getServiceDescription! - for data member as a service, the string
descriptor is empty.
\end{enumerate}
\subsection{CG\_.. .SharedMembers}
\label{sec:CG_xxxSharedMembers}
All data member declared inside the Shared region is defined in this class,
e.g. \verb!CG_LifeNodeSharedMembers!.
Also, any phases function defined inside the Shared region are defined here.
\begin{lstlisting}
class CG_LifeNodeSharedMembers
{
public:
virtual void setUp(const NDPairList& ndplist);
CG_LifeNodeSharedMembers();
virtual ~CG_LifeNodeSharedMembers();
virtual void duplicate(std::auto_ptr<CG_LifeNodeSharedMembers>& dup) const;
int tooCrowded;
int tooSparse;
int habitable;
};
\end{lstlisting}
\subsection{CG\_.. .CompCategory}
\label{sec:CG_xxxCompCategory}
\label{sec:CG_xxxCompCategory::setDistributionTemplates()}
This class is critical, as it keeps tracks of all instances of a given NodeType
(or VariableType, ConstantType) defined in a simulation as a \verb!_nodes!
array.
\begin{lstlisting}
class CG_LifeNodeCompCategory: public NodeCompCategoryBase{
protected:
// blockSize = 1000, blockIncrementsize=4
ShallowArray<LifeNode, 1000, 4> _nodes;
ConnectionIncrement _computeCost;
#ifdef HAVE_MPI
std::map <int, CCDemarshaller*> _demarshallerMap;
std::map <int, CCDemarshaller*>::iterator _demarshallerMapIter;
std::map <int, ShallowArray<CG_LifeNode*> > _sendMap;
std::map <int, ShallowArray<CG_LifeNode*> >::iterator _sendMapIter;
#endif
private:
#ifdef HAVE_MPI
std::map<std::string, CG_T_SendFunctionPtr> CG_sendTemplates;
std::map<std::string, CG_T_GetSendTypeFunctionPtr> CG_getSendTypeTemplates;
#endif
}
class NodeCompCategoryBase : public DistributableCompCategoryBase, public NodeType
{
#ifdef HAVE_MPI
virtual void allocateProxy(int partitionId, NodeDescriptor* nd) = 0;
virtual void addToSendMap(int partitionId, Node* node) = 0;
#endif
protected:
// Initialization time Single Threaded
std::deque<GridLayerData*> _gridLayerDataList;
// Run time Multi Threaded
GridLayerData** _gridLayerDataArray;
int _gridLayerDataArraySize;
NodePartitionItem* _partitions;
int _nbrPartitions;
std::string _modelName;
}
class DistributableCompCategoryBase :
#ifdef HAVE_MPI
public IndexedBlockCreator,
#endif
public CompCategoryBase
{
#ifdef HAVE_MPI
virtual void setDistributionTemplates() = 0;
virtual void resetSendProcessIdIterators() = 0;
virtual int getSendNextProcessId() = 0;
virtual bool atSendProcessIdEnd() = 0;
virtual void resetReceiveProcessIdIterators() = 0;
virtual int getReceiveNextProcessId() = 0;
virtual bool atReceiveProcessIdEnd() = 0;
virtual void send(int, OutputStream* ) = 0;
virtual Demarshaller* getDemarshaller(int pid) = 0;
virtual int getIndexedBlock(std::string phaseName, int dest, MPI_Datatype* blockType, MPI_Aint& blockLocation) = 0; // sendBlock
virtual IndexedBlockCreator* getReceiveBlockCreator(int pid) = 0;
#endif
}
\end{lstlisting}
\subsection{-- getIndexedBlock()}
\label{sec:CG_xxxCompCategory::getIndexedBlock}
The method \verb!CG_xxxCompCategory::getIndexedBlock()! returns the number of
bytes of data to be exchanged in total, for that particular modeltype, for a
given phaseName.
NOTE: \verb!CG_getSendTypeTemplates! are created by
\verb!setDistributionTemplate()! which basically maps from the phaseName, e.g.
'initialize' to the true function
\begin{verbatim}
typedef void (CG_LifeNode::*CG_T_GetSendTypeFunctionPtr)( std::vector<int>&,
std::vector<MPI_Aint>&) const;
// the pointer to the function that return the types of all data to be sent
// for the given kernel phase (with name as key in this map)
std::map<std::string, CG_T_GetSendTypeFunctionPtr> CG_getSendTypeTemplates;
// the pointer to the function that does the marshalling of data
std::map<std::string, CG_T_SendFunctionPtr> CG_sendTemplates;
CG_LifeNodeCompCategory::setDistributionTemplates()
{
CG_sendTemplates["FLUSH_LENS"] = &CG_LifeNode::CG_send_FLUSH_LENS;
CG_getSendTypetemplates["FLUSH_LENS"] =
&CG_LifeNode::CG_getSendType_FLUSH_LENS;
for (it = _phaseMappings.begin(); ...)
{
for (it->second->getName() == getSimulationPhaseName("initialize")
{
CG_sendTemplates[it->second->getName()] =
&CG_LifeNode::CG_send_initialize;
CG_getSendTypeTemplates[it->second->getName()] =
&CG_LifeNode::CG_getSendType_initialize;
}
}
}
CG_LifeNode::CG_send_initialize(OutputStream* stream) const
{
MarshallerInstance<ShallowArray<int> > mi0;