Skip to content

Commit

Permalink
Fixes to account for dx,dy removal and omp functions removal
Browse files Browse the repository at this point in the history
  • Loading branch information
r-barnes committed May 7, 2022
1 parent 6173d98 commit af61c65
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 26 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required (VERSION 3.9)

project(richdem
VERSION 2.2.10
VERSION 2.2.11
DESCRIPTION "High-performance terrain analysis"
LANGUAGES CXX
)
Expand Down
24 changes: 20 additions & 4 deletions include/richdem/common/ProgressBar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,22 @@ class ProgressBar{
std::cerr<<"\r\033[2K"<<std::flush;
}

int num_threads() const {
#ifdef _OPENMP
return omp_get_num_threads();
#else
return 1;
#endif
}

int thread_num() const {
#ifdef _OPENMP
return omp_get_thread_num();
#else
return 1;
#endif
}

public:
///@brief Start/reset the progress bar.
///@param total_work The amount of work to be completed, usually specified in cells.
Expand All @@ -73,7 +89,7 @@ class ProgressBar{
return;
#endif

if(omp_get_thread_num()!=0)
if(thread_num()!=0)
return;

work_done = work_done0;
Expand All @@ -83,7 +99,7 @@ class ProgressBar{

next_update += call_diff;

uint16_t percent = (uint8_t)(work_done*omp_get_num_threads()*100/total_work_);
uint16_t percent = (uint8_t)(work_done*num_threads()*100/total_work_);
if(percent>100)
percent=100;
if(percent==old_percent)
Expand All @@ -96,12 +112,12 @@ class ProgressBar{
<<percent<<"% - "
<<std::fixed<<std::setprecision(1)<<timer.lap()/percent*(100-percent)
<<"s - "
<<omp_get_num_threads()<< " threads)"<<std::flush;
<<num_threads()<< " threads)"<<std::flush;
}

///Increment by one the work done and update the progress bar
ProgressBar& operator++(){
if(omp_get_thread_num()!=0)
if(thread_num()!=0)
return *this;
work_done++;
update(work_done);
Expand Down
32 changes: 19 additions & 13 deletions include/richdem/flats/Barnes2014.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ static void BuildAwayGradient(
flat_mask(c.x,c.y) = loops;
flat_height[labels(c.x,c.y)] = loops;
for(int n=1;n<=8;n++){
const int nx = c.x+dx[n];
const int ny = c.y+dy[n];
const int nx = c.x+d8x[n];
const int ny = c.y+d8y[n];
if(
labels.inGrid(nx,ny)
&& labels(nx,ny)==labels(c.x,c.y)
Expand Down Expand Up @@ -194,8 +194,8 @@ static void BuildTowardsCombinedGradient(
flat_mask(c.x,c.y)=2*loops;

for(int n=1;n<=8;n++){
const int nx = c.x+dx[n];
const int ny = c.y+dy[n];
const int nx = c.x+d8x[n];
const int ny = c.y+d8y[n];
if(
labels.inGrid(nx,ny)
&& labels(nx,ny)==labels(c.x,c.y)
Expand Down Expand Up @@ -271,8 +271,8 @@ static void LabelFlat(

//Consider this cell's neighbours
for(int n=1;n<=8;n++){
const int nx = c.x+dx[n];
const int ny = c.y+dy[n];
const int nx = c.x+d8x[n];
const int ny = c.y+d8y[n];
if(labels.inGrid(nx,ny)) //TODO: Should probably avoid adding cells that cannot be part of the flat. This'll likely speed things up.
to_fill.emplace(nx,ny);
}
Expand All @@ -288,6 +288,8 @@ static void LabelFlat(
Cells adjacent to lower and higher terrain are identified and
added to the appropriate queue
Assumes a D8 topology
@param[out] &low_edges Queue for storing cells adjacent to lower terrain
@param[out] &high_edges Queue for storing cells adjacent to higher terrain
@param[in] &flats 2D array indicating flat membership from FindFlats()
Expand Down Expand Up @@ -329,8 +331,8 @@ static void FindFlatEdges(
continue;

for(int n=1;n<=8;n++){
const int nx = x+dx[n];
const int ny = y+dy[n];
const int nx = x+d8x[n];
const int ny = y+d8y[n];

if(!flats.inGrid(nx,ny))
continue;
Expand Down Expand Up @@ -473,6 +475,8 @@ void GetFlatMask(
This alters elevations within the DEM so that flats which have been
resolved using GetFlatMask() will drain.
Assumes a D8 topology
@param[in] &flat_mask A mask from GetFlatMask()
@param[in] &labels A grouping from GetFlatMask()
@param[in,out] &elevations 2D array of elevations
Expand Down Expand Up @@ -517,7 +521,7 @@ void ResolveFlatsEpsilon_Barnes2014(
//the focal cell has been raised too high.
bool lower[9];
for(int n=1;n<=8;++n)
lower[n] = elevations(x,y)<elevations(x+dx[n],y+dy[n]);
lower[n] = elevations(x,y)<elevations(x+d8x[n],y+d8y[n]);

//Raise the focal cell by the appropriate number of increments
for(int i=0;i<flat_mask(x,y);++i)
Expand All @@ -526,8 +530,8 @@ void ResolveFlatsEpsilon_Barnes2014(
//Check the surrounding cells to see if we are inappropriately higher than
//any of them
for(int n=1;n<=8;++n){
const int nx = x+dx[n];
const int ny = y+dy[n];
const int nx = x+d8x[n];
const int ny = y+d8y[n];
//This neighbour is part of the flat, so it does not matter if we are
//higher than it
if(labels(nx,ny)==labels(x,y))
Expand Down Expand Up @@ -566,6 +570,8 @@ void ResolveFlatsEpsilon_Barnes2014(
Uses the helper function D8MaskedFlowdir()
Assumes a D8 topology
@param[in] &flat_mask A mask from GetFlatMask()
@param[in] &labels The labels output from GetFlatMask()
@param[out] &flowdirs Returns flat-resolved flow directions
Expand Down Expand Up @@ -614,8 +620,8 @@ void ResolveFlatsFlowdirs_Barnes2014(
//It is safe to do this without checking to see that (nx,ny) is within
//the grid because we only call this function on interior cells
for(int n=1;n<=8;n++){
const int nx = x+dx[n];
const int ny = y+dy[n];
const int nx = x+d8x[n];
const int ny = y+d8y[n];
if( labels(nx,ny)!=labels(ci))
continue;
if( flat_mask(nx,ny)<minimum_elevation || (flat_mask(nx,ny)==minimum_elevation && flowdir>0 && flowdir%2==0 && n%2==1) ){
Expand Down
6 changes: 3 additions & 3 deletions include/richdem/flats/find_flats.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace richdem {
@brief Finds flats: cells with no local gradient
@author Richard Barnes (rbarnes@umn.edu)
TODO
Assumes a D8 topology
@param[in] &elevations An elevation field
@param[in] &flats An array of flat indicators (post-conditions)
Expand Down Expand Up @@ -54,8 +54,8 @@ void FindFlats(
flats(x,y) = IS_A_FLAT;

for(int n=1;n<=8;n++){
const int nx = x+dx[n];
const int ny = y+dy[n];
const int nx = x+d8x[n];
const int ny = y+d8y[n];
if(elevations(nx,ny)<elevations(x,y) || elevations.isNoData(nx,ny)){
flats(x,y) = NOT_A_FLAT;
break;
Expand Down
18 changes: 13 additions & 5 deletions src/random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,17 @@

namespace richdem {

static int rd_get_thread_num(){
#ifdef _OPENMP
return omp_get_thread_num();
#else
return 0;
#endif
}

our_random_engine& rand_engine(){
static std::array<our_random_engine, PRNG_THREAD_MAX> e;
return e[omp_get_thread_num()];
return e[rd_get_thread_num()];
}


Expand All @@ -31,7 +39,7 @@ void seed_rand(uint64_t seed){
std::seed_seq q(std::begin(seed_data), std::end(seed_data));
rand_engine().seed(q);
} else {
rand_engine().seed( seed*omp_get_thread_num() );
rand_engine().seed( seed*rd_get_thread_num() );
}
}
}
Expand All @@ -40,21 +48,21 @@ void seed_rand(uint64_t seed){
int uniform_rand_int(int from, int thru){
static std::array<std::uniform_int_distribution<>, PRNG_THREAD_MAX> d;
using parm_t = std::uniform_int_distribution<>::param_type;
return d[omp_get_thread_num()]( rand_engine(), parm_t{from, thru} );
return d[rd_get_thread_num()]( rand_engine(), parm_t{from, thru} );
}


double uniform_rand_real(double from, double thru){
static std::array<std::uniform_real_distribution<>, PRNG_THREAD_MAX> d;
using parm_t = std::uniform_real_distribution<>::param_type;
return d[omp_get_thread_num()]( rand_engine(), parm_t{from, thru} );
return d[rd_get_thread_num()]( rand_engine(), parm_t{from, thru} );
}


double normal_rand(double mean, double stddev){
static std::array<std::normal_distribution<double>, PRNG_THREAD_MAX> d;
using parm_t = std::normal_distribution<double>::param_type;
return d[omp_get_thread_num()]( rand_engine(), parm_t{mean, stddev} );
return d[rd_get_thread_num()]( rand_engine(), parm_t{mean, stddev} );
}

RandomEngineState SaveRandomState(){
Expand Down

0 comments on commit af61c65

Please sign in to comment.