Hi all,

I am writing a script that creates an array with a size that ranges between 15 and 20, each time I run it. The number of elements in the array -usually- changes each time I run the script because its size depend on some calculations I make, which is not very important at this point.

I am using every element of this array to execute a child process (a procedure I am not really familiar with), as the following:

my $passed_dir = $_[0]; my $dirtoget="${passed_dir}/StretchingDecaAlanine/GMXCubicBox/Umbr +ella"; opendir(IMD, $dirtoget) || die("Cannot open directory"); my @files= readdir(IMD); closedir(IMD); foreach my $f (@files) { if ( (-d $f) and ($f ne ".") and ($f ne "..") ){ my $secdirtoget= "${passed_dir}/StretchingDecaAlanine/GMX +CubicBox/Umbrella/${f}"; chdir ($secdirtoget); system ("ln -s ../md_umbrella.mdp ../index.ndx ../topol +.top ../posre_CTerm.itp ../posre.itp ."); system ("grompp -f md_umbrella.mdp -c pullconf.gro -n i +ndex.ndx"); my $pid = fork(); if ($pid == -1) { die; } elsif ($pid == 0) { print "Running mdrun for within $f\n"; exec 'mdrun', '>logje 2>&1 &' or die; } while (wait() != -1) {} print "Done with mdrun in $f\n"; chdir ("${passed_dir}/StretchingDecaAlanine/GMXCubicBox/Um +brella"); }
Above you see that for each element in @files; 1- I chdir to a corresponding folder 2- do some linking, 3- Make a system call to a program 4- Execute a process (in elsif), wait for it to end 5- Return (last chdir) to the parent directory and do the same thing with the next folder (element) in my array @files

Now, the thing is.. The program I run with exec takes almost 45 minutes to finish for a single folder in @files. As I already mentioned I do this for at least 15, at most 18-19 folders (the size of @files at that particular run). The thing about this program I am executing (mdrun) is that you can execute as many as you want. So actually instead of doing 1 by one (waiting for each to finish), I can execute for the whole size without using fork(), but then the computer gets really slow.

The advice I want from you is to tell me a way to split this @files into arrays of smaller size (say 4), and then each time execute 4 mdruns, and wait for (maybe again using fork) these 4 to finish in order to continue with another 4. But as I already said, the size of this array @files changes in each run. So if it is of size 17, what I need to do is to split it into arrays of size 4+4+4+4+1.. If it is size 15, then 4+4+4+3. So that I can execute mdrun first with the array of size 4, then wait for those to finish, and then execute it with the following 4, then wait again, then execute with the next 4 elements, and then wait, and then execute with the last 3, and then wait again.

Any ideas? I hope my question was clear.. Many thanks..

PS: Some of you may say that I am using fork, execute and wait in a wrong/inappropriate way. Well, you most probably are right because I am not really familiar with these tools and I could not find a nice website that explains how to wait until a process (which is called within a Perl script) to finish before continuing with the rest of the script. I ripped the above fork- exec-wait off a website and right now it works for me. But if you can advice a better usage of course I can replace. What I want to do with the above fork-exec-wait is just to wait until "mdrun" process is finished (it does not return anything to script etc, it's like a mkdir command with only different being it takes 45 minutes to finish).

If the title of the post is not descriptive enough, or simply incorrect, please advice a better title for this problem so that I can change it.

In reply to Splitting an array into subarrays of size ~n by hotel

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.