Actual source code: curand2.cu

  1: #include <petsc/private/randomimpl.h>
  2: #include <thrust/tuple.h>
  3: #include <thrust/transform.h>
  4: #include <thrust/device_ptr.h>
  5: #include <thrust/iterator/counting_iterator.h>

  7: #if defined(PETSC_USE_COMPLEX)
  8: struct complexscalelw
  9:   #if PETSC_PKG_CUDA_VERSION_LT(12, 8, 0)
 10:   :
 11:   public thrust::unary_function<thrust::tuple<PetscReal, size_t>, PetscReal>
 12:   #endif
 13: {
 14:   PetscReal rl, rw;
 15:   PetscReal il, iw;

 17:   complexscalelw(PetscScalar low, PetscScalar width)
 18:   {
 19:     rl = PetscRealPart(low);
 20:     il = PetscImaginaryPart(low);
 21:     rw = PetscRealPart(width);
 22:     iw = PetscImaginaryPart(width);
 23:   }

 25:   __host__ __device__ PetscReal operator()(thrust::tuple<PetscReal, size_t> x) { return thrust::get<1>(x) % 2 ? thrust::get<0>(x) * iw + il : thrust::get<0>(x) * rw + rl; }
 26: };
 27: #endif

 29: struct realscalelw
 30: #if PETSC_PKG_CUDA_VERSION_LT(12, 8, 0) // To suppress the warning "thrust::THRUST_200700_860_NS::unary_function is deprecated"
 31:   :
 32:   public thrust::unary_function<PetscReal, PetscReal>
 33: #endif
 34: {
 35:   PetscReal l, w;

 37:   realscalelw(PetscReal low, PetscReal width) : l(low), w(width) { }

 39:   __host__ __device__ PetscReal operator()(PetscReal x) { return x * w + l; }
 40: };

 42: PETSC_INTERN PetscErrorCode PetscRandomCurandScale_Private(PetscRandom r, size_t n, PetscReal *val, PetscBool isneg)
 43: {
 44:   PetscFunctionBegin;
 45:   if (!r->iset) PetscFunctionReturn(PETSC_SUCCESS);
 46:   if (isneg) { /* complex case, need to scale differently */
 47: #if defined(PETSC_USE_COMPLEX)
 48:     thrust::device_ptr<PetscReal> pval  = thrust::device_pointer_cast(val);
 49:     auto                          zibit = thrust::make_zip_iterator(thrust::make_tuple(pval, thrust::counting_iterator<size_t>(0)));
 50:     thrust::transform(zibit, zibit + n, pval, complexscalelw(r->low, r->width));
 51: #else
 52:     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Negative array size %" PetscInt_FMT, (PetscInt)n);
 53: #endif
 54:   } else {
 55:     PetscReal                     rl   = PetscRealPart(r->low);
 56:     PetscReal                     rw   = PetscRealPart(r->width);
 57:     thrust::device_ptr<PetscReal> pval = thrust::device_pointer_cast(val);
 58:     thrust::transform(pval, pval + n, pval, realscalelw(rl, rw));
 59:   }
 60:   PetscFunctionReturn(PETSC_SUCCESS);
 61: }