Blaze 3.9
Loading...
Searching...
No Matches
blaze::VectorSerializer Class Reference

Serializer for dense and sparse vectors. More...

#include <VectorSerializer.h>

Public Member Functions

Constructor
 VectorSerializer ()
 The default constructor of the VectorSerializer class.

Private Attributes

Member variables
uint8_t version_
 The version of the archive.
uint8_t type_
 The type of the vector.
uint8_t elementType_
 The type of an element.
uint8_t elementSize_
 The size in bytes of a single element of the vector.
uint64_t size_
 The size of the vector.
uint64_t number_
 The total number of elements contained in the vector.

Serialization functions

template<typename Archive, typename VT, bool TF>
void serialize (Archive &archive, const Vector< VT, TF > &vec)
 Serializes the given vector and writes it to the archive.
template<typename Archive, typename VT>
void serializeHeader (Archive &archive, const VT &vec)
 Serializes all meta information about the given vector.
template<typename Archive, typename VT, bool TF>
void serializeVector (Archive &archive, const DenseVector< VT, TF > &vec)
 Serializes the elements of a dense vector.
template<typename Archive, typename VT, bool TF>
void serializeVector (Archive &archive, const SparseVector< VT, TF > &vec)
 Serializes the elements of a sparse vector.

Deserialization functions

template<typename Archive, typename VT, bool TF>
void deserialize (Archive &archive, Vector< VT, TF > &vec)
 Deserializes a vector from the given archive.
template<typename Archive, typename VT>
void deserializeHeader (Archive &archive, const VT &vec)
 Deserializes all meta information about the given vector.
template<typename VT, bool TF>
DisableIf_t< IsResizable_v< VT > > prepareVector (DenseVector< VT, TF > &vec)
 Prepares the given non-resizable dense vector for the deserialization process.
template<typename VT, bool TF>
DisableIf_t< IsResizable_v< VT > > prepareVector (SparseVector< VT, TF > &vec)
 Prepares the given non-resizable sparse vector for the deserialization process.
template<typename VT>
EnableIf_t< IsResizable_v< VT > > prepareVector (VT &vec)
 Prepares the given resizable vector for the deserialization process.
template<typename Archive, typename VT>
void deserializeVector (Archive &archive, VT &vec)
 Deserializes a vector from the archive.
template<typename Archive, typename VT, bool TF>
DisableIf_t< VT::simdEnabled > deserializeDenseVector (Archive &archive, DenseVector< VT, TF > &vec)
 Deserializes a dense vector from the archive.
template<typename Archive, typename VT, bool TF>
EnableIf_t< VT::simdEnabled > deserializeDenseVector (Archive &archive, DenseVector< VT, TF > &vec)
 Deserializes a dense vector from the archive.
template<typename Archive, typename VT, bool TF>
void deserializeDenseVector (Archive &archive, SparseVector< VT, TF > &vec)
 Deserializes a dense vector from the archive.
template<typename Archive, typename VT, bool TF>
void deserializeSparseVector (Archive &archive, DenseVector< VT, TF > &vec)
 Deserializes a sparse vector from the archive.
template<typename Archive, typename VT, bool TF>
void deserializeSparseVector (Archive &archive, SparseVector< VT, TF > &vec)
 Deserializes a sparse vector from the archive.

Detailed Description

Serializer for dense and sparse vectors.

The VectorSerializer implements the necessary logic to serialize dense and sparse vectors, i.e. to convert them into a portable, binary representation. The following example demonstrates the (de-)serialization process of vectors:

// Serialization of both vectors
{
// ... Resizing and initialization
// Creating an archive that writes into a the file "vectors.blaze"
blaze::Archive<std::ofstream> archive( "vectors.blaze" );
// Serialization of both vectors into the same archive. Note that d lies before s!
archive << d << s;
}
// Reconstitution of both vectors
{
// Creating an archive that reads from the file "vectors.blaze"
blaze::Archive<std::ofstream> archive( "vectors.blaze" );
// Reconstituting the former d vector into d1. Note that it is possible to reconstitute
// the vector into a differrent kind of vector (StaticVector -> DynamicVector), but that
// the type of elements has to be the same.
archive >> d1;
// Reconstituting the former s vector into d2. Note that is is even possible to reconstitute
// a sparse vector as a dense vector (also the reverse is possible) and that a column vector
// can be reconstituted as row vector (and vice versa). Note however that also in this case
// the type of elements is the same!
archive >> d2
}
Binary archive for the portable serialization of data.
Definition Archive.h:140
Efficient implementation of an arbitrary sized sparse vector.
Definition CompressedVector.h:220
Efficient implementation of an arbitrary sized vector.
Definition DynamicVector.h:223
Efficient implementation of a fixed-sized vector.
Definition StaticVector.h:230
constexpr bool columnVector
Transpose flag for column vectors.
Definition TransposeFlag.h:58
constexpr bool rowVector
Transpose flag for row vectors.
Definition TransposeFlag.h:73

