I'm new with parallel computing and managed to change my code in such a way that it runs faster than my non parallel code, however my results slightly differ. I tried using the @sync, @async, @threads, and @distributed macro's but none of them seem to keep the outcome correct.
The goal is finding the shortest path through a maze and while running the code non parallel works fine it seems that running the code in parallel does not finish all iterations of the search algorithm and in turn only gives the shortest path through the maze found by one of the threads/workers.
It mainly works by starting at a the entrance, iterating over all possible points it can travel to towards the exit, it then selects the point it reached the quickest and repeats those steps until the exit is reached. It does this using two non-nested for loops but when i increase the execution speed using macros like @async or @distributed it never finds the shortest path.
Is there a way to do this concurrently or parallel while still getting the same result at the end?
Edit:
I added an example of a function that has the same problem. How would you be able to get the same z value in the end while speeding things up with parallelization?
a = rand(1, 2, 15)
function stufftodo(a)
z = 0
y = rand(size(a)[1], size(a)[2]) .* 1000
for i in 1:size(a)[3]
x = a[:, :, i]
sleep(0.05)
if sum(y)>sum(x)
y=x
end
end
z = minimum(y)
end
sleep(0.05)is the bottleneck here. You usesum(y)each time, although you already have it from previous iteration. Note that I have parallelized the code, and it gives speed-up although not that significant since each element is accessed only once, which makes the problem almost all about RAM speed. - hckr