File: FCN.c

package info (click to toggle)
pdl 1:2.019-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 12,276 kB
  • sloc: perl: 47,799; fortran: 13,113; ansic: 9,365; sh: 41; makefile: 38; sed: 6
file content (140 lines) | stat: -rw-r--r-- 2,858 bytes parent folder | download | duplicates (3)
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
#ifdef NO_TRAILING_USCORE

#define MNINIT mninit
#define MNSETI mnseti
#define MNPARM mnparm
#define MNPARS mnpars
#define MNEXCM mnexcm
#define MNCOMD mncomd
#define MNPOUT mnpout
#define MNSTAT mnstat
#define MNEMAT mnemat
#define MNERRS mnerrs
#define MNCONT mncont

#define ABRE   abre
#define CIERRA cierra

#else

#define MNINIT mninit_
#define MNSETI mnseti_
#define MNPARM mnparm_
#define MNPARS mnpars_
#define MNEXCM mnexcm_
#define MNCOMD mncomd_
#define MNPOUT mnpout_
#define MNSTAT mnstat_
#define MNEMAT mnemat_
#define MNERRS mnerrs_
#define MNCONT mncont_

#define ABRE   abre_
#define CIERRA cierra_

#endif 

static SV* mnfunname;
static long long ene;

void FCN(long long* npar,double* grad,double* fval,double* xval,long long* iflag,double* futil);

void FCN(long long* npar,double* grad,double* fval,double* xval,long long* iflag,double* futil){

  SV* funname;

  long long count,i;
  double* x;

  I32 ax ; 
  
  pdl* pgrad;
  SV* pgradsv;

  pdl* pxval;
  SV* pxvalsv;
  
  long long ndims;
  PDL_Indx *pdims;

  dSP;
  ENTER;
  SAVETMPS;

  /* get name of function on the Perl side */
  funname = mnfunname;

  ndims = 1;
  pdims = (PDL_Indx *)  PDL->smalloc( (STRLEN) ((ndims) * sizeof(*pdims)) );
  
  pdims[0] = (PDL_Indx) ene;

  PUSHMARK(SP);
  XPUSHs(sv_2mortal(newSVpv("PDL", 0)));
  PUTBACK;
  perl_call_method("initialize", G_SCALAR);
  SPAGAIN;
  pxvalsv = POPs;
  PUTBACK;
  pxval = PDL->SvPDLV(pxvalsv);
 
  PDL->converttype( &pxval, PDL_D, PDL_PERM );
  PDL->children_changesoon(pxval,PDL_PARENTDIMSCHANGED|PDL_PARENTDATACHANGED);
  PDL->setdims (pxval,pdims,ndims);
  pxval->state &= ~PDL_NOMYDIMS;
  pxval->state |= PDL_ALLOCATED | PDL_DONTTOUCHDATA;
  PDL->changed(pxval,PDL_PARENTDIMSCHANGED|PDL_PARENTDATACHANGED,0);

  PUSHMARK(SP);
  XPUSHs(sv_2mortal(newSVpv("PDL", 0)));
  PUTBACK;
  perl_call_method("initialize", G_SCALAR);
  SPAGAIN;
  pgradsv = POPs;
  PUTBACK;
  pgrad = PDL->SvPDLV(pgradsv);
  
  PDL->converttype( &pgrad, PDL_D, PDL_PERM );
  PDL->children_changesoon(pgrad,PDL_PARENTDIMSCHANGED|PDL_PARENTDATACHANGED);
  PDL->setdims (pgrad,pdims,ndims);
  pgrad->state &= ~PDL_NOMYDIMS;
  pgrad->state |= PDL_ALLOCATED | PDL_DONTTOUCHDATA;
  PDL->changed(pgrad,PDL_PARENTDIMSCHANGED|PDL_PARENTDATACHANGED,0);

  pxval->data = (void *) xval;
  pgrad->data = (void *) grad;  

  PUSHMARK(SP);

  XPUSHs(sv_2mortal(newSViv(*npar)));
  XPUSHs(pgradsv);
  XPUSHs(sv_2mortal(newSVnv(*fval)));
  XPUSHs(pxvalsv);
  XPUSHs(sv_2mortal(newSViv(*iflag)));

  PUTBACK;

  count=call_sv(funname,G_ARRAY);

  SPAGAIN; 
  SP -= count ;
  ax = (SP - PL_stack_base) + 1 ;

  if (count!=2)
    croak("error calling perl function\n");

  pgradsv = ST(1);
  pgrad = PDL->SvPDLV(pgradsv);

  x = (double *) pgrad->data;

  for(i=0;i<ene;i++)
    grad[i] = x[i];

  *fval = SvNV(ST(0));

  PUTBACK;
  FREETMPS;
  LEAVE;

}