-
Notifications
You must be signed in to change notification settings - Fork 629
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
WIP: Speed up choose_chunkdivision with geom_boxt_tree #1030
Open
ChristopherHogan
wants to merge
5
commits into
NanoComp:master
Choose a base branch
from
ChristopherHogan:chogan/geom_box_tree
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
06bb2fd
WIP: geom_box_tree for faster fragment_stats
ChristopherHogan 10420b5
WIP: speed up choose_chunkdivision with geom_box_tree
ChristopherHogan db9fe35
WIP: Speed up fragment_stats with geom_box_tree
ChristopherHogan 404d610
Working much better now
ChristopherHogan 6778a69
Remove debug code and fix anisotropic bug
ChristopherHogan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1639,6 +1639,7 @@ int fragment_stats::maxeval = 0; | |
int fragment_stats::resolution = 0; | ||
meep::ndim fragment_stats::dims = meep::D1; | ||
geometric_object_list fragment_stats::geom = {}; | ||
geom_box_tree fragment_stats::geom_tree = NULL; | ||
std::vector<dft_data> fragment_stats::dft_data_list; | ||
std::vector<meep::volume> fragment_stats::pml_1d_vols; | ||
std::vector<meep::volume> fragment_stats::pml_2d_vols; | ||
|
@@ -1706,6 +1707,7 @@ fragment_stats compute_fragment_stats( | |
geom_box box = make_box_from_cell(cell_size); | ||
fragment_stats stats = init_stats(box, tol, maxeval, gv); | ||
stats.compute(); | ||
fragment_stats::reset(); | ||
return stats; | ||
} | ||
|
||
|
@@ -1717,16 +1719,27 @@ fragment_stats::fragment_stats(geom_box &bx) | |
num_pixels_in_box = get_pixels_in_box(&bx); | ||
} | ||
|
||
void fragment_stats::reset() { | ||
resolution = 0; | ||
split_chunks_evenly = false; | ||
destroy_geom_box_tree(geom_tree); | ||
geom_tree = NULL; | ||
} | ||
|
||
void fragment_stats::init_libctl(material_type default_mat, bool ensure_per, meep::grid_volume *gv, | ||
vector3 cell_size, vector3 cell_center, | ||
geometric_object_list *geom_) { | ||
geom_initialize(); | ||
default_material = default_mat; | ||
ensure_periodicity = ensure_per; | ||
geometry_center = cell_center; | ||
dimensions = meep::number_of_directions(gv->dim); | ||
geometry_lattice.size = cell_size; | ||
geom_fix_object_list(*geom_); | ||
geom_box cell_box = make_box_from_cell(cell_size); | ||
dimensions = 3; | ||
ensure_periodicity = 0; | ||
geom_tree = create_geom_box_tree0(*geom_, cell_box); | ||
dimensions = meep::number_of_directions(gv->dim); | ||
ensure_periodicity = ensure_per; | ||
} | ||
|
||
bool fragment_stats::has_non_medium_material() { | ||
|
@@ -1757,48 +1770,68 @@ void fragment_stats::update_stats_from_material(material_type mat, size_t pixels | |
} | ||
} | ||
|
||
void fragment_stats::compute_stats() { | ||
|
||
if (geom.num_items == 0) { | ||
// If there is no geometry, count the default material for the whole fragment | ||
update_stats_from_material((material_type)default_material, num_pixels_in_box); | ||
} | ||
void fragment_stats::compute_overlaps_from_tree(geom_box_tree t, bool &anisotropic_pixels_already_added) { | ||
if (!t || !geom_boxes_intersect(&t->b, &box)) | ||
return; | ||
|
||
for (int i = 0; i < geom.num_items; ++i) { | ||
geometric_object *go = &geom.items[i]; | ||
double overlap = box_overlap_with_object(box, *go, tol, maxeval); | ||
if (t->nobjects > 0) { | ||
geom_box intersection; | ||
geom_box_intersection(&intersection, &t->b, &box); | ||
|
||
for (int i = 0; i < t->nobjects; ++i) { | ||
if (geom_boxes_intersect(&intersection, &t->objects[i].box)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One potential optimization: if this intersection is the whole of |
||
geom_box shifted_box = {vector3_minus(intersection.low, t->objects[i].shiftby), | ||
vector3_minus(intersection.high, t->objects[i].shiftby)}; | ||
double overlap = box_overlap_with_object(shifted_box, *t->objects[i].o, tol, maxeval); | ||
|
||
if (eps_averaging && !anisotropic_pixels_already_added) { | ||
// If the object doesn't overlap the entire box, that implies that | ||
// an object interface intercepts the box, which means we treat | ||
// the entire box as anisotropic. This method could give some false | ||
// positives if there is another object with the same material behind | ||
// the current object, but in practice it is probably reasonable to | ||
// assume that there is a material interface somwhere in the box so | ||
// we won't worry about fancier edge-detection methods for now. | ||
if (overlap != 1.0) { | ||
anisotropic_pixels_already_added = true; | ||
num_anisotropic_eps_pixels += num_pixels_in_box; | ||
if (mu_not_1(t->objects[i].o->material)) { | ||
num_anisotropic_mu_pixels += num_pixels_in_box; | ||
} | ||
} | ||
} | ||
|
||
bool anisotropic_pixels_already_added = false; | ||
if (eps_averaging) { | ||
// If the object doesn't overlap the entire box, that implies that | ||
// an object interface intercepts the box, which means we treat | ||
// the entire box as anisotropic. This method could give some false | ||
// positives if there is another object with the same material behind | ||
// the current object, but in practice it is probably reasonable to | ||
// assume that there is a material interface somwhere in the box so | ||
// we won't worry about fancier edge-detection methods for now. | ||
if (overlap != 1.0) { | ||
anisotropic_pixels_already_added = true; | ||
num_anisotropic_eps_pixels += num_pixels_in_box; | ||
if (mu_not_1(go->material)) { | ||
num_anisotropic_mu_pixels += num_pixels_in_box; | ||
if (overlap > 0.0) { | ||
// Count contributions from material of object | ||
size_t num_pixels_in_intersection_box = get_pixels_in_box(&intersection); | ||
size_t pixels = (size_t)ceil(overlap * num_pixels_in_intersection_box); | ||
if (pixels > 0) { | ||
material_type mat = (material_type)t->objects[i].o->material; | ||
update_stats_from_material(mat, pixels, anisotropic_pixels_already_added); | ||
} | ||
size_t default_material_pixels = num_pixels_in_intersection_box - pixels; | ||
if (default_material_pixels > 0) { | ||
update_stats_from_material((material_type)default_material, default_material_pixels, | ||
anisotropic_pixels_already_added); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Count contributions from material of object | ||
size_t pixels = (size_t)ceil(overlap * num_pixels_in_box); | ||
if (pixels > 0) { | ||
material_type mat = (material_type)go->material; | ||
update_stats_from_material(mat, pixels, anisotropic_pixels_already_added); | ||
} | ||
compute_overlaps_from_tree(t->t1, anisotropic_pixels_already_added); | ||
compute_overlaps_from_tree(t->t2, anisotropic_pixels_already_added); | ||
} | ||
|
||
// Count contributions from default_material | ||
size_t default_material_pixels = num_pixels_in_box - pixels; | ||
if (default_material_pixels > 0) { | ||
update_stats_from_material((material_type)default_material, default_material_pixels, | ||
anisotropic_pixels_already_added); | ||
} | ||
void fragment_stats::compute_stats() { | ||
|
||
if (geom.num_items == 0) { | ||
// If there is no geometry, count the default material for the whole fragment | ||
update_stats_from_material((material_type)default_material, num_pixels_in_box); | ||
} | ||
else { | ||
bool anisotropic_pixels_already_added = false; | ||
compute_overlaps_from_tree(geom_tree, anisotropic_pixels_already_added); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a simpler alternative to creating this tree, you could also just cache
geom_get_bounding_box
for each object, and check whether the bounding box overlaps before callingbox_overlap_with_object
. This only helps the constant factor, though, unlike a tree which in principle can give better scaling.