Storing internal function data between f-n calls

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!

Can static variables cause issues when used with MPI?
Well, yes. Shared state is a no-no in anything parallel.

It sounds like this function is structured like this:
1
2
3
4
T function(/*some parameters*/ /*more parameters*/){
    //Do something.
    //Do something else.
}
and that it could be restructured like this:
1
2
3
4
5
6
7
8
9
10
11
T2 do_something(/*some parameters*/){
    //Do something.
}

T do_something_else(T2 intermediate, /*more parameters*/){
    //Do something else.
}

T function(/*some parameters*/ /*more parameters*/){
    return do_something(do_something_else(/*some parameters*/) /*more parameters*/);
}
From there, you can easily move to
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Functioner{
    void do_something(/*some parameters*/){
        //Do something.
        //Save results.
    }
    T do_something_else(T2 intermediate, /*more parameters*/){
        //Take saved results.
        //Do something else.
    }
public:
    T function(/*some parameters*/ /*more parameters*/){
        if (!this->input_is_computed(/*some parameters*/))
            this->do_something(/*some parameters*/);
        return this->do_something_else(/*more parameters*/);
    }
};
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.
dhayden,
can you give an example how to actually store and access computed data without static variables?
Last edited on
There is no one true answer -- without at least knowing exactly what you are trying to do with this.

The idea you are circling is called 'memoization' and it is implemented by closures -- or, as we call them in OOP stuff -- objects.
can you give an example how to actually store and access computed data without static variables

No. :). Sorry I wasn't clear in my response. I meant that that you can use previously computed data inside the function using static variables.
Topic archived. No new replies allowed.