Skip to content

Commit

Permalink
Merge pull request #179 from intel/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
chuckyount authored Nov 14, 2018
2 parents e6476ed + 281eda7 commit ccd7d56
Show file tree
Hide file tree
Showing 20 changed files with 2,325 additions and 1,911 deletions.
2 changes: 1 addition & 1 deletion src/common/common_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace yask {
// for numbers above 9 (at least up to 99).

// Format: "major.minor.patch".
const string version = "2.15.09";
const string version = "2.15.10";

string yask_get_version_string() {
return version;
Expand Down
106 changes: 0 additions & 106 deletions src/common/tuple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,73 +246,6 @@ namespace yask {
return newt;
}

template <typename T>
T Tuple<T>::reduce(std::function<T (T lhs, T rhs)> reducer) const {
T result = 0;
int n = 0;
for (auto i : _q) {
//auto& tdim = i.getName();
auto& tval = i.getVal();
if (n == 0)
result = tval;
else
result = reducer(result, tval);
n++;
}
return result;
}

template <typename T>
Tuple<T> Tuple<T>::combineElements(std::function<T (T lhs, T rhs)> combiner,
const Tuple& rhs,
bool strictRhs) const {
Tuple newt = *this;
if (strictRhs) {
assert(areDimsSame(rhs, true));
for (size_t i = 0; i < _q.size(); i++) {
auto& tval = _q[i].getVal();
auto& rval = rhs[i];
T newv = combiner(tval, rval);
newt[i] = newv;
}
}
else {
for (auto& i : _q) {
auto& tdim = i.getName();
auto& tval = i.getVal();
auto* rp = rhs.lookup(tdim);
if (rp) {
T newv = combiner(tval, *rp);
newt.setVal(tdim, newv);
}
}
}
return newt;
}

template <typename T>
Tuple<T> Tuple<T>::mapElements(std::function<T (T lhs, T rhs)> func,
T rhs) const {
Tuple newt = *this;
for (size_t i = 0; i < _q.size(); i++) {
auto& tval = _q[i].getVal();
T newv = func(tval, rhs);
newt[i] = newv;
}
return newt;
}

template <typename T>
Tuple<T> Tuple<T>::mapElements(std::function<T (T in)> func) const {
Tuple newt = *this;
for (size_t i = 0; i < _q.size(); i++) {
auto& tval = _q[i].getVal();
T newv = func(tval);
newt[i] = newv;
}
return newt;
}

template <typename T>
std::string Tuple<T>::makeValStr(std::string separator,
std::string prefix,
Expand Down Expand Up @@ -384,45 +317,6 @@ namespace yask {
return oss.str();
}

template <typename T>
void Tuple<T>::visitAllPoints(std::function<bool (const Tuple&,
size_t idx)> visitor) const {

// Init lambda fn arg with *this to get dim names.
// Values will get set during scan.
Tuple tp(*this);

// 0-D?
if (!_q.size())
visitor(tp, 0);

// Call recursive version.
// Set begin/step dims depending on nesting.
else if (_firstInner)
_visitAllPoints(visitor, size()-1, -1, tp);
else
_visitAllPoints(visitor, 0, 1, tp);
}

template <typename T>
void Tuple<T>::visitAllPointsInParallel(std::function<bool (const Tuple&,
size_t idx)> visitor) const {

// 0-D?
if (!_q.size()) {
Tuple tp(*this);
visitor(tp, 0);
}

// Call order-independent version.
// Set begin/end/step dims depending on nesting.
// TODO: set this depending on dim sizes.
else if (_firstInner)
_visitAllPointsInPar(visitor, size()-1, -1);
else
_visitAllPointsInPar(visitor, 0, 1);
}

// Explicitly allowed instantiations.
template class Tuple<int>;
template class Tuple<idx_t>;
Expand Down
94 changes: 87 additions & 7 deletions src/common/tuple.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,17 @@ namespace yask {
Tuple removeDim(int posn) const;

// reductions.
T reduce(std::function<T (T lhs, T rhs)> reducer) const;
// Apply function over all elements, returning one value.
T reduce(std::function<T (T lhs, T rhs)> reducer) const {
T result = 0;
int n = 0;
for (auto i : _q) {
auto& tval = i.getVal();
result = (n == 0) ? tval : reducer(result, tval);
n++;
}
return result;
}
T sum() const {
return reduce([&](T lhs, T rhs){ return lhs + rhs; });
}
Expand All @@ -454,8 +464,31 @@ namespace yask {
// if strictRhs==true, RHS elements must be same as this;
// else, only matching ones are considered.
Tuple combineElements(std::function<T (T lhs, T rhs)> combiner,
const Tuple& rhs,
bool strictRhs=true) const;
const Tuple& rhs,
bool strictRhs=true) const {
Tuple newt = *this;
if (strictRhs) {
assert(areDimsSame(rhs, true));
for (size_t i = 0; i < _q.size(); i++) {
auto& tval = _q[i].getVal();
auto& rval = rhs[i];
T newv = combiner(tval, rval);
newt[i] = newv;
}
}
else {
for (auto& i : _q) {
auto& tdim = i.getName();
auto& tval = i.getVal();
auto* rp = rhs.lookup(tdim);
if (rp) {
T newv = combiner(tval, *rp);
newt.setVal(tdim, newv);
}
}
}
return newt;
}
Tuple addElements(const Tuple& rhs, bool strictRhs=true) const {
return combineElements([&](T lhs, T rhs){ return lhs + rhs; },
rhs, strictRhs);
Expand All @@ -479,8 +512,24 @@ namespace yask {

// Apply func to each element, creating a new Tuple.
Tuple mapElements(std::function<T (T lhs, T rhs)> func,
T rhs) const;
Tuple mapElements(std::function<T (T in)> func) const;
T rhs) const {
Tuple newt = *this;
for (size_t i = 0; i < _q.size(); i++) {
auto& tval = _q[i].getVal();
T newv = func(tval, rhs);
newt[i] = newv;
}
return newt;
}
Tuple mapElements(std::function<T (T in)> func) const {
Tuple newt = *this;
for (size_t i = 0; i < _q.size(); i++) {
auto& tval = _q[i].getVal();
T newv = func(tval);
newt[i] = newv;
}
return newt;
}
Tuple addElements(T rhs) const {
return mapElements([&](T lhs, T rhs){ return lhs + rhs; },
rhs);
Expand Down Expand Up @@ -533,14 +582,45 @@ namespace yask {
// through first dimension. If '_firstInner' is false, it is done the opposite way.
// Visitor should return 'true' to keep going or 'false' to stop.
void visitAllPoints(std::function<bool (const Tuple&,
size_t idx)> visitor) const;
size_t idx)> visitor) const {

// Init lambda fn arg with *this to get dim names.
// Values will get set during scan.
Tuple tp(*this);

// 0-D?
if (!_q.size())
visitor(tp, 0);

// Call recursive version.
// Set begin/step dims depending on nesting.
else if (_firstInner)
_visitAllPoints(visitor, size()-1, -1, tp);
else
_visitAllPoints(visitor, 0, 1, tp);
}

// Call the 'visitor' lambda function at every point in the space defined by 'this'.
// 'idx' parameter contains sequentially-numbered index.
// Visitation order is not predictable.
// Visitor return value only stops visit on one thread.
void visitAllPointsInParallel(std::function<bool (const Tuple&,
size_t idx)> visitor) const;
size_t idx)> visitor) const {

// 0-D?
if (!_q.size()) {
Tuple tp(*this);
visitor(tp, 0);
}

// Call order-independent version.
// Set begin/end/step dims depending on nesting.
// TODO: set this depending on dim sizes.
else if (_firstInner)
_visitAllPointsInPar(visitor, size()-1, -1);
else
_visitAllPointsInPar(visitor, 0, 1);
}

protected:

Expand Down
10 changes: 9 additions & 1 deletion src/compiler/lib/YaskKernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ namespace yask {

// Domain condition.
{
os << endl << " // Determine whether " << egsName << " is valid at the domain indices " <<
os << "\n // Determine whether " << egsName << " is valid at the domain indices " <<
_dims._stencilDims.makeDimStr() << ".\n"
" // Return true if indices are within the valid sub-domain or false otherwise.\n"
" virtual bool is_in_valid_domain(const Indices& idxs) const final {\n";
Expand All @@ -561,6 +561,14 @@ namespace yask {
" virtual bool is_sub_domain_expr() const {\n"
" return " << (eq->cond ? "true" : "false") <<
";\n }\n";

os << "\n // Return human-readable description of sub-domain.\n"
" virtual std::string get_domain_description() const {\n";
if (eq->cond)
os << " return \"" << eq->cond->makeStr() << "\";\n";
else
os << " return \"true\"; // full domain.\n";
os << " }\n";
}

// Step condition.
Expand Down
26 changes: 12 additions & 14 deletions src/compiler/tests/yask_compiler_api_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ int main() {
auto stdos = ofac.new_stdout_output();
soln->set_debug_output(stdos);

// Number of bytes in each FP value.
soln->set_element_bytes(4);

// Define the problem dimensions.
auto t = fac.new_step_index("t");
auto x = fac.new_domain_index("x");
Expand All @@ -54,11 +57,8 @@ int main() {
// Create a grid var.
auto g1 = soln->new_grid("test_grid", {t, x, y, z});

// Create a scratch-grid var.
auto sg1 = soln->new_scratch_grid("scratch_grid", {x, y, z});

// Create an equation for the scratch-grid.
auto n1 = fac.new_const_number_node(3.14);
// Create an equation based on some values from 'g1'.
auto n1 = g1->new_grid_point({t, x, y, z});
cout << n1->format_simple() << endl;

auto n2 = g1->new_grid_point({t, x+1, y, z-2});
Expand All @@ -70,20 +70,21 @@ int main() {
auto n4 = n2 * -n3 * 0.9;
cout << n4->format_simple() << endl;

auto n5 = g1->new_grid_point({t, x+1, y-1, z});
auto n5 = g1->new_grid_point({t, x+2, y-1, z});
cout << n5->format_simple() << endl;

auto n6 = n4 / n5;
cout << n6->format_simple() << endl;

// Define scratch grid value.
auto ns_lhs = sg1->new_grid_point({x, y, z});
cout << ns_lhs->format_simple() << endl;
// Create a scratch-grid var.
auto sg1 = soln->new_scratch_grid("scratch_grid", {x, y, z});

// Define scratch-grid value based on expression 'n6' above.
auto ns_lhs = sg1->new_grid_point({x, y, z});
auto ns_eq = fac.new_equation_node(ns_lhs, n6);
cout << ns_eq->format_simple() << endl;

// Use scratch grid value.
// Define another equation based on some values from 'sg1'.
auto n7a = sg1->new_grid_point({x-1, y, z+2});
auto n7b = sg1->new_grid_point({x+1, y-1, z-2});
auto n8 = n7a + n7b;
Expand All @@ -97,7 +98,7 @@ int main() {
auto sd0 = (x >= fac.new_first_domain_index(x) + 5);

// Set equations to update the main grid using
// expression n8 in sub-domain sd0 and -n8 otherwise.
// expression 'n8' in sub-domain 'sd0' and -'n8' otherwise.
auto n_eq0 = fac.new_equation_node(n_lhs, n8, sd0);
cout << n_eq0->format_simple() << endl;
auto n_eq1 = fac.new_equation_node(n_lhs, -n8, !sd0);
Expand All @@ -107,9 +108,6 @@ int main() {
soln->get_num_grids() << " grid(s), and " <<
soln->get_num_equations() << " equation(s)." << endl;

// Number of bytes in each FP value.
soln->set_element_bytes(4);

// Generate DOT output.
auto dot_file = ofac.new_file_output("yc-api-test-cxx.dot");
soln->format("dot", dot_file);
Expand Down
Loading

0 comments on commit ccd7d56

Please sign in to comment.