3

This question is related to this quesion. The goal is to use ParallelMap to map a function to a nested Table to get the FinestGrained distribution of calculation. But after that, I want to recover the original nested data structure instead of a flat one.

For example, say I have a nested Table like this

Table[
 Table[{line, honeycombnum, distance}, {line, 1, 
   honeycombnum + 1}, {distance, 1, 4}], {honeycombnum, 2, 5}]

as you can see, this nested table structure is not of equal length .

Then what is the general way to recover the data structure after using

ParallelMap[f[#[[1]], #[[2]], #[[3]]] &, Flatten[Table[
   Table[{line, honeycombnum, distance}, {line, 1, 
     honeycombnum + 1}, {distance, 1, 4}], {honeycombnum, 2, 5}], 2], Method -> "FinestGrained"]

By the word "general ", I mean the method should be suitable for arbitrary complex Table structure.

matheorem
  • 17,132
  • 8
  • 45
  • 115

2 Answers2

3

A possibility is the following:

it = Flatten@MapIndexed[#2 -> #1 &, t, {-2}];
if[a_ -> b_] := a -> f[b];
res = ParallelMap[if, it];
ReplacePart[t, res]

It adds to the table the index of every object, it creates a new function working like yours but preserving the indices, operates with parallel map and then substitutes all the parts in the original table with the results.

I think this is rather general and perhaps rather terse.

user8074
  • 1,592
  • 8
  • 7
2

I'm sure there should be an easier way. Meanwhile:

orig = Table[ Table[{line, honeycombnum, distance}, {line, 1, honeycombnum + 1}, 
                                                    {distance, 1, 4}], {honeycombnum, 2, 5}];
(* Form a template for the re-structure op, some pattern to match your data needed*)
i = 0; orig1 = orig /. {_Integer, _Integer, _Integer} :> x[++i] 
(* Let's see how it looks when flattened *)
orig2 = Flatten[orig1]
dest = Flatten[orig /. {a_Integer, b_, c_} -> {a + b + c}] (*Something like your ParallelMap *)
dest1 = orig1 /. Thread[orig2 -> dest] (* The re-structured result*)

Note that you need a pattern to match your data for this method to work

Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453