forked from lf-lang/lingua-franca
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request lf-lang#2375 from lf-lang/custom-serializer
Custom Serialization in Python Target
- Loading branch information
Showing
12 changed files
with
281 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
core/src/main/java/org/lflang/federated/serialization/FedCustomPythonSerialization.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/************* | ||
* Copyright (c) 2024, The University of California at Berkeley. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* 1. Redistributions of source code must retain the above copyright notice, | ||
* this list of conditions and the following disclaimer. | ||
* | ||
* 2. Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
***************/ | ||
|
||
package org.lflang.federated.serialization; | ||
|
||
import org.lflang.generator.GeneratorBase; | ||
import org.lflang.target.Target; | ||
|
||
/** | ||
* Enables support for custom serialization. | ||
* | ||
* @author Shulu Li | ||
*/ | ||
public class FedCustomPythonSerialization implements FedSerialization { | ||
|
||
String customSerializerPackage; | ||
|
||
public FedCustomPythonSerialization(String customSerializerPackage) { | ||
this.customSerializerPackage = customSerializerPackage; | ||
} | ||
|
||
@Override | ||
public boolean isCompatible(GeneratorBase generator) { | ||
if (generator.getTarget() != Target.Python) { | ||
throw new UnsupportedOperationException( | ||
"The FedCustomPythonSerialization class only supports the Python target."); | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public String serializedBufferLength() { | ||
return serializedVarName + ".len"; | ||
} | ||
|
||
@Override | ||
public String serializedBufferVar() { | ||
return serializedVarName + ".buf"; | ||
} | ||
|
||
private String initializeCustomSerializer() { | ||
return "if (self->custom_serializer == NULL) \n" | ||
+ "self->custom_serializer = load_serializer(\"%s\");\n".formatted(customSerializerPackage); | ||
} | ||
|
||
@Override | ||
public StringBuilder generateNetworkSerializerCode(String varName, String originalType) { | ||
StringBuilder serializerCode = new StringBuilder(); | ||
// Initialize self->custom_serializer if null | ||
serializerCode.append(this.initializeCustomSerializer()); | ||
// Serialize PyObject to bytes using custom serializer | ||
serializerCode.append( | ||
""" | ||
PyObject *serialized_pyobject = custom_serialize(msg[0]->value, self->custom_serializer); | ||
Py_buffer %s; | ||
int returnValue = PyBytes_AsStringAndSize(serialized_pyobject, (char**)&%s.buf, &%s.len); | ||
if (returnValue == -1) { | ||
if (PyErr_Occurred()) PyErr_Print(); | ||
lf_print_error_and_exit("Could not serialize %s."); | ||
} | ||
""" | ||
.formatted(serializedVarName, serializedVarName, serializedVarName, serializedVarName)); | ||
return serializerCode; | ||
} | ||
|
||
@Override | ||
public StringBuilder generateNetworkDeserializerCode(String varName, String targetType) { | ||
StringBuilder deserializerCode = new StringBuilder(); | ||
// Initialize self->custom_serializer if null | ||
deserializerCode.append(this.initializeCustomSerializer()); | ||
// Deserialize network message to a PyObject using custom serializer | ||
deserializerCode.append( | ||
""" | ||
PyObject *message_byte_array = PyBytes_FromStringAndSize((char*) %s->token->value, %s->token->length); | ||
PyObject *%s = custom_deserialize(message_byte_array, self->custom_serializer); | ||
if ( %s == NULL ) { | ||
if (PyErr_Occurred()) PyErr_Print(); | ||
lf_print_error_and_exit("Could not serialize %s."); | ||
} | ||
""" | ||
.formatted( | ||
varName, varName, deserializedVarName, deserializedVarName, deserializedVarName)); | ||
return deserializerCode; | ||
} | ||
|
||
@Override | ||
public StringBuilder generatePreambleForSupport() { | ||
return new StringBuilder(); | ||
} | ||
|
||
@Override | ||
public StringBuilder generateCompilerExtensionForSupport() { | ||
return new StringBuilder(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule reactor-c
updated
3 files
+14 −2 | core/threaded/watchdog.c | |
+23 −1 | python/include/pythontarget.h | |
+55 −30 | python/lib/pythontarget.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# To run this test, the `pickle_serializer` package must be installed in the Python environment. | ||
# Run `pip3 install -e ./test/Python/src/serialization/pickle_serializer` in the project root directory to install the pickle_serializer. | ||
target Python { | ||
coordination: decentralized | ||
} | ||
|
||
preamble {= | ||
os.system("pip install ./src/serialization/pickle_serializer/ --user") | ||
=} | ||
|
||
reactor Client { | ||
input server_message | ||
output client_message | ||
state count | ||
|
||
reaction(startup) {= | ||
self.count = 0 | ||
print("Client Startup!") | ||
=} | ||
|
||
reaction(server_message) -> client_message {= | ||
val = server_message.value | ||
if val != self.count: | ||
print("client: out of order", val, self.count) | ||
exit(1) | ||
self.count+=2 | ||
val += 1 | ||
print("client:", val) | ||
if val==23: | ||
print("client done") | ||
request_stop() | ||
if val<23: | ||
client_message.set(val) | ||
=} | ||
} | ||
|
||
reactor Server { | ||
output server_message | ||
input client_message | ||
state count | ||
|
||
reaction(startup) -> server_message {= | ||
self.count = 1 | ||
print("Server Startup!") | ||
server_message.set(0) | ||
=} | ||
|
||
reaction(client_message) -> server_message {= | ||
val = client_message.value | ||
if val != self.count: | ||
print("server: out of order", val, self.count) | ||
exit(1) | ||
self.count+=2 | ||
val += 1 | ||
print("server:", val) | ||
if val==22: | ||
print("server done") | ||
server_message.set(val) | ||
request_stop() | ||
if val<22: | ||
server_message.set(val) | ||
=} | ||
} | ||
|
||
federated reactor { | ||
client = new Client() | ||
server = new Server() | ||
server.server_message -> client.server_message after 100 ms serializer "pickle_serializer" | ||
client.client_message -> server.client_message serializer "pickle_serializer" | ||
} |
1 change: 1 addition & 0 deletions
1
test/Python/src/serialization/pickle_serializer/pickle_serializer/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .serializer import Serializer |
7 changes: 7 additions & 0 deletions
7
test/Python/src/serialization/pickle_serializer/pickle_serializer/serializer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import pickle | ||
|
||
class Serializer(): | ||
def serialize(self, obj)->bytes: | ||
return pickle.dumps(obj) | ||
def deserialize(self, message:bytes): | ||
return pickle.loads(message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from setuptools import setup, find_packages | ||
|
||
setup( | ||
name='pickle_serializer', | ||
version='0.1', | ||
packages=find_packages(), | ||
install_requires=[], | ||
) |