# Is there round(..., digits=2) in CUDA?

**URL:** <https://discourse.julialang.org/t/is-there-round-digits-2-in-cuda/108450>\
**Category:** GPU\
**Created:** [January 7, 2024, 12:05am UTC](https://discourse.julialang.org/t/is-there-round-digits-2-in-cuda/108450 "2024-01-07T00:05:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![BMval](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bmval/32/7647_2.png) [@BMval](https://discourse.julialang.org/u/BMval)\
**Post date:** [January 7, 2024, 12:05am UTC](https://discourse.julialang.org/t/is-there-round-digits-2-in-cuda/108450/1 "2024-01-07T00:05:26Z")

</div>

I would like to use the function round(a, digits=2) on the GPU, but it seems that this is not possible.

Is there another function for this purpose?

Thank you in advance

---

<div class="post-metadata">

**Author:** ![rmsmsgood](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rmsmsgood/32/20544_2.png) [@rmsmsgood](https://discourse.julialang.org/u/rmsmsgood)\
**Post date:** [January 7, 2024, 7:13am UTC](https://discourse.julialang.org/t/is-there-round-digits-2-in-cuda/108450/2 "2024-01-07T07:13:44Z")

</div>

How about this? I prefer trick of this kind:

```julia
roundigit2(x) = round(Int32, 100x) / 100

```

```julia
julia> using CUDA

julia> roundigit2(x) = round(Int32, 100x) / 100
roundigit2 (generic function with 1 method)

julia> X = CuArray(randn(100))
100-element CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}:
  0.9393934792087492
  0.6900935059439155
 -0.8939959207450046
 -1.8878733700288313
  ⋮
  0.0561743482887146
 -1.4615333912881514
 -0.4060048468494186
  0.8475641383618252

julia> roundigit2.(X)
100-element CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}:
  0.94
  0.69
 -0.89
 -1.89
  ⋮
  0.06
 -1.46
 -0.41
  0.85

```

---

<div class="post-metadata">

**Author:** ![maleadt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maleadt/32/10097_2.png) [@maleadt](https://discourse.julialang.org/u/maleadt)\
**Post date:** [January 7, 2024, 7:51am UTC](https://discourse.julialang.org/t/is-there-round-digits-2-in-cuda/108450/3 "2024-01-07T07:51:14Z")

</div>

> [@BMval](#):
>
> I would like to use the function round(a, digits=2) on the GPU, but it seems that this is not possible.

Why not?

```julia
julia> a = CUDA.rand(2)
2-element CuArray{Float32, 1, CUDA.Mem.DeviceBuffer}:
 0.30272454
 0.8437567

julia> function kernel(a,b)
       a[threadIdx().x] = round(b[threadIdx().x]; digits=2)
       return
       end

julia> b = similar(a)
2-element CuArray{Float32, 1, CUDA.Mem.DeviceBuffer}:
 0.0
 0.0

julia> @cuda kernel(b,a)
CUDA.HostKernel for kernel(CuDeviceVector{Float32, 1}, CuDeviceVector{Float32, 1})

julia> b
2-element CuArray{Float32, 1, CUDA.Mem.DeviceBuffer}:
 0.3
 0.0

```

You should include source code and error messages when opening topics like this.
