Skip to content

Commit 5b705f1

Browse files
authored
add logic to bp plugin to limit max hdf5 file handles open (#19043)
* add logic to bp plugin to limit max hdf5 file handles open * update release notes
1 parent db9ecdd commit 5b705f1

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

src/databases/Blueprint/avtBlueprintTreeCache.C

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ using namespace mfem;
4545
///
4646
/// avtBlueprintTreeCache::CacheMap Interface
4747
///
48+
//
49+
// Modifications:
50+
// Cyrus Harrison, Tue Nov 7 15:35:20 PST 2023
51+
// Add logic to limit max number of file handles held open
52+
//
4853
//----------------------------------------------------------------------------/
4954
class avtBlueprintTreeCache::CacheMap
5055
{
@@ -59,13 +64,17 @@ class avtBlueprintTreeCache::CacheMap
5964

6065
uint64 TotalSize() const;
6166
uint64 TotalHDF5Ids() const;
67+
68+
void CloseHDF5FileHandle(hid_t h5_file_id);
6269

6370
void Release();
6471

6572
private:
6673
std::map<int,Node> m_nodes;
6774
std::map<int,Node> m_sidre_nodes;
75+
int m_max_file_handles;
6876
std::map<std::string,hid_t> m_h5_ids;
77+
std::vector<std::string> m_h5_path_open_order;
6978

7079
};
7180

@@ -79,6 +88,7 @@ class avtBlueprintTreeCache::CacheMap
7988
avtBlueprintTreeCache::CacheMap::CacheMap()
8089
: m_nodes(),
8190
m_sidre_nodes(),
91+
m_max_file_handles(256),
8292
m_h5_ids()
8393
{}
8494

@@ -104,12 +114,11 @@ avtBlueprintTreeCache::CacheMap::Release()
104114
{
105115

106116
hid_t h5_file_id = (*itr).second;
107-
// close the hdf5 file
108-
CHECK_HDF5_ERROR(H5Fclose(h5_file_id),
109-
"Error closing HDF5 file handle: " << h5_file_id);
117+
CloseHDF5FileHandle(h5_file_id);
110118
}
111119

112120
m_h5_ids.clear();
121+
m_h5_path_open_order.clear();
113122

114123
}
115124

@@ -132,8 +141,21 @@ hid_t
132141
avtBlueprintTreeCache::CacheMap::FetchHDF5Id(const std::string &file_path)
133142
{
134143
hid_t h5_file_id = -1;
144+
145+
// check if handle is open
135146
if ( m_h5_ids.find(file_path) == m_h5_ids.end() )
136147
{
148+
// handle is not open, check if we are at max number of handles
149+
if( TotalHDF5Ids() > m_max_file_handles )
150+
{
151+
// take the first entry and close it
152+
std::string h5_file_path_to_close = m_h5_path_open_order[0];
153+
hid_t h5_id_to_close = m_h5_ids[h5_file_path_to_close];
154+
CloseHDF5FileHandle(h5_id_to_close);
155+
m_h5_path_open_order.erase(m_h5_path_open_order.begin());
156+
m_h5_ids.erase(h5_file_path_to_close);
157+
}
158+
137159
// assume fetch_path points to a hdf5 dataset
138160
// open the hdf5 file for reading
139161
h5_file_id = H5Fopen(file_path.c_str(),
@@ -142,6 +164,7 @@ avtBlueprintTreeCache::CacheMap::FetchHDF5Id(const std::string &file_path)
142164
CHECK_HDF5_ERROR(h5_file_id,
143165
"Error opening HDF5 file for reading: " << file_path);
144166
BP_PLUGIN_INFO("opened " << file_path << " hdf5 id = " << h5_file_id);
167+
m_h5_path_open_order.push_back(file_path);
145168
m_h5_ids[file_path] = h5_file_id;
146169
}
147170
else
@@ -152,6 +175,13 @@ avtBlueprintTreeCache::CacheMap::FetchHDF5Id(const std::string &file_path)
152175
return h5_file_id;
153176
}
154177

178+
//----------------------------------------------------------------------------/
179+
void avtBlueprintTreeCache::CacheMap::CloseHDF5FileHandle(hid_t h5_file_id)
180+
{
181+
// close the hdf5 file
182+
CHECK_HDF5_ERROR(H5Fclose(h5_file_id),
183+
"Error closing HDF5 file handle: " << h5_file_id);
184+
}
155185

156186
//----------------------------------------------------------------------------/
157187
uint64

src/resources/help/en_US/relnotes3.4.0.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@
8282
<li>Removed the HDF 4 support in the Enzo reader.</li>
8383
<li>VisIt's Blueprint reader now supports materials with material numbers that do NOT fall in the range [0, N), where N is the number of materials.</li>
8484
<li>VisIt's Blueprint reader now detects high-order volume fractions fields following the naming pattern <i>volume_fraction_ZZZ</i> as a material.</li>
85+
<li>VisIt's Blueprint reader now limits the total number of open HDF5 file handles.</li>
86+
8587
</ul>
8688

8789
<a name="Plot_changes"></a>

0 commit comments

Comments
 (0)