Note that it is even possible to (de-)serialize vectors with vector or matrix elements:

// Serialization
{
// ... Resizing and initialization
// Creating an archive that writes into a the file "vector.blaze"
blaze::Archive<std::ofstream> archive( "vector.blaze" );
// Serialization of the vector into the archive
archive << vec;
}
// Deserialization
{
// Creating an archive that reads from the file "vector.blaze"
blaze::Archive<std::ofstream> archive( "vector.blaze" );
// Reconstitution of the vector from the archive
archive >> vec;
}

As the examples demonstrates, the vector serialization offers an enormous flexibility. However, several actions result in errors:

  • vectors cannot be reconstituted as matrices (and vice versa)
  • the element type of the serialized and reconstituted vector must match, which means that on the source and destination platform the general type (signed/unsigned integral or floating point) and the size of the type must be exactly the same
  • when reconstituting a StaticVector, its size must match the size of the serialized vector

In case an error is encountered during (de-)serialization, a std::runtime_exception is thrown.

Member Function Documentation

◆ deserialize()

template<typename Archive, typename VT, bool TF>
void blaze::VectorSerializer::deserialize ( Archive & archive,
Vector< VT, TF > & vec )

Deserializes a vector from the given archive.

Parameters
archiveThe archive to be read from.
vecThe vector to be deserialized.
Returns
void
Exceptions
std::runtime_errorError during deserialization.

◆ deserializeDenseVector() [1/3]

template<typename Archive, typename VT, bool TF>
EnableIf_t< VT::simdEnabled > blaze::VectorSerializer::deserializeDenseVector ( Archive & archive,
DenseVector< VT, TF > & vec )
private

Deserializes a dense vector from the archive.

Parameters
archiveThe archive to be read from.
vecThe dense vector to be reconstituted.
Returns
void
Exceptions
std::runtime_errorDense vector could not be deserialized.

This function deserializes a dense vector from the archive and reconstitutes the given dense vector. In case any error is detected during the deserialization process, a std::runtime_error is thrown.

◆ deserializeDenseVector() [2/3]

template<typename Archive, typename VT, bool TF>
DisableIf_t< VT::simdEnabled > blaze::VectorSerializer::deserializeDenseVector ( Archive & archive,
DenseVector< VT, TF > & vec )
private

Deserializes a dense vector from the archive.

Parameters
archiveThe archive to be read from.
vecThe dense vector to be reconstituted.
Returns
void
Exceptions
std::runtime_errorDense vector could not be deserialized.

This function deserializes a dense vector from the archive and reconstitutes the given dense vector. In case any error is detected during the deserialization process, a std::runtime_error is thrown.

◆ deserializeDenseVector() [3/3]

template<typename Archive, typename VT, bool TF>
void blaze::VectorSerializer::deserializeDenseVector ( Archive & archive,
SparseVector< VT, TF > & vec )
private

Deserializes a dense vector from the archive.

Parameters
archiveThe archive to be read from.
vecThe sparse vector to be reconstituted.
Returns
void
Exceptions
std::runtime_errorSparse vector could not be deserialized.

This function deserializes a dense vector from the archive and reconstitutes the given sparse vector. In case any error is detected during the deserialization process, a std::runtime_error is thrown.

◆ deserializeHeader()

template<typename Archive, typename VT>
void blaze::VectorSerializer::deserializeHeader ( Archive & archive,
const VT & vec )
private

Deserializes all meta information about the given vector.

Parameters
archiveThe archive to be read from.
vecThe vector to be deserialized.
Returns
void
Exceptions
std::runtime_errorError during deserialization.

This function deserializes all meta information about the given vector contained in the header of the given archive. In case any error is detected during the deserialization process (for instance an invalid type of vector, element type, element size, or vector size) a std::runtime_error is thrown.

