blib/lib/PDL/CCS/IO/PETSc.pm

filter: Statementstmt 100% 116 / 116 Branchbran 75.0% 36 / 48 Conditioncond 54.2% 19 / 35 MC/DCmcdc 21.0% 4 / 19 Subroutinesub 100% 11 / 11 Podpod 100% 2 / 2 totaltotal 81.3% 188 / 231 CC 40 SCAR 30.1
CC
40
Cov
86%
ccs_rpetsc
29.9
ccs_wpetsc
16.5
BEGIN
1.0
CRAP 20.4
« blib/lib/PDL/CCS/IO/LDAC.pm ↑ Summary CCS/t/06_matops.t »
1 ## File: PDL::CCS::IO::PETSc.pm
2 ## Author: Bryan Jurish <moocow@cpan.org>
3 ## Description: LDA-C wrappers for PDL::CCS::Nd
4  
5 package PDL::CCS::IO::PETSc;
6 1 ▶use PDL::CCS::Version;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use PDL::CCS::Config qw(ccs_indx);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use PDL::CCS::Nd;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use PDL::CCS::IO::Common qw(:intern); ##-- for e.g. _ccsio_open(), _ccsio_close()
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use PDL;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Fcntl qw(:seek); ##-- for rewinding
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 our $VERSION = '1.24.1';
16 1 our @ISA = ('PDL::Exporter');
17 1 our @EXPORT_OK =
18 (
19 qw(ccs_wpetsc ccs_rpetsc),
20 );
21 1 our %EXPORT_TAGS =
22 (
23 Func => [@EXPORT_OK], ##-- respect PDL conventions (hopefully)
24 );
25  
26 1 our $PETSC_ASCII_HEADER = "Matrix Object: 1 MPI processes\n type: seqaij\n";
27  
28 ##======================================================================
29 ## pod: headers
30 =pod
31  
32 =head1 NAME
33  
34 PDL::CCS::IO::PETSc - PETSc-compatible I/O for PDL::CCS::Nd
35  
36 =head1 SYNOPSIS
37  
38 use PDL;
39 use PDL::CCS::Nd;
40 use PDL::CCS::IO::PETSc;
41  
42 ##-- sparse 2d matrix
43 $ccs = PDL::CCS::Nd->newFromWhich($which,$nzvals);
44
45 ccs_wpetsc($ccs,"ccs.petsc"); # write a sparse binary PETSc file
46 $ccs2 = ccs_rpetsc("ccs.petsc"); # read a sparse binary PETSc file
47  
48 =cut
49  
50  
51 ##======================================================================
52 ## I/O Utilities
53 =pod
54  
55 =head1 I/O Utilities
56  
57 =cut
58  
59 ##---------------------------------------------------------------
60 ## ccs_wpetsc
61 =pod
62  
63 =head2 ccs_wpetsc
64  
65 Write a 2d L<PDL::CCS::Nd|PDL::CCS::Nd> matrix in PETSc sparse binary format.
66  
67 ccs_wpetsc($ccs,$filename_or_fh)
68 ccs_wpetsc($ccs,$filename_or_fh,\%opts)
69  
70 Options %opts:
71  
72 class_id => $int, ##-- PETSc MAT_FILE_CLASSID (default=1211216; see petsc/include/petscmat.h)
73 pack_int => $pack, ##-- pack template for PETSc integers (default='N')
74 pack_val => $pack, ##-- pack template for PETSc values (default='d>')
75 ioblock => $size, ##-- I/O block size (default=8192)
76  
77 =cut
78  
79 1 *PDL::ccs_wpetsc = *PDL::CCS::Nd::wpetsc = \&ccs_wpetsc;
80 2 ▶sub ccs_wpetsc {
Subroutine ccs_wpetsc: called CC=16 SCAR=28.0
Pod documented
81 2 my ($ccs,$file,$opts) = @_;
82 2 ▶ my %opts = %{$opts||{}};
Condition100%2/2
$opts || {}
l!l
11
MC/DC100%1/1
$opts || {}
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
83 2 ▶ my $class_id = $opts{class_id} // 1211216;
Condition50%1/2
$opts{"class_id"} // 1211216
l!l
02
MC/DC0%0/1
$opts{"class_id"} // 1211216
$opts{"class_id"}
Truth table50%1/2
A
A= $opts{"class_id"}
Aresult
11
00
84 2 ▶ my $pack_int = $opts{pack_int} // 'N';
Condition50%1/2
$opts{"pack_int"} // "N"
l!l
02
MC/DC0%0/1
$opts{"pack_int"} // "N"
$opts{"pack_int"}
Truth table50%1/2
A
A= $opts{"pack_int"}
Aresult
11
00
85 2 ▶ my $pack_val = $opts{pack_val} // 'd>';
Condition50%1/2
$opts{"pack_val"} // "d>"
l!l
02
MC/DC0%0/1
$opts{"pack_val"} // "d>"
$opts{"pack_val"}
Truth table50%1/2
A
A= $opts{"pack_val"}
Aresult
11
00
86 2 ▶ my $ioblock = $opts{ioblock} || 8192;
Condition100%2/2
$opts{'ioblock'} || 8192
l!l
11
MC/DC100%1/1
$opts{'ioblock'} || 8192
$opts{'ioblock'}
Truth table100%2/2
A
A= $opts{'ioblock'}
Aresult
11
00
87  
88 ##-- sanity check(s)
89 2 ▶ confess("ccs_wpetsc(): input matrix must be physically indexed 2d!")
Branch: 1 branch
BranchTrueFalseTotal
if $ccs->pdims->nelem != 2 or not $ccs->is_physically_indexed 0 2 2
Condition33%1/3
$ccs->pdims->nelem != 2 or not $ccs->is_physically_indexed
l!l&&r!l&&!r
002
MC/DC0%0/2
$ccs->pdims->nelem != 2 or not $ccs->is_physically_indexed
$ccs->pdims->nelem != 2 not $ccs->is_physically_indexed
Truth table33%1/3
A or not(B)
A= $ccs->pdims->nelem != 2B= not $ccs->is_physically_indexed
ABresult
1X1
011
000
90 if ($ccs->pdims->nelem != 2 || !$ccs->is_physically_indexed);
91  
92 ##-- open output file
93 2 ▶ my $fh = _ccsio_open($file,'>')
Branch: 1 branch
BranchTrueFalseTotal
my $fh = _ccsio_open($file, ">") or confess("ccs_wpetsc(): open failed for output file '${file}': $!") 2 0 2
94 or confess("ccs_wpetsc(): open failed for output file '$file': $!");
95 2 binmode($fh,':raw');
96 2 local $,='';
97  
98 ##-- write output data: header
99 # + Format (see file:///usr/share/doc/petsc3.4.2-doc/docs/manualpages/Mat/MatLoad.html#MatLoad)
100 # int MAT_FILE_CLASSID
101 # int number of rows
102 # int number of columns
103 # int total number of nonzeros
104 # int *number nonzeros in each row
105 # int *column indices of all nonzeros (starting index is zero)
106 # PetscScalar *values of all nonzeros
107 2 my ($m,$n,$nnz) = ($ccs->pdims->list,$ccs->_nnz_p);
108 2 $fh->print(pack("($pack_int)[4]", $class_id, $m,$n,$nnz));
109  
110 ##-- compute row-lengths
111 2 my $ptr = $ccs->ptr(0);
112 2 my $plen = $ptr->slice("1:-1") - $ptr->slice("0:-2");
113  
114 ###-- write output data: ptr lens
115 2 my ($i,$j);
116 2 ▶ for ($i=0; $i < $m; $i = $j+1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $m) 3 2 5
117 3 $j = $i+$ioblock;
118 3 ▶ $j = $m-1 if ($j >= $m);
Branch: 1 branch
BranchTrueFalseTotal
if $j >= $m 2 1 3
119 3 $fh->print(pack("($pack_int)*", $plen->slice("$i:$j")->list));
120 }
121 2 undef $plen;
122 2 undef $ptr;
123  
124 ##-- write output data: colids
125 2 my $ix = $ccs->_whichND;
126 2 ▶ for ($i=0; $i < $nnz; $i = $j+1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $nnz) 7 2 9
127 7 $j = $i+$ioblock;
128 7 ▶ $j = $nnz-1 if ($j >= $nnz);
Branch: 1 branch
BranchTrueFalseTotal
if $j >= $nnz 2 5 7
129 7 $fh->print(pack("($pack_int)*", $ix->slice("(1),$i:$j")->list));
130 }
131  
132 ##-- write output data: nzvals
133 2 my $nz = $ccs->_nzvals;
134 2 ▶ for ($i=0; $i < $nnz; $i = $j+1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $nnz) 7 2 9
135 7 $j = $i+$ioblock;
136 7 ▶ $j = $nnz-1 if ($j >= $nnz);
Branch: 1 branch
BranchTrueFalseTotal
if $j >= $nnz 2 5 7
137 7 $fh->print(pack("($pack_val)*", $nz->slice("$i:$j")->list));
138 }
139  
140 ##-- cleanup
141 2 ▶ _ccsio_close($file,$fh)
Branch: 1 branch
BranchTrueFalseTotal
_ccsio_close($file, $fh) or confess("ccs_wpetsc(): close failed for output file '${file}': $!") 2 0 2
142 or confess("ccs_wpetsc(): close failed for output file '$file': $!");
143  
144 2 return 1;
145 }
146  
147  
148 ##---------------------------------------------------------------
149 ## ccs_rpetsc
150 =pod
151  
152 =head2 ccs_rpetsc
153  
154 REad a 2d L<PDL::CCS::Nd|PDL::CCS::Nd> matrix from PETSc sparse binary format.
155  
156 $ccs = ccs_rpetsc($filename_or_fh)
157 $ccs = ccs_rpetsc($filename_or_fh,\%opts)
158  
159 Options %opts:
160  
161 pack_int => $pack, ##-- pack template for PETSc integers (default='N')
162 pack_val => $pack, ##-- pack template for PETSc values (default='d>')
163 ioblock => $size, ##-- I/O block size (default=8192)
164 type => $type, ##-- value type to return (default: double)
165 sorted => $bool, ##-- assume input is lexicographically sorted (only if not transposted; default=do)
166 flags => $flags, ##-- flags for new ccs object (default=$PDL::CCS::Nd::CCSND_FLAGS_DEFAULT)
167  
168 =cut
169  
170 1 *PDL::ccs_rpetsc = *PDL::CCS::Nd::rpetsc = \&ccs_rpetsc;
171 2 ▶sub ccs_rpetsc {
Subroutine ccs_rpetsc: called CC=25 SCAR=34.0
Pod documented
172 2 ▶ shift if (UNIVERSAL::isa($_[0],'PDL') || UNIVERSAL::isa($_[0],'PDL::CCS::Nd'));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], "PDL") or UNIVERSAL::isa($_[0], "PDL::CCS::Nd") 0 2 2
Condition33%1/3
UNIVERSAL::isa($_[0], "PDL") or UNIVERSAL::isa($_[0], "PDL::CCS::Nd")
l!l&&r!l&&!r
002
MC/DC0%0/2
UNIVERSAL::isa($_[0], "PDL") or UNIVERSAL::isa($_[0], "PDL::CCS::Nd")
UNIVERSAL::isa($_[0], "PDL") UNIVERSAL::isa($_[0], "PDL::CCS::Nd")
Truth table33%1/3
A or B
A= UNIVERSAL::isa($_[0], "PDL")B= UNIVERSAL::isa($_[0], "PDL::CCS::Nd")
ABresult
1X1
011
000
173 2 my ($file,$opts) = @_;
174 2 ▶ my %opts = %{$opts||{}};
Condition100%2/2
$opts || {}
l!l
11
MC/DC100%1/1
$opts || {}
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
175 2 ▶ my $pack_int = $opts{pack_int} // 'N';
Condition50%1/2
$opts{"pack_int"} // "N"
l!l
02
MC/DC0%0/1
$opts{"pack_int"} // "N"
$opts{"pack_int"}
Truth table50%1/2
A
A= $opts{"pack_int"}
Aresult
11
00
176 2 ▶ my $pack_val = $opts{pack_val} // 'd>';
Condition50%1/2
$opts{"pack_val"} // "d>"
l!l
02
MC/DC0%0/1
$opts{"pack_val"} // "d>"
$opts{"pack_val"}
Truth table50%1/2
A
A= $opts{"pack_val"}
Aresult
11
00
177 2 ▶ my $ioblock = $opts{ioblock} || 8192;
Condition100%2/2
$opts{'ioblock'} || 8192
l!l
11
MC/DC100%1/1
$opts{'ioblock'} || 8192
$opts{'ioblock'}
Truth table100%2/2
A
A= $opts{'ioblock'}
Aresult
11
00
178 2 my $type = $opts{type};
179 2 ▶ $type = PDL->can($type)->() if (defined($type) && !ref($type) && PDL->can($type));
Branch: 1 branch
BranchTrueFalseTotal
if defined $type and not ref $type and "PDL"->can($type) 0 2 2
Condition33%2/6
defined $type and not ref $type and "PDL"->can($type)
!ll&&!rl&&r
200
defined $type and not ref $type
!ll&&!rl&&r
200
MC/DC0%0/3
defined $type and not ref $type and "PDL"->can($type)
defined $type not ref $type "PDL"->can($type)
Truth table25%1/4
A and not(B) and C
A= defined $typeB= not ref $typeC= "PDL"->can($type)
ABCresult
0XX0
10X0
1100
1111
180 2 ▶ $type = double if (!ref($type));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $type 2 0 2
181 2 ▶ $opts{sorted} //= 1;
Condition50%1/2
$opts{"sorted"} //= 1
l!l
02
MC/DC0%0/1
$opts{"sorted"} //= 1
$opts{"sorted"}
Truth table50%1/2
A
A= $opts{"sorted"}
Aresult
11
00
182 2 ▶ $opts{flags} //= $PDL::CCS::Nd::CCSND_FLAGS_DEFAULT;
Condition33%1/3
$opts{"flags"} //= $PDL::CCS::Nd::CCSND_FLAGS_DEFAULT
l!l&&r!l&&!r
020
MC/DC0%0/2
$opts{"flags"} //= $PDL::CCS::Nd::CCSND_FLAGS_DEFAULT
$opts{"flags"} $PDL::CCS::Nd::CCSND_FLAGS_DEFAULT
Truth table33%1/3
A //= B
A= $opts{"flags"}B= $PDL::CCS::Nd::CCSND_FLAGS_DEFAULT
ABresult
1X1
011
000
183  
184 ##-- open input file
185 2 ▶ my $fh = _ccsio_open($file,'<')
Branch: 1 branch
BranchTrueFalseTotal
my $fh = _ccsio_open($file, "<") or confess("ccs_rpetsc(): open failed for input file '${file}': $!") 2 0 2
186 or confess("ccs_rpetsc(): open failed for input file '$file': $!");
187 2 binmode($fh,':raw');
188 2 local $,='';
189 1 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
190  
191 ##-- read input data: header
192 # + Format (see file:///usr/share/doc/petsc3.4.2-doc/docs/manualpages/Mat/MatLoad.html#MatLoad)
193 # int MAT_FILE_CLASSID
194 # int number of rows
195 # int number of columns
196 # int total number of nonzeros
197 2 my $ilen = length(pack($pack_int,0));
198 2 my $buf;
199 2 ▶ read($fh,$buf,$ilen*4)==($ilen*4)
Branch: 1 branch
BranchTrueFalseTotal
read($fh, $buf, $ilen * 4) == $ilen * 4 or confess("ccs_rpetsc(): failed to read ", $ilen * 4, " bytes of header data from '${file}': $!") 2 0 2
200 or confess("ccs_rpetsc(): failed to read ", $ilen*4, " bytes of header data from '$file': $!");
201 2 my ($magic,$m,$n,$nnz) = unpack("($pack_int)[4]", $buf);
202  
203 ##-- read input data: row-lengths
204 # int *number nonzeros in each row
205 2 my $plen = zeroes(ccs_indx(), $m);
206 2 my ($i,$j,$blen,$tmp);
207 2 ▶ for ($i=0; $i < $m; $i=$j+1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $m) 3 2 5
208 3 $j = $i+$ioblock;
209 3 ▶ $j = $m-1 if ($j >= $m);
Branch: 1 branch
BranchTrueFalseTotal
if $j >= $m 2 1 3
210 3 $blen = $ilen * (1+$j-$i);
211 3 ▶ read($fh,$buf,$blen)==$blen
Branch: 1 branch
BranchTrueFalseTotal
read($fh, $buf, $blen) == $blen or confess("ccs_rpetsc(): failed to read $blen bytes of length data from '${file}': $!") 3 0 3
212 or confess("ccs_rpetsc(): failed to read $blen bytes of length data from '$file': $!");
213 3 ($tmp=$plen->slice("$i:$j")) .= pdl(ccs_indx(), [unpack("($pack_int)*", $buf)]);
214 }
215  
216 ##-- setup index pdl
217 2 my $ix = zeroes(ccs_indx(),2,$nnz);
218 2 $plen->rld($plen->sequence, $ix->slice("(0),"));
219 2 undef $plen;
220  
221 ##-- read input data: column-indices
222 # int *column indices of all nonzeros (starting index is zero)
223 2 ▶ for ($i=0; $i < $nnz; $i=$j+1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $nnz) 7 2 9
224 7 $j = $i+$ioblock;
225 7 ▶ $j = $nnz-1 if ($j >= $nnz);
Branch: 1 branch
BranchTrueFalseTotal
if $j >= $nnz 2 5 7
226 7 $blen = $ilen * (1+$j-$i);
227 7 ▶ read($fh,$buf,$blen)==$blen
Branch: 1 branch
BranchTrueFalseTotal
read($fh, $buf, $blen) == $blen or confess("ccs_rpetsc(): failed to read $blen bytes of column-index data from '${file}': $!") 7 0 7
228 or confess("ccs_rpetsc(): failed to read $blen bytes of column-index data from '$file': $!");
229 7 ($tmp=$ix->slice("(1),$i:$j")) .= pdl(ccs_indx(), [unpack("($pack_int)*", $buf)]);
230 }
231  
232 ##-- read input data: nzvals
233 # PetscScalar *values of all nonzeros
234 2 my $vlen = length(pack($pack_val,0));
235 2 my $nz = zeroes($type, $nnz+1);
236 2 ▶ for ($i=0; $i < $nnz; $i = $j+1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $nnz) 7 2 9
237 7 $j = $i+$ioblock;
238 7 ▶ $j = $nnz-1 if ($j >= $nnz);
Branch: 1 branch
BranchTrueFalseTotal
if $j >= $nnz 2 5 7
239 7 $blen = $vlen * (1+$j-$i);
240 7 ▶ read($fh,$buf,$blen)==$blen
Branch: 1 branch
BranchTrueFalseTotal
read($fh, $buf, $blen) == $blen or confess("ccs_rpetsc(): failed to read $vlen bytes of nonzero-value data from '${file}': $!") 7 0 7
241 or confess("ccs_rpetsc(): failed to read $vlen bytes of nonzero-value data from '$file': $!");
242 7 ($tmp=$nz->slice("$i:$j")) .= pdl($type, [unpack("($pack_val)*", $buf)]);
243 }
244  
245 ##-- cleanup
246 2 ▶ _ccsio_close($file,$fh)
Branch: 1 branch
BranchTrueFalseTotal
_ccsio_close($file, $fh) or confess("ccs_wpetsc(): close failed for output file '${file}': $!") 2 0 2
247 or confess("ccs_wpetsc(): close failed for output file '$file': $!");
248  
249 ##-- construct and return
250 return PDL::CCS::Nd->newFromWhich($ix,$nz,
251 pdims=>[$m,$n],
252 flags=>$opts{flags},
253 sorted=>$opts{sorted},
254 2 steal=>1,
255 );
256 }
257  
258  
259 1 1; ##-- be happy
260  
261 ##======================================================================
262 ## POD: footer
263 =pod
264  
265 =head1 ACKNOWLEDGEMENTS
266  
267 Perl by Larry Wall.
268  
269 PDL by Karl Glazebrook, Tuomas J. Lukka, Christian Soeller, and others.
270  
271 =cut
272  
273  
274 ##---------------------------------------------------------------------
275 =pod
276  
277 =head1 AUTHOR
278  
279 Bryan Jurish E<lt>moocow@cpan.orgE<gt>
280  
281 =head2 Copyright Policy
282  
283 Copyright (C) 2015-2024, Bryan Jurish. All rights reserved.
284  
285 This package is free software, and entirely without warranty.
286 You may redistribute it and/or modify it under the same terms
287 as Perl itself.
288  
289 =head1 SEE ALSO
290  
291 L<perl>,
292 L<PDL>,
293 L<PDL::CCS::Nd>,
294 L<PDL::CCS::IO::FastRaw>,
295 L<PDL::CCS::IO::FITS>,
296 L<PDL::CCS::IO::MatrixMarket>,
297 L<PDL::CCS::IO::LDAC>,
298 the PETSc binary matrix format definition at L<http://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Mat/MatLoad.html>,
299 the PETSc homepage at L<http://www.mcs.anl.gov/petsc/>.
300 ...
301  
302 =cut
303  
304  
305 1 1; ##-- make perl happy
« blib/lib/PDL/CCS/IO/LDAC.pm ↑ Summary CCS/t/06_matops.t »