75 lines
1.7 KiB
C++
75 lines
1.7 KiB
C++
#include <algorithm>
|
|
#include <iostream>
|
|
#include <iterator>
|
|
|
|
#include <pfor/pfor.h>
|
|
|
|
#define N 10
|
|
#define DELAY 10'000'000
|
|
|
|
#define WAIT(DELAY) \
|
|
for(std::size_t i = 0; i < DELAY; ++i)
|
|
|
|
template<std::size_t n>
|
|
constexpr auto _ = pfor::ctv<n>;
|
|
|
|
std::size_t pfor::ParallelForParameters::nThreads = 4;
|
|
|
|
/* slow operator+ */
|
|
template<typename T = int>
|
|
class Foo {
|
|
T data;
|
|
|
|
public:
|
|
Foo() = default;
|
|
Foo(T const&data): data{data} {}
|
|
|
|
Foo &operator=(T const&data) {
|
|
WAIT(DELAY);
|
|
this->data = data;
|
|
return *this;
|
|
}
|
|
|
|
Foo operator+(Foo const&o) {
|
|
WAIT(DELAY);
|
|
return Foo{data+o.data};
|
|
}
|
|
|
|
operator T() const { return data; }
|
|
};
|
|
|
|
int main() {
|
|
Foo<> a[N+1], b[N], c[N], d[N];
|
|
int t[N];
|
|
|
|
{ int i = 0; std::generate_n(std::begin(a), N+1, [&i]{ return N-(i++); }); }
|
|
{ int i = 0; std::generate_n(std::begin(b), N, [&i]{ return i++; }); }
|
|
{ std::generate_n(std::begin(c), N, []{ return 250; }); }
|
|
{ int i = 0; std::generate_n(std::begin(d), N, [&i]{ return i++&1; }); }
|
|
|
|
pfor::parallelFor<pfor::ForLoopOMP>(pfor::Range{0, N-1},
|
|
[
|
|
a=pfor::Operand<Foo<>*, class IDa>(a),
|
|
b=pfor::Operand<Foo<>*, class IDb>(b),
|
|
c=pfor::Operand<Foo<>*, class IDc>(c),
|
|
d=pfor::Operand<Foo<>*, class IDd>(d),
|
|
t=pfor::expr::Constant<int*>(t)
|
|
] (auto i) mutable {
|
|
return (
|
|
a[i] = b[i]+a[i+_<1>],
|
|
c[i] = c[i]+c[i],
|
|
b[i] = 3
|
|
);
|
|
}
|
|
);
|
|
|
|
std::copy(a, a+N, std::ostream_iterator<int>(std::cout, " "));
|
|
std::cout << std::endl;
|
|
std::copy(b, b+N, std::ostream_iterator<int>(std::cout, " "));
|
|
std::cout << std::endl;
|
|
std::copy(c, c+N, std::ostream_iterator<int>(std::cout, " "));
|
|
std::cout << std::endl;
|
|
std::copy(d, d+N, std::ostream_iterator<int>(std::cout, " "));
|
|
std::cout << std::endl;
|
|
}
|