# Floops multithreading HasBoxedVariableError

**URL:** <https://discourse.julialang.org/t/floops-multithreading-hasboxedvariableerror/127139>\
**Category:** Performance\
**Tags:** multithreading, distributed, floops, parallel-computing\
**Created:** [March 19, 2025, 2:02pm UTC](https://discourse.julialang.org/t/floops-multithreading-hasboxedvariableerror/127139 "2025-03-19T14:02:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![s\_amap](https://avatars.discourse-cdn.com/v4/letter/s/b19c9b/32.png) [@s\_amap](https://discourse.julialang.org/u/s_amap)\
**Post date:** [March 19, 2025, 2:02pm UTC](https://discourse.julialang.org/t/floops-multithreading-hasboxedvariableerror/127139/1 "2025-03-19T14:02:58Z")

</div>

I’ve been trying to adjust a parallel loop after some changes, and I need some clarification to make sure I’m not at risk of false sharing.

The loop conceptually works like this:

```julia
object # holds a state, which is a tuple of a mix of scalar and vector variables
helper = prepare(n_steps)
output = preallocate(n_steps)
vars = keys(output[1])
state_template = init_state(object.current_state,1) # creates a tuple with the scalars + ith row of the vector variables

@floop ThreadedEx() for (i, helper_i) in enumerate(helper)
	@init begin
			state = deepcopy(state_template) # thread-local init
		end
	
	state = init_state(object.current_state, i) # each thread uses vector vars starting at a different row
	compute_state(state, helper_i)
	for vars in vars
		output[i][var] = state[var] # threads access different rows
	end
end

```

I get:

```julia
HasBoxedVariableError: Closure ##reducing_function#233#255 (defined in custom_package) has 1 boxed variable: output

```

I have identical results in single and multi-thread runs; different threads don’t write to the same output row.

Am I causing some false sharing though ? Assuming the output row strides cache lines, I think two threads might write to the same cache line and cause some thrashing ?

if so, how should I adjust my code so that I get no Box error ? The examples given in the FLoops documentation only showcase scalar variables. Do I need to somehow preallocate or partition my output per-thread and then recombine it, or is there a way to pad it ?

Or am I barking up the wrong tree and there are no potential false sharing issues ?

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [March 19, 2025, 3:01pm UTC](https://discourse.julialang.org/t/floops-multithreading-hasboxedvariableerror/127139/2 "2025-03-19T15:01:12Z")

</div>

> [@s\_amap](#):
>
> ```julia
> output = preallocate(n_steps)
> 
> ```

The simplest solution, when possible, could be to declare the type of `output`, if it’s known concretely upfront:

```julia
output::ConcreteType = preallocate(n_steps)

```

The context here is the notorious [Performance of captured variable](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-captured) issue, which might lead to subtle bugs in cases like these, which is why the error was introduced AFAIK.

Relevant FLoops documentation:

- [How to avoid Box · FLoops](https://juliafolds.github.io/FLoops.jl/dev/howto/avoid-box/)

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [March 19, 2025, 3:07pm UTC](https://discourse.julialang.org/t/floops-multithreading-hasboxedvariableerror/127139/3 "2025-03-19T15:07:46Z")

</div>

> [@s\_amap](#):
>
> The loop conceptually works like this

Can you find all instances of the `output` variable being **assigned** to, in your actual code? That is, expressions of the form `output = expr`? I’m guessing there’s more such examples in your actual code than in the snippet you gave.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [March 19, 2025, 3:14pm UTC](https://discourse.julialang.org/t/floops-multithreading-hasboxedvariableerror/127139/4 "2025-03-19T15:14:56Z")

</div>

Here’s some documetation that tries to explain this behaviour: [Boxed Variables · OhMyThreads.jl](https://juliafolds2.github.io/OhMyThreads.jl/dev/literate/boxing/boxing/)

It’s for OhMyThreads.jl instead of FLoops.jl but it’s the exact same idea.

---

<div class="post-metadata">

**Author:** ![s\_amap](https://avatars.discourse-cdn.com/v4/letter/s/b19c9b/32.png) [@s\_amap](https://discourse.julialang.org/u/s_amap)\
**Post date:** [March 19, 2025, 3:17pm UTC](https://discourse.julialang.org/t/floops-multithreading-hasboxedvariableerror/127139/5 "2025-03-19T15:17:14Z")

</div>

Aah, I return the output structure immediately after the floop loop, but I **did** have a single-threaded version in a later section within the same function which made use of the same variable name.

It looks like having two different names fixes the issue. I guess it was ambiguous for the compiler that there couldn’t be any kind of interference between those two sections of code.

My mental model of Julia’s multithreading is still pretty shaky (does false sharing still risk occurring ?), but it looks like my problem is solved.

Thanks a lot !

@Mason I haven’t read that link, (I did read FLoops’s docs), I’ll check it out, thanks as well.

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [March 19, 2025, 3:35pm UTC](https://discourse.julialang.org/t/floops-multithreading-hasboxedvariableerror/127139/6 "2025-03-19T15:35:03Z")

</div>

> [@s\_amap](#):
>
> It looks like having two different names fixes the issue. I guess it was ambiguous for the compiler that there couldn’t be any kind of interference between those two sections of code.
> 
> My mental model of Julia’s multithreading

For the record, the issue of closure captures getting boxed is not specifically related to multithreading. Some attempts at improving the situation are by improving the Julia compiler’s lowering pass (which comes after parsing and before inference), while others rely on making the optimizer/inference do more work.

What makes this relevant to multithreading is basically just that closures are used naturally and often as part of any such interface. Coupled with the fact that boxing may cause subtle bugs in concurrent usage.

In case you don’t know, a closure is a “local” function that may “capture” variables from its enclosing scope.

> [@s\_amap](#):
>
> It looks like having two different names fixes the issue.

In my opinion, avoiding the reuse of variable names is a good practice in general. So I, more or less, try to write in a single-assignment form. Many will disagree, I guess. One of the benefits is having more information while debugging.

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [March 19, 2025, 3:43pm UTC](https://discourse.julialang.org/t/floops-multithreading-hasboxedvariableerror/127139/7 "2025-03-19T15:43:29Z")

</div>

> [@Mason](#):
>
> Here’s some documetation

BTW, there’s a small Markdown bug in there, one of the titles starts with rendered `##`.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [March 19, 2025, 3:54pm UTC](https://discourse.julialang.org/t/floops-multithreading-hasboxedvariableerror/127139/8 "2025-03-19T15:54:34Z")

</div>

Thanks! should be fixed now.
