# Parallel computing of matrix with @threads

Hi, I am trying to implement some multi-threading on my project written in julia. Although, it seems that @threads macro does not work as I intended. I have tried almost every solutions / multithreading methods I could find online, but none of them worked.

Here is the simplified code of what I have tried to do.

``````function part_myfunc(tmpobj, array1, array2)  # array1 and array2 has same size
for i in eachindex(array2)
tmpobj= calc(tmpobj, array1[i], array2[i])
end

tmpobj
end

function myfunc(myobj, matrix1, matrix2)
_, k = size(matrix2)

tmpobjs = Array{MyObject, 1}(undef, k)
for i = 1 : k
tmpobjs[i] = initialise()
end

@threads for i = 1 : k
tmpobjs[i] = part_myfunc(tmpobjs[i], array1[:, i], array2[:, i])
end

for i = 1 : k
myobj = part_myfunc2(myobj, tmpobjs[i])
end

myobj
end
``````

This code returns a valid output when I get rid of @threads from the second for loop in the function myfunc, but with @threads, it does not. I don’t even have a slightest clue why it returns an invalid output. I have read the julia docs but it wasn’t very helpful. Is there anything that I’m missing?

There’s nothing shocking at first glance, but it’s really hard to tell without a complete working example. Could you specify the MWE a bit more?

So, tempobj and the each components of matrix1 are matrices of polynomials. The components of matrix2 are integers. the function calc contains polynomial decompositions and multiplications between tempobj and components of array1 and array2. That’s pretty much it!

Thanks for the details! However, what I meant was that a complete, working code would actually be easier to debug. Without being able to play around, it’s hard to help you.
See Please read: make it easier to help you for more advice on how to ask for help around here

1 Like