Can I make this code use multiple threads?

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):

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?

1 Like

Thank you, I’ll go through your example!

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.

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