瀏覽代碼

Initial commit

Matthias Vogelgesang 9 年之前
當前提交
1f0229a95e
共有 8 個文件被更改,包括 339 次插入0 次删除
  1. 2 0
      Makefile
  2. 66 0
      c.mk
  3. 7 0
      hdf5perf/Makefile
  4. 100 0
      hdf5perf/hdf5perf.c
  5. 6 0
      tiffperf/Makefile
  6. 104 0
      tiffperf/tiffperf.c
  7. 42 0
      tiffperf/timer.c
  8. 12 0
      tiffperf/timer.h

+ 2 - 0
Makefile

@@ -0,0 +1,2 @@
+all:
+	@make -C tiffperf

+ 66 - 0
c.mk

@@ -0,0 +1,66 @@
+#
+# 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
+#
+
+ifeq ($V, 1)
+	Q =
+else
+	Q = @
+endif
+
+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 -O2
+CFLAGS += $(PKG_CFLAGS) -std=c99
+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 $@"
+	$(Q)$(CC) -c $(CFLAGS) -o $@ $<
+
+$(BIN): $(OBJS)
+	@echo " LD $@"
+	$(Q)$(CC) $(OBJS) -o $@ $(LDFLAGS)
+
+clean:
+	$(Q)rm -f $(BIN) $(OBJS)
+
+install: $(BIN)
+	$(Q)install -D -m 755 $(BIN) $(DESTDIR)$(bindir)/$(BIN)

+ 7 - 0
hdf5perf/Makefile

@@ -0,0 +1,7 @@
+SRC = hdf5perf.c
+BIN = hdf5perf
+PKG_DEPS = glib-2.0
+CFLAGS = -Wall -Werror -std=c99 -O3 -DH5_USE_16_API
+LDFLAGS = -lhdf5
+
+include ../c.mk

+ 100 - 0
hdf5perf/hdf5perf.c

@@ -0,0 +1,100 @@
+#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;
+}

+ 6 - 0
tiffperf/Makefile

@@ -0,0 +1,6 @@
+BIN=tiffperf
+SRC=tiffperf.c timer.c
+PKG_DEPS = libtiff-4
+CFLAGS = -g -ggdb
+
+include ../c.mk

+ 104 - 0
tiffperf/tiffperf.c

@@ -0,0 +1,104 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <tiffio.h>
+#include "timer.h"
+
+typedef struct {
+    unsigned width;
+    unsigned height;
+    unsigned bits;
+    unsigned num;
+} ImageSize;
+
+static void
+write_tiff_data (TIFF *tiff, uint16_t *data, ImageSize *size)
+{
+    TIFFSetField (tiff, TIFFTAG_IMAGEWIDTH, size->width);
+    TIFFSetField (tiff, TIFFTAG_IMAGELENGTH, size->height);
+    TIFFSetField (tiff, TIFFTAG_BITSPERSAMPLE, size->bits);
+    TIFFSetField (tiff, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
+    TIFFSetField (tiff, TIFFTAG_SAMPLESPERPIXEL, 1);
+    TIFFSetField (tiff, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
+    TIFFSetField (tiff, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize (tiff, 0));
+
+    for (unsigned y = 0; y < size->height; y++, data += size->width) {
+        TIFFWriteScanline (tiff, data, y, 0);
+    }
+}
+
+static void
+write_multi_tiff (uint16_t *data, ImageSize *size)
+{
+    TIFF *tiff;
+
+    tiff = TIFFOpen ("multi.tiff", "w");
+
+    for (unsigned i = 0; i < size->num; i++) {
+        TIFFSetField (tiff, TIFFTAG_SUBFILETYPE, FILETYPE_PAGE);
+        TIFFSetField (tiff, TIFFTAG_PAGENUMBER, i, size->num);
+
+        write_tiff_data (tiff, data, size);
+
+        TIFFWriteDirectory (tiff);
+    }
+
+    TIFFClose (tiff);
+}
+
+static void
+write_single_tiffs (uint16_t *data, ImageSize *size)
+{
+    char fname[256];
+
+    for (unsigned i = 0; i < size->num; i++) {
+        TIFF *tiff;
+
+        snprintf (fname, 256, "single-%05i.tiff", i);
+        tiff = TIFFOpen (fname, "w");
+        write_tiff_data (tiff, data, size);
+        TIFFClose (tiff);
+    }
+}
+
+static void
+print_time (const char *fmt, ImageSize *size, Timer *timer)
+{
+    size_t total;
+
+    total = size->width * size->height * size->num * 2;
+    printf (fmt, ((double) total) / 1000. / 1000. / timer_get_seconds (timer));
+}
+
+int
+main(int argc, char const* argv[])
+{
+    Timer *timer;
+
+    ImageSize size = {
+        .width = 2048,
+        .height = 2048,
+        .bits = 16,
+        .num = 20
+    };
+
+    uint16_t *data;
+
+    data = malloc (size.width * size.height * 2);
+    timer = timer_new ();
+
+    timer_start (timer);
+    write_multi_tiff (data, &size);
+    timer_stop (timer);
+    print_time ("Multi TIFF: %f MB/s\n", &size, timer);
+
+    timer_start (timer);
+    write_single_tiffs (data, &size);
+    timer_stop (timer);
+    print_time ("Single TIFF: %f MB/s\n", &size, timer);
+
+    timer_destroy (timer);
+    free (data);
+    return 0;
+}

+ 42 - 0
tiffperf/timer.c

@@ -0,0 +1,42 @@
+#include <sys/time.h>
+#include <stdlib.h>
+#include "timer.h"
+
+struct _Timer {
+    struct timeval start;
+    struct timeval end;
+};
+
+
+Timer *
+timer_new (void)
+{
+    Timer *t = (Timer *) malloc (sizeof (Timer));
+    return t;
+}
+
+void
+timer_destroy (Timer *t)
+{
+    free (t);
+}
+
+void
+timer_start (Timer *t)
+{
+    gettimeofday (&t->start, NULL);
+}
+
+void
+timer_stop (Timer *t)
+{
+    gettimeofday (&t->end, NULL);
+}
+
+double
+timer_get_seconds (Timer *t)
+{
+    long seconds = t->end.tv_sec - t->start.tv_sec;
+    long useconds = t->end.tv_usec - t->start.tv_usec;
+    return seconds + useconds / 1000.0 / 1000.0;
+}

+ 12 - 0
tiffperf/timer.h

@@ -0,0 +1,12 @@
+#ifndef TIMER_H
+#define TIMER_H
+
+typedef struct _Timer Timer;
+
+Timer * timer_new           (void);
+void    timer_destroy       (Timer *t);
+void    timer_start         (Timer *t);
+void    timer_stop          (Timer *t);
+double  timer_get_seconds   (Timer *t);
+
+#endif