mirror of
https://github.com/ultimatepp/ultimatepp.git
synced 2026-08-25 14:22:54 -06:00
CoWorkerResources
git-svn-id: svn://ultimatepp.org/upp/trunk@10525 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
parent
7d261db7bc
commit
52d5b0be15
2 changed files with 43 additions and 0 deletions
34
reference/CoWorkerResources/CoWorkerResources.cpp
Normal file
34
reference/CoWorkerResources/CoWorkerResources.cpp
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
#include <Core/Core.h>
|
||||
|
||||
using namespace Upp;
|
||||
|
||||
struct Model { // some computation model that is not stateless, abstract definition
|
||||
virtual void Start(int i) = 0;
|
||||
virtual int Compute() = 0;
|
||||
};
|
||||
|
||||
struct Model1 : Model { // concrete model (there would be more)
|
||||
int n;
|
||||
|
||||
virtual void Start(int i) { n = i; }
|
||||
virtual int Compute() { return n++; }
|
||||
};
|
||||
|
||||
CONSOLE_APP_MAIN
|
||||
{
|
||||
Vector<int> data;
|
||||
data.SetCount(3000);
|
||||
|
||||
CoWorkerResources<One<Model>> res;
|
||||
for(auto& r : res)
|
||||
r.Create<Model1>();
|
||||
|
||||
CoPartition(0, data.GetCount(), [&data, &res](int l, int h) {
|
||||
Model& m = *~res; // gets resource unique for worker
|
||||
m.Start(l);
|
||||
for(int i = l; i < h; i++)
|
||||
data[i] = m.Compute();
|
||||
});
|
||||
|
||||
DUMP(data);
|
||||
}
|
||||
9
reference/CoWorkerResources/CoWorkerResources.upp
Normal file
9
reference/CoWorkerResources/CoWorkerResources.upp
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
uses
|
||||
Core;
|
||||
|
||||
file
|
||||
CoWorkerResources.cpp;
|
||||
|
||||
mainconfig
|
||||
"" = "MT";
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue