Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Parallelization of multiple nested loops

by pryrt (Vicar)
on Feb 07, 2018 at 17:31 UTC ( #1208641=note: print w/replies, xml ) Need Help??

in reply to Parallelization of multiple nested loops

There have been a lot of good replies, but I'm going to recommend making things as generic as possible: the chances are, you're going to want to do something similar again later, but maybe with a different number of parameters, or maybe one or more parameter uses a different set of values. Thus, wrap it in a function where you can programmatically change those.

Note: with hardcoded loops, like you show, if you want to change the number of parameters, you need to copy/paste more nested loops (and fix the indentation). And if you want to vary 11 of the 13 parameters on one run, but only 5 of the 13 parameters on the next run, then you have to remove (or comment out) some of the levels of nesting. That is not easy for on-the-fly changes.

If all the parameters want to take on the same values, then BrowserUK's solution is excellent (I need to learn to better think in terms of true iterators and the Algorithm::Combinatorics for such problems). But if one or more of the parameters needs a different set of values, then it's not I don't see it being (updated phrasing) easily extensible.

When I see loops nested more than about 2 deep, especially when all the work is only in the deepest nest, and doubly especially when the iterators are themselves an array, I think of converting it to one external loop on an iterator index, and one internal loop to generate the parameter array. After the parameter array is generated, but inside the iterator loop, then do the processing.

So, combining those, here's a suggested implementation, where the action taken is just printing the results, either to STDOUT or to a file.

While thinking about it after I tested, I would actually add another parameter to the function, which expects a CODEREF, that way you could then run $function->(@p) in the place of the print-statement, and make it even more generic.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1208641]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2018-08-17 07:01 GMT
Find Nodes?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:

    Results (174 votes). Check out past polls.