Closed Daiweibin closed 8 years ago
Hi Daiweibin,
that's great to hear, glad you like it! :) The problem you describe originates from the way the shared list works and is mostly unrelated to pymp (I actually just wrap the multiprocessing
list for a complete set of datastructures in pymp.shared
). Even for the following:
import pymp
a=pymp.shared.list([[i,1] for i in range(4)])
a[0][1] = 10
the list a
will be unchanged. The reason is, that only the list elements of a itself are shared and stored in this datastructure. So a[0]
will give you a copy of the current element. Your commands then change, e.g., the value in the first element of the copy with a[0][1]
, but the result will not be stored and copied back to a[0]. So unless you assign to the element of a shared list, it will remain unchanged (i.e., you need a line like a[0] =
).
For a task like you have with nested lists, I'd rather go with a shared array with multiple dimensions. Such effects will not occur in this case. The shared list, I'd only use in cases where you need to make use of the classical list operations like append or delete and avoid it for multi-dimensional cases.
OK, thank you very much.
Your code is very nice and it helps me a lot. However, when I use pymp to implement the parallel computation, there is a problem confusing me.
a=pymp.shared.list([[i,1] for i in range(4)])
with pymp.Parallel(4) as pp:
for i in pp.range(4):
for j in range(2):
a[i][j]+=1
print (a)
Just like the example above, I want to change the elements of list a. If it works, it should print '[[1, 2], [2, 2], [3, 2], [4, 2]]'. However, it prints '[[0, 1], [1, 1], [2, 1], [3, 1]]' (i.e. a is not changed). It would be great, if you can have a look and give me some advice. Thank you!