All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines
Classes | Signals | Public Member Functions | Protected Slots | Protected Member Functions
QGpCoreTools::ParallelLoop Class Reference

Class that helps to parallelize loops. More...

#include <ParallelLoop.h>

Inheritance diagram for QGpCoreTools::ParallelLoop:
FKLoop SPACLoop HRFKLoop LinearFKPassiveLoop

List of all members.

Classes

struct  TaskInfo

Signals

void finished ()
void progressChanged (int processIndex, int progress)
void progressInit (int processIndex, int maximumValue)
void statusChanged (int processIndex, QString msg)

Public Member Functions

 ParallelLoop (QObject *parent=0)
void start (int iStart, int iEnd, bool forceParallel=false)
void terminate ()
void waitFinished ()

Protected Slots

virtual void taskFinished ()

Protected Member Functions

virtual LoopTasknewTask ()=0

Detailed Description

Class that helps to parallelize loops.


Constructor & Destructor Documentation

QGpCoreTools::ParallelLoop::ParallelLoop ( QObject *  parent = 0) [inline]

Initialize a loop. The parameters param will be provided as a pointer to each task.

: QObject(parent) {}

Member Function Documentation

Referenced by taskFinished().

virtual LoopTask* QGpCoreTools::ParallelLoop::newTask ( ) [protected, pure virtual]

Implemented in SPACLoop, LinearFKPassiveLoop, FKLoop, and HRFKLoop.

Referenced by start().

void QGpCoreTools::ParallelLoop::progressChanged ( int  processIndex,
int  progress 
) [signal]
void QGpCoreTools::ParallelLoop::progressInit ( int  processIndex,
int  maximumValue 
) [signal]
void QGpCoreTools::ParallelLoop::start ( int  iStart,
int  iEnd,
bool  forceParallel = false 
)

Start loop from iStart to iEnd-1

References QGpCoreTools::Thread::idealThreadCount(), newTask(), QGpCoreTools::LoopTask::setIndex(), QGpCoreTools::Thread::start(), QGpCoreTools::Thread::startNoThread(), and TRACE.

Referenced by ToolLinearFKPassive::finishPositive(), ToolSPAC::startLoop(), and ToolFK::startLoop().

  {
    TRACE;
    int nThreads=Thread::idealThreadCount();
    _index=iStart;
    LoopTask * t;
    TaskInfo infos;
    if(nThreads==1 && !forceParallel) {
      t=newTask(iEnd);
      infos.index=0;
      infos.maximumProgress=0;
      _tasks.insert(t, infos);
      t->startNoThread();
    } else {
      for(int i=0; i<nThreads; i++) {
        t=newTask(iEnd);
        t->setObjectName("parallelLoop");
        infos.index=i;
        infos.maximumProgress=0;
        _tasks.insert(t, infos);
        t->setIndex(&_index, &_indexMutex);
        t->start();
      }
    }
  }
void QGpCoreTools::ParallelLoop::statusChanged ( int  processIndex,
QString  msg 
) [signal]
void QGpCoreTools::ParallelLoop::taskFinished ( ) [protected, virtual, slot]

References finished(), QGpCoreTools::tr(), and TRACE.

  {
    TRACE;
    LoopTask * t=static_cast<LoopTask *>(sender());
    QMap<LoopTask *,TaskInfo>::iterator it=_tasks.find(t);
    if(it!=_tasks.end()) {
      emit statusChanged(it.value().index, tr("Finished"));
      emit progressChanged(it.value().index, it.value().maximumProgress);
      _tasks.erase(it);
      delete t;
      if(_tasks.isEmpty()) emit finished();
    }
  }

References TRACE.

Referenced by ToolFK::stop().

  {
    TRACE;
    for(QMap<LoopTask *,TaskInfo>::iterator it=_tasks.begin(); it!=_tasks.end(); it++) {
      if(!it.key()->terminated()) it.key()->terminate();
    }
  }

References QGpCoreTools::App::sleep().

Referenced by ToolFK::waitFinished(), and ToolSPAC::waitFinished().

  {
    while(!_tasks.isEmpty()) {
      App::sleep(500);
      QCoreApplication::processEvents();
    }
  }

The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines