Skip to content

Commit

Permalink
Add serialization support for Client and Dataset (#293)
Browse files Browse the repository at this point in the history
Enable serialization for Client and Dataset, such as via Python print() statement for debugging and demo support
[ committed by @billschereriii ]
[ reviewed by @MattToast @ashao @mellis13 ]
  • Loading branch information
billschereriii authored Jan 28, 2023
1 parent a90f051 commit 86b6d7a
Show file tree
Hide file tree
Showing 49 changed files with 609 additions and 63 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ include_directories(SYSTEM

if (BUILD_FORTRAN)
set(FORTRAN_SRC
src/fortran/errors.F90
src/fortran/client.F90
src/fortran/dataset.F90
src/fortran/errors.F90
src/fortran/fortran_c_interop.F90
src/fortran/logcontext.F90
src/fortran/logger.F90
Expand Down
3 changes: 3 additions & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Description

- Update to the latest version of PyBind
- Change documentation theme to sphinx_book_theme and fix doc strings
- Add print capability for Client and DataSet
- Add support for inspection of tensors and metadata inside datasets
- Add support for user-directed logging for Python clients, using Client, Dataset, or LogContext logging methods
- Add support for user-directed logging for C and Fortran clients without a Client or Dataset context
Expand All @@ -30,6 +31,7 @@ Detailed Notes

- Updated from PyBind v2.6.2 to v2.10.3 (PR295_)
- Change documentation theme to sphinx_book_theme to match SmartSim documentation theme and fix Python API doc string errors (PR294_)
- Added print capability for Client and DataSet to give details diagnostic information for debugging (PR293_)
- Added support for retrieval of names and types of tensors and metadata inside datasets (PR291_)
- Added support for user-directed logging for Python clients via {Client, Dataset, LogContext}.{log_data, log_warning, log_error} methods (PR289_)
- Added support for user-directed logging without a Client or Dataset context to C and Fortran clients via _string() methods (PR288_)
Expand All @@ -48,6 +50,7 @@ Detailed Notes

.. _PR295: https://github.com/CrayLabs/SmartRedis/pull/295
.. _PR294: https://github.com/CrayLabs/SmartRedis/pull/294
.. _PR294: https://github.com/CrayLabs/SmartRedis/pull/293
.. _PR291: https://github.com/CrayLabs/SmartRedis/pull/291
.. _PR289: https://github.com/CrayLabs/SmartRedis/pull/289
.. _PR288: https://github.com/CrayLabs/SmartRedis/pull/288
Expand Down
9 changes: 9 additions & 0 deletions include/c_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -1530,6 +1530,7 @@ SRError get_dataset_list_range(void* c_client, const char* list_name,
const size_t list_name_length,
const int start_index, const int end_index,
void*** datasets, size_t* num_datasets);

/*
* \brief Get a range of datasets (by index) from an aggregation list and
copy them into an already allocated vector of datasets. Note,
Expand Down Expand Up @@ -1567,6 +1568,14 @@ SRError _get_dataset_list_range_allocated(void* c_client, const char* list_name,
const int start_index, const int end_index,
void** datasets);


/*!
* \brief Retrieve a string representation of the client
* \param c_client The client object to use for communication
* \return A string with either the client representation or an error message
*/
const char* client_to_string(void* c_client);

#ifdef __cplusplus
}

Expand Down
7 changes: 7 additions & 0 deletions include/c_dataset.h
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,13 @@ SRError get_metadata_field_names(
SRError get_metadata_field_type(
void* dataset, const char* name, size_t name_len, SRMetaDataType* mdtype);

/*!
* \brief Retrieve a string representation of the dataset
* \param dataset The dataset to use for this operation
* \return A string with either the client representation or an error message
*/
const char* dataset_to_string(void* dataset);

#ifdef __cplusplus
}
#endif
Expand Down
19 changes: 19 additions & 0 deletions include/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -1250,6 +1250,12 @@ class Client : public SRObject
const int start_index,
const int end_index);

/*!
* \brief Create a string representation of the client
* \returns A string containing client details
*/
std::string to_string() const;

protected:

/*!
Expand Down Expand Up @@ -1653,6 +1659,19 @@ class Client : public SRObject

};

/*!
* \brief Serialize a client
* \param client The client to serialize
* \returns The output stream, for chaining
*/
inline
std::ostream& operator<<(std::ostream& stream, const Client& client)
{
stream << client.to_string();
return stream;
}


} // namespace SmartRedis

