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<T>::value >::value };
188  };
190  //**********************************************************************************************
191 
192  public:
193  //**Constructor*********************************************************************************
196  explicit inline VectorSerializer();
197  // No explicitly declared copy constructor.
199  //**********************************************************************************************
200 
201  //**Destructor**********************************************************************************
202  // No explicitly declared destructor.
203  //**********************************************************************************************
204 
205  //**Assignment operators************************************************************************
206  // No explicitly declared copy assignment operator.
207  //**********************************************************************************************
208 
209  //**Serialization functions*********************************************************************
212  template< typename Archive, typename VT, bool TF >
213  void serialize( Archive& archive, const Vector<VT,TF>& vec );
215  //**********************************************************************************************
216 
217  //**Deserialization functions*********************************************************************
220  template< typename Archive, typename VT, bool TF >
221  void deserialize( Archive& archive, Vector<VT,TF>& vec );
223  //**********************************************************************************************
224 
225  private:
226  //**Serialization functions*********************************************************************
229  template< typename Archive, typename VT >
230  void serializeHeader( Archive& archive, const VT& vec );
231 
232  template< typename Archive, typename VT, bool TF >
233  void serializeVector( Archive& archive, const DenseVector<VT,TF>& vec );
234 
235  template< typename Archive, typename VT, bool TF >
236  void serializeVector( Archive& archive, const SparseVector<VT,TF>& vec );
238  //**********************************************************************************************
239 
240  //**Deserialization functions*******************************************************************
243  template< typename Archive, typename VT >
244  void deserializeHeader( Archive& archive, const VT& vec );
245 
246  template< typename VT, bool TF >
248 
249  template< typename VT, bool TF >
251 
252  template< typename VT >
254 
255  template< typename Archive, typename VT >
256  void deserializeVector( Archive& archive, VT& vec );
257 
258  template< typename Archive, typename VT, bool TF >
261 
262  template< typename Archive, typename VT, bool TF >
265 
266  template< typename Archive, typename VT, bool TF >
267  void deserializeDenseVector( Archive& archive, SparseVector<VT,TF>& vec );
268 
269  template< typename Archive, typename VT, bool TF >
270  void deserializeSparseVector( Archive& archive, DenseVector<VT,TF>& vec );
271 
272  template< typename Archive, typename VT, bool TF >
273  void deserializeSparseVector( Archive& archive, SparseVector<VT,TF>& vec );
275  //**********************************************************************************************
276 
277  //**Member variables****************************************************************************
286 
287  //**********************************************************************************************
288 };
289 //*************************************************************************************************
290 
291 
292 
293 
294 //=================================================================================================
295 //
296 // CONSTRUCTOR
297 //
298 //=================================================================================================
299 
300 //*************************************************************************************************
304  : version_ ( 0U ) // The version of the archive
305  , type_ ( 0U ) // The type of the vector
306  , elementType_( 0U ) // The type of an element
307  , elementSize_( 0U ) // The size in bytes of a single element of the vector
308  , size_ ( 0UL ) // The size of the vector
309  , number_ ( 0UL ) // The total number of elements contained in the vector
310 {}
311 //*************************************************************************************************
312 
313 
314 
315 
316 //=================================================================================================
317 //
318 // SERIALIZATION FUNCTIONS
319 //
320 //=================================================================================================
321 
322 //*************************************************************************************************
333 template< typename Archive // Type of the archive
334  , typename VT // Type of the vector
335  , bool TF > // Transpose flag
337 {
338  if( !archive ) {
339  BLAZE_THROW_RUNTIME_ERROR( "Faulty archive detected" );
340  }
341 
342  serializeHeader( archive, ~vec );
343  serializeVector( archive, ~vec );
344 }
345 //*************************************************************************************************
346 
347 
348 //*************************************************************************************************
356 template< typename Archive // Type of the archive
357  , typename VT > // Type of the vector
358 void VectorSerializer::serializeHeader( Archive& archive, const VT& vec )
359 {
360  using ET = ElementType_<VT>;
361 
362  archive << uint8_t ( 1U );
363  archive << uint8_t ( VectorValueMapping<VT>::value );
364  archive << uint8_t ( TypeValueMapping<ET>::value );
365  archive << uint8_t ( sizeof( ET ) );
366  archive << uint64_t( vec.size() );
367  archive << uint64_t( IsDenseVector<VT>::value ? vec.size() : vec.nonZeros() );
368 
369  if( !archive ) {
370  BLAZE_THROW_RUNTIME_ERROR( "File header could not be serialized" );
371  }
372 }
373 //*************************************************************************************************
374 
375 
376 //*************************************************************************************************
384 template< typename Archive // Type of the archive
385  , typename VT // Type of the vector
386  , bool TF > // Transpose flag
388 {
389  size_t i( 0UL );
390  while( ( i < (~vec).size() ) && ( archive << (~vec)[i] ) ) {
391  ++i;
392  }
393 
394  if( !archive ) {
395  BLAZE_THROW_RUNTIME_ERROR( "Dense vector could not be serialized" );
396  }
397 }
398 //*************************************************************************************************
399 
400 
401 //*************************************************************************************************
409 template< typename Archive // Type of the archive
410  , typename VT // Type of the vector
411  , bool TF > // Transpose flag
413 {
415 
416  ConstIterator element( (~vec).begin() );
417  while( ( element != (~vec).end() ) &&
418  ( archive << element->index() << element->value() ) ) {
419  ++element;
420  }
421 
422  if( !archive ) {
423  BLAZE_THROW_RUNTIME_ERROR( "Sparse vector could not be serialized" );
424  }
425 }
426 //*************************************************************************************************
427 
428 
429 
430 
431 //=================================================================================================
432 //
433 // DESERIALIZATION FUNCTIONS
434 //
435 //=================================================================================================
436 
437 //*************************************************************************************************
445 template< typename Archive // Type of the archive
446  , typename VT // Type of the vector
447  , bool TF > // Transpose flag
449 {
450  if( !archive ) {
451  BLAZE_THROW_INVALID_ARGUMENT( "Faulty archive detected" );
452  }
453 
454  deserializeHeader( archive, ~vec );
455  prepareVector( ~vec );
456  deserializeVector( archive, ~vec );
457 }
458 //*************************************************************************************************
459 
460 
461 //*************************************************************************************************
474 template< typename Archive // Type of the archive
475  , typename VT > // Type of the vector
476 void VectorSerializer::deserializeHeader( Archive& archive, const VT& vec )
477 {
478  using ET = ElementType_<VT>;
479 
480  if( !( archive >> version_ >> type_ >> elementType_ >> elementSize_ >> size_ >> number_ ) ) {
481  BLAZE_THROW_RUNTIME_ERROR( "Corrupt archive detected" );
482  }
483  else if( version_ != 1UL ) {
484  BLAZE_THROW_RUNTIME_ERROR( "Invalid version detected" );
485  }
486  else if( ( type_ & 1U ) != 0U || ( type_ & (~3U) ) != 0U ) {
487  BLAZE_THROW_RUNTIME_ERROR( "Invalid vector type detected" );
488  }
490  BLAZE_THROW_RUNTIME_ERROR( "Invalid element type detected" );
491  }
492  else if( elementSize_ != sizeof( ET ) ) {
493  BLAZE_THROW_RUNTIME_ERROR( "Invalid element size detected" );
494  }
495  else if( !IsResizable<VT>::value && size_ != vec.size() ) {
496  BLAZE_THROW_RUNTIME_ERROR( "Invalid vector size detected" );
497  }
498  else if( number_ > size_ ) {
499  BLAZE_THROW_RUNTIME_ERROR( "Invalid number of elements detected" );
500  }
501 }
502 //*************************************************************************************************
503 
504 
505 //*************************************************************************************************
511 template< typename VT // Type of the dense vector
512  , bool TF > // Transpose flag
514 {
515  reset( ~vec );
516 }
517 //*************************************************************************************************
518 
519 
520 //*************************************************************************************************
526 template< typename VT // Type of the sparse vector
527  , bool TF > // Transpose flag
529 {
530  (~vec).reserve( number_ );
531  reset( ~vec );
532 }
533 //*************************************************************************************************
534 
535 
536 //*************************************************************************************************
542 template< typename VT > // Type of the vector
544 {
545  vec.resize ( size_, false );
546  vec.reserve( number_ );
547  reset( vec );
548 }
549 //*************************************************************************************************
550 
551 
552 //*************************************************************************************************
563 template< typename Archive // Type of the archive
564  , typename VT > // Type of the vector
566 {
567  if( type_ == 0U ) {
568  deserializeDenseVector( archive, vec );
569  }
570  else if( type_ == 2U ) {
571  deserializeSparseVector( archive, vec );
572  }
573  else {
574  BLAZE_INTERNAL_ASSERT( false, "Undefined type flag" );
575  }
576 }
577 //*************************************************************************************************
578 
579 
580 //*************************************************************************************************
592 template< typename Archive // Type of the archive
593  , typename VT // Type of the vector
594  , bool TF > // Transpose flag
597 {
598  using ET = ElementType_<VT>;
599 
600  size_t i( 0UL );
601  ET value{};
602 
603  while( ( i != size_ ) && ( archive >> value ) ) {
604  (~vec)[i] = value;
605  ++i;
606  }
607 
608  if( !archive ) {
609  BLAZE_THROW_RUNTIME_ERROR( "Dense vector could not be deserialized" );
610  }
611 }
612 //*************************************************************************************************
613 
614 
615 //*************************************************************************************************
627 template< typename Archive // Type of the archive
628  , typename VT // Type of the vector
629  , bool TF > // Transpose flag
632 {
633  if( size_ == 0UL ) return;
634  archive.read( &(~vec)[0], size_ );
635 
636  if( !archive ) {
637  BLAZE_THROW_RUNTIME_ERROR( "Dense vector could not be deserialized" );
638  }
639 }
640 //*************************************************************************************************
641 
642 
643 //*************************************************************************************************
655 template< typename Archive // Type of the archive
656  , typename VT // Type of the vector
657  , bool TF > // Transpose flag
659 {
660  using ET = ElementType_<VT>;
661 
662  size_t i( 0UL );
663  ET value{};
664 
665  while( ( i != size_ ) && ( archive >> value ) ) {
666  (~vec)[i] = value;
667  ++i;
668  }
669 
670  if( !archive ) {
671  BLAZE_THROW_RUNTIME_ERROR( "Sparse vector could not be deserialized" );
672  }
673 }
674 //*************************************************************************************************
675 
676 
677 //*************************************************************************************************
689 template< typename Archive // Type of the archive
690  , typename VT // Type of the vector
691  , bool TF > // Transpose flag
693 {
694  using ET = ElementType_<VT>;
695 
696  size_t i ( 0UL );
697  size_t index( 0UL );
698  ET value{};
699 
700  while( ( i != number_ ) && ( archive >> index >> value ) ) {
701  (~vec)[index] = value;
702  ++i;
703  }
704 
705  if( !archive ) {
706  BLAZE_THROW_RUNTIME_ERROR( "Dense vector could not be deserialized" );
707  }
708 }
709 //*************************************************************************************************
710 
711 
712 //*************************************************************************************************
724 template< typename Archive // Type of the archive
725  , typename VT // Type of the vector
726  , bool TF > // Transpose flag
728 {
729  using ET = ElementType_<VT>;
730 
731  size_t i ( 0UL );
732  size_t index( 0UL );
733  ET value{};
734 
735  while( ( i != number_ ) && ( archive >> index >> value ) ) {
736  (~vec).append( index, value, false );
737  ++i;
738  }
739 
740  if( !archive ) {
741  BLAZE_THROW_RUNTIME_ERROR( "Sparse vector could not be deserialized" );
742  }
743 }
744 //*************************************************************************************************
745 
746 
747 
748 
749 //=================================================================================================
750 //
751 // VECTORVALUEMAPPINGHELPER SPECIALIZATIONS
752 //
753 //=================================================================================================
754 
755 //*************************************************************************************************
759 template<>
760 struct VectorSerializer::VectorValueMappingHelper<true>
761 {
762  enum { value = 0 };
763 };
765 //*************************************************************************************************
766 
767 
768 //*************************************************************************************************
772 template<>
773 struct VectorSerializer::VectorValueMappingHelper<false>
774 {
775  enum { value = 2 };
776 };
778 //*************************************************************************************************
779 
780 
781 
782 
783 //=================================================================================================
784 //
785 // GLOBAL FUNCTIONS
786 //
787 //=================================================================================================
788 
789 //*************************************************************************************************
853 template< typename Archive // Type of the archive
854  , typename VT // Type of the vector
855  , bool TF > // Transpose flag
856 void serialize( Archive& archive, const Vector<VT,TF>& vec )
857 {
858  VectorSerializer().serialize( archive, ~vec );
859 }
860 //*************************************************************************************************
861 
862 
863 //*************************************************************************************************
875 template< typename Archive // Type of the archive
876  , typename VT // Type of the vector
877  , bool TF > // Transpose flag
878 void deserialize( Archive& archive, Vector<VT,TF>& vec )
879 {
880  VectorSerializer().deserialize( archive, ~vec );
881 }
882 //*************************************************************************************************
883 
884 } // namespace blaze
885 
886 #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:336
Header file for auxiliary alias declarations.
uint64_t size_
The size of the vector.
Definition: VectorSerializer.h:284
Binary archive for the portable serialization of data.The Archive class implements the functionality ...
Definition: Archive.h:141
Header file for basic type definitions.
Header file for the SparseVector base class.
T Type
The instantiated type.
Definition: DisableIf.h:99
BLAZE_ALWAYS_INLINE size_t size(const Vector< VT, TF > &vector) noexcept
Returns the current size/dimension of the vector.
Definition: Vector.h:265
Serializer for dense and sparse vectors.The VectorSerializer implements the necessary logic to serial...
Definition: VectorSerializer.h:153
BLAZE_ALWAYS_INLINE MT::Iterator begin(Matrix< MT, SO > &matrix, size_t i)
Returns an iterator to the first element of row/column i.
Definition: Matrix.h:364
void reset(const DiagonalProxy< MT > &proxy)
Resetting the represented element to the default initial values.
Definition: DiagonalProxy.h:588
typename DisableIf< Condition, T >::Type DisableIf_
Auxiliary type for the DisableIf class template.The DisableIf_ alias declaration provides a convenien...
Definition: DisableIf.h:224
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:565
void deserializeHeader(Archive &archive, const VT &vec)
Deserializes all meta information about the given vector.
Definition: VectorSerializer.h:476
void serializeVector(Archive &archive, const DenseVector< VT, TF > &vec)
Serializes the elements of a dense vector.
Definition: VectorSerializer.h:387
uint8_t version_
The version of the archive.
Definition: VectorSerializer.h:280
#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
Header file for the DisableIf class template.
void serializeHeader(Archive &archive, const VT &vec)
Serializes all meta information about the given vector.
Definition: VectorSerializer.h:358
Namespace of the Blaze C++ math library.
Definition: Blaze.h:58
uint8_t elementSize_
The size in bytes of a single element of the vector.
Definition: VectorSerializer.h:283
const Element * ConstIterator
Iterator over constant elements.
Definition: CompressedMatrix.h:3085
typename T::ElementType ElementType_
Alias declaration for nested ElementType type definitions.The ElementType_ alias declaration provides...
Definition: Aliases.h:163
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.
BLAZE_ALWAYS_INLINE 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:430
Header file for the EnableIf class template.
64-bit unsigned integer type of the Blaze library.
Compile time check for resizable data types.This type trait tests whether the given data type is a re...
Definition: IsResizable.h:75
typename EnableIfTrue< Condition, T >::Type EnableIfTrue_
Auxiliary type for the EnableIfTrue class template.The EnableIfTrue_ alias declaration provides a con...
Definition: EnableIf.h:138
void deserialize(Archive &archive, Vector< VT, TF > &vec)
Deserializes a vector from the given archive.
Definition: VectorSerializer.h:448
#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:303
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:285
uint8_t type_
The type of the vector.
Definition: VectorSerializer.h:281
typename EnableIf< Condition, T >::Type EnableIf_
Auxiliary alias declaration for the EnableIf class template.The EnableIf_ alias declaration provides ...
Definition: EnableIf.h:224
Header file for the IsDenseVector type trait.
void deserializeSparseVector(Archive &archive, DenseVector< VT, TF > &vec)
Deserializes a sparse vector from the archive.
Definition: VectorSerializer.h:692
Header file for the TypeValueMapping class template.
typename T::ConstIterator ConstIterator_
Alias declaration for nested ConstIterator type definitions.The ConstIterator_ alias declaration prov...
Definition: Aliases.h:103
DisableIfTrue< VT::simdEnabled >::Type deserializeDenseVector(Archive &archive, DenseVector< VT, TF > &vec)
Deserializes a dense vector from the archive.
Definition: VectorSerializer.h:596
DisableIf_< IsResizable< VT > > prepareVector(DenseVector< VT, TF > &vec)
Prepares the given non-resizable dense vector for the deserialization process.
Definition: VectorSerializer.h:513
Base class for N-dimensional vectors.The Vector class is a base class for all arbitrarily sized (N-di...
Definition: Forward.h:177
Base class for sparse vectors.The SparseVector class is a base class for all arbitrarily sized (N-dim...
Definition: Forward.h:130
Constraint on the data type.
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:282