aboutsummaryrefslogtreecommitdiff
path: root/Carpet/Carpet/src/Checksum.cc
blob: 1d7e72a90fcf1040a2a337f0e7f99ff3953bc736 (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
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
#include <assert.h>
#include <stdlib.h>

#include "cctk.h"
#include "cctk_Parameters.h"

#include "Carpet/CarpetLib/src/gh.hh"

#include "carpet.hh"

static const char* rcsid = "$Header: /home/eschnett/C/carpet/Carpet/Carpet/Carpet/src/Checksum.cc,v 1.3 2001/11/02 10:58:57 schnetter Exp $";



namespace Carpet {
  
  using namespace std;
  
  
  
  void CalculateChecksums (cGH* cgh, const checktimes where)
  {
    DECLARE_CCTK_PARAMETERS;
    
    if (! checksum_timelevels) return;
    
    Checkpoint ("%*sCalculateChecksums", 2*reflevel, "");
    
    checksums.resize(CCTK_NumVars());
    for (int group=0; group<CCTK_NumGroups(); ++group) {
      for (int var=0; var<CCTK_NumVarsInGroupI(group); ++var) {
	
	const int n = CCTK_FirstVarIndexI(group) + var;
	const int num_tl = CCTK_NumTimeLevelsFromVarI(n);
	assert (num_tl>0);
	const int min_tl = mintl(where, num_tl);
	const int max_tl = maxtl(where, num_tl);
	
	checksums[n].resize(maxreflevels);
	checksums[n][reflevel].resize(num_tl);
	for (int tl=min_tl; tl<=max_tl; ++tl) {
	  checksums[n][reflevel][tl].resize(hh->components(reflevel));
	  BEGIN_COMPONENT_LOOP(cgh) {
	    checksums[n][reflevel][tl][component].valid = false;
	  } END_COMPONENT_LOOP(cgh);
	}
      }
    }
    
    for (int group=0; group<CCTK_NumGroups(); ++group) {
      if (CCTK_QueryGroupStorageI((cGH*)cgh, group)) {
	for (int var=0; var<CCTK_NumVarsInGroupI(group); ++var) {
	  
	  const int n = CCTK_FirstVarIndexI(group) + var;
	  const int sz = CCTK_VarTypeSize(CCTK_VarTypeI(n));
	  assert (sz>0);
	  const int num_tl = CCTK_NumTimeLevelsFromVarI(n);
	  assert (num_tl>0);
	  const int min_tl = mintl(where, num_tl);
	  const int max_tl = maxtl(where, num_tl);
	  
	  for (int tl=min_tl; tl<=max_tl; ++tl) {
	    BEGIN_COMPONENT_LOOP(cgh) {
	      if (hh->is_local(reflevel,component)) {
		const int gpdim = arrdata[group].info.dim;
		int np = 1;
		for (int d=0; d<gpdim; ++d) {
		  np *= *CCTK_ArrayGroupSizeI((cGH*)cgh, d, group);
		}
		const void* data = cgh->data[n][tl];
		int chk = 0;
		for (int i=0; i<np*sz/(int)sizeof(chk); ++i) {
		  chk += ((const int*)data)[i];
		}
		checksums[n][reflevel][tl][component].sum = chk;
		checksums[n][reflevel][tl][component].valid = true;
	      }
	    } END_COMPONENT_LOOP(cgh);
	  } // for tl
	} // for var
      }	// if has storage
    } // for group
  }
  
  
  
  void CheckChecksums (cGH* cgh, const checktimes where)
  {
    DECLARE_CCTK_PARAMETERS;
    
    if (! checksum_timelevels) return;
    
    Checkpoint ("%*sCheckChecksums", 2*reflevel, "");
    
    assert ((int)checksums.size()==CCTK_NumVars());
    for (int group=0; group<CCTK_NumGroups(); ++group) {
      if (CCTK_QueryGroupStorageI((cGH*)cgh, group)) {
	for (int var=0; var<CCTK_NumVarsInGroupI(group); ++var) {
	  
	  const int n = CCTK_FirstVarIndexI(group) + var;
	  const int sz = CCTK_VarTypeSize(CCTK_VarTypeI(n));
	  assert (sz>0);
	  const int num_tl = CCTK_NumTimeLevelsFromVarI(n);
	  assert (num_tl>0);
	  const int min_tl = mintl(where, num_tl);
	  const int max_tl = maxtl(where, num_tl);
	  
	  assert ((int)checksums[n].size()==maxreflevels);
	  assert ((int)checksums[n][reflevel].size()==num_tl);
	  
	  for (int tl=min_tl; tl<=max_tl; ++tl) {
	    
	    bool unexpected_change = false;
	    
	    assert ((int)checksums[n][reflevel][tl].size()
		    == hh->components(reflevel));
	    BEGIN_COMPONENT_LOOP(cgh) {
	      if (checksums[n][reflevel][tl][component].valid) {
		if (hh->is_local(reflevel,component)) {
		  const int gpdim = arrdata[group].info.dim;
		  int np = 1;
		  for (int d=0; d<gpdim; ++d) {
		    np *= *CCTK_ArrayGroupSizeI((cGH*)cgh, d, group);
		  }
		  const void* data = cgh->data[n][tl];
		  int chk = 0;
		  for (int i=0; i<np*sz/(int)sizeof(chk); ++i) {
		    chk += ((const int*)data)[i];
		  }
		  unexpected_change
		    = (unexpected_change
		       || chk != checksums[n][reflevel][tl][component].sum);
		}
	      }
	    } END_COMPONENT_LOOP(cgh);
	    
	    if (unexpected_change) {
	      char* fullname = CCTK_FullName(n);
	      CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
			  "Timelevel %d of the variable \"%s\" has changed unexpectedly.",
			  tl, fullname);
	      free (fullname);
	    }
	    
	  } // for tl
	  
	} // for var
      }	// if has storage
    } // for group
  }
  
} // namespace Carpet