Line data Source code
1 : /*
2 : * Copyright (c) 2013-2015: G-CSC, Goethe University Frankfurt
3 : * Author: Martin Rupp
4 : *
5 : * This file is part of UG4.
6 : *
7 : * UG4 is free software: you can redistribute it and/or modify it under the
8 : * terms of the GNU Lesser General Public License version 3 (as published by the
9 : * Free Software Foundation) with the following additional attribution
10 : * requirements (according to LGPL/GPL v3 §7):
11 : *
12 : * (1) The following notice must be displayed in the Appropriate Legal Notices
13 : * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14 : *
15 : * (2) The following notice must be displayed at a prominent place in the
16 : * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17 : *
18 : * (3) The following bibliography is recommended for citation and must be
19 : * preserved in all covered files:
20 : * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21 : * parallel geometric multigrid solver on hierarchically distributed grids.
22 : * Computing and visualization in science 16, 4 (2013), 151-164"
23 : * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24 : * flexible software system for simulating pde based models on high performance
25 : * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26 : *
27 : * This program is distributed in the hope that it will be useful,
28 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 : * GNU Lesser General Public License for more details.
31 : */
32 :
33 :
34 : // extern headers
35 : #include <iostream>
36 : #include <sstream>
37 : #include <string>
38 :
39 : // include bridge
40 : #include "bridge/bridge.h"
41 : #include "bridge/util.h"
42 : #include "bridge/util_algebra_dependent.h"
43 :
44 : // preconditioner
45 : #include "lib_algebra/lib_algebra.h"
46 : #include "common/serialization.h"
47 : #include "../util_overloaded.h"
48 : #include "common/util/binary_buffer.h"
49 :
50 : using namespace std;
51 :
52 : #if UG_PARALLEL
53 : #include "pcl/parallel_archive.h"
54 : #include "pcl/parallel_file.h"
55 : #else
56 :
57 : namespace pcl {
58 0 : void WriteCombinedParallelFile(ug::BinaryBuffer &buffer, std::string strFilename)
59 : {
60 0 : FILE *f = fopen(strFilename.c_str(), "wb");
61 0 : int numProcs = 1;
62 :
63 : int mySize = buffer.write_pos();
64 0 : int myNextOffset = 2*sizeof(int) + mySize;
65 :
66 0 : fwrite(&numProcs, sizeof(numProcs), 1, f);
67 0 : fwrite(&myNextOffset, sizeof(myNextOffset), 1, f);
68 0 : fwrite(buffer.buffer(), sizeof(char), buffer.write_pos(), f);
69 0 : fclose(f);
70 0 : }
71 :
72 0 : void ReadCombinedParallelFile(ug::BinaryBuffer &buffer, std::string strFilename)
73 : {
74 0 : FILE *f = fopen(strFilename.c_str(), "rb");
75 : int numProcs, myNextOffset;
76 : fread(&numProcs, sizeof(numProcs), 1, f);
77 : fread(&myNextOffset, sizeof(myNextOffset), 1, f);
78 :
79 0 : UG_COND_THROW(numProcs != 1, "checkPoint numProcs = " << numProcs << ", but running on 1 cores (and UG_SERIAL!)");
80 : int firstOffset = 2*sizeof(int);
81 0 : int mySize = myNextOffset - firstOffset;
82 :
83 0 : char *p = new char[mySize];
84 : fread(p, sizeof(char), mySize, f);
85 :
86 0 : buffer.clear();
87 0 : buffer.reserve(mySize);
88 0 : buffer.write(p, mySize);
89 0 : delete[] p;
90 :
91 0 : fclose(f);
92 0 : }
93 :
94 : }
95 : #endif
96 :
97 :
98 : namespace ug{
99 :
100 : /// Serialize for ParallelVector<T>
101 : #ifdef UG_PARALLEL
102 : template<typename T, class TOStream>
103 : void Serialize(TOStream &buf, const ParallelVector<T> &v)
104 : {
105 : uint t= v.get_storage_mask();
106 : Serialize(buf, t);
107 : Serialize(buf, *dynamic_cast<const T*>(&v));
108 : }
109 :
110 : /// Deerialize for ParallelVector<T>
111 : template<typename T, class TIStream>
112 : void Deserialize(TIStream &buf, ParallelVector<T> &v)
113 : {
114 : uint t = Deserialize<uint>(buf);
115 : v.set_storage_type(t);
116 : Deserialize(buf, *dynamic_cast<T*>(&v));
117 : }
118 : #endif
119 :
120 :
121 : /*
122 :
123 : Here's an example how to use SaveToFile/ReadFromFile
124 : Note that they are using MPI I/O to store the BinaryBuffers from all cores into one single file.
125 :
126 : restartfilename = "uNewtonSolution_T_"..step.."_p_".ug4vector"
127 :
128 : restartStep = util.GetParamNumber("-restartStep", 0)
129 : -- apply newton solver
130 : if step < restartStep then
131 : ReadFromFile(u, restartfilename)
132 : else
133 : -- prepare newton solver
134 : if newtonSolver:prepare(u) == false then
135 : print ("Newton solver failed at step "..step.."."); exit();
136 : end
137 : if newtonSolver:apply(u) == false then
138 : print ("Newton solver failed at step "..step.."."); exit();
139 : end
140 : SaveToFile(u, restartfilename)
141 : end
142 :
143 :
144 : See also checkpoint_util.lua and time_step_util.lua on how to generate checkpointing/debugging mechanisms with this.
145 : */
146 :
147 : template<typename T>
148 0 : void SaveToFile(const T &v, std::string filename)
149 : {
150 0 : BinaryBuffer b;
151 0 : Serialize(b, v);
152 0 : pcl::WriteCombinedParallelFile(b, filename);
153 0 : }
154 :
155 : template<typename T>
156 0 : void ReadFromFile(T &v, std::string filename)
157 : {
158 0 : BinaryBuffer b;
159 0 : pcl::ReadCombinedParallelFile(b, filename);
160 0 : Deserialize(b, v);
161 0 : }
162 :
163 :
164 :
165 : namespace bridge{
166 : namespace Restart{
167 :
168 : /**
169 : * \defgroup precond_bridge Preconditioner Bridge
170 : * \ingroup algebra_bridge
171 : * \{
172 : */
173 :
174 : /**
175 : * Class exporting the functionality. All functionality that is to
176 : * be used in scripts or visualization must be registered here.
177 : */
178 :
179 : struct Functionality
180 : {
181 :
182 : /**
183 : * Function called for the registration of Algebra dependent parts.
184 : * All Functions and Classes depending on Algebra
185 : * are to be placed here when registering. The method is called for all
186 : * available Algebra types, based on the current build options.
187 : *
188 : * @param reg registry
189 : * @param parentGroup group for sorting of functionality
190 : */
191 : template <typename TAlgebra>
192 3 : static void Algebra(Registry& reg, string grp)
193 : {
194 3 : string suffix = GetAlgebraSuffix<TAlgebra>();
195 3 : string tag = GetAlgebraTag<TAlgebra>();
196 :
197 : // typedefs for this algebra
198 : typedef typename TAlgebra::vector_type vector_type;
199 :
200 9 : reg.add_function("SaveToFile", OVERLOADED_FUNCTION_PTR(void, SaveToFile<vector_type>, (const vector_type &, std::string)), grp);
201 9 : reg.add_function("ReadFromFile", OVERLOADED_FUNCTION_PTR(void, ReadFromFile<vector_type>, (vector_type &, std::string)), grp);
202 3 : }
203 :
204 : }; // end Functionality
205 :
206 : }// end Restart
207 :
208 : /// \addtogroup precond_bridge
209 1 : void RegisterBridge_Restart(Registry& reg, string grp)
210 : {
211 1 : grp.append("/Algebra/Restart");
212 : typedef Restart::Functionality Functionality;
213 :
214 : try{
215 1 : RegisterAlgebraDependent<Functionality>(reg,grp);
216 : }
217 0 : UG_REGISTRY_CATCH_THROW(grp);
218 : //reg.add_function("testomato", testomato, grp);
219 1 : }
220 :
221 : } // namespace bridge
222 : } // namespace ug
223 :
|