◆ deserializeSparseVector() [1/2]

template<typename Archive, typename VT, bool TF>
void blaze::VectorSerializer::deserializeSparseVector ( Archive & archive,
DenseVector< VT, TF > & vec )
private

Deserializes a sparse vector from the archive.

Parameters
archiveThe archive to be read from.
vecThe dense vector to be reconstituted.
Returns
void
Exceptions
std::runtime_errorDense vector could not be deserialized.

This function deserializes a sparse vector from the archive and reconstitutes the given dense vector. In case any error is detected during the deserialization process, a std::runtime_error is thrown.

◆ deserializeSparseVector() [2/2]

template<typename Archive, typename VT, bool TF>
void blaze::VectorSerializer::deserializeSparseVector ( Archive & archive,
SparseVector< VT, TF > & vec )
private

Deserializes a sparse vector from the archive.

Parameters
archiveThe archive to be read from.
vecThe sparse vector to be reconstituted.
Returns
void
Exceptions
std::runtime_errorSparse vector could not be deserialized.

This function deserializes a sparse vector from the archive and reconstitutes the given sparse vector. In case any error is detected during the deserialization process, a std::runtime_error is thrown.

◆ deserializeVector()

template<typename Archive, typename VT>
void blaze::VectorSerializer::deserializeVector ( Archive & archive,
VT & vec )
private

Deserializes a vector from the archive.

Parameters
archiveThe archive to be read from.
vecThe vector to be reconstituted.
Returns
void
Exceptions
std::runtime_errorError during deserialization.

This function deserializes the contents of the vector from the archive and reconstitutes the given vector.

◆ prepareVector() [1/3]

template<typename VT, bool TF>
DisableIf_t< IsResizable_v< VT > > blaze::VectorSerializer::prepareVector ( DenseVector< VT, TF > & vec)
private

Prepares the given non-resizable dense vector for the deserialization process.

Parameters
vecThe dense vector to be prepared.
Returns
void

◆ prepareVector() [2/3]

template<typename VT, bool TF>
DisableIf_t< IsResizable_v< VT > > blaze::VectorSerializer::prepareVector ( SparseVector< VT, TF > & vec)
private

Prepares the given non-resizable sparse vector for the deserialization process.

Parameters
vecThe sparse vector to be prepared.
Returns
void

◆ prepareVector() [3/3]

template<typename VT>
EnableIf_t< IsResizable_v< VT > > blaze::VectorSerializer::prepareVector ( VT & vec)
private

Prepares the given resizable vector for the deserialization process.

Parameters
vecThe vector to be prepared.
Returns
void

◆ serialize()

template<typename Archive, typename VT, bool TF>
void blaze::VectorSerializer::serialize ( Archive & archive,
const Vector< VT, TF > & vec )

Serializes the given vector and writes it to the archive.

Parameters
archiveThe archive to be written.
vecThe vector to be serialized.
Returns
void
Exceptions
std::runtime_errorError during serialization.

This function serializes the given vector and writes it to the given archive. In case any error is detected during the serialization, a std::runtime_error is thrown.

◆ serializeHeader()

template<typename Archive, typename VT>
void blaze::VectorSerializer::serializeHeader ( Archive & archive,
const VT & vec )
private

Serializes all meta information about the given vector.

Parameters
archiveThe archive to be written.
vecThe vector to be serialized.
Returns
void
Exceptions
std::runtime_errorFile header could not be serialized.

◆ serializeVector() [1/2]

template<typename Archive, typename VT, bool TF>
void blaze::VectorSerializer::serializeVector ( Archive & archive,
const DenseVector< VT, TF > & vec )
private

Serializes the elements of a dense vector.

Parameters
archiveThe archive to be written.
vecThe vector to be serialized.
Returns
void
Exceptions
std::runtime_errorDense vector could not be serialized.

◆ serializeVector() [2/2]

template<typename Archive, typename VT, bool TF>
void blaze::VectorSerializer::serializeVector ( Archive & archive,
const SparseVector< VT, TF > & vec )
private

Serializes the elements of a sparse vector.

Parameters
archiveThe archive to be written.
vecThe vector to be serialized.
Returns
void
Exceptions
std::runtime_errorSparse vector could not be serialized.

The documentation for this class was generated from the following file: