Accelerating sequential code

Text

Accelerate the MM multiplication code using a FastFlow farm accelerator.

Hints

You should look for computations (tasks) that may be executed in parallel within the sequential code. Then

  1. you should add an accelerator (e.g. a farm with the true parameter passed to the constructor) computing taska from an input stream
  2. you should send the tasks to be computed to the accelerator, using the offload method
  3. after submitting the last task an FF_EOS should be offloaded to accelerator in order to make the accelerator terminate
  4. you should get back the results using the load_result method
  5. the farm must be run with a run_then_freeze call
  6. eventually you shoud wait for the accelerator termination (wait method)

APIs

Declare accelerator using a true parameter as the constructors parameter

farm<> myFarm(true); 

and eventually start it with the call

myFarm.run_then_freeze()

To offload tasks to the accelerator use a

myFarm.offload(void * task)

method.

Aftern having offloaded all the tasks, offload an EOS

myFarm.offload((void *) FF_EOS); 

To restrieve results from the accelerator use a

myFarm.load_result(void **)

Eventually, to await for accelerator termination, use a

myFarm.wait()

Accelerator should in principle use the spare cores of your machine. The total number of cores may be read (on Linux) as follows:

int cores =  sysconf( _SC_NPROCESSORS_ONLN );

therefore in our case, being one core used by the mm main code, you shoul assume the accelerator may use up to

sysconf( _SC_NPROCESSORS_ONLN ) -1 

cores.

Sample solution

Code here.

Lesson goal

Appreciate the possiblities offered to accelerate existing code, with minimal programming effort.