/[escript]/trunk/escript/src/Utils.cpp
ViewVC logotype

Diff of /trunk/escript/src/Utils.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2637 by jfenwick, Fri Aug 28 04:12:24 2009 UTC revision 2641 by jfenwick, Mon Aug 31 07:41:49 2009 UTC
# Line 12  Line 12 
12  *******************************************************/  *******************************************************/
13    
14    
15    #include <fstream>
16  #include <string.h>  #include <string.h>
17    
18  // added for saveCSV  // added for saveCSV
19  #include <boost/python.hpp>  #include <boost/python.hpp>
20    #include <boost/scoped_ptr.hpp>
21  #include "Data.h"  #include "Data.h"
22    
23  #include "Utils.h"  #include "Utils.h"
# Line 233  bool append) Line 235  bool append)
235      int dpps=data[0].getNumDataPointsPerSample();      int dpps=data[0].getNumDataPointsPerSample();
236    
237            
238      std::ofstream os;      std::ostringstream os;
239      if (append)  
     {  
     os.open(filename.c_str(), std::ios_base::app);  
     }  
     else  
     {  
     os.open(filename.c_str());  
     }  
     if (!os.is_open())  
     {  
     throw DataException("saveDataCSVcpp: unable to open file for writing");  
     }  
240    
241      bool first=true;      bool first=true;
242      for (int i=0;i<numdata;++i)      
243        if (data[0].getDomain()->getMPIRank()==0)
244      {      {
245          for (int i=0;i<numdata;++i)
246          {
247      const DataTypes::ShapeType& s=data[i].getDataPointShape();      const DataTypes::ShapeType& s=data[i].getDataPointShape();
248          switch (data[i].getDataPointRank())          switch (data[i].getDataPointRank())
249      {      {
# Line 335  bool append) Line 329  bool append)
329      default:      default:
330          throw DataException("saveDataCSV: Illegal rank");          throw DataException("saveDataCSV: Illegal rank");
331      }      }
332          }
333          os << endl;
334      }      }
     os << endl;  
   
335      boost::scoped_ptr<BufferGroup> maskbuffer;  // sample buffer for the mask [if we have one]      boost::scoped_ptr<BufferGroup> maskbuffer;  // sample buffer for the mask [if we have one]
336      const double* masksample=0;      const double* masksample=0;
337      int maskoffset=0;      int maskoffset=0;
# Line 360  bool append) Line 354  bool append)
354          expandedmask=true;          expandedmask=true;
355      }      }
356      }      }
357        os.setf(std::ios_base::scientific, std::ios_base::floatfield);
358        os.precision(15);
359    
360        // errors prior to this point will occur on all processes anyway
361        // so there is no need to explicitly notify other ranks
362        int error=0;
363      try{      try{
364        for (int i=0;i<numsamples;++i)        for (int i=0;i<numsamples;++i)
365        {        {
# Line 405  bool append) Line 405  bool append)
405        }        }
406      } catch (...)      } catch (...)
407      {      {
408          os.close();      error=1;
409    #ifndef PASO_MPI
410      throw;      throw;
411    #endif
412        }
413    #ifdef PASO_MPI
414        MPI_Comm com=data[0].getDomain()->getMPIComm();
415        int rerror=0;
416        MPI_Allreduce( &error, &rerror, 1, MPI_INT, MPI_MAX, com );
417        error=rerror;
418        if (error)
419        {
420        throw DataException("saveDataCSVcpp: error building output");
421        }
422    #endif
423    
424        // at this point os will contain the text to be written
425    #ifndef PASO_MPI
426    
427        std::ofstream ofs;
428        if (append)
429        {
430        ofs.open(filename.c_str(), std::ios_base::app);
431        }
432        else
433        {
434        ofs.open(filename.c_str());
435      }      }
436      os.close();      if (!ofs.is_open())
437        {
438        throw DataException("saveDataCSVcpp: unable to open file for writing");
439        }
440        ofs << os.str();
441        ofs.close();
442    
443  cout << "This method is not MPI safe" << endl;  #else
444    // here we have MPI
445        const char* mpistr=0;
446        MPI_File mpi_fileHandle_p;
447        MPI_Status mpi_status;
448        MPI_Info mpi_info = MPI_INFO_NULL;
449        char* fname_c=new char[filename.size()+1];
450        strcpy(fname_c,filename.c_str());
451        boost::scoped_ptr<char> fname_p(fname_c);
452        
453        int amode = MPI_MODE_CREATE|MPI_MODE_WRONLY|MPI_MODE_UNIQUE_OPEN;
454        if (append)
455        {
456        amode |= MPI_MODE_APPEND;
457        }
458        else
459        {
460        if (data[0].getDomain()->getMPIRank()==0)
461        {
462            std::ifstream ifs(fname_p.get());   // if file exists, remove it
463            if (ifs.is_open())
464            {
465            ifs.close();
466                if (!remove(fname_p.get()))
467            {
468                error=1;
469            }
470            }
471        }
472        data[0].getDomain()->MPIBarrier();
473        int rerror=0;
474        MPI_Allreduce( &error, &rerror, 1, MPI_INT, MPI_MAX, com );
475        if (rerror!=0)
476        {
477            std::ostringstream oss;
478            oss << "saveDataCSVcpp: File " << filename << " already exists and could not be removed in preparation for new output.";
479            throw DataException(oss.str());
480        }
481        }
482        int ierr;
483        ierr = MPI_File_open(com, fname_p.get(), amode, mpi_info, &mpi_fileHandle_p);
484        if (ierr != MPI_SUCCESS)
485        {
486        std::ostringstream oss;
487        oss << "saveDataCSVcpp: File " << filename << " could not be opened for writing in parallel";
488            // file is not open so we can throw
489        throw DataException(oss.str());
490        }
491        else
492        {
493                ierr=MPI_File_set_view(mpi_fileHandle_p,MPI_DISPLACEMENT_CURRENT,
494                        MPI_CHAR, MPI_CHAR, "native", mpi_info);
495    // here we are assuming that std::string holds the same type of char as MPI_CHAR
496        }
497        std::string contents=os.str();
498        char* con=new char[contents.size()+1];
499        strcpy(con, contents.c_str());
500        boost::scoped_ptr<char> buff(con);
501        ierr=MPI_File_write_ordered(mpi_fileHandle_p, buff.get(), contents.size(), MPI_CHAR, &mpi_status);
502        if (ierr != MPI_SUCCESS)
503        {
504        error=1;
505        }
506    
507        if (MPI_File_close(&mpi_fileHandle_p)!= MPI_SUCCESS)
508        {
509        error=1;
510        }
511        data[0].getDomain()->MPIBarrier();
512        if (error)  // any errors at this stage are from collective routines
513        {       // so there is no need to reduce_all
514        throw DataException("saveDataCSVcpp: Error writing and closing file");
515        }
516        
517    #endif
518  }  }
519    
520  }  // end of namespace  }  // end of namespace

Legend:
Removed from v.2637  
changed lines
  Added in v.2641

  ViewVC Help
Powered by ViewVC 1.1.26