WarpX
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 WARPX_PARTICLES_COLLISION_SHUFFLE_FISHER_YATES_H_
8 #define WARPX_PARTICLES_COLLISION_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::ParticleTileType::ParticleTileDataType>::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  T_index buf;
24  for (int i = ie-1; i >= static_cast<int>(is+1); --i)
25  {
26  // get random number j: is <= j <= i
27  const int j = amrex::Random_int(i-is+1, engine) + is;
28  // swap the ith array element with the jth
29  buf = array[i];
30  array[i] = array[j];
31  array[j] = buf;
32  }
33 }
34 
35 #endif // WARPX_PARTICLES_COLLISION_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
unsigned int Random_int(unsigned int n)
i
Definition: check_interp_points_and_weights.py:174