Reference documentation for deal.II version 8.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
swappable_vector.templates.h
1 // ---------------------------------------------------------------------
2 // @f$Id: swappable_vector.templates.h 30036 2013-07-18 16:55:32Z maier @f$
3 //
4 // Copyright (C) 2000 - 2013 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // The deal.II library is free software; you can use it, redistribute
9 // it, and/or modify it under the terms of the GNU Lesser General
10 // Public License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 // The full text of the license can be found in the file LICENSE at
13 // the top level of the deal.II distribution.
14 //
15 // ---------------------------------------------------------------------
16 
17 #ifndef __deal2__swappable_vector_templates_h
18 #define __deal2__swappable_vector_templates_h
19 
20 
21 #include <deal.II/base/memory_consumption.h>
22 #include <deal.II/lac/swappable_vector.h>
23 #include <fstream>
24 
25 DEAL_II_NAMESPACE_OPEN
26 
27 
28 template <typename number>
30  :
31  data_is_preloaded (false)
32 {}
33 
34 
35 
36 template <typename number>
38  Vector<number>(v),
39  filename (),
40  data_is_preloaded (false)
41 {
42  Assert (v.filename == "", ExcInvalidCopyOperation());
43 }
44 
45 
46 
47 template <typename number>
49 {
50  // if the vector was stored in a
51  // file previously, and that has
52  // not been deleted in the
53  // meantime, then we kill that file
54  // first, before killing the vector
55  // itself
56 
57  if (filename != "")
58  kill_file ();
59 }
60 
61 
62 
63 template <typename number>
66 {
67  // if necessary, first delete data
68  if (filename != "")
69  kill_file ();
70 
71  // if in MT mode, block all other
72  // operations. if not in MT mode,
73  // this is a no-op
74  Threads::Mutex::ScopedLock lock(this->lock);
75 
77  data_is_preloaded = false;
78 
79  return *this;
80 }
81 
82 
83 
84 template <typename number>
85 void SwappableVector<number>::swap_out (const std::string &name)
86 {
87  // if the vector was stored in
88  // another file previously, and
89  // that has not been deleted in the
90  // meantime, then we kill that file
91  // first
92  if (filename != "")
93  kill_file ();
94 
95  filename = name;
96 
97  Assert (this->size() != 0, ExcSizeZero());
98 
99  // if in MT mode, block all other
100  // operations. if not in MT mode,
101  // this is a no-op
102  Threads::Mutex::ScopedLock lock(this->lock);
103 
104  // check that we have not called
105  // @p alert without the respective
106  // @p reload function
107  Assert (data_is_preloaded == false, ExcInternalError());
108 
109  std::ofstream tmp_out(filename.c_str());
110  this->block_write (tmp_out);
111  tmp_out.close ();
112 
113  this->reinit (0);
114 }
115 
116 
117 
118 template <typename number>
120 {
121  // if in MT mode: synchronise with
122  // possibly existing @p alert
123  // calls. if not in MT mode, this
124  // is a no-op
125  lock.acquire ();
126 
127  // if data was already preloaded,
128  // then there is no more need to
129  // load it
130  if (data_is_preloaded == false)
131  // reload data. note that this
132  // function also releases the
133  // lock
134  reload_vector (false);
135  else
136  {
137  // clear flag since no more
138  // needed
139  data_is_preloaded = false;
140 
141  // release lock. the lock is
142  // also released in the other
143  // branch of the if-clause
144  lock.release ();
145  };
146 }
147 
148 
149 
150 template <typename number>
152 {
153 #ifndef DEAL_II_WITH_THREADS
154  // note: this function does nothing
155  // in non-MT mode
156  return;
157 #else
158 
159  // synchronise with possible other
160  // invocations of this function and
161  // other functions in this class
162  lock.acquire ();
163 
164  // calling this function multiple
165  // times does no harm:
166  if ( (data_is_preloaded == true) ||
167  // calling this function while the
168  // vector is active does no harm
169  // either
170  (this->size() != 0))
171  lock.release ();
172  else
173  // data has not been preloaded so
174  // far, so go on! For this, start
175  // a detached thread
177  // note that reload_vector also
178  // releases the lock
179 #endif
180 }
181 
182 
183 
184 template <typename number>
185 void SwappableVector<number>::reload_vector (const bool set_flag)
186 {
187  Assert (filename != "", ExcInvalidFilename (filename));
188  Assert (this->size() == 0, ExcSizeNonzero());
189 
190  std::ifstream tmp_in(filename.c_str());
191  this->block_read (tmp_in);
192  tmp_in.close ();
193 
194 #ifdef DEAL_II_WITH_THREADS
195  // release the lock that was
196  // acquired by the calling
197  // functions
198 
199  // set the flag if so required
200  if (set_flag)
201  data_is_preloaded = true;
202  lock.release ();
203 #endif
204 }
205 
206 
207 
208 template <typename number>
210 {
211  // if in MT mode, wait for other
212  // operations to finish first
213  // (there should be none, but who
214  // knows). if not in MT mode,
215  // this is a no-op
216  Threads::Mutex::ScopedLock lock(this->lock);
217 
218  // this is too bad: someone
219  // requested the vector in advance,
220  // but never got to fetch it. this
221  // is most probably an error, not?
222  Assert (data_is_preloaded == false, ExcInternalError());
223 
224  if (filename != "")
225  {
226  int status = std::remove (filename.c_str());
227  AssertThrow (status == 0, ExcInternalError());
228 
229  filename = "";
230  };
231 }
232 
233 
234 
235 template <typename number>
236 const std::string &
238 {
239  return filename;
240 }
241 
242 
243 
244 template <typename number>
245 std::size_t
247 {
248  return (MemoryConsumption::memory_consumption (filename) +
249  sizeof(lock) +
250  MemoryConsumption::memory_consumption (data_is_preloaded) +
252 }
253 
254 
255 
256 
257 DEAL_II_NAMESPACE_CLOSE
258 
259 #endif // __deal2__swappable_vector_templates_h
SwappableVector & operator=(const SwappableVector &)
#define AssertThrow(cond, exc)
Definition: exceptions.h:362
Thread< RT > new_thread(const std_cxx1x::function< RT()> &function)
void reload_vector(const bool set_flag)
Vector< Number > & operator=(const Number s)
std::string filename
#define Assert(cond, exc)
Definition: exceptions.h:299
std::size_t memory_consumption(const T &t)
const std::string & get_filename() const
std::size_t memory_consumption() const
void swap_out(const std::string &filename)
::ExceptionBase & ExcInternalError()