Blaze 3.9
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>
52#include <blaze/util/Assert.h>
53#include <blaze/util/EnableIf.h>
54#include <blaze/util/Types.h>
55
56
57namespace blaze {
58
59//=================================================================================================
60//
61// CLASS DEFINITION
62//
63//=================================================================================================
64
65//*************************************************************************************************
153{
154 private:
155 //**Private class VectorValueMappingHelper******************************************************
169 template< bool IsDenseVector >
170 struct VectorValueMappingHelper;
172 //**********************************************************************************************
173
174 //**Private class VectorValueMapping************************************************************
182 template< typename T >
183 struct VectorValueMapping
184 {
185 enum { value = VectorValueMappingHelper< IsDenseVector_v<T> >::value };
187 };
189 //**********************************************************************************************
190
191 public:
192 //**Constructor*********************************************************************************
195 inline VectorSerializer();
197 //**********************************************************************************************
198
199 //**Serialization functions*********************************************************************
202 template< typename Archive, typename VT, bool TF >
203 void serialize( Archive& archive, const Vector<VT,TF>& vec );
205 //**********************************************************************************************
206
207 //**Deserialization functions*********************************************************************
210 template< typename Archive, typename VT, bool TF >
211 void deserialize( Archive& archive, Vector<VT,TF>& vec );
213 //**********************************************************************************************
214
215 private:
216 //**Serialization functions*********************************************************************
219 template< typename Archive, typename VT >
220 void serializeHeader( Archive& archive, const VT& vec );
221
222 template< typename Archive, typename VT, bool TF >
223 void serializeVector( Archive& archive, const DenseVector<VT,TF>& vec );
224
225 template< typename Archive, typename VT, bool TF >
226 void serializeVector( Archive& archive, const SparseVector<VT,TF>& vec );
228 //**********************************************************************************************
229
230 //**Deserialization functions*******************************************************************
233 template< typename Archive, typename VT >
234 void deserializeHeader( Archive& archive, const VT& vec );
235
236 template< typename VT, bool TF >
238
239 template< typename VT, bool TF >
241
242 template< typename VT >
244
245 template< typename Archive, typename VT >
246 void deserializeVector( Archive& archive, VT& vec );
247
248 template< typename Archive, typename VT, bool TF >
251
252 template< typename Archive, typename VT, bool TF >
255
256 template< typename Archive, typename VT, bool TF >
258
259 template< typename Archive, typename VT, bool TF >
261
262 template< typename Archive, typename VT, bool TF >
265 //**********************************************************************************************
266
267 //**Member variables****************************************************************************
270 uint8_t version_;
271 uint8_t type_;
272 uint8_t elementType_;
273 uint8_t elementSize_;
274 uint64_t size_;
275 uint64_t number_;
277 //**********************************************************************************************
278};
279//*************************************************************************************************
280
281
282
283
284//=================================================================================================
285//
286// CONSTRUCTOR
287//
288//=================================================================================================
289
290//*************************************************************************************************
294 : version_ ( 0U ) // The version of the archive
295 , type_ ( 0U ) // The type of the vector
296 , elementType_( 0U ) // The type of an element
297 , elementSize_( 0U ) // The size in bytes of a single element of the vector
298 , size_ ( 0UL ) // The size of the vector
299 , number_ ( 0UL ) // The total number of elements contained in the vector
300{}
301//*************************************************************************************************
302
303
304
305
306//=================================================================================================
307//
308// SERIALIZATION FUNCTIONS
309//
310//=================================================================================================
311
312//*************************************************************************************************
323template< typename Archive // Type of the archive
324 , typename VT // Type of the vector
325 , bool TF > // Transpose flag
327{
328 if( !archive ) {
329 BLAZE_THROW_RUNTIME_ERROR( "Faulty archive detected" );
330 }
331
332 serializeHeader( archive, *vec );
333 serializeVector( archive, *vec );
334}
335//*************************************************************************************************
336
337
338//*************************************************************************************************
346template< typename Archive // Type of the archive
347 , typename VT > // Type of the vector
348void VectorSerializer::serializeHeader( Archive& archive, const VT& vec )
349{
350 using ET = ElementType_t<VT>;
351
352 archive << uint8_t ( 1U );
353 archive << uint8_t ( VectorValueMapping<VT>::value );
354 archive << uint8_t ( TypeValueMapping<ET>::value );
355 archive << uint8_t ( sizeof( ET ) );
356 archive << uint64_t( vec.size() );
357 archive << uint64_t( IsDenseVector_v<VT> ? vec.size() : vec.nonZeros() );
358
359 if( !archive ) {
360 BLAZE_THROW_RUNTIME_ERROR( "File header could not be serialized" );
361 }
362}
363//*************************************************************************************************
364
365
366//*************************************************************************************************
374template< typename Archive // Type of the archive
375 , typename VT // Type of the vector
376 , bool TF > // Transpose flag
378{
379 size_t i( 0UL );
380 while( ( i < (*vec).size() ) && ( archive << (*vec)[i] ) ) {
381 ++i;
382 }
383
384 if( !archive ) {
385 BLAZE_THROW_RUNTIME_ERROR( "Dense vector could not be serialized" );
386 }
387}
388//*************************************************************************************************
389
390
391//*************************************************************************************************
399template< typename Archive // Type of the archive
400 , typename VT // Type of the vector
401 , bool TF > // Transpose flag
403{
404 using ConstIterator = ConstIterator_t<VT>;
405
406 ConstIterator element( (*vec).begin() );
407 while( ( element != (*vec).end() ) &&
408 ( archive << element->index() << element->value() ) ) {
409 ++element;
410 }
411
412 if( !archive ) {
413 BLAZE_THROW_RUNTIME_ERROR( "Sparse vector could not be serialized" );
414 }
415}
416//*************************************************************************************************
417
418
419
420
421//=================================================================================================
422//
423// DESERIALIZATION FUNCTIONS
424//
425//=================================================================================================
426
427//*************************************************************************************************
435template< typename Archive // Type of the archive
436 , typename VT // Type of the vector
437 , bool TF > // Transpose flag
439{
440 if( !archive ) {
441 BLAZE_THROW_INVALID_ARGUMENT( "Faulty archive detected" );
442 }
443
444 deserializeHeader( archive, *vec );
445 prepareVector( *vec );
446 deserializeVector( archive, *vec );
447}
448//*************************************************************************************************
449
450
451//*************************************************************************************************
464template< typename Archive // Type of the archive
465 , typename VT > // Type of the vector
466void VectorSerializer::deserializeHeader( Archive& archive, const VT& vec )
467{
468 using ET = ElementType_t<VT>;
469
470 if( !( archive >> version_ >> type_ >> elementType_ >> elementSize_ >> size_ >> number_ ) ) {
471 BLAZE_THROW_RUNTIME_ERROR( "Corrupt archive detected" );
472 }
473 else if( version_ != 1UL ) {
474 BLAZE_THROW_RUNTIME_ERROR( "Invalid version detected" );
475 }
476 else if( ( type_ & 1U ) != 0U || ( type_ & (~3U) ) != 0U ) {
477 BLAZE_THROW_RUNTIME_ERROR( "Invalid vector type detected" );
478 }
480 BLAZE_THROW_RUNTIME_ERROR( "Invalid element type detected" );
481 }
482 else if( elementSize_ != sizeof( ET ) ) {
483 BLAZE_THROW_RUNTIME_ERROR( "Invalid element size detected" );
484 }
485 else if( !IsResizable_v<VT> && size_ != vec.size() ) {
486 BLAZE_THROW_RUNTIME_ERROR( "Invalid vector size detected" );
487 }
488 else if( number_ > size_ ) {
489 BLAZE_THROW_RUNTIME_ERROR( "Invalid number of elements detected" );
490 }
491}
492//*************************************************************************************************
493
494
495//*************************************************************************************************
501template< typename VT // Type of the dense vector
502 , bool TF > // Transpose flag
504{
505 reset( *vec );
506}
507//*************************************************************************************************
508
509
510//*************************************************************************************************
516template< typename VT // Type of the sparse vector
517 , bool TF > // Transpose flag
519{
520 (*vec).reserve( number_ );
521 reset( *vec );
522}
523//*************************************************************************************************
524
525
526//*************************************************************************************************
532template< typename VT > // Type of the vector
534{
535 vec.resize ( size_, false );
536 vec.reserve( number_ );
537 reset( vec );
538}
539//*************************************************************************************************
540
541
542//*************************************************************************************************
553template< typename Archive // Type of the archive
554 , typename VT > // Type of the vector
556{
557 if( type_ == 0U ) {
558 deserializeDenseVector( archive, vec );
559 }
560 else if( type_ == 2U ) {
561 deserializeSparseVector( archive, vec );
562 }
563 else {
564 BLAZE_INTERNAL_ASSERT( false, "Undefined type flag" );
565 }
566}
567//*************************************************************************************************
568
569
570//*************************************************************************************************
582template< typename Archive // Type of the archive
583 , typename VT // Type of the vector
584 , bool TF > // Transpose flag
587{
588 using ET = ElementType_t<VT>;
589
590 size_t i( 0UL );
591 ET value{};
592
593 while( ( i != size_ ) && ( archive >> value ) ) {
594 (*vec)[i] = value;
595 ++i;
596 }
597
598 if( !archive ) {
599 BLAZE_THROW_RUNTIME_ERROR( "Dense vector could not be deserialized" );
600 }
601}
602//*************************************************************************************************
603
604
605//*************************************************************************************************
617template< typename Archive // Type of the archive
618 , typename VT // Type of the vector
619 , bool TF > // Transpose flag
622{
623 if( size_ == 0UL ) return;
624 archive.read( &(*vec)[0], size_ );
625
626 if( !archive ) {
627 BLAZE_THROW_RUNTIME_ERROR( "Dense vector could not be deserialized" );
628 }
629}
630//*************************************************************************************************
631
632
633//*************************************************************************************************
645template< typename Archive // Type of the archive
646 , typename VT // Type of the vector
647 , bool TF > // Transpose flag
649{
650 using ET = ElementType_t<VT>;
651
652 size_t i( 0UL );
653 ET value{};
654
655 while( ( i != size_ ) && ( archive >> value ) ) {
656 (*vec)[i] = value;
657 ++i;
658 }
659
660 if( !archive ) {
661 BLAZE_THROW_RUNTIME_ERROR( "Sparse vector could not be deserialized" );
662 }
663}
664//*************************************************************************************************
665
666
667//*************************************************************************************************
679template< typename Archive // Type of the archive
680 , typename VT // Type of the vector
681 , bool TF > // Transpose flag
683{
684 using ET = ElementType_t<VT>;
685
686 size_t i ( 0UL );
687 size_t index( 0UL );
688 ET value{};
689
690 while( ( i != number_ ) && ( archive >> index >> value ) ) {
691 (*vec)[index] = value;
692 ++i;
693 }
694
695 if( !archive ) {
696 BLAZE_THROW_RUNTIME_ERROR( "Dense vector could not be deserialized" );
697 }
698}
699//*************************************************************************************************
700
701
702//*************************************************************************************************
714template< typename Archive // Type of the archive
715 , typename VT // Type of the vector
716 , bool TF > // Transpose flag
718{
719 using ET = ElementType_t<VT>;
720
721 size_t i ( 0UL );
722 size_t index( 0UL );
723 ET value{};
724
725 while( ( i != number_ ) && ( archive >> index >> value ) ) {
726 (*vec).append( index, value, false );
727 ++i;
728 }
729
730 if( !archive ) {
731 BLAZE_THROW_RUNTIME_ERROR( "Sparse vector could not be deserialized" );
732 }
733}
734//*************************************************************************************************
735
736
737
738
739//=================================================================================================
740//
741// VECTORVALUEMAPPINGHELPER SPECIALIZATIONS
742//
743//=================================================================================================
744
745//*************************************************************************************************
749template<>
750struct VectorSerializer::VectorValueMappingHelper<true>
751{
752 enum { value = 0 };
753};
755//*************************************************************************************************
756
757
758//*************************************************************************************************
762template<>
763struct VectorSerializer::VectorValueMappingHelper<false>
764{
765 enum { value = 2 };
766};
768//*************************************************************************************************
769
770
771
772
773//=================================================================================================
774//
775// GLOBAL FUNCTIONS
776//
777//=================================================================================================
778
779//*************************************************************************************************
843template< typename Archive // Type of the archive
844 , typename VT // Type of the vector
845 , bool TF > // Transpose flag
846void serialize( Archive& archive, const Vector<VT,TF>& vec )
847{
848 VectorSerializer().serialize( archive, *vec );
849}
850//*************************************************************************************************
851
852
853//*************************************************************************************************
865template< typename Archive // Type of the archive
866 , typename VT // Type of the vector
867 , bool TF > // Transpose flag
868void deserialize( Archive& archive, Vector<VT,TF>& vec )
869{
870 VectorSerializer().deserialize( archive, *vec );
871}
872//*************************************************************************************************
873
874} // namespace blaze
875
876#endif
Header file for auxiliary alias declarations.
typename T::ElementType ElementType_t
Alias declaration for nested ElementType type definitions.
Definition: Aliases.h:190
typename T::ConstIterator ConstIterator_t
Alias declaration for nested ConstIterator type definitions.
Definition: Aliases.h:130
Header file for run time assertion macros.
Header file for the EnableIf class template.
Header file for the IsDenseVector type trait.
Header file for the IsResizable type trait.
void serialize(Archive &archive, const Matrix< MT, SO > &mat)
Serializes the given matrix and writes it to the archive.
Definition: MatrixSerializer.h:1256
void deserialize(Archive &archive, Matrix< MT, SO > &mat)
Deserializes a matrix from the given archive.
Definition: MatrixSerializer.h:1278
Header file for the TypeValueMapping class template.
Binary archive for the portable serialization of data.
Definition: Archive.h:140
Base class for N-dimensional dense vectors.
Definition: DenseVector.h:77
Base class for sparse vectors.
Definition: SparseVector.h:72
Serializer for dense and sparse vectors.
Definition: VectorSerializer.h:153
void deserializeHeader(Archive &archive, const VT &vec)
Deserializes all meta information about the given vector.
Definition: VectorSerializer.h:466
uint8_t elementSize_
The size in bytes of a single element of the vector.
Definition: VectorSerializer.h:273
void deserialize(Archive &archive, Vector< VT, TF > &vec)
Deserializes a vector from the given archive.
Definition: VectorSerializer.h:438
uint8_t elementType_
The type of an element.
Definition: VectorSerializer.h:272
uint64_t number_
The total number of elements contained in the vector.
Definition: VectorSerializer.h:275
VectorSerializer()
The default constructor of the VectorSerializer class.
Definition: VectorSerializer.h:293
void serialize(Archive &archive, const Vector< VT, TF > &vec)
Serializes the given vector and writes it to the archive.
Definition: VectorSerializer.h:326
void deserializeVector(Archive &archive, VT &vec)
Deserializes a vector from the archive.
Definition: VectorSerializer.h:555
void deserializeSparseVector(Archive &archive, DenseVector< VT, TF > &vec)
Deserializes a sparse vector from the archive.
Definition: VectorSerializer.h:682
uint64_t size_
The size of the vector.
Definition: VectorSerializer.h:274
uint8_t type_
The type of the vector.
Definition: VectorSerializer.h:271
void serializeHeader(Archive &archive, const VT &vec)
Serializes all meta information about the given vector.
Definition: VectorSerializer.h:348
DisableIf_t< IsResizable_v< VT > > prepareVector(DenseVector< VT, TF > &vec)
Prepares the given non-resizable dense vector for the deserialization process.
Definition: VectorSerializer.h:503
DisableIf_t< VT::simdEnabled > deserializeDenseVector(Archive &archive, DenseVector< VT, TF > &vec)
Deserializes a dense vector from the archive.
Definition: VectorSerializer.h:586
void serializeVector(Archive &archive, const DenseVector< VT, TF > &vec)
Serializes the elements of a dense vector.
Definition: VectorSerializer.h:377
uint8_t version_
The version of the archive.
Definition: VectorSerializer.h:270
Base class for N-dimensional vectors.
Definition: Vector.h:82
Constraint on the data type.
Header file for the DenseVector base class.
Header file for the SparseVector base class.
Header file for the Vector CRTP base class.
#define BLAZE_CONSTRAINT_MUST_BE_VECTOR_TYPE(T)
Constraint on the data type.
Definition: Vector.h:61
constexpr void reset(Matrix< MT, SO > &matrix)
Resetting the given matrix.
Definition: Matrix.h:806
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.
Definition: Assert.h:101
#define BLAZE_THROW_RUNTIME_ERROR(MESSAGE)
Macro for the emission of a std::runtime_error exception.
Definition: Exception.h:379
typename EnableIf< Condition, T >::Type EnableIf_t
Auxiliary type for the EnableIf class template.
Definition: EnableIf.h:138
#define BLAZE_THROW_INVALID_ARGUMENT(MESSAGE)
Macro for the emission of a std::invalid_argument exception.
Definition: Exception.h:235
typename EnableIf<!Condition, T >::Type DisableIf_t
Auxiliary type for the EnableIf class template.
Definition: EnableIf.h:175
Header file for the exception macros of the math module.
Conversion from a data type to a serial representation.
Definition: TypeValueMapping.h:164
Header file for basic type definitions.