openPMD / openPMD-api

:floppy_disk: C++ & Python API for Scientific I/O
https://openpmd-api.readthedocs.io
GNU Lesser General Public License v3.0
138 stars 51 forks source link

Flush required for temporary vector attributes? #1603

Closed pgrete closed 6 months ago

pgrete commented 6 months ago

I'm trying to store some data (of multiple objects) similar to

for (obj : objects) {
  std::vector<double> tmp = GetStdVector(obj);
  iteration.setAttribute(obj.label(), tmp);
}

Is this safe (i.e., is the content of the temporary vector persistent in the openpmd-api internals) or do I need to make sure that the memory still lives until I call flush?

Software Environment: Have you already installed openPMD-api? If so, please tell us which version of openPMD-api your question is about:

franzpoeschel commented 6 months ago

Is this safe (i.e., is the content of the temporary vector persistent in the openpmd-api internals) or do I need to make sure that the memory still lives until I call flush?

Yes, this is safe. The operation will generally be passed on to the I/O backend later upon flush()ing only, but attributes don't keep handles on user data, they are stored internally.

Note also that setAttribute uses pass-by-value, so the function header alone gives us no chance to access the user vector:

template <typename T>
inline bool Attributable::setAttribute(std::string const &key, T value)
pgrete commented 6 months ago

Perfect, thanks for confirming.