3
$\begingroup$

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.

$\endgroup$
3
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ Thank you! But I can't understand. Can you explain a little? What is t,it,if? $\endgroup$ – matheorem Nov 10 '13 at 10:01
  • $\begingroup$ Oh, I understand $\endgroup$ – matheorem Nov 10 '13 at 10:07
  • $\begingroup$ Yes, I am sorry for the excessive synthesis...: "t" is the table, "it" is the indexed table, "if" is the index-preserving function based on f. The final line produces the result $\endgroup$ – user8074 Nov 10 '13 at 19:04
2
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.