# Break statement in foldscuda/floops is giving error

**URL:** <https://discourse.julialang.org/t/break-statement-in-foldscuda-floops-is-giving-error/89864>\
**Category:** General Usage\
**Tags:** cuda, floops, folds\
**Created:** [November 7, 2022, 9:14am UTC](https://discourse.julialang.org/t/break-statement-in-foldscuda-floops-is-giving-error/89864 "2022-11-07T09:14:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nethaji8](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@nethaji8](https://discourse.julialang.org/u/nethaji8)\
**Post date:** [November 7, 2022, 9:14am UTC](https://discourse.julialang.org/t/break-statement-in-foldscuda-floops-is-giving-error/89864/1 "2022-11-07T09:14:45Z")

</div>

Hi everyone,  
I am trying to use Floops, and FoldsCuda library as an alternative to OpenACC in Cuda c++, and Fortran in Julia. Consider the following code

```julia
using Random, CUDA, FLoops, Test, FoldsCUDA

function main()

    N = 100000

    A = Vector{Int}(shuffle(collect(1:N)))

    C = CuArray{Int}(copy(A))

    @floop for i in 1:N
        A[i] = A[i]+1
        if i == 10 
            break 
        end
    end

    @floop CUDAEx() for j in 1:N
        C[j] = C[j]+1
        if j == 10 
            break 
        end
    end

    println(A==C)
end

main()

```

The second loop using GPU is giving some weird error, can please someone explain how to resolve this issue…?

error:

```julia
ERROR: DivideError: integer division error
Stacktrace:
  [1] div
    @ ./int.jl:288 [inlined]
  [2] _derived_array
    @ ~/.julia/packages/CUDA/DfvRa/src/array.jl:730 [inlined]
  [3] reinterpret(#unused#::Type{Tuple{}}, a::CuArray{Nothing, 1, CUDA.Mem.DeviceBuffer})
    @ CUDA ~/.julia/packages/CUDA/DfvRa/src/array.jl:754
  [4] #18
    @ ~/.julia/packages/UnionArrays/ubZMg/src/impl/vectors.jl:51 [inlined]
  [5] macro expansion
    @ ~/.julia/packages/Transducers/DSfBv/src/core.jl:155 [inlined]
  [6] foldlargs
    @ ~/.julia/packages/UnionArrays/ubZMg/src/impl/utils.jl:19 [inlined]
  [7] UnionArrays.Impl.UnionVector(ETS::Type{Union{Tuple{}, Transducers.Reduced{Tuple{}}}}, data::CuArray{Nothing, 1, CUDA.Mem.DeviceBuffer}, typeid::CuArray{UInt8, 1, CUDA.Mem.DeviceBuffer})
    @ UnionArrays.Impl ~/.julia/packages/UnionArrays/ubZMg/src/impl/vectors.jl:50
  [8] UnionArrays.Impl.UnionVector(#unused#::UndefInitializer, DataVectorType::Type{CuArray{T, 1} where T}, TypeTagVectorType::Type{CuArray{UInt8, 1}}, ETS::Type{Union{Tuple{}, Transducers.Reduced{Tuple{}}}}, n::Int64)
    @ UnionArrays.Impl ~/.julia/packages/UnionArrays/ubZMg/src/impl/vectors.jl:98
  [9] UnionArrays.Impl.UnionVector(undef::UndefInitializer, VectorType::Type{CuArray{T, 1} where T}, ETS::Type{Union{Tuple{}, Transducers.Reduced{Tuple{}}}}, n::Int64)
    @ UnionArrays.Impl ~/.julia/packages/UnionArrays/ubZMg/src/impl/vectors.jl:85
 [10] #UnionVector#1
    @ ~/.julia/packages/UnionArrays/ubZMg/src/UnionArrays.jl:17 [inlined]
 [11] UnionArray
    @ ~/.julia/packages/UnionArrays/ubZMg/src/UnionArrays.jl:17 [inlined]
 [12] allocate_buffer(#unused#::Type{Union{Tuple{}, Transducers.Reduced{Tuple{}}}}, n::Int64)
    @ FoldsCUDA ~/.julia/packages/FoldsCUDA/Mo35m/src/kernels.jl:83
 [13] _transduce!(buf::Nothing, rf::Transducers.Reduction{Transducers.Map{typeof(first)}, Transducers.BottomRF{Transducers.AdHocRF{var"#__##oninit_function#1359#741", typeof(identity), InitialValues.AdjoinIdentity{var"#__##reducing_function#1360#742"{CuArray{Int64, 1, CUDA.Mem.DeviceBuffer}}}, typeof(identity), typeof(identity), var"#__##combine_function#1361#743"}}}, init::Transducers.InitOf{Transducers.DefaultInitOf}, arrays::UnitRange{Int64})
    @ FoldsCUDA ~/.julia/packages/FoldsCUDA/Mo35m/src/kernels.jl:138
 [14] transduce_impl(rf::Transducers.Reduction{Transducers.Map{typeof(first)}, Transducers.BottomRF{Transducers.AdHocRF{var"#__##oninit_function#1359#741", typeof(identity), InitialValues.AdjoinIdentity{var"#__##reducing_function#1360#742"{CuArray{Int64, 1, CUDA.Mem.DeviceBuffer}}}, typeof(identity), typeof(identity), var"#__##combine_function#1361#743"}}}, init::Transducers.InitOf{Transducers.DefaultInitOf}, arrays::UnitRange{Int64})
    @ FoldsCUDA ~/.julia/packages/FoldsCUDA/Mo35m/src/kernels.jl:32
 [15] _transduce_cuda(op::Function, init::Transducers.InitOf{Transducers.DefaultInitOf}, xs::UnitRange{Int64})
    @ FoldsCUDA ~/.julia/packages/FoldsCUDA/Mo35m/src/kernels.jl:18
 [16] #_transduce_cuda#5
    @ ~/.julia/packages/FoldsCUDA/Mo35m/src/kernels.jl:1 [inlined]
 [17] _transduce_cuda
    @ ~/.julia/packages/FoldsCUDA/Mo35m/src/kernels.jl:1 [inlined]
 [18] transduce
    @ ~/.julia/packages/FoldsCUDA/Mo35m/src/api.jl:45 [inlined]
 [19] _fold
    @ ~/.julia/packages/FLoops/6PVny/src/reduce.jl:851 [inlined]
 [20] macro expansion
    @ ~/.julia/packages/FLoops/6PVny/src/reduce.jl:829 [inlined]
 [21] main()
    @ Main ~/logistics_updated/logistics_nethaji/removing_element copy2.jl:18
 [22] top-level scope
    @ ~/logistics_updated/logistics_nethaji/removing_element copy2.jl:28

```

---

<div class="post-metadata">

**Author:** ![nethaji8](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@nethaji8](https://discourse.julialang.org/u/nethaji8)\
**Post date:** [November 10, 2022, 7:55am UTC](https://discourse.julialang.org/t/break-statement-in-foldscuda-floops-is-giving-error/89864/2 "2022-11-10T07:55:25Z")

</div>

Just like Cuda in c++ and Fortran, kernels in Cuda Julia don’t support break statements.  
I don’t understand the error displayed here as `ERROR: DivideError: integer division error`. Please let me know if anyone has any information regarding this.

---

<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:** [November 11, 2022, 9:40am UTC](https://discourse.julialang.org/t/break-statement-in-foldscuda-floops-is-giving-error/89864/3 "2022-11-11T09:40:51Z")

</div>

> [@nethaji8](#):
>
> Just like Cuda in c++ and Fortran, kernels in Cuda Julia don’t support break statements.

There is no such restriction, not in CUDA C++ and neither in CUDA.jl kernels written in Julia.

The issue you’re seeing is a “bug” in CUDA.jl where it fails to reinterpret an array with a zero-sized element type. I’m not sure that’s what you want though, so there may be an issue with the layers above (i.e. UnionArrays or FoldsCUDA).

---

<div class="post-metadata">

**Author:** ![nethaji8](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@nethaji8](https://discourse.julialang.org/u/nethaji8)\
**Post date:** [November 11, 2022, 10:16am UTC](https://discourse.julialang.org/t/break-statement-in-foldscuda-floops-is-giving-error/89864/4 "2022-11-11T10:16:19Z")

</div>

I am sorry for the wrong answer @maleadt, thanks for your reply…!  
I just understand there is an issue with UnionArrays or FoldsCUDA but I didn’t understand the rest of your answer can you please explain clearly…?

---

<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:** [November 11, 2022, 10:23am UTC](https://discourse.julialang.org/t/break-statement-in-foldscuda-floops-is-giving-error/89864/5 "2022-11-11T10:23:15Z")

</div>

There’s a corner case in CUDA.jl’s `reinterpret` that isn’t handled appropriately, [reinterpret doesn't handle singleton types · Issue #1669 · JuliaGPU/CUDA.jl · GitHub](https://github.com/JuliaGPU/CUDA.jl/issues/1669), but it is a corner case, so I’m surprised FLoops requires it.
