Skip to content
This repository was archived by the owner on Sep 30, 2022. It is now read-only.

Commit 4860038

Browse files
authored
Merge pull request #1300 from thananon/p-fix_random
libevent, romio: use opal_random() instead of rand(3)
2 parents d7db39c + ee6240d commit 4860038

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

ompi/mca/io/romio314/romio/adio/common/shfp_fname.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@
1717
#ifdef HAVE_TIME_H
1818
#include <time.h>
1919
#endif
20+
21+
/*
22+
* Open MPI: we have to use internal opal_random() instead of rand(3)
23+
* to prevent pertubing user's randon seed
24+
*/
25+
#include <opal/util/alfg.h>
26+
2027
/* The following function selects the name of the file to be used to
2128
store the shared file pointer. The shared-file-pointer file is a
2229
hidden file in the same directory as the real file being accessed.
@@ -35,12 +42,18 @@ void ADIOI_Shfp_fname(ADIO_File fd, int rank, int *error_code)
3542
int len;
3643
char *slash, *ptr, tmp[128];
3744
int pid = 0;
45+
opal_rng_buff_t adio_rand_buff;
3846

3947
fd->shared_fp_fname = (char *) ADIOI_Malloc(PATH_MAX);
4048

4149
if (!rank) {
42-
srand(time(NULL));
43-
i = rand();
50+
/*
51+
* Open MPI: we have to use internal opal_random() instead of rand(3)
52+
* to prevent pertubing user's randon seed
53+
*/
54+
opal_srand(&adio_rand_buff,time(NULL));
55+
i = opal_random();
56+
4457
pid = (int)getpid();
4558

4659
if (ADIOI_Strncpy(fd->shared_fp_fname, fd->filename, PATH_MAX)) {

opal/mca/event/libevent2022/configure.m4

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

159159
AC_MSG_RESULT([$event_args])
160160

161+
# We define "random" to be "opal_random" so that Libevent will not
162+
# use random(3) internally (and potentially unexpectedly perturb
163+
# values returned by rand(3) to the application).
164+
165+
CPPFLAGS="$CPPFLAGS -Drandom=opal_random"
161166
OPAL_CONFIG_SUBDIR([$libevent_basedir/libevent],
162-
[$event_args $opal_subdir_args],
167+
[$event_args $opal_subdir_args 'CPPFLAGS=$CPPFLAGS'],
163168
[libevent_happy="yes"], [libevent_happy="no"])
164169
if test "$libevent_happy" = "no"; then
165170
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)