77 lines
2.3 KiB
C++
77 lines
2.3 KiB
C++
#include <algorithm>
|
|
#include <cmath>
|
|
#include <iostream>
|
|
#include <iterator>
|
|
|
|
#include <pfor/pfor.h>
|
|
#include <pfor/traits.h>
|
|
|
|
std::size_t pfor::ParallelForParameters::nThreads = 16;
|
|
|
|
constexpr long n = 16;
|
|
|
|
template<std::size_t v>
|
|
constexpr auto _ = pfor::ctv<v>;
|
|
|
|
#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;
|
|
}
|
|
|
|
int main() {
|
|
using TypeA = std::array<int, n+1>; OPERAND(TypeA, a);
|
|
using TypeB = std::array<int, 2*n+1>; OPERAND(TypeB, b);
|
|
using Type = std::array<int, n>; OPERAND(Type, c); OPERAND(Type, d);
|
|
|
|
std::generate_n(std::begin(a_), a_.size(), [i=0]() mutable { return i++; });
|
|
std::generate_n(std::begin(b_), b_.size(), [i=0]() mutable { return n-i++; });
|
|
std::generate_n(std::begin(c_), c_.size(), [i=0]() mutable { return 1+i++%3; });
|
|
std::generate_n(std::begin(d_), d_.size(), [i=0]() mutable { return -i++; });
|
|
|
|
pfor::Index i;
|
|
|
|
{
|
|
auto e = a[_<2>*i+_<1>] = a[_<2>*i];
|
|
std::cout << "Is {a[2*i+1] = a[2*i]} parallelizable? " << std::boolalpha << parallelTest(pfor::Range{0,0}, e) << std::endl;
|
|
}
|
|
|
|
{
|
|
auto e = a[i*i] = a[i*i]+1;
|
|
std::cout << "Is {a[i*i] = a[i*i]+1} parallelizable? " << std::boolalpha << parallelTest(pfor::Range{0,0}, e) << std::endl;
|
|
}
|
|
|
|
{
|
|
auto e = a[injective(i*i)] = a[injective(i*i)]+1;
|
|
std::cout << "Is {a[injective(i*i)] = a[injective(i*i)]+1} parallelizable? " << std::boolalpha << parallelTest(pfor::Range{0,0}, e) << std::endl;
|
|
}
|
|
|
|
{
|
|
auto e = a[i+_<1>] = a[i];
|
|
std::cout << "Is {a[i+1] = a[i]}(step=1) parallelizable? " << std::boolalpha << parallelTest(pfor::RangeCT<0,1>{0}, e) << std::endl;
|
|
std::cout << "Is {a[i+1] = a[i]}(step=2) parallelizable? " << std::boolalpha << parallelTest(pfor::RangeCT<0,2>{0}, e) << std::endl;
|
|
}
|
|
|
|
pfor::parallelFor<pfor::ForLoopThread>(pfor::Range{0, n},
|
|
d[i] = (xpr(i)+1)*a[i+_<1>],
|
|
b[_<2>*i+_<1>] = b[_<2>*i]
|
|
);
|
|
|
|
pfor::parallelFor<pfor::ForLoopThread>(pfor::Range{0, static_cast<pfor::Range::ValueType>(std::sqrt(n))},
|
|
c[injective(i*i)] = c[injective(i*i)] + i
|
|
);
|
|
|
|
pfor::parallelFor<pfor::ForLoopThread>(pfor::RangeCT<0,2>{n},
|
|
a[i+_<1>] = a[i]
|
|
);
|
|
|
|
printArray(a_);
|
|
printArray(b_);
|
|
printArray(c_);
|
|
printArray(d_);
|
|
}
|