Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed MRA example to use UxT reducer #2

Merged
merged 1 commit into from
Nov 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 10 additions & 32 deletions examples/madness/mrattg_streaming.cc
Original file line number Diff line number Diff line change
Expand Up @@ -413,16 +413,10 @@ void send_leaves_up(const Key<NDIM>& key,
}
}

template <typename T, size_t K, Dimension NDIM>
void reduce_leaves(const Key<NDIM>& key, const FunctionReconstructedNodeWrap<T,K,NDIM>& node, std::tuple<rnodeOut<T,K,NDIM>>& out) {
//std::cout << "Reduce_leaves " << node.key.childindex() << " " << node.neighbor_sum[node.key.childindex()] << std::endl;
std::get<0>(out).send(key, node);
}

// With data streaming up the tree run compression
template <typename T, size_t K, Dimension NDIM>
void do_compress(const Key<NDIM>& key,
const FunctionReconstructedNodeWrap<T,K,NDIM> &in,
const std::array<FunctionReconstructedNodeWrap<T,K,NDIM>, 1<<NDIM > &ins,
std::tuple<rnodeOut<T,K,NDIM>, cnodeOut<T,K,NDIM>> &out) {
//const typename ::detail::tree_types<T,K,NDIM>::compress_in_type& in,
//typename ::detail::tree_types<T,K,NDIM>::compress_out_type& out) {
Expand All @@ -435,9 +429,9 @@ void do_compress(const Key<NDIM>& key,
FixedTensor<T,2*K,NDIM> s;
//auto ins = ::mra::tuple_to_array_of_ptrs_const(in); /// Ugh ... cannot get const to match
for (size_t i : range(Key<NDIM>::num_children)) {
s(child_slices[i]) = in.get().neighbor_coeffs[i];
result.get().is_leaf[i] = in.get().is_neighbor_leaf[i];
sumsq += in.get().neighbor_sum[i]; // Accumulate sumsq from child difference coeffs
s(child_slices[i]) = ins[i].get().coeffs; //in.get().neighbor_coeffs[i];
result.get().is_leaf[i] = ins[i].get().is_leaf; //in.get().is_neighbor_leaf[i];
sumsq += ins[i].get().sum; //in.get().neighbor_sum[i]; // Accumulate sumsq from child difference coeffs
//if (in.neighbor_sum[i] > 10000)
//std::cout << i << " " << in.neighbor_sum[i] << " " << sumsq << std::endl;

Expand Down Expand Up @@ -478,11 +472,10 @@ std::string int2bitstring(size_t i, size_t width) {
/// Make a composite operator that implements compression for a single function
template <typename T, size_t K, Dimension NDIM>
auto make_compress(rnodeEdge<T,K,NDIM>& in, cnodeEdge<T,K,NDIM>& out, const std::string& name = "compress") {
rnodeEdge<T,K,NDIM> children1("children1"), children2("children2");
rnodeEdge<T,K,NDIM> children1("children1");

return std::make_tuple(ttg::make_tt(&send_leaves_up<T,K,NDIM>, edges(in), edges(children1, out), "send_leaves_up", {"input"}, {"children1", "output"}),
ttg::make_tt(&reduce_leaves<T,K,NDIM>, edges(children1), edges(children2), "reduce_leaves", {"children1"}, {"children2"}),
ttg::make_tt(&do_compress<T,K,NDIM>, edges(children2), edges(children1,out), "do_compress", {"children2"}, {"recur","output"}));
ttg::make_tt(&do_compress<T,K,NDIM>, edges(children1), edges(children1,out), "do_compress", {"children2"}, {"recur","output"}));
}

template <typename T, size_t K, Dimension NDIM>
Expand Down Expand Up @@ -687,16 +680,10 @@ void test1() {
auto compress = make_compress<T,K,NDIM>(a, b);

auto &reduce_leaves_op = std::get<1>(compress);
reduce_leaves_op->template set_input_reducer<0>([](FunctionReconstructedNodeWrap<T,K,NDIM> &node,
reduce_leaves_op->template set_input_reducer<0>([](std::array<FunctionReconstructedNodeWrap<T, K, NDIM>, 1<<NDIM> &nodes,
const FunctionReconstructedNodeWrap<T,K,NDIM> &another)
{
//Update self values into the array.
node.get().neighbor_coeffs[node.get().key.childindex()] = node.get().coeffs;
node.get().is_neighbor_leaf[node.get().key.childindex()] = node.get().is_leaf;
node.get().neighbor_sum[node.get().key.childindex()] = node.get().sum;
node.get().neighbor_coeffs[another.get().key.childindex()] = another.get().coeffs;
node.get().is_neighbor_leaf[another.get().key.childindex()] = another.get().is_leaf;
node.get().neighbor_sum[another.get().key.childindex()] = another.get().sum;
nodes[another.get().key.childindex()] = another;
});
reduce_leaves_op->template set_static_argstream_size<0>(1 << NDIM);

Expand All @@ -712,7 +699,6 @@ void test1() {
ops.push_back(std::move(p1));
ops.push_back(std::move(std::get<0>(compress)));
ops.push_back(std::move(std::get<1>(compress)));
ops.push_back(std::move(std::get<2>(compress)));
ops.push_back(std::move(recon));
ops.push_back(std::move(printer));
ops.push_back(std::move(printer2));
Expand Down Expand Up @@ -775,19 +761,12 @@ void test2(size_t nfunc, T thresh = 1e-6) {
auto compress = make_compress<T,K,NDIM>(a, b);
std::get<0>(compress)->set_keymap(pmap);
std::get<1>(compress)->set_keymap(pmap);
std::get<2>(compress)->set_keymap(pmap);

auto &reduce_leaves_op = std::get<1>(compress);
reduce_leaves_op->template set_input_reducer<0>([](FunctionReconstructedNodeWrap<T,K,NDIM> &node,
reduce_leaves_op->template set_input_reducer<0>([](std::array<FunctionReconstructedNodeWrap<T, K, NDIM>, 1<<NDIM> &nodes,
const FunctionReconstructedNodeWrap<T,K,NDIM> &another)
{
//Update self values into the array.
node.get().neighbor_coeffs[node.get().key.childindex()] = node.get().coeffs;
node.get().is_neighbor_leaf[node.get().key.childindex()] = node.get().is_leaf;
node.get().neighbor_sum[node.get().key.childindex()] = node.get().sum;
node.get().neighbor_coeffs[another.get().key.childindex()] = another.get().coeffs;
node.get().is_neighbor_leaf[another.get().key.childindex()] = another.get().is_leaf;
node.get().neighbor_sum[another.get().key.childindex()] = another.get().sum;
nodes[another.get().key.childindex()] = another;
});
reduce_leaves_op->template set_static_argstream_size<0>(1 << NDIM);

Expand All @@ -804,7 +783,6 @@ void test2(size_t nfunc, T thresh = 1e-6) {
ops.push_back(std::move(p1));
ops.push_back(std::move(std::get<0>(compress)));
ops.push_back(std::move(std::get<1>(compress)));
ops.push_back(std::move(std::get<2>(compress)));
ops.push_back(std::move(recon));
ops.push_back(std::move(printer));
ops.push_back(std::move(printer2));
Expand Down
4 changes: 0 additions & 4 deletions examples/mrafunctionnode.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,6 @@ namespace mra {
FunctionReconstructedNode(const Key<NDIM>& key) : key(key), sum(0.0), is_leaf(false) {}
T normf() const {return (is_leaf ? coeffs.normf() : 0.0);}
bool has_children() const {return !is_leaf;}
//Can't make it a vector to keep the class as POD.
std::array<FixedTensor<T, K, NDIM>, 1 << NDIM> neighbor_coeffs;
std::array<bool, 1 << NDIM> is_neighbor_leaf;
std::array<T, 1 << NDIM> neighbor_sum;
};

template <typename T, size_t K, Dimension NDIM>
Expand Down
24 changes: 18 additions & 6 deletions ttg/ttg/madness/ttg.h
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,10 @@ namespace ttg_madness {
static_assert(std::is_same_v<std::decay_t<Value>, std::decay_t<valueT>>,
"TT::set_arg(key,value) given value of type incompatible with TT");

using input_arg_type = std::decay_t<std::tuple_element_t<i, input_args_type>>;
using decay_value_t = std::decay_t<valueT>;
constexpr const bool edge_arg_convertible = std::is_convertible_v<decay_value_t, input_arg_type>;

const auto owner = keymap(key);
if (owner != world.rank()) {
ttg::trace(world.rank(), ":", get_name(), " : ", key, ": forwarding setting argument : ", i);
Expand Down Expand Up @@ -406,8 +410,14 @@ namespace ttg_madness {
if constexpr (!ttg::meta::is_void_v<valueT>) { // for data values
// have a value already? if not, set, otherwise reduce
if (args->nargs[i] == std::numeric_limits<std::size_t>::max()) {
this->get<i, std::decay_t<valueT> &>(args->input_values) = std::forward<Value>(value);

//this->get<i, std::decay_t<valueT> &>(args->input_values) = std::forward<Value>(value);
if constexpr(edge_arg_convertible) {
/* convert the types by assignment */
this->get<i, std::decay_t<input_arg_type> &>(args->input_values) = std::forward<Value>(value);
} else {
/* have to call reducer to do the conversion */
reducer(this->get<i, std::decay_t<input_arg_type> &>(args->input_values), value);
}
// now have a value, reset nargs
// check if we have a stream size for the op, which has precedence over the global setting.
if (args->stream_size[i] != 0) {
Expand All @@ -419,7 +429,7 @@ namespace ttg_madness {
args->nargs[i] = 1;
}
} else {
reducer(this->get<i, std::decay_t<valueT> &>(args->input_values), value);
reducer(this->get<i, std::decay_t<input_arg_type> &>(args->input_values), value);
}
} else {
reducer(); // even if this was a control input, must execute the reducer for possible side effects
Expand All @@ -433,11 +443,13 @@ namespace ttg_madness {
}
args->unlock();
} else { // this is a nonstreaming input => set the value
if constexpr (!ttg::meta::is_void_v<valueT>) { // for data values
if constexpr(!edge_arg_convertible) {
throw std::logic_error("Types are not convertible and no reducer was set!");
} else {
this->get<i, std::decay_t<valueT> &>(args->input_values) = std::forward<Value>(value);
args->nargs[i] = 0;
args->counter--;
}
args->nargs[i] = 0;
args->counter--;
}

// ready to run the task?
Expand Down