2018-04-19 07:04:24 -05:00
|
|
|
// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
// vi: set et ts=4 sw=4 sts=4:
|
|
|
|
/*
|
|
|
|
This file is part of the Open Porous Media project (OPM).
|
|
|
|
|
|
|
|
OPM is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
OPM is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with OPM. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
Consult the COPYING file in the top-level source directory of this
|
|
|
|
module for the precise wording of the license and the list of
|
|
|
|
copyright holders.
|
|
|
|
*/
|
|
|
|
/*!
|
|
|
|
* \file
|
|
|
|
*
|
|
|
|
* \brief This file serves as an example of how to use the tasklet mechanism for
|
|
|
|
* asynchronous work.
|
|
|
|
*/
|
|
|
|
#include "config.h"
|
|
|
|
|
2019-09-16 02:48:55 -05:00
|
|
|
#include <opm/models/parallel/tasklets.hh>
|
2018-04-19 07:04:24 -05:00
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
#include <iostream>
|
|
|
|
|
2018-04-23 06:16:40 -05:00
|
|
|
std::mutex outputMutex;
|
|
|
|
|
2019-09-05 09:21:10 -05:00
|
|
|
Opm::TaskletRunner *runner;
|
2018-04-23 06:16:35 -05:00
|
|
|
|
2019-09-05 09:21:10 -05:00
|
|
|
class SleepTasklet : public Opm::TaskletInterface
|
2018-04-19 07:04:24 -05:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
SleepTasklet(int mseconds)
|
|
|
|
: mseconds_(mseconds)
|
|
|
|
{
|
|
|
|
n_ = numInstantiated_;
|
|
|
|
++ numInstantiated_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void run()
|
|
|
|
{
|
2018-04-23 06:16:35 -05:00
|
|
|
assert(0 <= runner->workerThreadIndex() && runner->workerThreadIndex() < runner->numWorkerThreads());
|
2018-04-19 07:04:24 -05:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(mseconds_));
|
2018-04-23 06:16:40 -05:00
|
|
|
outputMutex.lock();
|
2018-04-23 06:16:35 -05:00
|
|
|
std::cout << "Sleep tasklet " << n_ << " of " << mseconds_ << " ms completed by worker thread " << runner->workerThreadIndex() << std::endl;
|
2018-04-23 06:16:40 -05:00
|
|
|
outputMutex.unlock();
|
2018-04-19 07:04:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static int numInstantiated_;
|
|
|
|
int n_;
|
|
|
|
int mseconds_;
|
|
|
|
};
|
|
|
|
|
2018-07-02 02:16:26 -05:00
|
|
|
void sleepAndPrintFunction();
|
2018-06-21 07:56:10 -05:00
|
|
|
void sleepAndPrintFunction()
|
|
|
|
{
|
|
|
|
int ms = 100;
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
|
|
|
|
outputMutex.lock();
|
|
|
|
std::cout << "Sleep completed by worker thread " << runner->workerThreadIndex() << std::endl;
|
|
|
|
outputMutex.unlock();
|
|
|
|
}
|
|
|
|
|
2018-04-19 07:04:24 -05:00
|
|
|
int SleepTasklet::numInstantiated_ = 0;
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2018-04-23 06:16:35 -05:00
|
|
|
int numWorkers = 2;
|
2019-09-05 09:21:10 -05:00
|
|
|
runner = new Opm::TaskletRunner(numWorkers);
|
2018-04-23 06:16:35 -05:00
|
|
|
|
|
|
|
// the master thread is not a worker thread
|
|
|
|
assert(runner->workerThreadIndex() < 0);
|
|
|
|
assert(runner->numWorkerThreads() == numWorkers);
|
2018-04-19 07:04:24 -05:00
|
|
|
|
|
|
|
for (int i = 0; i < 5; ++ i) {
|
|
|
|
//auto st = std::make_shared<SleepTasklet>((i + 1)*1000);
|
|
|
|
auto st = std::make_shared<SleepTasklet>(100);
|
2018-04-23 06:16:35 -05:00
|
|
|
runner->dispatch(st);
|
2018-04-19 07:04:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << "before barrier" << std::endl;
|
2018-04-23 06:16:35 -05:00
|
|
|
runner->barrier();
|
2018-04-19 07:04:24 -05:00
|
|
|
std::cout << "after barrier" << std::endl;
|
|
|
|
|
2018-06-21 07:56:10 -05:00
|
|
|
runner->dispatchFunction(sleepAndPrintFunction);
|
|
|
|
runner->dispatchFunction(sleepAndPrintFunction, /*numInvokations=*/6);
|
2018-04-19 07:04:24 -05:00
|
|
|
|
2018-04-23 06:16:35 -05:00
|
|
|
delete runner;
|
|
|
|
|
2018-04-19 07:04:24 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|