Skip to content

Commit

Permalink
use PDL_MYDIMS_TRANS to detect whether output pdls were passed as null
Browse files Browse the repository at this point in the history
  • Loading branch information
moocow-the-bovine committed Dec 28, 2024
1 parent 3f0e6c9 commit 4126ab2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
22 changes: 7 additions & 15 deletions CCS/Ufunc/ccsufunc.pd
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,13 @@ vvpp_def(
indx [o]nOut();
',

##-- try dimension-twiddling via RedoDimsCode
##-- dimension-twiddling via RedoDimsCode
## + null-detection adapted from PDL_MAYBE_SIZE macro at https://github.com/PDLPorters/pdl-linearalgebra/blob/f789c4100d04ba9d1b50f8c18249bdef29338496/Real/real.pd#L63-L75
RedoDimsCode => q(
fprintf(stderr, "RedoDimsCode: got NnzOut=%zd\n", $SIZE(NnzOut));
/*
* If $ixOut and $nzvalsOut are passed as PDL->null, this gets NnzOut=1.
*
* Is there a better way to determine if we were passed a null output PDL? (maybe
* the user is really doing a flat sum() and know that they're going to need
* only 1 output value)
* --> looking it Core.xs isnull def: pdl *self; RETVAL= !!(self->state & PDL_NOMYDIMS);
*/
fprintf(stderr, "RedoDimsCode: got ixOut.isnull=%d\n", !!($COMP(ixOut)->state & PDL_NOMYDIMS));
/* --> no joy; sadly, this gets us "ixOut.isnull=1" */
),
if ( $PDL(ixOut)->state & $PDL(nzvalsOut)->state & PDL_MYDIMS_TRANS ) {
/*-- output PDLs passed as null; use default output dimensions --*/
$SIZE(NnzOut) = $SIZE(NnzIn);
}),

##-- code
'Code' => '
Expand Down Expand Up @@ -173,7 +165,7 @@ if (0) {
$nOut //= PDL->null; # ok
$ixOut //= PDL->null;
$nzvalsOut //= PDL->null;
print STDERR "PM-DEBUG: passing ixOut=$ixOut ; nzvalsOut=$nzvalsOut ; nOut=$nOut\n";
print STDERR "PM-DEBUG: passing ixIn~(", join(" ", $ixIn->dims), ") ; ixOut=$ixOut ; nzvalsOut=$nzvalsOut ; nOut=$nOut\n";
##
##-- guts
Expand Down
26 changes: 26 additions & 0 deletions CCS/Ufunc/t/01_ufunc.t
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ sub test_ufunc {

my $BAD = pdl(0)->setvaltobad(0);

##----------------------------------------------------------------------
## generic tests

for my $missing (0,1,31,$BAD) {
for my $pdl_ufunc_name (
qw(sumover),
Expand All @@ -92,6 +95,29 @@ for my $missing (0,1,31,$BAD) {
}
}


##----------------------------------------------------------------------
## specific tests

##-- test explicit output allocation
my $dense_rv = $a->sumover;
my $which_prealloc = zeroes(indx, 1, 6);
my $nzvals_prealloc = zeroes($a->type, 6);
foreach (
[null, null],
[null, $nzvals_prealloc],
[$which_prealloc, null],
[$which_prealloc, $nzvals_prealloc],
) {
my $label = "sumover with explicit output PDLs (".join(', ', map {$_->isnull ? 'null' : 'pre-allocated'} @$_).")";
my ($tmp_which, $tmp_nzvals) = @$_;
my ($which_rv,$nzvals_rv) = ccs_accum_sum($awhich1, $avals, 0, 0, $tmp_which, $tmp_nzvals);
my $decoded_rv = $dense_rv->zeroes;
$decoded_rv->indexND($which_rv) .= $nzvals_rv;

pdlok($label, $decoded_rv, $dense_rv);
}

print "\n";

done_testing;

0 comments on commit 4126ab2

Please sign in to comment.