#endif // __cplusplus
Expand Down
31 changes: 25 additions & 6 deletions include/dataset.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ class DataSet : public SRObject
* metadata field name
* \throw SmartRedis::Exception if metadata retrieval fails
*/
std::vector<std::string> get_meta_strings(const std::string& name);
std::vector<std::string> get_meta_strings(
const std::string& name) const;

/*!
* \brief Retrieve metadata string field values from the DataSet.
Expand All @@ -266,7 +267,7 @@ class DataSet : public SRObject
* \param field_name The name of the field to check
* \returns True iff the DataSet contains the field
*/
bool has_field(const std::string& field_name);
bool has_field(const std::string& field_name) const;


/*!
Expand Down Expand Up @@ -297,7 +298,7 @@ class DataSet : public SRObject
* \returns The name of the tensors in the DataSet
* \throw SmartRedis::Exception if metadata retrieval fails
*/
std::vector<std::string> get_tensor_names();
std::vector<std::string> get_tensor_names() const;

/*!
* \brief Retrieve tensor names from the DataSet.
Expand All @@ -318,13 +319,13 @@ class DataSet : public SRObject
* \returns The data type for the tensor
* \throw SmartRedis::Exception if tensor name retrieval fails
*/
SRTensorType get_tensor_type(const std::string& name);
SRTensorType get_tensor_type(const std::string& name) const;

/*!
* \brief Retrieve the names of all metadata fields in the DataSet
* \returns A vector of metadata field names
*/
std::vector<std::string> get_metadata_field_names();
std::vector<std::string> get_metadata_field_names() const;

/*!
* \brief Retrieve metadata field names from the DataSet.
Expand All @@ -346,7 +347,13 @@ class DataSet : public SRObject
* \returns The data type for the metadata field
* \throw SmartRedis::Exception if metadata field name retrieval fails
*/
SRMetaDataType get_metadata_field_type(const std::string& name);
SRMetaDataType get_metadata_field_type(const std::string& name) const;

/*!
* \brief Create a string representation of the DataSet
* \returns A string containing DataSet details
*/
std::string to_string() const;

friend class Client;
friend class PyDataset;
Expand Down Expand Up @@ -475,6 +482,18 @@ class DataSet : public SRObject

};

/*!
* \brief Serialize a dataset
* \param dataset The dataset to serialize
* \returns The output stream, for chaining
*/
inline
std::ostream& operator<<(std::ostream& stream, const DataSet& dataset)
{
stream << dataset.to_string();
return stream;
}

} // namespace SmartRedis

#endif
Expand Down
10 changes: 6 additions & 4 deletions include/metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ class MetaData
* \param name The name of the string field to retrieve
* \returns A vector of the strings in the field
*/
std::vector<std::string> get_string_values(const std::string& name);
std::vector<std::string> get_string_values(
const std::string& name) const;

/*!
* \brief Get metadata string field using a c-style
Expand Down Expand Up @@ -214,7 +215,7 @@ class MetaData
* \returns Boolean indicating if the DataSet has
* the field.
*/
bool has_field(const std::string& field_name);
bool has_field(const std::string& field_name) const;

/*!
* \brief This function clears all entries in a
Expand All @@ -238,14 +239,15 @@ class MetaData
* \param name The name of the field to check
* \throw KeyException if the name is not present
*/
SRMetaDataType get_field_type(const std::string& name);
SRMetaDataType get_field_type(const std::string& name) const;

/*!
* \brief Retrieve a vector of metadata field names
* \param skip_internal Omit internal items (such as .tensor_names)
* from the results
*/
std::vector<std::string> get_field_names(bool skip_internal = false);
std::vector<std::string> get_field_names(
bool skip_internal = false) const;

/*!
* \brief Get metadata field names using a c-style
Expand Down
6 changes: 3 additions & 3 deletions include/metadatafield.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,19 @@ class MetadataField {
* \brief Retrieve the MetadataField name
* \returns MetadataField name
*/
std::string name();
std::string name() const;

/*!
* \brief Retrieve the Metadatafield type
* \returns MetadataField type
*/
SRMetaDataType type();
SRMetaDataType type() const;

