VectorSerializer.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_SERIALIZATION_VECTORSERIALIZER_H_
36 #define _BLAZE_MATH_SERIALIZATION_VECTORSERIALIZER_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <blaze/math/Aliases.h>
45 #include <blaze/math/Exception.h>
52 #include <blaze/util/Assert.h>
53 #include <blaze/util/DisableIf.h>
54 #include <blaze/util/EnableIf.h>
55 #include <blaze/util/Types.h>
56 
57 
58 namespace blaze {
59 
60 //=================================================================================================
61 //
62 // CLASS DEFINITION
63 //
64 //=================================================================================================
65 
66 //*************************************************************************************************
154 {
155  private:
156  //**Private class VectorValueMappingHelper******************************************************
170  template< bool IsDenseVector >
171  struct VectorValueMappingHelper;
173  //**********************************************************************************************
174 
175  //**Private class VectorValueMapping************************************************************
183  template< typename T >
184  struct VectorValueMapping
185  {
186  enum { value = VectorValueMappingHelper< IsDenseVector_v<T> >::value };
188  };
190  //**********************************************************************************************
191 
192  public:
193  //**Constructor*********************************************************************************
196  explicit inline VectorSerializer();
198  //**********************************************************************************************
199 
200  //**Serialization functions*********************************************************************
203  template< typename Archive, typename VT, bool TF >
204  void serialize( Archive& archive, const Vector<VT,TF>& vec );
206  //**********************************************************************************************
207 
208  //**Deserialization functions*********************************************************************
211  template< typename Archive, typename VT, bool TF >
212  void deserialize( Archive& archive, Vector<VT,TF>& vec );
214  //**********************************************************************************************
215 
216  private:
217  //**Serialization functions*********************************************************************
220  template< typename Archive, typename VT >
221  void serializeHeader( Archive& archive, const VT& vec );
222 
223  template< typename Archive, typename VT, bool TF >
224  void serializeVector( Archive& archive, const DenseVector<VT,TF>& vec );
225 
226  template< typename Archive, typename VT, bool TF >
227  void serializeVector( Archive& archive, const SparseVector<VT,TF>& vec );
229  //**********************************************************************************************
230 
231  //**Deserialization functions*******************************************************************
234  template< typename Archive, typename VT >
235  void deserializeHeader( Archive& archive, const VT& vec );
236 
237  template< typename VT, bool TF >
239 
240  template< typename VT, bool TF >
242 
243  template< typename VT >
245 
246  template< typename Archive, typename VT >
247  void deserializeVector( Archive& archive, VT& vec );
248 
249  template< typename Archive, typename VT, bool TF >
252 
253  template< typename Archive, typename VT, bool TF >
256 
257  template< typename Archive, typename VT, bool TF >
258  void deserializeDenseVector( Archive& archive, SparseVector<VT,TF>& vec );
259 
260  template< typename Archive, typename VT, bool TF >
261  void deserializeSparseVector( Archive& archive, DenseVector<VT,TF>& vec );
262 
263  template< typename Archive, typename VT, bool TF >
264  void deserializeSparseVector( Archive& archive, SparseVector<VT,TF>& vec );
266  //**********************************************************************************************
267 
268  //**Member variables****************************************************************************
277 
278  //**********************************************************************************************
279 };
280 //*************************************************************************************************
281 
282 
283 
284 
285 //=================================================================================================
286 //
287 // CONSTRUCTOR
288 //
289 //=================================================================================================
290 
291 //*************************************************************************************************
295  : version_ ( 0U ) // The version of the archive
296  , type_ ( 0U ) // The type of the vector
297  , elementType_( 0U ) // The type of an element
298  , elementSize_( 0U ) // The size in bytes of a single element of the vector
299  , size_ ( 0UL ) // The size of the vector
300  , number_ ( 0UL ) // The total number of elements contained in the vector
301 {}
302 //*************************************************************************************************
303 
304 
305 
306 
307 //=================================================================================================
308 //
309 // SERIALIZATION FUNCTIONS
310 //
311 //=================================================================================================
312 
313 //*************************************************************************************************
324 template< typename Archive // Type of the archive
325  , typename VT // Type of the vector
326  , bool TF > // Transpose flag
328 {
329  if( !archive ) {
330  BLAZE_THROW_RUNTIME_ERROR( "Faulty archive detected" );
331  }
332 
333  serializeHeader( archive, ~vec );
334  serializeVector( archive, ~vec );
335 }
336 //*************************************************************************************************
337 
338 
339 //*************************************************************************************************
347 template< typename Archive // Type of the archive
348  , typename VT > // Type of the vector
349 void VectorSerializer::serializeHeader( Archive& archive, const VT& vec )
350 {
351  using ET = ElementType_t<VT>;
352 
353  archive << uint8_t ( 1U );
354  archive << uint8_t ( VectorValueMapping<VT>::value );
355  archive << uint8_t ( TypeValueMapping<ET>::value );
356  archive << uint8_t ( sizeof( ET ) );
357  archive << uint64_t( vec.size() );
358  archive << uint64_t( IsDenseVector_v<VT> ? vec.size() : vec.nonZeros() );
359 
360  if( !archive ) {
361  BLAZE_THROW_RUNTIME_ERROR( "File header could not be serialized" );
362  }
363 }
364 //*************************************************************************************************
365 
366 
367 //*************************************************************************************************
375 template< typename Archive // Type of the archive
376  , typename VT // Type of the vector
377  , bool TF > // Transpose flag
379 {
380  size_t i( 0UL );
381  while( ( i < (~vec).size() ) && ( archive << (~vec)[i] ) ) {
382  ++i;
383  }
384 
385  if( !archive ) {
386  BLAZE_THROW_RUNTIME_ERROR( "Dense vector could not be serialized" );
387  }
388 }
389 //*************************************************************************************************
390 
391 
392 //*************************************************************************************************
400 template< typename Archive // Type of the archive
401  , typename VT // Type of the vector
402  , bool TF > // Transpose flag
404 {
406 
407  ConstIterator element( (~vec).begin() );
408  while( ( element != (~vec).end() ) &&
409  ( archive << element->index() << element->value() ) ) {
410  ++element;
411  }
412 
413  if( !archive ) {
414  BLAZE_THROW_RUNTIME_ERROR( "Sparse vector could not be serialized" );
415  }
416 }
417 //*************************************************************************************************
418 
419 
420 
421 
422 //=================================================================================================
423 //
424 // DESERIALIZATION FUNCTIONS
425 //
426 //=================================================================================================
427 
428 //*************************************************************************************************
436 template< typename Archive // Type of the archive
437  , typename VT // Type of the vector
438  , bool TF > // Transpose flag
440 {
441  if( !archive ) {
442  BLAZE_THROW_INVALID_ARGUMENT( "Faulty archive detected" );
443  }
444 
445  deserializeHeader( archive, ~vec );
446  prepareVector( ~vec );
447  deserializeVector( archive, ~vec );
448 }
449 //*************************************************************************************************
450 
451 
452 //*************************************************************************************************
465 template< typename Archive // Type of the archive
466  , typename VT > // Type of the vector
467 void VectorSerializer::deserializeHeader( Archive& archive, const VT& vec )
468 {
469  using ET = ElementType_t<VT>;
470 
471  if( !( archive >> version_ >> type_ >> elementType_ >> elementSize_ >> size_ >> number_ ) ) {
472  BLAZE_THROW_RUNTIME_ERROR( "Corrupt archive detected" );
473  }
474  else if( version_ != 1UL ) {
475  BLAZE_THROW_RUNTIME_ERROR( "Invalid version detected" );
476  }
477  else if( ( type_ & 1U ) != 0U || ( type_ & (~3U) ) != 0U ) {
478  BLAZE_THROW_RUNTIME_ERROR( "Invalid vector type detected" );
479  }
481  BLAZE_THROW_RUNTIME_ERROR( "Invalid element type detected" );
482  }
483  else if( elementSize_ != sizeof( ET ) ) {
484  BLAZE_THROW_RUNTIME_ERROR( "Invalid element size detected" );
485  }
486  else if( !IsResizable_v<VT> && size_ != vec.size() ) {
487  BLAZE_THROW_RUNTIME_ERROR( "Invalid vector size detected" );
488  }
489  else if( number_ > size_ ) {
490  BLAZE_THROW_RUNTIME_ERROR( "Invalid number of elements detected" );
491  }
492 }
493 //*************************************************************************************************
494 
495 
496 //*************************************************************************************************
502 template< typename VT // Type of the dense vector
503  , bool TF > // Transpose flag
505 {
506  reset( ~vec );
507 }
508 //*************************************************************************************************
509 
510 
511 //*************************************************************************************************
517 template< typename VT // Type of the sparse vector
518  , bool TF > // Transpose flag
520 {
521  (~vec).reserve( number_ );
522  reset( ~vec );
523 }
524 //*************************************************************************************************
525 
526 
527 //*************************************************************************************************
533 template< typename VT > // Type of the vector
535 {
536  vec.resize ( size_, false );
537  vec.reserve( number_ );
538  reset( vec );
539 }
540 //*************************************************************************************************
541 
542 
543 //*************************************************************************************************
554 template< typename Archive // Type of the archive
555  , typename VT > // Type of the vector
557 {
558  if( type_ == 0U ) {
559  deserializeDenseVector( archive, vec );
560  }
561  else if( type_ == 2U ) {
562  deserializeSparseVector( archive, vec );
563  }
564  else {
565  BLAZE_INTERNAL_ASSERT( false, "Undefined type flag" );
566  }
567 }
568 //*************************************************************************************************
569 
570 
571 //*************************************************************************************************
583 template< typename Archive // Type of the archive
584  , typename VT // Type of the vector
585  , bool TF > // Transpose flag
588 {
589  using ET = ElementType_t<VT>;
590 
591  size_t i( 0UL );
592  ET value{};
593 
594  while( ( i != size_ ) && ( archive >> value ) ) {
595  (~vec)[i] = value;
596  ++i;
597  }
598 
599  if( !archive ) {
600  BLAZE_THROW_RUNTIME_ERROR( "Dense vector could not be deserialized" );
601  }
602 }
603 //*************************************************************************************************
604 
605 
606 //*************************************************************************************************
618 template< typename Archive // Type of the archive
619  , typename VT // Type of the vector
620  , bool TF > // Transpose flag
623 {
624  if( size_ == 0UL ) return;
625  archive.read( &(~vec)[0], size_ );
626 
627  if( !archive ) {
628  BLAZE_THROW_RUNTIME_ERROR( "Dense vector could not be deserialized" );
629  }
630 }
631 //*************************************************************************************************
632 
633 
634 //*************************************************************************************************
646 template< typename Archive // Type of the archive
647  , typename VT // Type of the vector
648  , bool TF > // Transpose flag
650 {
651  using ET = ElementType_t<VT>;
652 
653  size_t i( 0UL );
654  ET value{};
655 
656  while( ( i != size_ ) && ( archive >> value ) ) {
657  (~vec)[i] = value;
658  ++i;
659  }
660 
661  if( !archive ) {
662  BLAZE_THROW_RUNTIME_ERROR( "Sparse vector could not be deserialized" );
663  }
664 }
665 //*************************************************************************************************
666 
667 
668 //*************************************************************************************************
680 template< typename Archive // Type of the archive
681  , typename VT // Type of the vector
682  , bool TF > // Transpose flag
684 {
685  using ET = ElementType_t<VT>;
686 
687  size_t i ( 0UL );
688  size_t index( 0UL );
689  ET value{};
690 
691  while( ( i != number_ ) && ( archive >> index >> value ) ) {
692  (~vec)[index] = value;
693  ++i;
694  }
695 
696  if( !archive ) {
697  BLAZE_THROW_RUNTIME_ERROR( "Dense vector could not be deserialized" );
698  }
699 }
700 //*************************************************************************************************
701 
702 
703 //*************************************************************************************************
715 template< typename Archive // Type of the archive
716  , typename VT // Type of the vector
717  , bool TF > // Transpose flag
719 {
720  using ET = ElementType_t<VT>;
721 
722  size_t i ( 0UL );
723  size_t index( 0UL );
724  ET value{};
725 
726  while( ( i != number_ ) && ( archive >> index >> value ) ) {
727  (~vec).append( index, value, false );
728  ++i;
729  }
730 
731  if( !archive ) {
732  BLAZE_THROW_RUNTIME_ERROR( "Sparse vector could not be deserialized" );
733  }
734 }
735 //*************************************************************************************************
736 
737 
738 
739 
740 //=================================================================================================
741 //
742 // VECTORVALUEMAPPINGHELPER SPECIALIZATIONS
743 //
744 //=================================================================================================
745 
746 //*************************************************************************************************
750 template<>
751 struct VectorSerializer::VectorValueMappingHelper<true>
752 {
753  enum { value = 0 };
754 };
756 //*************************************************************************************************
757 
758 
759 //*************************************************************************************************
763 template<>
764 struct VectorSerializer::VectorValueMappingHelper<false>
765 {
766  enum { value = 2 };
767 };
769 //*************************************************************************************************
770 
771 
772 
773 
774 //=================================================================================================
775 //
776 // GLOBAL FUNCTIONS
777 //
778 //=================================================================================================
779 
780 //*************************************************************************************************
844 template< typename Archive // Type of the archive
845  , typename VT // Type of the vector
846  , bool TF > // Transpose flag
847 void serialize( Archive& archive, const Vector<VT,TF>& vec )
848 {
849  VectorSerializer().serialize( archive, ~vec );
850 }
851 //*************************************************************************************************
852 
853 
854 //*************************************************************************************************
866 template< typename Archive // Type of the archive
867  , typename VT // Type of the vector
868  , bool TF > // Transpose flag
869 void deserialize( Archive& archive, Vector<VT,TF>& vec )
870 {
871  VectorSerializer().deserialize( archive, ~vec );
872 }
873 //*************************************************************************************************
874 
875 } // namespace blaze
876 
877 #endif
#define BLAZE_THROW_INVALID_ARGUMENT(MESSAGE)
Macro for the emission of a std::invalid_argument exception.This macro encapsulates the default way o...
Definition: Exception.h:235
void serialize(Archive &archive, const Vector< VT, TF > &vec)
Serializes the given vector and writes it to the archive.
Definition: VectorSerializer.h:327
Header file for auxiliary alias declarations.
uint64_t size_
The size of the vector.
Definition: VectorSerializer.h:275
Binary archive for the portable serialization of data.The Archive class implements the functionality ...
Definition: Archive.h:140
Header file for basic type definitions.
Header file for the SparseVector base class.
Serializer for dense and sparse vectors.The VectorSerializer implements the necessary logic to serial...
Definition: VectorSerializer.h:153
MT::Iterator begin(Matrix< MT, SO > &matrix, size_t i)
Returns an iterator to the first element of row/column i.
Definition: Matrix.h:372
void reset(const DiagonalProxy< MT > &proxy)
Resetting the represented element to the default initial values.
Definition: DiagonalProxy.h:591
8-bit unsigned integer type of the Blaze library.
Header file for the DenseVector base class.
void deserializeVector(Archive &archive, VT &vec)
Deserializes a vector from the archive.
Definition: VectorSerializer.h:556
void deserializeHeader(Archive &archive, const VT &vec)
Deserializes all meta information about the given vector.
Definition: VectorSerializer.h:467
void serializeVector(Archive &archive, const DenseVector< VT, TF > &vec)
Serializes the elements of a dense vector.
Definition: VectorSerializer.h:378
uint8_t version_
The version of the archive.
Definition: VectorSerializer.h:271
typename T::ElementType ElementType_t
Alias declaration for nested ElementType type definitions.The ElementType_t alias declaration provide...
Definition: Aliases.h:170
#define BLAZE_THROW_RUNTIME_ERROR(MESSAGE)
Macro for the emission of a std::runtime_error exception.This macro encapsulates the default way of B...
Definition: Exception.h:379
typename EnableIf< Condition, T >::Type EnableIf_t
Auxiliary type for the EnableIf class template.The EnableIf_t alias declaration provides a convenient...
Definition: EnableIf.h:138
Header file for the DisableIf class template.
DisableIf_t< IsResizable_v< VT > > prepareVector(DenseVector< VT, TF > &vec)
Prepares the given non-resizable dense vector for the deserialization process.
Definition: VectorSerializer.h:504
void serializeHeader(Archive &archive, const VT &vec)
Serializes all meta information about the given vector.
Definition: VectorSerializer.h:349
Namespace of the Blaze C++ math library.
Definition: Blaze.h:58
void serialize(Archive &archive, const Matrix< MT, SO > &mat)
Serializes the given matrix and writes it to the archive.
Definition: MatrixSerializer.h:1257
uint8_t elementSize_
The size in bytes of a single element of the vector.
Definition: VectorSerializer.h:274
const Element * ConstIterator
Iterator over constant elements.
Definition: CompressedMatrix.h:3086
Base class for N-dimensional dense vectors.The DenseVector class is a base class for all arbitrarily ...
Definition: DenseVector.h:76
Header file for the exception macros of the math module.
MT::Iterator end(Matrix< MT, SO > &matrix, size_t i)
Returns an iterator just past the last element of row/column i.
Definition: Matrix.h:438
Header file for the EnableIf class template.
64-bit unsigned integer type of the Blaze library.
void deserialize(Archive &archive, Vector< VT, TF > &vec)
Deserializes a vector from the given archive.
Definition: VectorSerializer.h:439
#define BLAZE_CONSTRAINT_MUST_BE_VECTOR_TYPE(T)
Constraint on the data type.In case the given data type T is not a N-dimensional vector type...
Definition: Vector.h:61
Header file for run time assertion macros.
VectorSerializer()
The default constructor of the VectorSerializer class.
Definition: VectorSerializer.h:294
Conversion from a data type to a serial representation.This class template converts the given data ty...
Definition: TypeValueMapping.h:163
uint64_t number_
The total number of elements contained in the vector.
Definition: VectorSerializer.h:276
constexpr size_t size(const Matrix< MT, SO > &matrix) noexcept
Returns the total number of elements of the matrix.
Definition: Matrix.h:530
DisableIf_t< VT::simdEnabled > deserializeDenseVector(Archive &archive, DenseVector< VT, TF > &vec)
Deserializes a dense vector from the archive.
Definition: VectorSerializer.h:587
uint8_t type_
The type of the vector.
Definition: VectorSerializer.h:272
Header file for the IsDenseVector type trait.
void deserialize(Archive &archive, Matrix< MT, SO > &mat)
Deserializes a matrix from the given archive.
Definition: MatrixSerializer.h:1279
void deserializeSparseVector(Archive &archive, DenseVector< VT, TF > &vec)
Deserializes a sparse vector from the archive.
Definition: VectorSerializer.h:683
Header file for the TypeValueMapping class template.
typename T::ConstIterator ConstIterator_t
Alias declaration for nested ConstIterator type definitions.The ConstIterator_t alias declaration pro...
Definition: Aliases.h:110
Base class for N-dimensional vectors.The Vector class is a base class for all arbitrarily sized (N-di...
Definition: Forward.h:186
Base class for sparse vectors.The SparseVector class is a base class for all arbitrarily sized (N-dim...
Definition: Forward.h:138
Constraint on the data type.
typename DisableIf< Condition, T >::Type DisableIf_t
Auxiliary type for the DisableIf class template.The DisableIf_t alias declaration provides a convenie...
Definition: DisableIf.h:138
Header file for the IsResizable type trait.
Header file for the Vector CRTP base class.
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.In case of an invalid run time expression, the program execution is terminated. The BLAZE_INTERNAL_ASSERT macro can be disabled by setting the BLAZE_USER_ASSERTION flag to zero or by defining NDEBUG during the compilation.
Definition: Assert.h:101
uint8_t elementType_
The type of an element.
Definition: VectorSerializer.h:273