plagent has asked for the wisdom of the Perl Monks concerning the following question:

use strict; use List::Util qw(shuffle); use Parallel::ForkManager; my $multi_cpu_num = 2; my @chars_1 = split '', 'ACTCTGCATGCATACGCATCAGCATCAT'; my @chars_2 = split '', 'TAGCTCATGTCGATGCATGCTCGTCTCG'; my $pm = Parallel::ForkManager->new( $multi_cpu_num ); PWM: for my $pwm (1..5) { $pm->start and next PWM; my @chars_1_temp = shuffle @chars_1; my @chars_2_temp = shuffle @chars_2; print "this is $pwm....\n"; print join '',@chars_1_temp,"\n"; print join '',@chars_2_temp,"\n"; $pm->finish; } $pm->wait_all_children;
I tried to use the above code to generate 5 different paired sequences using 2 CPU. But the result was only one paired sequences generated, the other four were cloned paired without shuffle. They were identical each other. I expected those five paired sequences were different. Why? If I do not use Parallel::ForkManager module, then everything is OK.

Replies are listed 'Best First'.
Re: shuffle in Parallel::ForkManager;
by Corion (Patriarch) on Apr 16, 2015 at 08:09 UTC

    fork does not reset the seed that the rand function uses. This means that all forked children will generate the same (pseudo)random sequences and thus also will generate the same permutation from shuffle. Maybe calling srand helps?

      Demo:
      $ perl -E' rand; my $pid = fork; srand if $ARGV[0]; say rand; waitpid($pid, 0); ' 0 0.458778299481736 0.458778299481736 $ perl -E' rand; my $pid = fork; srand if $ARGV[0]; say rand; waitpid($pid, 0); ' 1 0.656664192517386 0.153404296449484

      (The seed is determined the first time rand is called, so the problem only manifests itself if rand is called before fork.)

Re: shuffle in Parallel::ForkManager;
by QM (Parson) on Apr 16, 2015 at 15:49 UTC
    Do you need them to be unique, or just randomly different?

    I would almost just compute the shuffle before the fork, and let the child do the processing. Regardless of your immediate need, this allows you to control the shuffle.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of