aboutsummaryrefslogtreecommitdiff
path: root/Carpet/Carpet/src/Shutdown.cc
blob: 0d5fbe244b428141580b704cdb3447bd4abe1682 (plain)
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
#include <cstdio>
#include <cstdlib>

#include <cctk.h>
#include <cctk_Parameters.h>

#include <dist.hh>

#include <carpet.hh>
#include <Timers.hh>
#include <TimerNode.hh>
#include <TimerSet.hh>



namespace Carpet {
  
  using namespace std;
  
  
  
  int Shutdown (tFleshConfig* fc)
  {
    DECLARE_CCTK_PARAMETERS;
    
    Waypoint ("Starting shutdown");
    
    const int convlev = 0;
    cGH* cctkGH = fc->GH[convlev];
    
    static Timer timer ("Shutdown");
    timer.start();
    for (int rl=reflevels-1; rl>=0; --rl) {
      BEGIN_REVERSE_MGLEVEL_LOOP(cctkGH) {
        ENTER_LEVEL_MODE (cctkGH, rl) {
          BeginTimingLevel (cctkGH);
          
          do_early_global_mode = reflevel==reflevels-1;
          do_late_global_mode = reflevel==0;
          do_early_meta_mode = do_early_global_mode and mglevel==0;
          do_late_meta_mode = do_late_global_mode and mglevel==mglevels-1;
          do_global_mode = do_late_global_mode;
          do_meta_mode = do_late_meta_mode;
          
          Checkpoint ("Shutdown at iteration %d time %g%s%s",
                      cctkGH->cctk_iteration, (double)cctkGH->cctk_time,
                      (do_global_mode ? " (global)" : ""),
                      (do_meta_mode ? " (meta)" : ""));
          
          // Terminate
          Checkpoint ("Scheduling TERMINATE");
          CCTK_ScheduleTraverse ("CCTK_TERMINATE", cctkGH, CallFunction);
          
          EndTimingLevel (cctkGH);
        } LEAVE_LEVEL_MODE;
      } END_REVERSE_MGLEVEL_LOOP;
    } // for rl

    // Stop all timers before shutdown, since timers may rely on data
    // structures which are destroyed during shutdown
    int const ierr = CCTK_TimerStop ("CCTK total time");
    assert (not ierr);
    timer.stop();
    if (output_timers_every > 0) {
      TimerSet::writeData (cctkGH, timer_file);
    }
    
    BEGIN_REVERSE_MGLEVEL_LOOP(cctkGH) {
      do_early_global_mode = true;
      do_late_global_mode = true;
      do_early_meta_mode = do_early_global_mode and mglevel==0;
      do_late_meta_mode = do_late_global_mode and mglevel==mglevels-1;
      do_global_mode = do_late_global_mode;
      do_meta_mode = do_late_meta_mode;
      
      // Shutdown
      Checkpoint ("Scheduling SHUTDOWN");
      CCTK_ScheduleTraverse ("CCTK_SHUTDOWN", cctkGH, CallFunction);
      
    } END_REVERSE_MGLEVEL_LOOP;
    
    main_timer_tree.root->stop();
    
    if (output_timer_tree_every > 0) {
      TimerNode *et = main_timer_tree.root->getChildTimer("Evolve");
      double total_avg, total_max;
      et->getGlobalTime(total_avg, total_max);
      et->print(cout, total_max, 0, timer_tree_threshold_percentage, timer_tree_output_precision);
      mode_timer_tree.root->getGlobalTime(total_avg, total_max);
      mode_timer_tree.root->print(cout, total_max, 0, timer_tree_threshold_percentage, timer_tree_output_precision);
    }
    
    if (output_xml_timer_tree) {
      main_timer_tree.root->outputXML(out_dir,CCTK_MyProc (cctkGH));
    }
    
    // earlier checkpoint before finalising MPI
    Waypoint ("Done with shutdown");
    
    return 0;
  }
  
} // namespace Carpet