-
Notifications
You must be signed in to change notification settings - Fork 116
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
Sparse optimizations for GaBW sampling #331
Open
lucaperju
wants to merge
3
commits into
GeomScale:develop
Choose a base branch
from
lucaperju:gabw_optimizations
base: develop
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
3 commits
Select commit
Hold shift + click to select a range
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
133 changes: 133 additions & 0 deletions
133
include/random_walks/accelerated_billiard_walk_utils.hpp
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 |
---|---|---|
@@ -0,0 +1,133 @@ | ||
// VolEsti (volume computation and sampling library) | ||
|
||
// Copyright (c) 2012-2020 Vissarion Fisikopoulos | ||
// Copyright (c) 2018-2020 Apostolos Chalkis | ||
// Copyright (c) 2024 Luca Perju | ||
|
||
// Licensed under GNU LGPL.3, see LICENCE file | ||
|
||
#ifndef ACCELERATED_BILLIARD_WALK_UTILS_HPP | ||
#define ACCELERATED_BILLIARD_WALK_UTILS_HPP | ||
|
||
#include <Eigen/Eigen> | ||
#include <vector> | ||
|
||
const double eps = 1e-10; | ||
|
||
// data structure which maintains the values of (b - Ar)/Av, and can extract the minimum positive value and the facet associated with it | ||
// vec[i].first contains the value of (b(i) - Ar(i))/Av(i) + moved_dist, where moved_dist is the total distance that the point has travelled so far | ||
// The heap will only contain the values from vec which are greater than moved_dist (so that they are positive) | ||
template<typename NT> | ||
class BoundaryOracleHeap { | ||
public: | ||
int n, heap_size; | ||
std::vector<std::pair<NT, int>> heap; | ||
std::vector<std::pair<NT, int>> vec; | ||
|
||
private: | ||
int siftDown(int index) { | ||
while((index << 1) + 1 < heap_size) { | ||
int child = (index << 1) + 1; | ||
if(child + 1 < heap_size && heap[child + 1].first < heap[child].first - eps) { | ||
child += 1; | ||
} | ||
if(heap[child].first < heap[index].first - eps) | ||
{ | ||
std::swap(heap[child], heap[index]); | ||
std::swap(vec[heap[child].second].second, vec[heap[index].second].second); | ||
index = child; | ||
} else { | ||
return index; | ||
} | ||
} | ||
return index; | ||
} | ||
|
||
int siftUp(int index) { | ||
while(index > 0 && heap[(index - 1) >> 1].first - eps > heap[index].first) { | ||
std::swap(heap[(index - 1) >> 1], heap[index]); | ||
std::swap(vec[heap[(index - 1) >> 1].second].second, vec[heap[index].second].second); | ||
index = (index - 1) >> 1; | ||
} | ||
return index; | ||
} | ||
|
||
// takes the index of a facet, and (in case it is in the heap) removes it from the heap. | ||
void remove (int index) { | ||
index = vec[index].second; | ||
if(index == -1) { | ||
return; | ||
} | ||
std::swap(heap[heap_size - 1], heap[index]); | ||
std::swap(vec[heap[heap_size - 1].second].second, vec[heap[index].second].second); | ||
vec[heap[heap_size - 1].second].second = -1; | ||
heap_size -= 1; | ||
index = siftDown(index); | ||
siftUp(index); | ||
} | ||
|
||
// inserts a new value into the heap, with its associated facet | ||
void insert (const std::pair<NT, int> val) { | ||
vec[val.second].second = heap_size; | ||
vec[val.second].first = val.first; | ||
heap[heap_size++] = val; | ||
siftUp(heap_size - 1); | ||
} | ||
|
||
public: | ||
BoundaryOracleHeap() {} | ||
|
||
BoundaryOracleHeap(int n) : n(n), heap_size(0) { | ||
heap.resize(n); | ||
vec.resize(n); | ||
} | ||
|
||
// rebuilds the heap with the existing values from vec | ||
// O(n) | ||
void rebuild (const NT &moved_dist) { | ||
heap_size = 0; | ||
for(int i = 0; i < n; ++i) { | ||
vec[i].second = -1; | ||
if(vec[i].first - eps > moved_dist) { | ||
vec[i].second = heap_size; | ||
heap[heap_size++] = {vec[i].first, i}; | ||
} | ||
} | ||
for(int i = heap_size - 1; i >= 0; --i) { | ||
siftDown(i); | ||
} | ||
} | ||
|
||
// returns (b(i) - Ar(i))/Av(i) + moved_dist | ||
// O(1) | ||
NT get_val (const int &index) { | ||
return vec[index].first; | ||
} | ||
|
||
// returns the nearest facet | ||
// O(1) | ||
std::pair<NT, int> get_min () { | ||
return heap[0]; | ||
} | ||
|
||
// changes the stored value for a given facet, and updates the heap accordingly | ||
// O(logn) | ||
void change_val(const int& index, const NT& new_val, const NT& moved_dist) { | ||
if(new_val < moved_dist - eps) { | ||
vec[index].first = new_val; | ||
remove(index); | ||
} else { | ||
if(vec[index].second == -1) { | ||
insert({new_val, index}); | ||
} else { | ||
heap[vec[index].second].first = new_val; | ||
vec[index].first = new_val; | ||
siftDown(vec[index].second); | ||
siftUp(vec[index].second); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
|
||
#endif |
Oops, something went wrong.
Oops, something went wrong.
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.
What is
update_parameters
here? I do not understand!std::is_same_v<update_parameters
could you please explain?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.
basically there's another compute_reflection function above which takes an integer (just the facet) as the 3rd argument, and for some reason, if I don't have that condition the compiler decides to call this function assuming that the typename of update_parameters is int. There might maybe be better ways of dealing with these issues, but I remember I tried to solve them for some time and this is the best I could do, I couldn't at all understand how the compiler chooses which function to use when there's multiple ones that match
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.
It seems that we are too generic here and that complicates the interface a lot!
compute_reflection
is called by several walks. Instead of creating all those complicated overloads why not simply create a new name. Especially if this is the case that this function is only called by a single walk (i.e. accelerated billiard walk). Moreover,update_parameters
should not be a template but the struct defined in billiard walk, in all other cases this code will not compile since all other "update_parameters" does not have amoved_dist
field.