Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automation of proto R/W in hierarchies #61

Open
brunoguindani opened this issue Mar 12, 2021 · 5 comments
Open

Automation of proto R/W in hierarchies #61

brunoguindani opened this issue Mar 12, 2021 · 5 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@brunoguindani
Copy link
Contributor

Something that just crossed my mind. Do we have any way to automatize this process? maybe through a standardized interface for protos? The functions read/write_state_to_proto() and write_hypers_to_proto() are really the only low-level hierarchy operation left to the user's manual implementation. It would be amazing if they didn't have to do that.

@brunoguindani brunoguindani added the enhancement New feature or request label Mar 13, 2021
@mberaha
Copy link
Contributor

mberaha commented Mar 13, 2021

This is extremely interesting, but kind of far fetched at the moment. The problem is that a proto is only as good as its submessages, one could have a general proto like

message GeneralProto {
 map<string, double> scalarparams = 1;
 map<string, Vector> vectorparams = 2;
 map<string, Matrix> matrixparams = 3;
}

this is likely to fit all the possible states and hypers that we want to work with.
But then another problem comes along, suppose you have a simple struct like struct State {double a, b;} and want to write that struct to a GeneralProto using some call to a template function like

template<typename T>
void write_to_general_proto(const T& t, GeneralProto* out);

and here's where the fun begins: how do you do it? In Python, one can iterate through the elements of T (this is called reflection), but C++ has not such feature by default, i.e. in python I would do

def write_to_general_proto(obj, out: GeneralProto, prefix=""):
  for fieldname, fieldval in obj.__dict__.values():
     if isinstance(fieldval, double):
       out.scalarparms[prefix + fieldname] = fieldval # or something along this line
     elif....

     else:
       write_to_general_proto(fieldval, out, fieldname + ".")

where the last else should be able to handle nested objects

There are some tools to add reflection (one that I know of is https://github.com/garbageslam/visit_struct) but before settling for one, I would see what is the ecosystem available.

On the other hand, we could get rid of proto, and use some other C++ based serialization library only meant for C++. This would make life for pybmix a little harder, since then one needs to fine another workaround to pass the chains back and forth, but I'm open to discussion

@brunoguindani
Copy link
Contributor Author

Yeah, I figured it all comes down to looping on a struct's fields. Python dictionaries really spoiled us huh.
Protobufs are kinda nice TBH, especially for iterfacing with Python, and I wouldn't get rid of them. Also, finding a reflection tool sounds promising. Possibly, could we outsource the writing to another programming language? Would it be too difficult, or computationally expensive?

@mberaha
Copy link
Contributor

mberaha commented Mar 14, 2021

I'm not sure what you mean by "could we outsource the writing to another programming language?"

What is it you have in mind?

@brunoguindani
Copy link
Contributor Author

Like, embedding lines of another programming language (Python?) to perform that operation. I don't have anything particular in mind, I don't even know if it's possible or efficient. Just brainstorming.

@mberaha
Copy link
Contributor

mberaha commented Mar 14, 2021

I don't see how this could work... An object is a sequence of bytes, the programming language knows how to interpret those bytes.

@brunoguindani brunoguindani added the help wanted Extra attention is needed label Apr 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants