# Calling another function in loop using Base.Threads

**URL:** https://discourse.julialang.org/t/calling-another-function-in-loop-using-base-threads/85037
**Category:** General Usage
**Tags:** multithreading
**Created:** [July 30, 2022, 3:55pm UTC](https://discourse.julialang.org/t/calling-another-function-in-loop-using-base-threads/85037 "2022-07-30T15:55:50Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Amol\_H](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amol_h/32/34413_2.png) [@Amol\_H](https://discourse.julialang.org/u/Amol_H)
#### Post date: [July 30, 2022, 3:55pm UTC](https://discourse.julialang.org/t/calling-another-function-in-loop-using-base-threads/85037/1 "2022-07-30T15:55:50Z")

</div>

Dear All,

I am having a problem running the following code in parallel. Please suggest how to achieve the parallelization to populate an array by a so-called very computationally expensive function.

Thanks and Regards  
Amol

```julia
#!/usr/bin/julia

using Base.Threads
using LinearAlgebra,NumericalIntegration

x = collect(0:1e-6:1);

function test_func(a)
  return a*a;
end

test = zeros(length(x));

Threads.@threads for i = 0:length(x)
	 test[i] = test_func(x[i]);
end

println("integrate = ", integrate(x,test) );
println(" ");

```

---

<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: [July 30, 2022, 4:03pm UTC](https://discourse.julialang.org/t/calling-another-function-in-loop-using-base-threads/85037/2 "2022-07-30T16:03:11Z")

</div>

> [@Amol\_H](#):
>
> I am having a problem running the following code in parallel.

what problem?

> [@Amol\_H](#):
>
> ```julia
> test = zeros(length(x));
> 
> Threads.@threads for i = 0:length(x)
> test[i] = test_func(x[i]);
> end
> 
> ```

this is gonna be slow, you’re running code in global with non-constant global variable

---

<div class="post-metadata">

### Author: ![Amol\_H](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amol_h/32/34413_2.png) [@Amol\_H](https://discourse.julialang.org/u/Amol_H)
#### Post date: [July 30, 2022, 4:04pm UTC](https://discourse.julialang.org/t/calling-another-function-in-loop-using-base-threads/85037/3 "2022-07-30T16:04:18Z")

</div>

```julia
ERROR: LoadError: TaskFailedException
Stacktrace:
 [1] wait
   @ ./task.jl:334 [inlined]
 [2] threading_run(func::Function)
   @ Base.Threads ./threadingconstructs.jl:38
 [3] top-level scope
   @ ./threadingconstructs.jl:97

    nested task error: BoundsError: attempt to access 1000001-element Vector{Float64} at index [0]
    Stacktrace:
     [1] getindex(A::Vector{Float64}, i1::Int64)
       @ Base ./array.jl:861
     [2] macro expansion
       @ ~/TestProg/threads.jl:15 [inlined]
     [3] (::var"#2#threadsfor_fun#1"{UnitRange{Int64}})(onethread::Bool)
       @ Main ./threadingconstructs.jl:85
     [4] (::var"#2#threadsfor_fun#1"{UnitRange{Int64}})()
       @ Main ./threadingconstructs.jl:52
in expression starting at /home/amol/TestProg/threads.jl:14

```

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [July 30, 2022, 4:05pm UTC](https://discourse.julialang.org/t/calling-another-function-in-loop-using-base-threads/85037/4 "2022-07-30T16:05:51Z")

</div>

> [@Amol\_H](#):
>
> `Threads.@threads for i = 0:length(x)`

Julia `Array`s have one-based indexing, and use inclusive ranges.

Use `1:length(x)` or, preferably, `eachindex(x)`

---

<div class="post-metadata">

### Author: ![Amol\_H](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amol_h/32/34413_2.png) [@Amol\_H](https://discourse.julialang.org/u/Amol_H)
#### Post date: [July 30, 2022, 4:06pm UTC](https://discourse.julialang.org/t/calling-another-function-in-loop-using-base-threads/85037/5 "2022-07-30T16:06:03Z")

</div>

I am new to these ‘Threads’. How to exactly achieve the same using Base.Threads package? I would be really grateful for your help in this regard.

---

<div class="post-metadata">

### Author: ![Amol\_H](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amol_h/32/34413_2.png) [@Amol\_H](https://discourse.julialang.org/u/Amol_H)
#### Post date: [July 30, 2022, 4:07pm UTC](https://discourse.julialang.org/t/calling-another-function-in-loop-using-base-threads/85037/6 "2022-07-30T16:07:34Z")

</div>

Thanks! Gotcha…solved it. 😃

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [July 30, 2022, 4:09pm UTC](https://discourse.julialang.org/t/calling-another-function-in-loop-using-base-threads/85037/7 "2022-07-30T16:09:18Z")

</div>

But also make sure to put your code in functions, otherwise, threads will be slow.
