Commit 8dab5618 authored by Thomas Jahns's avatar Thomas Jahns 🤸
Browse files

Split out random number generator interface from ppm_base.

* This is meant to reduce the dependencies of ppm_base.
parent 76e2450c
......@@ -32,6 +32,7 @@ if FC_MOD_UPPERCASE
PPM_MOD_NAME=SCALES_PPM
PPM_GRAPH_PARTITION_MPI_MOD_NAME=PPM_GRAPH_PARTITION_MPI
PPM_BASE_MOD_NAME=PPM_BASE
PPM_RANDOM_MOD_NAME=PPM_RANDOM
PPM_IRAND_INTERNAL_MOD_NAME=PPM_IRAND_INTERNAL
PPM_EXTENTS_MOD_NAME=PPM_EXTENTS
PPM_POSIX_MOD_NAME=PPM_POSIX
......@@ -52,6 +53,7 @@ else
PPM_MOD_NAME=scales_ppm
PPM_GRAPH_PARTITION_MPI_MOD_NAME=ppm_graph_partition_mpi
PPM_BASE_MOD_NAME=ppm_base
PPM_RANDOM_MOD_NAME=ppm_random
PPM_IRAND_INTERNAL_MOD_NAME=ppm_irand_internal
PPM_EXTENTS_MOD_NAME=ppm_extents
PPM_POSIX_MOD_NAME=ppm_posix
......@@ -90,6 +92,10 @@ $(PPM_IRAND_INTERNAL_MOD_NAME).$(FCMODEXT): \
../../src/$(PPM_IRAND_INTERNAL_MOD_NAME).$(FCMODEXT)
$(LN_S) ../../src/$(PPM_IRAND_INTERNAL_MOD_NAME).$(FCMODEXT) .
$(PPM_RANDOM_MOD_NAME).$(FCMODEXT): \
../../src/$(PPM_RANDOM_MOD_NAME).$(FCMODEXT)
$(LN_S) ../../src/$(PPM_RANDOM_MOD_NAME).$(FCMODEXT) .
$(PPM_EXTENTS_MOD_NAME).$(FCMODEXT): \
../../src/$(PPM_EXTENTS_MOD_NAME).$(FCMODEXT)
$(LN_S) ../../src/$(PPM_EXTENTS_MOD_NAME).$(FCMODEXT) .
......@@ -143,6 +149,7 @@ $(SOLVER_ALL_MOD_NAME).$(FCMODEXT): ../../src/$(SOLVER_ALL_MOD_NAME).$(FCMODEXT)
include_HEADERS=$(PPM_MOD_NAME).$(FCMODEXT) \
$(PPM_BASE_MOD_NAME).$(FCMODEXT) \
$(PPM_RANDOM_MOD_NAME).$(FCMODEXT) \
$(PPM_IRAND_INTERNAL_MOD_NAME).$(FCMODEXT) \
$(PPM_EXTENTS_MOD_NAME).$(FCMODEXT) \
$(PPM_POSIX_MOD_NAME).$(FCMODEXT) \
......
......@@ -54,6 +54,7 @@ libscalesppm_a_SOURCES=ppm/scales_ppm.f90 \
core/ppm_statistics.f90 \
core/yarandom.c core/yarandom.h \
core/ppm_base.f90 core/ppm_irand_internal.f90 \
core/ppm_random.f90 core/ppm_random_c.c \
core/ppm_set_default_comm.f90 \
core/ppm_posix.f90 core/ppm_posix_c.c \
core/ppm_extents.f90 core/ppm_extents_c.c \
......
......@@ -53,13 +53,9 @@
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef _OPENMP
#include <omp.h>
#endif
#include <stdio.h>
#include <cfortran.h>
#include "core.h"
#include "yarandom.h"
MPI_Comm PPM_default_comm =
#ifdef USE_MPI
......@@ -133,43 +129,6 @@ PPM_abort_default(MPI_Comm *comm, const char *msg, const char *source, int line)
PPM_abort_func PPM_abort = PPM_abort_default;
int
PPM_irand()
{
uint32_t i = ppm_ya_random();
if (i == (uint32_t)-2147483648UL) i = (uint32_t)0U;
return (int)i;
}
FCALLSCFUN0(INT,PPM_irand,PPM_IRAND,ppm_irand)
void
PPM_irand_a(int *a, int n)
{
int i;
for (i = 0; i < n; ++i)
a[i] = PPM_irand();
}
FCALLSCSUB2(PPM_irand_a,PPM_IRAND_A,ppm_irand_a,PINT,INT)
static void
initIRand_f(MPI_Fint *comm_f, int random_seed)
{
MPI_Comm comm_c;
#if defined(USE_MPI) && defined(HAVE_MPI_COMM_F2C)
int flag = 0;
if (MPI_Initialized(&flag) == MPI_SUCCESS && flag)
comm_c = MPI_Comm_f2c((MPI_Fint)*comm_f);
#else
comm_c = *comm_f;
#endif
ppm_ya_rand_init(&comm_c, random_seed);
}
FCALLSCSUB2(initIRand_f,PPM_INITIALIZE_IRAND,ppm_initialize_irand,PVOID,INT)
/*
* Local Variables:
* license-project-url: "https://www.dkrz.de/redmine/projects/show/scales-ppm"
......
......@@ -43,9 +43,6 @@
! Code:
!
MODULE ppm_base
USE ppm_irand_internal, ONLY: irand => ppm_irand, &
initialize_irand => ppm_initialize_irand, &
a_rand
IMPLICIT NONE
PRIVATE
#ifdef USE_MPI
......@@ -65,7 +62,6 @@ MODULE ppm_base
END INTERFACE
PUBLIC :: ppm_default_comm, set_default_comm
PUBLIC :: abort_ppm, assertion
PUBLIC :: irand, initialize_irand, a_rand
CONTAINS
SUBROUTINE abort_ppm(msg, source, line, comm)
CHARACTER(len=*), INTENT(in) :: source, msg
......
!! @file ppm_random.f90
!! @brief gather interfaces to random numbers
!
! Copyright (C) 2011 Thomas Jahns <jahns@dkrz.de>
!
! @version 1.0
! Keywords:
! @author Thomas Jahns <jahns@dkrz.de>
! Maintainer: Thomas Jahns <jahns@dkrz.de>
! URL: https://www.dkrz.de/redmine/projects/show/scales-ppm
!
! Redistribution and use in source and binary forms, with or without
! modification, are permitted provided that the following conditions are
! met:
!
! Redistributions of source code must retain the above copyright notice,
! this list of conditions and the following disclaimer.
!
! Redistributions in binary form must reproduce the above copyright
! notice, this list of conditions and the following disclaimer in the
! documentation and/or other materials provided with the distribution.
!
! Neither the name of the DKRZ GmbH nor the names of its contributors
! may be used to endorse or promote products derived from this software
! without specific prior written permission.
!
! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
! IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
! TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
! PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
! OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
! EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
! PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
! LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
! NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
! SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
!
MODULE ppm_random
USE ppm_irand_internal, ONLY: irand => ppm_irand, &
initialize_irand => ppm_initialize_irand, &
a_rand
IMPLICIT NONE
PRIVATE
PUBLIC :: irand, initialize_irand, a_rand
END MODULE ppm_random
!
! Local Variables:
! license-project-url: "https://www.dkrz.de/redmine/projects/show/scales-ppm"
! license-markup: "doxygen"
! license-default: "bsd"
! End:
/*
* @file ppm_random_c.c
* @brief C routines to use pseudo-random number generator in Fortran
*
* Copyright (C) 2011 Thomas Jahns <jahns@dkrz.de>
*
* @version 1.0
* Keywords:
* @author Thomas Jahns <jahns@dkrz.de>
* Maintainer: Thomas Jahns <jahns@dkrz.de>
* URL: https://www.dkrz.de/redmine/projects/show/scales-ppm
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Neither the name of the DKRZ GmbH nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <cfortran.h>
#include "core.h"
#include "yarandom.h"
int
PPM_irand()
{
uint32_t i = ppm_ya_random();
if (i == (uint32_t)-2147483648UL) i = (uint32_t)0U;
return (int)i;
}
FCALLSCFUN0(INT,PPM_irand,PPM_IRAND,ppm_irand)
void
PPM_irand_a(int *a, int n)
{
int i;
for (i = 0; i < n; ++i)
a[i] = PPM_irand();
}
FCALLSCSUB2(PPM_irand_a,PPM_IRAND_A,ppm_irand_a,PINT,INT)
static void
initIRand_f(MPI_Fint *comm_f, int random_seed)
{
MPI_Comm comm_c;
#if defined(USE_MPI) && defined(HAVE_MPI_COMM_F2C)
int flag = 0;
if (MPI_Initialized(&flag) == MPI_SUCCESS && flag)
comm_c = MPI_Comm_f2c((MPI_Fint)*comm_f);
#else
comm_c = *comm_f;
#endif
ppm_ya_rand_init(&comm_c, random_seed);
}
FCALLSCSUB2(initIRand_f,PPM_INITIALIZE_IRAND,ppm_initialize_irand,PVOID,INT)
/*
* Local Variables:
* license-project-url: "https://www.dkrz.de/redmine/projects/show/scales-ppm"
* license-markup: "doxygen"
* license-default: "bsd"
* End:
*/
......@@ -47,7 +47,8 @@ MODULE scales_ppm
USE omp_lib
#endif
USE ppm_base, ONLY: abort_ppm, ppm_default_comm, set_default_comm, &
initialize_irand, irand, ppm_default_comm, assertion
ppm_default_comm, assertion
USE ppm_random, ONLY: initialize_irand, irand, a_rand
USE ppm_extents, ONLY: extent, extent_size, extent_end, &
extent_start, rebased_extent, &
extent_set_iinterval, extent_from_iinterval, char, iinterval, &
......
! test_extents.f90 --- unit tests for extents data type
!
! Copyright (C) 2011 Thomas Jahns <jahns@dkrz.de>
!
! Version: 1.0
! Keywords:
! Author: Thomas Jahns <jahns@dkrz.de>
! Maintainer: Thomas Jahns <jahns@dkrz.de>
! URL: https://www.dkrz.de/redmine/projects/show/scales-ppm
!
! Redistribution and use in source and binary forms, with or without
! modification, are permitted provided that the following conditions are
! met:
!
! Redistributions of source code must retain the above copyright notice,
! this list of conditions and the following disclaimer.
!
! Redistributions in binary form must reproduce the above copyright
! notice, this list of conditions and the following disclaimer in the
! documentation and/or other materials provided with the distribution.
!
! Neither the name of the DKRZ GmbH nor the names of its contributors
! may be used to endorse or promote products derived from this software
! without specific prior written permission.
!
! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
! IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
! TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
! PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
! OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
! EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
! PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
! LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
! NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
! SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
!
PROGRAM test_extents
USE ppm_extents, ONLY: ASSIGNMENT(=), extent, extent_size, iinterval, &
OPERATOR(==), extents_do_intersect, extent_intersect
USE ppm_base, ONLY: assertion, initialize_irand, irand, ppm_default_comm
USE ppm_base, ONLY: assertion, ppm_default_comm
USE ppm_random, ONLY: initialize_irand, irand
IMPLICIT NONE
INTEGER, PARAMETER :: rmod=1000, rstart_mod=10000, num_tests=10000
TYPE(extent) :: a(2)
......@@ -32,3 +70,8 @@ PROGRAM test_extents
__LINE__, "overlap size mismatch")
END DO
END PROGRAM test_extents
!
! Local Variables:
! license-project-url: "https://www.dkrz.de/redmine/projects/show/scales-ppm"
! license-default: "bsd"
! End:
! test_irand.f90 --- random number generator unit-tests
!
! Copyright (C) 2011 Thomas Jahns <jahns@dkrz.de>
!
! Version: 1.0
! Keywords:
! Author: Thomas Jahns <jahns@dkrz.de>
! Maintainer: Thomas Jahns <jahns@dkrz.de>
! URL: https://www.dkrz.de/redmine/projects/show/scales-ppm
!
! Redistribution and use in source and binary forms, with or without
! modification, are permitted provided that the following conditions are
! met:
!
! Redistributions of source code must retain the above copyright notice,
! this list of conditions and the following disclaimer.
!
! Redistributions in binary form must reproduce the above copyright
! notice, this list of conditions and the following disclaimer in the
! documentation and/or other materials provided with the distribution.
!
! Neither the name of the DKRZ GmbH nor the names of its contributors
! may be used to endorse or promote products derived from this software
! without specific prior written permission.
!
! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
! IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
! TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
! PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
! OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
! EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
! PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
! LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
! NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
! SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
!
PROGRAM test_irand
#ifdef _OPENMP
USE omp_lib
#endif
USE ppm_base, ONLY: irand, initialize_irand, ppm_default_comm, a_rand
USE ppm_base, ONLY: ppm_default_comm
USE ppm_random, ONLY: irand, initialize_irand, a_rand
IMPLICIT NONE
INTEGER, ALLOCATABLE :: irand_res(:, :)
INTEGER :: num_rands, num_threads, tid, i, ref
......@@ -17,6 +55,7 @@ PROGRAM test_irand
num_threads = 1
tid = 0
#endif
! test for reproducability in multi-thread setup
CALL initialize_irand(ppm_default_comm, 9)
!$omp master
num_rands = MOD(IAND(irand(), 2147483647), 10000)
......@@ -42,3 +81,8 @@ PROGRAM test_irand
STOP 1
END IF
END PROGRAM test_irand
!
! Local Variables:
! license-project-url: "https://www.dkrz.de/redmine/projects/show/scales-ppm"
! license-default: "bsd"
! End:
......@@ -35,14 +35,9 @@
! NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
! SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
!
! Commentary:
!
!
!
! Code:
!
PROGRAM test_qsort
USE ppm_base, ONLY: irand, initialize_irand, a_rand, ppm_default_comm
USE ppm_random, ONLY: irand, initialize_irand, a_rand
USE ppm_base, ONLY: ppm_default_comm
IMPLICIT NONE
INCLUDE 'ftype_size.inc'
INTEGER, PARAMETER :: maxasize = 30, minasize = 2
......
......@@ -35,15 +35,10 @@
! NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
! SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
!
! Commentary:
!
!
!
! Code:
!
PROGRAM test_uniform_partitions
USE ppm_base, ONLY: assertion, irand, initialize_irand, ppm_default_comm
USE ppm_base, ONLY: assertion, ppm_default_comm
USE ppm_extents, ONLY: extent_size, extent_start, extent_end, extent!, char
USE ppm_random, ONLY: irand, initialize_irand
USE ppm_uniform_partition, ONLY: uniform_partition_1d!, uniform_decomposition_2d
IMPLICIT NONE
INTEGER, PARAMETER :: maxsize = 1000000, maxparts=maxsize
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment