1 <!DOCTYPE html>
   2 <html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
   3 <head>
   4   <meta charset="utf-8" />
   5   <meta name="generator" content="pandoc" />
   6   <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
   7   <title>Using Panama &quot;foreign&quot; JDK</title>
   8   <style type="text/css">
   9       code{white-space: pre-wrap;}
  10       span.smallcaps{font-variant: small-caps;}
  11       span.underline{text-decoration: underline;}
  12       div.column{display: inline-block; vertical-align: top; width: 50%;}
  13   </style>
  14   <style type="text/css">
  15 a.sourceLine { display: inline-block; line-height: 1.25; }
  16 a.sourceLine { pointer-events: none; color: inherit; text-decoration: inherit; }
  17 a.sourceLine:empty { height: 1.2em; }
  18 .sourceCode { overflow: visible; }
  19 code.sourceCode { white-space: pre; position: relative; }
  20 div.sourceCode { margin: 1em 0; }
  21 pre.sourceCode { margin: 0; }
  22 @media screen {
  23 div.sourceCode { overflow: auto; }
  24 }
  25 @media print {
  26 code.sourceCode { white-space: pre-wrap; }
  27 a.sourceLine { text-indent: -1em; padding-left: 1em; }
  28 }
  29 pre.numberSource a.sourceLine
  30   { position: relative; left: -4em; }
  31 pre.numberSource a.sourceLine::before
  32   { content: attr(data-line-number);
  33     position: relative; left: -1em; text-align: right; vertical-align: baseline;
  34     border: none; pointer-events: all; display: inline-block;
  35     -webkit-touch-callout: none; -webkit-user-select: none;
  36     -khtml-user-select: none; -moz-user-select: none;
  37     -ms-user-select: none; user-select: none;
  38     padding: 0 4px; width: 4em;
  39     color: #aaaaaa;
  40   }
  41 pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
  42 div.sourceCode
  43   {  }
  44 @media screen {
  45 a.sourceLine::before { text-decoration: underline; }
  46 }
  47 code span.al { color: #ff0000; font-weight: bold; } /* Alert */
  48 code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
  49 code span.at { color: #7d9029; } /* Attribute */
  50 code span.bn { color: #40a070; } /* BaseN */
  51 code span.bu { } /* BuiltIn */
  52 code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
  53 code span.ch { color: #4070a0; } /* Char */
  54 code span.cn { color: #880000; } /* Constant */
  55 code span.co { color: #60a0b0; font-style: italic; } /* Comment */
  56 code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
  57 code span.do { color: #ba2121; font-style: italic; } /* Documentation */
  58 code span.dt { color: #902000; } /* DataType */
  59 code span.dv { color: #40a070; } /* DecVal */
  60 code span.er { color: #ff0000; font-weight: bold; } /* Error */
  61 code span.ex { } /* Extension */
  62 code span.fl { color: #40a070; } /* Float */
  63 code span.fu { color: #06287e; } /* Function */
  64 code span.im { } /* Import */
  65 code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
  66 code span.kw { color: #007020; font-weight: bold; } /* Keyword */
  67 code span.op { color: #666666; } /* Operator */
  68 code span.ot { color: #007020; } /* Other */
  69 code span.pp { color: #bc7a00; } /* Preprocessor */
  70 code span.sc { color: #4070a0; } /* SpecialChar */
  71 code span.ss { color: #bb6688; } /* SpecialString */
  72 code span.st { color: #4070a0; } /* String */
  73 code span.va { color: #19177c; } /* Variable */
  74 code span.vs { color: #4070a0; } /* VerbatimString */
  75 code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
  76   </style>
  77   <link rel="stylesheet" href="../make/data/docs-resources/resources/jdk-default.css" />
  78   <!--[if lt IE 9]>
  79     <script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script>
  80   <![endif]-->
  81 </head>
  82 <body>
  83 <header>
  84 <h1 class="title">Using Panama &quot;foreign&quot; JDK</h1>
  85 </header>
  86 <nav id="TOC">
  87 <ul>
  88 <li><a href="#using-panama-foreign-jdk">Using Panama &quot;foreign&quot; JDK</a><ul>
  89 <li><a href="#windows-notes">Windows notes</a></li>
  90 <li><a href="#embedding-python-interpreter-in-your-java-program-mac-os">Embedding Python interpreter in your Java program (Mac OS)</a><ul>
  91 <li><a href="#jextract-a-jar-file-for-python.h">jextract a Jar file for Python.h</a></li>
  92 <li><a href="#java-program-that-uses-extracted-python-interface">Java program that uses extracted Python interface</a></li>
  93 <li><a href="#running-the-java-code-that-calls-python-interpreter">Running the Java code that calls Python interpreter</a></li>
  94 </ul></li>
  95 <li><a href="#jlinking-python-interpreter-in-your-jdk-mac-os">jlinking Python Interpreter in your JDK (Mac OS)</a><ul>
  96 <li><a href="#generating-jmod-using-jextract">Generating jmod using jextract</a></li>
  97 <li><a href="#jlinking-python-module-to-create-a-jdk-with-python-in-it">Jlinking python module to create a JDK with Python in it</a></li>
  98 <li><a href="#compile-and-run-user-code-with-pythonjdk-jdk">Compile and run user code with &quot;pythonjdk&quot; jdk</a></li>
  99 </ul></li>
 100 <li><a href="#embedding-python-interpreter-in-your-java-program-ubuntu-16.04">Embedding Python interpreter in your Java program (Ubuntu 16.04)</a><ul>
 101 <li><a href="#jextract-a-jar-file-for-python.h-1">jextract a Jar file for Python.h</a></li>
 102 <li><a href="#compiling-and-running-python-java-example">Compiling and Running Python Java example</a></li>
 103 </ul></li>
 104 <li><a href="#embedding-python-interpreter-in-your-java-program-windows">Embedding Python interpreter in your Java program (Windows)</a><ul>
 105 <li><a href="#jextract-a-jar-file-for-python.h-2">jextract a Jar file for Python.h</a></li>
 106 <li><a href="#compiling-and-running-python-java-example-1">Compiling and Running Python Java example</a></li>
 107 </ul></li>
 108 <li><a href="#using-sqlite3-library-in-your-java-program-mac-os">Using sqlite3 library in your Java program (Mac OS)</a><ul>
 109 <li><a href="#jextract-a-jar-file-for-sqlite3.h">jextract a jar file for sqlite3.h</a></li>
 110 <li><a href="#java-sample-that-uses-sqlite3-library">Java sample that uses sqlite3 library</a></li>
 111 <li><a href="#compiling-and-running-sqlite-java-example">Compiling and Running sqlite Java example</a></li>
 112 </ul></li>
 113 <li><a href="#using-sqlite3-library-in-your-java-program-ubuntu-16.04">Using sqlite3 library in your Java program (Ubuntu 16.04)</a><ul>
 114 <li><a href="#installing-sqlite3">Installing sqlite3</a></li>
 115 <li><a href="#jextract-a-jar-file-for-sqlite3.h-1">jextract a jar file for sqlite3.h</a></li>
 116 <li><a href="#compiling-and-running-sqlite-java-example-1">Compiling and Running sqlite Java example</a></li>
 117 </ul></li>
 118 <li><a href="#using-blas-library">Using BLAS library</a><ul>
 119 <li><a href="#installing-openblas-mac-os">Installing OpenBLAS (Mac OS)</a></li>
 120 <li><a href="#installing-openblas-ubuntu-16.04">Installing OpenBLAS (Ubuntu 16.04)</a></li>
 121 <li><a href="#jextracting-cblas.h-macos">jextracting cblas.h (MacOS)</a></li>
 122 <li><a href="#jextracting-cblas.h-ubuntu-16.04">jextracting cblas.h (Ubuntu 16.04)</a></li>
 123 <li><a href="#java-sample-code-that-uses-cblas-library">Java sample code that uses cblas library</a></li>
 124 <li><a href="#compiling-and-running-the-above-cblas-samples">Compiling and running the above cblas samples</a></li>
 125 </ul></li>
 126 <li><a href="#using-lapack-library-ubuntu">Using LAPACK library (Ubuntu)</a><ul>
 127 <li><a href="#jextracting-clapack.h-ubuntu-16.04">jextracting clapack.h (Ubuntu 16.04)</a></li>
 128 <li><a href="#java-sample-code-that-uses-lapack-library">Java sample code that uses LAPACK library</a></li>
 129 <li><a href="#compiling-and-running-the-above-lapack-sample">Compiling and running the above LAPACK sample</a></li>
 130 </ul></li>
 131 <li><a href="#using-lapack-library-mac-os">Using LAPACK library (Mac OS)</a><ul>
 132 <li><a href="#jextracting-lapacke.h">jextracting lapacke.h</a></li>
 133 <li><a href="#java-sample-code-that-uses-lapack-library-1">Java sample code that uses LAPACK library</a></li>
 134 <li><a href="#compiling-and-running-the-above-lapack-sample-1">Compiling and running the above LAPACK sample</a></li>
 135 </ul></li>
 136 <li><a href="#using-libproc-library-to-list-processes-from-java-mac-os">Using libproc library to list processes from Java (Mac OS)</a><ul>
 137 <li><a href="#jextract-a-jar-file-for-libproc.h">jextract a jar file for libproc.h</a></li>
 138 <li><a href="#java-program-that-uses-libproc-to-list-processes">Java program that uses libproc to list processes</a></li>
 139 <li><a href="#running-the-java-code-that-uses-libproc">Running the Java code that uses libproc</a></li>
 140 </ul></li>
 141 <li><a href="#using-readline-library-from-java-code-mac-os">Using readline library from Java code (Mac OS)</a><ul>
 142 <li><a href="#jextract-a-jar-file-for-readline.h">jextract a jar file for readline.h</a></li>
 143 <li><a href="#java-code-that-uses-readline">Java code that uses readline</a></li>
 144 <li><a href="#running-the-java-code-that-uses-readline">Running the java code that uses readline</a></li>
 145 </ul></li>
 146 <li><a href="#using-libcurl-from-java-mac-os">Using libcurl from Java (Mac OS)</a><ul>
 147 <li><a href="#jextract-a-jar-for-curl.h">jextract a jar for curl.h</a></li>
 148 <li><a href="#java-code-that-uses-libcurl">Java code that uses libcurl</a></li>
 149 <li><a href="#running-the-java-code-that-uses-libcurl">Running the java code that uses libcurl</a></li>
 150 </ul></li>
 151 <li><a href="#using-unistd.h-from-java-code-linux">Using unistd.h from Java code (Linux)</a><ul>
 152 <li><a href="#jextract-a-jar-file-for-unistd.h">jextract a jar file for unistd.h</a></li>
 153 <li><a href="#java-code-that-calls-getpid">Java code that calls getpid</a></li>
 154 <li><a href="#running-the-java-code-that-uses-getpid">Running the Java code that uses getpid</a></li>
 155 </ul></li>
 156 <li><a href="#using-opengl-graphic-library-ubuntu-16.04">Using OpenGL graphic library (Ubuntu 16.04)</a><ul>
 157 <li><a href="#installing-opengl-ubuntu-16.04">Installing OpenGL (Ubuntu 16.04)</a></li>
 158 <li><a href="#jextracting-opengl-ubuntu-16.04">jextracting OpenGL (Ubuntu 16.04)</a></li>
 159 <li><a href="#java-sample-code-that-uses-the-opengl-library">Java sample code that uses the OpenGL library</a></li>
 160 <li><a href="#running-the-java-code-that-uses-opengl-ubuntu-16.04">Running the Java code that uses OpenGL (Ubuntu 16.04)</a></li>
 161 </ul></li>
 162 <li><a href="#using-opengl-graphic-library-windows">Using OpenGL graphic library (Windows)</a><ul>
 163 <li><a href="#installing-opengl">Installing OpenGL</a></li>
 164 <li><a href="#jextracting-opengl">jextracting OpenGL</a></li>
 165 <li><a href="#java-sample-code-that-uses-the-opengl-library-1">Java sample code that uses the OpenGL library</a></li>
 166 <li><a href="#running-the-java-code-that-uses-opengl">Running the Java code that uses OpenGL</a></li>
 167 </ul></li>
 168 <li><a href="#using-tensorflow-c-api-mac-os">Using TensorFlow C API (Mac OS)</a><ul>
 169 <li><a href="#installing-libtensorflow">Installing libtensorflow</a></li>
 170 <li><a href="#jextracting-libtensorflow-c_api.h">jextracting libtensorflow c_api.h</a></li>
 171 <li><a href="#java-sample-code-that-uses-tensorflow-library">Java sample code that uses tensorflow library</a></li>
 172 <li><a href="#compiling-and-running-the-above-tensorflow-sample">Compiling and running the above TensorFlow sample</a></li>
 173 </ul></li>
 174 <li><a href="#using-tensorflow-c-api-windows">Using TensorFlow C API (Windows)</a><ul>
 175 <li><a href="#installing-libtensorflow-1">Installing libtensorflow</a></li>
 176 <li><a href="#jextracting-libtensorflow-c_api.h-1">jextracting libtensorflow c_api.h</a></li>
 177 <li><a href="#java-sample-code-that-uses-tensorflow-library-1">Java sample code that uses tensorflow library</a></li>
 178 <li><a href="#compiling-and-running-the-above-tensorflow-sample-1">Compiling and running the above TensorFlow sample</a></li>
 179 </ul></li>
 180 </ul></li>
 181 </ul>
 182 </nav>
 183 <?xml version="1.0" encoding="utf-8"?>
 184 <h1 id="using-panama-foreign-jdk">Using Panama &quot;foreign&quot; JDK</h1>
 185 <p>There are two ways to get a panama foreign branch JDK.</p>
 186 <ol type="1">
 187 <li>Locally build &quot;foreign&quot; branch of panama repo <a href="http://hg.openjdk.java.net/panama/dev/" class="uri">http://hg.openjdk.java.net/panama/dev/</a></li>
 188 <li>Download pre-built panama &quot;foreign&quot; early access binaries from <a href="http://jdk.java.net/panama/" class="uri">http://jdk.java.net/panama/</a></li>
 189 </ol>
 190 <p>Using foreign function call in Java involves the following three steps:</p>
 191 <ol type="1">
 192 <li>Use <strong>jextract</strong> tool to generate java interface for your C header file(s)</li>
 193 <li>Use <strong>java.foreign</strong> API to create (&quot;bind&quot;) implementation for C header interfaces</li>
 194 <li>Invoke C functions via the jextracted Java interface</li>
 195 </ol>
 196 <h3 id="windows-notes">Windows notes</h3>
 197 <p>You will (almost always) need to have Visual Studio installed, since most libraries indirectly depend on Visual Studio runtime libraries and this currently means that jextract needs their header to extract successfully. Windows examples have been tested with <a href="https://visualstudio.microsoft.com/downloads/#build-tools-for-visual-studio-2017">Build Tools for Visual Studio 2017</a>.</p>
 198 <p>It is generally a good idea to give jextract a bunch of extra memory since a lot of big system headers are transitively included. The extra memory will make the jextract run significantly faster. Windows support was added only recently, and the memory usage of jextract has not been optimized yet, so this is a workaround. You can give extra memory by passing e.g. <code>-J-Xmx8G</code> to jextract as an additional argument, which in this example gives jextract 8 gigabytes of memory.</p>
 199 <p>Commands are tested in PowerShell.</p>
 200 <h2 id="embedding-python-interpreter-in-your-java-program-mac-os">Embedding Python interpreter in your Java program (Mac OS)</h2>
 201 <h3 id="jextract-a-jar-file-for-python.h">jextract a Jar file for Python.h</h3>
 202 <div class="sourceCode" id="cb1"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb1-1" data-line-number="1"></a>
 203 <a class="sourceLine" id="cb1-2" data-line-number="2"><span class="ex">jextract</span> -l python2.7 \</a>
 204 <a class="sourceLine" id="cb1-3" data-line-number="3">  -L /System/Library/Frameworks/Python.framework/Versions/2.7/lib --record-library-path \</a>
 205 <a class="sourceLine" id="cb1-4" data-line-number="4">  --exclude-symbols .*_FromFormatV<span class="dt">\|</span>_.*<span class="dt">\|</span>PyOS_vsnprintf<span class="dt">\|</span>.*_VaParse.*<span class="dt">\|</span>.*_VaBuild.*<span class="dt">\|</span>PyBuffer_SizeFromFormat<span class="dt">\|</span>vasprintf<span class="dt">\|</span>vfprintf<span class="dt">\|</span>vprintf<span class="dt">\|</span>vsprintf \</a>
 206 <a class="sourceLine" id="cb1-5" data-line-number="5">  -t org.python \</a>
 207 <a class="sourceLine" id="cb1-6" data-line-number="6">  /usr/include/python2.7/Python.h \</a>
 208 <a class="sourceLine" id="cb1-7" data-line-number="7">  -o python.jar</a></code></pre></div>
 209 <h3 id="java-program-that-uses-extracted-python-interface">Java program that uses extracted Python interface</h3>
 210 <div class="sourceCode" id="cb2"><pre class="sourceCode java"><code class="sourceCode java"><a class="sourceLine" id="cb2-1" data-line-number="1"></a>
 211 <a class="sourceLine" id="cb2-2" data-line-number="2"><span class="co">// import java.foreign packages</span></a>
 212 <a class="sourceLine" id="cb2-3" data-line-number="3"><span class="kw">import</span><span class="im"> java.foreign.Libraries;</span></a>
 213 <a class="sourceLine" id="cb2-4" data-line-number="4"><span class="kw">import</span><span class="im"> java.foreign.Scope;</span></a>
 214 <a class="sourceLine" id="cb2-5" data-line-number="5"><span class="kw">import</span><span class="im"> java.foreign.memory.Pointer;</span></a>
 215 <a class="sourceLine" id="cb2-6" data-line-number="6"></a>
 216 <a class="sourceLine" id="cb2-7" data-line-number="7"><span class="co">// import jextracted python 'header' classes</span></a>
 217 <a class="sourceLine" id="cb2-8" data-line-number="8"><span class="kw">import static</span><span class="im"> org.python.Python_h.*;</span></a>
 218 <a class="sourceLine" id="cb2-9" data-line-number="9"><span class="kw">import static</span><span class="im"> org.python.pythonrun_h.*;</span></a>
 219 <a class="sourceLine" id="cb2-10" data-line-number="10"></a>
 220 <a class="sourceLine" id="cb2-11" data-line-number="11"><span class="kw">public</span> <span class="kw">class</span> PythonMain {</a>
 221 <a class="sourceLine" id="cb2-12" data-line-number="12">    <span class="kw">public</span> <span class="dt">static</span> <span class="dt">void</span> <span class="fu">main</span>(<span class="bu">String</span>[] args) {</a>
 222 <a class="sourceLine" id="cb2-13" data-line-number="13">        <span class="fu">Py_Initialize</span>();</a>
 223 <a class="sourceLine" id="cb2-14" data-line-number="14">        <span class="kw">try</span> (Scope s = org.<span class="fu">python</span>.<span class="fu">Python_h</span>.<span class="fu">scope</span>().<span class="fu">fork</span>()) {</a>
 224 <a class="sourceLine" id="cb2-15" data-line-number="15">            <span class="fu">PyRun_SimpleStringFlags</span>(s.<span class="fu">allocateCString</span>(</a>
 225 <a class="sourceLine" id="cb2-16" data-line-number="16">                <span class="st">&quot;print(sum([33, 55, 66])); print('Hello from Python!')</span><span class="sc">\n</span><span class="st">&quot;</span>),</a>
 226 <a class="sourceLine" id="cb2-17" data-line-number="17">                Pointer.<span class="fu">ofNull</span>());</a>
 227 <a class="sourceLine" id="cb2-18" data-line-number="18">        }</a>
 228 <a class="sourceLine" id="cb2-19" data-line-number="19">        <span class="fu">Py_Finalize</span>();</a>
 229 <a class="sourceLine" id="cb2-20" data-line-number="20">    }</a>
 230 <a class="sourceLine" id="cb2-21" data-line-number="21">}</a></code></pre></div>
 231 <h3 id="running-the-java-code-that-calls-python-interpreter">Running the Java code that calls Python interpreter</h3>
 232 <div class="sourceCode" id="cb3"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb3-1" data-line-number="1"></a>
 233 <a class="sourceLine" id="cb3-2" data-line-number="2"><span class="ex">javac</span> -cp python.jar PythonMain.java</a>
 234 <a class="sourceLine" id="cb3-3" data-line-number="3"></a>
 235 <a class="sourceLine" id="cb3-4" data-line-number="4"><span class="ex">java</span> -cp python.jar:. PythonMain</a></code></pre></div>
 236 <h2 id="jlinking-python-interpreter-in-your-jdk-mac-os">jlinking Python Interpreter in your JDK (Mac OS)</h2>
 237 <h3 id="generating-jmod-using-jextract">Generating jmod using jextract</h3>
 238 <div class="sourceCode" id="cb4"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb4-1" data-line-number="1"></a>
 239 <a class="sourceLine" id="cb4-2" data-line-number="2"><span class="ex">jextract</span> -l python2.7 \</a>
 240 <a class="sourceLine" id="cb4-3" data-line-number="3">  -L /System/Library/Frameworks/Python.framework/Versions/2.7/lib \</a>
 241 <a class="sourceLine" id="cb4-4" data-line-number="4">  --exclude-symbols .*_FromFormatV<span class="dt">\|</span>_.*<span class="dt">\|</span>PyOS_vsnprintf<span class="dt">\|</span>.*_VaParse.*<span class="dt">\|</span>.*_VaBuild.*<span class="dt">\|</span>PyBuffer_SizeFromFormat<span class="dt">\|</span>vasprintf<span class="dt">\|</span>vfprintf<span class="dt">\|</span>vprintf<span class="dt">\|</span>vsprintf \</a>
 242 <a class="sourceLine" id="cb4-5" data-line-number="5">  -t org.python \</a>
 243 <a class="sourceLine" id="cb4-6" data-line-number="6">  /usr/include/python2.7/Python.h \</a>
 244 <a class="sourceLine" id="cb4-7" data-line-number="7">  -o org.python.jmod</a></code></pre></div>
 245 <h3 id="jlinking-python-module-to-create-a-jdk-with-python-in-it">Jlinking python module to create a JDK with Python in it</h3>
 246 <p>jdk.compiler and org.python modules are added to the generated (jlinked) JDK</p>
 247 <div class="sourceCode" id="cb5"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb5-1" data-line-number="1"></a>
 248 <a class="sourceLine" id="cb5-2" data-line-number="2"><span class="ex">jlink</span> --add-modules org.python,jdk.compiler --module-path . --output pythonjdk</a></code></pre></div>
 249 <h3 id="compile-and-run-user-code-with-pythonjdk-jdk">Compile and run user code with &quot;pythonjdk&quot; jdk</h3>
 250 <p>In the following commands, it is assumed that you've put $pythonjdk/bin in your $PATH</p>
 251 <div class="sourceCode" id="cb6"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb6-1" data-line-number="1"></a>
 252 <a class="sourceLine" id="cb6-2" data-line-number="2"><span class="ex">javac</span> PythonMain.java</a>
 253 <a class="sourceLine" id="cb6-3" data-line-number="3"><span class="ex">java</span> PythonMain</a></code></pre></div>
 254 <h2 id="embedding-python-interpreter-in-your-java-program-ubuntu-16.04">Embedding Python interpreter in your Java program (Ubuntu 16.04)</h2>
 255 <h3 id="jextract-a-jar-file-for-python.h-1">jextract a Jar file for Python.h</h3>
 256 <div class="sourceCode" id="cb7"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb7-1" data-line-number="1"></a>
 257 <a class="sourceLine" id="cb7-2" data-line-number="2"><span class="ex">jextract</span> -l python2.7 \</a>
 258 <a class="sourceLine" id="cb7-3" data-line-number="3">  -L /usr/lib/python2.7/config-x86_64-linux-gnu --record-library-path \</a>
 259 <a class="sourceLine" id="cb7-4" data-line-number="4">  --exclude-symbols .*_FromFormatV<span class="dt">\|</span>_.*<span class="dt">\|</span>PyOS_vsnprintf<span class="dt">\|</span>.*_VaParse.*<span class="dt">\|</span>.*_VaBuild.*<span class="dt">\|</span>PyBuffer_SizeFromFormat<span class="dt">\|</span>vasprintf<span class="dt">\|</span>vfprintf<span class="dt">\|</span>vprintf<span class="dt">\|</span>vsprintf \</a>
 260 <a class="sourceLine" id="cb7-5" data-line-number="5">  -t org.python \</a>
 261 <a class="sourceLine" id="cb7-6" data-line-number="6">  /usr/include/python2.7/Python.h \</a>
 262 <a class="sourceLine" id="cb7-7" data-line-number="7">  -o python.jar</a></code></pre></div>
 263 <h3 id="compiling-and-running-python-java-example">Compiling and Running Python Java example</h3>
 264 <p>Follow the instructions from the Mac OS section</p>
 265 <h2 id="embedding-python-interpreter-in-your-java-program-windows">Embedding Python interpreter in your Java program (Windows)</h2>
 266 <h3 id="jextract-a-jar-file-for-python.h-2">jextract a Jar file for Python.h</h3>
 267 <p>Where python 2.7 is installed in the <code>C:\Python27</code> directory:</p>
 268 <div class="sourceCode" id="cb8"><pre class="sourceCode powershell"><code class="sourceCode powershell"><a class="sourceLine" id="cb8-1" data-line-number="1">jextract -L <span class="st">&quot;C:\Windows\System32&quot;</span> -l python27 -o python.<span class="fu">jar</span> -t <span class="st">&quot;org.python&quot;</span> --record-library-path C:\Python27\include\Python.<span class="fu">h</span></a></code></pre></div>
 269 <h3 id="compiling-and-running-python-java-example-1">Compiling and Running Python Java example</h3>
 270 <div class="sourceCode" id="cb9"><pre class="sourceCode powershell"><code class="sourceCode powershell"><a class="sourceLine" id="cb9-1" data-line-number="1">javac -cp python.<span class="fu">jar</span> PythonMain.<span class="fu">java</span></a>
 271 <a class="sourceLine" id="cb9-2" data-line-number="2">java -cp <span class="st">&quot;python.jar;.&quot;</span> PythonMain</a></code></pre></div>
 272 <h2 id="using-sqlite3-library-in-your-java-program-mac-os">Using sqlite3 library in your Java program (Mac OS)</h2>
 273 <h3 id="jextract-a-jar-file-for-sqlite3.h">jextract a jar file for sqlite3.h</h3>
 274 <div class="sourceCode" id="cb10"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb10-1" data-line-number="1"></a>
 275 <a class="sourceLine" id="cb10-2" data-line-number="2"><span class="ex">jextract</span>  /usr/include/sqlite3.h -t org.sqlite -lsqlite3 \</a>
 276 <a class="sourceLine" id="cb10-3" data-line-number="3">    -L /usr/lib --record-library-path         \</a>
 277 <a class="sourceLine" id="cb10-4" data-line-number="4">    --exclude-symbols sqlite3_vmprintf        \</a>
 278 <a class="sourceLine" id="cb10-5" data-line-number="5">    --exclude-symbols sqlite3_vsnprintf       \</a>
 279 <a class="sourceLine" id="cb10-6" data-line-number="6">    -o sqlite3.jar</a></code></pre></div>
 280 <h3 id="java-sample-that-uses-sqlite3-library">Java sample that uses sqlite3 library</h3>
 281 <div class="sourceCode" id="cb11"><pre class="sourceCode java"><code class="sourceCode java"><a class="sourceLine" id="cb11-1" data-line-number="1"></a>
 282 <a class="sourceLine" id="cb11-2" data-line-number="2"><span class="kw">import</span><span class="im"> java.lang.invoke.*;</span></a>
 283 <a class="sourceLine" id="cb11-3" data-line-number="3"><span class="kw">import</span><span class="im"> java.foreign.*;</span></a>
 284 <a class="sourceLine" id="cb11-4" data-line-number="4"><span class="kw">import</span><span class="im"> java.foreign.memory.*;</span></a>
 285 <a class="sourceLine" id="cb11-5" data-line-number="5"><span class="kw">import</span><span class="im"> org.sqlite.sqlite3.*;</span></a>
 286 <a class="sourceLine" id="cb11-6" data-line-number="6"><span class="kw">import static</span><span class="im"> org.sqlite.sqlite3_h.*;</span></a>
 287 <a class="sourceLine" id="cb11-7" data-line-number="7"></a>
 288 <a class="sourceLine" id="cb11-8" data-line-number="8"><span class="kw">public</span> <span class="kw">class</span> SqliteMain {</a>
 289 <a class="sourceLine" id="cb11-9" data-line-number="9">   <span class="kw">public</span> <span class="dt">static</span> <span class="dt">void</span> <span class="fu">main</span>(<span class="bu">String</span>[] args) <span class="kw">throws</span> <span class="bu">Exception</span> {</a>
 290 <a class="sourceLine" id="cb11-10" data-line-number="10">        <span class="kw">try</span> (Scope scope = <span class="fu">scope</span>().<span class="fu">fork</span>()) {</a>
 291 <a class="sourceLine" id="cb11-11" data-line-number="11">            <span class="co">// char* errMsg;</span></a>
 292 <a class="sourceLine" id="cb11-12" data-line-number="12">            Pointer&lt;Pointer&lt;<span class="bu">Byte</span>&gt;&gt; errMsg = scope.<span class="fu">allocate</span>(NativeTypes.<span class="fu">INT8</span>.<span class="fu">pointer</span>());</a>
 293 <a class="sourceLine" id="cb11-13" data-line-number="13"></a>
 294 <a class="sourceLine" id="cb11-14" data-line-number="14">            <span class="co">// sqlite3* db;</span></a>
 295 <a class="sourceLine" id="cb11-15" data-line-number="15">            Pointer&lt;Pointer&lt;sqlite3&gt;&gt; db = scope.<span class="fu">allocate</span>(LayoutType.<span class="fu">ofStruct</span>(sqlite3.<span class="fu">class</span>).<span class="fu">pointer</span>());</a>
 296 <a class="sourceLine" id="cb11-16" data-line-number="16"></a>
 297 <a class="sourceLine" id="cb11-17" data-line-number="17">            <span class="dt">int</span> rc = <span class="fu">sqlite3_open</span>(scope.<span class="fu">allocateCString</span>(<span class="st">&quot;employee.db&quot;</span>), db);</a>
 298 <a class="sourceLine" id="cb11-18" data-line-number="18">            <span class="kw">if</span> (rc != <span class="dv">0</span>) {</a>
 299 <a class="sourceLine" id="cb11-19" data-line-number="19">                <span class="bu">System</span>.<span class="fu">err</span>.<span class="fu">println</span>(<span class="st">&quot;sqlite3_open failed: &quot;</span> + rc);</a>
 300 <a class="sourceLine" id="cb11-20" data-line-number="20">                <span class="kw">return</span>;</a>
 301 <a class="sourceLine" id="cb11-21" data-line-number="21">            }</a>
 302 <a class="sourceLine" id="cb11-22" data-line-number="22"></a>
 303 <a class="sourceLine" id="cb11-23" data-line-number="23">            <span class="co">// create a new table</span></a>
 304 <a class="sourceLine" id="cb11-24" data-line-number="24">            Pointer&lt;<span class="bu">Byte</span>&gt; sql = scope.<span class="fu">allocateCString</span>(</a>
 305 <a class="sourceLine" id="cb11-25" data-line-number="25">                <span class="st">&quot;CREATE TABLE EMPLOYEE (&quot;</span>  +</a>
 306 <a class="sourceLine" id="cb11-26" data-line-number="26">                <span class="st">&quot;  ID INT PRIMARY KEY NOT NULL,&quot;</span>       +</a>
 307 <a class="sourceLine" id="cb11-27" data-line-number="27">                <span class="st">&quot;  NAME TEXT NOT NULL,&quot;</span>    +</a>
 308 <a class="sourceLine" id="cb11-28" data-line-number="28">                <span class="st">&quot;  SALARY REAL NOT NULL )&quot;</span></a>
 309 <a class="sourceLine" id="cb11-29" data-line-number="29">            );</a>
 310 <a class="sourceLine" id="cb11-30" data-line-number="30"></a>
 311 <a class="sourceLine" id="cb11-31" data-line-number="31">            rc = <span class="fu">sqlite3_exec</span>(db.<span class="fu">get</span>(), sql, <span class="bu">Callback</span>.<span class="fu">ofNull</span>(), Pointer.<span class="fu">ofNull</span>(), errMsg);</a>
 312 <a class="sourceLine" id="cb11-32" data-line-number="32"></a>
 313 <a class="sourceLine" id="cb11-33" data-line-number="33">            <span class="kw">if</span> (rc != <span class="dv">0</span>) {</a>
 314 <a class="sourceLine" id="cb11-34" data-line-number="34">                <span class="bu">System</span>.<span class="fu">err</span>.<span class="fu">println</span>(<span class="st">&quot;sqlite3_exec failed: &quot;</span> + rc);</a>
 315 <a class="sourceLine" id="cb11-35" data-line-number="35">                <span class="bu">System</span>.<span class="fu">err</span>.<span class="fu">println</span>(<span class="st">&quot;SQL error: &quot;</span> + Pointer.<span class="fu">toString</span>(errMsg.<span class="fu">get</span>()));</a>
 316 <a class="sourceLine" id="cb11-36" data-line-number="36">                <span class="fu">sqlite3_free</span>(errMsg.<span class="fu">get</span>());</a>
 317 <a class="sourceLine" id="cb11-37" data-line-number="37">            }</a>
 318 <a class="sourceLine" id="cb11-38" data-line-number="38"></a>
 319 <a class="sourceLine" id="cb11-39" data-line-number="39">            <span class="co">// insert two rows</span></a>
 320 <a class="sourceLine" id="cb11-40" data-line-number="40">            sql = scope.<span class="fu">allocateCString</span>(</a>
 321 <a class="sourceLine" id="cb11-41" data-line-number="41">                <span class="st">&quot;INSERT INTO EMPLOYEE (ID,NAME,SALARY) &quot;</span> +</a>
 322 <a class="sourceLine" id="cb11-42" data-line-number="42">                    <span class="st">&quot;VALUES (134, 'Xyz', 200000.0); &quot;</span> +</a>
 323 <a class="sourceLine" id="cb11-43" data-line-number="43">                <span class="st">&quot;INSERT INTO EMPLOYEE (ID,NAME,SALARY) &quot;</span> +</a>
 324 <a class="sourceLine" id="cb11-44" data-line-number="44">                    <span class="st">&quot;VALUES (333, 'Abc', 100000.0);&quot;</span></a>
 325 <a class="sourceLine" id="cb11-45" data-line-number="45">            );</a>
 326 <a class="sourceLine" id="cb11-46" data-line-number="46">            rc = <span class="fu">sqlite3_exec</span>(db.<span class="fu">get</span>(), sql, <span class="bu">Callback</span>.<span class="fu">ofNull</span>(), Pointer.<span class="fu">ofNull</span>(), errMsg);</a>
 327 <a class="sourceLine" id="cb11-47" data-line-number="47"></a>
 328 <a class="sourceLine" id="cb11-48" data-line-number="48">            <span class="kw">if</span> (rc != <span class="dv">0</span>) {</a>
 329 <a class="sourceLine" id="cb11-49" data-line-number="49">                <span class="bu">System</span>.<span class="fu">err</span>.<span class="fu">println</span>(<span class="st">&quot;sqlite3_exec failed: &quot;</span> + rc);</a>
 330 <a class="sourceLine" id="cb11-50" data-line-number="50">                <span class="bu">System</span>.<span class="fu">err</span>.<span class="fu">println</span>(<span class="st">&quot;SQL error: &quot;</span> + Pointer.<span class="fu">toString</span>(errMsg.<span class="fu">get</span>()));</a>
 331 <a class="sourceLine" id="cb11-51" data-line-number="51">                <span class="fu">sqlite3_free</span>(errMsg.<span class="fu">get</span>());</a>
 332 <a class="sourceLine" id="cb11-52" data-line-number="52">            }</a>
 333 <a class="sourceLine" id="cb11-53" data-line-number="53"></a>
 334 <a class="sourceLine" id="cb11-54" data-line-number="54">            <span class="dt">int</span>[] rowNum = <span class="kw">new</span> <span class="dt">int</span>[<span class="dv">1</span>];</a>
 335 <a class="sourceLine" id="cb11-55" data-line-number="55">            <span class="co">// callback to print rows from SELECT query</span></a>
 336 <a class="sourceLine" id="cb11-56" data-line-number="56">            <span class="bu">Callback</span>&lt;FI1&gt; callback = scope.<span class="fu">allocateCallback</span>(FI1.<span class="fu">class</span>, (a, argc, argv, columnNames) -&gt; {</a>
 337 <a class="sourceLine" id="cb11-57" data-line-number="57">                <span class="bu">System</span>.<span class="fu">out</span>.<span class="fu">println</span>(<span class="st">&quot;Row num: &quot;</span> + rowNum[<span class="dv">0</span>]++);</a>
 338 <a class="sourceLine" id="cb11-58" data-line-number="58">                <span class="bu">System</span>.<span class="fu">out</span>.<span class="fu">println</span>(<span class="st">&quot;numColumns = &quot;</span> + argc);</a>
 339 <a class="sourceLine" id="cb11-59" data-line-number="59">                <span class="kw">for</span> (<span class="dt">int</span> i = <span class="dv">0</span>; i &lt; argc; i++) {</a>
 340 <a class="sourceLine" id="cb11-60" data-line-number="60">                     <span class="bu">String</span> name = Pointer.<span class="fu">toString</span>(columnNames.<span class="fu">offset</span>(i).<span class="fu">get</span>());</a>
 341 <a class="sourceLine" id="cb11-61" data-line-number="61">                     <span class="bu">String</span> value = Pointer.<span class="fu">toString</span>(argv.<span class="fu">offset</span>(i).<span class="fu">get</span>());</a>
 342 <a class="sourceLine" id="cb11-62" data-line-number="62">                     <span class="bu">System</span>.<span class="fu">out.printf</span>(<span class="st">&quot;</span><span class="sc">%s</span><span class="st"> = </span><span class="sc">%s\n</span><span class="st">&quot;</span>, name, value);</a>
 343 <a class="sourceLine" id="cb11-63" data-line-number="63">                }</a>
 344 <a class="sourceLine" id="cb11-64" data-line-number="64">                <span class="kw">return</span> <span class="dv">0</span>;</a>
 345 <a class="sourceLine" id="cb11-65" data-line-number="65">            });</a>
 346 <a class="sourceLine" id="cb11-66" data-line-number="66"></a>
 347 <a class="sourceLine" id="cb11-67" data-line-number="67">            <span class="co">// select query</span></a>
 348 <a class="sourceLine" id="cb11-68" data-line-number="68">            sql = scope.<span class="fu">allocateCString</span>(<span class="st">&quot;SELECT * FROM EMPLOYEE&quot;</span>);</a>
 349 <a class="sourceLine" id="cb11-69" data-line-number="69">            rc = <span class="fu">sqlite3_exec</span>(db.<span class="fu">get</span>(), sql, callback, Pointer.<span class="fu">ofNull</span>(), errMsg);</a>
 350 <a class="sourceLine" id="cb11-70" data-line-number="70"></a>
 351 <a class="sourceLine" id="cb11-71" data-line-number="71">            <span class="kw">if</span> (rc != <span class="dv">0</span>) {</a>
 352 <a class="sourceLine" id="cb11-72" data-line-number="72">                <span class="bu">System</span>.<span class="fu">err</span>.<span class="fu">println</span>(<span class="st">&quot;sqlite3_exec failed: &quot;</span> + rc);</a>
 353 <a class="sourceLine" id="cb11-73" data-line-number="73">                <span class="bu">System</span>.<span class="fu">err</span>.<span class="fu">println</span>(<span class="st">&quot;SQL error: &quot;</span> + Pointer.<span class="fu">toString</span>(errMsg.<span class="fu">get</span>()));</a>
 354 <a class="sourceLine" id="cb11-74" data-line-number="74">                <span class="fu">sqlite3_free</span>(errMsg.<span class="fu">get</span>());</a>
 355 <a class="sourceLine" id="cb11-75" data-line-number="75">            }</a>
 356 <a class="sourceLine" id="cb11-76" data-line-number="76"> </a>
 357 <a class="sourceLine" id="cb11-77" data-line-number="77">            <span class="fu">sqlite3_close</span>(db.<span class="fu">get</span>());</a>
 358 <a class="sourceLine" id="cb11-78" data-line-number="78">        }</a>
 359 <a class="sourceLine" id="cb11-79" data-line-number="79">   }</a>
 360 <a class="sourceLine" id="cb11-80" data-line-number="80">}</a></code></pre></div>
 361 <h3 id="compiling-and-running-sqlite-java-example">Compiling and Running sqlite Java example</h3>
 362 <div class="sourceCode" id="cb12"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb12-1" data-line-number="1"></a>
 363 <a class="sourceLine" id="cb12-2" data-line-number="2"><span class="ex">javac</span> -cp sqlite3.jar SqlMain.java</a>
 364 <a class="sourceLine" id="cb12-3" data-line-number="3"><span class="ex">java</span> -cp sqlite3.jar:. SqlMain</a></code></pre></div>
 365 <h2 id="using-sqlite3-library-in-your-java-program-ubuntu-16.04">Using sqlite3 library in your Java program (Ubuntu 16.04)</h2>
 366 <h3 id="installing-sqlite3">Installing sqlite3</h3>
 367 <p>On Ubuntu (16.04) to install sqlite3 headers and libraries the following command is required:</p>
 368 <div class="sourceCode" id="cb13"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb13-1" data-line-number="1"><span class="fu">sudo</span> apt-get install libsqlite3-dev</a></code></pre></div>
 369 <p>This should install the sqlite3 header (under <code>/usr/include</code>), as well as the sqlite3 shared library (under <code>/usr/lib/x86_64-linux-gnu</code>).</p>
 370 <h3 id="jextract-a-jar-file-for-sqlite3.h-1">jextract a jar file for sqlite3.h</h3>
 371 <p>To extract sqlite, run the following command:</p>
 372 <div class="sourceCode" id="cb14"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb14-1" data-line-number="1"><span class="ex">jextract</span>  /usr/include/sqlite3.h -t org.sqlite -lsqlite3 \</a>
 373 <a class="sourceLine" id="cb14-2" data-line-number="2">          -L /usr/lib/x86_64-linux-gnu --record-library-path \</a>
 374 <a class="sourceLine" id="cb14-3" data-line-number="3">           --exclude-symbols sqlite3_vmprintf \</a>
 375 <a class="sourceLine" id="cb14-4" data-line-number="4">           --exclude-symbols sqlite3_vsnprintf \</a>
 376 <a class="sourceLine" id="cb14-5" data-line-number="5">           -o sqlite3.jar</a></code></pre></div>
 377 <h3 id="compiling-and-running-sqlite-java-example-1">Compiling and Running sqlite Java example</h3>
 378 <p>Please refer to the Mac OS instructions; once the library as been extracted (as per the instructions above), the sample program shown in that section should work on Ubuntu as well.</p>
 379 <h2 id="using-blas-library">Using BLAS library</h2>
 380 <p>BLAS is a popular library that allows fast matrix and vector computation: <a href="http://www.netlib.org/blas/" class="uri">http://www.netlib.org/blas/</a>.</p>
 381 <h3 id="installing-openblas-mac-os">Installing OpenBLAS (Mac OS)</h3>
 382 <p>On Mac, blas is available as part of the OpenBLAS library: <a href="https://github.com/xianyi/OpenBLAS/wiki" class="uri">https://github.com/xianyi/OpenBLAS/wiki</a></p>
 383 <p>OpenBLAS is an optimized BLAS library based on GotoBLAS2 1.13 BSD version.</p>
 384 <p>You can install openblas using HomeBrew</p>
 385 <div class="sourceCode" id="cb15"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb15-1" data-line-number="1"></a>
 386 <a class="sourceLine" id="cb15-2" data-line-number="2"><span class="ex">brew</span> install openblas</a></code></pre></div>
 387 <p>It installs include and lib directories under /usr/local/opt/openblas</p>
 388 <h3 id="installing-openblas-ubuntu-16.04">Installing OpenBLAS (Ubuntu 16.04)</h3>
 389 <p>On Ubuntu, blas is distributed as part of the atlas library: <a href="http://math-atlas.sourceforge.net/" class="uri">http://math-atlas.sourceforge.net/</a>.</p>
 390 <p>You can install atlas using apt</p>
 391 <div class="sourceCode" id="cb16"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb16-1" data-line-number="1"></a>
 392 <a class="sourceLine" id="cb16-2" data-line-number="2"><span class="fu">sudo</span> apt-get install libatlas-base-dev</a></code></pre></div>
 393 <p>This command will install include files under <code>/usr/include/atlas</code> and corresponding libraries under <code>/usr/lib/atlas-dev</code>.</p>
 394 <h3 id="jextracting-cblas.h-macos">jextracting cblas.h (MacOS)</h3>
 395 <p>The following command can be used to extract cblas.h on MacOs</p>
 396 <div class="sourceCode" id="cb17"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb17-1" data-line-number="1"></a>
 397 <a class="sourceLine" id="cb17-2" data-line-number="2"><span class="ex">jextract</span> -C <span class="st">&quot;-D FORCE_OPENBLAS_COMPLEX_STRUCT&quot;</span> \</a>
 398 <a class="sourceLine" id="cb17-3" data-line-number="3">  -L /usr/local/opt/openblas/lib -I /usr/local/opt/openblas \</a>
 399 <a class="sourceLine" id="cb17-4" data-line-number="4">  -l openblas -t blas --record-library-path /usr/local/opt/openblas/include/cblas.h \</a>
 400 <a class="sourceLine" id="cb17-5" data-line-number="5">  -o cblas.jar</a></code></pre></div>
 401 <p>The FORCE_OPENBLAS_COMPLEX_STRUCT define is needed because jextract does not yet handle C99 <code>_Complex</code> types. The rest of the options are standard ones.</p>
 402 <h3 id="jextracting-cblas.h-ubuntu-16.04">jextracting cblas.h (Ubuntu 16.04)</h3>
 403 <p>The following command can be used to extract cblas.h on Ubuntu</p>
 404 <div class="sourceCode" id="cb18"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb18-1" data-line-number="1"></a>
 405 <a class="sourceLine" id="cb18-2" data-line-number="2"><span class="ex">jextract</span> -L /usr/lib/atlas-base -I /usr/include/atlas/ \</a>
 406 <a class="sourceLine" id="cb18-3" data-line-number="3">   -l cblas -t blas --record-library-path \</a>
 407 <a class="sourceLine" id="cb18-4" data-line-number="4">   /usr/include/atlas/cblas.h -o cblas.jar</a></code></pre></div>
 408 <h3 id="java-sample-code-that-uses-cblas-library">Java sample code that uses cblas library</h3>
 409 <div class="sourceCode" id="cb19"><pre class="sourceCode java"><code class="sourceCode java"><a class="sourceLine" id="cb19-1" data-line-number="1"></a>
 410 <a class="sourceLine" id="cb19-2" data-line-number="2"><span class="kw">import</span><span class="im"> blas.cblas;</span></a>
 411 <a class="sourceLine" id="cb19-3" data-line-number="3"></a>
 412 <a class="sourceLine" id="cb19-4" data-line-number="4"><span class="kw">import static</span><span class="im"> blas.cblas_h.*;</span></a>
 413 <a class="sourceLine" id="cb19-5" data-line-number="5"><span class="kw">import static</span><span class="im"> blas.cblas_h.CBLAS_ORDER.*;</span></a>
 414 <a class="sourceLine" id="cb19-6" data-line-number="6"><span class="kw">import static</span><span class="im"> blas.cblas_h.CBLAS_TRANSPOSE.*;</span></a>
 415 <a class="sourceLine" id="cb19-7" data-line-number="7"></a>
 416 <a class="sourceLine" id="cb19-8" data-line-number="8"><span class="kw">import</span><span class="im"> java.foreign.NativeTypes;</span></a>
 417 <a class="sourceLine" id="cb19-9" data-line-number="9"><span class="kw">import</span><span class="im"> java.foreign.Scope;</span></a>
 418 <a class="sourceLine" id="cb19-10" data-line-number="10"><span class="kw">import</span><span class="im"> java.foreign.memory.Array;</span></a>
 419 <a class="sourceLine" id="cb19-11" data-line-number="11"></a>
 420 <a class="sourceLine" id="cb19-12" data-line-number="12"><span class="kw">public</span> <span class="kw">class</span> TestBlas {</a>
 421 <a class="sourceLine" id="cb19-13" data-line-number="13">   <span class="kw">public</span> <span class="dt">static</span> <span class="dt">void</span> <span class="fu">main</span>(<span class="bu">String</span>[] args) {</a>
 422 <a class="sourceLine" id="cb19-14" data-line-number="14">       <span class="at">@cblas</span>.<span class="fu">CBLAS_ORDER</span> <span class="dt">int</span> Layout;</a>
 423 <a class="sourceLine" id="cb19-15" data-line-number="15">       <span class="at">@cblas</span>.<span class="fu">CBLAS_TRANSPOSE</span> <span class="dt">int</span> transa;</a>
 424 <a class="sourceLine" id="cb19-16" data-line-number="16"></a>
 425 <a class="sourceLine" id="cb19-17" data-line-number="17">       <span class="dt">double</span> alpha, beta;</a>
 426 <a class="sourceLine" id="cb19-18" data-line-number="18">       <span class="dt">int</span> m, n, lda, incx, incy, i;</a>
 427 <a class="sourceLine" id="cb19-19" data-line-number="19"></a>
 428 <a class="sourceLine" id="cb19-20" data-line-number="20">       Layout = CblasColMajor;</a>
 429 <a class="sourceLine" id="cb19-21" data-line-number="21">       transa = CblasNoTrans;</a>
 430 <a class="sourceLine" id="cb19-22" data-line-number="22"></a>
 431 <a class="sourceLine" id="cb19-23" data-line-number="23">       m = <span class="dv">4</span>; <span class="co">/* Size of Column ( the number of rows ) */</span></a>
 432 <a class="sourceLine" id="cb19-24" data-line-number="24">       n = <span class="dv">4</span>; <span class="co">/* Size of Row ( the number of columns ) */</span></a>
 433 <a class="sourceLine" id="cb19-25" data-line-number="25">       lda = <span class="dv">4</span>; <span class="co">/* Leading dimension of 5 * 4 matrix is 5 */</span></a>
 434 <a class="sourceLine" id="cb19-26" data-line-number="26">       incx = <span class="dv">1</span>;</a>
 435 <a class="sourceLine" id="cb19-27" data-line-number="27">       incy = <span class="dv">1</span>;</a>
 436 <a class="sourceLine" id="cb19-28" data-line-number="28">       alpha = <span class="dv">1</span>;</a>
 437 <a class="sourceLine" id="cb19-29" data-line-number="29">       beta = <span class="dv">0</span>;</a>
 438 <a class="sourceLine" id="cb19-30" data-line-number="30"></a>
 439 <a class="sourceLine" id="cb19-31" data-line-number="31">       <span class="kw">try</span> (Scope sc = <span class="fu">scope</span>().<span class="fu">fork</span>()){</a>
 440 <a class="sourceLine" id="cb19-32" data-line-number="32">           <span class="bu">Array</span>&lt;<span class="bu">Double</span>&gt; a = sc.<span class="fu">allocateArray</span>(NativeTypes.<span class="fu">DOUBLE</span>, m * n);</a>
 441 <a class="sourceLine" id="cb19-33" data-line-number="33">           <span class="bu">Array</span>&lt;<span class="bu">Double</span>&gt; x = sc.<span class="fu">allocateArray</span>(NativeTypes.<span class="fu">DOUBLE</span>, n);</a>
 442 <a class="sourceLine" id="cb19-34" data-line-number="34">           <span class="bu">Array</span>&lt;<span class="bu">Double</span>&gt; y = sc.<span class="fu">allocateArray</span>(NativeTypes.<span class="fu">DOUBLE</span>, n);</a>
 443 <a class="sourceLine" id="cb19-35" data-line-number="35">           <span class="co">/* The elements of the first column */</span></a>
 444 <a class="sourceLine" id="cb19-36" data-line-number="36">           a.<span class="fu">set</span>(<span class="dv">0</span>, <span class="fl">1.0</span>);</a>
 445 <a class="sourceLine" id="cb19-37" data-line-number="37">           a.<span class="fu">set</span>(<span class="dv">1</span>, <span class="fl">2.0</span>);</a>
 446 <a class="sourceLine" id="cb19-38" data-line-number="38">           a.<span class="fu">set</span>(<span class="dv">2</span>, <span class="fl">3.0</span>);</a>
 447 <a class="sourceLine" id="cb19-39" data-line-number="39">           a.<span class="fu">set</span>(<span class="dv">3</span>, <span class="fl">4.0</span>);</a>
 448 <a class="sourceLine" id="cb19-40" data-line-number="40">           <span class="co">/* The elements of the second column */</span></a>
 449 <a class="sourceLine" id="cb19-41" data-line-number="41">           a.<span class="fu">set</span>(m, <span class="fl">1.0</span>);</a>
 450 <a class="sourceLine" id="cb19-42" data-line-number="42">           a.<span class="fu">set</span>(m + <span class="dv">1</span>, <span class="fl">1.0</span>);</a>
 451 <a class="sourceLine" id="cb19-43" data-line-number="43">           a.<span class="fu">set</span>(m + <span class="dv">2</span>, <span class="fl">1.0</span>);</a>
 452 <a class="sourceLine" id="cb19-44" data-line-number="44">           a.<span class="fu">set</span>(m + <span class="dv">3</span>, <span class="fl">1.0</span>);</a>
 453 <a class="sourceLine" id="cb19-45" data-line-number="45">           <span class="co">/* The elements of the third column */</span></a>
 454 <a class="sourceLine" id="cb19-46" data-line-number="46">           a.<span class="fu">set</span>(m * <span class="dv">2</span>, <span class="fl">3.0</span>);</a>
 455 <a class="sourceLine" id="cb19-47" data-line-number="47">           a.<span class="fu">set</span>(m * <span class="dv">2</span> + <span class="dv">1</span>, <span class="fl">4.0</span>);</a>
 456 <a class="sourceLine" id="cb19-48" data-line-number="48">           a.<span class="fu">set</span>(m * <span class="dv">2</span> + <span class="dv">2</span>, <span class="fl">5.0</span>);</a>
 457 <a class="sourceLine" id="cb19-49" data-line-number="49">           a.<span class="fu">set</span>(m * <span class="dv">2</span> + <span class="dv">3</span>, <span class="fl">6.0</span>);</a>
 458 <a class="sourceLine" id="cb19-50" data-line-number="50">           <span class="co">/* The elements of the fourth column */</span></a>
 459 <a class="sourceLine" id="cb19-51" data-line-number="51">           a.<span class="fu">set</span>(m * <span class="dv">3</span>, <span class="fl">5.0</span>);</a>
 460 <a class="sourceLine" id="cb19-52" data-line-number="52">           a.<span class="fu">set</span>(m * <span class="dv">3</span> + <span class="dv">1</span>, <span class="fl">6.0</span>);</a>
 461 <a class="sourceLine" id="cb19-53" data-line-number="53">           a.<span class="fu">set</span>(m * <span class="dv">3</span> + <span class="dv">2</span>, <span class="fl">7.0</span>);</a>
 462 <a class="sourceLine" id="cb19-54" data-line-number="54">           a.<span class="fu">set</span>(m * <span class="dv">3</span> + <span class="dv">3</span>, <span class="fl">8.0</span>);</a>
 463 <a class="sourceLine" id="cb19-55" data-line-number="55">           <span class="co">/* The elemetns of x and y */</span></a>
 464 <a class="sourceLine" id="cb19-56" data-line-number="56">           x.<span class="fu">set</span>(<span class="dv">0</span>, <span class="fl">1.0</span>);</a>
 465 <a class="sourceLine" id="cb19-57" data-line-number="57">           x.<span class="fu">set</span>(<span class="dv">1</span>, <span class="fl">2.0</span>);</a>
 466 <a class="sourceLine" id="cb19-58" data-line-number="58">           x.<span class="fu">set</span>(<span class="dv">2</span>, <span class="fl">1.0</span>);</a>
 467 <a class="sourceLine" id="cb19-59" data-line-number="59">           x.<span class="fu">set</span>(<span class="dv">3</span>, <span class="fl">1.0</span>);</a>
 468 <a class="sourceLine" id="cb19-60" data-line-number="60">           y.<span class="fu">set</span>(<span class="dv">0</span>, <span class="fl">0.0</span>);</a>
 469 <a class="sourceLine" id="cb19-61" data-line-number="61">           y.<span class="fu">set</span>(<span class="dv">1</span>, <span class="fl">0.0</span>);</a>
 470 <a class="sourceLine" id="cb19-62" data-line-number="62">           y.<span class="fu">set</span>(<span class="dv">2</span>, <span class="fl">0.0</span>);</a>
 471 <a class="sourceLine" id="cb19-63" data-line-number="63">           y.<span class="fu">set</span>(<span class="dv">3</span>, <span class="fl">0.0</span>);</a>
 472 <a class="sourceLine" id="cb19-64" data-line-number="64"></a>
 473 <a class="sourceLine" id="cb19-65" data-line-number="65">           <span class="fu">cblas_dgemv</span>(Layout, transa, m, n, alpha, a.<span class="fu">elementPointer</span>(), lda, x.<span class="fu">elementPointer</span>(), incx, beta,</a>
 474 <a class="sourceLine" id="cb19-66" data-line-number="66">                   y.<span class="fu">elementPointer</span>(), incy);</a>
 475 <a class="sourceLine" id="cb19-67" data-line-number="67">           <span class="co">/* Print y */</span></a>
 476 <a class="sourceLine" id="cb19-68" data-line-number="68">           <span class="kw">for</span> (i = <span class="dv">0</span>; i &lt; n; i++)</a>
 477 <a class="sourceLine" id="cb19-69" data-line-number="69">               <span class="bu">System</span>.<span class="fu">out</span>.<span class="fu">print</span>(<span class="bu">String</span><span class="fu">.format</span>(<span class="st">&quot; y</span><span class="sc">%d</span><span class="st"> = </span><span class="sc">%f\n</span><span class="st">&quot;</span>, i, y.<span class="fu">get</span>(i)));</a>
 478 <a class="sourceLine" id="cb19-70" data-line-number="70">       }</a>
 479 <a class="sourceLine" id="cb19-71" data-line-number="71">   }</a>
 480 <a class="sourceLine" id="cb19-72" data-line-number="72">}</a></code></pre></div>
 481 <h3 id="compiling-and-running-the-above-cblas-samples">Compiling and running the above cblas samples</h3>
 482 <div class="sourceCode" id="cb20"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb20-1" data-line-number="1"></a>
 483 <a class="sourceLine" id="cb20-2" data-line-number="2"><span class="ex">javac</span> -cp cblas.jar TestBlas.java</a>
 484 <a class="sourceLine" id="cb20-3" data-line-number="3"></a>
 485 <a class="sourceLine" id="cb20-4" data-line-number="4"><span class="ex">java</span> -cp cblas.jar:. TestBlas</a></code></pre></div>
 486 <h2 id="using-lapack-library-ubuntu">Using LAPACK library (Ubuntu)</h2>
 487 <p>On Ubuntu, the same steps used to install the blas (via atlas) library also install headers and libraries for the LAPACK library, a linear algebra computation library built on top of blas.</p>
 488 <h3 id="jextracting-clapack.h-ubuntu-16.04">jextracting clapack.h (Ubuntu 16.04)</h3>
 489 <p>The following command can be used to extract the LAPACK header:</p>
 490 <div class="sourceCode" id="cb21"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb21-1" data-line-number="1"></a>
 491 <a class="sourceLine" id="cb21-2" data-line-number="2"><span class="ex">jextract</span> -L /usr/lib/atlas-base/atlas -I /usr/include/atlas/ \</a>
 492 <a class="sourceLine" id="cb21-3" data-line-number="3">   -l lapack -t lapack --record-library-path /usr/include/atlas/clapack.h -o clapack.jar</a></code></pre></div>
 493 <h3 id="java-sample-code-that-uses-lapack-library">Java sample code that uses LAPACK library</h3>
 494 <div class="sourceCode" id="cb22"><pre class="sourceCode java"><code class="sourceCode java"><a class="sourceLine" id="cb22-1" data-line-number="1"><span class="kw">import</span><span class="im"> java.foreign.NativeTypes;</span></a>
 495 <a class="sourceLine" id="cb22-2" data-line-number="2"><span class="kw">import</span><span class="im"> java.foreign.Scope;</span></a>
 496 <a class="sourceLine" id="cb22-3" data-line-number="3"><span class="kw">import</span><span class="im"> java.foreign.memory.Array;</span></a>
 497 <a class="sourceLine" id="cb22-4" data-line-number="4"></a>
 498 <a class="sourceLine" id="cb22-5" data-line-number="5"><span class="kw">import static</span><span class="im"> lapack.clapack_h.*;</span></a>
 499 <a class="sourceLine" id="cb22-6" data-line-number="6"><span class="kw">import static</span><span class="im"> lapack.cblas_h.*;</span></a>
 500 <a class="sourceLine" id="cb22-7" data-line-number="7"></a>
 501 <a class="sourceLine" id="cb22-8" data-line-number="8"><span class="kw">public</span> <span class="kw">class</span> TestLapack {</a>
 502 <a class="sourceLine" id="cb22-9" data-line-number="9">    <span class="kw">public</span> <span class="dt">static</span> <span class="dt">void</span> <span class="fu">main</span>(<span class="bu">String</span>[] args) {</a>
 503 <a class="sourceLine" id="cb22-10" data-line-number="10"></a>
 504 <a class="sourceLine" id="cb22-11" data-line-number="11">        <span class="co">/* Locals */</span></a>
 505 <a class="sourceLine" id="cb22-12" data-line-number="12">        <span class="kw">try</span> (Scope sc = lapack.<span class="fu">clapack_h</span>.<span class="fu">scope</span>().<span class="fu">fork</span>()) {</a>
 506 <a class="sourceLine" id="cb22-13" data-line-number="13">            <span class="bu">Array</span>&lt;<span class="bu">Double</span>&gt; A = sc.<span class="fu">allocateArray</span>(NativeTypes.<span class="fu">DOUBLE</span>, <span class="kw">new</span> <span class="dt">double</span>[]{</a>
 507 <a class="sourceLine" id="cb22-14" data-line-number="14">                    <span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">4</span>, <span class="dv">5</span>, <span class="dv">1</span>, <span class="dv">3</span>, <span class="dv">5</span>, <span class="dv">2</span>, <span class="dv">4</span>, <span class="dv">1</span>, <span class="dv">4</span>, <span class="dv">2</span>, <span class="dv">5</span>, <span class="dv">3</span></a>
 508 <a class="sourceLine" id="cb22-15" data-line-number="15">            });</a>
 509 <a class="sourceLine" id="cb22-16" data-line-number="16">            <span class="bu">Array</span>&lt;<span class="bu">Double</span>&gt; b = sc.<span class="fu">allocateArray</span>(NativeTypes.<span class="fu">DOUBLE</span>, <span class="kw">new</span> <span class="dt">double</span>[]{</a>
 510 <a class="sourceLine" id="cb22-17" data-line-number="17">                    <span class="dv">-10</span>, <span class="dv">12</span>, <span class="dv">14</span>, <span class="dv">16</span>, <span class="dv">18</span>, <span class="dv">-3</span>, <span class="dv">14</span>, <span class="dv">12</span>, <span class="dv">16</span>, <span class="dv">16</span></a>
 511 <a class="sourceLine" id="cb22-18" data-line-number="18">            });</a>
 512 <a class="sourceLine" id="cb22-19" data-line-number="19">            <span class="dt">int</span> info, m, n, lda, ldb, nrhs;</a>
 513 <a class="sourceLine" id="cb22-20" data-line-number="20"></a>
 514 <a class="sourceLine" id="cb22-21" data-line-number="21">            <span class="co">/* Initialization */</span></a>
 515 <a class="sourceLine" id="cb22-22" data-line-number="22">            m = <span class="dv">5</span>;</a>
 516 <a class="sourceLine" id="cb22-23" data-line-number="23">            n = <span class="dv">3</span>;</a>
 517 <a class="sourceLine" id="cb22-24" data-line-number="24">            nrhs = <span class="dv">2</span>;</a>
 518 <a class="sourceLine" id="cb22-25" data-line-number="25">            lda = <span class="dv">5</span>;</a>
 519 <a class="sourceLine" id="cb22-26" data-line-number="26">            ldb = <span class="dv">5</span>;</a>
 520 <a class="sourceLine" id="cb22-27" data-line-number="27"></a>
 521 <a class="sourceLine" id="cb22-28" data-line-number="28">            <span class="co">/* Print Entry Matrix */</span></a>
 522 <a class="sourceLine" id="cb22-29" data-line-number="29">            <span class="fu">print_matrix_colmajor</span>(<span class="st">&quot;Entry Matrix A&quot;</span>, m, n, A, lda );</a>
 523 <a class="sourceLine" id="cb22-30" data-line-number="30">            <span class="co">/* Print Right Rand Side */</span></a>
 524 <a class="sourceLine" id="cb22-31" data-line-number="31">            <span class="fu">print_matrix_colmajor</span>(<span class="st">&quot;Right Hand Side b&quot;</span>, n, nrhs, b, ldb );</a>
 525 <a class="sourceLine" id="cb22-32" data-line-number="32">            <span class="bu">System</span>.<span class="fu">out</span>.<span class="fu">println</span>();</a>
 526 <a class="sourceLine" id="cb22-33" data-line-number="33"></a>
 527 <a class="sourceLine" id="cb22-34" data-line-number="34">            <span class="co">/* Executable statements */</span></a>
 528 <a class="sourceLine" id="cb22-35" data-line-number="35">            <span class="co">//            printf( &quot;LAPACKE_dgels (col-major, high-level) Example Program Results\n&quot; );</span></a>
 529 <a class="sourceLine" id="cb22-36" data-line-number="36">            <span class="co">/* Solve least squares problem*/</span></a>
 530 <a class="sourceLine" id="cb22-37" data-line-number="37">            info = <span class="fu">clapack_dgels</span>(CblasColMajor, CblasNoTrans, m, n, nrhs, A.<span class="fu">elementPointer</span>(), lda, b.<span class="fu">elementPointer</span>(), ldb);</a>
 531 <a class="sourceLine" id="cb22-38" data-line-number="38"></a>
 532 <a class="sourceLine" id="cb22-39" data-line-number="39">            <span class="co">/* Print Solution */</span></a>
 533 <a class="sourceLine" id="cb22-40" data-line-number="40">            <span class="fu">print_matrix_colmajor</span>(<span class="st">&quot;Solution&quot;</span>, n, nrhs, b, ldb );</a>
 534 <a class="sourceLine" id="cb22-41" data-line-number="41">            <span class="bu">System</span>.<span class="fu">out</span>.<span class="fu">println</span>();</a>
 535 <a class="sourceLine" id="cb22-42" data-line-number="42">            <span class="bu">System</span>.<span class="fu">exit</span>(info);</a>
 536 <a class="sourceLine" id="cb22-43" data-line-number="43">        }</a>
 537 <a class="sourceLine" id="cb22-44" data-line-number="44">    }</a>
 538 <a class="sourceLine" id="cb22-45" data-line-number="45"></a>
 539 <a class="sourceLine" id="cb22-46" data-line-number="46">    <span class="dt">static</span> <span class="dt">void</span> <span class="fu">print_matrix_colmajor</span>(<span class="bu">String</span> msg, <span class="dt">int</span> m, <span class="dt">int</span> n, <span class="bu">Array</span>&lt;<span class="bu">Double</span>&gt; mat, <span class="dt">int</span> ldm) {</a>
 540 <a class="sourceLine" id="cb22-47" data-line-number="47">        <span class="dt">int</span> i, j;</a>
 541 <a class="sourceLine" id="cb22-48" data-line-number="48">        <span class="bu">System</span>.<span class="fu">out.printf</span>(<span class="st">&quot;</span><span class="sc">\n</span><span class="st"> </span><span class="sc">%s\n</span><span class="st">&quot;</span>, msg);</a>
 542 <a class="sourceLine" id="cb22-49" data-line-number="49"></a>
 543 <a class="sourceLine" id="cb22-50" data-line-number="50">        <span class="kw">for</span>( i = <span class="dv">0</span>; i &lt; m; i++ ) {</a>
 544 <a class="sourceLine" id="cb22-51" data-line-number="51">            <span class="kw">for</span>( j = <span class="dv">0</span>; j &lt; n; j++ ) <span class="bu">System</span>.<span class="fu">out.printf</span>(<span class="st">&quot; </span><span class="sc">%6.2f</span><span class="st">&quot;</span>, mat.<span class="fu">get</span>(i+j*ldm));</a>
 545 <a class="sourceLine" id="cb22-52" data-line-number="52">            <span class="bu">System</span>.<span class="fu">out.printf</span>( <span class="st">&quot;</span><span class="sc">\n</span><span class="st">&quot;</span> );</a>
 546 <a class="sourceLine" id="cb22-53" data-line-number="53">        }</a>
 547 <a class="sourceLine" id="cb22-54" data-line-number="54">    }</a>
 548 <a class="sourceLine" id="cb22-55" data-line-number="55">}</a></code></pre></div>
 549 <h3 id="compiling-and-running-the-above-lapack-sample">Compiling and running the above LAPACK sample</h3>
 550 <div class="sourceCode" id="cb23"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb23-1" data-line-number="1"></a>
 551 <a class="sourceLine" id="cb23-2" data-line-number="2"><span class="ex">javac</span> -cp clapack.jar TestLapack.java</a>
 552 <a class="sourceLine" id="cb23-3" data-line-number="3"></a>
 553 <a class="sourceLine" id="cb23-4" data-line-number="4"><span class="ex">java</span> -cp clapack.jar:. TestLapack</a></code></pre></div>
 554 <h2 id="using-lapack-library-mac-os">Using LAPACK library (Mac OS)</h2>
 555 <p>On Mac OS, lapack is installed under /usr/local/opt/lapack directory.</p>
 556 <h3 id="jextracting-lapacke.h">jextracting lapacke.h</h3>
 557 <div class="sourceCode" id="cb24"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb24-1" data-line-number="1"></a>
 558 <a class="sourceLine" id="cb24-2" data-line-number="2"><span class="ex">jextract</span> -L /usr/local/opt/lapack/lib -I /usr/local/opt/lapack/ \</a>
 559 <a class="sourceLine" id="cb24-3" data-line-number="3">  -l lapacke -t lapack --record-library-path /usr/local/opt/lapack/include/lapacke.h -o clapack.jar</a></code></pre></div>
 560 <h3 id="java-sample-code-that-uses-lapack-library-1">Java sample code that uses LAPACK library</h3>
 561 <div class="sourceCode" id="cb25"><pre class="sourceCode java"><code class="sourceCode java"><a class="sourceLine" id="cb25-1" data-line-number="1"></a>
 562 <a class="sourceLine" id="cb25-2" data-line-number="2"><span class="kw">import</span><span class="im"> java.foreign.NativeTypes;</span></a>
 563 <a class="sourceLine" id="cb25-3" data-line-number="3"><span class="kw">import</span><span class="im"> java.foreign.Scope;</span></a>
 564 <a class="sourceLine" id="cb25-4" data-line-number="4"><span class="kw">import</span><span class="im"> java.foreign.memory.Array;</span></a>
 565 <a class="sourceLine" id="cb25-5" data-line-number="5"></a>
 566 <a class="sourceLine" id="cb25-6" data-line-number="6"><span class="kw">import static</span><span class="im"> lapack.lapacke_h.*;</span></a>
 567 <a class="sourceLine" id="cb25-7" data-line-number="7"></a>
 568 <a class="sourceLine" id="cb25-8" data-line-number="8"><span class="kw">public</span> <span class="kw">class</span> TestLapack {</a>
 569 <a class="sourceLine" id="cb25-9" data-line-number="9">    <span class="kw">public</span> <span class="dt">static</span> <span class="dt">void</span> <span class="fu">main</span>(<span class="bu">String</span>[] args) {</a>
 570 <a class="sourceLine" id="cb25-10" data-line-number="10"></a>
 571 <a class="sourceLine" id="cb25-11" data-line-number="11">        <span class="co">/* Locals */</span></a>
 572 <a class="sourceLine" id="cb25-12" data-line-number="12">        <span class="kw">try</span> (Scope sc = <span class="fu">scope</span>().<span class="fu">fork</span>()) {</a>
 573 <a class="sourceLine" id="cb25-13" data-line-number="13">            <span class="bu">Array</span>&lt;<span class="bu">Double</span>&gt; A = sc.<span class="fu">allocateArray</span>(NativeTypes.<span class="fu">DOUBLE</span>, <span class="kw">new</span> <span class="dt">double</span>[]{</a>
 574 <a class="sourceLine" id="cb25-14" data-line-number="14">                    <span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">4</span>, <span class="dv">5</span>, <span class="dv">1</span>, <span class="dv">3</span>, <span class="dv">5</span>, <span class="dv">2</span>, <span class="dv">4</span>, <span class="dv">1</span>, <span class="dv">4</span>, <span class="dv">2</span>, <span class="dv">5</span>, <span class="dv">3</span></a>
 575 <a class="sourceLine" id="cb25-15" data-line-number="15">            });</a>
 576 <a class="sourceLine" id="cb25-16" data-line-number="16">            <span class="bu">Array</span>&lt;<span class="bu">Double</span>&gt; b = sc.<span class="fu">allocateArray</span>(NativeTypes.<span class="fu">DOUBLE</span>, <span class="kw">new</span> <span class="dt">double</span>[]{</a>
 577 <a class="sourceLine" id="cb25-17" data-line-number="17">                    <span class="dv">-10</span>, <span class="dv">12</span>, <span class="dv">14</span>, <span class="dv">16</span>, <span class="dv">18</span>, <span class="dv">-3</span>, <span class="dv">14</span>, <span class="dv">12</span>, <span class="dv">16</span>, <span class="dv">16</span></a>
 578 <a class="sourceLine" id="cb25-18" data-line-number="18">            });</a>
 579 <a class="sourceLine" id="cb25-19" data-line-number="19">            <span class="dt">int</span> info, m, n, lda, ldb, nrhs;</a>
 580 <a class="sourceLine" id="cb25-20" data-line-number="20"></a>
 581 <a class="sourceLine" id="cb25-21" data-line-number="21">            <span class="co">/* Initialization */</span></a>
 582 <a class="sourceLine" id="cb25-22" data-line-number="22">            m = <span class="dv">5</span>;</a>
 583 <a class="sourceLine" id="cb25-23" data-line-number="23">            n = <span class="dv">3</span>;</a>
 584 <a class="sourceLine" id="cb25-24" data-line-number="24">            nrhs = <span class="dv">2</span>;</a>
 585 <a class="sourceLine" id="cb25-25" data-line-number="25">            lda = <span class="dv">5</span>;</a>
 586 <a class="sourceLine" id="cb25-26" data-line-number="26">            ldb = <span class="dv">5</span>;</a>
 587 <a class="sourceLine" id="cb25-27" data-line-number="27"></a>
 588 <a class="sourceLine" id="cb25-28" data-line-number="28">            <span class="co">/* Print Entry Matrix */</span></a>
 589 <a class="sourceLine" id="cb25-29" data-line-number="29">            <span class="fu">print_matrix_colmajor</span>(<span class="st">&quot;Entry Matrix A&quot;</span>, m, n, A, lda );</a>
 590 <a class="sourceLine" id="cb25-30" data-line-number="30">            <span class="co">/* Print Right Rand Side */</span></a>
 591 <a class="sourceLine" id="cb25-31" data-line-number="31">            <span class="fu">print_matrix_colmajor</span>(<span class="st">&quot;Right Hand Side b&quot;</span>, n, nrhs, b, ldb );</a>
 592 <a class="sourceLine" id="cb25-32" data-line-number="32">            <span class="bu">System</span>.<span class="fu">out</span>.<span class="fu">println</span>();</a>
 593 <a class="sourceLine" id="cb25-33" data-line-number="33"></a>
 594 <a class="sourceLine" id="cb25-34" data-line-number="34">            <span class="co">/* Executable statements */</span></a>
 595 <a class="sourceLine" id="cb25-35" data-line-number="35">            <span class="co">//            printf( &quot;LAPACKE_dgels (col-major, high-level) Example Program Results\n&quot; );</span></a>
 596 <a class="sourceLine" id="cb25-36" data-line-number="36">            <span class="co">/* Solve least squares problem*/</span></a>
 597 <a class="sourceLine" id="cb25-37" data-line-number="37">            info = <span class="fu">LAPACKE_dgels</span>(LAPACK_COL_MAJOR, (<span class="dt">byte</span>)<span class="ch">'N'</span>, m, n, nrhs, A.<span class="fu">elementPointer</span>(), lda, b.<span class="fu">elementPointer</span>(), ldb);</a>
 598 <a class="sourceLine" id="cb25-38" data-line-number="38"></a>
 599 <a class="sourceLine" id="cb25-39" data-line-number="39">            <span class="co">/* Print Solution */</span></a>
 600 <a class="sourceLine" id="cb25-40" data-line-number="40">            <span class="fu">print_matrix_colmajor</span>(<span class="st">&quot;Solution&quot;</span>, n, nrhs, b, ldb );</a>
 601 <a class="sourceLine" id="cb25-41" data-line-number="41">            <span class="bu">System</span>.<span class="fu">out</span>.<span class="fu">println</span>();</a>
 602 <a class="sourceLine" id="cb25-42" data-line-number="42">            <span class="bu">System</span>.<span class="fu">exit</span>(info);</a>
 603 <a class="sourceLine" id="cb25-43" data-line-number="43">        }</a>
 604 <a class="sourceLine" id="cb25-44" data-line-number="44">    }</a>
 605 <a class="sourceLine" id="cb25-45" data-line-number="45"></a>
 606 <a class="sourceLine" id="cb25-46" data-line-number="46">    <span class="dt">static</span> <span class="dt">void</span> <span class="fu">print_matrix_colmajor</span>(<span class="bu">String</span> msg, <span class="dt">int</span> m, <span class="dt">int</span> n, <span class="bu">Array</span>&lt;<span class="bu">Double</span>&gt; mat, <span class="dt">int</span> ldm) {</a>
 607 <a class="sourceLine" id="cb25-47" data-line-number="47">        <span class="dt">int</span> i, j;</a>
 608 <a class="sourceLine" id="cb25-48" data-line-number="48">        <span class="bu">System</span>.<span class="fu">out.printf</span>(<span class="st">&quot;</span><span class="sc">\n</span><span class="st"> </span><span class="sc">%s\n</span><span class="st">&quot;</span>, msg);</a>
 609 <a class="sourceLine" id="cb25-49" data-line-number="49"></a>
 610 <a class="sourceLine" id="cb25-50" data-line-number="50">        <span class="kw">for</span>( i = <span class="dv">0</span>; i &lt; m; i++ ) {</a>
 611 <a class="sourceLine" id="cb25-51" data-line-number="51">            <span class="kw">for</span>( j = <span class="dv">0</span>; j &lt; n; j++ ) <span class="bu">System</span>.<span class="fu">out.printf</span>(<span class="st">&quot; </span><span class="sc">%6.2f</span><span class="st">&quot;</span>, mat.<span class="fu">get</span>(i+j*ldm));</a>
 612 <a class="sourceLine" id="cb25-52" data-line-number="52">            <span class="bu">System</span>.<span class="fu">out.printf</span>( <span class="st">&quot;</span><span class="sc">\n</span><span class="st">&quot;</span> );</a>
 613 <a class="sourceLine" id="cb25-53" data-line-number="53">        }</a>
 614 <a class="sourceLine" id="cb25-54" data-line-number="54">    }</a>
 615 <a class="sourceLine" id="cb25-55" data-line-number="55">}</a></code></pre></div>
 616 <h3 id="compiling-and-running-the-above-lapack-sample-1">Compiling and running the above LAPACK sample</h3>
 617 <div class="sourceCode" id="cb26"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb26-1" data-line-number="1"></a>
 618 <a class="sourceLine" id="cb26-2" data-line-number="2"><span class="ex">javac</span> -cp clapack.jar TestLapack.java</a>
 619 <a class="sourceLine" id="cb26-3" data-line-number="3"></a>
 620 <a class="sourceLine" id="cb26-4" data-line-number="4"><span class="ex">java</span> -cp clapack.jar:. TestLapack</a></code></pre></div>
 621 <h2 id="using-libproc-library-to-list-processes-from-java-mac-os">Using libproc library to list processes from Java (Mac OS)</h2>
 622 <h3 id="jextract-a-jar-file-for-libproc.h">jextract a jar file for libproc.h</h3>
 623 <p>jextract -t org.unix -lproc -L /usr/lib --record-library-path -o libproc.jar /usr/include/libproc.h</p>
 624 <h3 id="java-program-that-uses-libproc-to-list-processes">Java program that uses libproc to list processes</h3>
 625 <div class="sourceCode" id="cb27"><pre class="sourceCode java"><code class="sourceCode java"><a class="sourceLine" id="cb27-1" data-line-number="1"></a>
 626 <a class="sourceLine" id="cb27-2" data-line-number="2"><span class="kw">import</span><span class="im"> java.foreign.*;</span></a>
 627 <a class="sourceLine" id="cb27-3" data-line-number="3"><span class="kw">import</span><span class="im"> java.foreign.memory.*;</span></a>
 628 <a class="sourceLine" id="cb27-4" data-line-number="4"><span class="kw">import static</span><span class="im"> org.unix.libproc_h.*;</span></a>
 629 <a class="sourceLine" id="cb27-5" data-line-number="5"></a>
 630 <a class="sourceLine" id="cb27-6" data-line-number="6"><span class="kw">public</span> <span class="kw">class</span> LibprocMain {</a>
 631 <a class="sourceLine" id="cb27-7" data-line-number="7">    <span class="kw">private</span> <span class="dt">static</span> <span class="dt">final</span> <span class="dt">int</span> NAME_BUF_MAX = <span class="dv">256</span>;</a>
 632 <a class="sourceLine" id="cb27-8" data-line-number="8"></a>
 633 <a class="sourceLine" id="cb27-9" data-line-number="9">    <span class="kw">public</span> <span class="dt">static</span> <span class="dt">void</span> <span class="fu">main</span>(<span class="bu">String</span>[] args) {</a>
 634 <a class="sourceLine" id="cb27-10" data-line-number="10">        <span class="co">// Scope for native allocations</span></a>
 635 <a class="sourceLine" id="cb27-11" data-line-number="11">        <span class="kw">try</span> (Scope s = <span class="fu">scope</span>().<span class="fu">fork</span>()) {</a>
 636 <a class="sourceLine" id="cb27-12" data-line-number="12">            <span class="co">// get the number of processes</span></a>
 637 <a class="sourceLine" id="cb27-13" data-line-number="13">            <span class="dt">int</span> numPids = <span class="fu">proc_listallpids</span>(Pointer.<span class="fu">ofNull</span>(), <span class="dv">0</span>);</a>
 638 <a class="sourceLine" id="cb27-14" data-line-number="14">            <span class="co">// allocate an array</span></a>
 639 <a class="sourceLine" id="cb27-15" data-line-number="15">            <span class="bu">Array</span>&lt;<span class="bu">Integer</span>&gt; pids = s.<span class="fu">allocateArray</span>(NativeTypes.<span class="fu">INT32</span>, numPids);</a>
 640 <a class="sourceLine" id="cb27-16" data-line-number="16">            <span class="co">// list all the pids into the native array</span></a>
 641 <a class="sourceLine" id="cb27-17" data-line-number="17">            <span class="fu">proc_listallpids</span>(pids.<span class="fu">elementPointer</span>(), numPids);</a>
 642 <a class="sourceLine" id="cb27-18" data-line-number="18">            <span class="co">// convert native array to java array</span></a>
 643 <a class="sourceLine" id="cb27-19" data-line-number="19">            <span class="dt">int</span>[] jpids = pids.<span class="fu">toArray</span>(num -&gt; <span class="kw">new</span> <span class="dt">int</span>[num]);</a>
 644 <a class="sourceLine" id="cb27-20" data-line-number="20">            <span class="co">// buffer for process name</span></a>
 645 <a class="sourceLine" id="cb27-21" data-line-number="21">            Pointer&lt;<span class="bu">Byte</span>&gt; nameBuf = s.<span class="fu">allocate</span>(NativeTypes.<span class="fu">INT8</span>, NAME_BUF_MAX);</a>
 646 <a class="sourceLine" id="cb27-22" data-line-number="22">            <span class="kw">for</span> (<span class="dt">int</span> i = <span class="dv">0</span>; i &lt; jpids.<span class="fu">length</span>; i++) {</a>
 647 <a class="sourceLine" id="cb27-23" data-line-number="23">                <span class="dt">int</span> pid = jpids[i];</a>
 648 <a class="sourceLine" id="cb27-24" data-line-number="24">                <span class="co">// get the process name</span></a>
 649 <a class="sourceLine" id="cb27-25" data-line-number="25">                <span class="fu">proc_name</span>(pid, nameBuf, NAME_BUF_MAX);</a>
 650 <a class="sourceLine" id="cb27-26" data-line-number="26">                <span class="bu">String</span> procName = Pointer.<span class="fu">toString</span>(nameBuf);</a>
 651 <a class="sourceLine" id="cb27-27" data-line-number="27">                <span class="co">// print pid and process name</span></a>
 652 <a class="sourceLine" id="cb27-28" data-line-number="28">                <span class="bu">System</span>.<span class="fu">out.printf</span>(<span class="st">&quot;</span><span class="sc">%d</span><span class="st"> </span><span class="sc">%s\n</span><span class="st">&quot;</span>, pid, procName);</a>
 653 <a class="sourceLine" id="cb27-29" data-line-number="29">            }</a>
 654 <a class="sourceLine" id="cb27-30" data-line-number="30">        }</a>
 655 <a class="sourceLine" id="cb27-31" data-line-number="31">    }</a>
 656 <a class="sourceLine" id="cb27-32" data-line-number="32">}</a></code></pre></div>
 657 <h3 id="running-the-java-code-that-uses-libproc">Running the Java code that uses libproc</h3>
 658 <div class="sourceCode" id="cb28"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb28-1" data-line-number="1"></a>
 659 <a class="sourceLine" id="cb28-2" data-line-number="2"><span class="ex">javac</span> -cp libproc.jar LibprocMain.java</a>
 660 <a class="sourceLine" id="cb28-3" data-line-number="3"></a>
 661 <a class="sourceLine" id="cb28-4" data-line-number="4"><span class="ex">java</span> -cp libproc.jar:. LibprocMain</a></code></pre></div>
 662 <h2 id="using-readline-library-from-java-code-mac-os">Using readline library from Java code (Mac OS)</h2>
 663 <h3 id="jextract-a-jar-file-for-readline.h">jextract a jar file for readline.h</h3>
 664 <div class="sourceCode" id="cb29"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb29-1" data-line-number="1"></a>
 665 <a class="sourceLine" id="cb29-2" data-line-number="2"><span class="ex">jextract</span> -l readline -L /usr/local/opt/readline/lib/ --record-library-path \</a>
 666 <a class="sourceLine" id="cb29-3" data-line-number="3">    -t org.unix \</a>
 667 <a class="sourceLine" id="cb29-4" data-line-number="4">    /usr/include/readline/readline.h \</a>
 668 <a class="sourceLine" id="cb29-5" data-line-number="5">    --exclude-symbol readline_echoing_p -o readline.jar</a></code></pre></div>
 669 <h3 id="java-code-that-uses-readline">Java code that uses readline</h3>
 670 <div class="sourceCode" id="cb30"><pre class="sourceCode java"><code class="sourceCode java"><a class="sourceLine" id="cb30-1" data-line-number="1"></a>
 671 <a class="sourceLine" id="cb30-2" data-line-number="2"><span class="kw">import</span><span class="im"> java.foreign.*;</span></a>
 672 <a class="sourceLine" id="cb30-3" data-line-number="3"><span class="kw">import</span><span class="im"> java.foreign.memory.*;</span></a>
 673 <a class="sourceLine" id="cb30-4" data-line-number="4"><span class="kw">import static</span><span class="im"> org.unix.readline_h.*;</span></a>
 674 <a class="sourceLine" id="cb30-5" data-line-number="5"></a>
 675 <a class="sourceLine" id="cb30-6" data-line-number="6"><span class="kw">public</span> <span class="kw">class</span> Readline {</a>
 676 <a class="sourceLine" id="cb30-7" data-line-number="7">    <span class="kw">public</span> <span class="dt">static</span> <span class="dt">void</span> <span class="fu">main</span>(<span class="bu">String</span>[] args) {</a>
 677 <a class="sourceLine" id="cb30-8" data-line-number="8">        <span class="co">// Scope for native allocations</span></a>
 678 <a class="sourceLine" id="cb30-9" data-line-number="9">        <span class="kw">try</span> (Scope s = <span class="fu">scope</span>().<span class="fu">fork</span>()) {</a>
 679 <a class="sourceLine" id="cb30-10" data-line-number="10">            <span class="co">// allocate C memory initialized with Java string content</span></a>
 680 <a class="sourceLine" id="cb30-11" data-line-number="11">            var pstr = s.<span class="fu">allocateCString</span>(<span class="st">&quot;name? &quot;</span>);</a>
 681 <a class="sourceLine" id="cb30-12" data-line-number="12"></a>
 682 <a class="sourceLine" id="cb30-13" data-line-number="13">            <span class="co">// call &quot;readline&quot; API</span></a>
 683 <a class="sourceLine" id="cb30-14" data-line-number="14">            var p = <span class="fu">readline</span>(pstr);</a>
 684 <a class="sourceLine" id="cb30-15" data-line-number="15"></a>
 685 <a class="sourceLine" id="cb30-16" data-line-number="16">            <span class="co">// print char* as is</span></a>
 686 <a class="sourceLine" id="cb30-17" data-line-number="17">            <span class="bu">System</span>.<span class="fu">out</span>.<span class="fu">println</span>(p);</a>
 687 <a class="sourceLine" id="cb30-18" data-line-number="18">            <span class="co">// convert char* ptr from readline as Java String &amp; print it</span></a>
 688 <a class="sourceLine" id="cb30-19" data-line-number="19">            <span class="bu">System</span>.<span class="fu">out</span>.<span class="fu">println</span>(Pointer.<span class="fu">toString</span>(p));</a>
 689 <a class="sourceLine" id="cb30-20" data-line-number="20">        }</a>
 690 <a class="sourceLine" id="cb30-21" data-line-number="21">    }</a>
 691 <a class="sourceLine" id="cb30-22" data-line-number="22">}</a></code></pre></div>
 692 <h3 id="running-the-java-code-that-uses-readline">Running the java code that uses readline</h3>
 693 <pre><code>
 694 javac -cp readline.jar Readline.java
 695 
 696 java -cp readline.jar:. Readline
 697 </code></pre>
 698 <h2 id="using-libcurl-from-java-mac-os">Using libcurl from Java (Mac OS)</h2>
 699 <h3 id="jextract-a-jar-for-curl.h">jextract a jar for curl.h</h3>
 700 <div class="sourceCode" id="cb32"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb32-1" data-line-number="1"></a>
 701 <a class="sourceLine" id="cb32-2" data-line-number="2"><span class="ex">jextract</span> -t org.unix -L /usr/lib -lcurl --record-library-path /usr/include/curl/curl.h -o curl.jar</a></code></pre></div>
 702 <h3 id="java-code-that-uses-libcurl">Java code that uses libcurl</h3>
 703 <div class="sourceCode" id="cb33"><pre class="sourceCode java"><code class="sourceCode java"><a class="sourceLine" id="cb33-1" data-line-number="1"></a>
 704 <a class="sourceLine" id="cb33-2" data-line-number="2"><span class="kw">import</span><span class="im"> java.lang.invoke.*;</span></a>
 705 <a class="sourceLine" id="cb33-3" data-line-number="3"><span class="kw">import</span><span class="im"> java.foreign.*;</span></a>
 706 <a class="sourceLine" id="cb33-4" data-line-number="4"><span class="kw">import</span><span class="im"> java.foreign.memory.*;</span></a>
 707 <a class="sourceLine" id="cb33-5" data-line-number="5"><span class="kw">import</span><span class="im"> org.unix.curl.*;</span></a>
 708 <a class="sourceLine" id="cb33-6" data-line-number="6"><span class="kw">import</span><span class="im"> org.unix.curl_h;</span></a>
 709 <a class="sourceLine" id="cb33-7" data-line-number="7"><span class="kw">import static</span><span class="im"> org.unix.curl_h.*;</span></a>
 710 <a class="sourceLine" id="cb33-8" data-line-number="8"><span class="kw">import static</span><span class="im"> org.unix.easy_h.*;</span></a>
 711 <a class="sourceLine" id="cb33-9" data-line-number="9"></a>
 712 <a class="sourceLine" id="cb33-10" data-line-number="10"><span class="kw">public</span> <span class="kw">class</span> CurlMain {</a>
 713 <a class="sourceLine" id="cb33-11" data-line-number="11">   <span class="kw">public</span> <span class="dt">static</span> <span class="dt">void</span> <span class="fu">main</span>(<span class="bu">String</span>[] args) {</a>
 714 <a class="sourceLine" id="cb33-12" data-line-number="12">       <span class="kw">try</span> (Scope s = curl_h.<span class="fu">scope</span>().<span class="fu">fork</span>()) { </a>
 715 <a class="sourceLine" id="cb33-13" data-line-number="13">           <span class="fu">curl_global_init</span>(CURL_GLOBAL_DEFAULT);</a>
 716 <a class="sourceLine" id="cb33-14" data-line-number="14">           Pointer&lt;<span class="bu">Void</span>&gt; curl = <span class="fu">curl_easy_init</span>();</a>
 717 <a class="sourceLine" id="cb33-15" data-line-number="15">           <span class="kw">if</span>(!curl.<span class="fu">isNull</span>()) {</a>
 718 <a class="sourceLine" id="cb33-16" data-line-number="16">               Pointer&lt;<span class="bu">Byte</span>&gt; url = s.<span class="fu">allocateCString</span>(args[<span class="dv">0</span>]);</a>
 719 <a class="sourceLine" id="cb33-17" data-line-number="17">               <span class="fu">curl_easy_setopt</span>(curl, CURLOPT_URL, url);</a>
 720 <a class="sourceLine" id="cb33-18" data-line-number="18">               <span class="dt">int</span> res = <span class="fu">curl_easy_perform</span>(curl);</a>
 721 <a class="sourceLine" id="cb33-19" data-line-number="19">               <span class="kw">if</span> (res != CURLE_OK) {</a>
 722 <a class="sourceLine" id="cb33-20" data-line-number="20">                   <span class="fu">curl_easy_cleanup</span>(curl);</a>
 723 <a class="sourceLine" id="cb33-21" data-line-number="21">               }</a>
 724 <a class="sourceLine" id="cb33-22" data-line-number="22">           }</a>
 725 <a class="sourceLine" id="cb33-23" data-line-number="23">           <span class="fu">curl_global_cleanup</span>();</a>
 726 <a class="sourceLine" id="cb33-24" data-line-number="24">       }</a>
 727 <a class="sourceLine" id="cb33-25" data-line-number="25">    }</a>
 728 <a class="sourceLine" id="cb33-26" data-line-number="26">}</a></code></pre></div>
 729 <h3 id="running-the-java-code-that-uses-libcurl">Running the java code that uses libcurl</h3>
 730 <div class="sourceCode" id="cb34"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb34-1" data-line-number="1"></a>
 731 <a class="sourceLine" id="cb34-2" data-line-number="2"><span class="ex">javac</span> -cp curl.jar CurlMain.java</a>
 732 <a class="sourceLine" id="cb34-3" data-line-number="3"><span class="ex">java</span> -cp curl.jar:. CurlMain <span class="op">&lt;</span>url<span class="op">&gt;</span></a></code></pre></div>
 733 <h2 id="using-unistd.h-from-java-code-linux">Using unistd.h from Java code (Linux)</h2>
 734 <h3 id="jextract-a-jar-file-for-unistd.h">jextract a jar file for unistd.h</h3>
 735 <div class="sourceCode" id="cb35"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb35-1" data-line-number="1"></a>
 736 <a class="sourceLine" id="cb35-2" data-line-number="2"><span class="ex">jextract</span> /usr/include/unistd.h -t org.unix -o unistd.jar</a></code></pre></div>
 737 <h3 id="java-code-that-calls-getpid">Java code that calls getpid</h3>
 738 <div class="sourceCode" id="cb36"><pre class="sourceCode java"><code class="sourceCode java"><a class="sourceLine" id="cb36-1" data-line-number="1"></a>
 739 <a class="sourceLine" id="cb36-2" data-line-number="2"><span class="kw">import</span><span class="im"> java.foreign.*;</span></a>
 740 <a class="sourceLine" id="cb36-3" data-line-number="3"><span class="kw">import</span><span class="im"> java.lang.invoke.*;</span></a>
 741 <a class="sourceLine" id="cb36-4" data-line-number="4"><span class="kw">import</span><span class="im"> org.unix.unistd;</span></a>
 742 <a class="sourceLine" id="cb36-5" data-line-number="5"></a>
 743 <a class="sourceLine" id="cb36-6" data-line-number="6"></a>
 744 <a class="sourceLine" id="cb36-7" data-line-number="7"><span class="kw">public</span> <span class="kw">class</span> Getpid {</a>
 745 <a class="sourceLine" id="cb36-8" data-line-number="8">    <span class="kw">public</span> <span class="dt">static</span> <span class="dt">void</span> <span class="fu">main</span>(<span class="bu">String</span>[] args) {</a>
 746 <a class="sourceLine" id="cb36-9" data-line-number="9">        <span class="co">// bind unistd interface</span></a>
 747 <a class="sourceLine" id="cb36-10" data-line-number="10">        var u = Libraries.<span class="fu">bind</span>(MethodHandles.<span class="fu">lookup</span>(), unistd.<span class="fu">class</span>);</a>
 748 <a class="sourceLine" id="cb36-11" data-line-number="11">        <span class="co">// call getpid from the unistd.h</span></a>
 749 <a class="sourceLine" id="cb36-12" data-line-number="12">        <span class="bu">System</span>.<span class="fu">out</span>.<span class="fu">println</span>(u.<span class="fu">getpid</span>());</a>
 750 <a class="sourceLine" id="cb36-13" data-line-number="13">        <span class="co">// check process id from Java API!</span></a>
 751 <a class="sourceLine" id="cb36-14" data-line-number="14">        <span class="bu">System</span>.<span class="fu">out</span>.<span class="fu">println</span>(ProcessHandle.<span class="fu">current</span>().<span class="fu">pid</span>());</a>
 752 <a class="sourceLine" id="cb36-15" data-line-number="15">    }</a>
 753 <a class="sourceLine" id="cb36-16" data-line-number="16">}</a></code></pre></div>
 754 <h3 id="running-the-java-code-that-uses-getpid">Running the Java code that uses getpid</h3>
 755 <div class="sourceCode" id="cb37"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb37-1" data-line-number="1"></a>
 756 <a class="sourceLine" id="cb37-2" data-line-number="2"><span class="ex">javac</span> -cp unistd.jar Getpid.java</a>
 757 <a class="sourceLine" id="cb37-3" data-line-number="3"></a>
 758 <a class="sourceLine" id="cb37-4" data-line-number="4"><span class="ex">java</span> -cp unistd.jar:. Getpid</a></code></pre></div>
 759 <h2 id="using-opengl-graphic-library-ubuntu-16.04">Using OpenGL graphic library (Ubuntu 16.04)</h2>
 760 <p>OpenGL is a popular portable graphic library: <a href="https://www.opengl.org/" class="uri">https://www.opengl.org/</a></p>
 761 <h3 id="installing-opengl-ubuntu-16.04">Installing OpenGL (Ubuntu 16.04)</h3>
 762 <p>Installing relevant OpenGL headers and libraries can be a bit tricky, as it depends on what graphic card is installed on the target platform. The following instruction assume that the standard version of OpenGL is used (e.g. mesa), rather than a proprietary one (Nvidia or AMD), although the changes to get these working are rather small.</p>
 763 <p>OpenGL is always coupled with a bunch of other libraries, namely GLU and glut. You can install all those libraries using <code>apt</code>, as follows:</p>
 764 <div class="sourceCode" id="cb38"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb38-1" data-line-number="1"></a>
 765 <a class="sourceLine" id="cb38-2" data-line-number="2"><span class="fu">sudo</span> apt-get install libgl1-mesa-dev libglu1-mesa-dev freeglut3-dev</a></code></pre></div>
 766 <p>If the installation was successful, OpenGL headers can be found under <code>/usr/include/GL</code>, while libraries can be found in the folder <code>/usr/lib/x86_64-linux-gnu/</code>.</p>
 767 <h3 id="jextracting-opengl-ubuntu-16.04">jextracting OpenGL (Ubuntu 16.04)</h3>
 768 <p>To extract the opengl libraries the following command suffices:</p>
 769 <div class="sourceCode" id="cb39"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb39-1" data-line-number="1"></a>
 770 <a class="sourceLine" id="cb39-2" data-line-number="2"><span class="ex">jextract</span> -L /usr/lib/x86_64-linux-gnu  -l glut -l GLU -l GL --record-library-path -t opengl -o opengl.jar /usr/include/GL/glut.h</a></code></pre></div>
 771 <p>Since glut depends on the other libraries (GLU and GL), it is not necessary to give additional headers to jextract.</p>
 772 <h3 id="java-sample-code-that-uses-the-opengl-library">Java sample code that uses the OpenGL library</h3>
 773 <div class="sourceCode" id="cb40"><pre class="sourceCode java"><code class="sourceCode java"><a class="sourceLine" id="cb40-1" data-line-number="1"><span class="kw">import</span><span class="im"> java.foreign.NativeTypes;</span></a>
 774 <a class="sourceLine" id="cb40-2" data-line-number="2"><span class="kw">import</span><span class="im"> java.foreign.Scope;</span></a>
 775 <a class="sourceLine" id="cb40-3" data-line-number="3"><span class="kw">import</span><span class="im"> java.foreign.memory.Array;</span></a>
 776 <a class="sourceLine" id="cb40-4" data-line-number="4"><span class="kw">import</span><span class="im"> java.foreign.memory.Pointer;</span></a>
 777 <a class="sourceLine" id="cb40-5" data-line-number="5"></a>
 778 <a class="sourceLine" id="cb40-6" data-line-number="6"><span class="kw">import static</span><span class="im"> opengl.gl_h.*;</span></a>
 779 <a class="sourceLine" id="cb40-7" data-line-number="7"><span class="kw">import static</span><span class="im"> opengl.freeglut_std_h.*;</span></a>
 780 <a class="sourceLine" id="cb40-8" data-line-number="8"></a>
 781 <a class="sourceLine" id="cb40-9" data-line-number="9"><span class="kw">public</span> <span class="kw">class</span> Teapot {</a>
 782 <a class="sourceLine" id="cb40-10" data-line-number="10"></a>
 783 <a class="sourceLine" id="cb40-11" data-line-number="11">    <span class="dt">float</span> rot = <span class="dv">0</span>;</a>
 784 <a class="sourceLine" id="cb40-12" data-line-number="12"></a>
 785 <a class="sourceLine" id="cb40-13" data-line-number="13">    <span class="fu">Teapot</span>(Scope sc) {</a>
 786 <a class="sourceLine" id="cb40-14" data-line-number="14">        <span class="co">// Misc Parameters</span></a>
 787 <a class="sourceLine" id="cb40-15" data-line-number="15">        <span class="bu">Array</span>&lt;<span class="bu">Float</span>&gt; pos = sc.<span class="fu">allocateArray</span>(NativeTypes.<span class="fu">FLOAT</span>, <span class="kw">new</span> <span class="dt">float</span>[] {<span class="fl">0.0f</span>, <span class="fl">15.0f</span>, <span class="fl">-15.0f</span>, <span class="dv">0</span>});</a>
 788 <a class="sourceLine" id="cb40-16" data-line-number="16">        <span class="bu">Array</span>&lt;<span class="bu">Float</span>&gt; spec = sc.<span class="fu">allocateArray</span>(NativeTypes.<span class="fu">FLOAT</span>, <span class="kw">new</span> <span class="dt">float</span>[] {<span class="dv">1</span>, <span class="dv">1</span>, <span class="dv">1</span>, <span class="dv">0</span>});</a>
 789 <a class="sourceLine" id="cb40-17" data-line-number="17">        <span class="bu">Array</span>&lt;<span class="bu">Float</span>&gt; shini = sc.<span class="fu">allocateArray</span>(NativeTypes.<span class="fu">FLOAT</span>, <span class="kw">new</span> <span class="dt">float</span>[] {<span class="dv">113</span>});</a>
 790 <a class="sourceLine" id="cb40-18" data-line-number="18"></a>
 791 <a class="sourceLine" id="cb40-19" data-line-number="19">        <span class="co">// Reset Background</span></a>
 792 <a class="sourceLine" id="cb40-20" data-line-number="20">        <span class="fu">glClearColor</span>(<span class="dv">0</span>, <span class="dv">0</span>, <span class="dv">0</span>, <span class="dv">0</span>);</a>
 793 <a class="sourceLine" id="cb40-21" data-line-number="21"></a>
 794 <a class="sourceLine" id="cb40-22" data-line-number="22">        <span class="co">// Setup Lighting</span></a>
 795 <a class="sourceLine" id="cb40-23" data-line-number="23">        <span class="fu">glShadeModel</span>(GL_SMOOTH);</a>
 796 <a class="sourceLine" id="cb40-24" data-line-number="24">        <span class="fu">glLightfv</span>(GL_LIGHT0, GL_POSITION, pos.<span class="fu">elementPointer</span>());</a>
 797 <a class="sourceLine" id="cb40-25" data-line-number="25">        <span class="fu">glLightfv</span>(GL_LIGHT0, GL_AMBIENT, spec.<span class="fu">elementPointer</span>());</a>
 798 <a class="sourceLine" id="cb40-26" data-line-number="26">        <span class="fu">glLightfv</span>(GL_LIGHT0, GL_DIFFUSE, spec.<span class="fu">elementPointer</span>());</a>
 799 <a class="sourceLine" id="cb40-27" data-line-number="27">        <span class="fu">glLightfv</span>(GL_LIGHT0, GL_SPECULAR, spec.<span class="fu">elementPointer</span>());</a>
 800 <a class="sourceLine" id="cb40-28" data-line-number="28">        <span class="fu">glMaterialfv</span>(GL_FRONT, GL_SHININESS, shini.<span class="fu">elementPointer</span>());</a>
 801 <a class="sourceLine" id="cb40-29" data-line-number="29">        <span class="fu">glEnable</span>(GL_LIGHTING);</a>
 802 <a class="sourceLine" id="cb40-30" data-line-number="30">        <span class="fu">glEnable</span>(GL_LIGHT0);</a>
 803 <a class="sourceLine" id="cb40-31" data-line-number="31">        <span class="fu">glEnable</span>(GL_DEPTH_TEST);</a>
 804 <a class="sourceLine" id="cb40-32" data-line-number="32">    }</a>
 805 <a class="sourceLine" id="cb40-33" data-line-number="33"></a>
 806 <a class="sourceLine" id="cb40-34" data-line-number="34">    <span class="dt">void</span> <span class="fu">display</span>() {</a>
 807 <a class="sourceLine" id="cb40-35" data-line-number="35">        <span class="fu">glClear</span>(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);</a>
 808 <a class="sourceLine" id="cb40-36" data-line-number="36">        <span class="fu">glPushMatrix</span>();</a>
 809 <a class="sourceLine" id="cb40-37" data-line-number="37">        <span class="fu">glRotatef</span>(-<span class="dv">20</span>, <span class="dv">1</span>, <span class="dv">1</span>, <span class="dv">0</span>);</a>
 810 <a class="sourceLine" id="cb40-38" data-line-number="38">        <span class="fu">glRotatef</span>(rot, <span class="dv">0</span>, <span class="dv">1</span>, <span class="dv">0</span>);</a>
 811 <a class="sourceLine" id="cb40-39" data-line-number="39">        <span class="fu">glutSolidTeapot</span>(<span class="fl">0.5</span>);</a>
 812 <a class="sourceLine" id="cb40-40" data-line-number="40">        <span class="fu">glPopMatrix</span>();</a>
 813 <a class="sourceLine" id="cb40-41" data-line-number="41">        <span class="fu">glutSwapBuffers</span>();</a>
 814 <a class="sourceLine" id="cb40-42" data-line-number="42">    }</a>
 815 <a class="sourceLine" id="cb40-43" data-line-number="43"></a>
 816 <a class="sourceLine" id="cb40-44" data-line-number="44">    <span class="dt">void</span> <span class="fu">onIdle</span>() {</a>
 817 <a class="sourceLine" id="cb40-45" data-line-number="45">        rot += <span class="fl">0.1</span>;</a>
 818 <a class="sourceLine" id="cb40-46" data-line-number="46">        <span class="fu">glutPostRedisplay</span>();</a>
 819 <a class="sourceLine" id="cb40-47" data-line-number="47">    }</a>
 820 <a class="sourceLine" id="cb40-48" data-line-number="48"></a>
 821 <a class="sourceLine" id="cb40-49" data-line-number="49">    <span class="kw">public</span> <span class="dt">static</span> <span class="dt">void</span> <span class="fu">main</span>(<span class="bu">String</span>[] args) {</a>
 822 <a class="sourceLine" id="cb40-50" data-line-number="50">        <span class="kw">try</span> (Scope sc = opengl.<span class="fu">gl_h</span>.<span class="fu">scope</span>().<span class="fu">fork</span>()) {</a>
 823 <a class="sourceLine" id="cb40-51" data-line-number="51">            Pointer&lt;<span class="bu">Integer</span>&gt; argc = sc.<span class="fu">allocate</span>(NativeTypes.<span class="fu">INT32</span>);</a>
 824 <a class="sourceLine" id="cb40-52" data-line-number="52">            argc.<span class="fu">set</span>(<span class="dv">0</span>);</a>
 825 <a class="sourceLine" id="cb40-53" data-line-number="53">            <span class="fu">glutInit</span>(argc, Pointer.<span class="fu">ofNull</span>());</a>
 826 <a class="sourceLine" id="cb40-54" data-line-number="54">            <span class="fu">glutInitDisplayMode</span>(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);</a>
 827 <a class="sourceLine" id="cb40-55" data-line-number="55">            <span class="fu">glutInitWindowSize</span>(<span class="dv">900</span>, <span class="dv">900</span>);</a>
 828 <a class="sourceLine" id="cb40-56" data-line-number="56">            <span class="fu">glutCreateWindow</span>(sc.<span class="fu">allocateCString</span>(<span class="st">&quot;Hello Panama!&quot;</span>));</a>
 829 <a class="sourceLine" id="cb40-57" data-line-number="57">            Teapot teapot = <span class="kw">new</span> <span class="fu">Teapot</span>(sc);</a>
 830 <a class="sourceLine" id="cb40-58" data-line-number="58">            <span class="fu">glutDisplayFunc</span>(sc.<span class="fu">allocateCallback</span>(teapot::display));</a>
 831 <a class="sourceLine" id="cb40-59" data-line-number="59">            <span class="fu">glutIdleFunc</span>(sc.<span class="fu">allocateCallback</span>(teapot::onIdle));</a>
 832 <a class="sourceLine" id="cb40-60" data-line-number="60">            <span class="fu">glutMainLoop</span>();</a>
 833 <a class="sourceLine" id="cb40-61" data-line-number="61">        }</a>
 834 <a class="sourceLine" id="cb40-62" data-line-number="62">    }</a>
 835 <a class="sourceLine" id="cb40-63" data-line-number="63">}</a></code></pre></div>
 836 <h3 id="running-the-java-code-that-uses-opengl-ubuntu-16.04">Running the Java code that uses OpenGL (Ubuntu 16.04)</h3>
 837 <div class="sourceCode" id="cb41"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb41-1" data-line-number="1"></a>
 838 <a class="sourceLine" id="cb41-2" data-line-number="2"><span class="ex">javac</span> -cp opengl.jar Teapot.java</a>
 839 <a class="sourceLine" id="cb41-3" data-line-number="3"></a>
 840 <a class="sourceLine" id="cb41-4" data-line-number="4"><span class="ex">java</span> -cp opengl.jar:. Teapot</a></code></pre></div>
 841 <h2 id="using-opengl-graphic-library-windows">Using OpenGL graphic library (Windows)</h2>
 842 <h3 id="installing-opengl">Installing OpenGL</h3>
 843 <p>Download the freeglut package for MSVC at <a href="https://www.transmissionzero.co.uk/software/freeglut-devel/" class="uri">https://www.transmissionzero.co.uk/software/freeglut-devel/</a></p>
 844 <p>Extract the freeglut zip.</p>
 845 <h3 id="jextracting-opengl">jextracting OpenGL</h3>
 846 <p>Navigate to the root directory of the extracted zip and run the following commands:</p>
 847 <div class="sourceCode" id="cb42"><pre class="sourceCode powershell"><code class="sourceCode powershell"><a class="sourceLine" id="cb42-1" data-line-number="1"><span class="va">$inc</span> = <span class="st">&quot;C:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0&quot;</span></a>
 848 <a class="sourceLine" id="cb42-2" data-line-number="2">jextract -L C:\Windows\System32\ -L .\freeglut\bin\x64\ -l opengl32 -l freeglut -t opengl -o opengl.<span class="fu">jar</span> --package-map <span class="st">&quot;$inc\um\gl=opengl&quot;</span> --record-library-path .\freeglut\include\<span class="fu">GL</span>\glut.<span class="fu">h</span></a></code></pre></div>
 849 <p>The directory that is assigned to <code>$inc</code> is an example, and is system dependent. Make sure that the build number at the end of the path (in this case <code>10.0.17134.0</code>) is the latest one found in the parent folder (<code>C:\Program Files (x86)\Windows Kits\10\Include\</code>).</p>
 850 <p>There are a bunch of warnings generated, but as long as the jar file is generated in the working directory the extraction was successful.</p>
 851 <h3 id="java-sample-code-that-uses-the-opengl-library-1">Java sample code that uses the OpenGL library</h3>
 852 <p>This is the same as in the Ubuntu section</p>
 853 <h3 id="running-the-java-code-that-uses-opengl">Running the Java code that uses OpenGL</h3>
 854 <div class="sourceCode" id="cb43"><pre class="sourceCode powershell"><code class="sourceCode powershell"><a class="sourceLine" id="cb43-1" data-line-number="1">javac -cp .\opengl.<span class="fu">jar</span> Teapot.<span class="fu">java</span></a>
 855 <a class="sourceLine" id="cb43-2" data-line-number="2">java -cp <span class="st">&quot;opengl.jar;.&quot;</span> Teapot</a></code></pre></div>
 856 <h2 id="using-tensorflow-c-api-mac-os">Using TensorFlow C API (Mac OS)</h2>
 857 <p>Quoted from <a href="https://www.tensorflow.org/install/lang_c" class="uri">https://www.tensorflow.org/install/lang_c</a></p>
 858 <p>&quot;TensorFlow provides a C API that can be used to build bindings for other languages. The API is defined in c_api.h and designed for simplicity and uniformity rather than convenience.&quot;</p>
 859 <h3 id="installing-libtensorflow">Installing libtensorflow</h3>
 860 <p>You can follow the setup procedure as described in the above page.</p>
 861 <p>Alternatively, on Mac, you can install libtensorflow using HomeBrew</p>
 862 <div class="sourceCode" id="cb44"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb44-1" data-line-number="1"></a>
 863 <a class="sourceLine" id="cb44-2" data-line-number="2"><span class="ex">brew</span> install libtensorflow</a></code></pre></div>
 864 <p>Tensorflow ship the libtensorflow with an .so extension, this doesn't work well for java on MacOS as java expect .dylib extension. To work around this, create a symbolic link.</p>
 865 <div class="sourceCode" id="cb45"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb45-1" data-line-number="1"></a>
 866 <a class="sourceLine" id="cb45-2" data-line-number="2"><span class="fu">sudo</span> ln -s /usr/local/lib/libtensorflow.so /usr/local/lib/libtensorflow.dylib</a></code></pre></div>
 867 <h3 id="jextracting-libtensorflow-c_api.h">jextracting libtensorflow c_api.h</h3>
 868 <p>The following command can be used to extract c_api.h.</p>
 869 <div class="sourceCode" id="cb46"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb46-1" data-line-number="1"></a>
 870 <a class="sourceLine" id="cb46-2" data-line-number="2"><span class="ex">jextract</span> -C -x -C c++  \</a>
 871 <a class="sourceLine" id="cb46-3" data-line-number="3">        -L /usr/local/lib -l tensorflow --record-library-path \</a>
 872 <a class="sourceLine" id="cb46-4" data-line-number="4">        -o tf.jar -t org.tensorflow.panama \</a>
 873 <a class="sourceLine" id="cb46-5" data-line-number="5">        /usr/local/include/tensorflow/c/c_api.h</a></code></pre></div>
 874 <p>The caveat to extract tensorflow C API is that it declare function prototype without argument in C++ style, for example, TF_Version(), which is considered incomplete C function prototype instead of C style as in TF_Version(void). An incomplete function prototype will become vararg funciton. To avoid that, we need to pass clang '-x c++' options to jextract with '-C -x -C c++'</p>
 875 <h3 id="java-sample-code-that-uses-tensorflow-library">Java sample code that uses tensorflow library</h3>
 876 <div class="sourceCode" id="cb47"><pre class="sourceCode java"><code class="sourceCode java"><a class="sourceLine" id="cb47-1" data-line-number="1"></a>
 877 <a class="sourceLine" id="cb47-2" data-line-number="2"><span class="kw">import</span><span class="im"> java.foreign.NativeTypes;</span></a>
 878 <a class="sourceLine" id="cb47-3" data-line-number="3"><span class="kw">import</span><span class="im"> java.foreign.Scope;</span></a>
 879 <a class="sourceLine" id="cb47-4" data-line-number="4"><span class="kw">import</span><span class="im"> java.foreign.memory.Array;</span></a>
 880 <a class="sourceLine" id="cb47-5" data-line-number="5"><span class="kw">import</span><span class="im"> java.foreign.memory.LayoutType;</span></a>
 881 <a class="sourceLine" id="cb47-6" data-line-number="6"><span class="kw">import</span><span class="im"> java.foreign.memory.Pointer;</span></a>
 882 <a class="sourceLine" id="cb47-7" data-line-number="7"><span class="kw">import</span><span class="im"> org.tensorflow.panama.c_api.TF_DataType;</span></a>
 883 <a class="sourceLine" id="cb47-8" data-line-number="8"><span class="kw">import</span><span class="im"> org.tensorflow.panama.c_api.TF_Graph;</span></a>
 884 <a class="sourceLine" id="cb47-9" data-line-number="9"><span class="kw">import</span><span class="im"> org.tensorflow.panama.c_api.TF_Operation;</span></a>
 885 <a class="sourceLine" id="cb47-10" data-line-number="10"><span class="kw">import</span><span class="im"> org.tensorflow.panama.c_api.TF_OperationDescription;</span></a>
 886 <a class="sourceLine" id="cb47-11" data-line-number="11"><span class="kw">import</span><span class="im"> org.tensorflow.panama.c_api.TF_Output;</span></a>
 887 <a class="sourceLine" id="cb47-12" data-line-number="12"><span class="kw">import</span><span class="im"> org.tensorflow.panama.c_api.TF_Session;</span></a>
 888 <a class="sourceLine" id="cb47-13" data-line-number="13"><span class="kw">import</span><span class="im"> org.tensorflow.panama.c_api.TF_SessionOptions;</span></a>
 889 <a class="sourceLine" id="cb47-14" data-line-number="14"><span class="kw">import</span><span class="im"> org.tensorflow.panama.c_api.TF_Status;</span></a>
 890 <a class="sourceLine" id="cb47-15" data-line-number="15"><span class="kw">import</span><span class="im"> org.tensorflow.panama.c_api.TF_Tensor;</span></a>
 891 <a class="sourceLine" id="cb47-16" data-line-number="16"></a>
 892 <a class="sourceLine" id="cb47-17" data-line-number="17"><span class="kw">import static</span><span class="im"> org.tensorflow.panama.c_api_h.*;</span></a>
 893 <a class="sourceLine" id="cb47-18" data-line-number="18"><span class="kw">import static</span><span class="im"> org.tensorflow.panama.c_api_h.TF_DataType.*;</span></a>
 894 <a class="sourceLine" id="cb47-19" data-line-number="19"></a>
 895 <a class="sourceLine" id="cb47-20" data-line-number="20"><span class="kw">public</span> <span class="kw">class</span> TensorFlowExample {</a>
 896 <a class="sourceLine" id="cb47-21" data-line-number="21">    <span class="dt">static</span> Pointer&lt;TF_Operation&gt; <span class="fu">PlaceHolder</span>(Pointer&lt;TF_Graph&gt; graph, Pointer&lt;TF_Status&gt; status,</a>
 897 <a class="sourceLine" id="cb47-22" data-line-number="22">                                      <span class="at">@TF_DataType</span> <span class="dt">int</span> dtype, <span class="bu">String</span> name) {</a>
 898 <a class="sourceLine" id="cb47-23" data-line-number="23">        <span class="kw">try</span> (var s = <span class="fu">scope</span>().<span class="fu">fork</span>()) {</a>
 899 <a class="sourceLine" id="cb47-24" data-line-number="24">            Pointer&lt;TF_OperationDescription&gt; desc = <span class="fu">TF_NewOperation</span>(graph,</a>
 900 <a class="sourceLine" id="cb47-25" data-line-number="25">                    s.<span class="fu">allocateCString</span>(<span class="st">&quot;Placeholder&quot;</span>), s.<span class="fu">allocateCString</span>(name));</a>
 901 <a class="sourceLine" id="cb47-26" data-line-number="26">            <span class="fu">TF_SetAttrType</span>(desc, s.<span class="fu">allocateCString</span>(<span class="st">&quot;dtype&quot;</span>), TF_FLOAT);</a>
 902 <a class="sourceLine" id="cb47-27" data-line-number="27">            <span class="kw">return</span> <span class="fu">TF_FinishOperation</span>(desc, status);</a>
 903 <a class="sourceLine" id="cb47-28" data-line-number="28">        }</a>
 904 <a class="sourceLine" id="cb47-29" data-line-number="29">    }</a>
 905 <a class="sourceLine" id="cb47-30" data-line-number="30"></a>
 906 <a class="sourceLine" id="cb47-31" data-line-number="31">    <span class="dt">static</span> Pointer&lt;TF_Operation&gt; <span class="fu">ConstValue</span>(Pointer&lt;TF_Graph&gt; graph, Pointer&lt;TF_Status&gt; status,</a>
 907 <a class="sourceLine" id="cb47-32" data-line-number="32">                                Pointer&lt;TF_Tensor&gt; tensor, <span class="bu">String</span> name) {</a>
 908 <a class="sourceLine" id="cb47-33" data-line-number="33">        <span class="kw">try</span> (var s = <span class="fu">scope</span>().<span class="fu">fork</span>()) {</a>
 909 <a class="sourceLine" id="cb47-34" data-line-number="34">            Pointer&lt;TF_OperationDescription&gt; desc = <span class="fu">TF_NewOperation</span>(graph,</a>
 910 <a class="sourceLine" id="cb47-35" data-line-number="35">                    s.<span class="fu">allocateCString</span>(<span class="st">&quot;Const&quot;</span>), s.<span class="fu">allocateCString</span>(name));</a>
 911 <a class="sourceLine" id="cb47-36" data-line-number="36">            <span class="fu">TF_SetAttrTensor</span>(desc, s.<span class="fu">allocateCString</span>(<span class="st">&quot;value&quot;</span>), tensor, status);</a>
 912 <a class="sourceLine" id="cb47-37" data-line-number="37">            <span class="fu">TF_SetAttrType</span>(desc, s.<span class="fu">allocateCString</span>(<span class="st">&quot;dtype&quot;</span>), <span class="fu">TF_TensorType</span>(tensor));</a>
 913 <a class="sourceLine" id="cb47-38" data-line-number="38">            <span class="kw">return</span> <span class="fu">TF_FinishOperation</span>(desc, status);</a>
 914 <a class="sourceLine" id="cb47-39" data-line-number="39">        }</a>
 915 <a class="sourceLine" id="cb47-40" data-line-number="40">    }</a>
 916 <a class="sourceLine" id="cb47-41" data-line-number="41"></a>
 917 <a class="sourceLine" id="cb47-42" data-line-number="42">    <span class="dt">static</span> Pointer&lt;TF_Operation&gt; <span class="fu">Add</span>(Pointer&lt;TF_Graph&gt; graph, Pointer&lt;TF_Status&gt; status,</a>
 918 <a class="sourceLine" id="cb47-43" data-line-number="43">                              Pointer&lt;TF_Operation&gt; one, Pointer&lt;TF_Operation&gt; two,</a>
 919 <a class="sourceLine" id="cb47-44" data-line-number="44">                              <span class="bu">String</span> name) {</a>
 920 <a class="sourceLine" id="cb47-45" data-line-number="45">        <span class="kw">try</span> (var s = <span class="fu">scope</span>().<span class="fu">fork</span>()) {</a>
 921 <a class="sourceLine" id="cb47-46" data-line-number="46">            Pointer&lt;TF_OperationDescription&gt; desc = <span class="fu">TF_NewOperation</span>(graph,</a>
 922 <a class="sourceLine" id="cb47-47" data-line-number="47">                    s.<span class="fu">allocateCString</span>(<span class="st">&quot;AddN&quot;</span>), s.<span class="fu">allocateCString</span>(name));</a>
 923 <a class="sourceLine" id="cb47-48" data-line-number="48">            <span class="bu">Array</span>&lt;TF_Output&gt; add_inputs = s.<span class="fu">allocateArray</span>(</a>
 924 <a class="sourceLine" id="cb47-49" data-line-number="49">                    LayoutType.<span class="fu">ofStruct</span>(TF_Output.<span class="fu">class</span>),<span class="dv">2</span>);</a>
 925 <a class="sourceLine" id="cb47-50" data-line-number="50">            add_inputs.<span class="fu">get</span>(<span class="dv">0</span>).<span class="fu">oper</span>$<span class="fu">set</span>(one);</a>
 926 <a class="sourceLine" id="cb47-51" data-line-number="51">            add_inputs.<span class="fu">get</span>(<span class="dv">0</span>).<span class="fu">index</span>$<span class="fu">set</span>(<span class="dv">0</span>);</a>
 927 <a class="sourceLine" id="cb47-52" data-line-number="52">            add_inputs.<span class="fu">get</span>(<span class="dv">1</span>).<span class="fu">oper</span>$<span class="fu">set</span>(two);</a>
 928 <a class="sourceLine" id="cb47-53" data-line-number="53">            add_inputs.<span class="fu">get</span>(<span class="dv">1</span>).<span class="fu">index</span>$<span class="fu">set</span>(<span class="dv">0</span>);</a>
 929 <a class="sourceLine" id="cb47-54" data-line-number="54">            <span class="fu">TF_AddInputList</span>(desc, add_inputs.<span class="fu">elementPointer</span>(), <span class="dv">2</span>);</a>
 930 <a class="sourceLine" id="cb47-55" data-line-number="55">            <span class="kw">return</span> <span class="fu">TF_FinishOperation</span>(desc, status);</a>
 931 <a class="sourceLine" id="cb47-56" data-line-number="56">        }</a>
 932 <a class="sourceLine" id="cb47-57" data-line-number="57">    }</a>
 933 <a class="sourceLine" id="cb47-58" data-line-number="58"></a>
 934 <a class="sourceLine" id="cb47-59" data-line-number="59">    <span class="kw">public</span> <span class="dt">static</span> <span class="dt">void</span> <span class="fu">main</span>(<span class="bu">String</span><span class="kw">... </span>args) {</a>
 935 <a class="sourceLine" id="cb47-60" data-line-number="60">        <span class="bu">System</span>.<span class="fu">out</span>.<span class="fu">println</span>(<span class="st">&quot;TensorFlow C library version: &quot;</span> + Pointer.<span class="fu">toString</span>(<span class="fu">TF_Version</span>()));</a>
 936 <a class="sourceLine" id="cb47-61" data-line-number="61"></a>
 937 <a class="sourceLine" id="cb47-62" data-line-number="62">        Pointer&lt;TF_Graph&gt; graph = <span class="fu">TF_NewGraph</span>();</a>
 938 <a class="sourceLine" id="cb47-63" data-line-number="63">        Pointer&lt;TF_SessionOptions&gt; options = <span class="fu">TF_NewSessionOptions</span>();</a>
 939 <a class="sourceLine" id="cb47-64" data-line-number="64">        Pointer&lt;TF_Status&gt; status = <span class="fu">TF_NewStatus</span>();</a>
 940 <a class="sourceLine" id="cb47-65" data-line-number="65">        Pointer&lt;TF_Session&gt; session = <span class="fu">TF_NewSession</span>(graph, options, status);</a>
 941 <a class="sourceLine" id="cb47-66" data-line-number="66"></a>
 942 <a class="sourceLine" id="cb47-67" data-line-number="67">        <span class="dt">float</span> in_val_one = <span class="fl">4.0f</span>;</a>
 943 <a class="sourceLine" id="cb47-68" data-line-number="68">        <span class="dt">float</span> const_two = <span class="fl">2.0f</span>;</a>
 944 <a class="sourceLine" id="cb47-69" data-line-number="69"></a>
 945 <a class="sourceLine" id="cb47-70" data-line-number="70">        Pointer&lt;TF_Tensor&gt; tensor_in = <span class="fu">TF_AllocateTensor</span>(TF_FLOAT, Pointer.<span class="fu">ofNull</span>(), <span class="dv">0</span>, <span class="bu">Float</span>.<span class="fu">BYTES</span>);</a>
 946 <a class="sourceLine" id="cb47-71" data-line-number="71">        <span class="fu">TF_TensorData</span>(tensor_in).<span class="fu">cast</span>(NativeTypes.<span class="fu">FLOAT</span>).<span class="fu">set</span>(in_val_one);</a>
 947 <a class="sourceLine" id="cb47-72" data-line-number="72">        Pointer&lt;TF_Tensor&gt; tensor_const_two = <span class="fu">TF_AllocateTensor</span>(TF_FLOAT, Pointer.<span class="fu">ofNull</span>(), <span class="dv">0</span>, <span class="bu">Float</span>.<span class="fu">BYTES</span>);</a>
 948 <a class="sourceLine" id="cb47-73" data-line-number="73">        <span class="fu">TF_TensorData</span>(tensor_const_two).<span class="fu">cast</span>(NativeTypes.<span class="fu">FLOAT</span>).<span class="fu">set</span>(const_two);</a>
 949 <a class="sourceLine" id="cb47-74" data-line-number="74"></a>
 950 <a class="sourceLine" id="cb47-75" data-line-number="75">        <span class="co">// Operations</span></a>
 951 <a class="sourceLine" id="cb47-76" data-line-number="76">        Pointer&lt;TF_Operation&gt; feed = <span class="fu">PlaceHolder</span>(graph, status, TF_FLOAT, <span class="st">&quot;feed&quot;</span>);</a>
 952 <a class="sourceLine" id="cb47-77" data-line-number="77">        Pointer&lt;TF_Operation&gt; two = <span class="fu">ConstValue</span>(graph, status, tensor_const_two, <span class="st">&quot;const&quot;</span>);</a>
 953 <a class="sourceLine" id="cb47-78" data-line-number="78">        Pointer&lt;TF_Operation&gt; add = <span class="fu">Add</span>(graph, status, feed, two, <span class="st">&quot;add&quot;</span>);</a>
 954 <a class="sourceLine" id="cb47-79" data-line-number="79"></a>
 955 <a class="sourceLine" id="cb47-80" data-line-number="80"></a>
 956 <a class="sourceLine" id="cb47-81" data-line-number="81">        <span class="kw">try</span> (var s = <span class="fu">scope</span>().<span class="fu">fork</span>()) {</a>
 957 <a class="sourceLine" id="cb47-82" data-line-number="82">            var ltPtrTensor = LayoutType.<span class="fu">ofStruct</span>(TF_Tensor.<span class="fu">class</span>).<span class="fu">pointer</span>();</a>
 958 <a class="sourceLine" id="cb47-83" data-line-number="83"></a>
 959 <a class="sourceLine" id="cb47-84" data-line-number="84">            <span class="co">// Session Inputs</span></a>
 960 <a class="sourceLine" id="cb47-85" data-line-number="85">            TF_Output input_operations = s.<span class="fu">allocateStruct</span>(TF_Output.<span class="fu">class</span>);</a>
 961 <a class="sourceLine" id="cb47-86" data-line-number="86">            input_operations.<span class="fu">oper</span>$<span class="fu">set</span>(feed);</a>
 962 <a class="sourceLine" id="cb47-87" data-line-number="87">            input_operations.<span class="fu">index</span>$<span class="fu">set</span>(<span class="dv">0</span>);</a>
 963 <a class="sourceLine" id="cb47-88" data-line-number="88">            Pointer&lt;Pointer&lt;TF_Tensor&gt;&gt; input_tensors = s.<span class="fu">allocate</span>(ltPtrTensor);</a>
 964 <a class="sourceLine" id="cb47-89" data-line-number="89">            input_tensors.<span class="fu">set</span>(tensor_in);</a>
 965 <a class="sourceLine" id="cb47-90" data-line-number="90"></a>
 966 <a class="sourceLine" id="cb47-91" data-line-number="91">            <span class="co">// Session Outputs</span></a>
 967 <a class="sourceLine" id="cb47-92" data-line-number="92">            TF_Output output_operations = s.<span class="fu">allocateStruct</span>(TF_Output.<span class="fu">class</span>);</a>
 968 <a class="sourceLine" id="cb47-93" data-line-number="93">            output_operations.<span class="fu">oper</span>$<span class="fu">set</span>(add);</a>
 969 <a class="sourceLine" id="cb47-94" data-line-number="94">            output_operations.<span class="fu">index</span>$<span class="fu">set</span>(<span class="dv">0</span>);</a>
 970 <a class="sourceLine" id="cb47-95" data-line-number="95">            Pointer&lt;Pointer&lt;TF_Tensor&gt;&gt; output_tensors = s.<span class="fu">allocate</span>(ltPtrTensor);</a>
 971 <a class="sourceLine" id="cb47-96" data-line-number="96">            <span class="fu">TF_SessionRun</span>(session, Pointer.<span class="fu">ofNull</span>(),</a>
 972 <a class="sourceLine" id="cb47-97" data-line-number="97">                <span class="co">// Inputs</span></a>
 973 <a class="sourceLine" id="cb47-98" data-line-number="98">                input_operations.<span class="fu">ptr</span>(), input_tensors, <span class="dv">1</span>,</a>
 974 <a class="sourceLine" id="cb47-99" data-line-number="99">                <span class="co">// Outputs</span></a>
 975 <a class="sourceLine" id="cb47-100" data-line-number="100">                output_operations.<span class="fu">ptr</span>(), output_tensors, <span class="dv">1</span>,</a>
 976 <a class="sourceLine" id="cb47-101" data-line-number="101">                <span class="co">// Target operations</span></a>
 977 <a class="sourceLine" id="cb47-102" data-line-number="102">                Pointer.<span class="fu">ofNull</span>(), <span class="dv">0</span>, Pointer.<span class="fu">ofNull</span>(),</a>
 978 <a class="sourceLine" id="cb47-103" data-line-number="103">                status);</a>
 979 <a class="sourceLine" id="cb47-104" data-line-number="104"></a>
 980 <a class="sourceLine" id="cb47-105" data-line-number="105">            <span class="bu">System</span>.<span class="fu">out</span>.<span class="fu">println</span>(<span class="bu">String</span><span class="fu">.format</span>(<span class="st">&quot;Session Run Status: </span><span class="sc">%d</span><span class="st"> - </span><span class="sc">%s</span><span class="st">&quot;</span>,</a>
 981 <a class="sourceLine" id="cb47-106" data-line-number="106">                    <span class="fu">TF_GetCode</span>(status), Pointer.<span class="fu">toString</span>(<span class="fu">TF_Message</span>(status))));</a>
 982 <a class="sourceLine" id="cb47-107" data-line-number="107">            Pointer&lt;TF_Tensor&gt; tensor_out = output_tensors.<span class="fu">get</span>();</a>
 983 <a class="sourceLine" id="cb47-108" data-line-number="108">            <span class="bu">System</span>.<span class="fu">out</span>.<span class="fu">println</span>(<span class="st">&quot;Output Tensor Type: &quot;</span> + <span class="fu">TF_TensorType</span>(tensor_out));</a>
 984 <a class="sourceLine" id="cb47-109" data-line-number="109">            <span class="dt">float</span> outval = <span class="fu">TF_TensorData</span>(tensor_out).<span class="fu">cast</span>(NativeTypes.<span class="fu">FLOAT</span>).<span class="fu">get</span>();</a>
 985 <a class="sourceLine" id="cb47-110" data-line-number="110">            <span class="bu">System</span>.<span class="fu">out</span>.<span class="fu">println</span>(<span class="st">&quot;Output Tensor Value: &quot;</span> + outval);</a>
 986 <a class="sourceLine" id="cb47-111" data-line-number="111"></a>
 987 <a class="sourceLine" id="cb47-112" data-line-number="112">            <span class="fu">TF_CloseSession</span>(session, status);</a>
 988 <a class="sourceLine" id="cb47-113" data-line-number="113">            <span class="fu">TF_DeleteSession</span>(session, status);</a>
 989 <a class="sourceLine" id="cb47-114" data-line-number="114"></a>
 990 <a class="sourceLine" id="cb47-115" data-line-number="115">            <span class="fu">TF_DeleteSessionOptions</span>(options);</a>
 991 <a class="sourceLine" id="cb47-116" data-line-number="116"></a>
 992 <a class="sourceLine" id="cb47-117" data-line-number="117">            <span class="fu">TF_DeleteGraph</span>(graph);</a>
 993 <a class="sourceLine" id="cb47-118" data-line-number="118"></a>
 994 <a class="sourceLine" id="cb47-119" data-line-number="119">            <span class="fu">TF_DeleteTensor</span>(tensor_in);</a>
 995 <a class="sourceLine" id="cb47-120" data-line-number="120">            <span class="fu">TF_DeleteTensor</span>(tensor_out);</a>
 996 <a class="sourceLine" id="cb47-121" data-line-number="121">            <span class="fu">TF_DeleteTensor</span>(tensor_const_two);</a>
 997 <a class="sourceLine" id="cb47-122" data-line-number="122"></a>
 998 <a class="sourceLine" id="cb47-123" data-line-number="123">            <span class="fu">TF_DeleteStatus</span>(status);</a>
 999 <a class="sourceLine" id="cb47-124" data-line-number="124">        }</a>
1000 <a class="sourceLine" id="cb47-125" data-line-number="125">    }</a>
1001 <a class="sourceLine" id="cb47-126" data-line-number="126">}</a></code></pre></div>
1002 <h3 id="compiling-and-running-the-above-tensorflow-sample">Compiling and running the above TensorFlow sample</h3>
1003 <div class="sourceCode" id="cb48"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb48-1" data-line-number="1"></a>
1004 <a class="sourceLine" id="cb48-2" data-line-number="2"><span class="ex">javac</span> -cp tf.jar TensorFlowExample.java</a>
1005 <a class="sourceLine" id="cb48-3" data-line-number="3"></a>
1006 <a class="sourceLine" id="cb48-4" data-line-number="4"><span class="ex">java</span> -cp tf.jar:. TensorFlowExample</a></code></pre></div>
1007 <h2 id="using-tensorflow-c-api-windows">Using TensorFlow C API (Windows)</h2>
1008 <h3 id="installing-libtensorflow-1">Installing libtensorflow</h3>
1009 <p>You can download a binary distribution from <a href="https://www.tensorflow.org/install/lang_c" class="uri">https://www.tensorflow.org/install/lang_c</a></p>
1010 <p>Extract the zip file.</p>
1011 <h3 id="jextracting-libtensorflow-c_api.h-1">jextracting libtensorflow c_api.h</h3>
1012 <p>The problem outlined in the Mac OS instruction w.r.t. incorrect function prototypes still exists (though it has been solved in the Tensorflow github repository, this change has not yet made it into the binary distributions). On Windows there is however no jextract command that works around this, so the only way to extract the \include\c\c_api.h header successfully is to first manually fix the incorrect function prototypes:</p>
1013 <div class="sourceCode" id="cb49"><pre class="sourceCode c"><code class="sourceCode c"><a class="sourceLine" id="cb49-1" data-line-number="1">TF_Version() -&gt; TF_Version(<span class="dt">void</span>)  </a>
1014 <a class="sourceLine" id="cb49-2" data-line-number="2">TF_NewGraph() -&gt; TF_NewGraph(<span class="dt">void</span>)  </a>
1015 <a class="sourceLine" id="cb49-3" data-line-number="3">TF_NewSessionOptions() -&gt; TF_NewSessionOptions(<span class="dt">void</span>)  </a>
1016 <a class="sourceLine" id="cb49-4" data-line-number="4">TF_NewStatus() -&gt; TF_NewStatus(<span class="dt">void</span>)</a>
1017 <a class="sourceLine" id="cb49-5" data-line-number="5">TF_NewBuffer() -&gt; TF_NewBuffer(<span class="dt">void</span>)</a>
1018 <a class="sourceLine" id="cb49-6" data-line-number="6">TF_NewImportGraphDefOptions() -&gt; TF_NewImportGraphDefOptions(<span class="dt">void</span>)</a>
1019 <a class="sourceLine" id="cb49-7" data-line-number="7">TF_GetAllOpList() -&gt; TF_GetAllOpList(<span class="dt">void</span>)</a></code></pre></div>
1020 <p>Once you've done this you can use the following jextract command from the libtensorflow root directory:</p>
1021 <div class="sourceCode" id="cb50"><pre class="sourceCode powershell"><code class="sourceCode powershell"><a class="sourceLine" id="cb50-1" data-line-number="1">jextract -L .\lib -l tensorflow -o tf.<span class="fu">jar</span> -t <span class="st">&quot;org.tensorflow.panama&quot;</span> --record-library-path .\include\tensorflow\c\c_api.<span class="fu">h</span></a></code></pre></div>
1022 <h3 id="java-sample-code-that-uses-tensorflow-library-1">Java sample code that uses tensorflow library</h3>
1023 <p>This is the same as for the Mac OS section.</p>
1024 <h3 id="compiling-and-running-the-above-tensorflow-sample-1">Compiling and running the above TensorFlow sample</h3>
1025 <div class="sourceCode" id="cb51"><pre class="sourceCode powershell"><code class="sourceCode powershell"><a class="sourceLine" id="cb51-1" data-line-number="1">javac -cp tf.<span class="fu">jar</span> TensorFlowExample.<span class="fu">java</span></a>
1026 <a class="sourceLine" id="cb51-2" data-line-number="2">java -cp <span class="st">&quot;tf.jar;.&quot;</span> TensorFlowExample</a></code></pre></div>
1027 </body>
1028 </html>