57 lines
1.5 KiB
C++
57 lines
1.5 KiB
C++
#include <algorithm>
|
|
#include <cmath>
|
|
#include <iostream>
|
|
#include <iterator>
|
|
|
|
#include <pfor/pfor.h>
|
|
|
|
constexpr long n = 10;
|
|
|
|
template<std::size_t n>
|
|
constexpr auto _ = pfor::ctv<n>;
|
|
|
|
#define OPERAND(T, ID) \
|
|
T ID##_; \
|
|
auto ID = pfor::Operand<T, class TID##ID>{ID##_}
|
|
|
|
template<typename T, std::size_t n>
|
|
void printArray(std::array<T, n> const& array) {
|
|
std::copy(std::begin(array), std::end(array), std::ostream_iterator<T>(std::cout, " "));
|
|
std::cout << std::endl;
|
|
}
|
|
|
|
inline static long long factorial(long long n) {
|
|
long long r = 1;
|
|
while(n > 1) r *= n--;
|
|
return r;
|
|
}
|
|
|
|
std::size_t pfor::ParallelForParameters::nThreads = 4;
|
|
|
|
int main() {
|
|
using Type = std::array<int, n>;
|
|
OPERAND(Type, a); OPERAND(Type, b); OPERAND(Type, c); OPERAND(Type, d);
|
|
|
|
std::generate_n(std::begin(a_), n, [i=0]() mutable { return i++; });
|
|
std::generate_n(std::begin(b_), n, [i=0]() mutable { return n-i++; });
|
|
std::generate_n(std::begin(c_), n, [i=0]() mutable { return i++%3; });
|
|
std::generate_n(std::begin(d_), n, [i=0]() mutable { return -i++; });
|
|
|
|
pfor::Index i;
|
|
|
|
auto add3 = pfor::makeOperator([](auto a, auto b, auto c) { return a+b+c; });
|
|
auto pow = pfor::makeOperator<float(&)(float, float)>(std::pow);
|
|
auto factorial = pfor::makeOperator(::factorial);
|
|
|
|
pfor::parallelFor<pfor::ForLoopThread>(pfor::Range{0, n},
|
|
a[i] = add3(a[i], c[i], c[i]),
|
|
b[i] = pow(b[i], c[i]),
|
|
d[i] = pow(factorial(-d[i]/2), xpr(i) % 4 + 1)
|
|
);
|
|
|
|
printArray(a_);
|
|
printArray(b_);
|
|
printArray(c_);
|
|
printArray(d_);
|
|
}
|