Skip to content

Commit 774759f

Browse files
committed
Few more cleanings in hdf5 handing methods
1 parent b9c3646 commit 774759f

File tree

3 files changed

+64
-109
lines changed

3 files changed

+64
-109
lines changed

src/io/APRWriter.hpp

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -339,21 +339,17 @@ class APRWriter {
339339

340340
file_name = file_name + "_apr";
341341

342-
hdf5_create_file_blosc(hdf5_file_name);
342+
hid_t fid = hdf5_create_file_blosc(hdf5_file_name);
343+
// fid = H5Fopen(hdf5_file_name.c_str(),H5F_ACC_RDWR,H5P_DEFAULT);
343344

344345
//hdf5 inits
345-
hid_t fid, pr_groupid, obj_id;
346+
hid_t pr_groupid, obj_id;
346347
H5G_info_t info;
347-
348348
hsize_t dims_out[2];
349-
350349
hsize_t rank = 1;
351-
352350
hsize_t dims;
353351
hsize_t dim_a=1;
354352

355-
fid = H5Fopen(hdf5_file_name.c_str(),H5F_ACC_RDWR,H5P_DEFAULT);
356-
357353
//Get the group you want to open
358354

359355
//////////////////////////////////////////////////////////////////
@@ -575,10 +571,11 @@ class APRWriter {
575571

576572
file_name = file_name + "_paraview";
577573

578-
hdf5_create_file_blosc(hdf5_file_name);
574+
hid_t fid = hdf5_create_file_blosc(hdf5_file_name);
575+
// hid_t fid = H5Fopen(hdf5_file_name.c_str(),H5F_ACC_RDWR,H5P_DEFAULT);
579576

580577
//hdf5 inits
581-
hid_t fid, pr_groupid, obj_id;
578+
hid_t pr_groupid, obj_id;
582579
H5G_info_t info;
583580

584581
hsize_t dims_out[2];
@@ -588,7 +585,6 @@ class APRWriter {
588585
hsize_t dims;
589586
hsize_t dim_a=1;
590587

591-
fid = H5Fopen(hdf5_file_name.c_str(),H5F_ACC_RDWR,H5P_DEFAULT);
592588

593589
//Get the group you want to open
594590

@@ -732,20 +728,16 @@ class APRWriter {
732728

733729
file_name = file_name + "_apr_extra_parts";
734730

735-
hdf5_create_file_blosc(hdf5_file_name);
731+
hid_t fid = hdf5_create_file_blosc(hdf5_file_name);
732+
// fid = H5Fopen(hdf5_file_name.c_str(),H5F_ACC_RDWR,H5P_DEFAULT);
736733

737734
//hdf5 inits
738-
hid_t fid, pr_groupid, obj_id;
735+
hid_t pr_groupid, obj_id;
739736
H5G_info_t info;
740-
741737
hsize_t dims_out[2];
742-
743738
hsize_t rank = 1;
744-
745739
hsize_t dims;
746740

747-
fid = H5Fopen(hdf5_file_name.c_str(),H5F_ACC_RDWR,H5P_DEFAULT);
748-
749741
//Get the group you want to open
750742

751743
//////////////////////////////////////////////////////////////////
@@ -864,7 +856,7 @@ class APRWriter {
864856
}
865857
};
866858

867-
template<> struct APRWriter::Hdf5Type<int8_t> {static hid_t type() {return H5T_NATIVE_INT8_g;}};
859+
template<> struct APRWriter::Hdf5Type<int8_t> {static hid_t type() {return H5T_NATIVE_INT8;}};
868860
template<> struct APRWriter::Hdf5Type<uint8_t> {static hid_t type() {return H5T_NATIVE_UINT8;}};
869861
template<> struct APRWriter::Hdf5Type<int16_t> {static hid_t type() {return H5T_NATIVE_INT16;}};
870862
template<> struct APRWriter::Hdf5Type<uint16_t> {static hid_t type() {return H5T_NATIVE_UINT16;}};

src/io/hdf5functions_blosc.cpp

Lines changed: 52 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
//////////////////////////////////////////
1010

1111
#include "hdf5functions_blosc.h"
12-
#include <array>
1312

1413

1514
/**
@@ -19,118 +18,83 @@ void register_blosc(){
1918
register_blosc(nullptr, nullptr);
2019
}
2120

21+
/**
22+
* reads data from hdf5
23+
*/
2224
void hdf5_load_data_blosc(hid_t obj_id, hid_t data_type, void* buff, const char* data_name) {
23-
//stuff required to pull the data in
24-
hid_t data_id = H5Dopen2(obj_id , data_name ,H5P_DEFAULT);
25-
H5Dread(data_id, data_type, H5S_ALL, H5S_ALL,H5P_DEFAULT, buff);
25+
hid_t data_id = H5Dopen2(obj_id, data_name ,H5P_DEFAULT);
26+
H5Dread(data_id, data_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, buff);
2627
H5Dclose(data_id);
2728
}
2829

29-
void hdf5_write_data_blosc(hid_t obj_id,hid_t type_id,const char* ds_name,hsize_t rank,hsize_t* dims, void* data ,unsigned int comp_type,unsigned int comp_level,unsigned int shuffle){
30-
//writes data to the hdf5 file or group identified by obj_id of hdf5 datatype data_type
31-
32-
unsigned int cd_values[7];
33-
//Declare the required hdf5 shiz
34-
hid_t space_id,dset_id,plist_id;
35-
//hsize_t *cdims = new hsize_t[rank]; //chunking dims
36-
hsize_t cdims[1]; //chunking dims
37-
38-
//compression parameters
39-
40-
41-
//int szip_options_mask = H5_SZIP_NN_OPTION_MASK;
42-
//int szip_pixels_per_block = 8;
30+
/**
31+
* writes data to the hdf5 file or group identified by obj_id of hdf5 datatype data_type
32+
*/
33+
void hdf5_write_data_blosc(hid_t obj_id, hid_t type_id, const char *ds_name, hsize_t rank, hsize_t *dims, void *data ,unsigned int comp_type,unsigned int comp_level,unsigned int shuffle) {
34+
hid_t plist_id = H5Pcreate(H5P_DATASET_CREATE);
35+
36+
// Dataset must be chunked for compression
37+
const uint64_t max_size = 100000;
38+
hsize_t cdims = (dims[0] < max_size) ? dims[0] : max_size;
4339
rank = 1;
44-
//dataspace id
45-
space_id = H5Screate_simple(rank, dims, NULL);
46-
plist_id = H5Pcreate(H5P_DATASET_CREATE);
47-
48-
/* Dataset must be chunked for compression */
49-
//cdims[0] = 20; //Could try playing with these for compression performance
50-
//cdims[1] = 20;
51-
52-
uint64_t max_size = 100000;
53-
54-
if (dims[0] < max_size){
55-
cdims[0] = dims[0];
56-
}else {
57-
cdims[0] = max_size;
58-
}
40+
H5Pset_chunk(plist_id, rank, &cdims);
5941

60-
H5Pset_chunk(plist_id, rank, cdims);
61-
6242
/////SET COMPRESSION TYPE /////
63-
64-
/* But you can also taylor Blosc parameters to your needs */
65-
/* 0 to 3 (inclusive) param slots are reserved. */
66-
cd_values[4] = comp_level; /* compression level */
67-
cd_values[5] = shuffle; /* 0: shuffle not active, 1: shuffle active */
68-
cd_values[6] = comp_type; /* the actual compressor to use */
69-
70-
/* Set the filter with 7 params */
71-
H5Pset_filter(plist_id, FILTER_BLOSC, H5Z_FLAG_OPTIONAL, 7, cd_values);
72-
43+
// But you can also taylor Blosc parameters to your needs
44+
// 0 to 3 (inclusive) param slots are reserved.
45+
const int numOfParams = 7;
46+
unsigned int cd_values[numOfParams];
47+
cd_values[4] = comp_level; // compression level
48+
cd_values[5] = shuffle; // 0: shuffle not active, 1: shuffle active
49+
cd_values[6] = comp_type; // the actual compressor to use
50+
H5Pset_filter(plist_id, FILTER_BLOSC, H5Z_FLAG_OPTIONAL, numOfParams, cd_values);
51+
7352
//create write and close
74-
dset_id = H5Dcreate2(obj_id,ds_name,type_id,space_id,H5P_DEFAULT,plist_id,H5P_DEFAULT);
75-
53+
hid_t space_id = H5Screate_simple(rank, dims, NULL);
54+
hid_t dset_id = H5Dcreate2(obj_id, ds_name, type_id, space_id, H5P_DEFAULT, plist_id, H5P_DEFAULT);
7655
H5Dwrite(dset_id,type_id,H5S_ALL,H5S_ALL,H5P_DEFAULT,data);
77-
7856
H5Dclose(dset_id);
57+
58+
H5Pclose(plist_id);
7959
}
8060

61+
/**
62+
* writes data to the hdf5 file or group identified by obj_id of hdf5 datatype data_type
63+
*/
8164
void hdf5_write_attribute_blosc(hid_t obj_id,hid_t type_id,const char* attr_name,hsize_t rank,hsize_t* dims, void* data ){
82-
//writes data to the hdf5 file or group identified by obj_id of hdf5 datatype data_type
83-
84-
//Declare the required hdf5 shiz
85-
hid_t space_id, attr_id;
86-
//hsize_t *cdims = new hsize_t[rank]; //chunking dims
87-
88-
space_id = H5Screate_simple(rank, dims, NULL);
89-
//plist_id = H5Pcreate(H5P_ATTRIBUTE_CREATE);
90-
91-
attr_id = H5Acreate2( obj_id, attr_name, type_id, space_id, H5P_DEFAULT, H5P_DEFAULT);
92-
93-
H5Awrite(attr_id, type_id, data );
65+
hid_t space_id = H5Screate_simple(rank, dims, NULL);
66+
hid_t attr_id = H5Acreate2( obj_id, attr_name, type_id, space_id, H5P_DEFAULT, H5P_DEFAULT);
67+
H5Awrite(attr_id, type_id, data);
9468
H5Aclose(attr_id);
95-
69+
H5Sclose(space_id);
9670
}
9771

98-
void hdf5_write_string_blosc(hid_t obj_id,const char* attr_name,std::string output_str){
99-
//
100-
// Writes string information as an attribute
101-
//
102-
//
103-
104-
hid_t aid = H5Screate(H5S_SCALAR);
105-
hid_t atype = H5Tcopy (H5T_C_S1);
106-
72+
/**
73+
* Writes string information as an attribute
74+
*/
75+
void hdf5_write_string_blosc(hid_t obj_id, const char *attr_name, const std::string &output_str) {
10776
if (output_str.size() > 0){
108-
H5Tset_size (atype, output_str.size());
77+
hid_t aid = H5Screate(H5S_SCALAR);
78+
hid_t atype = H5Tcopy (H5T_C_S1);
79+
H5Tset_size(atype, output_str.size());
10980
hid_t attr = H5Acreate2(obj_id, attr_name, atype, aid, H5P_DEFAULT,H5P_DEFAULT);
11081
H5Awrite (attr, atype,output_str.c_str());
82+
H5Tclose(atype);
83+
H5Aclose(attr);
84+
H5Sclose(aid);
11185
}
11286
}
11387

114-
void hdf5_create_file_blosc(std::string file_name){
115-
//creates the hdf5 file before you can then write to it
116-
117-
hid_t fid; //file id
118-
119-
//fid = H5F.create(name,'H5F_ACC_EXCL', 'H5P_DEFAULT', 'H5P_DEFAULT'); %create the file (throws error if it already exists)
120-
fid = H5Fcreate(file_name.c_str(),H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); //this writes over the current file
121-
122-
//close shiz
123-
H5Fclose(fid);
124-
88+
/**
89+
* creates the hdf5 file before you can then write to it
90+
*/
91+
hid_t hdf5_create_file_blosc(std::string file_name){
92+
return H5Fcreate(file_name.c_str(),H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); //this writes over the current file
12593
}
12694

12795
void write_main_paraview_xdmf_xml(std::string save_loc,std::string file_name,uint64_t num_parts){
128-
std::string hdf5_file_name = file_name + ".h5";
129-
std::string xdmf_file_name = save_loc + file_name + ".xmf";
130-
131-
std::ofstream myfile;
132-
myfile.open (xdmf_file_name);
133-
96+
const std::string hdf5_file_name = file_name + ".h5";
97+
std::ofstream myfile(save_loc + file_name + ".xmf");
13498
myfile << "<?xml version=\"1.0\" ?>\n";
13599
myfile << "<!DOCTYPE Xdmf SYSTEM \"Xdmf.dtd\" []>\n";
136100
myfile << "<Xdmf Version=\"2.0\" xmlns:xi=\"[http://www.w3.org/2001/XInclude]\">\n";
@@ -166,6 +130,5 @@ void write_main_paraview_xdmf_xml(std::string save_loc,std::string file_name,uin
166130
myfile << " </Grid>\n";
167131
myfile << " </Domain>\n";
168132
myfile << "</Xdmf>\n";
169-
170133
myfile.close();
171134
}

src/io/hdf5functions_blosc.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ extern "C" {
2323

2424

2525
void register_blosc();
26-
void hdf5_create_file_blosc(std::string file_name);
26+
hid_t hdf5_create_file_blosc(std::string file_name);
2727
void hdf5_load_data_blosc(hid_t obj_id,hid_t data_type,void* buff, const char* data_name);
2828
void hdf5_write_attribute_blosc(hid_t obj_id,hid_t type_id,const char* attr_name,hsize_t rank,hsize_t* dims, void* data );
29-
void hdf5_write_string_blosc(hid_t obj_id,const char* attr_name,std::string output_str);
29+
void hdf5_write_string_blosc(hid_t obj_id,const char* attr_name, const std::string &output_str);
3030
void hdf5_write_data_blosc(hid_t obj_id,hid_t type_id,const char* ds_name,hsize_t rank,hsize_t* dims, void* data ,unsigned int comp_type,unsigned int comp_level,unsigned int shuffle);
3131

3232
void write_main_paraview_xdmf_xml(std::string save_loc,std::string file_name,uint64_t num_parts);

0 commit comments

Comments
 (0)