Skip to content

Commit 36555b7

Browse files
authored
Merge pull request #1933 from thananon/fix_random
Make libevent use internal random
2 parents 085aef5 + b3e9dad commit 36555b7

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

opal/mca/event/libevent2022/configure.m4

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,13 @@ AC_DEFUN([MCA_opal_event_libevent2022_CONFIG],[
159159

160160
AC_MSG_RESULT([$event_args])
161161

162+
# We define "random" to be "opal_random" so that Libevent will not
163+
# use random(3) internally (and potentially unexpectedly perturb
164+
# values returned by rand(3) to the application).
165+
166+
CPPFLAGS="$CPPFLAGS -Drandom=opal_random"
162167
OPAL_CONFIG_SUBDIR([$libevent_basedir/libevent],
163-
[$event_args $opal_subdir_args],
168+
[$event_args $opal_subdir_args 'CPPFLAGS=$CPPFLAGS'],
164169
[libevent_happy="yes"], [libevent_happy="no"])
165170
if test "$libevent_happy" = "no"; then
166171
AC_MSG_WARN([Event library failed to configure])

opal/util/alfg.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#include "opal_config.h"
1212

13+
#include <string.h>
14+
1315
#include "alfg.h"
1416

1517
/* Mask corresponding to the primitive polynomial
@@ -52,6 +54,9 @@ static uint32_t galois(unsigned int *seed){
5254
return lsb;
5355
}
5456

57+
/* OPAL global rng buffer */
58+
static opal_rng_buff_t alfg_buffer;
59+
5560
/**
5661
* @brief Routine to seed the ALFG register
5762
*
@@ -80,6 +85,8 @@ int opal_srand(opal_rng_buff_t *buff, uint32_t seed) {
8085
buff->alfg[j] = buff->alfg[j] ^ ((galois(&seed_cpy))<<i);
8186
}
8287
}
88+
/* copy the ALFG to the global buffer */
89+
memcpy(&alfg_buffer, buff, sizeof(alfg_buffer));
8390

8491
return 1;
8592

@@ -114,4 +121,12 @@ uint32_t opal_rand(opal_rng_buff_t *buff){
114121

115122
}
116123

117-
124+
/**
125+
* @brief A wrapper for opal_rand() with our global ALFG buffer;
126+
*
127+
* @param[in] none
128+
* @param[out] int, the same as normal rand(3)
129+
*/
130+
int opal_random(void){
131+
return (int)opal_rand(&alfg_buffer);
132+
}

opal/util/alfg.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,6 @@ OPAL_DECLSPEC int opal_srand(opal_rng_buff_t *buff, uint32_t seed);
3232

3333
OPAL_DECLSPEC uint32_t opal_rand(opal_rng_buff_t *buff);
3434

35+
OPAL_DECLSPEC int opal_random(void);
36+
3537
#endif /* OPAL_ALFG_H */

0 commit comments

Comments
 (0)