# Can gpu calculation accelerates this function?

**URL:** <https://discourse.julialang.org/t/can-gpu-calculation-accelerates-this-function/88295>\
**Category:** New to Julia\
**Created:** [October 5, 2022, 2:03pm UTC](https://discourse.julialang.org/t/can-gpu-calculation-accelerates-this-function/88295 "2022-10-05T14:03:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [October 5, 2022, 2:03pm UTC](https://discourse.julialang.org/t/can-gpu-calculation-accelerates-this-function/88295/1 "2022-10-05T14:03:10Z")

</div>

I don’t have gpu on hands for now, and I am curious that weather this function can be accelerated by applying Gpu calculation. I used to think gpu can only speed up function like `+ - * ...`. In my experience, this kind of function consume very high `gc` time, so can gpu reduce the `gc` time too?

```julia
function test_func(n::Int)
    bit_set_vec = [BitSet() for i = 1 : n]
    bit_set = BitSet([1,2,3])
    for i = 1 : n
        bit_set_i = bit_set_vec[i]
        union!(bit_set_i,bit_set)
        setdiff!(bit_set_i,bit_set)
    end
    return nothing
end

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [October 5, 2022, 2:09pm UTC](https://discourse.julialang.org/t/can-gpu-calculation-accelerates-this-function/88295/2 "2022-10-05T14:09:32Z")

</div>

I think this example might be too minimal, since it doesn’t actually do any calculation, but in general, a `Vector{Bitset}` is unlikely to be the optimal data layout for your problem.

---

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [October 5, 2022, 2:15pm UTC](https://discourse.julialang.org/t/can-gpu-calculation-accelerates-this-function/88295/3 "2022-10-05T14:15:55Z")

</div>

There is two part in this function consume much time. 1. `bit_set_vec = [BitSet() for i = 1 : n]`, 2. ` union!(bit_set_i,bit_set) setdiff!(bit_set_i,bit_set)`, and it is the bottleneck in my code. I tried other data layout like `Vector{Tuple}`, it indeed reduced the `gc` time, but the `union!, setdiff!` on `Tuple` is much more inefficient than on `BitSet`, so I quit it.
