SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SerializableHdf5File.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2010 Soeren Sonnenburg
8  * Copyright (C) 2010 Berlin Institute of Technology
9  */
10 
11 #include <shogun/lib/config.h>
12 #ifdef HAVE_HDF5
13 
17 
18 #define NOT_OPEN ((hid_t) -1)
19 
20 #define STR_KEY_FILETYPE "filetype"
21 #define STR_FILETYPE_00 \
22  "_SHOGUN_SERIALIZABLE_HDF5_FILE_V_00_"
23 
24 using namespace shogun;
25 
26 CSerializableHdf5File::type_item_t::type_item_t(const char* name_)
27 {
28  rank = 0;
29  dims[0] = dims[1] = 0;
30  dspace = dtype = dset = NOT_OPEN;
31  vltype = NULL;
32  y = x = sub_y = 0;
33  sparse_ptr = NULL;
34  name = name_;
35 }
36 
37 CSerializableHdf5File::type_item_t::~type_item_t()
38 {
39  if (dset >= 0) H5Dclose(dset);
40  if (dtype >= 0) H5Tclose(dtype);
41  if (dspace >= 0) H5Sclose(dspace);
42  if (vltype != NULL) SG_FREE(vltype);
43  /* Do not delete SPARSE_PTR */
44 }
45 
46 hid_t
47 CSerializableHdf5File::sizeof_sparsetype() {
48  return H5Tget_size(TYPE_INDEX) + H5Tget_size(H5T_STD_REF_OBJ);
49 }
50 hid_t
51 CSerializableHdf5File::new_sparsetype()
52 {
53  hid_t result = H5Tcreate(H5T_COMPOUND, sizeof_sparsetype());
54 
55  if (H5Tinsert(result, STR_SPARSE_FPTR, H5Tget_size(TYPE_INDEX),
56  H5T_STD_REF_OBJ) < 0)
57  return NOT_OPEN;
58 
59  return result;
60 }
61 hobj_ref_t*
62 CSerializableHdf5File::get_ref_sparstype(void* sparse_buf) {
63  return (hobj_ref_t*)
64  ((char*) sparse_buf + H5Tget_size(TYPE_INDEX));
65 }
66 
67 hid_t
68 CSerializableHdf5File::new_sparseentrytype(EPrimitiveType ptype)
69 {
70  hid_t result = H5Tcreate(H5T_COMPOUND,
72  if (result < 0) return NOT_OPEN;
73 
74  if (H5Tinsert(result, STR_SPARSEENTRY_FINDEX,
75  HOFFSET(SGSparseVectorEntry<char>, feat_index), TYPE_INDEX)
76  < 0) return NOT_OPEN;
77  if (H5Tinsert(result, STR_SPARSEENTRY_ENTRY, TSGDataType
78  ::offset_sparseentry(ptype),
79  ptype2hdf5(ptype)) < 0) return NOT_OPEN;
80 
81  return result;
82 }
83 
84 hid_t
85 CSerializableHdf5File::ptype2hdf5(EPrimitiveType ptype)
86 {
87  switch (ptype) {
88  case PT_BOOL:
89  switch (sizeof (bool)) {
90  case 1: return H5T_NATIVE_UINT8;
91  case 2: return H5T_NATIVE_UINT16;
92  case 4: return H5T_NATIVE_UINT32;
93  case 8: return H5T_NATIVE_UINT64;
94  default: break;
95  }
96  break;
97  case PT_CHAR: return H5T_NATIVE_CHAR; break;
98  case PT_INT8: return H5T_NATIVE_INT8; break;
99  case PT_UINT8: return H5T_NATIVE_UINT8; break;
100  case PT_INT16: return H5T_NATIVE_INT16; break;
101  case PT_UINT16: return H5T_NATIVE_UINT16; break;
102  case PT_INT32: return H5T_NATIVE_INT32; break;
103  case PT_UINT32: return H5T_NATIVE_UINT32; break;
104  case PT_INT64: return H5T_NATIVE_INT64; break;
105  case PT_UINT64: return H5T_NATIVE_UINT64; break;
106  case PT_FLOAT32: return H5T_NATIVE_FLOAT; break;
107  case PT_FLOAT64: return H5T_NATIVE_DOUBLE; break;
108  case PT_FLOATMAX: return H5T_NATIVE_LDOUBLE; break;
109  case PT_COMPLEX128: return NOT_OPEN; break;
110  case PT_SGOBJECT: return NOT_OPEN; break;
111  case PT_UNDEFINED:
112  SG_SERROR("Type undefined\n");
113  return NOT_OPEN;
114  }
115 
116  return NOT_OPEN;
117 }
118 
119 hid_t
120 CSerializableHdf5File::new_stype2hdf5(EStructType stype,
121  EPrimitiveType ptype)
122 {
123  hid_t result = ptype2hdf5(ptype);
124 
125  switch (stype) {
126  case ST_NONE: result = H5Tcopy(result); break;
127  case ST_STRING: result = H5Tvlen_create(result); break;
128  case ST_SPARSE: result = new_sparsetype(); break;
129  default: break;
130  }
131 
132  return result;
133 }
134 
135 bool
136 CSerializableHdf5File::index2string(
137  char* dest, size_t n, EContainerType ctype, index_t y, index_t x)
138 {
139  switch (ctype) {
140  case CT_NDARRAY: SG_SNOTIMPLEMENTED
141  case CT_SCALAR: return false;
142  case CT_VECTOR: case CT_SGVECTOR: snprintf(dest, n, "y%u", y); break;
143  case CT_MATRIX: case CT_SGMATRIX: snprintf(dest, n, "y%u_x%u", y, x); break;
144  default: return false;
145  }
146 
147  return true;
148 }
149 
150 bool
151 CSerializableHdf5File::isequal_stype2hdf5(EStructType stype,
152  EPrimitiveType ptype,
153  hid_t htype)
154 {
155  hid_t pbuf = ptype2hdf5(ptype), pbuf2 = NOT_OPEN;
156 
157  bool to_close = false;
158  switch (stype) {
159  case ST_UNDEFINED:
160  case ST_NONE: break;
161  case ST_STRING:
162  to_close = true; pbuf = H5Tvlen_create(pbuf); break;
163  case ST_SPARSE:
164  to_close = true; pbuf = new_sparsetype();
165  pbuf2 = new_sparseentrytype(ptype); break;
166  }
167 
168  bool result = (H5Tequal(htype, pbuf) > 0)
169  || (pbuf2 >= 0 && H5Tequal(htype, pbuf2) > 0);
170 
171  if (pbuf2 >= 0 && H5Tclose(pbuf2) < 0) return false;
172  if (to_close && H5Tclose(pbuf) < 0) return false;
173  return result;
174 }
175 
176 bool
177 CSerializableHdf5File::dspace_select(EContainerType ctype, index_t y,
178  index_t x)
179 {
180  type_item_t* m = m_stack_type.back();
181 
182  if (H5Sselect_none(m->dspace) < 0) return false;
183 
184  hsize_t coord[2];
185  switch (ctype) {
186  case CT_NDARRAY: SG_NOTIMPLEMENTED
187  case CT_SCALAR: return false;
188  case CT_MATRIX: case CT_SGMATRIX: coord[1] = x; /* break; */
189  case CT_VECTOR: case CT_SGVECTOR: coord[0] = y; break;
190  default: return false;
191  }
192  if (H5Sselect_elements(m->dspace, H5S_SELECT_SET, 1, coord) < 0)
193  return false;
194 
195  return true;
196 }
197 
198 bool
199 CSerializableHdf5File::attr_write_scalar(
200  hid_t datatype, const char* name, const void* val)
201 {
202  hid_t dspace;
203  if ((dspace = H5Screate_simple(0, NULL, NULL)) < 0) return false;
204  hid_t dtype;
205  if ((dtype = H5Tcopy(datatype)) < 0) return false;
206  hid_t attr;
207  if ((attr = H5Acreate2(
208  m_stack_h5stream.back(), name, dtype, dspace,
209  H5P_DEFAULT, H5P_DEFAULT)) < 0) return false;
210 
211  if (H5Awrite(attr, datatype, val) < 0) return false;
212 
213  if (H5Aclose(attr) < 0) return false;
214  if (H5Tclose(dtype) < 0) return false;
215  if (H5Sclose(dspace) < 0) return false;
216 
217  return true;
218 }
219 
220 bool
221 CSerializableHdf5File::attr_write_string(
222  const char* name, const char* val)
223 {
224  hid_t dtype;
225  if ((dtype = H5Tcopy(H5T_C_S1)) < 0) return false;
226  if (H5Tset_size(dtype, strlen(val)+1) < 0) return false;
227 
228  if (!attr_write_scalar(dtype, name, val)) return false;
229 
230  if (H5Tclose(dtype) < 0) return false;
231 
232  return true;
233 }
234 
235 bool
236 CSerializableHdf5File::attr_exists(const char* name)
237 {
238  return H5Aexists(m_stack_h5stream.back(), name) > 0;
239 }
240 
241 size_t
242 CSerializableHdf5File::attr_get_size(const char* name)
243 {
244  if (!attr_exists(name)) return 0;
245 
246  hid_t attr;
247  if ((attr = H5Aopen(m_stack_h5stream.back(), name, H5P_DEFAULT))
248  < 0) return 0;
249 
250  hid_t dtype;
251  if ((dtype = H5Aget_type(attr)) < 0) return 0;
252 
253  size_t result = H5Tget_size(dtype);
254 
255  if (H5Tclose(dtype) < 0) return 0;
256  if (H5Aclose(attr) < 0) return 0;
257 
258  return result;
259 }
260 
261 bool
262 CSerializableHdf5File::attr_read_scalar(
263  hid_t datatype, const char* name, void* val)
264 {
265  if (!attr_exists(name)) return false;
266 
267  hid_t attr;
268  if ((attr = H5Aopen(m_stack_h5stream.back(), name, H5P_DEFAULT))
269  < 0) return false;
270 
271  hid_t dspace;
272  if ((dspace = H5Aget_space(attr)) < 0) return false;
273  if (H5Sget_simple_extent_type(dspace) != H5S_SCALAR) return false;
274 
275  hid_t dtype;
276  if ((dtype = H5Aget_type(attr)) < 0) return false;
277  if (H5Tequal(datatype, dtype) <= 0) return false;
278 
279  if (H5Aread(attr, datatype, val) < 0) return false;
280 
281  if (H5Tclose(dtype) < 0) return false;
282  if (H5Sclose(dspace) < 0) return false;
283  if (H5Aclose(attr) < 0) return false;
284 
285  return true;
286 }
287 
288 bool
289 CSerializableHdf5File::attr_read_string(
290  const char* name, char* val, size_t n)
291 {
292  size_t size = attr_get_size(name);
293  if (size == 0 || size > n) return false;
294 
295  hid_t dtype;
296  if ((dtype = H5Tcopy(H5T_C_S1)) < 0) return false;
297  if (H5Tset_size(dtype, size) < 0) return false;
298 
299  if (!attr_read_scalar(dtype, name, val)) return false;
300 
301  if (H5Tclose(dtype) < 0) return false;
302 
303  return true;
304 }
305 
306 bool
307 CSerializableHdf5File::group_create(const char* name,
308  const char* prefix)
309 {
310  hid_t ngroup;
311  string_t gname;
312 
313  snprintf(gname, STRING_LEN, "%s%s", prefix, name);
314 
315  m_stack_h5stream.push_back(
316  ngroup = H5Gcreate2(m_stack_h5stream.back(), gname, H5P_DEFAULT,
317  H5P_DEFAULT, H5P_DEFAULT));
318  if (ngroup < 0) return false;
319 
320  return true;
321 }
322 
323 bool
324 CSerializableHdf5File::group_open(const char* name,
325  const char* prefix)
326 {
327  hid_t group;
328  string_t gname;
329 
330  snprintf(gname, STRING_LEN, "%s%s", prefix, name);
331 
332  m_stack_h5stream.push_back(
333  group = H5Gopen2(m_stack_h5stream.back(), gname, H5P_DEFAULT));
334  if (group < 0) return false;
335 
336  return true;
337 }
338 
339 bool
340 CSerializableHdf5File::group_close()
341 {
342  if (H5Gclose(m_stack_h5stream.back()) < 0) return false;
343  m_stack_h5stream.pop_back();
344 
345  return true;
346 }
347 
348 CSerializableHdf5File::CSerializableHdf5File()
349  :CSerializableFile() { init(""); }
350 
351 CSerializableHdf5File::CSerializableHdf5File(const char* fname, char rw)
353 {
354  CSerializableFile::init(NULL, rw, fname);
355  init(fname);
356 }
357 
358 CSerializableHdf5File::~CSerializableHdf5File()
359 {
360  while (m_stack_type.get_num_elements() > 0) {
361  delete m_stack_type.back(); m_stack_type.pop_back();
362  }
363 
364  close();
365 }
366 
368 CSerializableHdf5File::new_reader(char* dest_version, size_t n)
369 {
370  if (!attr_read_string(STR_KEY_FILETYPE, dest_version, n))
371  return NULL;
372 
373  if (strcmp(STR_FILETYPE_00, dest_version) == 0)
374  return new SerializableHdf5Reader00(this);
375 
376  return NULL;
377 }
378 
379 void
380 CSerializableHdf5File::init(const char* fname)
381 {
382  if (m_filename == NULL || *m_filename == '\0') {
383  SG_WARNING("Filename not given for opening file!\n")
384  close(); return;
385  }
386 
387  hid_t h5stream = NOT_OPEN;
388  switch (m_task) {
389  case 'w':
390  h5stream = H5Fcreate(fname, H5F_ACC_TRUNC, H5P_DEFAULT,
391  H5P_DEFAULT);
392  break;
393  case 'r':
394  h5stream = H5Fopen(fname, H5F_ACC_RDONLY, H5P_DEFAULT);
395  break;
396  default:
397  SG_WARNING("Could not open file `%s', unknown mode!\n",
398  m_filename);
399  close(); return;
400  }
401 
402  if (h5stream < 0) {
403  SG_WARNING("Could not open file `%s'!\n", m_filename)
404  close(); return;
405  }
406 
407  m_stack_h5stream.push_back(h5stream);
408  switch (m_task) {
409  case 'w':
410  if (!attr_write_string(STR_KEY_FILETYPE, STR_FILETYPE_00)) {
411  SG_WARNING("%s: Could not open file for writing during "
412  "writing filetype!\n", fname);
413  close(); return;
414  }
415  break;
416  case 'r': break;
417  default: break;
418  }
419 }
420 
421 void
422 CSerializableHdf5File::close()
423 {
424  while (m_stack_h5stream.get_num_elements() > 1) {
425  if (m_stack_h5stream.back() >= 0)
426  H5Gclose(m_stack_h5stream.back());
427  m_stack_h5stream.pop_back();
428  }
429 
430  if (m_stack_h5stream.get_num_elements() == 1) {
431  if (m_stack_h5stream.back() >= 0)
432  H5Fclose(m_stack_h5stream.back());
433  m_stack_h5stream.pop_back();
434  }
435 }
436 
437 bool
438 CSerializableHdf5File::is_opened()
439 {
440  return m_stack_h5stream.get_num_elements() > 0;
441 }
442 
443 bool
444 CSerializableHdf5File::write_scalar_wrapped(
445  const TSGDataType* type, const void* param)
446 {
447  type_item_t* m = m_stack_type.back();
448 
449  switch (type->m_stype) {
450  case ST_NONE:
451  if (m->y != 0 || m->x != 0) return true;
452  break;
453  case ST_STRING:
454  if (m->sub_y == 0)
455  m->vltype[m->x*m->dims[1] + m->y].p = (void*) param;
456 
457  if ((m->sub_y
458  < (index_t) m->vltype[m->x*m->dims[1] + m->y].len-1)
459  || ((type->m_ctype == CT_VECTOR || type->m_ctype == CT_SGVECTOR) && m->y
460  < (index_t) m->dims[0]-1)
461  || ((type->m_ctype == CT_MATRIX || type->m_ctype==CT_SGMATRIX)
462  && (m->x < (index_t) m->dims[0]-1
463  || m->y < (index_t) m->dims[1]-1)))
464  return true;
465  break;
466  case ST_SPARSE:
467  if (m->sub_y != 0) return true;
468  break;
469  default: return false;
470  }
471 
472  hid_t mem_type_id;
473  if ((mem_type_id = new_stype2hdf5(type->m_stype, type->m_ptype)
474  ) < 0) return false;
475 
476  switch (type->m_stype) {
477  case ST_NONE:
478  if (H5Dwrite(m->dset, mem_type_id, H5S_ALL, H5S_ALL,
479  H5P_DEFAULT, param) < 0) return false;
480  break;
481  case ST_STRING:
482  if (H5Dwrite(m->dset, mem_type_id, H5S_ALL, H5S_ALL,
483  H5P_DEFAULT, m->vltype) < 0) return false;
484  break;
485  case ST_SPARSE:
486  if (H5Dwrite(m->dset, m->dtype, H5S_ALL, H5S_ALL,
487  H5P_DEFAULT, m->sparse_ptr) < 0) return false;
488  break;
489  default: return false;
490  }
491 
492  if (H5Tclose(mem_type_id) < 0) return false;
493 
494  return true;
495 }
496 
497 bool
498 CSerializableHdf5File::write_cont_begin_wrapped(
499  const TSGDataType* type, index_t len_real_y, index_t len_real_x)
500 {
501  hbool_t bool_buf = true;
502 
503  if (type->m_ptype != PT_SGOBJECT) return true;
504 
505  if (!attr_write_scalar(H5T_NATIVE_HBOOL, STR_IS_CONT, &bool_buf))
506  return false;
507 
508  string_t ctype_buf;
509  type->to_string(ctype_buf, STRING_LEN);
510  if (!attr_write_string(STR_CTYPE_NAME, ctype_buf)) return false;
511 
512  switch (type->m_ctype) {
513  case CT_NDARRAY:
515  case CT_SCALAR:
516  SG_ERROR("write_cont_begin_wrapped(): Implementation error "
517  "during writing Hdf5File!");
518  return false;
519  case CT_MATRIX: case CT_SGMATRIX:
520  if (!attr_write_scalar(TYPE_INDEX, STR_LENGTH_X, &len_real_x))
521  return false;
522  /* break; */
523  case CT_VECTOR: case CT_SGVECTOR:
524  if (!attr_write_scalar(TYPE_INDEX, STR_LENGTH_Y, &len_real_y))
525  return false;
526  break;
527  default: return false;
528  }
529 
530  return true;
531 }
532 
533 bool
534 CSerializableHdf5File::write_cont_end_wrapped(
535  const TSGDataType* type, index_t len_real_y, index_t len_real_x)
536 {
537  return true;
538 }
539 
540 bool
541 CSerializableHdf5File::write_string_begin_wrapped(
542  const TSGDataType* type, index_t length)
543 {
544  type_item_t* m = m_stack_type.back();
545 
546  m->vltype[m->x*m->dims[1] + m->y].len = length;
547 
548  return true;
549 }
550 
551 bool
552 CSerializableHdf5File::write_string_end_wrapped(
553  const TSGDataType* type, index_t length)
554 {
555  return true;
556 }
557 
558 bool
559 CSerializableHdf5File::write_stringentry_begin_wrapped(
560  const TSGDataType* type, index_t y)
561 {
562  type_item_t* m = m_stack_type.back();
563 
564  m->sub_y = y;
565 
566  return true;
567 }
568 
569 bool
570 CSerializableHdf5File::write_stringentry_end_wrapped(
571  const TSGDataType* type, index_t y)
572 {
573  return true;
574 }
575 
576 bool
577 CSerializableHdf5File::write_sparse_begin_wrapped(
578  const TSGDataType* type, index_t length)
579 {
580  type_item_t* m_prev = m_stack_type.back();
581 
582  if(!dspace_select(type->m_ctype, m_prev->y, m_prev->x))
583  return false;
584 
585  type_item_t* m = new type_item_t(m_stack_type.back()->name);
586  m_stack_type.push_back(m);
587 
588  /* ************************************************************ */
589 
590  if (m_prev->y == 0 && m_prev->x == 0) {
591  hbool_t bool_buf = true;
592  if (!group_create(m->name, STR_GROUP_PREFIX)) return false;
593 
594  if (!attr_write_scalar(H5T_NATIVE_HBOOL, STR_IS_SPARSE,
595  &bool_buf)) return false;
596  } else {
597  if (!group_open(m->name, STR_GROUP_PREFIX)) return false;
598  if (!attr_exists(STR_IS_SPARSE)) return false;
599  }
600 
601  m->rank = 1; m->dims[0] = length;
602  if (m->dims[0] == 0) m->dspace = H5Screate(H5S_NULL);
603 
604  if (m->dspace < 0 && (m->dspace = H5Screate_simple(
605  m->rank, m->dims, NULL)) < 0)
606  return false;
607  if ((m->dtype = new_sparseentrytype(type->m_ptype)) < 0)
608  return false;
609 
610  string_t name;
611  index2string(name, STRING_LEN, type->m_ctype, m_prev->y,
612  m_prev->x);
613  if ((m->dset = H5Dcreate2(
614  m_stack_h5stream.back(), name, m->dtype, m->dspace,
615  H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
616  return false;
617 
618  /* ************************************************************ */
619 
620  char* buf = SG_MALLOC(char, sizeof_sparsetype());
621 
622  hid_t mem_type_id;
623  if ((mem_type_id = new_sparsetype()) < 0) return false;
624 
625  hid_t mem_space_id;
626  if ((mem_space_id = H5Screate_simple(0, NULL, NULL)) < 0)
627  return false;
628 
629  hobj_ref_t* sparse_ref = get_ref_sparstype(buf);
630  if (H5Rcreate(sparse_ref, m_stack_h5stream.back(), name,
631  H5R_OBJECT, -1) < 0) return false;
632 
633  if (H5Dwrite(m_prev->dset, mem_type_id, mem_space_id,
634  m_prev->dspace, H5P_DEFAULT, buf) < 0) return false;
635 
636  if (H5Sclose(mem_space_id) < 0) return false;
637  if (H5Tclose(mem_type_id) < 0) return false;
638 
639  delete buf;
640 
641  return true;
642 }
643 
644 bool
645 CSerializableHdf5File::write_sparse_end_wrapped(
646  const TSGDataType* type, index_t length)
647 {
648  if (!group_close()) return false;
649  delete m_stack_type.back(); m_stack_type.pop_back();
650 
651  return true;
652 }
653 
654 bool
655 CSerializableHdf5File::write_sparseentry_begin_wrapped(
656  const TSGDataType* type, const SGSparseVectorEntry<char>* first_entry,
657  index_t feat_index, index_t y)
658 {
659  type_item_t* m = m_stack_type.back();
660 
661  m->sparse_ptr = (SGSparseVectorEntry<char>*) first_entry;
662  m->sub_y = y;
663 
664  return true;
665 }
666 
667 bool
668 CSerializableHdf5File::write_sparseentry_end_wrapped(
669  const TSGDataType* type, const SGSparseVectorEntry<char>* first_entry,
670  index_t feat_index, index_t y)
671 {
672  return true;
673 }
674 
675 bool
676 CSerializableHdf5File::write_item_begin_wrapped(
677  const TSGDataType* type, index_t y, index_t x)
678 {
679  type_item_t* m = m_stack_type.back();
680  m->y = y; m->x = x;
681 
682  if (type->m_ptype != PT_SGOBJECT) return true;
683 
684  string_t name;
685  if (!index2string(name, STRING_LEN, type->m_ctype, y, x))
686  return false;
687  if (!group_create(name, "")) return false;
688 
689  return true;
690 }
691 
692 bool
693 CSerializableHdf5File::write_item_end_wrapped(
694  const TSGDataType* type, index_t y, index_t x)
695 {
696  if (type->m_ptype == PT_SGOBJECT)
697  if (!group_close()) return false;
698 
699  return true;
700 }
701 
702 bool
703 CSerializableHdf5File::write_sgserializable_begin_wrapped(
704  const TSGDataType* type, const char* sgserializable_name,
705  EPrimitiveType generic)
706 {
707  hbool_t bool_buf = true;
708 
709  if (!attr_write_scalar(H5T_NATIVE_HBOOL, STR_IS_SGSERIALIZABLE,
710  &bool_buf)) return false;
711 
712  if (*sgserializable_name == '\0') {
713  if (!attr_write_scalar(H5T_NATIVE_HBOOL, STR_IS_NULL,
714  &bool_buf))
715  return false;
716  return true;
717  }
718 
719  if (!attr_write_string(STR_INSTANCE_NAME, sgserializable_name))
720  return false;
721 
722  if (generic != PT_NOT_GENERIC) {
723  string_t buf;
725  if (!attr_write_string(STR_GENERIC_NAME, buf)) return false;
726  }
727 
728  return true;
729 }
730 
731 bool
732 CSerializableHdf5File::write_sgserializable_end_wrapped(
733  const TSGDataType* type, const char* sgserializable_name,
734  EPrimitiveType generic)
735 {
736  return true;
737 }
738 
739 bool
740 CSerializableHdf5File::write_type_begin_wrapped(
741  const TSGDataType* type, const char* name, const char* prefix)
742 {
743  type_item_t* m = new type_item_t(name); m_stack_type.push_back(m);
744 
745  if (type->m_ptype == PT_SGOBJECT) {
746  if (!group_create(name, "")) return false;
747  return true;
748  }
749 
750  switch (type->m_ctype) {
751  case CT_NDARRAY:
753  case CT_SCALAR:
754  m->rank = 0;
755  if (type->m_stype == ST_STRING) m->vltype = SG_MALLOC(hvl_t, 1);
756  break;
757  case CT_VECTOR: case CT_SGVECTOR:
758  m->rank = 1; m->dims[0] = *type->m_length_y;
759  if (m->dims[0] == 0) m->dspace = H5Screate(H5S_NULL);
760  if (type->m_stype == ST_STRING)
761  m->vltype = SG_MALLOC(hvl_t, m->dims[0]);
762  break;
763  case CT_MATRIX: case CT_SGMATRIX:
764  m->rank = 2;
765  m->dims[0] = *type->m_length_x; m->dims[1] = *type->m_length_y;
766  if (m->dims[0] *m->dims[1] == 0)
767  m->dspace = H5Screate(H5S_NULL);
768  if (type->m_stype == ST_STRING)
769  m->vltype = SG_MALLOC(hvl_t, m->dims[0] *m->dims[1]);
770  break;
771  default: return false;
772  }
773 
774  if (m->dspace < 0 && (m->dspace = H5Screate_simple(
775  m->rank, m->dims, NULL)) < 0)
776  return false;
777  if ((m->dtype = new_stype2hdf5(type->m_stype, type->m_ptype)) < 0)
778  return false;
779 
780  if ((m->dset = H5Dcreate2(
781  m_stack_h5stream.back(), name, m->dtype, m->dspace,
782  H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
783  return false;
784 
785  return true;
786 }
787 
788 bool
789 CSerializableHdf5File::write_type_end_wrapped(
790  const TSGDataType* type, const char* name, const char* prefix)
791 {
792  if (type->m_ptype == PT_SGOBJECT)
793  if (!group_close()) return false;
794 
795  delete m_stack_type.back(); m_stack_type.pop_back();
796  return true;
797 }
798 
799 #endif /* HAVE_HDF5 */

SHOGUN Machine Learning Toolbox - Documentation