pdl icon indicating copy to clipboard operation
pdl copied to clipboard

PDLapi warnings in parallel calculations with PDL::Opt::NonLinear

Open YuryPakhomov opened this issue 10 months ago • 0 comments

Hello all!

I use PDL::Opt::NonLinear module in my code. It works fine but slowly due to complex minimization function. The minimization function can be paralleled with Parallel::ForkManager. It works fine in separate call. But when I try to use the function in PDL::Opt::NonLinear module, many warnings appear:

	(in cleanup) INVALID PDL MAGICNO, got hex=0xf02100 (37593680)
 at /usr/local/share/perl5/Parallel/ForkManager/Child.pm line 26.
	eval {...} called at /usr/local/share/perl5/Parallel/ForkManager/Child.pm line 26
Warning: special data without datasv is not freed currently!! at test.pl line 0.
	eval {...} called at test.pl line 0
Warning: special data without datasv is not freed currently!! at test.pl line 0.
	eval {...} called at test.pl line 0
Warning: special data without datasv is not freed currently!! at test.pl line 0.
	eval {...} called at test.pl line 0
Warning: special data without datasv is not freed currently!! at test.pl line 0.
	eval {...} called at test.pl line 0
Warning: special data without datasv is not freed currently!! at test.pl line 0.
	eval {...} called at test.pl line 0

The simple code to reproduce this output is:

#! /usr/bin/perl
use PDL;
use PDL::NiceSlice;
use PDL::Opt::NonLinear;
use Parallel::ForkManager;

my $pm = Parallel::ForkManager->new(4);

my $x=pdl(1);
my $fx=pdl(0);
my $gx=zeroes(nelem($x));

# This single call works fine
#fg_func($fx,$gx,$x); exit;

# Optimization algorithm
                my $bounds =  zeroes(nelem($x),2);
                $bounds(0,0).=  pdl(0.0);
                $bounds(0,1).= pdl(10.0);
                
                my $tbounds = zeroes(nelem($x));
                $tbounds.=2;

                my $gtol = pdl(0.9);
                my $pgtol = pdl(0.1);
                my $factr = pdl(1e7);
                my $m = pdl(10);
                my $print = pdl(-1);
                my $maxit = pdl(long,15);
                my $info = pdl(long,1);
                my $iv = zeroes(long,44);
                my $v = zeroes(29);

                lbfgsb($fx, $gx, $x, $m, $bounds, $tbounds, $maxit, $factr, $pgtol, $gtol,
                $print, $info,$iv, $v,\&fg_func);

sub fg_func{
 my ($f, $g, $x) = @_;
 $f .= 0;
 $g .= pdl(0);

# Parallel calculations block. This is demo without any calculation. Create external thread and then terminate it.
# If to comment this block then program works fine
 DATA_LOOP:
 for my $i (0 .. nelem($x)-1){
  my $ppid = $pm->start and next DATA_LOOP;
  $pm->finish($i); # Terminates the child process
 }
 $pm->wait_all_children;

 return 0;
}

The Warning points to "Child.pm" line 26, which contains CORE::exit($x || 0) of subroutine finish:

sub finish {
  my ($s, $x, $r)=@_;

  $s->store($r);

    CORE::exit($x || 0);
}

The messages "INVALID PDL MAGICNO" and "Warning: special data without datasv is not freed currently!" are generated by Basic/Core/pdlapi.c

YuryPakhomov avatar Apr 08 '24 14:04 YuryPakhomov