/usr/lib/perl5/PDL/PP/PdlParObj.pm is in pdl 1:2.007-2build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 | ##############################################
##############################################
package PDL::PP::PdlParObj;
use Carp;
use PDL::Types;
# check for bad value support
#
use PDL::Config;
my $usenan = $PDL::Config{BADVAL_USENAN} || 0;
our %Typemap = ();
use PDL::Types ':All';
# build a typemap for our translation purposes
# again from info in PDL::Types
for my $typ (typesrtkeys) {
$Typemap{typefld($typ,'ppforcetype')} = {
Ctype => typefld($typ,'ctype'),
Cenum => typefld($typ,'sym'),
Val => typefld($typ,'numval'),
};
}
# Try to load Text::Balanced
my $hasTB = 0;
eval q{
use Text::Balanced;
$hasTB = 1;
};
# split regex $re separated arglist
# but ignore bracket-protected bits
# (i.e. text that is within matched brackets)
# fallback to simple split if we can't find Text::Balanced
my $prebrackreg = qr/^([^\(\{\[]*)/;
sub splitprotected ($$) {
my ($re,$txt) = @_;
return split $re, $txt unless $hasTB;
return () if !defined $txt || $txt =~ /^\s*$/;
my ($got,$pre) = (1,'');
my @chunks = ('');
my $ct = 0; # infinite loop protection
while ($got && $txt =~ /[({\[]/ && $ct++ < 1000) {
# print "iteration $ct\n";
($got,$txt,$pre) =
Text::Balanced::extract_bracketed($txt,'{}()[]',$prebrackreg);
my @partialargs = split $re, $pre, -1;
$chunks[-1] .= shift @partialargs if @partialargs;
push @chunks, @partialargs;
$chunks[-1] .= $got;
}
confess "possible infinite parse loop, splitting '$txt' "
if $ct >= 1000;
my @partialargs = split $re, $txt, -1;
$chunks[-1] .= shift @partialargs if @partialargs;
push @chunks, @partialargs if @partialargs;
# print STDERR "args found: $#chunks\n";
# print STDERR "splitprotected $txt on $re: [",join('|',@chunks),"]\n";
return @chunks;
}
# null != [0]
# - in Core.
#{package PDL;
# sub isnull {
# my $this = shift;
# return ($this->getndims==1 && $this->getdim(0)==0) ? 1:0 }
#}
1;
#__DATA__
# need for $badflag is due to hacked get_xsdatapdecl()
# - this should disappear when (if?) things are done sensibly
#
my $typeregex = join '|', map {typefld($_,'ppforcetype')} typesrtkeys;
our $pars_re = qr/^
\s*((?:$typeregex)[+]*|)\s* # $1: first option
(?:
\[([^]]*)\] # $2: The initial [option] part
)?\s*
(\w+) # $3: The name
\(([^)]*)\) # $4: The indices
/x;
sub new {
my($type,$string,$number,$badflag) = @_;
$badflag ||= 0;
my $this = bless {Number => $number, BadFlag => $badflag},$type;
# Parse the parameter string. Note that the regexes for this match were
# originally defined here, but were moved to PDL::PP for FullDoc parsing.
$string =~ $pars_re
or confess "Invalid pdl def $string (regex $typeregex)\n";
my($opt1,$opt2,$name,$inds) = ($1,$2,$3,$4);
map {$_ = '' unless defined($_)} ($opt1,$opt2,$inds); # shut up -w
print "PDL: '$opt1', '$opt2', '$name', '$inds'\n"
if $::PP_VERBOSE;
# Set my internal variables
$this->{Name} = $name;
$this->{Flags} = [(split ',',$opt2),($opt1?$opt1:())];
for(@{$this->{Flags}}) {
/^io$/ and $this->{FlagW}=1 or
/^nc$/ and $this->{FlagNCreat}=1 or
/^o$/ and $this->{FlagOut}=1 and $this->{FlagCreat}=1 and $this->{FlagW}=1 or
/^oca$/ and $this->{FlagOut}=1 and $this->{FlagCreat}=1 and $this->{FlagW}=1
and $this->{FlagCreateAlways}=1 or
/^t$/ and $this->{FlagTemp}=1 and $this->{FlagCreat}=1 and $this->{FlagW}=1 or
/^phys$/ and $this->{FlagPhys} = 1 or
/^((?:$typeregex)[+]*)$/ and $this->{Type} = $1 and $this->{FlagTyped} = 1 or
confess("Invalid flag $_ given for $string\n");
}
# if($this->{FlagPhys}) {
# # warn("Warning: physical flag not implemented yet");
# }
if ($this->{FlagTyped} && $this->{Type} =~ s/[+]$// ) {
$this->{FlagTplus} = 1;
}
if($this->{FlagNCreat}) {
delete $this->{FlagCreat};
delete $this->{FlagCreateAlways};
}
my @inds = map{
s/\s//g; # Remove spaces
$_;
} split ',', $inds;
$this->{RawInds} = [@inds];
return $this;
}
sub name {return (shift)->{Name}}
sub add_inds {
my($this,$dimsobj) = @_;
$this->{IndObjs} = [map {$dimsobj->get_indobj_make($_)}
@{$this->{RawInds}}];
my %indcount;
$this->{IndCounts} = [
map {
0+($indcount{$_->name}++);
} @{$this->{IndObjs}}
];
$this->{IndTotCounts} = [
map {
($indcount{$_->name});
} @{$this->{IndObjs}}
];
}
# do the dimension checking for perl level threading
# assumes that IndObjs have been created
sub perldimcheck {
my ($this,$pdl) = @_;
croak ("can't create ".$this->name) if $pdl->isnull &&
!$this->{FlagCreat};
return 1 if $pdl->isnull;
my $rdims = @{$this->{RawInds}};
croak ("not enough dimensions for ".$this->name)
if ($pdl->threadids)[0] < $rdims;
my @dims = $pdl->dims;
my ($i,$ind) = (0,undef);
for $ind (@{$this->{IndObjs}}) {
$ind->add_value($dims[$i++]);
}
return 0; # not creating
}
sub finalcheck {
my ($this,$pdl) = @_;
return [] if $pdl->isnull;
my @corr = ();
my @dims = $pdl->dims;
my ($i,$ind) = (0,undef);
for $ind (@{$this->{IndObjs}}) {
push @corr,[$i-1,$ind->{Value},$dims[$i-1]] if $dims[$i++] != $ind->{Value};
}
return [@corr];
}
# get index sizes for a parameter that has to be created
sub getcreatedims {
my $this = shift;
return map
{ croak "can't create: index size ".$_->name." not initialised"
if !defined($_->{Value}) || $_->{Value} < 1;
$_->{Value} } @{$this->{IndObjs}};
}
# find the value for a given PDL type
sub typeval {
my $ctype = shift;
my @match = grep {$Typemap{$_}->{Ctype} =~ /^$ctype$/} keys(%Typemap);
if ($#match < 0) {
use Data::Dumper;
print Dumper \%Typemap;
croak "unknown PDL type '$ctype'" ;
}
return $Typemap{$match[0]}->{Val};
}
# return the PDL type for this pdl
sub ctype {
my ($this,$generic) = @_;
return $generic unless $this->{FlagTyped};
croak "ctype: unknownn type"
unless defined($Typemap{$this->{Type}});
my $type = $Typemap{$this->{Type}}->{Ctype};
if ($this->{FlagTplus}) {
$type = $Typemap{$this->{Type}}->{Val} >
PDL::PP::PdlParObj::typeval($generic) ?
$Typemap{$this->{Type}}->{Ctype} : $generic;
}
return $type;
}
# return the enum type for a parobj; it'd better be typed
sub cenum {
my $this = shift;
croak "cenum: unknown type [" . $this->{Type} . "]"
unless defined($PDL::PP::PdlParObj::Typemap{$this->{Type}});
return $PDL::PP::PdlParObj::Typemap{$this->{Type}}->{Cenum};
}
sub get_nname{ my($this) = @_;
"(\$PRIV(pdls[$this->{Number}]))";
}
sub get_nnflag { my($this) = @_;
"(\$PRIV(vtable->per_pdl_flags[$this->{Number}]))";
}
# XXX There might be weird backprop-of-changed stuff for [phys].
#
# Have changed code to assume that, if(!$this->{FlagCreat})
# then __creating[] will == 0
# -- see make_redodims_thread() in ../PP.pm
#
sub get_xsnormdimchecks {
my($this) = @_;
my $pdl = $this->get_nname;
my $iref = $this->{IndObjs};
my $ninds = 0+scalar(@$iref);
my $str = "";
$str .= "if(!__creating[$this->{Number}]) {\n" if $this->{FlagCreat};
# Dimensional Promotion when number of dims is less than required:
# Previous warning message now commented out,
# which means we only need include the code if $ninds > 0
#
if ( $ninds > 0 ) {
$str .= " if(($pdl)->ndims < $ninds) {\n" .
join('', map {
my $size = $iref->[$_-1]->get_size();
" if (($pdl)->ndims < $_ && $size <= 1) $size = 1;\n"
} (1..$ninds))
# XXX why is this here, commented, and not removed? If re-inserted, be sure to use PDL_COMMENT
## ." /* \$CROAK(\"Too few dimensions for argument \'$this->{Name}\'\\n\"); */\n"
. " }\n";
}
# Now, the real check.
my $no = 0;
for( @$iref ) {
my $siz = $_->get_size();
my $dim = "($pdl)->dims[$no]";
my $ndims = "($pdl)->ndims";
$str .= " if($siz == -1 || ($ndims > $no && $siz == 1)) {\n" .
" $siz = $dim;\n" .
" } else if($ndims > $no && $siz != $dim) {\n" .
# XXX should these lines simply be removed? If re-inserted, be sure to use PDL_COMMENT
# " if($dim == 1) {\n" .
# " /* Do nothing */ /* XXX Careful, increment? */" .
# " } else {\n" .
" if($dim != 1) {\n" .
" \$CROAK(\"Wrong dims\\n\");\n" .
" }\n }\n";
$no++;
}
$str .= "PDL->make_physical(($pdl));\n" if $this->{FlagPhys};
if ( $this->{FlagCreat} ) {
$str .= "} else {\n";
# We are creating this pdl.
$str .= " PDL_Indx dims[".($ninds+1)."]; PDL_COMMENT(\"Use ninds+1 to avoid smart (stupid) compilers\")";
$str .= join "",
(map {"dims[$_] = ".$iref->[$_]->get_size().";"} 0 .. $#$iref);
my $istemp = $this->{FlagTemp} ? 1 : 0;
$str .="\n PDL->thread_create_parameter(&\$PRIV(__pdlthread),$this->{Number},dims,$istemp);\n";
$str .= "}";
}
return $str;
} # sub: get_xsnormdimchecks()
sub get_incname {
my($this,$ind) = @_;
if($this->{IndTotCounts}[$ind] > 1) {
"__inc_".$this->{Name}."_".($this->{IndObjs}[$ind]->name).$this->{IndCounts}[$ind];
} else {
"__inc_".$this->{Name}."_".($this->{IndObjs}[$ind]->name);
}
}
sub get_incdecls {
my($this) = @_;
if(scalar(@{$this->{IndObjs}}) == 0) {return "";}
(join '',map {
"PDL_Indx ".($this->get_incname($_)).";";
} (0..$#{$this->{IndObjs}}) ) . ";"
}
sub get_incregisters {
my($this) = @_;
if(scalar(@{$this->{IndObjs}}) == 0) {return "";}
(join '',map {
"register PDL_Indx ".($this->get_incname($_))." = \$PRIV(".
($this->get_incname($_)).");\n";
} (0..$#{$this->{IndObjs}}) )
}
sub get_incdecl_copy {
my($this,$fromsub,$tosub) = @_;
join '',map {
my $iname = $this->get_incname($_);
&$fromsub($iname)."=".&$tosub($iname).";";
} (0..$#{$this->{IndObjs}})
}
sub get_incsets {
my($this,$str) = @_;
my $no=0;
(join '',map {
"if($str->ndims <= $_ || $str->dims[$_] <= 1)
\$PRIV(".($this->get_incname($_)).") = 0; else
\$PRIV(".($this->get_incname($_)).
") = ".($this->{FlagPhys}?
"$str->dimincs[$_];" :
"PDL_REPRINC($str,$_);");
} (0..$#{$this->{IndObjs}}) )
}
# Print an access part.
sub do_access {
my($this,$inds,$context) = @_;
my $pdl = $this->{Name};
# Parse substitutions into hash
my %subst = map
{/^\s*(\w+)\s*=>\s*(\S*)\s*$/ or confess "Invalid subst $_\n"; ($1,$2)}
splitprotected ',',$inds;
# Generate the text
my $text;
$text = "(${pdl}_datap)"."[";
$text .= join '+','0',map {
$this->do_indterm($pdl,$_,\%subst,$context);
} (0..$#{$this->{IndObjs}});
$text .= "]";
# If not all substitutions made, the user probably made a spelling
# error. Barf.
if(scalar(keys %subst) != 0) {
confess("Substitutions left: ".(join ',',keys %subst)."\n");
}
return "$text PDL_COMMENT(\"ACCESS($access)\") ";
}
sub has_dim {
my($this,$ind) = @_;
my $h = 0;
for(@{$this->{IndObjs}}) {
$h++ if $_->name eq $ind;
}
return $h;
}
sub do_resize {
my($this,$ind,$size) = @_;
my @c;my $index = 0;
for(@{$this->{IndObjs}}) {
push @c,$index if $_->name eq $ind; $index ++;
}
my $pdl = $this->get_nname;
return (join '',map {"$pdl->dims[$_] = $size;\n"} @c).
"PDL->resize_defaultincs($pdl);PDL->allocdata($pdl);".
$this->get_xsdatapdecl(undef,1);
}
sub do_pdlaccess {
my($this) = @_;
return '$PRIV(pdls['.$this->{Number}.'])';
}
sub do_pointeraccess {
my($this) = @_;
return $this->{Name}."_datap";
}
sub do_physpointeraccess {
my($this) = @_;
return $this->{Name}."_physdatap";
}
sub do_indterm { my($this,$pdl,$ind,$subst,$context) = @_;
# Get informed
my $indname = $this->{IndObjs}[$ind]->name;
my $indno = $this->{IndCounts}[$ind];
my $indtot = $this->{IndTotCounts}[$ind];
# See if substitutions
my $substname = ($indtot>1 ? $indname.$indno : $indname);
my $incname = $indname.($indtot>1 ? $indno : "");
my $index;
if(defined $subst->{$substname}) {$index = delete $subst->{$substname};}
else {
# No => get the one from the nearest context.
for(reverse @$context) {
if($_->[0] eq $indname) {$index = $_->[1]; last;}
}
}
if(!defined $index) {confess "Access Index not found: $pdl, $ind, $indname
On stack:".(join ' ',map {"($_->[0],$_->[1])"} @$context)."\n" ;}
# return "\$PRIV(".($this->get_incname($ind))."*". $index .")";
# Now we have them in register variables -> no PRIV
return ("(".($this->get_incname($ind))."*".
"PP_INDTERM(".$this->{IndObjs}[$ind]->get_size().", $index))");
}
# XXX hacked to create a variable containing the bad value for
# this piddle.
# This is a HACK (Doug Burke 07/08/00)
# XXX
#
sub get_xsdatapdecl {
my($this,$genlooptype,$asgnonly) = @_;
my $type;
my $pdl = $this->get_nname;
my $flag = $this->get_nnflag;
my $name = $this->{Name};
$type = $this->ctype($genlooptype) if defined $genlooptype;
my $declini = ($asgnonly ? "" : "\t$type *");
my $cast = ($type ? "($type *)" : "");
# ThreadLoop does this for us.
# return "$declini ${name}_datap = ($cast((${_})->data)) + (${_})->offs;\n";
my $str = "$declini ${name}_datap = ($cast(PDL_REPRP_TRANS($pdl,$flag)));\n" .
"$declini ${name}_physdatap = ($cast($pdl->data));\n";
# assuming we always need this
# - may not be true - eg if $asgnonly ??
# - not needed for floating point types when using NaN as bad values
if ( $this->{BadFlag} and $type and
( $usenan == 0 or $type !~ /^PDL_(Float|Double)$/ ) ) {
my $cname = $type; $cname =~ s/^PDL_//;
# $str .= "\t$type ${name}_badval = PDL->bvals.$cname;\n";
$str .= "\t$type ${name}_badval = ($type) PDL->get_pdl_badvalue($pdl);\n";
}
return "$str\n";
}
1;
|