aboutsummaryrefslogtreecommitdiff
path: root/src/file.c
blob: 330f0b9c58d436cee7c1b06a87eb622d41965fec (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "cctk.h"
#include "cctk_Arguments.h"
#include "cctk_Parameters.h"
#include "cctk_Termination.h"
#include "cctk_Timers.h"
#include "util_String.h"



enum { BUFLEN = 10000 };



static
const char * get_termination_file (void)
{
  DECLARE_CCTK_PARAMETERS;
  
  static char filename[BUFLEN];
  
  if (strlen (termination_file) == 0)
  {
    const char * pbs_jobid = getenv ("MANUAL_TERMINATION_JOB_ID");
    if (pbs_jobid == NULL)
      pbs_jobid = getenv ("PBS_JOBID");
    
    if (pbs_jobid == NULL)
      CCTK_VWarn (CCTK_WARN_ABORT, __LINE__, __FILE__, "ManualTermination",
                  "Could not find environment variable "
                  "'MANUAL_TERMINATION_JOB_ID' or 'PBS_JOBID'");
    else
      Util_snprintf (filename, BUFLEN, "/tmp/cactus_terminate.%s", pbs_jobid);
  }
  else
  {
    if (termination_file[0] == '/')
    {
      /* file name begins with a slash, do not use IO::out_dir */
      Util_snprintf (filename, BUFLEN, "%s", termination_file);
    }
    else
    {
      /* add IO::out_dir to filename */
      Util_snprintf (filename, BUFLEN,
                     "%s/%s", out_dir, termination_file);
    }
  }
  return filename;
}



/* Note that the termination file is created even if
   termination_from_file is false. This is because
   termination_from_file is steerable and may be changed to true at
   run time. */
void TerminationTrigger_CreateFile (CCTK_ARGUMENTS)
{
  DECLARE_CCTK_ARGUMENTS;
  DECLARE_CCTK_PARAMETERS;
  
  FILE *file;
  
  /* only one processor needs to create the file */
  if (CCTK_MyProc (cctkGH) != 0)
  {
    return;
  }
  
  file = fopen (get_termination_file(), "w");
  fclose (file);
}



void TerminationTrigger_CheckFile (CCTK_ARGUMENTS)
{
  DECLARE_CCTK_ARGUMENTS;
  DECLARE_CCTK_PARAMETERS;
  
  FILE *file;
  int terminate;
  
  if (! termination_from_file)
  {
    return;
  }

  /* only one processor needs to check the file */
  if (CCTK_MyProc (cctkGH) != 0)
  {
    return;
  }
  
  if (cctk_iteration % check_file_every != 0)
  {
    return;
  }
  
  file = fopen (get_termination_file(), "r");
  
  if (file != NULL) {
    terminate = 0;
    fscanf (file, "%d", &terminate);
    fclose (file);
    
    if (terminate == 1) {
      CCTK_INFO ("Found termination signal in termination file.  "
                 "Triggering termination...");
      
      CCTK_TerminateNext (cctkGH);
    }
  }
}