-
Notifications
You must be signed in to change notification settings - Fork 0
Non-const pointer arguments in write functions #1
Description
The purpose of the write member functions of the SolverInterface class is to transfer data to the other solver. Hence, there should be no need to modify the data inside a write function, which in turn means that it should be possible to declare all pointers/references to this data as const.
However, when implementing the MATLAB bindings, I realized that only some of preCICE's functions for writing data require const double* as parameter for the values, while other of them require double* as parameters.
When I dug a little into the code, I realized that there seems to be no reason for this. For example, the function writeBlockVectorData calls the corresponding function in impl/SolverInterfaceImpl.cpp defined here. This function in turn calls RequestManager::requestWriteBlockScalarData defined here, which again in turn calls the send function of com/Communication.hpp. However, this send function is overloaded as virtual void send(const double *itemsToSend, int size, int rankReceiver), so it takes a const pointer.
Thus, it seems that all the above functions could be refactored to take a const double* instead of a double* as a parameter. Or are there good reasons not to define them in that way? I, as of now, don't see any.
If the parameters remain non-const, this would be fatal for the MATLAB bindings, since MATLAB uses read on write: With the current precice code, pointers to the data in MATLAB can not be declared as const in the bindings (since const pointers can't be passed to the functions in precice). Therefore, MATLAB is unable to check that data passed to the binding function will remain unchanged. Consequently, MATLAB will always make an unnecessary copy of the data passed to the precice binding function to ensure that the original data is preserved. This wastes RAM and is highly inefficient for large data.