moab
WriteHDF5.hpp
Go to the documentation of this file.
00001 
00017 #ifndef WRITE_HDF5_HPP
00018 #define WRITE_HDF5_HPP
00019 
00020 #include <list>
00021 #ifdef USE_MPI // include this before HDF5 headers to avoid conflicts
00022 #  include "moab_mpi.h"
00023 #endif
00024 #include "moab_mpe.h"
00025 #include "mhdf.h"
00026 #include "moab/Forward.hpp"
00027 #include "moab/Range.hpp"
00028 #include "moab/WriterIface.hpp"
00029 #include "moab/RangeMap.hpp"
00030 #include "moab/WriteUtilIface.hpp"
00031 #include "DebugOutput.hpp"
00032 #include "HDF5Common.hpp"
00033 
00034 namespace moab {
00035 
00036 class IODebugTrack;
00037 
00038 /* If this define is not set, node->entity adjacencies will not be written */
00039 #undef MB_H5M_WRITE_NODE_ADJACENCIES
00040 
00046 class WriteHDF5 : public WriterIface
00047 {
00048 
00049 public:
00050 
00051   static WriterIface* factory( Interface* );
00052 
00053   WriteHDF5( Interface* iface );
00054   
00055   virtual ~WriteHDF5();
00056   
00062   ErrorCode write_file( const char* filename,
00063                           const bool overwrite,
00064                           const FileOptions& opts,
00065                           const EntityHandle* export_sets,
00066                           const int export_set_count,
00067                           const std::vector<std::string>& qa_records,
00068                           const Tag* tag_list = NULL,
00069                           int num_tags = 0,
00070                           int user_dimension = 3 );
00071 
00077   typedef EntityHandle id_t;
00078   
00080   static const hid_t id_type;
00081 
00082   struct ExportType
00083   {
00085     EntityType type;
00087     int num_nodes;
00088 
00089     virtual ~ExportType()
00090     { }
00091     
00092     bool operator==(ExportType t) const
00093       { return t.type == type && t.num_nodes == num_nodes; }
00094     bool operator!=(ExportType t) const
00095       { return t.type != type || t.num_nodes != num_nodes; }
00096     bool operator<(ExportType t) const
00097       { return type < t.type || (type == t.type && num_nodes < t.num_nodes); }
00098   }; 
00099 
00101   struct ExportSet : public ExportType
00102   {
00104     Range range;
00106     id_t first_id;
00109     long offset;
00111     long adj_offset;
00114     long max_num_ents, max_num_adjs;
00120     long total_num_ents;
00121     
00122     bool operator<( const ExportType& other ) const
00123       { return type < other.type || 
00124                (type == other.type && num_nodes < other.num_nodes); }
00125     
00126     bool operator<( std::pair<int,int> other ) const
00127       { return type < other.first || 
00128                (type == other.first && num_nodes < other.second); }
00129     
00130     bool operator==( const ExportType& other ) const
00131       { return (type == other.type && num_nodes == other.num_nodes); }
00132     
00133     bool operator==( std::pair<int,int> other ) const
00134       { return (type == other.first && num_nodes == other.second); }
00135                 
00136     const char* name() const;
00137   };
00138   
00140   struct TagDesc
00141   {
00143     Tag tag_id;
00146     id_t sparse_offset;
00150     id_t var_data_offset;
00152     bool write_sparse;
00155     unsigned long max_num_ents;
00160     unsigned long max_num_vals;
00161     
00164     std::vector<ExportType> dense_list;
00165     
00166     bool have_dense( const ExportType& type ) const
00167       { return std::find(dense_list.begin(), dense_list.end(), type) != dense_list.end(); }
00168     
00169     bool operator<(const TagDesc&) const;
00170   };
00171 
00175   //static ErrorCode register_known_tag_types( Interface* );
00176   
00178   struct HDF5ErrorHandler {
00179     HDF5_Error_Func_Type func;
00180     void* data;
00181   };
00182   
00183   mhdf_FileHandle file_ptr() { return filePtr; }
00184   
00185   WriteUtilIface* write_util() { return writeUtil; }
00186 
00187 protected:
00188   
00190   HDF5ErrorHandler errorHandler;
00191 
00195   virtual ErrorCode parallel_create_file( const char* filename,
00196                                             bool overwrite,
00197                                             const std::vector<std::string>& qa_records,
00198                                             const FileOptions& opts,
00199                                             const Tag* tag_list,
00200                                             int num_tags,
00201                                             int dimension = 3,
00202                                             double* times = 0 );
00203   virtual ErrorCode write_finished();
00204   virtual void debug_barrier_line(int lineno);
00205  
00207   ErrorCode gather_tags( const Tag* user_tag_list, int user_tag_list_length );
00208 
00223   bool check_dense_format_tag( const ExportSet& ents, 
00224                                const Range& all_tagged,
00225                                bool prefer_dense );
00226 
00232   ErrorCode count_adjacencies( const Range& elements, id_t& result );
00233   
00234 public: // make these public so helper classes in WriteHDF5Parallel can use them
00235 
00241   ErrorCode create_elem_table( const ExportSet& block, long num_ents, long& first_id_out );
00242   
00247   ErrorCode create_set_meta( long num_sets, long& first_id_out );
00248 
00249 protected:
00250   
00255   ErrorCode count_set_size( const Range& sets,
00256                               long& contents_length_out,
00257                               long& children_length_out,
00258                               long& parents_length_out );
00259 
00261   ErrorCode get_set_info( EntityHandle set,
00262                             long& num_entities,
00263                             long& num_children,
00264                             long& num_parents,
00265                             unsigned long& flags );
00266 
00271   ErrorCode create_set_tables( long contents_length,
00272                                long children_length,
00273                                long parents_length );
00274 
00276   ErrorCode write_qa( const std::vector<std::string>& list );
00277 
00279   ErrorCode get_num_sparse_tagged_entities( const TagDesc& tag, size_t& count );
00281   ErrorCode get_sparse_tagged_entities( const TagDesc& tag, Range& range );
00283   void get_write_entities( Range& range );
00284   
00286   size_t bufferSize;
00288   char* dataBuffer;
00289 
00291   Interface* iFace;
00293   WriteUtilIface* writeUtil;
00294   
00296   mhdf_FileHandle filePtr;
00297   
00299   RangeMap<EntityHandle,id_t> idMap;
00300   
00302   std::list<ExportSet> exportList;
00304   ExportSet nodeSet;
00306   ExportSet setSet;
00307   
00308   const ExportSet* find( ExportType type ) const {
00309     if (type.type == MBVERTEX)
00310       return &nodeSet;
00311     else if (type.type == MBENTITYSET)
00312       return &setSet;
00313     else {
00314       std::list<ExportSet>::const_iterator it;
00315       it = std::find( exportList.begin(), exportList.end(), type );
00316       return it == exportList.end() ? 0 : &*it;
00317     }
00318   }
00319   
00321   unsigned long setContentsOffset;
00323   unsigned long setChildrenOffset, setParentsOffset;
00326   long maxNumSetContents, maxNumSetChildren, maxNumSetParents;
00334   bool writeSets, writeSetContents, writeSetChildren, writeSetParents;
00335   
00339   struct SpecialSetData {
00340     EntityHandle setHandle;
00341     unsigned setFlags;
00342     std::vector<id_t> contentIds;
00343     std::vector<id_t> childIds;
00344     std::vector<id_t> parentIds;
00345   };
00346   struct SpecSetLess {
00347     bool operator() (const SpecialSetData& a, EntityHandle b) const
00348       { return a.setHandle < b; }
00349   };
00350 
00351   
00353   std::vector<SpecialSetData> specialSets;
00354   const SpecialSetData* find_set_data( EntityHandle h ) const
00355     { return const_cast<WriteHDF5*>(this)->find_set_data(h); }
00356   SpecialSetData* find_set_data( EntityHandle h );
00357   
00359   std::list<TagDesc> tagList;
00360 
00362   bool parallelWrite;
00364   bool collectiveIO;
00366   bool writeTagDense;
00367   
00371   hid_t writeProp;
00372   
00374   DebugOutput dbgOut;
00375   
00376   static MPEState topState;
00377   static MPEState subState;
00378   
00380   bool debugTrack;
00381 
00382   void print_id_map() const;
00383   void print_id_map( std::ostream& str, const char* prefix = "" ) const;
00384 
00393   ErrorCode create_tag( const TagDesc& tag_data, 
00394                         unsigned long num_entities,
00395                         unsigned long var_len_total );
00396   
00398   ErrorCode assign_ids( const Range& entities, id_t first_id );
00399   
00414   ErrorCode range_to_blocked_list( const Range& input_range,
00415                                    std::vector<id_t>& output_id_list , 
00416                                    bool& ranged_list );
00417   
00432   ErrorCode range_to_blocked_list( const EntityHandle* input_ranges,
00433                                    size_t num_input_ranges,
00434                                    std::vector<id_t>& output_id_list , 
00435                                    bool& ranged_list );
00436   
00437 
00438   ErrorCode range_to_id_list( const Range& input_range,
00439                                 id_t* array );
00441   ErrorCode vector_to_id_list( const std::vector<EntityHandle>& input,
00442                                std::vector<id_t>& output, 
00443                                bool remove_non_written = false );
00445   ErrorCode vector_to_id_list( const EntityHandle* input,
00446                                id_t* output,
00447                                size_t num_entities );
00449   ErrorCode vector_to_id_list( const EntityHandle* input,
00450                                size_t input_len,
00451                                id_t* output,
00452                                size_t& output_len,
00453                                bool remove_non_written );
00454 
00468   bool convert_handle_tag( EntityHandle* data, size_t count ) const;
00469   bool convert_handle_tag( const EntityHandle* source,
00470                            EntityHandle* dest, 
00471                            size_t count ) const;
00472 
00479   ErrorCode get_adjacencies( EntityHandle entity, std::vector<id_t>& adj );
00480                                 
00483   ErrorCode get_tag_data_length( const TagDesc& tag_info,
00484                                  const Range& range,
00485                                  unsigned long& result );
00486   
00487 private:
00488 
00491   ErrorCode write_file_impl( const char* filename,
00492                                const bool overwrite,
00493                                const FileOptions& opts,
00494                                const EntityHandle* export_sets,
00495                                const int export_set_count,
00496                                const std::vector<std::string>& qa_records,
00497                                const Tag* tag_list,
00498                                int num_tags,
00499                                int user_dimension = 3 );
00500 
00501   ErrorCode init();
00502   
00503   ErrorCode serial_create_file( const char* filename,
00504                                   bool overwrite,
00505                                   const std::vector<std::string>& qa_records,
00506                                   const Tag* tag_list,
00507                                   int num_tags,
00508                                   int dimension = 3 );
00509   
00517   ErrorCode gather_mesh_info( const std::vector<EntityHandle>& export_sets );
00518   
00520   ErrorCode gather_all_mesh( );
00521   
00523   ErrorCode initialize_mesh( const Range entities_by_dim[5] );
00524  
00529   ErrorCode write_nodes( );
00530   
00538   ErrorCode write_elems( ExportSet& elemset );
00539   
00547   ErrorCode write_sets( double* times );
00548 
00567   ErrorCode write_set_data( const WriteUtilIface::EntityListType which_data,
00568                             const hid_t handle,
00569                             IODebugTrack& track,
00570                             Range* ranged = 0,
00571                             Range* null_stripped = 0,
00572                             std::vector<long>* set_sizes = 0);
00573   
00579   ErrorCode write_adjacencies( const ExportSet& export_set );
00580   
00587 
00588   ErrorCode write_tag( const TagDesc& tag_data,
00589                        double* times );
00590                             
00592   ErrorCode get_connectivity( Range::const_iterator begin,
00593                               Range::const_iterator end,
00594                               int nodes_per_element,
00595                               id_t* id_data_out );
00596                                    
00613   ErrorCode get_tag_size( Tag tag,
00614                           DataType& moab_type,
00615                           int& num_bytes,
00616                           int& elem_size,
00617                           int& file_size,
00618                           mhdf_TagDataType& file_type,
00619                           hid_t& hdf_type );
00620                             
00622   ErrorCode write_sparse_ids( const TagDesc& tag_data, 
00623                               const Range& range,
00624                               hid_t table_handle, 
00625                               size_t table_size, 
00626                               const char* name = 0 );
00627   
00629   ErrorCode write_sparse_tag( const TagDesc& tag_data,
00630                               const std::string& tag_name,
00631                               DataType tag_data_type,
00632                               hid_t hdf5_data_type,
00633                               int hdf5_type_size );
00634 
00636   ErrorCode write_var_len_indices( const TagDesc& tag_data,
00637                                    const Range& range,
00638                                    hid_t idx_table,
00639                                    size_t table_size,
00640                                    int type_size,
00641                                    const char* name = 0 );
00642   
00644   ErrorCode write_var_len_data( const TagDesc& tag_data,
00645                                 const Range& range,
00646                                 hid_t table,
00647                                 size_t table_size,
00648                                 bool handle_tag,
00649                                 hid_t hdf_type,
00650                                 int type_size,
00651                                 const char* name = 0 );
00652   
00654   ErrorCode write_var_len_tag( const TagDesc& tag_info,
00655                                const std::string& tag_name,
00656                                DataType tag_data_type,
00657                                hid_t hdf5_type,
00658                                int hdf5_type_size );
00659 
00661   ErrorCode write_dense_tag( const TagDesc& tag_data,
00662                              const ExportSet& elem_data,
00663                              const std::string& tag_name,
00664                              DataType tag_data_type,
00665                              hid_t hdf5_data_type,
00666                              int hdf5_type_size );
00667 
00669   ErrorCode write_tag_values( Tag tag_id,
00670                               hid_t data_table,
00671                               unsigned long data_offset,
00672                               const Range& range,
00673                               DataType tag_data_type,
00674                               hid_t hdf5_data_type,
00675                               int hdf5_type_size,
00676                               unsigned long max_num_ents,
00677                               IODebugTrack& debug_track );
00678 
00679 protected:
00680 
00681   enum TimingValues { 
00682        TOTAL_TIME = 0,
00683          GATHER_TIME,
00684          CREATE_TIME,
00685            CREATE_NODE_TIME,
00686            NEGOTIATE_TYPES_TIME,
00687            CREATE_ELEM_TIME,
00688            FILEID_EXCHANGE_TIME,
00689            CREATE_ADJ_TIME,
00690            CREATE_SET_TIME,
00691              SHARED_SET_IDS,
00692              SHARED_SET_CONTENTS,
00693              SET_OFFSET_TIME,
00694            CREATE_TAG_TIME,
00695          COORD_TIME,
00696          CONN_TIME,
00697          SET_TIME,
00698            SET_META,
00699            SET_CONTENT,
00700            SET_PARENT,
00701            SET_CHILD,
00702          ADJ_TIME,
00703          TAG_TIME,
00704            DENSE_TAG_TIME,
00705            SPARSE_TAG_TIME,
00706            VARLEN_TAG_TIME,
00707          NUM_TIMES };
00708 
00709   
00710   virtual void print_times( const double times[NUM_TIMES] ) const;
00711 };
00712 
00713 } // namespace moab
00714 
00715 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines