0

I've the following problem I cannot solve.

I allocate an empty matrix, v, of size n x m. Then I want to do a Parallel loop (ex. ParallelDo), and do some operations (described by a function operation[j,y] of two arguements) that as a result gives a numerical output. I want to "store" the i-th output in the i-th element of the list.

If I use a Parallel loop with inside a For loop, there is no way to in fact store the output in the list, and at the end of the loop, v, is empty. If I do the same thing with a regular loop (ex. Do), then it works.

Here I report a generic expample:

v = Table[{}, {j, 1, m}, {i, 1, n}];

ParallelDo[For[j = 1, j < n+1, j++, v[[j,i]] = operation[j,i]], {i, 1, m}]

But in this case, no matter of what the operation function is, V is still an empty matrix. If I use Do, instead of ParallelDo, then it works.

All DistributeDefinitions and related stuff I think are taken care correctly.

Thanks!

sam84
  • 497
  • 6
  • 15
  • You know how people are always saying not to use capitol letters as variables? N is a Mathematica function and can't be used as a variable. Also, you have syntax errors: what is i? – bill s Jan 24 '14 at 21:51
  • You probably want SetSharedVariable@V;ParallelDo[V[[x,y]] = operation[x,y]], {y, 1, Y},{x,1,X}]. Although this is probably really inefficient. – Ajasja Jan 24 '14 at 22:08
  • @bills thanks, I have made corrections. – sam84 Jan 24 '14 at 22:08
  • V = ParallelTable[operation[j,i],{j,1,n},{i,1,m}] – Ymareth Jan 24 '14 at 22:34
  • @Ymareth Yup exactly, that's the mma way to do this. No realocation etc as in C/C++ – Ajasja Jan 24 '14 at 23:04
  • @Ajasja - Unless the OP is trying to do something clever like see how far a calc has got to. If the a kernel failed then some part of V wouldn't be filled in perhaps. I doubt this is the reason though. – Ymareth Jan 25 '14 at 00:07

0 Answers0