# Factorial on GPU

**URL:** <https://discourse.julialang.org/t/factorial-on-gpu/73116>\
**Category:** GPU\
**Created:** [December 15, 2021, 5:11am UTC](https://discourse.julialang.org/t/factorial-on-gpu/73116 "2021-12-15T05:11:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeremiedb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeremiedb/32/29150_2.png) [@jeremiedb](https://discourse.julialang.org/u/jeremiedb)\
**Post date:** [December 15, 2021, 5:11am UTC](https://discourse.julialang.org/t/factorial-on-gpu/73116/1 "2021-12-15T05:11:37Z")

</div>

I wanted to perform a factorial calculation within a kernel for implementing a statistical measure (Poisson likelihood) on GPU,  
It turns out like `factorial` isn’t supported on GPU:

```julia
using CUDA

function gpu_fac(y, x)
    for i = 1:length(y)
        @inbounds y[i] += factorial(x[i])
    end
    return nothing
end

N = 10
x = CUDA.fill(3, N)
y = CUDA.fill(1, N)

@cuda gpu_fac(y, x)

ERROR: LoadError: InvalidIRError: compiling kernel gpu_fac(CuDeviceVector{Int64, 1}, CuDeviceVector{Int64, 1}) resulted in invalid LLVM IR

```

Would it makes sense to have `factorial` support on the GPU?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [December 15, 2021, 6:07am UTC](https://discourse.julialang.org/t/factorial-on-gpu/73116/2 "2021-12-15T06:07:23Z")

</div>

not really, for integers, there are less than 20 numbers that fits in the range (depending on if you pick Int32 or Int64). Besides, 20 scalar multiplication is definitely not worth going to GPU by any means.

(if you’re thinking using floating numbers, you’re also out of luck:

```julia
julia> reduce(*, 1:1.0:25) |> BigInt
15511210043330986055303168

julia> factorial(BigInt(25))
15511210043330985984000000

```
