diff --git a/src/Chemistry/SurrogateModels/serializer.cpp b/src/Chemistry/SurrogateModels/serializer.cpp index 162e57cfa..a212937e3 100644 --- a/src/Chemistry/SurrogateModels/serializer.cpp +++ b/src/Chemistry/SurrogateModels/serializer.cpp @@ -41,6 +41,15 @@ size_t calculateStructSize(void *struct_pointer, char type){ struct_size += vector.size() * sizeof(double); } } + else if (type == 'C'){ + struct_size += sizeof(size_t); // number of layers + struct_size += static_cast>>*>(struct_pointer)->size() * 2 * sizeof(size_t); // dimensions of matrices + for(const std::vector> &matrix : *static_cast>>*>(struct_pointer)){ + struct_size += matrix.size() * matrix[0].size() * sizeof(double); + } + + + } return struct_size; @@ -213,4 +222,55 @@ std::vector> deserializeTrainingData(char* data){ } +int serializeCPPWeights(std::vector>> &cpp_weights, char* memory){ + + size_t num_layers = cpp_weights.size(); + size_t size_counter = 0; + std::memcpy(memory, &num_layers, sizeof(size_t)); + memory += sizeof(size_t); + + for (size_t i=0; i>> deserializeCPPWeights(char *data){ + + std::vector>> deserialized_weights; + size_t num_layers; + std::memcpy(&num_layers, data, sizeof(size_t)); + data += sizeof(size_t); + + for(size_t i=0; i> weight_matrix(rows, std::vector(cols)); + for (size_t j = 0; j < rows; j++) { + std::memcpy(weight_matrix[j].data(), data, cols * sizeof(double)); + data += cols * sizeof(double); + deserialized_weights.push_back(weight_matrix); + } + } + + return deserialized_weights; +} + + } \ No newline at end of file diff --git a/src/Chemistry/SurrogateModels/serializer.hpp b/src/Chemistry/SurrogateModels/serializer.hpp index aa94a9d02..da266517c 100644 --- a/src/Chemistry/SurrogateModels/serializer.hpp +++ b/src/Chemistry/SurrogateModels/serializer.hpp @@ -63,5 +63,21 @@ int serializeTrainingData(std::vector> *data, char *memory); * species with m training elements */ std::vector> deserializeTrainingData(char* data); + +/** + * @brief Serialize the weights and biases of the model into a memory location + * to send them via RDMA + * + * @param model: 3d vector containing the weights and biases of the + * model + * @param memory: Pointer to the memory location where the serialized data will + * be stored + * The serialized data looks as follows: + * |# layers|# rows of matrix 1|# cols of matrix 1|matrix 1 data|# rows of matrix 2|... + * @return int: 0 if the serialization was successful, -1 otherwise + */ +int serializeCPPWeights(std::vector>> &cpp_weights, char* memory); + +std::vector>> deserializeCPPWeights(char* data); } #endif \ No newline at end of file