Hello,
I have a function that is called thousands of times (with different parameters) and it could be optimized by using previously computed data inside this same function. There is no sense in writing the data to a file as this would be too slow.
I don't like to pass the irrelevant data outside of the function, cause its violates encapsulation principle.
Thus the only way I see is to introduce static variables inside the function for storing data between calls. The code is parallelized using MPI. Can static variables cause issues when used with MPI?
There is no MPI code inside the function, but only outside of it.
Or is there a better way?
Code is running under Linux, can use c++11 features.
The code too long to post, but I hope I explained the goal pretty well. Let me know if its not clear.
I am pretty new to C++, thus the more details/examples the better.
Thanks!
Yes, you can use previously computed data inside the function. Since this is a multi-threaded environment, be sure to use mutex locks where appropriate, or use thead-specific data if that makes sense.
Consider if you want to maintain all of the data or perhaps you just want to cache the most recently used data. If you keep all of the data and the function is called many thousands of times then the memory usage may be important.