Skip to content

Commit

Permalink
Use min and max from the standard library (#1717)
Browse files Browse the repository at this point in the history
Co-authored-by: Andreas Hoenselaar <[email protected]>
  • Loading branch information
ahoenselaar and Andreas Hoenselaar authored Jul 30, 2021
1 parent 5011d4d commit 9509547
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 48 deletions.
3 changes: 2 additions & 1 deletion src/array_slice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
h5fields.cpp
*/

#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <math.h>
Expand Down Expand Up @@ -277,7 +278,7 @@ static void get_array_slice_chunkloop(fields_chunk *fc, int ichnk, component cgr
for (int i = 0; i < data->rank; ++i) {
direction d = data->ds[i];
int isd = isS.in_direction(d), ied = ieS.in_direction(d);
start[i] = (min(isd, ied) - data->min_corner.in_direction(d)) / 2;
start[i] = (std::min(isd, ied) - data->min_corner.in_direction(d)) / 2;
count[i] = abs(ied - isd) / 2 + 1;
slice_size *= count[i];
if (ied < isd) offset[permute.in_direction(d)] = count[i] - 1;
Expand Down
5 changes: 3 additions & 2 deletions src/bands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
% Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
Expand All @@ -33,9 +34,9 @@ namespace meep {

double fields::last_source_time() {
double last_time = 0;
if (sources != NULL) last_time = max(last_time, sources->last_time_max());
if (sources != NULL) last_time = std::max(last_time, sources->last_time_max());
for (int i = 0; i < num_chunks; i++)
if (chunks[i]->is_mine()) last_time = max(last_time, chunks[i]->last_source_time());
if (chunks[i]->is_mine()) last_time = std::max(last_time, chunks[i]->last_source_time());
return max_to_all(last_time);
}

Expand Down
4 changes: 2 additions & 2 deletions src/dft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ complex<double> dft_chunk::process_dft_component(int rank, direction *ds, ivec m
for (int i = 0; i < rank; ++i) {
direction d = ds[i];
int isd = isS.in_direction(d), ied = ieS.in_direction(d);
start[i] = (min(isd, ied) - min_corner.in_direction(d)) / 2;
start[i] = (std::min(isd, ied) - min_corner.in_direction(d)) / 2;
file_count[i] = abs(ied - isd) / 2 + 1;
if (ied < isd) file_offset[permute.in_direction(d)] = file_count[i] - 1;
array_count[i] = (max_corner.in_direction(d) - min_corner.in_direction(d)) / 2 + 1;
Expand Down Expand Up @@ -944,7 +944,7 @@ complex<double> fields::process_dft_component(dft_chunk **chunklists, int num_ch
LOOP_OVER_DIRECTIONS(chunk->fc->gv.dim, d) {
this_bufsz *= (chunk->ie.in_direction(d) - chunk->is.in_direction(d)) / 2 + 1;
}
bufsz = max(bufsz, this_bufsz);
bufsz = std::max(bufsz, this_bufsz);
}
am_now_working_on(MpiAllTime);
max_corner = max_to_all(max_corner);
Expand Down
4 changes: 2 additions & 2 deletions src/h5fields.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ static void h5_findsize_chunkloop(fields_chunk *fc, int ichnk, component cgrid,
LOOP_OVER_DIRECTIONS(fc->gv.dim, d) {
bufsz *= (ie.in_direction(d) - is.in_direction(d)) / 2 + 1;
}
data->bufsz = max(data->bufsz, bufsz);
data->bufsz = std::max(data->bufsz, bufsz);
}

static void h5_output_chunkloop(fields_chunk *fc, int ichnk, component cgrid, ivec is, ivec ie,
Expand Down Expand Up @@ -124,7 +124,7 @@ static void h5_output_chunkloop(fields_chunk *fc, int ichnk, component cgrid, iv
for (int i = 0; i < data->rank; ++i) {
direction d = data->ds[i];
int isd = isS.in_direction(d), ied = ieS.in_direction(d);
start[i] = (min(isd, ied) - data->min_corner.in_direction(d)) / 2;
start[i] = (std::min(isd, ied) - data->min_corner.in_direction(d)) / 2;
count[i] = abs(ied - isd) / 2 + 1;
if (ied < isd) offset[permute.in_direction(d)] = count[i] - 1;
}
Expand Down
4 changes: 2 additions & 2 deletions src/integrate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ static void integrate_chunkloop(fields_chunk *fc, int ichunk, component cgrid, i
}

complex<double> integrand = data->integrand(fvals, loc, data->integrand_data_);
maxabs = max(maxabs, abs(integrand));
maxabs = std::max(maxabs, abs(integrand));
sum += integrand * IVEC_LOOP_WEIGHT(s0, s1, e0, e1, dV0 + dV1 * loop_i2);
}

data->maxabs = max(data->maxabs, maxabs);
data->maxabs = std::max(data->maxabs, maxabs);
data->sum += sum;
}

Expand Down
4 changes: 2 additions & 2 deletions src/integrate2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,11 @@ static void integrate_chunkloop(fields_chunk *fc, int ichunk, component cgrid, i
}

complex<double> integrand = data->integrand(fvals, loc, data->integrand_data_);
maxabs = max(maxabs, abs(integrand));
maxabs = std::max(maxabs, abs(integrand));
sum += integrand * IVEC_LOOP_WEIGHT(s0, s1, e0, e1, dV0 + dV1 * loop_i2);
}

data->maxabs = max(data->maxabs, maxabs);
data->maxabs = std::max(data->maxabs, maxabs);
data->sum += sum;
}

Expand Down
8 changes: 4 additions & 4 deletions src/loop_in_chunks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,22 +459,22 @@ void fields::loop_in_chunks(field_chunkloop chunkloop, void *chunkloop_data, con
e0c.set_direction(d, e1.in_direction(dS));
}
if (iecS.in_direction(dS) == iscS.in_direction(dS)) {
double w = min(s0c.in_direction(d), e0c.in_direction(d));
double w = std::min(s0c.in_direction(d), e0c.in_direction(d));
s0c.set_direction(d, w);
e0c.set_direction(d, w);
s1c.set_direction(d, w);
e1c.set_direction(d, w);
}
else if (iecS.in_direction(dS) == iscS.in_direction(dS) + 1 * 2) {
double w = min(s0c.in_direction(d), e1c.in_direction(d));
double w = std::min(s0c.in_direction(d), e1c.in_direction(d));
s0c.set_direction(d, w);
e1c.set_direction(d, w);
w = min(s1c.in_direction(d), e0c.in_direction(d));
w = std::min(s1c.in_direction(d), e0c.in_direction(d));
s1c.set_direction(d, w);
e0c.set_direction(d, w);
}
else if (iecS.in_direction(dS) == iscS.in_direction(dS) + 2 * 2) {
double w = min(s1c.in_direction(d), e1c.in_direction(d));
double w = std::min(s1c.in_direction(d), e1c.in_direction(d));
s1c.set_direction(d, w);
e1c.set_direction(d, w);
}
Expand Down
8 changes: 0 additions & 8 deletions src/meep_internals.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@ namespace meep {
#define DOCMP for (int cmp = 0; cmp < 2 - is_real; cmp++)
#define DOCMP2 for (int cmp = 0; cmp < 2; cmp++)

inline double max(double a, double b) { return (a > b) ? a : b; }
inline double min(double a, double b) { return (a < b) ? a : b; }
inline int max(int a, int b) { return (a > b) ? a : b; }
inline size_t max(size_t a, size_t b) { return (a > b) ? a : b; }
inline int min(int a, int b) { return (a < b) ? a : b; }
static inline int abs(int a) { return a < 0 ? -a : a; }
static inline double abs(double a) { return fabs(a); }

// note that C99 has a round() function, but I don't want to rely on it
static inline int my_round(double x) { return int(floor(fabs(x) + 0.5) * (x < 0 ? -1 : 1)); }

Expand Down
2 changes: 1 addition & 1 deletion src/sources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ src_time *src_time::add_to(src_time *others, src_time **added) const {
}

double src_time::last_time_max(double after) {
after = max(last_time(), after);
after = std::max(last_time(), after);
if (next)
return next->last_time_max(after);
else
Expand Down
6 changes: 3 additions & 3 deletions src/structure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -937,14 +937,14 @@ structure_chunk::structure_chunk(const grid_volume &thegv, const volume &vol_lim
double structure::max_eps() const {
double themax = 0.0;
for (int i = 0; i < num_chunks; i++)
if (chunks[i]->is_mine()) themax = max(themax, chunks[i]->max_eps());
if (chunks[i]->is_mine()) themax = std::max(themax, chunks[i]->max_eps());
return max_to_all(themax);
}

double fields::max_eps() const {
double themax = 0.0;
for (int i = 0; i < num_chunks; i++)
if (chunks[i]->is_mine()) themax = max(themax, chunks[i]->s->max_eps());
if (chunks[i]->is_mine()) themax = std::max(themax, chunks[i]->s->max_eps());
return max_to_all(themax);
}

Expand All @@ -954,7 +954,7 @@ double structure_chunk::max_eps() const {
direction d = component_direction(c);
if (chi1inv[c][d])
for (size_t i = 0; i < gv.ntot(); i++)
themax = max(themax, 1 / chi1inv[c][d][i]);
themax = std::max<double>(themax, 1 / chi1inv[c][d][i]);
}
return themax;
}
Expand Down
28 changes: 14 additions & 14 deletions src/vec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,31 +131,31 @@ component first_field_component(field_type ft) {
vec min(const vec &vec1, const vec &vec2) {
vec m(vec1.dim);
LOOP_OVER_DIRECTIONS(vec1.dim, d) {
m.set_direction(d, min(vec1.in_direction(d), vec2.in_direction(d)));
m.set_direction(d, std::min(vec1.in_direction(d), vec2.in_direction(d)));
}
return m;
}

vec max(const vec &vec1, const vec &vec2) {
vec m(vec1.dim);
LOOP_OVER_DIRECTIONS(vec1.dim, d) {
m.set_direction(d, max(vec1.in_direction(d), vec2.in_direction(d)));
m.set_direction(d, std::max(vec1.in_direction(d), vec2.in_direction(d)));
}
return m;
}

ivec min(const ivec &ivec1, const ivec &ivec2) {
ivec m(ivec1.dim);
LOOP_OVER_DIRECTIONS(ivec1.dim, d) {
m.set_direction(d, min(ivec1.in_direction(d), ivec2.in_direction(d)));
m.set_direction(d, std::min(ivec1.in_direction(d), ivec2.in_direction(d)));
}
return m;
}

ivec max(const ivec &ivec1, const ivec &ivec2) {
ivec m(ivec1.dim);
LOOP_OVER_DIRECTIONS(ivec1.dim, d) {
m.set_direction(d, max(ivec1.in_direction(d), ivec2.in_direction(d)));
m.set_direction(d, std::max(ivec1.in_direction(d), ivec2.in_direction(d)));
}
return m;
}
Expand Down Expand Up @@ -195,16 +195,16 @@ double volume::full_volume() const {

double volume::diameter() const {
double diam = 0.0;
LOOP_OVER_DIRECTIONS(dim, d) { diam = max(diam, in_direction(d)); }
LOOP_OVER_DIRECTIONS(dim, d) { diam = std::max(diam, in_direction(d)); }
return diam;
}

volume volume::intersect_with(const volume &a) const {
if (a.dim != dim) meep::abort("Can't intersect volumes of dissimilar dimensions.\n");
volume result(dim);
LOOP_OVER_DIRECTIONS(dim, d) {
double minval = max(in_direction_min(d), a.in_direction_min(d));
double maxval = min(in_direction_max(d), a.in_direction_max(d));
double minval = std::max(in_direction_min(d), a.in_direction_min(d));
double maxval = std::min(in_direction_max(d), a.in_direction_max(d));
if (minval > maxval) return volume(zero_vec(dim), zero_vec(dim));
result.set_direction_min(d, minval);
result.set_direction_max(d, maxval);
Expand All @@ -215,8 +215,8 @@ volume volume::intersect_with(const volume &a) const {
bool volume::intersects(const volume &a) const {
if (a.dim != dim) meep::abort("Can't intersect volumes of dissimilar dimensions.\n");
LOOP_OVER_DIRECTIONS(dim, d) {
double minval = max(in_direction_min(d), a.in_direction_min(d));
double maxval = min(in_direction_max(d), a.in_direction_max(d));
double minval = std::max(in_direction_min(d), a.in_direction_min(d));
double maxval = std::min(in_direction_max(d), a.in_direction_max(d));
if (minval > maxval) return false;
}
return true;
Expand Down Expand Up @@ -757,8 +757,8 @@ bool grid_volume::intersect_with(const grid_volume &vol_in, grid_volume *interse
int temp_num[3] = {0, 0, 0};
ivec new_io(dim);
LOOP_OVER_DIRECTIONS(dim, d) {
int minval = max(little_corner().in_direction(d), vol_in.little_corner().in_direction(d));
int maxval = min(big_corner().in_direction(d), vol_in.big_corner().in_direction(d));
int minval = std::max(little_corner().in_direction(d), vol_in.little_corner().in_direction(d));
int maxval = std::min(big_corner().in_direction(d), vol_in.big_corner().in_direction(d));
if (minval >= maxval) return false;
temp_num[d % 3] = (maxval - minval) / 2;
new_io.set_direction(d, minval);
Expand Down Expand Up @@ -825,7 +825,7 @@ vec grid_volume::loc_at_resolution(ptrdiff_t index, double res) const {
const direction d = (direction)dd;
if (has_boundary(High, d)) {
const double dist = boundary_location(High, d) - boundary_location(Low, d);
const int nhere = max(1, (int)floor(dist * res + 0.5));
const int nhere = std::max(1, (int)floor(dist * res + 0.5));
where.set_direction(d, origin.in_direction(d) + ((index % nhere) + 0.5) * (1.0 / res));
index /= nhere;
}
Expand All @@ -839,7 +839,7 @@ size_t grid_volume::ntot_at_resolution(double res) const {
if (has_boundary(High, (direction)d)) {
const double dist =
boundary_location(High, (direction)d) - boundary_location(Low, (direction)d);
mytot *= max(size_t(1), (size_t)(dist * res + 0.5));
mytot *= std::max(size_t(1), (size_t)(dist * res + 0.5));
}
return mytot;
}
Expand Down Expand Up @@ -1019,7 +1019,7 @@ void grid_volume::find_best_split(int desired_chunks, bool fragment_cost,
std::complex<double> costs = get_split_costs(d, split_point, fragment_cost);
double left_cost = real(costs), right_cost = imag(costs);
double total_cost = left_cost + right_cost;
double split_measure = max(left_cost / (desired_chunks / 2), right_cost / (desired_chunks - (desired_chunks / 2)));
double split_measure = std::max(left_cost / (desired_chunks / 2), right_cost / (desired_chunks - (desired_chunks / 2)));
// Give a 30% preference to the longest axis, as a heuristic to prefer lower communication costs
// when the split_measure is somewhat close. TODO: use a data-driven communication cost function.
if (d == longest_axis) split_measure *= 0.7;
Expand Down
16 changes: 9 additions & 7 deletions tests/h5test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include "config.h"
using namespace meep;
using std::complex;
using std::max;
using std::min;

const double xsize = 2.0;
const double ysize = 2.0;
Expand Down Expand Up @@ -157,9 +159,9 @@ bool check_2d(double eps(const vec &), double a, int splitting, symfunc Sf, doub

double err =
compare(h5data[idx], get_reim(f.get_field(cs, loc) * ph, reim), name, i0, i1, 0);
err_max = max(err, err_max);
data_min = min(data_min, h5data[idx]);
data_max = max(data_max, h5data[idx]);
err_max = max<double>(err, err_max);
data_min = min<double>(data_min, h5data[idx]);
data_max = max<double>(data_max, h5data[idx]);
}
}
delete[] h5data;
Expand Down Expand Up @@ -269,8 +271,8 @@ bool check_3d(double eps(const vec &), double a, int splitting, symfunc Sf, comp
double err =
compare(h5data[idx], get_reim(f.get_field(cs, loc) * ph, reim), name, i0, i1, i2);
err_max = max(err, err_max);
data_min = min(data_min, h5data[idx]);
data_max = max(data_max, h5data[idx]);
data_min = min<double>(data_min, h5data[idx]);
data_max = max<double>(data_max, h5data[idx]);
}
}
}
Expand Down Expand Up @@ -348,8 +350,8 @@ bool check_2d_monitor(double eps(const vec &), double a, int splitting, symfunc
for (int i = 0; i < f.t; ++i) {
double err = compare(h5data[i], get_reim(mon[i], reim), name, i, 0, 0);
err_max = max(err, err_max);
data_min = min(data_min, h5data[i]);
data_max = max(data_max, h5data[i]);
data_min = min<double>(data_min, h5data[i]);
data_max = max<double>(data_max, h5data[i]);
}
delete[] h5data;
}
Expand Down

0 comments on commit 9509547

Please sign in to comment.