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 157 158
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
<head>
<title>GNU Octave: External Code Interface</title>
<meta name="description" content="GNU Octave: External Code Interface">
<meta name="keywords" content="GNU Octave: External Code Interface">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="index.html#Top" rel="up" title="Top">
<link href="Oct_002dFiles.html#Oct_002dFiles" rel="next" title="Oct-Files">
<link href="Missing-Components.html#Missing-Components" rel="prev" title="Missing Components">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.indentedblock {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
div.smalllisp {margin-left: 3.2em}
kbd {font-style:oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nocodebreak {white-space:nowrap}
span.nolinebreak {white-space:nowrap}
span.roman {font-family:serif; font-weight:normal}
span.sansserif {font-family:sans-serif; font-weight:normal}
ul.no-bullet {list-style: none}
-->
</style>
</head>
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="External-Code-Interface"></a>
<div class="header">
<p>
Next: <a href="Test-and-Demo-Functions.html#Test-and-Demo-Functions" accesskey="n" rel="next">Test and Demo Functions</a>, Previous: <a href="Packages.html#Packages" accesskey="p" rel="prev">Packages</a>, Up: <a href="index.html#Top" accesskey="u" rel="up">Top</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="External-Code-Interface-1"></a>
<h2 class="appendix">Appendix A External Code Interface</h2>
<a name="index-dynamic_002dlinking"></a>
<a name="index-Dynamically-Linked-Functions"></a>
<a name="index-Octave-API"></a>
<p>"The sum of human wisdom is not contained in any one language"
—Ezra Pound
</p>
<p>Octave is a fantastic language for solving many problems in science and
engineering. However, it is not the only computer language and there
are times when you may want to use code written in other languages.
Good reasons for doing so include: 1) not re-inventing the wheel; existing
function libraries which have been thoroughly tested and debugged or
large scale simulation codebases are a good example, 2) accessing unique
capabilities of a different language; for example the well-known regular
expression functions of Perl (but don’t do that because <code>regexp</code>
already exists in Octave).
</p>
<p>Performance should generally <strong>not</strong> be a reason for using compiled
extensions. Although compiled extensions can run faster, particularly
if they replace a loop in Octave code, this is almost never the best path
to take. First, there are many techniques to speed up Octave performance while
remaining within the language. Second, Octave is a high-level language that
makes it easy to perform common mathematical tasks. Giving that up means
shifting the focus from solving the real problem to solving a computer
programming problem. It means returning to low-level constructs such as
pointers, memory management, mathematical overflow/underflow, etc. Because
of the low level nature, and the fact that the compiled code is executed outside
of Octave, there is the very real possibility of crashing the interpreter and
losing work.
</p>
<p>Before going further, you should first determine if you really need to bother
writing code outside of Octave.
</p>
<ul>
<li> Can I get the same functionality using the Octave scripting language alone?
<p>Even when a function already exists outside the language, it may be
better to simply reproduce the behavior in an m-file rather than attempt to
interface to the outside code.
</p>
</li><li> Is the code thoroughly optimized for Octave?
<p>If performance is an issue you should always start with the in-language
techniques for getting better performance. Chief among these is vectorization
(see <a href="Vectorization-and-Faster-Code-Execution.html#Vectorization-and-Faster-Code-Execution">Vectorization and Faster Code Execution</a>) which not only makes the
code concise and more understandable but improves performance (10X-100X).
If loops must be used, make sure that the allocation of space for variables
takes place outside the loops using an assignment to a matrix of the right
size, or zeros.
</p>
</li><li> Does the code make as much use as possible of existing built-in library
routines?
<p>These routines are highly optimized and many do not carry the overhead
of being interpreted.
</p>
</li><li> Does writing a dynamically linked function represent a useful investment
of your time, relative to staying in Octave?
<p>It will take time to learn Octave’s interface for external code and
there will inevitably be issues with tools such as compilers.
</p></li></ul>
<p>With that said, Octave offers a versatile interface for including chunks
of compiled code as dynamically linked extensions. These dynamically linked
functions can be called from the interpreter in the same manner as any
ordinary function. The interface is bi-directional and external code can
call Octave functions (like <code>plot</code>) which otherwise might be very
difficult to develop.
</p>
<p>The interface is centered around supporting the languages C++, C, and Fortran.
Octave itself is written in C++ and can call external C++/C code through its
native oct-file interface. The C language is also supported through the
mex-file interface for compatibility with <small>MATLAB</small>. Fortran code is easiest
to reach through the oct-file interface.
</p>
<p>Because many other languages provide C or C++ APIs it is relatively simple
to build bridges between Octave and other languages. This is also a way to
bridge to hardware resources which often have device drivers written in C.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">• <a href="Oct_002dFiles.html#Oct_002dFiles" accesskey="1">Oct-Files</a>:</td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">• <a href="Mex_002dFiles.html#Mex_002dFiles" accesskey="2">Mex-Files</a>:</td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">• <a href="Standalone-Programs.html#Standalone-Programs" accesskey="3">Standalone Programs</a>:</td><td> </td><td align="left" valign="top">
</td></tr>
</table>
<hr>
<div class="header">
<p>
Next: <a href="Test-and-Demo-Functions.html#Test-and-Demo-Functions" accesskey="n" rel="next">Test and Demo Functions</a>, Previous: <a href="Packages.html#Packages" accesskey="p" rel="prev">Packages</a>, Up: <a href="index.html#Top" accesskey="u" rel="up">Top</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>
|