# Can I make this code use multiple threads?

**URL:** <https://discourse.julialang.org/t/can-i-make-this-code-use-multiple-threads/30049>\
**Category:** General Usage\
**Tags:** multithreading\
**Created:** [October 18, 2019, 9:33am UTC](https://discourse.julialang.org/t/can-i-make-this-code-use-multiple-threads/30049 "2019-10-18T09:33:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cserteGT3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/csertegt3/32/8283_2.png) [@cserteGT3](https://discourse.julialang.org/u/cserteGT3)\
**Post date:** [October 18, 2019, 9:33am UTC](https://discourse.julialang.org/t/can-i-make-this-code-use-multiple-threads/30049/1 "2019-10-18T09:33:05Z")

</div>

I have an expensive evaluation function, that takes long to compute and I run this over multiple objects. In theory these comuptations are independent of each other (though they use the same arguments for the evaluation). I’d like to ask if I could make the evaluation part run in parallel (using the new v1.3 features).  
Here is a “sketch-MWE” (just to get the idea):

```julia
using LinearAlgebra: dot

struct MyType
	f::Float64
end

function evaluate(obj::MyType, points, normals)
	# this is the expensive evaluation function,
    # which iterates through the points and normals
	s = 0
	for i in eachindex(points)
		s += obj.f*dot(points[i], normals[i])
	end
	return s
end

function test()
	# generate random MyTypes
	objs = MyType.(rand(100))
	
	# points and normals are large arrays
	# up to hundred thousand-million scale
	ps = [rand(3) for i in 100_000]
	ns = [rand(3) for i in 100_000]
	
	# can I make this run in parallel?
	score = evaluate.(objs, Ref(ps), Ref(ns))
	
	return score
end

```

I have two problems: 1) I don’t know how to do this, and 2) the `points` and `normals` arrays are large, and I’m afraid that it would blow in memory.  
I’v read the announcement blog post on multi-threading, but still no idea how to do this.

While writing this post, just came in mind to check the docs. Is it just to use the `@threads for` macro?

Edit: If I have a processor with 4 cores, 8 threads, can I use `JULIA_NUM_THREADS=8`?

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [October 18, 2019, 9:49am UTC](https://discourse.julialang.org/t/can-i-make-this-code-use-multiple-threads/30049/2 "2019-10-18T09:49:13Z")

</div>

> [@Variable declaration and initialisation](https://discourse.julialang.org/t/variable-declaration-and-initialisation/29923/7):
>
> Thank you. I am relatively new to Julia and not an ace programmer. Can I ask if there is any documentation on the use of threads, some example to look up to?

---

<div class="post-metadata">

**Author:** ![cserteGT3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/csertegt3/32/8283_2.png) [@cserteGT3](https://discourse.julialang.org/u/cserteGT3)\
**Post date:** [October 18, 2019, 12:03pm UTC](https://discourse.julialang.org/t/can-i-make-this-code-use-multiple-threads/30049/3 "2019-10-18T12:03:05Z")

</div>

Thank you, I’ll go through your example!

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [October 18, 2019, 12:57pm UTC](https://discourse.julialang.org/t/can-i-make-this-code-use-multiple-threads/30049/4 "2019-10-18T12:57:09Z")

</div>

> [@cserteGT3](#):
>
> s = 0

Fix this type instability first before thinking about parallelization, it will give you about `3X` speedup for free and why do you use `Ref(ps), Ref(ns)`?, I don’t think Ref is needed here.

```julia
s = zero( obj.f*dot(points[1], normals[1]) )

```
