hdf5perf.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include <sys/stat.h>
  2. #include <unistd.h>
  3. #include <fcntl.h>
  4. #include <glib.h>
  5. #include <hdf5.h>
  6. static const int WIDTH = 2048;
  7. static const int HEIGHT = 2048;
  8. static const int N_DARKS = 10;
  9. static const int N_FLATS = 10;
  10. static const int N_PROJS = 200;
  11. static void
  12. write_dataset (hid_t file, const char *name, const guint16 *data, int n_items)
  13. {
  14. GTimer *timer;
  15. hid_t datatype;
  16. hid_t dataspace;
  17. hid_t dataset;
  18. hsize_t dims[3];
  19. timer = g_timer_new ();
  20. dims[0] = WIDTH;
  21. dims[1] = HEIGHT;
  22. dims[2] = n_items;
  23. datatype = H5Tcopy (H5T_STD_U16LE);
  24. dataspace = H5Screate_simple (3, dims, NULL);
  25. dataset = H5Dcreate (file, name, datatype, dataspace, H5P_DEFAULT);
  26. H5Dwrite (dataset, H5T_STD_U16LE, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
  27. H5Sclose (dataspace);
  28. H5Dclose (dataset);
  29. H5Tclose (datatype);
  30. g_timer_destroy (timer);
  31. }
  32. static void
  33. create_individual_data_sets (hid_t file, const guint16 *data)
  34. {
  35. write_dataset (file, "projs", data, N_PROJS);
  36. write_dataset (file, "darks", data, N_DARKS);
  37. write_dataset (file, "flats", data, N_FLATS);
  38. }
  39. static void
  40. create_single_dataset (hid_t file, const guint16 *data)
  41. {
  42. write_dataset (file, "all", data, N_DARKS + N_FLATS + N_PROJS);
  43. }
  44. static gdouble
  45. run (void (*func)(hid_t, const guint16 *), const char *fname, const guint16 *data)
  46. {
  47. hid_t file;
  48. int fid;
  49. GTimer *timer;
  50. gdouble elapsed;
  51. timer = g_timer_new ();
  52. g_timer_start (timer);
  53. file = H5Fcreate (fname, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
  54. func (file, data);
  55. H5Fflush (file, H5F_SCOPE_GLOBAL);
  56. H5Fclose (file);
  57. /* H5Fflush is extremely unreliable. So let's take out the hammer ... */
  58. fid = open (fname, O_RDWR | O_SYNC);
  59. fsync (fid);
  60. close (fid);
  61. g_timer_stop (timer);
  62. elapsed = g_timer_elapsed (timer, NULL);
  63. g_timer_destroy (timer);
  64. return elapsed;
  65. }
  66. int
  67. main (int argc, char const* argv[])
  68. {
  69. guint16 *data;
  70. gsize size;
  71. size = 2 * WIDTH * HEIGHT * (N_DARKS + N_FLATS + N_PROJS);
  72. data = g_malloc (size);
  73. g_print ("Single data set: %f MB/s\n",
  74. ((gdouble) size) / 1024. / 1024. / run (create_single_dataset, "bar.h5", data));
  75. g_print ("Multi data set: %f MB/s\n",
  76. ((gdouble) size) / 1024. / 1024. / run (create_individual_data_sets, "foo.h5", data));
  77. g_free (data);
  78. return 0;
  79. }