Func/func.pd

filter: Statementstmt 74.3% 327 / 440 Branchbran 56.3% 134 / 238 Conditioncond 47.2% 17 / 36 MC/DCmcdc 13.0% 3 / 23 Subroutinesub 80.0% 28 / 35 Podpod 11.5% 3 / 26 totaltotal 64.1% 512 / 798 CC 142 SCAR 34.9
CC
142
Cov
67%
make_ccode
73.4
generate_C_source
60.2
DESTROY
50.4
CRAP 32.9
« levmar.pd ↑ Summary Func/t/func.t »
1 use strict;
2 use warnings;
3 use Carp;
4  
5 our $VERSION = '0.0103';
6 pp_setversion( $VERSION );
7  
8 pp_bless('PDL::Fit::Levmar::Func'); # make the functions generated go into our namespace
9 pp_addpm({At=>'Top'},<<'======EOD_Documentation======');
10 =head1 NAME
11  
12 PDL::Fit::Levmar::Func - Create model functions for Levenberg-Marquardt fit routines
13  
14 =head1 DESCRIPTION
15  
16 This module creates and manages functions for use with the
17 Levmar fitting module. The functions are created with a
18 very simple description language (that is mostly C), or are
19 pure C, or are perl code. For many applications, the present
20 document is not necessary because levmar uses the
21 Levmar::Func module transparantly. Therefore, before
22 reading the following, refer to L<PDL::Fit::Levmar>.
23  
24 =head1 SYNOPSIS
25  
26 use PDL::Fit::Levmar::Func;
27  
28 $func = levmar_func( FUNC => # define and link function
29  
30 '
31 function gaussian
32 loop
33 x[i] = p[0] * exp(-(t[i] - p[1])*(t[i] - p[1])*p[2]);
34 end function
35  
36 jacobian jacgaussian
37 double arg, expf;
38 loop
39 arg = t[i] - p[1];
40 expf = exp(-arg*arg*p[2]);
41 d0 = expf;
42 d1 = p[0]*2*arg*p[2]*expf;
43 d2 = p[0]*(-arg*arg)*expf;
44 end jacobian
45 '
46 );
47  
48 $hout = PDL::Fit::Levmar::levmar($p,$x,$t, # fit the data
49 FUNCTION => $func,
50 );
51  
52 =head1 FUNCTIONS
53  
54 =head2 levmar_func()
55  
56 =for usage
57  
58 levmar_func( OPTIONS )
59  
60 =for ref
61  
62 This function creates and links a function, ie, takes a function definition
63 and returns a function (object instance) ready for use by levmar.
64 see PDL::Fit::Levmar::Func for more information.
65  
66 OPTIONS:
67  
68 =for options
69  
70 Some of the options are described in the PDL::Fit::Levmar documentation.
71  
72 MKOBJ -- command to compile source into object code. This can be set.
73 The default value is determined by the perl installation and can
74 be determined by examining the Levmar::Func object returned by
75 new or the output hash of a call to levmar. A typical value is
76 cc -c -O2 -fPIC -o %o %c
77  
78 MKSO -- command to convert object code to dynamic library code. A typical
79 default value is
80 cc -shared -L/usr/local/lib -fstack-protector %o -o %s
81
82  
83 CTOP -- The value of this string will be written at the top of the c code
84 written by Levmar::Func. This can be used to include headers and so forth.
85 This option is actually set in the Levmar::Func object.
86  
87  
88 =head2 call()
89  
90 =for ref
91  
92 Call (evaluate) the fit function in a Levmar::Func object.
93  
94 =for usage
95  
96 use PDL::Fit::Levmar::Func;
97  
98 $Gh = levmar_func(FUNC=>$fcode);
99  
100 $x = $Gh->call($p,$t);
101  
102 Here $fcode is a function definition (say lpp code). (This does
103 not currently work with perl subroutines, I think. Of course,
104 you can call the subroutine directly. But it would be good
105 to support it for consistency.)
106
107 $p is a pdl or ref to array of parameters.
108 $t is a pdl of co-ordinate values.
109 $x is the fit function evaluated at $p and $t. $x has the
110 same shape as $t.
111  
112 =for example
113  
114 $Gf = levmar_func( FUNC => '
115 function
116 x = p0 * t * t;
117 ');
118  
119 print $Gf->call([2],sequence(10)) , "\n";
120 [0 2 8 18 32 50 72 98 128 162]
121  
122 =cut
123 2 ▶ 
Subroutine BEGIN: called CC=1 SCAR=0.0
124 2 ▶ 
Subroutine BEGIN: called CC=1 SCAR=0.0
125 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
126 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
127 2 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
128 use File::Path;
129 use File::Spec::Functions;
130  
131 sub munlink;
132 4 ▶ 
Subroutine BEGIN: called CC=1 SCAR=0.0
133 #use PDL::NiceSlice;
134 4 ▶use PDL::Core ':Internal'; # For topdl()
Subroutine BEGIN: called CC=1 SCAR=0.0
135 4  
136 use vars ('$MKOBJ', '$MKSO', '$OBJ_EXT' , '$SO',
137 '$LEVFUNC', '$JLEVFUNC', '$LPPEXT' );
138  
139 # These are pointers to the C functions that wrap the user's
140 # perl fit functions.
141 #$LEVFUNC = get_perl_func_wrap();
142 #$JLEVFUNC = get_perl_jac_wrap();
143  
144 $OBJ_EXT = $Config{obj_ext};
145 $SO = $Config{so};
146 $LPPEXT = '.lpp';
147 # string to compile .c to .o
148 $MKOBJ = $Config{cc} . " -c " . $Config{optimize} . " " .
149 $Config{cccdlflags} . " -o %o %c ";
150  
151 # string to compile .o to .so
152 2 ▶$MKSO = $Config{ld} . " " . $Config{lddlflags} ." %o -o %s ";
Subroutine BEGIN: called CC=1 SCAR=0.0
153  
154 use File::Temp qw(tempdir);
155  
156 ======EOD_Documentation======
157  
158 pp_addpm({At=>'Bot'},<<'======EOD_Authors======');
159  
160 =head1 AUTHORS
161  
162 Copyright (C) 2006 John Lapeyre.
163 All rights reserved. There is no warranty. You are allowed
164 to redistribute this software / documentation under certain
165 conditions. For details, see the file COPYING in the PDL
166 distribution. If this file is separated from the PDL distribution,
167 the copyright notice should be included in the file.
168  
169 =cut
170  
171 ======EOD_Authors======
172  
173 pp_addhdr('
174 #include <stdio.h>
175 #include <stdlib.h>
176 #include "func.h"
177  
178 ');
179  
180 pp_add_exported('', 'levmar_func');
181 pp_addpm(<<'======EOD_new======');
182  
183 =head2 new
184  
185 =for usage
186  
187 my $fitfunc = new PDL::Fit::Levmar::Func(...);
188 or
189 my $fitfunc = levmar_func(...);
190  
191 =for ref
192  
193 Generic constructor. Use levmar_func rather than new.
194  
195 =cut
196  
197 $PDL::Fit::Levmar::Func::SRC_EXT = ".c";
198 #$PDL::Fit::Levmar::Func::SO_EXT = ".so";
199 #$PDL::Fit::Levmar::Func::OBJ_EXT = ".o";
200 32 ▶ 
Subroutine deb: called CC=1 SCAR=0.0
Pod undocumented
201 sub deb {
202 # print STDERR $_[0], "\n";
203 }
204 0 ▶ 
Subroutine prwarn: not called CC=1 SCAR=6.9
Pod undocumented
205 sub prwarn { print STDERR $_[0], "\n";}
206  
207 ##########################################
208 # Methods follow
209  
210 # some kind of stack corruption is related to this function, maybe. no probably not.
211 # So, only shift opts if they are there
212 4 ▶ 
Subroutine new: called CC=5 SCAR=16.8
Pod documented
213 4 sub new {
214 4 my($class) = shift;
215 my($opts) = shift;
216 4 ▶# print STDERR "In new class: $class, opts $opts\n";
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts ne "HASH") 4 0 4
217 4 ▶ if(ref $opts ne 'HASH') { # change opts to hash of opts
Branch: 1 branch
BranchTrueFalseTotal
defined $opts ? : 4 0 4
218 $opts = defined $opts ? {$opts,@_} : {} ;
219 }
220 my $self = { # following are defaults
221 MKOBJ => $MKOBJ,
222 MKSO => $MKSO,
223 NOCLEAN => undef,
224 FUNC => undef,
225 JFUNC => undef,
226 # TYPE => 'double',
227 NOSO => undef,
228 CSRC => undef,
229 4 CTOP => undef,
230 DIR => tempdir( CLEANUP => !$opts->{NOCLEAN} ), # where the .so is built
231 TESTSYNTAX => undef,
232 LIBHANDLE => undef,
233 FVERBOSE => 0,
234 4 };
235 5 ▶ foreach ( keys %$opts ) { # replace defaults
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$_}) { } 5 0 5
236 5 if ( exists $self->{$_} ) {
237 $self->{$_} = $opts->{$_};
238 0 }
239 else { die "Levmar::Func::new: Unrecognized option to Levmar::Func '$_'";}
240 4 }
241 return bless $self,$class;
242 }
243  
244 # all these attempts at dying and croaking fail.
245 # program does not quit and error messages are not printed
246 # dont know why. It must be some bad bug in the xs code.
247 # If I just print the error message, its fine.
248 4 ▶# I think it has to do with DESTROY. return does not work either
Subroutine DESTROY: called CC=24 SCAR=39.2
249 4 sub DESTROY {
250 4 my $self = shift;
251 4 ▶ my $cnt = 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"NOSO"} 0 4 4
252 4 ▶ return if defined $self->{NOSO};
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"LIBHANDLE"}) 4 0 4
253 4 if ( defined $self->{LIBHANDLE}) {
254 4 ▶ my ($ret, $err) = close_shared_object_file($self->{LIBHANDLE});
Branch: 1 branch
BranchTrueFalseTotal
if ($err ne "" or $ret != 0) 0 4 4
Condition33%1/3
$err ne "" or $ret != 0
l!l&&r!l&&!r
004
MC/DC0%0/2
$err ne "" or $ret != 0
$err ne "" $ret != 0
Truth table33%1/3
A or B
A= $err ne ""B= $ret != 0
ABresult
1X1
011
000
255 0 if ( $err ne '' or $ret != 0) {
256 warn "Levmar::Func::DESTROY: Error closing function library:$ret '$err'";
257 4 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
if $ret != 0 0 4 4
258 4 ▶ warn "Error closing function library:$ret '$err'" if $ret != 0;
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"JPOINTER"}) 2 2 4
259 2 if ( defined $self->{JPOINTER} ) { # call close twice because we got two symbols
260 2 ▶ ($ret, $err) = close_shared_object_file($self->{LIBHANDLE});
Branch: 1 branch
BranchTrueFalseTotal
if $ret != 0 0 2 2
261 2 ▶ warn "Error closing function library:$ret '$err'" if $ret !=0 ;
Branch: 1 branch
BranchTrueFalseTotal
if ($err ne "" or $ret != 0) 0 2 2
Condition33%1/3
$err ne "" or $ret != 0
l!l&&r!l&&!r
002
MC/DC0%0/2
$err ne "" or $ret != 0
$err ne "" $ret != 0
Truth table33%1/3
A or B
A= $err ne ""B= $ret != 0
ABresult
1X1
011
000
262 0 if ( $err ne '' or $ret != 0 ) {
263 warn "Levmar::Func::DESTROY: Error closing function library:$ret '$err'";
264 }
265 4 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"SJPOINTER"}) 2 2 4
266 2 if ( defined $self->{SJPOINTER} ) { # call close twice because we got two symbols
267 2 ▶ ($ret, $err) = close_shared_object_file($self->{LIBHANDLE});
Branch: 1 branch
BranchTrueFalseTotal
if $ret != 0 0 2 2
268 2 ▶ warn "Error closing function library:$ret '$err'" if $ret !=0 ;
Branch: 1 branch
BranchTrueFalseTotal
if ($err ne "" or $ret != 0) 0 2 2
Condition33%1/3
$err ne "" or $ret != 0
l!l&&r!l&&!r
002
MC/DC0%0/2
$err ne "" or $ret != 0
$err ne "" $ret != 0
Truth table33%1/3
A or B
A= $err ne ""B= $ret != 0
ABresult
1X1
011
000
269 0 if ( $err ne '' or $ret != 0 ) {
270 warn "Levmar::Func::DESTROY: Error closing function library:$ret '$err'";
271 }
272 4 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"SFPOINTER"}) 4 0 4
273 4 if ( defined $self->{SFPOINTER} ) { # call close twice because we got two symbols
274 4 ▶ ($ret, $err) = close_shared_object_file($self->{LIBHANDLE});
Branch: 1 branch
BranchTrueFalseTotal
if $ret != 0 0 4 4
275 4 ▶ warn "Error closing function library:$ret '$err'" if $ret !=0 ;
Branch: 1 branch
BranchTrueFalseTotal
if ($err ne "" or $ret != 0) 0 4 4
Condition33%1/3
$err ne "" or $ret != 0
l!l&&r!l&&!r
004
MC/DC0%0/2
$err ne "" or $ret != 0
$err ne "" $ret != 0
Truth table33%1/3
A or B
A= $err ne ""B= $ret != 0
ABresult
1X1
011
000
276 0 if ( $err ne '' or $ret != 0 ) {
277 warn "Levmar::Func::DESTROY: Error closing function library:$ret '$err'";
278 }
279 }
280 4 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"NOCLEAN"} and defined $self->{"SONAME"}) 3 1 4
Condition66%2/3
not defined $self->{"NOCLEAN"} and defined $self->{"SONAME"}
!ll&&!rl&&r
103
MC/DC50%1/2
not defined $self->{"NOCLEAN"} and defined $self->{"SONAME"}
not defined $self->{"NOCLEAN"} defined $self->{"SONAME"}
Truth table66%2/3
not(A) and B
A= not defined $self->{"NOCLEAN"}B= defined $self->{"SONAME"}
ABresult
0X0
100
111
281 3 if (not defined $self->{NOCLEAN} and defined $self->{SONAME} ) {
282 3 foreach my $k ( qw ( SONAME )) { # clean so file
283 3 ▶ my $fn = $self->{$k};
Branch: 1 branch
BranchTrueFalseTotal
if (-e $fn) 3 0 3
284 3 ▶ if (-e $fn) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -f $fn) { } 0 3 3
285 0 if ( not -f $fn ) {
286 warn "Levmar::Func::DESTROY: Refusing to remove non-file '$fn'";
287 }
288 3 else {
289 3 ▶ $cnt = munlink $fn;
Branch: 1 branch
BranchTrueFalseTotal
if ($cnt < 1) { } 0 3 3
290 0 if ( $cnt < 1 ) {
291 warn "Levmar::Func::DESTROY: failed removing '$fn' (lib probably open)";
292 }
293 else {
294 # warn "Levmar::Func::DESTROY: SUCCESS removing '$fn'";
295 }
296 }
297 }
298 }
299 }
300 }
301 4 ▶ 
Subroutine levmar_func: called CC=11 SCAR=29.9
Pod documented
302 4 sub levmar_func { my $self = new PDL::Fit::Levmar::Func(@_);
303 4 ▶ my @err;
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'FUNC'} and $self->{'FUNC'} =~ /CODE/) { } 0 4 4
Condition66%2/3
defined $self->{"FUNC"} and $self->{"FUNC"} =~ /CODE/
!ll&&!rl&&r
130
MC/DC0%0/2
defined $self->{"FUNC"} and $self->{"FUNC"} =~ /CODE/
defined $self->{"FUNC"} $self->{"FUNC"} =~ /CODE/
Truth table66%2/3
A and B
A= defined $self->{"FUNC"}B= $self->{"FUNC"} =~ /CODE/
ABresult
0X0
100
111
304 0 if ( defined $self->{FUNC} and $self->{FUNC} =~ /CODE/ ) {
305 0 $self->{NOSO} = 1; # bad way to handle this
306 $self->{FPOINTER} = 0; # test for this in levmar
307 }
308 4 ▶ else {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"FUNC"} or defined $self->{"CSRC"}) 0 4 4
Condition66%2/3
defined $self->{"FUNC"} or defined $self->{"CSRC"}
l!l&&r!l&&!r
310
MC/DC0%0/2
defined $self->{"FUNC"} or defined $self->{"CSRC"}
defined $self->{"FUNC"} defined $self->{"CSRC"}
Truth table66%2/3
A or B
A= defined $self->{"FUNC"}B= defined $self->{"CSRC"}
ABresult
1X1
011
000
309 0 if ( not defined $self->{FUNC} and not defined $self->{CSRC} ) {
310 die "Levmar::Func::lemar_func: neither FUNC nor CSRC defined";
311 4 }
312 4 ▶ @err = $self->make_ccode();
Branch: 1 branch
BranchTrueFalseTotal
if $err[0] == -1 0 4 4
313 4 return @err if $err[0] == -1;
314 4 $self->make_build_names();
315 4 ▶ $self->write_ccode();
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->make_shared_object_file) 0 4 4
316 0 if ( not $self->make_shared_object_file() ) {
317 die "Levmar::Func::levmar_func: Compilation failed.";
318 4 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->load_fit_library) 0 4 4
319 0 if ( not $self->load_fit_library() ) {
320 die "Levmar::Func::levmar_func: Loading dynamic library failed.";
321 4 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"NOCLEAN"} 3 1 4
322 $self->clean_files() unless defined $self->{NOCLEAN};
323 4 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"JFUNC"}) 0 4 4
324 0 ▶ if ( defined $self->{JFUNC} ) { # probably won't get called
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'JFUNC'} =~ /CODE/) { } 0 0 0
325 0 if ( $self->{JFUNC} =~ /CODE/ ) {
326 $self->{NOSO} = 1;
327 }
328 0 else {
329 die "Levmar::Func::levmar_func option JFUNC must be a ref to a perl sub";
330 }
331 4 }
332 return $self;
333 }
334 4 ▶ 
Subroutine make_build_names: called CC=5 SCAR=17.2
Pod undocumented
335 4  
336 sub make_build_names {
337 4 ▶ my $self = shift;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"NAME"} 0 4 4
338 4 die "Levmar::Func::levmar_func: no name for function"
339 4 ▶ unless defined $self->{NAME};
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $q) 0 4 4
340 0 ▶ my $q = $self->{DIR};
Branch: 1 branch
BranchTrueFalseTotal
unless File::Path::mkpath($q) 0 0 0
341 if ( not -d $q ) {
342 die "Levmar::Func::levmar_func: Can't make directory (folder) '$q'"
343 4 ▶ unless File::Path::mkpath( $q );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"FNAME"} 4 0 4
344 4 }
345 4 $self->{FNAME} = $self->{NAME} unless exists $self->{FNAME}; # base file name
346 4 my $n = $self->{FNAME};
347 4 my $srcn = catfile( $q , $n . $PDL::Fit::Levmar::Func::SRC_EXT);
348 4 my $son = catfile($q ,$n . "." . $SO);
349 4 my $objn = catfile($q , $n . $OBJ_EXT);
350 4 $self->{SRCNAME} = $srcn;
351 $self->{SONAME} = $son;
352 $self->{OBJNAME} = $objn;
353 }
354 13 ▶ 
Subroutine mfqp: called CC=1 SCAR=0.0
Pod undocumented
355 13  
356 13  
357 # change this to identity, can remove sometime
358 sub mfqp {
359 my ($self,$key) = @_;
360 return $self->{$key};
361 2 ▶}
Subroutine subst_func_name: called CC=1 SCAR=0.0
Pod undocumented
362 2  
363 2 # substitue string $new for $old when it
364 # occurs after 'void'.
365 sub subst_func_name {
366 my ($sref,$old,$new) = @_;
367 $$sref =~ s/(\W)void\s+$old/$1void $new/;
368 }
369  
370 # Write c source file from function definition.
371 4 ▶# Entire definition is given as a string, or the
Subroutine make_ccode: called CC=22 SCAR=43.0
Pod undocumented
372 4 # filename , ending in $LPPEXT is given
373 4 # Copying the source strings around, inefficient. should use
374 # refs
375 4 ▶sub make_ccode {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"CSRC"}) 3 1 4
376 3 ▶ my $self = shift;
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"FUNC"} =~ /$PDL::Fit::Levmar::Func::LPPEXT$/o and $self->{"FUNC"} =~ /\.c$/ || !($self->{"FUNC"} =~ /(^|\n)\s*function(\s+\w+|\s*)\n/)) 0 3 3
Condition33%2/6
not $self->{"FUNC"} =~ /$PDL::Fit::Levmar::Func::LPPEXT$/o and $self->{"FUNC"} =~ /\.c$/ || !($self->{"FUNC"} =~ /(^|\n)\s*function(\s+\w+|\s*)\n/)
!ll&&!rl&&r
030
$self->{"FUNC"} =~ /\.c$/ or not $self->{"FUNC"} =~ /(^|\n)\s*function(\s+\w+|\s*)\n/
l!l&&r!l&&!r
003
MC/DC0%0/3
not $self->{"FUNC"} =~ /$PDL::Fit::Levmar::Func::LPPEXT$/o and $self->{"FUNC"} =~ /\.c$/ || !($self->{"FUNC"} =~ /(^|\n)\s*function(\s+\w+|\s*)\n/)
not $self->{"FUNC"} =~ /$PDL::Fit::Levmar::Func::LPPEXT$/o $self->{"FUNC"} =~ /\.c$/ not $self->{"FUNC"} =~ /(^|\n)\s*function(\s+\w+|\s*)\n/
Truth table25%1/4
not(A) and B or not(C)
A= not $self->{"FUNC"} =~ /$PDL::Fit::Levmar::Func::LPPEXT$/oB= $self->{"FUNC"} =~ /\.c$/C= not $self->{"FUNC"} =~ /(^|\n)\s*function(\s+\w+|\s*)\n/
ABCresult
0XX0
1000
11X1
1011
377 my @err;
378  
379 if ( not defined $self->{CSRC} ) { # is FUNC really c code?
380 0 if (not $self->{FUNC} =~ /$LPPEXT$/o # is not a $LPPEXT file
381 and ( $self->{FUNC} =~ /\.c$/ or # is a .c file
382 # doesnt have function def
383 4 ▶ not $self->{FUNC} =~ /(^|\n)\s*function(\s+\w+|\s*)\n/) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{'CSRC'}) { } 1 3 4
elsif (defined $self->{'FUNC'}) { } 3 0 3
384 1 ▶ $self->{CSRC} = $self->{FUNC};
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'CSRC'} =~ /\.c$/) { } 0 1 1
385 0 }
386 0 }
387 0 if ( defined $self->{CSRC} ) {
388 0 ▶ if ( $self->{CSRC} =~ /\.c$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
open CHAND, "<$cf" or die "Levmar::Func::make_code : Can't open C src file '${cf}'" 0 0 0
389 0 my $cf = $self->{CSRC};
390 0 local (*CHAND);
391 local $/;
392 open CHAND, "<$cf" or die "Levmar::Func::make_code : Can't open C src file '$cf'";
393 1 $self->{CCODE} = <CHAND>;
394 close(CHAND);
395 1 }
396 1 ▶ else { # inefficient, but it works
Branch: 1 branch
BranchTrueFalseTotal
unless defined $jacname 0 1 1
397 1 $self->{CCODE} = $self->{CSRC};
398 1 }
399 1 ▶ my ($funcname, $jacname) = $self->get_funcs_from_c();
Branch: 1 branch
BranchTrueFalseTotal
if $jacname 1 0 1
400 1 $jacname = '' unless defined $jacname;
401 1 my ($sfuncname, $sjacname);
402 1 $sfuncname = 's' . $funcname;
403 1 ▶ $sjacname = 's' . $jacname if $jacname;
Branch: 1 branch
BranchTrueFalseTotal
if $jacname 1 0 1
404 1 ▶ my $doublecode = $self->{CCODE};
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /MSWin32/i) 0 1 1
405 0 my $floatcode = $doublecode;
406 0 subst_func_name(\$floatcode, $funcname, $sfuncname);
407 0 subst_func_name(\$floatcode, $jacname, $sjacname) if $jacname;
408 0 ▶ if ($^O =~ /MSWin32/i) {
Branch: 1 branch
BranchTrueFalseTotal
if $jacname 0 0 0
409 0 ▶ my $export = ' __declspec (dllexport) ';
Branch: 1 branch
BranchTrueFalseTotal
if $jacname 0 0 0
410 subst_func_name(\$doublecode, $funcname, $export . $funcname);
411 subst_func_name(\$floatcode, $sfuncname, $export . $sfuncname);
412 subst_func_name(\$doublecode, $jacname, $export . $jacname) if $jacname;
413 1 subst_func_name(\$floatcode, $sjacname, $export . $sjacname) if $jacname;
414 }
415 # if ( /^\s*void\s+([\w]+)/ )
416 $self->{CCODE} =
417 "
418 #define FLOAT double
419 " . $doublecode .
420 1 ▶"
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"NAME"} 1 0 1
421 1 ▶#undef FLOAT
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"JACNAME"} 1 0 1
422 1 #define FLOAT float
423 " . $floatcode;
424 $self->{NAME} = $funcname unless exists $self->{NAME};
425 3 $self->{JACNAME} = $jacname unless exists $self->{JACNAME};
426 3 ▶ return (1); # don't use FUNC even if its there.
Branch: 1 branch
BranchTrueFalseTotal
if ($st =~ /$PDL::Fit::Levmar::Func::LPPEXT$/o) 0 3 3
427 0 }
428 0 elsif ( defined $self->{FUNC} ) {
429 0 ▶ my $st = $self->{FUNC};
Branch: 1 branch
BranchTrueFalseTotal
open FUNCHAND, "<$st" or die "Levmar::Func::make_ccode : Can't open def file '${st}'" 0 0 0
430 0 if ($st =~ /$LPPEXT$/o) { # file, not string
431 0 local (*FUNCHAND);
432 local $/;
433 3 open FUNCHAND, "<$st" or die "Levmar::Func::make_ccode : Can't open def file '$st'";
434 3 $st = <FUNCHAND>;
435 3 ▶ close(FUNCHAND);
Branch: 1 branch
BranchTrueFalseTotal
if ($ret[0] eq '-1') { } 0 3 3
436 0 }
437 my ($funcname,$jacname,$ccode);
438 0 my @ret = generate_C_source($self, $st);
439 if ( $ret[0] eq '-1' ) { # stop warnings with ==
440 my $r;
441 3 # ($r, $funcname, $jacname, $code, @err) = @ret;
442 return @ret;
443 3 }
444 3 ▶ else {
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"NAME"} 3 0 3
445 3 ▶ ($funcname,$jacname,$ccode) = @ret;
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"JACNAME"} 3 0 3
446 3 }
447 $self->{CCODE} = $ccode;
448 0 $self->{NAME} = $funcname unless exists $self->{NAME};
449 $self->{JACNAME} = $jacname unless exists $self->{JACNAME};
450 return (1);
451 }
452 1 ▶ return (-1, "Can't make C code with no defintion or source file");
Subroutine get_funcs_from_c: called CC=6 SCAR=18.3
Pod undocumented
453 1 }
454 1  
455 1 # jacobian function name **must** start with 'jac' if using pure c code
456 1 sub get_funcs_from_c {
457 40 ▶ my $self = shift;
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*void\s+([\w]+)/) 2 38 40
458 2 my @lines = split ("\n",$self->{CCODE});
459 2 ▶ my ($fname, $jacname) = (undef, undef);
Branch: 1 branch
BranchTrueFalseTotal
if ($fn =~ /^jac/) { } 1 1 2
460 1 foreach (@lines ) {
461 if ( /^\s*void\s+([\w]+)/ ) { # prototype begins with 'void' return type
462 my $fn = $1;
463 1 if ($fn =~ /^jac/ ) {
464 $jacname = $fn;
465 }
466 else {
467 1 ▶ $fname = $fn;
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $fname or $fname eq "") 0 1 1
Condition33%1/3
not defined $fname or $fname eq ""
l!l&&r!l&&!r
001
MC/DC0%0/2
not defined $fname or $fname eq ""
not defined $fname $fname eq ""
Truth table33%1/3
not(A) or B
A= not defined $fnameB= $fname eq ""
ABresult
1X1
011
000
468 0 }
469 }
470 1 }
471 if ( not defined $fname or $fname eq '' ) { # ok if jacname == undef
472 die "Levmar::Func::get_funcs_from_c : Can't find function name from c code";
473 4 ▶ }
Subroutine write_ccode: called CC=5 SCAR=17.1
Pod undocumented
474 4 return ($fname, $jacname);
475 4 }
476 4 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"CCODE"} 0 4 4
477 4 ▶sub write_ccode {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"NAME"}) 0 4 4
478 0 my $self = shift;
479 local (*CCODEH);
480 4 return unless exists $self->{CCODE};
481 4 ▶ if ( not defined $self->{NAME} ) {
Branch: 1 branch
BranchTrueFalseTotal
open CCODEH, ">$srcn" or die "Levmar::Func::write_ccode : Can't open C source file '${srcn}' for writing" 4 0 4
482 4 ▶ die "Levmar::Func::write_ccode : Can't write C code for function with no name";
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"CTOP"} 0 4 4
483 4 }
484 4 my $srcn = $self->{SRCNAME};
485 open CCODEH, ">$srcn" or die "Levmar::Func::write_ccode : Can't open C source file '$srcn' for writing";
486 print CCODEH $self->{CTOP}, "\n" if defined $self->{CTOP};
487 4 ▶ print CCODEH $self->{CCODE};
Subroutine make_shared_object_file: called CC=8 SCAR=25.0
Pod undocumented
488 4 close CCODEH;
489 4 }
490 4  
491 4 sub make_shared_object_file {
492 4 my $self = shift;
493 4 my $ret;
494 4 ▶ my $srcn1 = $self->{SRCNAME};
Branch: 1 branch
BranchTrueFalseTotal
if (-f $self->{"SONAME"}) 0 4 4
495 0 my $fname1 = $self->{FNAME};
496 0 my $i = 0;
497 0 my $max = 20; # allow $max renamed files
498 0 while ( -f $self->{SONAME} ) { # this is important to avoid user side bugs
499 $i++;
500 0 ▶ my $so = $self->{SONAME};
Branch: 1 branch
BranchTrueFalseTotal
if $i > $max 0 0 0
501 $self->{FNAME} = $fname1 . $i;
502 4 $self->make_build_names();
503 4 # warn "Levmar::Func::make_shared_object_file: $so exists. Trying " . $self->{SONAME};
504 4 last if $i > $max;
505 4 }
506 4 ▶ my $srcn = $self->{SRCNAME};
Branch: 2 branches
BranchTrueFalseTotal
if ($i > $max) { } 0 4 4
elsif ($i > 0) { } 0 4 4
507 0 my $objn = $self->{OBJNAME};
508 my $sobjn = $self->{SONAME};
509 my $fname = $self->{FNAME};
510 0 ▶ if ( $i > $max ){
Branch: 1 branch
BranchTrueFalseTotal
if (rename($srcn1, $srcn) == 0) 0 0 0
511 0 die 'Levmar::Func::make_shared_object_file: Too many renamed files in '. $self->{DIR} . ' try cleaning.';
512 }
513 elsif ($i > 0) {
514 if ( rename($srcn1,$srcn) == 0 ) {
515 4 die "Levmar::Func::make_shared_object_file: Faile to rename $srcn1 to $srcn";
516 4 }
517 4 }
518 4 # my $mkobjstr = $self->{MKOBJ} ." -o $objn $srcn";
519 4 my $mkobjstr = $self->{MKOBJ};
520 4 ▶ $mkobjstr =~ s/\%c/$srcn/g;
Branch: 1 branch
BranchTrueFalseTotal
if ($ret != 0) 0 4 4
521 0 $mkobjstr =~ s/\%o/$objn/g;
522 0 $ret = do_system($mkobjstr, $self); # compile c to o
523 deb "Levmar::Func::make_shared_object_file: compile returned '$ret'";
524 4 if ( $ret != 0) { # zero is success
525 4 warn "Levmar::Func::make_shared_object_file: Compiling '$srcn' failed.";
526 4 return 0;
527 4 }
528 4 $self->{MKOBJSTR} = $mkobjstr;
529 4 my $sostr = $self->{MKSO};
530 4 ▶ $sostr =~ s/\%s/$sobjn/g;
Branch: 1 branch
BranchTrueFalseTotal
if (0 != $ret) 0 4 4
531 0 $sostr =~ s/\%o/$objn/g;
532 0 $ret = do_system($sostr, $self); # make so from o
533 deb "Levmar::Func::make_shared_object_file: '$sostr' make so from o returned '$ret'";
534 4 if ( 0 != $ret ) {
535 warn "Levmar::Func::make_shared_object_file: Making shared library '$sobjn' failed.";
536 return 0;
537 }
538 $self->{SOSTR} = $sostr;
539 0 ▶}
Subroutine get_cc_str: not called CC=1 SCAR=6.9
Pod undocumented
540 0  
541 0 #### Some methods to return information
542 # get compiler strings
543 sub get_cc_str {
544 0 ▶ my $self = shift;
Subroutine get_fpoint: not called CC=1 SCAR=6.9
Pod undocumented
545 0 return ($self->{MKOBJSTR}, $self->{SOSTR});
546 0 }
547  
548 sub get_fpoint {
549 0 ▶ my $self = shift;
Subroutine get_jacpoint: not called CC=1 SCAR=6.9
Pod undocumented
550 0 $self->{FPOINTER};
551 0 }
552  
553 sub get_jacpoint {
554 0 ▶ my $self = shift;
Subroutine get_fit_pointers: not called CC=1 SCAR=6.9
Pod undocumented
555 0 $self->{JPOINTER};
556 }
557  
558 sub get_fit_pointers {
559 0 my $self = shift;
560 ($self->{FPOINTER},
561 $self->{SFPOINTER},
562 0 ▶ $self->{JPOINTER},
Subroutine get_file_names: not called CC=1 SCAR=6.9
Pod undocumented
563 0 $self->{SJPOINTER});
564 0 }
565  
566 sub get_file_names {
567 my $self = shift;
568 return ($self->mfqp('SRCNAME'),
569 $self->mfqp('OBJNAME'),
570 $self->mfqp('SONAME'));
571 3 ▶}
Subroutine clean_files: called CC=13 SCAR=26.9
Pod undocumented
572 3  
573 3 # remove generated o, and C files
574 3 # Well, don't remove so file.
575 3 sub clean_files {
576 3 my $self = shift;
577 3 my $cnt = 0;
578 3 ▶ my $srcn = $self->mfqp('SRCNAME'); # fully qualified names
Branch: 1 branch
BranchTrueFalseTotal
if (-e $fn) 3 0 3
579 3 ▶ my $objn = $self->mfqp('OBJNAME');
Branch: 1 branch
BranchTrueFalseTotal
if (not -f $fn) { } 0 3 3
580 0 foreach my $k ( qw ( OBJNAME )) { # soname needs to be there
581 my $fn = $self->mfqp($k);
582 if (-e $fn) {
583 3 if ( not -f $fn ) {
584 3 ▶ warn "Levmar::Func::clean_files : Object file '$fn' not a file. Not deleting";
Branch: 1 branch
BranchTrueFalseTotal
if ($cnt < 1) 0 3 3
585 0 }
586 else {
587 $cnt = munlink $fn;
588 if ( $cnt < 1) {
589 warn "Levmar::Func::clean_files : cnt $cnt: Failed to delete object file '$fn'";
590 3 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
if (-e $srcn) 3 0 3
591 3 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $srcn) 0 3 3
592 0 }
593 }
594 3 ▶ if ( -e $srcn ) {
Branch: 1 branch
BranchTrueFalseTotal
open my $srch, "<", $srcn or die "Levmar::Func::clean_files : Can't open C source file '${srcn}' for reading" 3 0 3
595 if ( not -f $srcn ) {
596 3 warn "Levmar::Func::clean_files: Source file '$srcn' not a file. Not deleting";
597 }
598 open my $srch , '<' ,
599 $srcn or die "Levmar::Func::clean_files : Can't open C source file '$srcn' for reading";
600 3 ▶ my $closed = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $srch)) 4 0 4
601 4 ▶ # weird stack bug present when using implicit $_ rather than $line.
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ 1 3 4
602 3 ▶ # A function that calls levmar_func sometimes has an item in arg stack
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ m[/\* This file automatically]) 2 1 3
603 2 # replaced by the first line read here.
604 2 while (my $line = <$srch>) {
605 2 next if $line =~ /^\s*$/;
606 2 if ($line =~ /\/\* This file automatically/) {
607 2 ▶ close($srch);
Branch: 1 branch
BranchTrueFalseTotal
if $cnt < 1 0 2 2
608 $closed = 1;
609 2 $cnt = 0;
610 $cnt = munlink $srcn;
611 1 warn "Levmar::Func::clean_files: cnt $cnt: Failed to delete source file '$srcn'"
612 if $cnt < 1;
613 3 ▶ last;
Branch: 1 branch
BranchTrueFalseTotal
unless $closed == 1 1 2 3
614 }
615 last; # only check up to first non-blank line
616 }
617 close($srch) unless $closed == 1;
618 }
619 }
620 4 ▶ 
Subroutine load_fit_library: called CC=7 SCAR=20.4
Pod undocumented
621 4 # Improve effiency after this is well tested by getting all four
622 4 # symbols at once.
623 4  
624 4 sub load_fit_library {
625 4 ▶ my $self = shift;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $so 0 4 4
626 4 my $so = $self->mfqp('SONAME');
627 my $fn = $self->{NAME};
628 4 my $jn = $self->{JACNAME};
629 die "Levmar::Func::load_fit_library : Can't find dynamic library file '$so'" unless -e $so;
630 4 my ($func_pointer, $jac_pointer, $lib_handle, $error_message)=
631 (0,0,0,0);
632 4 my ($sfunc_pointer, $sjac_pointer) = # single precision
633 (0,0);
634 4 ▶ ($func_pointer, $lib_handle, $error_message) =
Branch: 1 branch
BranchTrueFalseTotal
if ($error_message ne "") 0 4 4
635 0 open_and_find($so,$fn);
636 ($sfunc_pointer, $lib_handle, $error_message) = # single precision
637 0 open_and_find($so,'s'.$fn);
638 if ( $error_message ne '' ) {
639 4 ▶ warn "Levmar::Func::load_fit_library: Failed to load dynamic library '$so'\n".
Branch: 1 branch
BranchTrueFalseTotal
if (defined $jn and $jn ne "") 2 2 4
Condition66%2/3
defined $jn and $jn ne ""
!ll&&!rl&&r
022
MC/DC50%1/2
defined $jn and $jn ne ""
defined $jn $jn ne ""
Truth table66%2/3
A and B
A= defined $jnB= $jn ne ""
ABresult
0X0
100
111
640 2 " and find fit func '$fn'.\n dlopen:'$error_message'";
641 return 0;
642 2 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
if ($error_message ne '') { } 0 2 2
643 0 if ( defined $jn and $jn ne '') {
644 ($jac_pointer, $lib_handle, $error_message) =
645 open_and_find($so,$jn);
646 if ( $error_message ne '' ) {
647 2 warn "Levmar::Func::load_fit_library: Failed to load dynamic library '$so' and find jacobian '$jn'.\n".
648 " dlopen:'$error_message' ";
649 2 }
650 else {
651 2 ▶ $self->{JPOINTER} = $jac_pointer;
Branch: 1 branch
BranchTrueFalseTotal
if ($error_message ne '') { } 0 2 2
652 0 }
653 ($sjac_pointer, $lib_handle, $error_message) =
654 open_and_find($so, 's' . $jn);
655 if ( $error_message ne '' ) {
656 2 warn "Levmar::Func::load_fit_library: Failed to load dynamic library '$so' and find jacobian '$jn'.\n".
657 " dlopen:'$error_message' ";
658 }
659 else {
660 4 $self->{SJPOINTER} = $sjac_pointer;
661 4 }
662 4  
663 }
664 $self->{FPOINTER} = $func_pointer;
665 $self->{SFPOINTER} = $sfunc_pointer;
666 $self->{LIBHANDLE} = $lib_handle;
667 }
668  
669  
670 # End of methods
671 # (Actually there are some more methods near the bottom
672 8 ▶# that are close to the corresponding pp_defs)
Subroutine do_system: called CC=2 SCAR=7.1
Pod undocumented
673 8 ########################################
674 8 ▶# Non-methods below here
Branch: 1 branch
BranchTrueFalseTotal
if $f->{"FVERBOSE"} 0 8 8
675 8  
676 # should return 0 on success
677 sub do_system {
678 8 ▶ my ($c,$f) = @_;
Subroutine munlink: called CC=4 SCAR=16.5
Pod undocumented
679 8 print STDERR $c,"\n" if $f->{FVERBOSE};
680 8 system $c;
681 8 }
682 8 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $f) 0 8 8
683 0 sub munlink {
684 0 my $f = shift;
685 my $cnt;
686 8 ▶ my $sf = "'$f'";
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $f) 0 8 8
687 0 if ( not -e $f ) {
688 0 prwarn "munlink: $sf does not exist";
689 return 0;
690 8 }
691 8 ▶ if ( not -f $f ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cnt > 0) 0 8 8
692 0 prwarn "munlink: $sf is not a file. Not removing.";
693 return 0;
694 8 }
695 $cnt = unlink $f;
696 if (not $cnt > 0) {
697 prwarn "munlink: Can't unlink exisiting file $sf";
698 }
699 return $cnt;
700 }
701 3 ▶ 
Subroutine generate_C_source: called CC=36 SCAR=41.0
Pod undocumented
702 3 # Turn a function description into C source
703 3 # !care! I disabled NiceSlice, because it erroneously saw
704 3 # slicing in this piddle-less routine. But I was not using it
705 # anyway. (parser is rewritten, maybe not a problem anymore.)
706 sub generate_C_source {
707 my ($self, $fdef) = @_;
708 my $funcname = ''; # this one is returned
709 my $jacname = ''; # this one is returned
710 # Do this first now send t -> t[i], x -> x[i], p3 -> p[3]
711 3 # Probably a better way than doing this in a loop
712 3 # but overlapping matchin patterns do not work with global flag
713 3 # t --> t[i]
714 3  
715 3 # I tried to substitutions on $fdef vs on each line. Not much difference.
716 3 my $defs = {};
717 42 ▶ my ($lines,$def, $loopf, $noloopf, $name);
Branch: 1 branch
BranchTrueFalseTotal
if $oneline =~ /^\s*end\s+(function|jacobian)\s*/ 0 42 42
718 42 ▶ my (@flines) = split(/\n/, $fdef);
Branch: 1 branch
BranchTrueFalseTotal
if ($oneline =~ /^\s*(function|jacobian)\s*(\w*)\s*$/) 4 38 42
719 4 $lines = [];
720 4 ▶ my $prefunc = $lines;# lines before any func def
Branch: 1 branch
BranchTrueFalseTotal
unless $name ne "" 2 2 4
721 4 foreach my $oneline (@flines) { #break code into functions
722 4 ▶ next if $oneline =~ /^\s*end\s+(function|jacobian)\s*/;
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "jacobian" and not $name =~ /^jac/ 1 3 4
Condition66%2/3
$type eq "jacobian" and not $name =~ /^jac/
!ll&&!rl&&r
301
MC/DC50%1/2
$type eq "jacobian" and not $name =~ /^jac/
$type eq "jacobian" not $name =~ /^jac/
Truth table66%2/3
A and not(B)
A= $type eq "jacobian"B= not $name =~ /^jac/
ABresult
0X0
100
111
723 4 if ( $oneline =~ /^\s*(function|jacobian)\s*(\w*)\s*$/ ) { # func declaration
724 4 $name = $2;
725 4 $name = "func" unless $name ne '';
726 4 my $type = $1;
727 4 $name = "jac" . $name if $type eq 'jacobian' and not $name =~ /^jac/ ;
728 4 $def = $defs->{$name} = {};
729 4 $def->{type} = $type;
730 $lines = [];
731 38 ▶ $def->{loop} = $lines;
Branch: 2 branches
BranchTrueFalseTotal
if ($oneline =~ /^\s*noloop/) { } 2 36 38
elsif ($oneline =~ /^\s*loop/) { } 1 35 36
732 2 $noloopf = 0;
733 2 $loopf = 0;
734 2 next;
735 2 }
736 if ( $oneline =~ /^\s*noloop/ ) { # don't want any implicit loop
737 $def->{preloop} = $lines;
738 1 $def->{loop} = undef;
739 1 $noloopf = 1;
740 1 next;
741 1 }
742 1 elsif ( $oneline =~ /^\s*loop/ ) { # start implicit loop
743 $def->{preloop} = $lines;
744 $lines = [];
745 35 $def->{loop} = $lines;
746 $loopf = 1;
747 next;
748 }
749 else {
750 3 ▶ push @$lines, "" .$oneline ."\n";
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"TESTSYNTAX"}) 0 3 3
751 0 }
752 0 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
if (@$prefunc > 0) 0 0 0
753 0  
754 0
755 if ( defined $self->{TESTSYNTAX} ) {
756 0 my $st = '';
757 0 if ( @$prefunc > 0) {
758 0 $st .= ":PREFUNC: lines:" . scalar(@{$prefunc}). "\n";
759 0 ▶ $st .= join("\n",@{$prefunc}). "\n";
Branch: 1 branch
BranchTrueFalseTotal
if (defined $d->{"preloop"}) 0 0 0
760 0 }
761 0 foreach my $name ( keys %$defs ) {
762 my $d = $defs->{$name};
763 0 ▶ $st .= "NAME: $name, TYPE: " . $d->{type};
Branch: 1 branch
BranchTrueFalseTotal
if (defined $d->{"loop"}) 0 0 0
764 0 if (defined $d->{preloop}) {
765 0 $st .= ":PRELOOP: lines:" . scalar(@{$d->{preloop}}). "\n";
766 $st .= join("\n",@{$d->{preloop}}). "\n";
767 }
768 0 if (defined $d->{loop}) {
769 $st .= ":LOOP: lines:" . scalar(@{$d->{loop}}). "\n";
770 $st .= join("\n",@{$d->{loop}}). "\n";
771 3 }
772 3 }
773 3 $self->{SYNTAXRESULTS} = $st;
774 }
775  
776 my $end_function = "\}\n\n";
777 3 my $end_loop = " \}\n";
778 3 my $start_loop = { 'jacobian' => " for(i=j=0; i<n; ++i){\n",
779 'function' => " for(i=0; i<n; ++i){\n" };
780  
781 # my $dtype = $self->{TYPE};
782 my $dtype = 'FLOAT';
783 my $start_function = {
784 'jacobian' => "void JACFUNNAME($dtype *p, $dtype *d, int m, int n, $dtype *t)\n\{\n".
785 3 " int i,j;\n",
786 # " $dtype *t = ($dtype *) data;\n",
787 'function' => "void FITFUNNAME($dtype *p, $dtype *x, int m, int n, $dtype *t)\n\{\n".
788 " int i;\n" };
789 # " $dtype *t = ($dtype *) data;\n" };
790 3 my $kode =
791 "/* This file automatically created and deleted. Do not edit. */\n" .
792 3 "#include<math.h>\n#include <stdio.h>\n\n" .
793 4 join('',@$prefunc);
794 4  
795 4  
796 4 ▶ my $kode1 = '';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "function" 3 1 4
797 4 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "jacobian" 1 3 4
798 4 ▶ foreach my $name ( keys %$defs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $d->{"loop"}) 2 2 4
799 2 my $d = $defs->{$name};
800 2 ▶ my ($loop, $preloop, $loopcode);
Branch: 1 branch
BranchTrueFalseTotal
if ($loopcode =~ /^\s*$/) { } 0 2 2
801 0 my $type = $d->{type};
802 $funcname = $name if $type eq 'function';
803 $jacname = $name if $type eq 'jacobian';
804 2 if ( defined $d->{loop} ) {
805 $loopcode = join("", @{$d->{loop} });
806 if ( $loopcode =~ /^\s*$/ ) { # loop code is whitespace
807 4 ▶ $loopcode = undef; # so don't print it
Branch: 1 branch
BranchTrueFalseTotal
if defined $d->{"preloop"} 3 1 4
808 }
809 else {
810 4 $loop = $d->{loop};
811 4 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $preloop) 3 1 4
812 3 }
813 $preloop = $d->{preloop} if defined $d->{preloop};
814 27 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ s/([^\w]|^)p(\d+)([^\w\[])/$1p[$2]$3/g) 7 27 34
815 27 ▶# $kode1 .= sprintf($start_function->{$type},$name);
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'function') { } 6 21 27
elsif ($type eq 'jacobian') { } 21 0 21
816 $kode1 .= sprintf($start_function->{$type}, '');
817 6 ▶ if (defined $preloop ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ s/([^\w]|^)x(\d+)([^\w\[])/$1x[$2]$3/g) 4 6 10
818 foreach (@$preloop) {
819 # p5 --> p[5]
820 while ($_ =~ s/([^\w]|^)p(\d+)([^\w\[])/$1p[$2]$3/g){}
821 21 ▶ if ( $type eq 'function' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /([^\w]|^)d(\d+)\[(\d+)\]/) 16 21 37
822 16 # x7 -- > x[7]
823 16 while($_ =~ s/([^\w]|^)x(\d+)([^\w\[])/$1x[$2]$3/g){}
824 }
825 elsif ( $type eq 'jacobian' ) {
826 # d3[6] -> d[ 3+ (n-1)*6], no! [3+m*6] is correct
827 3 while($_ =~ /([^\w]|^)d(\d+)\[(\d+)\]/ ) {
828 my $i = "$2+m*$3" ;
829 4 ▶ $_ =~ s/([^\w]|^)d\d+\[\d+\]/$1d[$i]/;
Branch: 1 branch
BranchTrueFalseTotal
if (defined $loop) 2 2 4
830 2 }
831 2 }
832 }
833 4 ▶ $kode1 .= join('',@$preloop);
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ s/([^\w]|^)t([^\w\[])/$1t[i]$2/g) 2 4 6
834 }
835 4 ▶ if ( defined $loop ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ s/([^\w]|^)p(\d+)([^\w\[])/$1p[$2]$3/g) 2 4 6
836 4 ▶ $kode1 .= $start_loop->{$type};
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'function') { } 4 0 4
elsif ($type eq 'jacobian') { } 0 0 0
837 foreach ( @$loop ) {
838 4 ▶ # t -- > t[i]
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ s/([^\w]|^)x([^\w\[])/$1x[i]$2/g) 2 4 6
839 while($_ =~ s/([^\w]|^)t([^\w\[])/$1t[i]$2/g){}
840 # p5 --> p[5]
841 0 ▶ while ($_ =~ s/([^\w]|^)p(\d+)([^\w\[])/$1p[$2]$3/g){}
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*d\d+\[?i?\]?(.+)/) 0 0 0
842 0 if ( $type eq 'function' ) {
843 0 # x -> x[i]
844 0 while($_ =~ s/([^\w]|^)x([^\w\[])/$1x[i]$2/g){}
845 }
846 elsif ( $type eq 'jacobian' ) {
847 if ( /^\s*d\d+\[?i?\]?(.+)/ ) {
848 0 my $line = $1;
849 $line =~ s/\s*//; # strip space
850 $_ = " d[j++] $line\n";
851 2 }
852 }
853 4 else {
854 warn "Levmar::Func::generate_C_source: Function type neither 'function' nor 'jacobian'";
855 3 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
$^O =~ /MSWin32/i ? : 0 3 3
856 3 }
857 $kode1 .= join('',@$loop) . $end_loop;
858 }
859 $kode1 .= $end_function;
860 } #foreach my $name ..
861 my $export = $^O =~ /MSWin32/i ? '__declspec (dllexport)' : '';
862 $kode .= "
863 #define FLOAT double
864 #define JACFUNNAME $export $jacname
865 #define FITFUNNAME $export $funcname
866 " . $kode1 .
867 "
868 #undef FLOAT
869 3 #undef JACFUNNAME
870 #undef FITFUNNAME
871 #define FLOAT float
872 #define JACFUNNAME $export s$jacname
873 #define FITFUNNAME $export s$funcname
874 " . $kode1;
875 return ($funcname,$jacname,$kode);
876 } # end sub generate_C_source {
877  
878 12 ▶# load shared library with fit function and find a symbol
Subroutine open_and_find: called CC=1 SCAR=0.0
Pod undocumented
879 12 # (pointer to fit function or jacobian in our case.)
880 12 # $lib_name : name of library file , eg "./func.so"
881 12 # $func_name : name of function, eg "square"
882 12 # $func_pointer : value of C level pointer to $func_name, cast to int
883 12 # $lib_handle : same, but for pointer to library
884 12 sub open_and_find {
885 my ($lib_name, $func_name) = @_;
886 12 my $lib_handle = 0;
887 my $func_pointer = 0;
888 my $nchar = 1000; # avoid buffer overun by sending this number to memccpy
889 my $error_message = "." x $nchar;
890 deb "call_open_and_find for lib $lib_name, func $func_name.";
891 12 # print STDERR "Nargs 1 ", scalar(@_) , "\n";
892 my @res = call_open_and_find($lib_name, $func_name, $lib_handle,
893 $func_pointer, $error_message , $nchar);
894 # print STDERR "Nargs 2 ", scalar(@_) , "\n";
895 12 ▶# print STDERR ("call_open_and_find returned ", scalar(@res), " items\n.");
Subroutine close_shared_object_file: called CC=1 SCAR=0.0
Pod undocumented
896 12 # print STDERR (@res),"\n";
897 12 return ($func_pointer, $lib_handle, $error_message);
898 12 # return ($func_pointer, $lib_handle, '');
899 12 }
900 12  
901 sub close_shared_object_file {
902 12 my ($lib_handle) = @_;
903 my $nchar = 1000; # avoid buffer overun by sending this number to memccpy
904 my $error_message = "." x $nchar;
905 my $ret = 0;
906 12 deb "Closing so file.";
907 # print STDERR "Nargs 3 ", scalar(@_) , "\n";
908 my @res = call_close_shared_object_file( $lib_handle, $ret, $error_message, $nchar);
909 # print STDERR "Nargs 4 ", scalar(@_) , "\n";
910 # print STDERR ("close shared returned ", scalar(@res), " items\n.");
911 # print STDERR (@res),"\n";
912 return ($ret, $error_message);
913 }
914  
915 #-----------------------------------------
916 # Some methods to call the functions
917  
918 2 ▶# evaluate the function.
Subroutine call: called CC=2 SCAR=7.5
Pod documented
919 2 # Given parameters p and coordiante pdl t,
920 2 # return x(t;p) with x->dims == t->dims.
921 2 # The functions don't really have to have
922 2 ▶# this form, but this is what we assume for
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"FPOINTER"} == 0) 0 2 2
923 0 # this call.
924 0 sub call {
925 my $self = shift;
926 2 my ($p,$t) = @_; # pdls
927 2 my $x = zeroes($t);
928 if ($self->{FPOINTER} == 0) {
929 warn "Levmar::Func::call : No function loaded.";
930 return PDL->null;
931 }
932 0 ▶ _callf(topdl($p),$t,$x, $self->{FPOINTER});
Subroutine jac_of_t: not called CC=3 SCAR=24.8
Pod undocumented
933 0 return($x);
934 0 }
935 0 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"JPOINTER"} == 0) 0 0 0
936 0 # call jacobian and get back 2-d pdl
937 0 # return jac(m,n) = jacfunc(p(m),t(n))
938 sub jac_of_t {
939 0 my $self = shift;
940 0 my ($p,$t) = @_; # pdls
941 0 ▶ if ($self->{JPOINTER} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ref($jac) =~ /PDL/ 0 0 0
942 0 warn "Levmar::Func::jac_of_t: No jacobian function loaded.";
943 0 return PDL->null;
944 }
945 my @dims = dims($t);
946 my $jac = zeroes($p->type, $p->nelem,@dims);
947 return 0 unless ref($jac) =~ /PDL/;
948 _callj($p,$t,$jac, $self->{JPOINTER});
949 2 ▶ return($jac);
Subroutine jac_of_t1: called CC=4 SCAR=14.5
Pod undocumented
950 2 }
951 2  
952 2 ▶# treat array of derivatives as 1-d, just at the
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"JPOINTER"} == 0) 0 2 2
953 0 # supplied c routines do. For debugging.
954 0 # return jac(m*n) = jacfunc(p(m),t(n))
955 sub jac_of_t1 {
956 2 my $self = shift;
957 2 my ($p,$t) = @_; # pdls
958 2 if ($self->{JPOINTER} == 0) {
959 2 ▶ warn "Levmar::Func::jac_of_t1: No jacobian function loaded.";
Branch: 1 branch
BranchTrueFalseTotal
unless ref($jac) =~ /PDL/ 0 2 2
960 2 return PDL->null;
961 2 }
962 my @dims = dims($t);
963 foreach (@dims) { $_ *= $p->nelem}
964 my $jac = zeroes($p->type, @dims);
965 return 0 unless ref($jac) =~ /PDL/;
966 _callj1($p,$t,$jac, $self->{JPOINTER});
967 return($jac);
968 }
969  
970 ======EOD_new======
971  
972 pp_def ( '_callf' ,
973 Pars => ' double p(m); double t(nt); double [o]x(n);',
974 OtherPars => ' IV func; ',
975 GenericTypes => ['D'], Doc => undef,
976 Code => ' void (*fp)(double *, double *, int, int, void *);
977 fp = (void (*)(double*, double*, int, int, void*)) $COMP(func);
978 fp($P(p),$P(x),$SIZE(m),$SIZE(n),(void *)$P(t));
979 ');
980  
981 # This treats d as 2d pdl
982 pp_def ( '_callj' ,
983 Pars => ' double p(m); double t(nt); double [o]x(m,n);',
984 OtherPars => ' IV func; ',
985 GenericTypes => ['D'], Doc => undef,
986 Code => ' void (*fp)(double *, double *, int, int, void *);
987 fp = (void (*)(double*, double*, int, int, void*)) $COMP(func);
988 fp($P(p),$P(x),$SIZE(m),$SIZE(n),(void *)$P(t));
989 ');
990  
991 # d is longer 1d pdl
992 pp_def ( '_callj1' ,
993 Pars => ' double p(m); double t(nt); double [o]jac(jm);',
994 OtherPars => ' IV func; ',
995 GenericTypes => ['D'], Doc => undef,
996 Code => ' void (*fp)(double *, double *, int, int, void *);
997 fp = (void (*)(double*, double*, int, int, void*)) $COMP(func);
998 fp($P(p),$P(jac),$SIZE(m),$SIZE(nt),(void *)$P(t));
999 ');
1000  
1001 # Following routines are in getsymbol.c and do not invovle piddles
1002 # note that pointers to ints are passed for library and function pointers
1003 # The routines called use them as pointers to pointers to void (ie for library
1004 # handle and functions).
1005 # I don't know how portable this is. It relies on ints and pointers being the
1006 # same size.
1007  
1008 pp_addxs ( '', '
1009  
1010 MODULE = PDL::Fit::Levmar::Func PACKAGE = PDL::Fit::Levmar::Func
1011  
1012  
1013 void
1014 call_open_and_find( lib_name, func_name, lib_handle, func_pointer, error_message, nchar )
1015 char * lib_name
1016 char * func_name
1017 void * lib_handle
1018 void * func_pointer
1019 char * error_message
1020 int nchar
1021 CODE:
1022 _open_and_find( lib_name, func_name, &lib_handle, &func_pointer, error_message, nchar );
1023 OUTPUT:
1024 lib_handle
1025 func_pointer
1026 error_message
1027  
1028 void
1029 call_close_shared_object_file ( lib_handle, rval , error_message, nchar )
1030 void * lib_handle
1031 int rval
1032 char * error_message
1033 int nchar
1034 CODE:
1035 _close_shared_object_file ( &lib_handle, &rval , error_message, nchar);
1036 OUTPUT:
1037 rval
1038 error_message
1039  
1040 ');
1041  
1042 pp_done();
« levmar.pd ↑ Summary Func/t/func.t »