File: Calling-External-Code-from-Oct_002dFiles.html

package info (click to toggle)
octave3.2 3.2.4-8
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 62,936 kB
  • ctags: 37,353
  • sloc: cpp: 219,497; fortran: 116,336; ansic: 10,264; sh: 5,508; makefile: 4,245; lex: 3,573; yacc: 3,062; objc: 2,042; lisp: 1,692; awk: 860; perl: 844
file content (156 lines) | stat: -rw-r--r-- 6,494 bytes parent folder | download
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
<html lang="en">
<head>
<title>Calling External Code from Oct-Files - Untitled</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Untitled">
<meta name="generator" content="makeinfo 4.11">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Oct_002dFiles.html#Oct_002dFiles" title="Oct-Files">
<link rel="prev" href="Calling-Octave-Functions-from-Oct_002dFiles.html#Calling-Octave-Functions-from-Oct_002dFiles" title="Calling Octave Functions from Oct-Files">
<link rel="next" href="Allocating-Local-Memory-in-Oct_002dFiles.html#Allocating-Local-Memory-in-Oct_002dFiles" title="Allocating Local Memory in Oct-Files">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
  pre.display { font-family:inherit }
  pre.format  { font-family:inherit }
  pre.smalldisplay { font-family:inherit; font-size:smaller }
  pre.smallformat  { font-family:inherit; font-size:smaller }
  pre.smallexample { font-size:smaller }
  pre.smalllisp    { font-size:smaller }
  span.sc    { font-variant:small-caps }
  span.roman { font-family:serif; font-weight:normal; } 
  span.sansserif { font-family:sans-serif; font-weight:normal; } 
--></style>
</head>
<body>
<div class="node">
<p>
<a name="Calling-External-Code-from-Oct-Files"></a>
<a name="Calling-External-Code-from-Oct_002dFiles"></a>
Next:&nbsp;<a rel="next" accesskey="n" href="Allocating-Local-Memory-in-Oct_002dFiles.html#Allocating-Local-Memory-in-Oct_002dFiles">Allocating Local Memory in Oct-Files</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="Calling-Octave-Functions-from-Oct_002dFiles.html#Calling-Octave-Functions-from-Oct_002dFiles">Calling Octave Functions from Oct-Files</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Oct_002dFiles.html#Oct_002dFiles">Oct-Files</a>
<hr>
</div>

<h4 class="subsection">A.1.9 Calling External Code from Oct-Files</h4>

<p>Linking external C code to Octave is relatively simple, as the C
functions can easily be called directly from C++.  One possible issue is
the declarations of the external C functions might need to be explicitly
defined as C functions to the compiler.  If the declarations of the
external C functions are in the header <code>foo.h</code>, then the manner in
which to ensure that the C++ compiler treats these declarations as C
code is

<pre class="example">     #ifdef __cplusplus
     extern "C"
     {
     #endif
     #include "foo.h"
     #ifdef __cplusplus
     }  /* end extern "C" */
     #endif
</pre>
   <p>Calling Fortran code however can pose some difficulties.  This is due to
differences in the manner in compilers treat the linking of Fortran code
with C or C++ code.  Octave supplies a number of macros that allow
consistent behavior across a number of compilers.

   <p>The underlying Fortran code should use the <code>XSTOPX</code> function to
replace the Fortran <code>STOP</code> function.  <code>XSTOPX</code> uses the Octave
exception handler to treat failing cases in the fortran code
explicitly.  Note that Octave supplies its own replacement <span class="sc">blas</span>
<code>XERBLA</code> function, which uses <code>XSTOPX</code>.

   <p>If the underlying code calls <code>XSTOPX</code>, then the <code>F77_XFCN</code><!-- /@w -->
macro should be used to call the underlying fortran function.  The Fortran
exception state can then be checked with the global variable
<code>f77_exception_encountered</code>.  If <code>XSTOPX</code> will not be called,
then the <code>F77_FCN</code><!-- /@w --> macro should be used instead to call the Fortran
code.

   <p>There is no harm in using <code>F77_XFCN</code><!-- /@w --> in all cases, except that for
Fortran code that is short running and executes a large number of times,
there is potentially an overhead in doing so.  However, if <code>F77_FCN</code><!-- /@w -->
is used with code that calls <code>XSTOP</code>, Octave can generate a
segmentation fault.

   <p>An example of the inclusion of a Fortran function in an oct-file is
given in the following example, where the C++ wrapper is

<pre class="example"><pre class="verbatim">     #include &lt;octave/oct.h>
     #include &lt;octave/f77-fcn.h>
     
     extern "C" 
     {
       F77_RET_T 
       F77_FUNC (fortsub, FORTSUB) 
             (const int&amp;, double*, F77_CHAR_ARG_DECL  
              F77_CHAR_ARG_LEN_DECL);
     }
     
     DEFUN_DLD (fortdemo , args , , "Fortran Demo.")
     {
       octave_value_list retval;  
       int nargin = args.length();
       if (nargin != 1)
         print_usage ();
       else
         {
           NDArray a = args(0).array_value ();
           if (! error_state)
             {
               double *av = a.fortran_vec ();
               octave_idx_type na = a.nelem ();
               OCTAVE_LOCAL_BUFFER (char, ctmp, 128);
     
               F77_XFCN (fortsub, FORTSUB, (na, av, ctmp 
                         F77_CHAR_ARG_LEN (128)));
     
     	  retval(1) = std::string (ctmp);
     	  retval(0) = a;
             }
         }
       return retval;
     }
</pre></pre>
   <p class="noindent">and the fortran function is

<pre class="example"><pre class="verbatim">           subroutine fortsub (n, a, s)
           implicit none
           character*(*) s
           real*8 a(*)
           integer*4 i, n, ioerr
           do i = 1, n
             if (a(i) .eq. 0d0) then
               call xstopx ('fortsub: divide by zero')
             else
               a(i) = 1d0 / a(i)
             endif
           enddo
           write (unit = s, fmt = '(a,i3,a,a)', iostat = ioerr)
          $       'There are ', n,
          $       ' values in the input vector', char(0)
           if (ioerr .ne. 0) then
             call xstopx ('fortsub: error writing string')
           endif
           return
           end
     
</pre></pre>
   <p>This example demonstrates most of the features needed to link to an
external Fortran function, including passing arrays and strings, as well
as exception handling.  An example of the behavior of this function is

<pre class="example">     [b, s] = fortdemo (1:3)
     
       b = 1.00000   0.50000   0.33333
       s = There are   3 values in the input vector
     [b, s] = fortdemo(0:3)
     error: fortsub:divide by zero
     error: exception encountered in Fortran subroutine fortsub_
     error: fortdemo: error in fortran
</pre>
   </body></html>