GenericOutput.tpp
1 /*---------------------------------------------------------------------------*\
2  *
3  * mimmo
4  *
5  * Copyright (C) 2015-2021 OPTIMAD engineering Srl
6  *
7  * -------------------------------------------------------------------------
8  * License
9  * This file is part of mimmo.
10  *
11  * mimmo is free software: you can redistribute it and/or modify it
12  * under the terms of the GNU Lesser General Public License v3 (LGPL)
13  * as published by the Free Software Foundation.
14  *
15  * mimmo is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18  * License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with mimmo. If not, see <http://www.gnu.org/licenses/>.
22  *
23 \*---------------------------------------------------------------------------*/
24 
25 namespace mimmo{
26 
27 namespace outputCSVStream {
28 
35  template <class T>
36  std::fstream& ofstreamcsv(std::fstream &out, const T &x)
37  {
38  out << x << ",";
39  return(out);
40  };
41 
48  template <class T>
49  std::fstream& ofstreamcsvend(std::fstream &out, const T &x)
50  {
51  out << x;
52  out<<'\n';
53  return(out);
54  };
55 
62  template <class T>
63  std::fstream& ofstreamcsv(std::fstream &out, const std::vector< T > &x)
64  {
65 
66  size_t n = x.size();
67  if (n == 0) {
68  return(out);
69  }
70  for (size_t i = 0; i < n-1; i++) {
71  ofstreamcsv(out,x[i]);
72  } //next i
73  ofstreamcsvend(out,x[n-1]);
74  return(out);
75  };
76 
83  template <class T>
84  std::fstream& ofstreamcsvend(std::fstream &out, const std::vector< T > &x)
85  {
86  ofstreamcsv(out,x);
87  return(out);
88  };
89 
96  template <class T, size_t d>
97  std::fstream& ofstreamcsv(std::fstream &out, const std::array< T,d > &x)
98  {
99 
100  if (d == 0) return(out);
101  for (size_t i = 0; i < d-1; i++) {
102  ofstreamcsv(out,x[i]);
103  } //next i
104  ofstreamcsvend(out,x[d-1]);
105  return(out);
106  };
107 
115  template <class T, size_t d>
116  std::fstream& ofstreamcsvend(std::fstream &out, const std::array< T,d > &x)
117  {
118  ofstreamcsv(out,x);
119  return(out);
120  };
121 
128  template <class T>
129  std::fstream& ofstreamcsv(std::fstream &out, const MimmoPiercedVector< T > &x)
130  {
131  size_t n = x.size();
132  if (n == 0) {
133  return(out);
134  }
135  std::string name = x.getName();
136  out << name<< '\n';
137  int loc = static_cast<int>(x.getConstDataLocation());
138  out << loc<< '\n';
139  out << long(x.size())<<'\n';
140  typename bitpit::PiercedVector<T>::const_iterator itB, itE = x.end();
141  for (itB = x.begin(); itB != itE; ++itB) {
142  ofstreamcsv(out, itB.getId());
143  ofstreamcsvend(out,*itB);
144  } //next i
145  return(out);
146  };
147 
154  template <class T>
155  std::fstream& ofstreamcsvend(std::fstream &out, const MimmoPiercedVector< T > &x)
156  {
157  ofstreamcsv(out,x);
158  return(out);
159  };
160 
161 }//end of namespace
162 
168 template<typename T>
169 void
171  _setInput(*data);
172  std::fstream file;
173 #if MIMMO_ENABLE_MPI
174  if(getRank() == 0)
175 #endif
176  {
177  file.open(m_dir+"/"+m_filename, std::fstream::out);
178  if (file.is_open()){
179  if (m_csv){
180  outputCSVStream::ofstreamcsv(file, *data);
181  }
182  else{
183  file << *data;
184  }
185  file.close();
186  }
187  }
188 }
189 
195 template<typename T>
196 void
198  setInput(&data);
199 }
200 
205 template<typename T>
206 T*
208  return(static_cast<IODataT<T>*>(m_input.get())->getData());
209 }
210 
214 template<typename T>
215 void
216 GenericOutput::_setInput(T & data){
217  m_input = std::move(std::unique_ptr<IOData>(new IODataT<T>(data)));
218 }
219 
225 template<typename T>
226 void
228  _setInput(*data);
229  if(m_csv) m_binary = false;
230  std::string name = data->getName();
231  int loc = static_cast<int>(data->getDataLocation());
232  MimmoPiercedVector<T> * workingptr_ = data;
233 
234 #if MIMMO_ENABLE_MPI
235  std::unique_ptr< MimmoPiercedVector<T> > dataglobal(new MimmoPiercedVector<T>());
236  collectDataFromAllProcs(*data, dataglobal.get());
237  workingptr_ = dataglobal.get();
238  // get only 0 to work;
239  if(getRank() == 0)
240 #endif
241  {
242  std::fstream file;
243  file.open(m_dir+"/"+m_filename, std::fstream::out);
244  if (file.is_open()){
245  if(m_binary){
246  std::size_t length = name.length();
247  bitpit::genericIO::flushBINARY(file, length);
248  for (std::size_t i=0; i<length; i++){
249  char a = name.at(i);
250  bitpit::genericIO::flushBINARY(file, a);
251  }
252  bitpit::genericIO::flushBINARY(file, loc);
253  bitpit::genericIO::flushBINARY(file, long(workingptr_->size()));
254  for (auto datait = workingptr_->begin(); datait != workingptr_->end(); ++datait) {
255  bitpit::genericIO::flushBINARY(file, datait.getId());
256  bitpit::genericIO::flushBINARY(file, *datait);
257  }
258  } else if (m_csv){
259  outputCSVStream::ofstreamcsv(file, *workingptr_);
260  }else{
261  bitpit::genericIO::flushASCII(file, name);
262  bitpit::genericIO::flushASCII(file,loc);
263  file<<'\n';
264  bitpit::genericIO::flushASCII(file,long(workingptr_->size()));
265  file<<'\n';
266  for (auto datait = workingptr_->begin(); datait != workingptr_->end(); ++datait) {
267  bitpit::genericIO::flushASCII(file, datait.getId());
268  bitpit::genericIO::flushASCII(file, *datait);
269  file<<'\n';
270  }
271  }
272  file.close();
273  }
274  }// exiting scope writing.
275 }
276 
277 #if MIMMO_ENABLE_MPI
278 
284 template<typename T>
285 void
286 GenericOutputMPVData::collectDataFromAllProcs(MimmoPiercedVector<T> & locdata, MimmoPiercedVector<T> * globdata){
287 
288  if(getRank() == 0){
289  //prefill global data with 0 rank info.
290  globdata->clear();
291  globdata->setName(locdata.getName());
292  globdata->setDataLocation(locdata.getDataLocation());
293  for(auto it=locdata.begin(); it!=locdata.end(); ++it){
294  globdata->insert(it.getId(), *it);
295  }
296 
297 
298  //Receive data to all other procs and fill globdata
299  for (int sendRank=1; sendRank<m_nprocs; sendRank++){
300  long dataBufferSize;
301  MPI_Recv(&dataBufferSize, 1, MPI_LONG, sendRank, 100, m_communicator, MPI_STATUS_IGNORE);
302  mimmo::IBinaryStream dataBuffer(dataBufferSize);
303  MPI_Recv(dataBuffer.data(), dataBuffer.getSize(), MPI_CHAR, sendRank, 110, m_communicator, MPI_STATUS_IGNORE);
304 
305  //reverse in temp
306  MimmoPiercedVector<T> temp;
307  dataBuffer >> temp;
308 
309  //check values of temp and insert into globdata. If ID already exists skip it.
310  for(auto it=temp.begin(); it!=temp.end(); ++it){
311  if(!globdata->exists(it.getId())){
312  globdata->insert(it.getId(), *it);
313  }
314  }
315  }
316  //hey 0, your job is done.
317  }else{
318 
319  mimmo::OBinaryStream dataBuffer;
320  dataBuffer << locdata;
321  long dataBufferSize = dataBuffer.getSize();
322  //Send data to rank 0
323  MPI_Send(&dataBufferSize, 1, MPI_LONG, 0, 100, m_communicator);
324  MPI_Send(dataBuffer.data(), dataBuffer.getSize(), MPI_CHAR, 0, 110, m_communicator);
325  }
326 }
327 
328 #endif
329 
330 
331 
337 template<typename T>
338 void
340  setInput(&data);
341 }
342 
347 template<typename T>
350  return(static_cast<IODataT<MimmoPiercedVector< T > >*>(m_input.get())->getData());
351 }
352 
353 
357 template<typename T>
358 void
359 GenericOutputMPVData::_setInput(MimmoPiercedVector< T > & data){
360  m_input = std::move(std::unique_ptr<IOData>(new IODataT<MimmoPiercedVector< T > >(data)));
361 }
362 
363 
364 }
MPVLocation getConstDataLocation() const
void setInput(MimmoPiercedVector< T > *data)
void setInput(T *data)
void setName(std::string name)
mimmo custom derivation of bitpit OBinaryStream (see relative doc)
T * getData()
Definition: IOData.hpp:115
MimmoPiercedVector is the basic data container for mimmo library.
std::fstream & ofstreamcsvend(std::fstream &in, const T &x)
void setDataLocation(MPVLocation loc)
mimmo custom derivation of bitpit IBinaryStream (see relative doc)
std::fstream & ofstreamcsv(std::fstream &in, const T &x)
IODataT is the templated class of generic data derived from IOData base class.
Definition: IOData.hpp:79
MimmoPiercedVector< T > * getInput()