Skip to content

Commit

Permalink
RC tags stored separate from regular tags
Browse files Browse the repository at this point in the history
The rc tags are stored in a new vector of tags. They are converted to
"mesh format" and stored in the tags vector as needed for the various
adaption processes.
  • Loading branch information
jacobmerson committed May 27, 2022
1 parent 0772fdf commit 7dbcd10
Show file tree
Hide file tree
Showing 15 changed files with 154 additions and 337 deletions.
9 changes: 1 addition & 8 deletions src/Omega_h_adapt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,7 @@ static void validate(Mesh* mesh, AdaptOpts const& opts) {
}

static bool pre_adapt(Mesh* mesh, AdaptOpts const& opts) {
std::cout<<"calling validate"<<std::endl;
validate(mesh, opts);
std::cout<<"calling xfer validate"<<std::endl;
opts.xfer_opts.validate(mesh);
if (opts.verbosity >= EACH_ADAPT && !mesh->comm()->rank()) {
std::cout << "before adapting:\n";
Expand Down Expand Up @@ -284,9 +282,7 @@ bool adapt(Mesh* mesh, AdaptOpts const& opts) {
OMEGA_H_CHECK(mesh->family() == OMEGA_H_SIMPLEX);
auto t0 = now();

std::cout<<"ADAPT"<<std::endl;
mesh->change_all_rcFieldsToMesh();
std::cout<<"Calling Pre_adapt"<<std::endl;
ScopedChangeRCFieldsToMesh change_to_mesh(*mesh);
if (!pre_adapt(mesh, opts)) return false;
setup_conservation_tags(mesh, opts);
auto t1 = now();
Expand All @@ -297,15 +293,12 @@ bool adapt(Mesh* mesh, AdaptOpts const& opts) {
correct_integral_errors(mesh, opts);
auto t4 = now();

//mesh->change_all_rcFieldsTorc();

mesh->set_parting(OMEGA_H_ELEM_BASED);

//mesh->change_all_rcFieldsToMesh();

post_adapt(mesh, opts, t0, t1, t2, t3, t4);

mesh->change_all_rcFieldsTorc();

return true;
}
Expand Down
6 changes: 0 additions & 6 deletions src/Omega_h_coarsen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,21 +161,17 @@ static bool coarsen(Mesh* mesh, AdaptOpts const& opts, OvershootLimit overshoot,
auto ret = coarsen_element_based1(mesh);
if (ret) {

mesh->change_all_rcFieldsTorc();

mesh->set_parting(OMEGA_H_GHOSTED);

mesh->change_all_rcFieldsToMesh();

ret = coarsen_ghosted(mesh, opts, overshoot, improve);
}
if (ret) {

mesh->change_all_rcFieldsTorc();

mesh->set_parting(OMEGA_H_ELEM_BASED, false);

mesh->change_all_rcFieldsToMesh();

coarsen_element_based2(mesh, opts);
}
Expand Down Expand Up @@ -222,11 +218,9 @@ bool coarsen_by_size(Mesh* mesh, AdaptOpts const& opts) {
bool coarsen_slivers(Mesh* mesh, AdaptOpts const& opts) {
OMEGA_H_TIME_FUNCTION;

mesh->change_all_rcFieldsTorc();

mesh->set_parting(OMEGA_H_GHOSTED);

mesh->change_all_rcFieldsToMesh();

auto comm = mesh->comm();
auto elems_are_cands =
Expand Down
2 changes: 0 additions & 2 deletions src/Omega_h_conserve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -801,11 +801,9 @@ void correct_integral_errors(Mesh* mesh, AdaptOpts const& opts) {
if (!should_conserve_any(mesh, xfer_opts)) return;
begin_code("correct_integral_errors");

mesh->change_all_rcFieldsTorc();

mesh->set_parting(OMEGA_H_GHOSTED);

mesh->change_all_rcFieldsToMesh();

auto verbose = opts.verbosity > SILENT;
auto diffusion_graph = get_elem_diffusion_graph(mesh);
Expand Down
9 changes: 5 additions & 4 deletions src/Omega_h_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,11 @@ static void read_tag(std::istream& stream, Mesh* mesh, Int d,
using T = decltype(t);
Read<T> array;
read_array(stream, array, is_compressed, needs_swapping);
mesh->add_tag(d, name, ncomps, array, true);
const auto has_rc_suffix = (name.find("_rc") != std::string::npos);
if (has_rc_suffix) {
mesh->change_tagTorc<T> (d, ncomps, name, class_ids);
if(is_rc_tag(name)) {
mesh->set_rc_from_mesh_array(d,ncomps,class_ids,name,array);
}
else {
mesh->add_tag(d, name, ncomps, array, true);
}
};
detail::apply_to_omega_h_types(static_cast<Omega_h_Type>(type), f);
Expand Down
4 changes: 1 addition & 3 deletions src/Omega_h_mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -509,17 +509,15 @@ Mesh::TagCIter Mesh::tag_iter(Int ent_dim, std::string const& name) const {
[&](TagPtr const& a) { return a->name() == name; });
}
std::pair<bool,Mesh::TagIter> Mesh::rc_tag_iter(Int ent_dim, std::string const& name) {
std::cout<<"normal iterator"<<std::endl;
auto rc_begin = rc_field_tags_[ent_dim].begin();
auto rc_end = rc_field_tags_[ent_dim].end();
auto it = std::find_if(rc_begin, rc_end,
[&](TagPtr const& a) { std::cout<<"search: "<<a->name()<<" "<<name<<std::endl; return a->name() == name; });
[&](TagPtr const& a) { return (a->name() == name); });
auto found = (it!=rc_field_tags_[ent_dim].end());
return {found, it};
}

std::pair<bool,Mesh::TagCIter> Mesh::rc_tag_iter(Int ent_dim, std::string const& name) const {
std::cout<<"constant iterator"<<std::endl;
auto rc_begin = rc_field_tags_[ent_dim].begin();
auto rc_end = rc_field_tags_[ent_dim].end();
auto it = std::find_if(rc_begin, rc_end,
Expand Down
38 changes: 31 additions & 7 deletions src/Omega_h_mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,17 +201,22 @@ class Mesh {
void sync_rcField(Int ent_dim, std::string const& name);
[[nodiscard]] bool has_rcField(Int ent_dim, std::string const& name) const;



template <typename T>
void set_rc_from_mesh_array(Int ent_dim, Int ncomps, LOs class_ids,
std::string const& name, Read<T> array);
friend class ScopedChangeRCFieldsToMesh;
private:
bool change_all_rcFieldsToMesh();
bool change_all_rcFieldsTorc();
template <typename T>
void change_tagTorc(Int ent_dim, Int ncomps, std::string const& name,
LOs class_ids, bool remove = true);
template <typename T>
void change_tagToMesh(Int ent_dim, Int ncomps, std::string const& name,
LOs class_ids, bool remove = true);

void change_all_rcFieldsToMesh();
void change_all_rcFieldsTorc();

private:
template <typename T>
[[nodiscard]] Read<T> get_rc_mesh_array(
Int ent_dim, Int ncomps, std::string const& name, LOs class_ids);
Expand All @@ -220,9 +225,6 @@ class Mesh {
[[nodiscard]] Read<T> get_rc_array_from_mesh_array(Int ent_dim, Int ncomps,
std::string const& name, LOs class_ids, Read<T> mesh_array);

template <typename T>
void set_rc_from_mesh_array(Int ent_dim, Int ncomps, LOs class_ids,
std::string const& name, Read<T> array);

template <typename T>
[[nodiscard]] std::unique_ptr<Tag<T>> get_rc_mesh_tag_from_rc_tag(
Expand Down Expand Up @@ -381,6 +383,26 @@ class Mesh {
Int dim, TagBase const*);
};

class ScopedChangeRCFieldsToMesh {
public:
[[nodiscard]]
explicit ScopedChangeRCFieldsToMesh(Mesh& mesh) : mesh_(mesh) {
changed_here_ = mesh_.change_all_rcFieldsToMesh();
}
~ScopedChangeRCFieldsToMesh() {
if(changed_here_)
{
mesh_.change_all_rcFieldsTorc();
}
}
[[nodiscard]]
bool did_conversion() const noexcept { return changed_here_; }

private:
Mesh& mesh_;
bool changed_here_;
};

bool can_print(Mesh* mesh);

Real repro_sum_owned(Mesh* mesh, Int dim, Reals a);
Expand All @@ -404,6 +426,8 @@ LOs ents_on_closure(
LOs nodes_on_closure(
Mesh* mesh, std::set<std::string> const& class_names, Graph nodes2ents[4]);

bool is_rc_tag(std::string const& name);

// workaround CUDA compiler bug
#ifdef OMEGA_H_USE_CUDA
__host__
Expand Down
81 changes: 14 additions & 67 deletions src/Omega_h_migrate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,79 +126,26 @@ void push_tags(Mesh *old_mesh, Mesh* new_mesh, Int ent_dim,
Dist old_owners2new_ents) {
OMEGA_H_TIME_FUNCTION;
OMEGA_H_CHECK(old_owners2new_ents.nroots() == old_mesh->nents(ent_dim));
ScopedChangeRCFieldsToMesh rc_to_mesh(*old_mesh);
for (Int i = 0; i < old_mesh->ntags(ent_dim); ++i) {
auto tag = old_mesh->get_tag(ent_dim, i);
auto const& name = tag->name();
auto ncomps = tag->ncomps();
auto class_ids = tag->class_ids();

if (is<I8>(tag)) {

//TODO call this after creating a new flag to check if conversion is
// needed
size_t found = name.find("_rc");
if (found != std::string::npos) {
if (old_mesh->nents(ent_dim))
old_mesh->change_tagToMesh<I8> (ent_dim, ncomps, name, class_ids);
}

auto array = as<I8>(tag)->array();
array = old_owners2new_ents.exch(array, tag->ncomps());
new_mesh->add_tag<I8>(ent_dim, tag->name(), tag->ncomps(), array, true);

if (found != std::string::npos) {
new_mesh->change_tagTorc<I8> (ent_dim, ncomps, name, class_ids);
}

} else if (is<I32>(tag)) {

size_t found = (tag->name()).find("_rc");
if (found != std::string::npos) {
if (old_mesh->nents(ent_dim))
old_mesh->change_tagToMesh<I32> (ent_dim, ncomps, name, class_ids);
const auto ncomps = tag->ncomps();
const auto class_ids = tag->class_ids();
detail::apply_to_omega_h_types(tag->type(), [&](auto t){
using T = decltype(t);
auto array = old_mesh->get_array<T>(ent_dim,name);
array = old_owners2new_ents.exch(array, ncomps);

if(is_rc_tag(name) && rc_to_mesh.did_conversion() ) {
new_mesh->set_rc_from_mesh_array(ent_dim,ncomps,class_ids,name,array);
}

auto array = as<I32>(tag)->array();
array = old_owners2new_ents.exch(array, tag->ncomps());
new_mesh->add_tag<I32>(ent_dim, tag->name(), tag->ncomps(), array, true);

if (found != std::string::npos) {
new_mesh->change_tagTorc<I32> (ent_dim, ncomps, name, class_ids);
else {
// FIXME missing class_ids for rc tag
new_mesh->add_tag<T>(ent_dim, name, ncomps, array, true);
}

} else if (is<I64>(tag)) {

size_t found = (tag->name()).find("_rc");
if (found != std::string::npos) {
if (old_mesh->nents(ent_dim))
old_mesh->change_tagToMesh<I64> (ent_dim, ncomps, name, class_ids);
}

auto array = as<I64>(tag)->array();
array = old_owners2new_ents.exch(array, tag->ncomps());
new_mesh->add_tag<I64>(ent_dim, tag->name(), tag->ncomps(), array, true);

if (found != std::string::npos) {
new_mesh->change_tagTorc<I64> (ent_dim, ncomps, name, class_ids);
}

} else if (is<Real>(tag)) {

size_t found = (tag->name()).find("_rc");
if (found != std::string::npos) {
if (old_mesh->nents(ent_dim))
old_mesh->change_tagToMesh<Real> (ent_dim, ncomps, name, class_ids);
}

auto array = as<Real>(tag)->array();
array = old_owners2new_ents.exch(array, tag->ncomps());
new_mesh->add_tag<Real>(ent_dim, tag->name(), tag->ncomps(), array, true);

if (found != std::string::npos) {
new_mesh->change_tagTorc<Real> (ent_dim, ncomps, name, class_ids);
}

}
});
}
}

Expand Down
30 changes: 18 additions & 12 deletions src/Omega_h_rcFields.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,12 @@ Read<T> Mesh::get_rc_array_from_mesh_array(Int ent_dim, Int ncomps,
void Mesh::set_rc_from_mesh_array(Int ent_dim, Int ncomps, LOs class_ids,
std::string const& name, Read<T> array) {
OMEGA_H_TIME_FUNCTION;
auto [has_rc_tag, itr] = rc_tag_iter(ent_dim,name);
auto b_field =
get_rc_array_from_mesh_array(ent_dim, ncomps, name, class_ids, array);
if(!has_rc_tag) {
add_rcField<T>(class_ids,ent_dim,name,ncomps);
}
set_rcField_array(ent_dim, name, b_field);
}

Expand Down Expand Up @@ -444,14 +448,7 @@ void Mesh::reduce_rcField(Int ent_dim, std::string const& name, Omega_h_Op op) {

void Mesh::sync_rcField(Int ent_dim, std::string const& name) {
auto new_name = get_rc_name(name);
auto [tag_found, itr] = rc_tag_iter(ent_dim, name);
std::cout<<rc_field_tags_[ent_dim].size()<<std::endl;
for(auto& tag: rc_field_tags_[ent_dim]) {
OMEGA_H_CHECK(tag != nullptr);
std::cout<<"list_tag: "<<tag->name()<<std::endl;
}
auto rank = library()->world()->rank();
std::cout<<tag_found <<" "<<rank<<std::endl;
auto [tag_found, itr] = rc_tag_iter(ent_dim, new_name);
OMEGA_H_CHECK(tag_found);

const auto ncomps = (*itr)->ncomps();
Expand All @@ -466,10 +463,12 @@ void Mesh::sync_rcField(Int ent_dim, std::string const& name) {
detail::apply_to_omega_h_types((*itr)->type(), f);
}

void Mesh::change_all_rcFieldsToMesh() {
bool Mesh::change_all_rcFieldsToMesh() {
OMEGA_H_TIME_FUNCTION;
bool changed = false;
for (Int ent_dim = 0; ent_dim <= dim(); ++ent_dim) {
for (const auto& rc_tag : rc_field_tags_[ent_dim]) {
changed = true;
OMEGA_H_CHECK(rc_tag != nullptr);
auto const& name = rc_tag->name();
auto ncomps = rc_tag->ncomps();
Expand All @@ -486,13 +485,16 @@ void Mesh::change_all_rcFieldsToMesh() {
rc_field_tags_[ent_dim].clear();
OMEGA_H_CHECK(rc_field_tags_[ent_dim].size() == 0);
}
return changed;
}

void Mesh::change_all_rcFieldsTorc() {
bool Mesh::change_all_rcFieldsTorc() {
OMEGA_H_TIME_FUNCTION;
bool changed = false;
for (Int ent_dim = 0; ent_dim <= dim(); ++ent_dim) {
for (const auto& tag : tags_[ent_dim]) {
if (tag->name().find("_rc") != std::string::npos) {
if (is_rc_tag(tag->name())) {
changed = true;
OMEGA_H_CHECK(tag != nullptr);
auto const& name = tag->name();
auto ncomps = tag->ncomps();
Expand All @@ -509,10 +511,11 @@ void Mesh::change_all_rcFieldsTorc() {
tags_[ent_dim].erase(
std::remove_if(tags_[ent_dim].begin(), tags_[ent_dim].end(),
[](const auto& tag) {
return (tag->name().find("_rc") != std::string::npos);
return is_rc_tag(tag->name());
}),
tags_[ent_dim].end());
}
return changed;
}

Adj Mesh::ask_revClass_downAdj(Int from, Int to) {
Expand All @@ -539,5 +542,8 @@ Adj Mesh::ask_revClass_downAdj(Int from, Int to) {
parallel_for(n_gents + 1, f2, "createDownA2ab");
return {LOs(g2g_hl), LOs(g_hl2l)};
}
bool is_rc_tag(std::string const& name) {
return (name.find("_rc") != std::string::npos);
}

} // end namespace Omega_h
12 changes: 0 additions & 12 deletions src/Omega_h_refine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,22 +84,10 @@ static void refine_element_based(Mesh* mesh, AdaptOpts const& opts) {

static bool refine(Mesh* mesh, AdaptOpts const& opts) {

mesh->change_all_rcFieldsTorc();

mesh->set_parting(OMEGA_H_GHOSTED);

mesh->change_all_rcFieldsToMesh();

if (!refine_ghosted(mesh, opts)) return false;

mesh->change_all_rcFieldsTorc();

mesh->set_parting(OMEGA_H_ELEM_BASED);

mesh->change_all_rcFieldsToMesh();

refine_element_based(mesh, opts);

return true;
}

Expand Down
Loading

0 comments on commit 7dbcd10

Please sign in to comment.