Skip to content

Commit 3b40479

Browse files
committed
Add random_init() and testcase.
1 parent 2a8134d commit 3b40479

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,9 @@ if(opencoarrays_aware_compiler)
782782
message( AUTHOR_WARNING "Skipping the following test to GFortran < 7.4.0 lack of compatibility:
783783
send-strided-self.f90")
784784
endif()
785+
if((NOT CMAKE_Fortran_COMPILER_VERSION VERSION_LESS 12.0.0) OR (CAF_RUN_DEVELOPER_TESTS OR $ENV{OPENCOARRAYS_DEVELOPER}))
786+
add_caf_test(random_init 4 random_init)
787+
endif()
785788
endif()
786789

787790
# Pure get tests

src/libcaf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,8 @@ void PREFIX (event_post) (caf_token_t, size_t, int, int *, char *, charlen_t);
331331
void PREFIX (event_wait) (caf_token_t, size_t, int, int *, char *, charlen_t);
332332
void PREFIX (event_query) (caf_token_t, size_t, int, int *, int *);
333333

334+
void PREFIX (random_init) (bool, bool);
335+
334336
/* Language extension */
335337
#ifdef HAVE_MPI
336338
MPI_Fint PREFIX (get_communicator) (caf_team_t *);

src/mpi/mpi_caf.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8491,3 +8491,73 @@ void PREFIX(sync_team) (caf_team_t *team , int unused __attribute__((unused)))
84918491

84928492
int ierr = MPI_Barrier(*tmp_comm); chk_err(ierr);
84938493
}
8494+
8495+
extern void _gfortran_random_seed_i4 (int32_t *size, gfc_dim1_descriptor_t *put,
8496+
gfc_dim1_descriptor_t *get);
8497+
8498+
void PREFIX(random_init) (bool repeatable, bool image_distinct)
8499+
{
8500+
static gfc_dim1_descriptor_t rand_seed;
8501+
static bool rep_needs_init = true, arr_needs_init = true;
8502+
static int32_t seed_size;
8503+
8504+
if (arr_needs_init)
8505+
{
8506+
_gfortran_random_seed_i4(&seed_size, NULL, NULL);
8507+
memset(&rand_seed, 0, sizeof(gfc_dim1_descriptor_t));
8508+
rand_seed.base.base_addr = malloc(seed_size * sizeof(int32_t)); // because using seed_i4
8509+
rand_seed.base.offset = -1;
8510+
rand_seed.base.dtype.elem_len = sizeof(int32_t);
8511+
rand_seed.base.dtype.rank = 1;
8512+
rand_seed.base.dtype.type = BT_INTEGER;
8513+
rand_seed.base.span = 0;
8514+
rand_seed.dim[0].lower_bound = 1;
8515+
rand_seed.dim[0]._ubound = seed_size;
8516+
rand_seed.dim[0]._stride = 1;
8517+
8518+
arr_needs_init = false;
8519+
}
8520+
8521+
if (repeatable)
8522+
{
8523+
if (rep_needs_init)
8524+
{
8525+
int32_t lcg_seed = 57911963;
8526+
if (image_distinct)
8527+
{
8528+
lcg_seed *= caf_this_image;
8529+
}
8530+
int32_t *curr = rand_seed.base.base_addr;
8531+
for (int i = 0; i < seed_size; ++i)
8532+
{
8533+
const int32_t a = 16087;
8534+
const int32_t m = INT32_MAX;
8535+
const int32_t q = 127773;
8536+
const int32_t r = 2836;
8537+
lcg_seed = a * (lcg_seed % q) - r * (lcg_seed / q);
8538+
if (lcg_seed <= 0) lcg_seed += m;
8539+
*curr = lcg_seed;
8540+
++curr;
8541+
}
8542+
rep_needs_init = false;
8543+
}
8544+
_gfortran_random_seed_i4(NULL, &rand_seed, NULL);
8545+
}
8546+
else if (image_distinct)
8547+
{
8548+
_gfortran_random_seed_i4(NULL, NULL, NULL);
8549+
}
8550+
else
8551+
{
8552+
if (caf_this_image == 0)
8553+
{
8554+
_gfortran_random_seed_i4(NULL, NULL, &rand_seed);
8555+
MPI_Bcast(rand_seed.base.base_addr, seed_size, MPI_INT32_T, 0, CAF_COMM_WORLD);
8556+
}
8557+
else
8558+
{
8559+
MPI_Bcast(rand_seed.base.base_addr, seed_size, MPI_INT32_T, 0, CAF_COMM_WORLD);
8560+
_gfortran_random_seed_i4(NULL, &rand_seed, NULL);
8561+
}
8562+
}
8563+
}

src/tests/unit/simple/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
caf_compile_executable(increment_my_neighbor increment_neighbor.f90)
44
caf_compile_executable(atomics testAtomics.f90)
5+
caf_compile_executable(random_init random_init.f90)
56

67
# C tests
78
#include(CMakeForceCompiler)

0 commit comments

Comments
 (0)