-
Notifications
You must be signed in to change notification settings - Fork 6
/
memory.tmpl
1461 lines (1311 loc) · 41.4 KB
/
memory.tmpl
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
/**
* \file memory.c
* \brief Holds memory functions.
*/
#include "header.h"
<?foreach xagent?><?foreach function?>
void unittest_$name_$current_state_$next_state()
{
<?if function_input?>int rc;<?end if?>
<?foreach function_input?>
<?if filter?><?if nottree?>rc = MB_Iterator_CreateFiltered(b_$name, &i_$name, &$filter, current_xmachine_$agent_name);<?end if?><?end if?>
<?if no_filter?>rc = MB_Iterator_Create(b_$name, &i_$name);<?end if?>
#ifdef ERRCHECK
if (rc != MB_SUCCESS)
{
fprintf(stderr, "ERROR: Could not create Iterator for '$name'\n");
switch(rc) {
case MB_ERR_INVALID:
fprintf(stderr, "\t reason: '$name' board is invalid\n");
break;
case MB_ERR_LOCKED:
fprintf(stderr, "\t reason: '$name' board is locked\n");
break;
case MB_ERR_MEMALLOC:
fprintf(stderr, "\t reason: out of memory\n");
break;
case MB_ERR_INTERNAL:
fprintf(stderr, "\t reason: internal error. Recompile libmoard in debug mode for more info \n");
break;
}
}
#endif
<?end foreach?>
//return $name();
}
<?end foreach?><?end foreach?>
void free_messages()
{
<?foreach message?><?if first?>int rc;
<?end if?>
rc = MB_Clear(b_$name);
#ifdef ERRCHECK
if (rc != MB_SUCCESS)
{
fprintf(stderr, "ERROR: Could not clear '$name' board\n");
switch(rc) {
case MB_ERR_INVALID:
fprintf(stderr, "\t reason: '$name' board is invalid\n");
break;
case MB_ERR_LOCKED:
fprintf(stderr, "\t reason: '$name' board is locked\n");
break;
case MB_ERR_INTERNAL:
fprintf(stderr, "\t reason: internal error. Recompile libmoard in debug mode for more info \n");
break;
default:
fprintf(stderr, "\t MB_Clear returned error code: %d (see libmboard docs for details)\n", rc);
break;
}
<?if parallel?>MPI_Abort(MPI_COMM_WORLD, rc);<?end if?>
exit(rc);
}
#endif
<?end foreach?>
}
/** \fn void initialise_pointers()
* \brief Initialises pointers to xmachine, message, and node lists.
*/
void initialise_pointers()
{
<?foreach message?><?if first?>int rc;
<?end if?>
/* Initialise message sync composite params as NULL */
FLAME_m_$name_composite_params = NULL;
rc = MB_Create(&b_$name, sizeof(m_$name));
#ifdef ERRCHECK
if (rc != MB_SUCCESS)
{
fprintf(stderr, "ERROR: Could not create '$name' board\n");
switch(rc) {
case MB_ERR_INVALID:
fprintf(stderr, "\t reason: Invalid message size\n");
break;
case MB_ERR_MEMALLOC:
fprintf(stderr, "\t reason: out of memory\n");
break;
case MB_ERR_INTERNAL:
fprintf(stderr, "\t reason: internal error. Recompile libmoard in debug mode for more info \n");
break;
default:
fprintf(stderr, "\t MB_Create returned error code: %d (see libmboard docs for details)\n", rc);
break;
}
<?if parallel?>MPI_Abort(MPI_COMM_WORLD, rc);<?end if?>
exit(rc);
}
#endif
<?end foreach?><?foreach xagent?><?foreach state?>
$agent_name_$name_state = init_$agent_name_state();
<?end foreach?><?end foreach?>
temp_node_info = NULL;
p_node_info = &temp_node_info;
}
/** \fn void initialise_unit_testing()
* \brief Initialises framework for unit testing.
*/
void initialise_unit_testing()
{
int rc;
rc = MB_Env_Init();
#ifdef ERRCHECK
if (rc != MB_SUCCESS)
{
fprintf(stderr, "ERROR: Failed to initialise Message Board environment\n");
switch(rc) {
case MB_ERR_MPI:
fprintf(stderr, "\t reason: MPI library not initialised\n");
break;
case MB_ERR_MEMALLOC:
fprintf(stderr, "\t reason: out of memory\n");
break;
default:
fprintf(stderr, "\t MB_Env_Init returned error code: %d (see libmboard docs for details)\n", rc);
break;
}
<?if parallel?>MPI_Abort(MPI_COMM_WORLD, rc);<?end if?>
exit(rc);
}
#endif
initialise_pointers();
}
FLAME_output * add_FLAME_output(FLAME_output ** outputs)
{
FLAME_output * current;
current = (FLAME_output *)malloc(sizeof(FLAME_output));
CHECK_POINTER(current);
current->next = *outputs;
*outputs = current;
current->type = -1;
current->format = -1;
current->location = NULL;
current->period = -1;
current->phase = -1;
current->flag = 0;
return current;
}
void free_FLAME_outputs(FLAME_output ** outputs)
{
FLAME_output * current, * next;
current = *outputs;
while(current)
{
next = current->next;
free(current->location);
free(current);
current = next;
}
*outputs = NULL;
}
/* add_location */
/** \fn void add_location(double point, location ** p_location)
* \brief Adds a location in order into the location list.
* \param point Position of an agent.
* \param p_location Pointer Pointer to the location list.
*/
void add_location(double point, location ** p_location)
{
location * current = *p_location;
location * tmp = NULL;
location * newlocation = NULL;
int found = 0;
while(found == 0)
{
if(current == NULL) found = 1;
else if(point > current->point) found = 1;
else
{
tmp = current;
current = current->next;
}
}
newlocation = (location *)malloc(sizeof(location));
CHECK_POINTER(newlocation);
if(tmp)
{
tmp->next = newlocation;
}
else
{
*p_location = newlocation;
}
newlocation->next = current;
newlocation->point = point;
}
/* freelocations */
/** \fn void freelocations(location ** p_location)
* \brief Free locations from the location list.
* \param p_location Pointer Pointer to the location list.
*/
void freelocations(location ** p_location)
{
location * head = *p_location;
location * tmp = NULL;
while(head)
{
tmp = head->next;
free(head);
head = tmp;
}
*p_location = NULL;
}
void init_int_static_array(/*@out@*/ int * array, int size)
{
int i;
for(i = 0; i < size; i++) array[i] = 0;
}
void init_float_static_array(/*@out@*/ float * array, int size)
{
int i;
for(i = 0; i < size; i++) array[i] = 0.0;
}
void init_double_static_array(/*@out@*/ double* array, int size)
{
int i;
for(i = 0; i < size; i++) array[i] = 0.0;
}
void init_char_static_array(/*@out@*/ char * array, int size)
{
int i;
for(i = 0; i < size; i++) array[i] = '\0';
}
<?foreach datatype?>
void init_$name(/*@out@*/ $name * temp)
{
<?foreach datatypevar?><?if modeldatatype?><?if not_array?> init_$type(&(*temp).$name);
<?end if?><?end if?><?if static_array?> init_$type_static_array((*temp).$name, $arraylength);
<?end if?><?if dynamic_array?> init_$type(&(*temp).$name);
<?end if?><?if not_modeldatatype?><?if not_array?> (*temp).$name = $default_value;
<?end if?><?end if?><?end foreach?>
}
void init_$name_static_array(/*@out@*/ $name * array, int size)
{
int i;
for(i = 0; i < size; i++) init_$name(&array[i]);
}
void free_$name($name * temp)
{
<?foreach datatypevar?><?if modeldatatype?><?if not_array?> free_$type(&(*temp).$name);
<?end if?><?if static_array?> free_$type_static_array((*temp).$name, $arraylength);
<?end if?><?end if?><?if dynamic_array?> free_$type(&(*temp).$name);
<?end if?><?end foreach?>
}
void free_$name_static_array($name * array, int size)
{
int i;
for(i = 0; i < size; i++) free_$name(&array[i]);
}
void copy_$name($name * from, $name * to)
{
<?foreach datatypevar?> <?if modeldatatype?><?if not_array?>copy_$type(&(*from).$name, &(*to).$name);
<?end if?><?if dynamic_array?>copy_$type(&(*from).$name, &(*to).$name);
<?end if?><?if static_array?>copy_$type_static_array((*from).$name, (*to).$name, $arraylength);
<?end if?><?end if?><?if not_modeldatatype?><?if not_array?>(*to).$name = (*from).$name;
<?end if?><?if dynamic_array?>copy_$type(&(*from).$name, &(*to).$name);
<?end if?><?if static_array?>memcpy((*to).$name, (*from).$name, $arraylength*sizeof($type));
<?end if?><?end if?><?end foreach?>}
void copy_$name_static_array($name * from, $name * to, int size)
{
int i;
for(i = 0; i < size; i++)
{
copy_$name(&from[i], &to[i]);
}
}
<?end foreach?>
<?foreach xagent?>
xmachine_memory_$name_state * init_$name_state()
{
xmachine_memory_$name_state * current = (xmachine_memory_$name_state *)malloc(sizeof(xmachine_memory_$name_state));
CHECK_POINTER(current);
current->agents = NULL;
current->count = 0;
return current;
}
xmachine_memory_$name * init_$name_agent()
{
xmachine_memory_$name * current = (xmachine_memory_$name *)malloc(sizeof(xmachine_memory_$name));
CHECK_POINTER(current);
<?foreach xagentvar?><?if modeldatatype?><?if not_array?> init_$type(¤t->$name);
<?end if?><?end if?><?if static_array?> init_$type_static_array(current->$name, $arraylength);
<?end if?><?if dynamic_array?> init_$type(¤t->$name);
<?end if?><?if not_modeldatatype?><?if not_array?> current->$name = $default_value;
<?end if?><?end if?><?end foreach?>
return current;
}
void free_$name_agent(xmachine_memory_$name_holder * tmp, xmachine_memory_$name_state * state)
{
if(tmp->prev == NULL) state->agents = tmp->next;
else tmp->prev->next = tmp->next;
if(tmp->next != NULL) tmp->next->prev = tmp->prev;
<?foreach xagentvar?><?if modeldatatype?><?if not_array?>free_$type(&tmp->agent->$name);
<?end if?><?if static_array?>free_$type_static_array(tmp->agent->$name, $arraylength);
<?end if?><?end if?><?if dynamic_array?>free_$type(&tmp->agent->$name);
<?end if?><?end foreach?>
free(tmp->agent);
free(tmp);
}
void unittest_init_$name_agent()
{
current_xmachine_$name = (xmachine_memory_$name *)malloc(sizeof(xmachine_memory_$name));
CHECK_POINTER(current);
<?foreach xagentvar?><?if modeldatatype?><?if not_array?> init_$type(¤t_xmachine_$agent_name->$name);
<?end if?><?end if?><?if static_array?> init_$type_static_array(current_xmachine_$agent_name->$name, $arraylength);
<?end if?><?if dynamic_array?> init_$type(¤t_xmachine_$agent_name->$name);
<?end if?><?if not_modeldatatype?><?if not_array?> current_xmachine_$agent_name->$name = $default_value;
<?end if?><?end if?><?end foreach?>
}
void unittest_free_$name_agent()
{
<?foreach xagentvar?><?if modeldatatype?><?if not_array?>free_$type(¤t_xmachine_$agent_name->$name);
<?end if?><?if static_array?>free_$type_static_array(current_xmachine_$agent_name->$name, $arraylength);
<?end if?><?end if?><?if dynamic_array?>free_$type(¤t_xmachine_$agent_name->$name);
<?end if?><?end foreach?>
free(current_xmachine_$name);
}
void free_$name_agents()
{
<?foreach state?> current_xmachine_$agent_name_holder = $agent_name_$name_state->agents;
while(current_xmachine_$agent_name_holder)
{
temp_xmachine_$agent_name_holder = current_xmachine_$agent_name_holder->next;
free_$agent_name_agent(current_xmachine_$agent_name_holder, $agent_name_$name_state);
current_xmachine_$agent_name_holder = temp_xmachine_$agent_name_holder;
}
$agent_name_$name_state->count = 0;
<?end foreach?>}
void free_$name_states()
{
<?foreach state?> free($agent_name_$name_state);
<?end foreach?>}
void transition_$name_agent(xmachine_memory_$name_holder * tmp, xmachine_memory_$name_state * from_state, xmachine_memory_$name_state * to_state)
{
if(tmp->prev == NULL) from_state->agents = tmp->next;
else tmp->prev->next = tmp->next;
if(tmp->next != NULL) tmp->next->prev = tmp->prev;
add_$name_agent_internal(tmp->agent, to_state);
free(tmp);
}
void add_$name_agent_internal(xmachine_memory_$name * agent, xmachine_memory_$name_state * state)
{
xmachine_memory_$name_holder * current = (xmachine_memory_$name_holder *)malloc(sizeof(xmachine_memory_$name_holder));
CHECK_POINTER(current);
current->next = state->agents;
current->prev = NULL;
state->agents = current;
if(current->next != NULL) current->next->prev = current;
current->agent = agent;
state->count++;
<?if parallel?> current_node->agent_total++;<?end if?>
}
/** \fn void add_$name_agent(<?foreach xagentvar?>$type <?if not_modeldatatype?><?if dynamic_array?>* <?end if?><?end if?><?if modeldatatype?><?if static_array?>*<?end if?>* <?end if?>$name<?if not_modeldatatype?><?if static_array?>[]<?end if?><?end if?><?if notlast?>, <?end if?><?end foreach?>)
* \brief Add $name X-machine to the current being used X-machine list.
<?foreach xagentvar?> * \param $name Variable for the X-machine memory.
<?end foreach?> */
void add_$name_agent(<?foreach xagentvar?>$type <?if dynamic_array?>* <?end if?>$name<?if static_array?>[]<?end if?><?if notlast?>, <?end if?><?end foreach?>)
{
xmachine_memory_$name * current;
current = init_$name_agent();
/* new line added to handle dynamic agent creation*/
current_xmachine_$name_next_state = $name_$start_state_state;
add_$name_agent_internal(current, current_xmachine_$name_next_state);
<?foreach xagentvar?> <?if modeldatatype?><?if not_array?>copy_$type(&$name, ¤t->$name);
<?end if?><?if dynamic_array?>copy_$type($name, ¤t->$name);
<?end if?><?if static_array?>copy_$type_static_array($name, current->$name, $arraylength);
<?end if?><?end if?><?if not_modeldatatype?><?if not_array?>current->$name = $name;
<?end if?><?if dynamic_array?>copy_$type($name, ¤t->$name);
<?end if?><?if static_array?>memcpy(current->$name, $name, $arraylength*sizeof($type));
<?end if?><?end if?><?end foreach?>}
<?end foreach?>
/* freexmachines */
/** \fn void freexmachines()
* \brief Free the currently being used X-machine list.
*/
void freexmachines()
{
<?foreach xagent?>free_$name_agents();
<?end foreach?>
}
<?foreach allvar?><?if not_modeldatatype?><?if not_array?>
/** \fn void set_$name($type $name)
* \brief Set $name memory variable for current X-machine.
* \param $name New value for variable.
*/
void set_$name($type <?if dynamic_array?>* <?end if?><?if static_array?>*<?end if?>$name<?if static_array?>[]<?end if?>)
{
<?foreach xagent?><?if allvar_in_agent?> if(current_xmachine->xmachine_$name)
{
assert($allvar_name < FLAME_DOUBLE_MAX);
assert($allvar_name > FLAME_DOUBLE_MIN);
(*current_xmachine->xmachine_$name).$allvar_name = $allvar_name;
}
<?end if?><?end foreach?>}
<?end if?><?end if?>
/** \fn $type get_$name()
* \brief Get $name memory variable from current X-machine.
* \return Value for variable.
*/
$type <?if modeldatatype?>* <?end if?><?if not_modeldatatype?><?if array?>* <?end if?><?end if?>get_$name()
{
<?foreach xagent?><?if allvar_in_agent?> if(current_xmachine->xmachine_$name) return <?if modeldatatype?><?if not_static_array?>&<?end if?><?end if?><?if not_modeldatatype?><?if dynamic_array?>&<?end if?><?end if?>(*current_xmachine->xmachine_$name).$allvar_name;
<?end if?><?end foreach?>
// suppress compiler warning by returning dummy value /
// this statement should rightfully NEVER be reached /
return <?if not_array?><?if not_modeldatatype?>($type)0<?end if?><?end if?><?if modeldatatype?>NULL<?end if?><?if not_modeldatatype?><?if array?>NULL<?end if?><?end if?>;
}
<?end foreach?>
/** \fn double agent_get_range()
* \brief Fixed routine to get the range from current X-machine
* \return Value of range
*/
double agent_get_range()
{
double value = 0.0;
<?foreach xagent?> /*if (current_xmachine->xmachine_$name) value = current_xmachine->xmachine_$name->$rangevar;*/
<?end foreach?>
return value;
}
/** \fn int agent_get_id()
* \brief Fixed routine to get the id for the current X-machine
* \return Value of agent id
*/
int agent_get_id()
{
int value = 0;
<?foreach xagent?> /*if (current_xmachine->xmachine_$name) value = current_xmachine->xmachine_$name->$idvar;*/
<?end foreach?>
return value;
}
/** \fn double agent_get_x()
* \brief Fixed routine to get the x coordinate from current X-machine
* \return Value of x coordinate
*/
double agent_get_x()
{
double value = 0.0;
<?foreach xagent?> /*if (current_xmachine->xmachine_$name) value = current_xmachine->xmachine_$name->$xvar;*/
<?end foreach?>
return value;
}
/** \fn double agent_get_y()
* \brief Fixed routine to get the y coordinate from current X-machine
* \return Value of y coordinate
*/
double agent_get_y()
{
double value = 0.0;
<?foreach xagent?> /*if (current_xmachine->xmachine_$name) value = current_xmachine->xmachine_$name->$yvar;*/
<?end foreach?>
return value;
}
/** \fn double agent_get_z()
* \brief Fixed routine to get the z coordinate from current X-machine
* \return Value of z coordinate
*/
double agent_get_z()
{
double value = 0.0;
<?foreach xagent?><?if use_zvar?> /*if (current_xmachine->xmachine_$name) value = current_xmachine->xmachine_$name->$zvar;*/
<?end if?><?end foreach?>
return value;
}
/** \fn void add_node(int node_id, double minx, double maxx, double miny, double maxy, double minz, double maxz)
* \brief Add a node to the node list.
* \param node_id The node ID.
* \param minx The minumum value on the x-axis of the bounding volume.
* \param maxx The maximum value on the x-axis of the bounding volume.
* \param miny The minumum value on the y-axis of the bounding volume.
* \param maxy The maximum value on the y-axis of the bounding volume.
* \param minz The minumum value on the z-axis of the bounding volume.
* \param maxz The maximum value on the z-axis of the bounding volume.
*/
void add_node(int node_id, double minx, double maxx, double miny, double maxy, double minz, double maxz)
{
node_information * current = *p_node_info;
node_information * tmp = NULL;
while(current)
{
tmp = current;
current = current->next;
}
current = (node_information *)malloc(sizeof(node_information));
CHECK_POINTER(current);
<?if parallel?> if(node_id == node_number) current_node = current;<?end if?>
if(tmp)
{
tmp->next = current;
}
else
{
*p_node_info = current;
}
current->next = NULL;
current->node_id = node_id;
current->agents_in_halo = 0;
current->agent_total = 0;
current->agents = NULL;
<?foreach message?> current->$name_messages = NULL;
<?end foreach?>
<?if parallel?><?foreach message?> current->$name_message_no = 0;
<?end foreach?><?foreach xagent?> current->$name_agents = NULL;
current->$name_agent_no = 0;
<?end foreach?>
<?end if?>
current->partition_data[0] = minx;
current->partition_data[1] = maxx;
current->partition_data[2] = miny;
current->partition_data[3] = maxy;
current->partition_data[4] = minz;
current->partition_data[5] = maxz;
}
/**\fn void free_node_info()
* \brief Free the node list.
*/
void free_node_info()
{
node_information * tmp, * head;
head = *p_node_info;
while(head)
{
tmp = head->next;
free(head);
head = tmp;
}
*p_node_info = NULL;
}
/** \fn void clean_up(int code)
* \brief Add a node to the node list.
* \param code The error code (zero is no error).
*/
void clean_up(int code)
{
int rc;
FILE *file;
char data[100];
free(current_xmachine);
/* Free x-machine memory */
freexmachines();
/* Free space partitions linked list */
free_node_info();
/* Free output list */
free_FLAME_outputs(&FLAME_outputs);
/* Free agent states */
<?foreach xagent?> free_$name_states();
<?end foreach?>
/* Free index maps */
<?foreach constant_filter_variable?>
rc = MB_IndexMap_Delete(&FLAME_map_$agent_name_$name);
if (rc != MB_SUCCESS)
{
fprintf(stderr, "ERROR: Could not delete map of $agent_name $name\n");
/* Add reason for error using error codes here */
<?if parallel?>MPI_Abort(MPI_COMM_WORLD, rc);<?end if?>
exit(rc);
}
<?end foreach?>
/* Free message boards */
<?foreach message?>
rc = MB_Delete(&b_$name);
#ifdef ERRCHECK
if (rc != MB_SUCCESS)
{
fprintf(stderr, "ERROR: Could not delete '$name' board\n");
switch(rc) {
case MB_ERR_INVALID:
fprintf(stderr, "\t reason: '$name' board has not been created?\n");
break;
case MB_ERR_LOCKED:
fprintf(stderr, "\t reason: '$name' board is locked\n");
break;
case MB_ERR_INTERNAL:
fprintf(stderr, "\t reason: internal error. Recompile libmoard in debug mode for more info \n");
break;
default:
fprintf(stderr, "\t MB_Delete returned error code: %d (see libmboard docs for details)\n", rc);
break;
}
<?if parallel?>MPI_Abort(MPI_COMM_WORLD, rc);<?end if?>
exit(rc);
}
#endif
<?end foreach?>
rc = MB_Env_Finalise();
#ifdef ERRCHECK
if (rc != MB_SUCCESS)
{
fprintf(stderr, "ERROR: Could not finalise MB environment\n");
switch(rc) {
case MB_ERR_ENV:
fprintf(stderr, "\t reason: MB environment not yet started?\n");
break;
default:
fprintf(stderr, "\t MB_Env_Finalise returned error code: %d (see libmboard docs for details)\n", rc);
break;
}
<?if parallel?>MPI_Abort(MPI_COMM_WORLD, rc);<?end if?>
exit(rc);
}
#endif
<?if parallel?>
/* Clear MPI_Datatypes */
MPI_Type_free(&spacePartitionType);
<?foreach xagent?>
/* MPI_Type_free(&xmachine$nameType); */
<?end foreach?>
<?foreach message?>
/* MPI_Type_free(&message$nameType); */
<?end foreach?>
/* MPI finalise routine */
MPI_Finalize();
if(node_number == 0)
{
<?end if?>
/* Write log file */
sprintf(data, "%slog.xml", outputpath);
file = fopen(data, "a");
fputs("<!-- <totaltime> unit: seconds -->\n", file);
fputs("<totaltime>", file);
sprintf(data, "%.3f", total_time);
fputs(data, file);
fputs("</totaltime>\n", file);
/*fputs("<totalmessages>", file);
sprintf(data, "%i", total_messages);
fputs(data, file);
fputs("</totalmessages>", file);*/
fputs("</model_run>", file);
fclose(file);
<?if parallel?> }<?end if?>
if(code != 0)
{
printf("*** Error: ");
if(code == 100) printf("cannot handle specified number of space partitions");
if(code == 101) printf("could not find number of space partitions config in file");
printf(" ***");
exit(0);
}
}
<?if parallel?>
/** \fn void propagate_agents()
* \brief Check agent positions to see if any need to be moved to a another node.
*/
void propagate_agents()
{
/* node_information * node_info;
xmachine * before_xmachine, * temp_xmachine;
xmachine ** p_temp_xmachine;
double x_xmachine, y_xmachine, z_xmachine;
current_xmachine = *p_xmachine;
before_xmachine = NULL;
while(current_xmachine)
{
<?foreach xagent?> <?if notfirst?>else <?end if?>if(current_xmachine->xmachine_$name != NULL)
{
x_xmachine = current_xmachine->xmachine_$name->$xvar;
y_xmachine = current_xmachine->xmachine_$name->$yvar;
<?if use_zvar?> z_xmachine = current_xmachine->xmachine_$name->$zvar;<?end if?><?if no_zvar?> z_xmachine = 0.0;<?end if?>
}
<?end foreach?>
if(x_xmachine < current_node->partition_data[0] ||
x_xmachine > current_node->partition_data[1] ||
y_xmachine < current_node->partition_data[2] ||
y_xmachine > current_node->partition_data[3] ||
z_xmachine < current_node->partition_data[4] ||
z_xmachine > current_node->partition_data[5])
{
node_info = *p_node_info;
while(node_info)
{
if(node_info->node_id != current_node->node_id &&
node_info->partition_data[0] < x_xmachine && node_info->partition_data[1] > x_xmachine &&
node_info->partition_data[2] < y_xmachine && node_info->partition_data[3] > y_xmachine &&
node_info->partition_data[4] < z_xmachine && node_info->partition_data[5] > z_xmachine)
{
// Remove agent
if(before_xmachine) before_xmachine->next = current_xmachine->next;
else *p_xmachine = current_xmachine->next;
current_node->agent_total--;
// Add agent
p_temp_xmachine = &node_info->agents;
temp_xmachine = *p_temp_xmachine;
current_xmachine->next = temp_xmachine;
*p_temp_xmachine = current_xmachine;
node_info->agent_total++;
node_info = NULL;
}
else node_info = node_info->next;
}
}
else before_xmachine = current_xmachine;
if(before_xmachine) current_xmachine = before_xmachine->next;
else current_xmachine = NULL;
}*/
}
<?end if?>
/** \fn int_array * init_int_array()
* \brief Allocate memory for a dynamic integer array.
* \return int_array Pointer to the new dynamic integer array.
*/
void init_int_array(int_array * array)
{
(*array).size = 0;
(*array).total_size = ARRAY_BLOCK_SIZE;
(*array).array = (int *)malloc(ARRAY_BLOCK_SIZE * sizeof(int));
CHECK_POINTER((*array).array);
}
/** \fn void reset_int_array(int_array * array)
* \brief Reset the int array to hold nothing.
* \param array Pointer to the dynamic integer array.
*/
void reset_int_array(int_array * array)
{
(*array).size = 0;
}
/** \fn void free_int_array(int_array * array)
* \brief Free the memory of a dynamic integer array.
* \param array Pointer to the dynamic integer array.
*/
void free_int_array(int_array * array)
{
free((*array).array);
}
void copy_int_array(int_array * from, int_array * to)
{
int i;
//to = init_int_array();
for (i = 0; i < (*from).size; i++)
{
add_int(to, (*from).array[i]);
}
}
/** \fn void sort_int_array(int_array * array)
* \brief Sort the elements of a dynamic integer array with smallest first.
* \param array Pointer to the dynamic integer array.
*/
/*void sort_int_array(int_array * array)
{
int i, j, temp;
for(i=0; i<((*array).size-1); i++)
{
for(j=0; j<((*array).size-1)-i; j++)
{
if(*((*array).array+j+1) < *((*array).array+j))
{
temp = *((*array).(*array)+j);
*((*array).array+j) = *((*array).array+j+1);
*((*array).array+j+1) = temp;
}
}
}
}*/
/** \fn int quicksort_int(int *array, int elements)
* \brief Sorts the elements of the integer array in ascending order.
* \param Pointer to integer array
* \param Number of elements that must be sorted
* \return integer value indicating success(0) or failure(1)
*/
/*int quicksort_int(int array, int elements)
{
#define LEVEL 1000
int pivot, begin[LEVEL], end[LEVEL], i=0, left, right ;
begin[0]=0; end[0]=elements;
while (i>=0)
{
left=begin[i]; right=end[i]-1;
if (left<right)
{
pivot=array[left]; if (i==LEVEL-1) return 1;
while (left<right)
{
while (array[right]>=pivot && left<right) right--;
if (left<right) array[left++]=array[right];
while (array[left]<=pivot && left<right) left++;
if (left<right) array[right--]=array[left];
}
array[left]=pivot;
begin[i+1]=left+1;
end[i+1]=end[i];
end[i++]=left;
}
else
{
i--;
}
}
return 0;
}*/
/** \fn void add_int(int_array * array, int new_int)
* \brief Add an integer to the dynamic integer array.
* \param array Pointer to the dynamic integer array.
* \param new_int The integer to add
*/
void add_int(int_array * array, int new_int)
{
if((*array).size == (*array).total_size)
{
(*array).total_size = (int)((*array).total_size * ARRAY_GROWTH_RATE);
(*array).array = (int *)realloc((*array).array, ((*array).total_size * sizeof(int)));
}
(*array).array[(*array).size] = new_int;
(*array).size++;
}
/** \fn void remove_int(int_array * array, int index)
* \brief Remove an integer from a dynamic integer array.
* \param array Pointer to the dynamic integer array.
* \param index The index of the integer to remove.
*/
void remove_int(int_array * array, int index)
{
int i;
if(index < (*array).size)
{
/* memcopy?? */
for(i = index; i < (*array).size - 1; i++)
{
(*array).array[i] = (*array).array[i+1];
}
(*array).size--;
}
}
/** \fn void print_int_array(int_array * array)
* \brief Print the elements of a dynamic integer array.
* \param array Pointer to the dynamic integer array.
*/
void print_int_array(int_array * array)
{
int i;
for(i=0; i<(*array).size; i++)
{
printf("%d> %d", i, (*array).array[i]);
}
}
/** \fn float_array * init_float_array()
* \brief Allocate memory for a dynamic float array.
* \return float_array Pointer to the new dynamic float array.
*/
void init_float_array(float_array * array)
{
(*array).size = 0;
(*array).total_size = ARRAY_BLOCK_SIZE;
(*array).array = (float *)malloc(ARRAY_BLOCK_SIZE * sizeof(float));
CHECK_POINTER((*array).array);
}
/** \fn void reset_float_array(float_array * array)
* \brief Reset the float array to hold nothing.
* \param array Pointer to the dynamic float array.
*/
void reset_float_array(float_array * array)
{
(*array).size = 0;
}
/** \fn void free_float_array(float_array * array)
* \brief Free the memory of a dynamic float array.
* \param array Pointer to the dynamic float array.
*/
void free_float_array(float_array * array)
{
free((*array).array);
}
void copy_float_array(float_array * from, float_array * to)
{
int i;
//to = init_float_array();
for (i = 0; i < (*from).size; i++)
{
add_float(to, (*from).array[i]);
}
}
/** \fn void sort_float_array(float_array * array)
* \brief Sort the elements of a dynamic float array with smallest first.
* \param array Pointer to the dynamic float array.
*/
/*void sort_float_array(float_array array)
{
int i, j;
float temp;
// Using bubble sorts nested loops //
for(i=0; i<(array.size-1); i++)
{
for(j=0; j<(array.size-1)-i; j++)
{