AbstractHdf5Access.cpp

00001 /*
00002 
00003 Copyright (c) 2005-2015, University of Oxford.
00004 All rights reserved.
00005 
00006 University of Oxford means the Chancellor, Masters and Scholars of the
00007 University of Oxford, having an administrative office at Wellington
00008 Square, Oxford OX1 2JD, UK.
00009 
00010 This file is part of Chaste.
00011 
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014  * Redistributions of source code must retain the above copyright notice,
00015    this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright notice,
00017    this list of conditions and the following disclaimer in the documentation
00018    and/or other materials provided with the distribution.
00019  * Neither the name of the University of Oxford nor the names of its
00020    contributors may be used to endorse or promote products derived from this
00021    software without specific prior written permission.
00022 
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00029 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00032 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 
00034 */
00035 
00036 #include "Exception.hpp"
00037 #include "AbstractHdf5Access.hpp"
00038 
00039 bool AbstractHdf5Access::DoesDatasetExist(const std::string& rDatasetName)
00040 {
00041 #if H5_VERS_MAJOR>=1 && H5_VERS_MINOR>=8
00042     // This is a nice method for testing existence, introduced in HDF5 1.8.0
00043     htri_t dataset_status = H5Lexists(mFileId, rDatasetName.c_str(), H5P_DEFAULT);
00044     return (dataset_status>0);
00045 #else
00046     bool result=false;
00047     // This is not a nice way of doing it because the error stack produces a load of 'HDF failed' output.
00048     // The "TRY" macros are a convenient way to temporarily turn the error stack off.
00049     H5E_BEGIN_TRY
00050     {
00051         hid_t dataset_id = H5Dopen(mFileId, rDatasetName.c_str());
00052         if (dataset_id>0)
00053         {
00054             H5Dclose(dataset_id);
00055             result = true;
00056         }
00057     }
00058     H5E_END_TRY;
00059 
00060     return result;
00061 #endif
00062 }
00063 
00064 void AbstractHdf5Access::SetUnlimitedDatasetId()
00065 {
00066     // Now deal with the unlimited dimension
00067 
00068     // In terms of an Unlimited dimension dataset:
00069     // * Files pre - r16738 (inc. Release 3.1 and earlier) use simply "Time" for "Data"'s unlimited variable.
00070     // * Files generated by r16738 - r18257 used "<DatasetName>_Time" for "<DatasetName>"'s unlimited variable,
00071     //   - These are not to be used and there is no backwards compatibility for them, since they weren't in a release.
00072     // * Files post r18257 (inc. Release 3.2 onwards) use "<DatasetName>_Unlimited" for "<DatasetName>"'s
00073     //   unlimited variable,
00074     //   - a new attribute "Name" has been added to the Unlimited Dataset to allow it to assign
00075     //     any name to the unlimited variable. Which can then be easily read by Hdf5DataReader.
00076     //   - if this dataset is missing we look for simply "Time" to remain backwards compatible with Releases <= 3.1
00077 
00078     if (DoesDatasetExist(mDatasetName + "_Unlimited"))
00079     {
00080         mUnlimitedDatasetId = H5Dopen(mFileId, (mDatasetName + "_Unlimited").c_str());
00081         hid_t name_attribute_id = H5Aopen_name(mUnlimitedDatasetId, "Name");
00082         hid_t unit_attribute_id = H5Aopen_name(mUnlimitedDatasetId, "Unit");
00083 
00084         hid_t attribute_type  = H5Aget_type(name_attribute_id);
00085 
00086         // Read into it.
00087         char* string_array = (char *)malloc(sizeof(char)*MAX_STRING_SIZE);
00088         H5Aread( name_attribute_id, attribute_type, string_array);
00089         std::string name_string(&string_array[0]);
00090         mUnlimitedDimensionName = name_string;
00091 
00092         H5Aread( unit_attribute_id, attribute_type, string_array);
00093         std::string unit_string(&string_array[0]);
00094         mUnlimitedDimensionUnit = unit_string;
00095 
00096         free(string_array);
00097         H5Tclose(attribute_type);
00098         H5Aclose(name_attribute_id);
00099         H5Aclose(unit_attribute_id);
00100     }
00101     else if (DoesDatasetExist("Time"))
00102     {
00103         mUnlimitedDimensionName = "Time";
00104         mUnlimitedDimensionUnit = "msec";
00105         mUnlimitedDatasetId = H5Dopen(mFileId, mUnlimitedDimensionName.c_str());
00106     }
00107     else
00108     {
00109         NEVER_REACHED;
00110     }
00111     mIsUnlimitedDimensionSet = true;
00112 }
00113 
00114 AbstractHdf5Access::AbstractHdf5Access(const std::string& rDirectory,
00115                    const std::string& rBaseName,
00116                    const std::string& rDatasetName,
00117                    bool makeAbsolute)
00118  : mBaseName(rBaseName),
00119    mDatasetName(rDatasetName),
00120    mIsDataComplete(true),
00121    mIsUnlimitedDimensionSet(false)
00122 {
00123     RelativeTo::Value relative_to;
00124     if (makeAbsolute)
00125     {
00126         relative_to = RelativeTo::ChasteTestOutput;
00127     }
00128     else
00129     {
00130         relative_to = RelativeTo::Absolute;
00131     }
00132     mDirectory.SetPath(rDirectory, relative_to);
00133 }
00134 
00135 AbstractHdf5Access::AbstractHdf5Access(const FileFinder& rDirectory,
00136                                        const std::string& rBaseName,
00137                                        const std::string& rDatasetName)
00138  : mBaseName(rBaseName),
00139    mDatasetName(rDatasetName),
00140    mDirectory(rDirectory),
00141    mIsDataComplete(true),
00142    mIsUnlimitedDimensionSet(false)
00143 {
00144 }
00145 
00146 AbstractHdf5Access::~AbstractHdf5Access()
00147 {
00148 }
00149 
00150 
00151 bool AbstractHdf5Access::IsDataComplete()
00152 {
00153     return mIsDataComplete;
00154 }
00155 
00156 
00157 std::vector<unsigned> AbstractHdf5Access::GetIncompleteNodeMap()
00158 {
00159     return mIncompleteNodeIndices;
00160 }
00161 
00162 std::string AbstractHdf5Access::GetUnlimitedDimensionName()
00163 {
00164     return mUnlimitedDimensionName;
00165 }
00166 
00167 std::string AbstractHdf5Access::GetUnlimitedDimensionUnit()
00168 {
00169     return mUnlimitedDimensionUnit;
00170 }
00171 
00172 void AbstractHdf5Access::SetMainDatasetRawChunkCache()
00173 {
00174     // 128 M cache for raw data. 12799 is a prime number which is 100 x larger
00175     // than the number of 1 MB chunks (the largest we have tested with) which
00176     // could fit into the 128 M cache. 0.75 for the last argument is the default.
00177     // See: http://www.hdfgroup.org/HDF5/doc/RM/RM_H5P.html#Property-SetChunkCache
00178 
00179     hsize_t max_objects_in_chunk_cache = 12799u;
00180     hsize_t max_bytes_in_cache = 128u*1024u*1024u;
00181 #if H5_VERS_MAJOR>=1 && H5_VERS_MINOR>=8 && H5_VERS_RELEASE>=3 // HDF5 1.8.3+
00182     // These methods set the cache on a dataset basis
00183     hid_t dapl_id = H5Dget_access_plist( mVariablesDatasetId );
00184     H5Pset_chunk_cache( dapl_id,
00185                         max_objects_in_chunk_cache ,
00186                         max_bytes_in_cache ,
00187                         H5D_CHUNK_CACHE_W0_DEFAULT);
00188 #else
00189     // These older methods set the cache on a file basis
00190     hid_t fapl_id = H5Fget_access_plist( mFileId );
00191     H5Pset_cache( fapl_id,
00192                   0, // unused
00193                   max_objects_in_chunk_cache,
00194                   max_bytes_in_cache,
00195                   0.75); // default value
00196 #endif
00197 }
00198 
00199 

Generated by  doxygen 1.6.2