Skip to content

Commit c30a716

Browse files
authored
Merge pull request #8553 from hjelmn/opal_min_max
opal: add min and max implementations
2 parents 8f8e9b8 + f79e6ff commit c30a716

File tree

5 files changed

+106
-33
lines changed

5 files changed

+106
-33
lines changed

ompi/mca/mtl/psm2/mtl_psm2.c

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -250,14 +250,6 @@ ompi_mtl_psm2_connect_error_msg(psm2_error_t err)
250250
}
251251
}
252252

253-
#ifndef min
254-
# define min(a,b) ((a) < (b) ? (a) : (b))
255-
#endif
256-
257-
#ifndef max
258-
# define max(a,b) ((a) > (b) ? (a) : (b))
259-
#endif
260-
261253
int
262254
ompi_mtl_psm2_add_procs(struct mca_mtl_base_module_t *mtl,
263255
size_t nprocs,
@@ -312,7 +304,7 @@ ompi_mtl_psm2_add_procs(struct mca_mtl_base_module_t *mtl,
312304
mask_in[i] = 1;
313305
}
314306

315-
timeout_in_secs = max(ompi_mtl_psm2.connect_timeout, 0.5 * nprocs);
307+
timeout_in_secs = opal_max(ompi_mtl_psm2.connect_timeout, 0.5 * nprocs);
316308

317309
psm2_error_register_handler(ompi_mtl_psm2.ep, PSM2_ERRHANDLER_NOP);
318310

opal/mca/base/mca_base_pvar.c

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,14 @@
2525

2626
#include "opal/class/opal_pointer_array.h"
2727
#include "opal/class/opal_hash_table.h"
28+
#include "opal/util/minmax.h"
2829
#include "opal/util/printf.h"
2930

3031
static opal_hash_table_t mca_base_pvar_index_hash;
3132
static opal_pointer_array_t registered_pvars;
3233
static bool mca_base_pvar_initialized = false;
3334
static int pvar_count = 0;
3435

35-
#define min(a,b) ((a) < (b) ? (a) : (b))
36-
#define max(a,b) ((a) > (b) ? (a) : (b))
37-
3836
static int mca_base_pvar_get_internal (int index, mca_base_pvar_t **pvar, bool invalidok);
3937

4038
/* string representations of class names */
@@ -610,20 +608,20 @@ int mca_base_pvar_handle_update (mca_base_pvar_handle_t *handle)
610608
if (MCA_BASE_PVAR_CLASS_LOWWATERMARK == handle->pvar->var_class) {
611609
switch (handle->pvar->type) {
612610
case MCA_BASE_VAR_TYPE_UNSIGNED_INT:
613-
((unsigned *) handle->current_value)[i] = min(((unsigned *) handle->tmp_value)[i],
614-
((unsigned *) handle->current_value)[i]);
611+
((unsigned *) handle->current_value)[i] = opal_min(((unsigned *) handle->tmp_value)[i],
612+
((unsigned *) handle->current_value)[i]);
615613
break;
616614
case MCA_BASE_VAR_TYPE_UNSIGNED_LONG:
617-
((unsigned long *) handle->current_value)[i] = min(((unsigned long *) handle->tmp_value)[i],
618-
((unsigned long *) handle->current_value)[i]);
615+
((unsigned long *) handle->current_value)[i] = opal_min(((unsigned long *) handle->tmp_value)[i],
616+
((unsigned long *) handle->current_value)[i]);
619617
break;
620618
case MCA_BASE_VAR_TYPE_UNSIGNED_LONG_LONG:
621-
((unsigned long long *) handle->current_value)[i] = min(((unsigned long long *) handle->tmp_value)[i],
622-
((unsigned long long *) handle->current_value)[i]);
619+
((unsigned long long *) handle->current_value)[i] = opal_min(((unsigned long long *) handle->tmp_value)[i],
620+
((unsigned long long *) handle->current_value)[i]);
623621
break;
624622
case MCA_BASE_VAR_TYPE_DOUBLE:
625-
((double *) handle->current_value)[i] = min(((double *) handle->tmp_value)[i],
626-
((double *) handle->current_value)[i]);
623+
((double *) handle->current_value)[i] = opal_min(((double *) handle->tmp_value)[i],
624+
((double *) handle->current_value)[i]);
627625
break;
628626
default:
629627
/* shouldn't happen */
@@ -632,20 +630,20 @@ int mca_base_pvar_handle_update (mca_base_pvar_handle_t *handle)
632630
} else {
633631
switch (handle->pvar->type) {
634632
case MCA_BASE_VAR_TYPE_UNSIGNED_INT:
635-
((unsigned *) handle->current_value)[i] = max(((unsigned *) handle->tmp_value)[i],
636-
((unsigned *) handle->current_value)[i]);
633+
((unsigned *) handle->current_value)[i] = opal_max(((unsigned *) handle->tmp_value)[i],
634+
((unsigned *) handle->current_value)[i]);
637635
break;
638636
case MCA_BASE_VAR_TYPE_UNSIGNED_LONG:
639-
((unsigned long *) handle->current_value)[i] = max(((unsigned long *) handle->tmp_value)[i],
640-
((unsigned long *) handle->current_value)[i]);
637+
((unsigned long *) handle->current_value)[i] = opal_max(((unsigned long *) handle->tmp_value)[i],
638+
((unsigned long *) handle->current_value)[i]);
641639
break;
642640
case MCA_BASE_VAR_TYPE_UNSIGNED_LONG_LONG:
643-
((unsigned long long *) handle->current_value)[i] = max(((unsigned long long *) handle->tmp_value)[i],
644-
((unsigned long long *) handle->current_value)[i]);
641+
((unsigned long long *) handle->current_value)[i] = opal_max(((unsigned long long *) handle->tmp_value)[i],
642+
((unsigned long long *) handle->current_value)[i]);
645643
break;
646644
case MCA_BASE_VAR_TYPE_DOUBLE:
647-
((double *) handle->current_value)[i] = max(((double *) handle->tmp_value)[i],
648-
((double *) handle->current_value)[i]);
645+
((double *) handle->current_value)[i] = opal_max(((double *) handle->tmp_value)[i],
646+
((double *) handle->current_value)[i]);
649647
break;
650648
default:
651649
/* shouldn't happen */

opal/util/Makefile.am

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
# Copyright (c) 2016-2017 IBM Corporation. All rights reserved.
2020
# Copyright (c) 2020 Amazon.com, Inc. or its affiliates.
2121
# All Rights reserved.
22+
# Copyright (c) 2021 Google, LLC. All rights reserved.
2223
# $COPYRIGHT$
2324
#
2425
# Additional copyrights may follow
@@ -78,7 +79,8 @@ headers = \
7879
timings.h \
7980
uri.h \
8081
info_subscriber.h \
81-
info.h
82+
info.h \
83+
minmax.h
8284

8385
libopalutil_la_SOURCES = \
8486
$(headers) \

opal/util/minmax.h

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
2+
/*
3+
* Copyright (c) 2021 Google, LLC. All rights reserved.
4+
* $COPYRIGHT$
5+
*
6+
* Additional copyrights may follow
7+
*
8+
* $HEADER$
9+
*
10+
* @file
11+
*/
12+
13+
/**
14+
* @file Contains implementations of opal_min(a,b) and opal_max(a,b).
15+
*/
16+
#ifndef OPAL_MINMAX_H
17+
#define OPAL_MINMAX_H
18+
19+
#define OPAL_DEFINE_MINMAX(type, suffix) \
20+
static inline const type opal_min_##suffix(const type a, const type b) \
21+
{ \
22+
return (a < b) ? a : b; \
23+
} \
24+
static inline const type opal_max_##suffix(const type a, const type b) \
25+
{ \
26+
return (a > b) ? a : b; \
27+
}
28+
29+
OPAL_DEFINE_MINMAX(int8_t, 8)
30+
OPAL_DEFINE_MINMAX(uint8_t, u8)
31+
OPAL_DEFINE_MINMAX(int16_t, 16)
32+
OPAL_DEFINE_MINMAX(uint16_t, u16)
33+
OPAL_DEFINE_MINMAX(int32_t, 32)
34+
OPAL_DEFINE_MINMAX(uint32_t, u32)
35+
OPAL_DEFINE_MINMAX(int64_t, 64)
36+
OPAL_DEFINE_MINMAX(uint64_t, u64)
37+
OPAL_DEFINE_MINMAX(size_t, size_t)
38+
OPAL_DEFINE_MINMAX(ssize_t, ssize_t)
39+
OPAL_DEFINE_MINMAX(float, float)
40+
OPAL_DEFINE_MINMAX(double, double)
41+
OPAL_DEFINE_MINMAX(void *, ptr)
42+
43+
#if OPAL_C_HAVE__GENERIC
44+
#define opal_min(a, b) \
45+
(_Generic((a) + (b), \
46+
int8_t: opal_min_8, \
47+
uint8_t: opal_min_u8, \
48+
int16_t: opal_min_16, \
49+
uint16_t: opal_min_u16, \
50+
int32_t: opal_min_32, \
51+
uint32_t: opal_min_u32, \
52+
int64_t: opal_min_64, \
53+
uint64_t: opal_min_u64, \
54+
float: opal_min_float, \
55+
double: opal_min_double, \
56+
void *: opal_min_ptr, \
57+
default: opal_min_64)((a), (b)))
58+
59+
#define opal_max(a, b) \
60+
(_Generic((a) + (b), \
61+
int8_t: opal_max_8, \
62+
uint8_t: opal_max_u8, \
63+
int16_t: opal_max_16, \
64+
uint16_t: opal_max_u16, \
65+
int32_t: opal_max_32, \
66+
uint32_t: opal_max_u32, \
67+
int64_t: opal_max_64, \
68+
uint64_t: opal_max_u64, \
69+
float: opal_max_float, \
70+
double: opal_max_double, \
71+
void *: opal_max_ptr, \
72+
default: opal_max_64)((a), (b)))
73+
#else
74+
75+
/* these versions suffer from double-evaluation. please upgrade to a modern compiler */
76+
77+
#define opal_min(a, b) (((a) < (b)) ? (a) : (b))
78+
#define opal_max(a, b) (((a) > (b)) ? (a) : (b))
79+
80+
#endif
81+
82+
#endif /* OPAL_MINMAX_H */

opal/util/qsort.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,13 @@
3737

3838
#include <stdlib.h>
3939

40+
#include "opal/util/minmix.h"
4041
#include "opal/util/qsort.h"
4142

4243
typedef int cmp_t(const void *, const void *);
4344
static inline char *med3(char *, char *, char *, cmp_t *, void *);
4445
static inline void swapfunc(char *, char *, int, int);
4546

46-
#define min(a, b) (a) < (b) ? a : b
47-
4847
/*
4948
* Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
5049
*/
@@ -160,9 +159,9 @@ loop: SWAPINIT(a, es);
160159
}
161160

162161
pn = (char *)a + n * es;
163-
r = (int) min(pa - (char *)a, pb - pa);
162+
r = (int) opal_min(pa - (char *)a, pb - pa);
164163
vecswap(a, pb - r, r);
165-
r = (int) (min((char*) (pd - pc), (char*) (pn - pd - es)));
164+
r = (int) (opal_min((char*) (pd - pc), (char*) (pn - pd - es)));
166165
vecswap(pb, pn - r, r);
167166
if ((size_t) (r = pb - pa) > es)
168167
opal_qsort(a, r / es, es, cmp);

0 commit comments

Comments
 (0)