Skip to content

Instantly share code, notes, and snippets.

@smutch
Created March 18, 2018 23:10
Show Gist options
  • Save smutch/ef008136452f29cccd4e67771b1f947f to your computer and use it in GitHub Desktop.
Save smutch/ef008136452f29cccd4e67771b1f947f to your computer and use it in GitHub Desktop.
c:hdf5 compression test code
#include <stdio.h>
#include <hdf5.h>
#include <stdlib.h>
#include <hdf5_hl.h>
#include <math.h>
int main() {
uint dim = 512;
uint n_cell = (uint)pow(512, 3);
double fill_frac = 0.6;
uint fill_n = (uint)(fill_frac * n_cell);
float* data = calloc(n_cell, sizeof(float));
for (int ii = 0; ii < fill_n; ++ii) {
data[arc4random_uniform(n_cell)] = (float)(arc4random_uniform(100)*0.1);
}
hid_t f_id = H5Fcreate("test.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
hsize_t hdim[3] = {dim, dim, dim};
// compressed write ------------------------
hid_t dspace_id = H5Screate_simple(3, hdim, NULL);
hid_t plist_id = H5Pcreate(H5P_DATASET_CREATE);
hsize_t chunks[3] = {512, 512, 1};
H5Pset_chunk(plist_id, 3, chunks);
H5Pset_deflate(plist_id, 6);
hid_t dset_id = H5Dcreate(f_id, "compressed", H5T_NATIVE_FLOAT,
dspace_id, H5P_DEFAULT, plist_id, H5P_DEFAULT);
H5Dwrite(dset_id, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
H5Sclose(dspace_id);
H5Dclose(dset_id);
H5Pclose(plist_id);
// -----------------------------------------
// standard write --------------------------
H5LTmake_dataset_float(f_id, "standard", 3, hdim, data);
// -----------------------------------------
H5Fclose(f_id);
free(data);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment