Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I process a partial order of tasks concurrently using Perl?

I have a partially ordered set of tasks, where for each task all of the tasks that are strictly before it in the partial order must be executed before it can be executed. I want to execute tasks which are not related (either before or after one other) concurrently to try to minimise the total execution time - but without starting a task before its dependencies are completed.

The tasks will run as (non-perl) child processes.

How should I approach solving a problem like this using Perl? What concurrency control facilities and data structures are available?

like image 333
lexicalscope Avatar asked Nov 04 '22 10:11

lexicalscope


2 Answers

I would use a hash of arrays. For each task, all its prerequisities will be mentioned in the corresponding array:

$prereq{task1} = [qw/task2 task3 task4/];

I would keep completed tasks in a different hash, and then just

my @prereq = @{ $prereq{$task} };
if (@prereq == grep exists $completed{$_}, @prereq) {
    run($task);
}
like image 118
choroba Avatar answered Nov 09 '22 09:11

choroba


Looks like a full solution is NP-complete.

As for a partial solution, I would use some form of reference counting to determine which jobs are ready to run, Forks::Super::Job to run the background jobs and check their statuses and POSIX::pause to sleep when maximum number of jobs is spawned.

No threads are involved since you're already dealing with separate processes.

Read the first link for possible algorithms/heuristics to determine runnable jobs' priorities.

like image 42
Dallaylaen Avatar answered Nov 09 '22 08:11

Dallaylaen