# @synchronize causing silent failures in function

**URL:** <https://discourse.julialang.org/t/synchronize-causing-silent-failures-in-function/106919>\
**Category:** GPU\
**Tags:** question\
**Created:** [November 29, 2023, 11:03pm UTC](https://discourse.julialang.org/t/synchronize-causing-silent-failures-in-function/106919 "2023-11-29T23:03:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![erasedbird](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erasedbird/32/202739_2.png) [@erasedbird](https://discourse.julialang.org/u/erasedbird)\
**Post date:** [November 29, 2023, 11:03pm UTC](https://discourse.julialang.org/t/synchronize-causing-silent-failures-in-function/106919/1 "2023-11-29T23:03:51Z")

</div>

Hello! I’ve been having this strange issue. Running the following code in a Jupyter notebook causes it to be forever stuck on the last line:

```julia
using KernelAbstractions
using CUDA

@kernel function tile_lu_factor!(A, n)
    
    I, J, K, L = @index(Global, NTuple)
    
    for k = 1:2    
        
        if K == 1 && L == 1
            for k = 1:n
        
                @synchronize

            end
        end
        
        if K == 1 && L <= 3-k
            for k = 1:n
        
                @synchronize

            end
        end
    end
end

A = CuArray(rand(2, 2))
backend = get_backend(A)

tile_lu_factor!(backend, (2, 2, 1, 3))(A, 2, ndrange = (2, 2, 1, 3))

A_not_gpu = Array(A)

```

However when I replace the n with a 2 on the for loop instead, it is able to finish:

```julia
for k = 1:2    
        
        if K == 1 && L == 1
            for k = 1:2
        
                @synchronize

            end
        end
        
        if K == 1 && L <= 3-k
            for k = 1:2
        
                @synchronize

            end
        end
    end

```

What is going on?

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [November 30, 2023, 12:15am UTC](https://discourse.julialang.org/t/synchronize-causing-silent-failures-in-function/106919/2 "2023-11-30T00:15:14Z")

</div>

I don’t know what the error is, but one thing to notice is that you used `k` as the index of both the inner loops and the outer loops. That should not cause any issues, but it could maybe confuse people.

Could you explain why all the threads will hit the same number of `@synchronize` calls? Looking at the code, it seems that those with lower `L` values and `K=1` will synchronize more often than other threads, which means they would wait forever.

---

<div class="post-metadata">

**Author:** ![erasedbird](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erasedbird/32/202739_2.png) [@erasedbird](https://discourse.julialang.org/u/erasedbird)\
**Post date:** [November 30, 2023, 1:24am UTC](https://discourse.julialang.org/t/synchronize-causing-silent-failures-in-function/106919/3 "2023-11-30T01:24:03Z")

</div>

Thanks for the suggestions, I changed the loop variable for clarity and added else statements so every thread should synchronize the same number of times. However, it is still getting stuck when I try to access A after running the function.

Here is the new function:

```julia
@kernel function tile_lu_factor!(A, n)
    
    I, J, K, L = @index(Global, NTuple)
    
    for s = 1:2    
        
        if K == 1 && L == 1
            for s = 1:n
        
                @synchronize

            end
        else 
            for s = 1:n
        
                @synchronize

            end
        end
        
        if K == 1 && L <= 3-s
            for s = 1:n
        
                @synchronize

            end
        else
            for s = 1:n
        
                @synchronize

            end
        end
    end
end

```

---

<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:** [December 1, 2023, 10:52am UTC](https://discourse.julialang.org/t/synchronize-causing-silent-failures-in-function/106919/4 "2023-12-01T10:52:49Z")

</div>

You’re not allowed to synchronize from a divergent context (i.e. from branches that are not taken uniformly across a warp); generally all threads have to reach the barrier, or you risk deadlocks.

---

<div class="post-metadata">

**Author:** ![erasedbird](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erasedbird/32/202739_2.png) [@erasedbird](https://discourse.julialang.org/u/erasedbird)\
**Post date:** [December 4, 2023, 9:41pm UTC](https://discourse.julialang.org/t/synchronize-causing-silent-failures-in-function/106919/5 "2023-12-04T21:41:28Z")

</div>

Got it, thanks!
