[Flac-dev] multiple cores?

Nicolas Mizel nicolas at mizel.ch
Tue Aug 7 01:19:40 PDT 2007


Here's the small script I wrote to reflac all my files using the 8 cores
of a server I have at work.

Best regards,
Nicolas

---

#!/usr/bin/perl

use strict;
use warnings;

use File::Basename;
use File::Spec::Functions qw(catdir);

use Time::HiRes qw(sleep);

use threads;
use threads::shared;

my $threads_max = 8;
my @threads_status :shared;

@threads_status = (-1) x $threads_max;

$| = 1;

if ($#ARGV != 0) {
        print "usage: $0 <dir>.\n";
        exit 1;
}

my $dir = $ARGV[0];

parse_dir($dir);

&t_join;

sub parse_dir {
        my($dir) = @_;

        opendir(DIR, $dir) || die "error: cannot open $dir.\n";
        my @names = readdir(DIR);
        closedir(DIR);

        foreach my $name (sort @names) {
                next if ($name =~ /^\./);
                my $file = catdir($dir, $name);

                (-d $file) && parse_dir($file);
                if ($name =~ /\.flac$/i) {
                        print "$file\n";
                        &t_start($file);
                }
        }
}

sub t_start {
        my $data = shift;
        my $t_started = 0;

        while (! $t_started) {
                for (my $tnum = 0; $tnum < $threads_max; $tnum++) {
                        if ($threads_status[$tnum] != 0) {
                                my $tid = $threads_status[$tnum];
                                if (my $thr = threads->object($tid)) {
                                        $thr->join();
                                        undef($thr);
                                }
                                $threads_status[$tnum] = 0;
                                threads->new(\&worker, $tnum, $data);
                                $t_started = 1;
                                last;
                        }
                }
                sleep(0.1);
        }
}

sub t_join {
        my $t_joined = 0;

        while (! $t_joined) {
                $t_joined = 1;
                for (my $tnum = 0; $tnum < $threads_max; $tnum++) {
                        if ($threads_status[$tnum] == 0) {
                                $t_joined = 0;
                                sleep 1;
                        } elsif ($threads_status[$tnum] > 0) {
                                my $tid = $threads_status[$tnum];
                                if (my $thr = threads->object($tid)) {
                                        $thr->join();
                                }
                                $threads_status[$tnum] = -1;
                        }
                }
                if (! $t_joined) {
                        sleep(0.1);
                }
        }
}

sub worker {
        my ($tnum, $data) = @_;
        my $file = $data;
        system("/usr/local/bin/flac -s --best -f \"$file\"");
        $threads_status[$tnum] = threads->self()->tid();
}

---


> Scott C. Brown 02 wrote:
>
>> A question that someone asked me, cut and pasted from another board:
>>
>> >>I've got 8 cores on my machine and while encoding only one processor
>> is used,
>> and maxed out. Is there any way to distribute the load across
>> multi-cores?<<
>>
>> I have no idea what the answer to this is.  The question was about flac
>> on OS X
>
> The only way to distribute FLAC encoding load across CPUS is
> to use N separate flac processes to encode N separate files.
>
> Erik
> --
> -----------------------------------------------------------------
> Erik de Castro Lopo
> -----------------------------------------------------------------
> Open Source and Free Software means that you never sacrifice quality
> of the code for meeting deadlines set up by people not participating
> directly in the software development process.
> _______________________________________________
> Flac-dev mailing list
> Flac-dev at xiph.org
> http://lists.xiph.org/mailman/listinfo/flac-dev
>



More information about the Flac-dev mailing list