/*!
* \brief Retrieve the number of values in the field
* \returns The number of values
*/
virtual size_t size() = 0;
virtual size_t size() const = 0;

/*!
* \brief Clear the values in the field
Expand Down
7 changes: 7 additions & 0 deletions include/pyclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,13 @@ class PyClient : public PySRObject
const int start_index,
const int end_index);

/*!
* \brief Create a string representation of the Client
* \returns A string representation of the Client
*/
std::string to_string();


private:

/*!
Expand Down
6 changes: 6 additions & 0 deletions include/pydataset.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ class PyDataset : public PySRObject
*/
DataSet* get();

/*!
* \brief Create a string representation of the DataSet
* \returns A string representation of the DataSet
*/
std::string to_string();

private:

DataSet* _dataset;
Expand Down
6 changes: 6 additions & 0 deletions include/redis.h
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,12 @@ class Redis : public RedisServer
const std::string& key,
const bool reset_stat);

/*!
* \brief Create a string representation of the Redis connection
* \returns A string representation of the Redis connection
*/
virtual std::string to_string() const;

private:

/*!
Expand Down
6 changes: 6 additions & 0 deletions include/rediscluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,12 @@ class RedisCluster : public RedisServer
const std::string& key,
const bool reset_stat);

/*!
* \brief Create a string representation of the Redis connection
* \returns A string representation of the Redis connection
*/
virtual std::string to_string() const;

protected:

/*!
Expand Down
6 changes: 6 additions & 0 deletions include/redisserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,12 @@ class RedisServer {
const std::string& key,
const bool reset_stat) = 0;

/*!
* \brief Create a string representation of the Redis connection
* \returns A string representation of the Redis connection
*/
virtual std::string to_string() const = 0;

protected:

/*!
Expand Down
2 changes: 1 addition & 1 deletion include/scalarfield.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class ScalarField : public MetadataField {
* \brief Retrieve the number of values in the field
* \returns The number of values
*/
virtual size_t size();
virtual size_t size() const;

/*!
* \brief Clear the values in the field
Expand Down
2 changes: 1 addition & 1 deletion include/scalarfield.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void ScalarField<T>::append(const void* value)

// Retrieve the number of values in a scalar
template <class T>
size_t ScalarField<T>::size()
size_t ScalarField<T>::size() const
{
return _vals.size();
}
Expand Down
2 changes: 1 addition & 1 deletion include/srobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class SRObject
return _lname;
}

private:
protected:

/*!
* \brief The name prefix log entries with
Expand Down
2 changes: 1 addition & 1 deletion include/stringfield.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class StringField : public MetadataField {
* \brief Retrieve the number of values in the field
* \returns The number of values
*/
virtual size_t size();
virtual size_t size() const;

/*!
* \brief Clear the values in the field
Expand Down
8 changes: 4 additions & 4 deletions include/tensorbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,13 @@ class TensorBase{
* \brief Retrieve the name of the TensorBase
* \returns The name of the TensorBase
*/
std::string name();
std::string name() const;

/*!
* \brief Retrieve the type of the TensorBase
* \returns The type of the TensorBase
*/
SRTensorType type();
SRTensorType type() const;

/*!
* \brief Retrieve a string representation of
Expand All @@ -170,13 +170,13 @@ class TensorBase{
* \brief Retrieve the dimensions of the TensorBase
* \returns TensorBase dimensions
*/
std::vector<size_t> dims();
std::vector<size_t> dims() const;

/*!
* \brief Retrieve number of values in the TensorBase
* \returns The number values in the TensorBase
*/
size_t num_values();
size_t num_values() const;

/*!
* \brief Retrieve a pointer to the TensorBase data
Expand Down
4 changes: 2 additions & 2 deletions include/tensorpack.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class TensorPack
* \param name The name used to reference the tensor
* \returns A pointer to the TensorBase object
*/
TensorBase* get_tensor(const std::string& name);
TensorBase* get_tensor(const std::string& name) const;

/*!
* \brief Return a pointer to the tensor data memory space
Expand All @@ -149,7 +149,7 @@ class TensorPack
* \returns True if the name corresponds to a tensor
* in the TensorPack, otherwise False.
*/
bool tensor_exists(const std::string& name);
bool tensor_exists(const std::string& name) const;

/*!
* \brief Returns an iterator pointing to the
Expand Down
Loading

0 comments on commit 86b6d7a

Please sign in to comment.