Hipace
ShuffleFisherYates.H
Go to the documentation of this file.
1 /* Copyright 2019 Yinjian Zhao
2  *
3  * This file is part of WarpX.
4  *
5  * License: BSD-3-Clause-LBNL
6  */
7 #ifndef HIPACE_SHUFFLE_FISHER_YATES_H_
8 #define HIPACE_SHUFFLE_FISHER_YATES_H_
9 
10 #include <AMReX_Random.H>
11 
12 /* \brief Shuffle array according to Fisher-Yates algorithm.
13  * Only shuffle the part between is <= i < ie, n = ie-is.
14  * T_index shall be
15  * amrex::DenseBins<WarpXParticleContainer::ParticleType>::index_type
16  */
17 
18 template <typename T_index>
20 void ShuffleFisherYates (T_index *array, T_index const is, T_index const ie,
21  amrex::RandomEngine const& engine)
22 {
23  int j;
24  T_index buf;
25  for (int i = ie-1; i >= static_cast<int>(is+1); --i)
26  {
27  // get random number j: is <= j <= i
28  j = amrex::Random_int(i-is+1, engine) + is;
29  // swop the ith array element with the jth
30  buf = array[i];
31  array[i] = array[j];
32  array[j] = buf;
33  }
34 }
35 
36 #endif // HIPACE_SHUFFLE_FISHER_YATES_H_
#define AMREX_INLINE
#define AMREX_GPU_HOST_DEVICE
AMREX_GPU_HOST_DEVICE AMREX_INLINE void ShuffleFisherYates(T_index *array, T_index const is, T_index const ie, amrex::RandomEngine const &engine)
Definition: ShuffleFisherYates.H:20
int i
Definition: MakeOpenBoundary.py:152
j
Definition: MakeOpenBoundary.py:128
unsigned int Random_int(unsigned int n)