Parallel calculator objects
Calculator |
Normally all objects in Vimms have an associated mutex, which prevents
each object to be executed at the same time by multiple threads. For example, when running Vimms on a
multiprocessor machine it is often useful to execute the same code on multiple processors. Without parallel
objects it would be necessary to make multiple copies of the same calculator object, corresponding to the
number of desired threads. As this makes a program unnecessary complex and uneasy to handle,
the code in the calculator object may unlock the object so that it is possible for another thread to execute the
same calculator object in parallel.
When doing that, some precautions must be taken:
- Input/Output variables must not be used during the time the object is unlocked as all threads
share input and output variables. All necessary data
must be copied to local variables before the object is unlocked and all results must be copied to the
output variables after the object has been locked again.
- Any user defined global variables are shared by all threads that execute the object in parallel.
- An object that has been unlocked must be locked again before the code in the calculator object exits.
For locking and unlocking each calculator object has a variable object_control of type
Object_Control. This class contains the following function definition (see vflcalc.h):
void* Object_Control::unlock();
void Object_Control::lock(void* lockd);
The first function unlocks the object and returns a void pointer which must be stored in a local variable.
This function unlocks the associated mutex and another thread may execute the same code. For locking the
object again the void pointer the unlock function returned must be passed as parameter to lock(). After
locking, input and output variables may be accessed again.
The following example demonstrates the usage:
#include <sys/times.h>
extern "C" bool run() {
tms st,et;
int iter = inputiter; //integer input variable
void* lockd = object_control.unlock(); //unlock the object
times(&st);
volatile int k = 0;
for (int i=0;i<10000;i++) for (int j=0;j<iter;j++) k++;
times(&et);
object_control.lock(lockd); //lock the object again
int delta = et.tms_utime - st.tms_utime;
outtime = delta; //integer output variable
return true; //continues execution
}
The example stores the value of the input variable inputiter into the local variable iter and
than unlocks the object. Then it does some iterations and measures the time needed for the loops.
After that the object is locked again and the elapsed time is stored into the output variable outtime.
Back
The Vimms User Manual