libMesh
checkpoint.C
Go to the documentation of this file.
1 // Ignore unused parameter warnings coming from cppunit headers
2 #include <libmesh/ignore_warnings.h>
3 #include <cppunit/extensions/HelperMacros.h>
4 #include <cppunit/TestCase.h>
5 #include <libmesh/restore_warnings.h>
6 
7 #include "libmesh/distributed_mesh.h"
8 #include "libmesh/replicated_mesh.h"
9 #include "libmesh/checkpoint_io.h"
10 #include "libmesh/mesh_generation.h"
11 #include "libmesh/partitioner.h"
12 
13 #include "test_comm.h"
14 
15 // THE CPPUNIT_TEST_SUITE_END macro expands to code that involves
16 // std::auto_ptr, which in turn produces -Wdeprecated-declarations
17 // warnings. These can be ignored in GCC as long as we wrap the
18 // offending code in appropriate pragmas. We can't get away with a
19 // single ignore_warnings.h inclusion at the beginning of this file,
20 // since the libmesh headers pull in a restore_warnings.h at some
21 // point. We also don't bother restoring warnings at the end of this
22 // file since it's not a header.
23 #include <libmesh/ignore_warnings.h>
24 
25 using namespace libMesh;
26 
27 class CheckpointIOTest : public CppUnit::TestCase {
31 public:
32  CPPUNIT_TEST_SUITE( CheckpointIOTest );
33 
34  CPPUNIT_TEST( testAsciiDistRepSplitter );
35  CPPUNIT_TEST( testBinaryDistRepSplitter );
36  CPPUNIT_TEST( testAsciiRepDistSplitter );
37  CPPUNIT_TEST( testBinaryRepDistSplitter );
38  CPPUNIT_TEST( testAsciiRepRepSplitter );
39  CPPUNIT_TEST( testBinaryRepRepSplitter );
40  CPPUNIT_TEST( testAsciiDistDistSplitter );
41  CPPUNIT_TEST( testBinaryDistDistSplitter );
42 
43  CPPUNIT_TEST_SUITE_END();
44 
45 protected:
46 
47 public:
48  void setUp()
49  {
50  }
51 
52  void tearDown()
53  {
54  }
55 
56  // Test that we can write multiple checkpoint files from a single processor.
57  template <typename MeshA, typename MeshB>
58  void testSplitter(bool binary, bool using_distmesh)
59  {
60  // The CheckpointIO-based splitter requires XDR.
61 #ifdef LIBMESH_HAVE_XDR
62 
63  // In this test, we partition the mesh into n_procs parts. Don't
64  // try to partition a DistributedMesh into more parts than we have
65  // processors, though.
66  const unsigned int n_procs = using_distmesh ?
67  std::min(static_cast<unsigned int>(2), TestCommWorld->size()) :
68  2;
69 
70  // The number of elements in the original mesh. For verification
71  // later.
72  dof_id_type original_n_elem = 0;
73 
74  const std::string filename =
75  std::string("checkpoint_splitter.cp") + (binary ? "r" : "a");
76 
77  {
78  MeshA mesh(*TestCommWorld);
79 
81  4, 4,
82  0., 1.,
83  0., 1.,
84  QUAD4);
85 
86  // Store the number of elements that were in the original mesh.
87  original_n_elem = mesh.n_elem();
88 
89  // Partition the mesh into n_procs pieces
90  mesh.partition(n_procs);
91 
92  // Write out checkpoint files for each piece. Since on a
93  // ReplicatedMesh we might have more pieces than we do
94  // processors, some processors may have to write out more than
95  // one piece.
96  CheckpointIO cpr(mesh);
97  cpr.current_processor_ids().clear();
98  for (processor_id_type pid = mesh.processor_id(); pid < n_procs; pid += mesh.n_processors())
99  cpr.current_processor_ids().push_back(pid);
100  cpr.current_n_processors() = n_procs;
101  cpr.binary() = binary;
102  cpr.parallel() = true;
103  cpr.write(filename);
104  }
105 
107 
108  // Test that we can read in the files we wrote and sum up to the
109  // same total number of elements.
110  {
111  MeshB mesh(*TestCommWorld);
112  CheckpointIO cpr(mesh);
113  cpr.binary() = binary;
114  cpr.read(filename);
115 
116  std::size_t read_in_elements = 0;
117 
118  for (unsigned pid=mesh.processor_id(); pid<n_procs; pid += mesh.n_processors())
119  {
120  read_in_elements += std::distance(mesh.pid_elements_begin(pid),
121  mesh.pid_elements_end(pid));
122  }
123  mesh.comm().sum(read_in_elements);
124 
125  // Verify that we read in exactly as many elements as we started with.
126  CPPUNIT_ASSERT_EQUAL(static_cast<dof_id_type>(read_in_elements), original_n_elem);
127  }
128 #endif // LIBMESH_HAVE_XDR
129  }
130 
132  {
133  testSplitter<DistributedMesh, ReplicatedMesh>(false, true);
134  }
135 
137  {
138  testSplitter<DistributedMesh, ReplicatedMesh>(true, true);
139  }
140 
142  {
143  testSplitter<ReplicatedMesh, DistributedMesh>(false, true);
144  }
145 
147  {
148  testSplitter<ReplicatedMesh, DistributedMesh>(true, true);
149  }
150 
152  {
153  testSplitter<ReplicatedMesh, ReplicatedMesh>(false, false);
154  }
155 
157  {
158  testSplitter<ReplicatedMesh, ReplicatedMesh>(true, false);
159  }
160 
162  {
163  testSplitter<DistributedMesh, DistributedMesh>(false, true);
164  }
165 
167  {
168  testSplitter<DistributedMesh, DistributedMesh>(true, true);
169  }
170 
171 };
172 
The CheckpointIO class can be used to write simplified restart files that can be used to restart simu...
Definition: checkpoint_io.h:50
void testBinaryRepRepSplitter()
Definition: checkpoint.C:156
void testSplitter(bool binary, bool using_distmesh)
Definition: checkpoint.C:58
unsigned int size() const
Definition: parallel.h:726
libMesh::Parallel::Communicator * TestCommWorld
Definition: driver.C:28
const std::vector< processor_id_type > & current_processor_ids() const
Get/Set the processor id or processor ids to use.
MeshBase & mesh
uint8_t processor_id_type
Definition: id_types.h:99
void build_square(UnstructuredMesh &mesh, const unsigned int nx, const unsigned int ny, const Real xmin=0., const Real xmax=1., const Real ymin=0., const Real ymax=1., const ElemType type=INVALID_ELEM, const bool gauss_lobatto_grid=false)
A specialized build_cube() for 2D meshes.
The libMesh namespace provides an interface to certain functionality in the library.
Real distance(const Point &p)
void testBinaryDistDistSplitter()
Definition: checkpoint.C:166
bool parallel() const
Get/Set the flag indicating if we should read/write binary.
virtual void read(const std::string &) libmesh_override
This method implements reading a mesh from a specified file.
void testAsciiRepRepSplitter()
Definition: checkpoint.C:151
void barrier() const
Pause execution until all processors reach a certain point.
const processor_id_type & current_n_processors() const
Get/Set the n_processors to use.
CPPUNIT_TEST_SUITE_REGISTRATION(CheckpointIOTest)
void testAsciiDistDistSplitter()
Definition: checkpoint.C:161
void testAsciiDistRepSplitter()
Definition: checkpoint.C:131
void testBinaryDistRepSplitter()
Definition: checkpoint.C:136
void testBinaryRepDistSplitter()
Definition: checkpoint.C:146
virtual void write(const std::string &) libmesh_override
This method implements writing a mesh to a specified file.
Definition: checkpoint_io.C:83
bool binary() const
Get/Set the flag indicating if we should read/write binary.
Definition: checkpoint_io.h:99
long double min(long double a, double b)
void testAsciiRepDistSplitter()
Definition: checkpoint.C:141
uint8_t dof_id_type
Definition: id_types.h:64