ug4
parallel_archive.h
Go to the documentation of this file.
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 #ifndef PARALLEL_ARCHIVE_H_
34 #define PARALLEL_ARCHIVE_H_
35 
38 #include "common/log.h"
39 #include <map>
40 #include <string>
41 #include <mpi.h>
44 
45 namespace pcl{
46 
48 {
49  FileBufferDescriptor(std::string _name, const char *_buf, size_t _size) :
50  name(_name), buf(_buf), size(_size)
51  { }
52 
53  FileBufferDescriptor(std::string _name, ug::BinaryBuffer& _buf) :
54  name(_name), buf(_buf.buffer()), size(_buf.write_pos())
55  { }
56 
57  FileBufferDescriptor(std::string _name, ug::BinaryStream& _buf) :
58  name(_name), buf((char*)_buf.buffer()), size(_buf.size())
59  { }
60 
61  // Initializing buf with buf.str().c_str() is unsafe, as _buf.str() is only temporary.
62  // Therefore commenting out (not used anywhere anyhow).
63  //FileBufferDescriptor(std::string _name, std::stringstream& _buf) :
64  // name(_name), buf(_buf.str().c_str()), size(_buf.str().length())
65  //{ }
66 
67  std::string name;
68  const char *buf;
69  size_t size;
70 
71 };
72 
73 void WriteParallelArchive(pcl::ProcessCommunicator &pc, std::string strFilename, const std::vector<FileBufferDescriptor> &files);
74 
75 template<typename TBuffer>
76 void WriteParallelArchive(pcl::ProcessCommunicator &pc, std::string strFilename, const std::map<std::string, TBuffer> &files)
77 {
78  std::vector<FileBufferDescriptor> fdesc;
79  for(typename std::map<std::string, TBuffer>::const_iterator it = files.begin(); it != files.end(); ++it)
80  files.push_back(FileBufferDescriptor(ug::FilenameWithoutPath(it->first), it->second));
81  WriteParallelArchive(pc, strFilename, fdesc);
82 }
83 
102 {
103 private:
106  {
107  virtual ~BufferInterface() {}
108  virtual const char *buffer()=0;
109  virtual size_t size()=0;
110  virtual void update() {}
111  };
112 
114  {
116  virtual const char *buffer() { return internal_buffer.buffer(); };
117  virtual size_t size() { return internal_buffer.write_pos(); }
118  };
119 
121  {
123  virtual const char *buffer() { return (const char*)internal_buffer.buffer(); };
124  virtual size_t size() { return internal_buffer.size(); }
125  };
126 
128  {
129  std::stringstream internal_buffer;
130  std::string m_s;
131  virtual void update()
132  {
133  m_s = internal_buffer.str();
134  }
135  virtual const char *buffer() { return m_s.c_str(); }
136  virtual size_t size() { return m_s.length(); }
137  };
138 
140  {
141  ConstCharBuffer(const char *p, size_t s) : m_p(p), m_size(s) {}
142  virtual const char *buffer() { return m_p; };
143  virtual size_t size() { return m_size; }
144  const char *m_p;
145  size_t m_size;
146  };
147 
148 
149 public:
157  : m_filename(filename), m_pc(pc)
158  {
159  m_bWritten = false;
160  m_bUnsafe = false;
161  }
162 
164 
170  {
171  if(!m_bWritten)
172  {
173  if (m_bUnsafe)
174  {
175  UG_LOGN("ParallelArchive: Cannot write from destructor when using add_raw.\n"
176  "Use the write() method when using add_raw.");
177  return;
178  }
179 
180  write();
181  }
182  }
183 
190  {
192  files[name] = p;
193  return p->internal_buffer;
194  }
195 
202  {
204  files[name] = p;
205  return p->internal_buffer;
206  }
207 
213  std::stringstream &create_stringstream_file(std::string name)
214  {
216  files[name] = p;
217  return p->internal_buffer;
218  }
219 
228  {
229  files[f.name] = make_sp(new ConstCharBuffer(f.buf, f.size));
230  m_bUnsafe = true;
231  }
232 
237  void write()
238  {
239  std::vector<FileBufferDescriptor> fdesc;
240  for(map_iterator it = files.begin(); it != files.end(); ++it)
241  {
242  (it->second)->update();
243  fdesc.push_back(
245  (it->second)->buffer() , (it->second)->size() ) );
246  }
248  files.clear();
249  m_bWritten = true;
250  }
251 
252  void create_new_archive(std::string filename)
253  {
254  if(m_bWritten == false)
255  write();
256  m_filename = filename;
257  }
258 
259 private:
260 
261  typedef std::map<std::string, SmartPtr<BufferInterface> >::iterator map_iterator;
262 
263  std::map<std::string, SmartPtr<BufferInterface> > files;
264  std::string m_filename;
267  bool m_bUnsafe;
268 };
269 
270 }
271 #endif /* PARALLEL_ARCHIVE_H_ */
parameterString p
parameterString s
location name
Definition: checkpoint_util.lua:128
Definition: smart_pointer.h:108
Definition: parallel_archive.h:102
bool m_bWritten
Definition: parallel_archive.h:266
pcl::ProcessCommunicator m_pc
Definition: parallel_archive.h:265
ug::BinaryStream & create_BinaryStream_file(std::string name)
Definition: parallel_archive.h:201
bool m_bUnsafe
Definition: parallel_archive.h:267
ParallelArchive(std::string filename, pcl::ProcessCommunicator pc=pcl::ProcessCommunicator(pcl::PCD_WORLD))
Definition: parallel_archive.h:156
ug::BinaryBuffer & create_BinaryBuffer_file(std::string name)
Definition: parallel_archive.h:189
void create_new_archive(std::string filename)
Definition: parallel_archive.h:252
void write()
Definition: parallel_archive.h:237
~ParallelArchive()
Definition: parallel_archive.h:169
std::map< std::string, SmartPtr< BufferInterface > >::iterator map_iterator
Definition: parallel_archive.h:261
void add_raw(FileBufferDescriptor f)
Definition: parallel_archive.h:227
std::string m_filename
Definition: parallel_archive.h:264
std::map< std::string, SmartPtr< BufferInterface > > files
Definition: parallel_archive.h:263
std::stringstream & create_stringstream_file(std::string name)
Definition: parallel_archive.h:213
Definition: pcl_process_communicator.h:70
A Buffer for binary data.
Definition: binary_buffer.h:56
char * buffer()
returns the raw buffer pointer or NULL if the buffer is empty (capacity() == 0)
Definition: binary_buffer_impl.h:94
size_t write_pos() const
returns the current write-pos (in bytes)
Definition: binary_buffer_impl.h:53
a specialzation of std::iostream, that uses a
Definition: binary_stream.h:109
void * buffer()
Definition: binary_stream.h:123
size_t size() const
Definition: binary_stream.h:126
@ PCD_WORLD
Definition: pcl_process_communicator.h:55
#define UG_LOGN(msg)
Definition: log.h:369
Definition: parallel_grid_layout.h:46
void WriteParallelArchive(ProcessCommunicator &pc, std::string strFilename, const std::vector< FileBufferDescriptor > &files)
Definition: parallel_archive.cpp:44
string FilenameWithoutPath(const string &str)
Definition: string_util.cpp:195
SmartPtr< T, FreePolicy > make_sp(T *inst)
returns a SmartPtr for the passed raw pointer
Definition: smart_pointer.h:836
Definition: parallel_archive.h:48
const char * buf
Definition: parallel_archive.h:68
size_t size
Definition: parallel_archive.h:69
FileBufferDescriptor(std::string _name, ug::BinaryBuffer &_buf)
Definition: parallel_archive.h:53
std::string name
Definition: parallel_archive.h:67
FileBufferDescriptor(std::string _name, ug::BinaryStream &_buf)
Definition: parallel_archive.h:57
FileBufferDescriptor(std::string _name, const char *_buf, size_t _size)
Definition: parallel_archive.h:49
Definition: parallel_archive.h:128
virtual void update()
Definition: parallel_archive.h:131
std::string m_s
Definition: parallel_archive.h:130
std::stringstream internal_buffer
Definition: parallel_archive.h:129
virtual size_t size()
Definition: parallel_archive.h:136
virtual const char * buffer()
Definition: parallel_archive.h:135
Definition: parallel_archive.h:114
virtual size_t size()
Definition: parallel_archive.h:117
virtual const char * buffer()
Definition: parallel_archive.h:116
ug::BinaryBuffer internal_buffer
Definition: parallel_archive.h:115
Definition: parallel_archive.h:121
virtual size_t size()
Definition: parallel_archive.h:124
ug::BinaryStream internal_buffer
Definition: parallel_archive.h:122
virtual const char * buffer()
Definition: parallel_archive.h:123
internal virtual buffer interface to support different buffers
Definition: parallel_archive.h:106
virtual ~BufferInterface()
Definition: parallel_archive.h:107
virtual const char * buffer()=0
virtual void update()
Definition: parallel_archive.h:110
Definition: parallel_archive.h:140
size_t m_size
Definition: parallel_archive.h:145
const char * m_p
Definition: parallel_archive.h:144
ConstCharBuffer(const char *p, size_t s)
Definition: parallel_archive.h:141
virtual size_t size()
Definition: parallel_archive.h:143
virtual const char * buffer()
Definition: parallel_archive.h:142