Skip to content

Instantly share code, notes, and snippets.

@matze
Last active August 29, 2015 14:01
Show Gist options
  • Save matze/0cfa9db445e3ed70819d to your computer and use it in GitHub Desktop.
Save matze/0cfa9db445e3ed70819d to your computer and use it in GitHub Desktop.
HDF5 performance
#
# c.mk - Generic Makefile for Linux toy applications
#
# Required variables:
#
# - $(SRC): C source files
# - $(BIN): filename of linked binary
#
# Optional variables:
#
# - $(PKG_DEPS): List of pkg-config compatible packages
# - $(CFLAGS), $(LDFLAGS), GNU compliant directories
#
# Example Makefile:
#
# PKG_DEPS = glib-2.0
# SRC = foo.c
# BIN = bar
#
# include c.mk
#
OBJS = $(patsubst %.c,%.o,$(SRC))
# Determine C flags and ld flags
ifdef PKG_DEPS
PKG_CFLAGS = $(shell pkg-config --cflags $(PKG_DEPS))
PKG_LDFLAGS = $(shell pkg-config --libs $(PKG_DEPS))
else
PKG_CFLAGS =
PKG_LDFLAGS =
endif
CFLAGS ?= -Wall -Werror -std=c99 -O2
CFLAGS += $(PKG_CFLAGS)
LDFLAGS += $(PKG_LDFLAGS)
# GNU-compliant install directories
prefix ?= /usr/local
exec_prefix ?= $(prefix)
bindir ?= $(exec_prefix)/bin
# Targets
.PHONY: clean
all: $(BIN)
%.o: %.c
@echo " CC $@"
@$(CC) -c $(CFLAGS) -o $@ $<
$(BIN): $(OBJS)
@echo " LD $@"
@$(CC) $(OBJS) -o $@ $(LDFLAGS)
clean:
rm -f $(BIN) $(OBJS)
install: $(BIN)
install -D -m 755 $(BIN) $(DESTDIR)$(bindir)/$(BIN)
SRC = perf.c
BIN = perf
PKG_DEPS = glib-2.0
LDFLAGS = -lhdf5
include c.mk
#include <glib.h>
#include <hdf5.h>
static const int WIDTH = 2048;
static const int HEIGHT = 1024;
static const int N_DARKS = 10;
static const int N_FLATS = 10;
static const int N_PROJS = 100;
static void
write_dataset (hid_t file, const char *name, const float *data, int n_items)
{
GTimer *timer;
hid_t datatype;
hid_t dataspace;
hid_t dataset;
hsize_t dims[3];
timer = g_timer_new ();
dims[0] = WIDTH;
dims[1] = HEIGHT;
dims[2] = n_items;
datatype = H5Tcopy (H5T_NATIVE_FLOAT);
g_timer_start (timer);
dataspace = H5Screate_simple (3, dims, NULL);
g_timer_stop (timer);
g_print (" H5Screate_simple: %3.5fs\n", g_timer_elapsed(timer, NULL));
g_timer_start (timer);
dataset = H5Dcreate (file, name, datatype, dataspace, H5P_DEFAULT);
g_timer_stop (timer);
g_print (" H5Dcreate: %3.5fs\n", g_timer_elapsed(timer, NULL));
g_timer_start (timer);
H5Dwrite (dataset, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
g_timer_stop (timer);
g_print (" H5Dwrite: %3.5fs\n", g_timer_elapsed(timer, NULL));
H5Sclose (dataspace);
H5Dclose (dataset);
H5Tclose (datatype);
g_timer_destroy (timer);
}
static void
create_individual_data_sets (hid_t file, const float *data)
{
write_dataset (file, "projs", data, N_PROJS);
write_dataset (file, "darks", data, N_DARKS);
write_dataset (file, "flats", data, N_FLATS);
}
static void
create_single_dataset (hid_t file, const float *data)
{
write_dataset (file, "all", data, N_DARKS + N_FLATS + N_PROJS);
}
static void
run (void (*func)(hid_t, const float *), const char *fname, const float *data)
{
hid_t file;
GTimer *timer;
timer = g_timer_new ();
g_timer_start (timer);
file = H5Fcreate (fname, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
func (file, data);
H5Fflush (file, H5F_SCOPE_GLOBAL);
H5Fclose (file);
g_timer_stop (timer);
g_print (" Total: %3.5fs\n", g_timer_elapsed (timer, NULL));
g_timer_destroy (timer);
}
int
main (int argc, char const* argv[])
{
float *data;
data = g_malloc (sizeof(float) * WIDTH * HEIGHT * (N_DARKS + N_FLATS + N_PROJS));
g_print ("Single data set\n");
run (create_single_dataset, "bar.h5", data);
g_print ("\nIndividual data sets\n");
run (create_individual_data_sets, "foo.h5", data);
g_free (data);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment