76 lines
2.0 KiB
C++
76 lines
2.0 KiB
C++
#include <catch.hpp>
|
|
#include "common.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <pfor/pfor.h>
|
|
|
|
#define N 100
|
|
|
|
TEST_CASE("parallelFor") {
|
|
int a[N+1], b[N], c[N], d[N], e[N], f[N];
|
|
|
|
std::generate_n(std::begin(a), N+1, [it=0]() mutable { return N-(it++); });
|
|
std::generate_n(std::begin(b), N, [it=0]() mutable { return it++; });
|
|
std::generate_n(std::begin(c), N, []{ return 250; });
|
|
std::generate_n(std::begin(d), N, [it=0]() mutable { return it++&1; });
|
|
std::generate_n(std::begin(e), N, [it=0]() mutable { return it++<<1; });
|
|
std::generate_n(std::begin(f), N, []{ return 0; });
|
|
|
|
SECTION("multiple instructions") {
|
|
int ca[N+1], cb[N], cc[N], cd[N], ce[N], cf[N];
|
|
|
|
pfor::Operand<int*, class IDa> oa{a};
|
|
pfor::Operand<int*, class IDb> ob{b};
|
|
pfor::Operand<int*, class IDc> oc{c};
|
|
pfor::Operand<int*, class IDd> od{d};
|
|
pfor::Operand<int*, class IDe> oe{e};
|
|
pfor::Operand<int*, class IDf> of{f};
|
|
|
|
std::copy(std::begin(a), std::end(a), std::begin(ca));
|
|
std::copy(std::begin(b), std::end(b), std::begin(cb));
|
|
std::copy(std::begin(c), std::end(c), std::begin(cc));
|
|
std::copy(std::begin(d), std::end(d), std::begin(cd));
|
|
std::copy(std::begin(e), std::end(e), std::begin(ce));
|
|
std::copy(std::begin(f), std::end(f), std::begin(cf));
|
|
|
|
SECTION("OpenMP") {
|
|
pfor::Index i;
|
|
pfor::parallelFor<pfor::ForLoopThread>(pfor::Range{0, N}, (
|
|
of = oc,
|
|
oa[i] = oa[i+_<1>],
|
|
ob[i] = ob[i] + 2*oa[i],
|
|
oe = oe+oe,
|
|
oc = od
|
|
));
|
|
|
|
for(std::size_t i = 0; i < N; ++i) {
|
|
REQUIRE(f[i] == cc[i]);
|
|
REQUIRE(a[i] == ca[i+1]);
|
|
REQUIRE(b[i] == (cb[i] + 2*ca[i+1]));
|
|
REQUIRE(e[i] == ce[i]+ce[i]);
|
|
REQUIRE(c[i] == cd[i]);
|
|
}
|
|
}
|
|
|
|
SECTION("Threads") {
|
|
pfor::Index i;
|
|
pfor::parallelFor<pfor::ForLoopThread>(pfor::Range{0, N}, (
|
|
of = oc,
|
|
oa[i] = oa[i+_<1>],
|
|
ob[i] = ob[i] + 2*oa[i],
|
|
oe = oe+oe,
|
|
oc = od
|
|
));
|
|
|
|
for(std::size_t i = 0; i < N; ++i) {
|
|
REQUIRE(f[i] == cc[i]);
|
|
REQUIRE(a[i] == ca[i+1]);
|
|
REQUIRE(b[i] == (cb[i] + 2*ca[i+1]));
|
|
REQUIRE(e[i] == ce[i]+ce[i]);
|
|
REQUIRE(c[i] == cd[i]);
|
|
}
|
|
}
|
|
}
|
|
}
|