YANE-Framework Tutorial 1.1.0

examples/parallelcomputing/src/parallelcomputingdemo.cpp

00001 /***************************************************************************
00002  *                                                                         *
00003  * Copyright (C) 2011 by www.nonlinearmpc.com                              *
00004  *                                                                         *
00005  * Authors:                                                                *
00006  *  Juergen Pannek <juergen.pannek@googlemail.com>                         *
00007  *  Michael Schamel <michael.schamel@uni-bayreuth.de>                      *
00008  *                                                                         *
00009  ***************************************************************************/
00010 
00011 #include <iostream>
00012 #include <cstdlib>
00013 #include <yane/utils.h>
00014 
00015 #include "simplecomputation.h"
00016 
00017 using namespace std;
00018 using namespace yane::Utils;
00019 
00020 int main ( int argc, char *argv [ ] )
00021 {
00022 
00023         // Create new clock
00024         RTClock *clock = new RTClock ( );
00025 
00026         // Debug to Console
00027         DebugMasterConsole *debugmaster = new DebugMasterConsole ( clock, 90 );
00028         DebugClient * debugclient = debugmaster->newClient ( "Main" );
00029 
00030         SimpleComputation * computer = new SimpleComputation ( "Parallel Demo",
00031                 debugmaster, 2 );
00032 
00033         SimpleComputation::T_COMPUTATIONINFO task1, task2, task3, task4;
00034 
00035         //create task info
00036         task1.sleep = 0.1;
00037         task2.sleep = 0.9;
00038         task3.sleep = 0.3;
00039         task4.sleep = 1.0;
00040 
00041         // add items and start computing
00042         computer->addItem ( & task1, task1.sleep );
00043         computer->addItem ( & task2, task2.sleep );
00044         computer->addItem ( & task3, task3.sleep );
00045         computer->addItem ( & task4, task4.sleep );
00046 
00047         clock->reset ( );
00048         computer->startComputing ( );
00049         computer->clearItems ( );
00050 
00051         // write result and finish
00052         stringstream out;
00053         out << "time finished: " << clock->elapsedSeconds ( )
00054                 << ", accumulated sleeptime: " << ( task1.sleep + task2.sleep
00055                 + task3.sleep + task4.sleep );
00056         debugclient->sendDebugInfo ( out.str ( ) );
00057 
00058         delete computer;
00059         delete clock;
00060         delete debugmaster;
00061         delete debugclient;
00062 
00063         return 0;
00064 }