< prev index next >

doc/panama_foreign.md

Print this page




  31 
  32 jextract -l python2.7 \
  33   -L /System/Library/Frameworks/Python.framework/Versions/2.7/lib --record-library-path \
  34   --exclude-symbols .*_FromFormatV\|_.*\|PyOS_vsnprintf\|.*_VaParse.*\|.*_VaBuild.*\|PyBuffer_SizeFromFormat\|vasprintf\|vfprintf\|vprintf\|vsprintf \
  35   -t org.python \
  36   /usr/include/python2.7/Python.h \
  37   -o python.jar
  38 
  39 ```
  40 
  41 ### Java program that uses extracted Python interface
  42 
  43 ```java
  44 
  45 // import java.foreign packages
  46 import java.foreign.Libraries;
  47 import java.foreign.Scope;
  48 import java.foreign.memory.Pointer;
  49 
  50 // import jextracted python 'header' classes
  51 import static org.python.Python_h.*;
  52 import static org.python.pythonrun_h.*;
  53 
  54 public class PythonMain {
  55     public static void main(String[] args) {
  56         Py_Initialize();
  57         try (Scope s = org.python.Python_h.scope().fork()) {
  58             PyRun_SimpleStringFlags(s.allocateCString(
  59                 "print(sum([33, 55, 66])); print('Hello from Python!')\n"),
  60                 Pointer.ofNull());
  61         }
  62         Py_Finalize();
  63     }
  64 }
  65 
  66 ```
  67 
  68 ### Running the Java code that calls Python interpreter
  69 
  70 ```sh
  71 
  72 javac -cp python.jar PythonMain.java
  73 
  74 java -cp python.jar:. PythonMain
  75 
  76 ```
  77 


 151 
 152 ### jextract a jar file for sqlite3.h
 153 
 154 ```sh
 155 
 156 jextract  /usr/include/sqlite3.h -t org.sqlite -lsqlite3 \
 157     -L /usr/lib --record-library-path         \
 158     --exclude-symbols sqlite3_vmprintf        \
 159     --exclude-symbols sqlite3_vsnprintf       \
 160     -o sqlite3.jar
 161 
 162 ```
 163 
 164 ### Java sample that uses sqlite3 library
 165 
 166 ```java
 167 
 168 import java.lang.invoke.*;
 169 import java.foreign.*;
 170 import java.foreign.memory.*;
 171 import org.sqlite.sqlite3.*;
 172 import static org.sqlite.sqlite3_h.*;
 173 
 174 public class SqliteMain {
 175    public static void main(String[] args) throws Exception {
 176         try (Scope scope = scope().fork()) {
 177             // char* errMsg;
 178             Pointer<Pointer<Byte>> errMsg = scope.allocate(NativeTypes.INT8.pointer());
 179 
 180             // sqlite3* db;
 181             Pointer<Pointer<sqlite3>> db = scope.allocate(LayoutType.ofStruct(sqlite3.class).pointer());
 182 
 183             int rc = sqlite3_open(scope.allocateCString("employee.db"), db);
 184             if (rc != 0) {
 185                 System.err.println("sqlite3_open failed: " + rc);
 186                 return;
 187             }
 188 
 189             // create a new table
 190             Pointer<Byte> sql = scope.allocateCString(
 191                 "CREATE TABLE EMPLOYEE ("  +
 192                 "  ID INT PRIMARY KEY NOT NULL,"       +


 336 yet handle C99 `_Complex` types. The rest of the options are standard ones.
 337 
 338 ### jextracting cblas.h (Ubuntu 16.04)
 339 
 340 The following command can be used to extract cblas.h on Ubuntu
 341 
 342 ```sh
 343 
 344 jextract -L /usr/lib/atlas-base -I /usr/include/atlas/ \
 345    -l cblas -t blas --record-library-path \
 346    /usr/include/atlas/cblas.h -o cblas.jar
 347 
 348 ```
 349 
 350 ### Java sample code that uses cblas library
 351 
 352 ```java
 353 
 354 import blas.cblas;
 355 
 356 import static blas.cblas_h.*;
 357 import static blas.cblas_h.CBLAS_ORDER.*;
 358 import static blas.cblas_h.CBLAS_TRANSPOSE.*;
 359 
 360 import java.foreign.NativeTypes;
 361 import java.foreign.Scope;
 362 import java.foreign.memory.Array;
 363 
 364 public class TestBlas {
 365    public static void main(String[] args) {
 366        @cblas.CBLAS_ORDER int Layout;
 367        @cblas.CBLAS_TRANSPOSE int transa;
 368 
 369        double alpha, beta;
 370        int m, n, lda, incx, incy, i;
 371 
 372        Layout = CblasColMajor;
 373        transa = CblasNoTrans;
 374 
 375        m = 4; /* Size of Column ( the number of rows ) */
 376        n = 4; /* Size of Row ( the number of columns ) */
 377        lda = 4; /* Leading dimension of 5 * 4 matrix is 5 */
 378        incx = 1;
 379        incy = 1;
 380        alpha = 1;
 381        beta = 0;
 382 
 383        try (Scope sc = scope().fork()){
 384            Array<Double> a = sc.allocateArray(NativeTypes.DOUBLE, m * n);
 385            Array<Double> x = sc.allocateArray(NativeTypes.DOUBLE, n);
 386            Array<Double> y = sc.allocateArray(NativeTypes.DOUBLE, n);
 387            /* The elements of the first column */


 440 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.
 441 
 442 ### jextracting clapack.h (Ubuntu 16.04)
 443 
 444 The following command can be used to extract the LAPACK header:
 445 
 446 ```sh
 447 
 448 jextract -L /usr/lib/atlas-base/atlas -I /usr/include/atlas/ \
 449    -l lapack -t lapack --record-library-path /usr/include/atlas/clapack.h -o clapack.jar
 450 
 451 ```
 452 
 453 ### Java sample code that uses LAPACK library
 454 
 455 ```java
 456 import java.foreign.NativeTypes;
 457 import java.foreign.Scope;
 458 import java.foreign.memory.Array;
 459 
 460 import static lapack.clapack_h.*;
 461 import static lapack.cblas_h.*;
 462 
 463 public class TestLapack {
 464     public static void main(String[] args) {
 465 
 466         /* Locals */
 467         try (Scope sc = lapack.clapack_h.scope().fork()) {
 468             Array<Double> A = sc.allocateArray(NativeTypes.DOUBLE, new double[]{
 469                     1, 2, 3, 4, 5, 1, 3, 5, 2, 4, 1, 4, 2, 5, 3
 470             });
 471             Array<Double> b = sc.allocateArray(NativeTypes.DOUBLE, new double[]{
 472                     -10, 12, 14, 16, 18, -3, 14, 12, 16, 16
 473             });
 474             int info, m, n, lda, ldb, nrhs;
 475 
 476             /* Initialization */
 477             m = 5;
 478             n = 3;
 479             nrhs = 2;
 480             lda = 5;
 481             ldb = 5;
 482 
 483             /* Print Entry Matrix */
 484             print_matrix_colmajor("Entry Matrix A", m, n, A, lda );
 485             /* Print Right Rand Side */
 486             print_matrix_colmajor("Right Hand Side b", n, nrhs, b, ldb );
 487             System.out.println();


 523 ## Using LAPACK library (Mac OS)
 524 
 525 On Mac OS, lapack is installed under /usr/local/opt/lapack directory.
 526 
 527 ### jextracting lapacke.h
 528 
 529 ```sh
 530 
 531 jextract -L /usr/local/opt/lapack/lib -I /usr/local/opt/lapack/ \
 532   -l lapacke -t lapack --record-library-path /usr/local/opt/lapack/include/lapacke.h -o clapack.jar
 533 
 534 ```
 535 ### Java sample code that uses LAPACK library
 536 
 537 ```java
 538 
 539 import java.foreign.NativeTypes;
 540 import java.foreign.Scope;
 541 import java.foreign.memory.Array;
 542 
 543 import static lapack.lapacke_h.*;
 544 
 545 public class TestLapack {
 546     public static void main(String[] args) {
 547 
 548         /* Locals */
 549         try (Scope sc = scope().fork()) {
 550             Array<Double> A = sc.allocateArray(NativeTypes.DOUBLE, new double[]{
 551                     1, 2, 3, 4, 5, 1, 3, 5, 2, 4, 1, 4, 2, 5, 3
 552             });
 553             Array<Double> b = sc.allocateArray(NativeTypes.DOUBLE, new double[]{
 554                     -10, 12, 14, 16, 18, -3, 14, 12, 16, 16
 555             });
 556             int info, m, n, lda, ldb, nrhs;
 557 
 558             /* Initialization */
 559             m = 5;
 560             n = 3;
 561             nrhs = 2;
 562             lda = 5;
 563             ldb = 5;


 598 ```sh
 599 
 600 javac -cp clapack.jar TestLapack.java
 601 
 602 java -cp clapack.jar:. TestLapack
 603 
 604 ```
 605 
 606 ## Using libproc library to list processes from Java (Mac OS)
 607 
 608 ### jextract a jar file for libproc.h
 609 
 610 jextract -t org.unix -lproc -L /usr/lib --record-library-path -o libproc.jar /usr/include/libproc.h
 611 
 612 ### Java program that uses libproc to list processes
 613 
 614 ```java
 615 
 616 import java.foreign.*;
 617 import java.foreign.memory.*;
 618 import static org.unix.libproc_h.*;
 619 
 620 public class LibprocMain {
 621     private static final int NAME_BUF_MAX = 256;
 622 
 623     public static void main(String[] args) {
 624         // Scope for native allocations
 625         try (Scope s = scope().fork()) {
 626             // get the number of processes
 627             int numPids = proc_listallpids(Pointer.ofNull(), 0);
 628             // allocate an array
 629             Array<Integer> pids = s.allocateArray(NativeTypes.INT32, numPids);
 630             // list all the pids into the native array
 631             proc_listallpids(pids.elementPointer(), numPids);
 632             // convert native array to java array
 633             int[] jpids = pids.toArray(num -> new int[num]);
 634             // buffer for process name
 635             Pointer<Byte> nameBuf = s.allocate(NativeTypes.INT8, NAME_BUF_MAX);
 636             for (int i = 0; i < jpids.length; i++) {
 637                 int pid = jpids[i];
 638                 // get the process name


 659 
 660 ## Using readline library from Java code (Mac OS)
 661 
 662 ### jextract a jar file for readline.h
 663 
 664 ```sh
 665 
 666 jextract -l readline -L /usr/local/opt/readline/lib/ --record-library-path \
 667     -t org.unix \
 668     /usr/include/readline/readline.h \
 669     --exclude-symbol readline_echoing_p -o readline.jar
 670 
 671 ```
 672 
 673 ### Java code that uses readline
 674 
 675 ```java
 676 
 677 import java.foreign.*;
 678 import java.foreign.memory.*;
 679 import static org.unix.readline_h.*;
 680 
 681 public class Readline {
 682     public static void main(String[] args) {
 683         // Scope for native allocations
 684         try (Scope s = scope().fork()) {
 685             // allocate C memory initialized with Java string content
 686             var pstr = s.allocateCString("name? ");
 687 
 688             // call "readline" API
 689             var p = readline(pstr);
 690 
 691             // print char* as is
 692             System.out.println(p);
 693             // convert char* ptr from readline as Java String & print it
 694             System.out.println(Pointer.toString(p));
 695         }
 696     }
 697 }
 698 
 699 ```


 708 
 709 ```
 710 
 711 ## Using libcurl from Java (Mac OS)
 712 
 713 ### jextract a jar for curl.h
 714 
 715 ```sh
 716 
 717 jextract -t org.unix -L /usr/lib -lcurl --record-library-path /usr/include/curl/curl.h -o curl.jar
 718 
 719 ```
 720 
 721 ### Java code that uses libcurl
 722 
 723 ```java
 724 
 725 import java.lang.invoke.*;
 726 import java.foreign.*;
 727 import java.foreign.memory.*;
 728 import org.unix.curl.*;
 729 import org.unix.curl_h;
 730 import static org.unix.curl_h.*;
 731 import static org.unix.easy_h.*;
 732 
 733 public class CurlMain {
 734    public static void main(String[] args) {
 735        try (Scope s = curl_h.scope().fork()) { 
 736            curl_global_init(CURL_GLOBAL_DEFAULT);
 737            Pointer<Void> curl = curl_easy_init();
 738            if(!curl.isNull()) {
 739                Pointer<Byte> url = s.allocateCString(args[0]);
 740                curl_easy_setopt(curl, CURLOPT_URL, url);
 741                int res = curl_easy_perform(curl);
 742                if (res != CURLE_OK) {
 743                    curl_easy_cleanup(curl);
 744                }
 745            }
 746            curl_global_cleanup();
 747        }
 748     }
 749 }
 750 
 751 ```
 752 
 753 ### Running the java code that uses libcurl
 754 
 755 ```sh


 758 java -cp curl.jar:. CurlMain <url>
 759 
 760 ```
 761 
 762 ## Using unistd.h from Java code (Linux)
 763 
 764 ### jextract a jar file for unistd.h
 765 
 766 ```sh
 767 
 768 jextract /usr/include/unistd.h -t org.unix -o unistd.jar
 769 
 770 ```
 771 
 772 ### Java code that calls getpid
 773 
 774 ```java
 775 
 776 import java.foreign.*;
 777 import java.lang.invoke.*;
 778 import org.unix.unistd;
 779 
 780 
 781 public class Getpid {
 782     public static void main(String[] args) {
 783         // bind unistd interface
 784         var u = Libraries.bind(MethodHandles.lookup(), unistd.class);
 785         // call getpid from the unistd.h
 786         System.out.println(u.getpid());
 787         // check process id from Java API!
 788         System.out.println(ProcessHandle.current().pid());
 789     }
 790 }
 791 
 792 ```
 793 
 794 ### Running the Java code that uses getpid
 795 
 796 ```sh
 797 
 798 javac -cp unistd.jar Getpid.java
 799 
 800 java -cp unistd.jar:. Getpid
 801 
 802 ```
 803 
 804 


 823 ### jextracting OpenGL (Ubuntu 16.04)
 824 
 825 To extract the opengl libraries the following command suffices:
 826 
 827 ```sh
 828 
 829 jextract -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
 830 
 831 ```
 832 
 833 Since glut depends on the other libraries (GLU and GL), it is not necessary to give additional headers to jextract.
 834 
 835 ### Java sample code that uses the OpenGL library
 836 
 837 ```java
 838 import java.foreign.NativeTypes;
 839 import java.foreign.Scope;
 840 import java.foreign.memory.Array;
 841 import java.foreign.memory.Pointer;
 842 
 843 import static opengl.gl_h.*;
 844 import static opengl.freeglut_std_h.*;
 845 
 846 public class Teapot {
 847 
 848     float rot = 0;
 849 
 850     Teapot(Scope sc) {
 851         // Misc Parameters
 852         Array<Float> pos = sc.allocateArray(NativeTypes.FLOAT, new float[] {0.0f, 15.0f, -15.0f, 0});
 853         Array<Float> spec = sc.allocateArray(NativeTypes.FLOAT, new float[] {1, 1, 1, 0});
 854         Array<Float> shini = sc.allocateArray(NativeTypes.FLOAT, new float[] {113});
 855 
 856         // Reset Background
 857         glClearColor(0, 0, 0, 0);
 858 
 859         // Setup Lighting
 860         glShadeModel(GL_SMOOTH);
 861         glLightfv(GL_LIGHT0, GL_POSITION, pos.elementPointer());
 862         glLightfv(GL_LIGHT0, GL_AMBIENT, spec.elementPointer());
 863         glLightfv(GL_LIGHT0, GL_DIFFUSE, spec.elementPointer());
 864         glLightfv(GL_LIGHT0, GL_SPECULAR, spec.elementPointer());


 867         glEnable(GL_LIGHT0);
 868         glEnable(GL_DEPTH_TEST);
 869     }
 870 
 871     void display() {
 872         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 873         glPushMatrix();
 874         glRotatef(-20, 1, 1, 0);
 875         glRotatef(rot, 0, 1, 0);
 876         glutSolidTeapot(0.5);
 877         glPopMatrix();
 878         glutSwapBuffers();
 879     }
 880 
 881     void onIdle() {
 882         rot += 0.1;
 883         glutPostRedisplay();
 884     }
 885 
 886     public static void main(String[] args) {
 887         try (Scope sc = opengl.gl_h.scope().fork()) {
 888             Pointer<Integer> argc = sc.allocate(NativeTypes.INT32);
 889             argc.set(0);
 890             glutInit(argc, Pointer.ofNull());
 891             glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
 892             glutInitWindowSize(900, 900);
 893             glutCreateWindow(sc.allocateCString("Hello Panama!"));
 894             Teapot teapot = new Teapot(sc);
 895             glutDisplayFunc(sc.allocateCallback(teapot::display));
 896             glutIdleFunc(sc.allocateCallback(teapot::onIdle));
 897             glutMainLoop();
 898         }
 899     }
 900 }
 901 ```
 902 ### Running the Java code that uses OpenGL (Ubuntu 16.04)
 903 
 904 ```sh
 905 
 906 javac -cp opengl.jar Teapot.java
 907 


 983         -o tf.jar -t org.tensorflow.panama \
 984         /usr/local/include/tensorflow/c/c_api.h
 985 
 986 ```
 987 
 988 The caveat to extract tensorflow C API is that it declare function prototype
 989 without argument in C++ style, for example, TF_Version(), which is considered
 990 incomplete C function prototype instead of C style as in TF_Version(void). An
 991 incomplete function prototype will become vararg funciton. To avoid that, we
 992 need to pass clang '-x c++' options to jextract with '-C -x -C c++'
 993 
 994 ### Java sample code that uses tensorflow library
 995 
 996 ```java
 997 
 998 import java.foreign.NativeTypes;
 999 import java.foreign.Scope;
1000 import java.foreign.memory.Array;
1001 import java.foreign.memory.LayoutType;
1002 import java.foreign.memory.Pointer;
1003 import org.tensorflow.panama.c_api.TF_DataType;
1004 import org.tensorflow.panama.c_api.TF_Graph;
1005 import org.tensorflow.panama.c_api.TF_Operation;
1006 import org.tensorflow.panama.c_api.TF_OperationDescription;
1007 import org.tensorflow.panama.c_api.TF_Output;
1008 import org.tensorflow.panama.c_api.TF_Session;
1009 import org.tensorflow.panama.c_api.TF_SessionOptions;
1010 import org.tensorflow.panama.c_api.TF_Status;
1011 import org.tensorflow.panama.c_api.TF_Tensor;
1012 
1013 import static org.tensorflow.panama.c_api_h.*;
1014 import static org.tensorflow.panama.c_api_h.TF_DataType.*;
1015 
1016 public class TensorFlowExample {
1017     static Pointer<TF_Operation> PlaceHolder(Pointer<TF_Graph> graph, Pointer<TF_Status> status,
1018                                       @TF_DataType int dtype, String name) {
1019         try (var s = scope().fork()) {
1020             Pointer<TF_OperationDescription> desc = TF_NewOperation(graph,
1021                     s.allocateCString("Placeholder"), s.allocateCString(name));
1022             TF_SetAttrType(desc, s.allocateCString("dtype"), TF_FLOAT);
1023             return TF_FinishOperation(desc, status);
1024         }
1025     }
1026 
1027     static Pointer<TF_Operation> ConstValue(Pointer<TF_Graph> graph, Pointer<TF_Status> status,
1028                                 Pointer<TF_Tensor> tensor, String name) {
1029         try (var s = scope().fork()) {
1030             Pointer<TF_OperationDescription> desc = TF_NewOperation(graph,
1031                     s.allocateCString("Const"), s.allocateCString(name));
1032             TF_SetAttrTensor(desc, s.allocateCString("value"), tensor, status);
1033             TF_SetAttrType(desc, s.allocateCString("dtype"), TF_TensorType(tensor));
1034             return TF_FinishOperation(desc, status);




  31 
  32 jextract -l python2.7 \
  33   -L /System/Library/Frameworks/Python.framework/Versions/2.7/lib --record-library-path \
  34   --exclude-symbols .*_FromFormatV\|_.*\|PyOS_vsnprintf\|.*_VaParse.*\|.*_VaBuild.*\|PyBuffer_SizeFromFormat\|vasprintf\|vfprintf\|vprintf\|vsprintf \
  35   -t org.python \
  36   /usr/include/python2.7/Python.h \
  37   -o python.jar
  38 
  39 ```
  40 
  41 ### Java program that uses extracted Python interface
  42 
  43 ```java
  44 
  45 // import java.foreign packages
  46 import java.foreign.Libraries;
  47 import java.foreign.Scope;
  48 import java.foreign.memory.Pointer;
  49 
  50 // import jextracted python 'header' classes
  51 import static org.python.Python_lib.*;
  52 import static org.python.pythonrun_lib.*;
  53 
  54 public class PythonMain {
  55     public static void main(String[] args) {
  56         Py_Initialize();
  57         try (Scope s = org.python.Python_lib.scope().fork()) {
  58             PyRun_SimpleStringFlags(s.allocateCString(
  59                 "print(sum([33, 55, 66])); print('Hello from Python!')\n"),
  60                 Pointer.ofNull());
  61         }
  62         Py_Finalize();
  63     }
  64 }
  65 
  66 ```
  67 
  68 ### Running the Java code that calls Python interpreter
  69 
  70 ```sh
  71 
  72 javac -cp python.jar PythonMain.java
  73 
  74 java -cp python.jar:. PythonMain
  75 
  76 ```
  77 


 151 
 152 ### jextract a jar file for sqlite3.h
 153 
 154 ```sh
 155 
 156 jextract  /usr/include/sqlite3.h -t org.sqlite -lsqlite3 \
 157     -L /usr/lib --record-library-path         \
 158     --exclude-symbols sqlite3_vmprintf        \
 159     --exclude-symbols sqlite3_vsnprintf       \
 160     -o sqlite3.jar
 161 
 162 ```
 163 
 164 ### Java sample that uses sqlite3 library
 165 
 166 ```java
 167 
 168 import java.lang.invoke.*;
 169 import java.foreign.*;
 170 import java.foreign.memory.*;
 171 import org.sqlite.sqlite3_h.*;
 172 import static org.sqlite.sqlite3_lib.*;
 173 
 174 public class SqliteMain {
 175    public static void main(String[] args) throws Exception {
 176         try (Scope scope = scope().fork()) {
 177             // char* errMsg;
 178             Pointer<Pointer<Byte>> errMsg = scope.allocate(NativeTypes.INT8.pointer());
 179 
 180             // sqlite3* db;
 181             Pointer<Pointer<sqlite3>> db = scope.allocate(LayoutType.ofStruct(sqlite3.class).pointer());
 182 
 183             int rc = sqlite3_open(scope.allocateCString("employee.db"), db);
 184             if (rc != 0) {
 185                 System.err.println("sqlite3_open failed: " + rc);
 186                 return;
 187             }
 188 
 189             // create a new table
 190             Pointer<Byte> sql = scope.allocateCString(
 191                 "CREATE TABLE EMPLOYEE ("  +
 192                 "  ID INT PRIMARY KEY NOT NULL,"       +


 336 yet handle C99 `_Complex` types. The rest of the options are standard ones.
 337 
 338 ### jextracting cblas.h (Ubuntu 16.04)
 339 
 340 The following command can be used to extract cblas.h on Ubuntu
 341 
 342 ```sh
 343 
 344 jextract -L /usr/lib/atlas-base -I /usr/include/atlas/ \
 345    -l cblas -t blas --record-library-path \
 346    /usr/include/atlas/cblas.h -o cblas.jar
 347 
 348 ```
 349 
 350 ### Java sample code that uses cblas library
 351 
 352 ```java
 353 
 354 import blas.cblas;
 355 
 356 import static blas.cblas_lib.*;
 357 import static blas.cblas_lib.CBLAS_ORDER.*;
 358 import static blas.cblas_lib.CBLAS_TRANSPOSE.*;
 359 
 360 import java.foreign.NativeTypes;
 361 import java.foreign.Scope;
 362 import java.foreign.memory.Array;
 363 
 364 public class TestBlas {
 365    public static void main(String[] args) {
 366        @blas.cblas_h.CBLAS_ORDER int Layout;
 367        @blas.cblas_h.CBLAS_TRANSPOSE int transa;
 368 
 369        double alpha, beta;
 370        int m, n, lda, incx, incy, i;
 371 
 372        Layout = CblasColMajor;
 373        transa = CblasNoTrans;
 374 
 375        m = 4; /* Size of Column ( the number of rows ) */
 376        n = 4; /* Size of Row ( the number of columns ) */
 377        lda = 4; /* Leading dimension of 5 * 4 matrix is 5 */
 378        incx = 1;
 379        incy = 1;
 380        alpha = 1;
 381        beta = 0;
 382 
 383        try (Scope sc = scope().fork()){
 384            Array<Double> a = sc.allocateArray(NativeTypes.DOUBLE, m * n);
 385            Array<Double> x = sc.allocateArray(NativeTypes.DOUBLE, n);
 386            Array<Double> y = sc.allocateArray(NativeTypes.DOUBLE, n);
 387            /* The elements of the first column */


 440 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.
 441 
 442 ### jextracting clapack.h (Ubuntu 16.04)
 443 
 444 The following command can be used to extract the LAPACK header:
 445 
 446 ```sh
 447 
 448 jextract -L /usr/lib/atlas-base/atlas -I /usr/include/atlas/ \
 449    -l lapack -t lapack --record-library-path /usr/include/atlas/clapack.h -o clapack.jar
 450 
 451 ```
 452 
 453 ### Java sample code that uses LAPACK library
 454 
 455 ```java
 456 import java.foreign.NativeTypes;
 457 import java.foreign.Scope;
 458 import java.foreign.memory.Array;
 459 
 460 import static lapack.clapack_lib.*;
 461 import static lapack.cblas_lib.*;
 462 
 463 public class TestLapack {
 464     public static void main(String[] args) {
 465 
 466         /* Locals */
 467         try (Scope sc = lapack.clapack_lib.scope().fork()) {
 468             Array<Double> A = sc.allocateArray(NativeTypes.DOUBLE, new double[]{
 469                     1, 2, 3, 4, 5, 1, 3, 5, 2, 4, 1, 4, 2, 5, 3
 470             });
 471             Array<Double> b = sc.allocateArray(NativeTypes.DOUBLE, new double[]{
 472                     -10, 12, 14, 16, 18, -3, 14, 12, 16, 16
 473             });
 474             int info, m, n, lda, ldb, nrhs;
 475 
 476             /* Initialization */
 477             m = 5;
 478             n = 3;
 479             nrhs = 2;
 480             lda = 5;
 481             ldb = 5;
 482 
 483             /* Print Entry Matrix */
 484             print_matrix_colmajor("Entry Matrix A", m, n, A, lda );
 485             /* Print Right Rand Side */
 486             print_matrix_colmajor("Right Hand Side b", n, nrhs, b, ldb );
 487             System.out.println();


 523 ## Using LAPACK library (Mac OS)
 524 
 525 On Mac OS, lapack is installed under /usr/local/opt/lapack directory.
 526 
 527 ### jextracting lapacke.h
 528 
 529 ```sh
 530 
 531 jextract -L /usr/local/opt/lapack/lib -I /usr/local/opt/lapack/ \
 532   -l lapacke -t lapack --record-library-path /usr/local/opt/lapack/include/lapacke.h -o clapack.jar
 533 
 534 ```
 535 ### Java sample code that uses LAPACK library
 536 
 537 ```java
 538 
 539 import java.foreign.NativeTypes;
 540 import java.foreign.Scope;
 541 import java.foreign.memory.Array;
 542 
 543 import static lapack.lapacke_lib.*;
 544 
 545 public class TestLapack {
 546     public static void main(String[] args) {
 547 
 548         /* Locals */
 549         try (Scope sc = scope().fork()) {
 550             Array<Double> A = sc.allocateArray(NativeTypes.DOUBLE, new double[]{
 551                     1, 2, 3, 4, 5, 1, 3, 5, 2, 4, 1, 4, 2, 5, 3
 552             });
 553             Array<Double> b = sc.allocateArray(NativeTypes.DOUBLE, new double[]{
 554                     -10, 12, 14, 16, 18, -3, 14, 12, 16, 16
 555             });
 556             int info, m, n, lda, ldb, nrhs;
 557 
 558             /* Initialization */
 559             m = 5;
 560             n = 3;
 561             nrhs = 2;
 562             lda = 5;
 563             ldb = 5;


 598 ```sh
 599 
 600 javac -cp clapack.jar TestLapack.java
 601 
 602 java -cp clapack.jar:. TestLapack
 603 
 604 ```
 605 
 606 ## Using libproc library to list processes from Java (Mac OS)
 607 
 608 ### jextract a jar file for libproc.h
 609 
 610 jextract -t org.unix -lproc -L /usr/lib --record-library-path -o libproc.jar /usr/include/libproc.h
 611 
 612 ### Java program that uses libproc to list processes
 613 
 614 ```java
 615 
 616 import java.foreign.*;
 617 import java.foreign.memory.*;
 618 import static org.unix.libproc_lib.*;
 619 
 620 public class LibprocMain {
 621     private static final int NAME_BUF_MAX = 256;
 622 
 623     public static void main(String[] args) {
 624         // Scope for native allocations
 625         try (Scope s = scope().fork()) {
 626             // get the number of processes
 627             int numPids = proc_listallpids(Pointer.ofNull(), 0);
 628             // allocate an array
 629             Array<Integer> pids = s.allocateArray(NativeTypes.INT32, numPids);
 630             // list all the pids into the native array
 631             proc_listallpids(pids.elementPointer(), numPids);
 632             // convert native array to java array
 633             int[] jpids = pids.toArray(num -> new int[num]);
 634             // buffer for process name
 635             Pointer<Byte> nameBuf = s.allocate(NativeTypes.INT8, NAME_BUF_MAX);
 636             for (int i = 0; i < jpids.length; i++) {
 637                 int pid = jpids[i];
 638                 // get the process name


 659 
 660 ## Using readline library from Java code (Mac OS)
 661 
 662 ### jextract a jar file for readline.h
 663 
 664 ```sh
 665 
 666 jextract -l readline -L /usr/local/opt/readline/lib/ --record-library-path \
 667     -t org.unix \
 668     /usr/include/readline/readline.h \
 669     --exclude-symbol readline_echoing_p -o readline.jar
 670 
 671 ```
 672 
 673 ### Java code that uses readline
 674 
 675 ```java
 676 
 677 import java.foreign.*;
 678 import java.foreign.memory.*;
 679 import static org.unix.readline_lib.*;
 680 
 681 public class Readline {
 682     public static void main(String[] args) {
 683         // Scope for native allocations
 684         try (Scope s = scope().fork()) {
 685             // allocate C memory initialized with Java string content
 686             var pstr = s.allocateCString("name? ");
 687 
 688             // call "readline" API
 689             var p = readline(pstr);
 690 
 691             // print char* as is
 692             System.out.println(p);
 693             // convert char* ptr from readline as Java String & print it
 694             System.out.println(Pointer.toString(p));
 695         }
 696     }
 697 }
 698 
 699 ```


 708 
 709 ```
 710 
 711 ## Using libcurl from Java (Mac OS)
 712 
 713 ### jextract a jar for curl.h
 714 
 715 ```sh
 716 
 717 jextract -t org.unix -L /usr/lib -lcurl --record-library-path /usr/include/curl/curl.h -o curl.jar
 718 
 719 ```
 720 
 721 ### Java code that uses libcurl
 722 
 723 ```java
 724 
 725 import java.lang.invoke.*;
 726 import java.foreign.*;
 727 import java.foreign.memory.*;
 728 import org.unix.curl_lib;
 729 import static org.unix.curl_lib.*;
 730 import static org.unix.easy_lib.*;

 731 
 732 public class CurlMain {
 733    public static void main(String[] args) {
 734        try (Scope s = curl_lib.scope().fork()) { 
 735            curl_global_init(CURL_GLOBAL_DEFAULT);
 736            Pointer<Void> curl = curl_easy_init();
 737            if(!curl.isNull()) {
 738                Pointer<Byte> url = s.allocateCString(args[0]);
 739                curl_easy_setopt(curl, CURLOPT_URL, url);
 740                int res = curl_easy_perform(curl);
 741                if (res != CURLE_OK) {
 742                    curl_easy_cleanup(curl);
 743                }
 744            }
 745            curl_global_cleanup();
 746        }
 747     }
 748 }
 749 
 750 ```
 751 
 752 ### Running the java code that uses libcurl
 753 
 754 ```sh


 757 java -cp curl.jar:. CurlMain <url>
 758 
 759 ```
 760 
 761 ## Using unistd.h from Java code (Linux)
 762 
 763 ### jextract a jar file for unistd.h
 764 
 765 ```sh
 766 
 767 jextract /usr/include/unistd.h -t org.unix -o unistd.jar
 768 
 769 ```
 770 
 771 ### Java code that calls getpid
 772 
 773 ```java
 774 
 775 import java.foreign.*;
 776 import java.lang.invoke.*;
 777 import org.unix.unistd_h;
 778 
 779 
 780 public class Getpid {
 781     public static void main(String[] args) {
 782         // bind unistd interface
 783         var u = Libraries.bind(MethodHandles.lookup(), unistd_h.class);
 784         // call getpid from the unistd.h
 785         System.out.println(u.getpid());
 786         // check process id from Java API!
 787         System.out.println(ProcessHandle.current().pid());
 788     }
 789 }
 790 
 791 ```
 792 
 793 ### Running the Java code that uses getpid
 794 
 795 ```sh
 796 
 797 javac -cp unistd.jar Getpid.java
 798 
 799 java -cp unistd.jar:. Getpid
 800 
 801 ```
 802 
 803 


 822 ### jextracting OpenGL (Ubuntu 16.04)
 823 
 824 To extract the opengl libraries the following command suffices:
 825 
 826 ```sh
 827 
 828 jextract -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
 829 
 830 ```
 831 
 832 Since glut depends on the other libraries (GLU and GL), it is not necessary to give additional headers to jextract.
 833 
 834 ### Java sample code that uses the OpenGL library
 835 
 836 ```java
 837 import java.foreign.NativeTypes;
 838 import java.foreign.Scope;
 839 import java.foreign.memory.Array;
 840 import java.foreign.memory.Pointer;
 841 
 842 import static opengl.gl_lib.*;
 843 import static opengl.freeglut_std_lib.*;
 844 
 845 public class Teapot {
 846 
 847     float rot = 0;
 848 
 849     Teapot(Scope sc) {
 850         // Misc Parameters
 851         Array<Float> pos = sc.allocateArray(NativeTypes.FLOAT, new float[] {0.0f, 15.0f, -15.0f, 0});
 852         Array<Float> spec = sc.allocateArray(NativeTypes.FLOAT, new float[] {1, 1, 1, 0});
 853         Array<Float> shini = sc.allocateArray(NativeTypes.FLOAT, new float[] {113});
 854 
 855         // Reset Background
 856         glClearColor(0, 0, 0, 0);
 857 
 858         // Setup Lighting
 859         glShadeModel(GL_SMOOTH);
 860         glLightfv(GL_LIGHT0, GL_POSITION, pos.elementPointer());
 861         glLightfv(GL_LIGHT0, GL_AMBIENT, spec.elementPointer());
 862         glLightfv(GL_LIGHT0, GL_DIFFUSE, spec.elementPointer());
 863         glLightfv(GL_LIGHT0, GL_SPECULAR, spec.elementPointer());


 866         glEnable(GL_LIGHT0);
 867         glEnable(GL_DEPTH_TEST);
 868     }
 869 
 870     void display() {
 871         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 872         glPushMatrix();
 873         glRotatef(-20, 1, 1, 0);
 874         glRotatef(rot, 0, 1, 0);
 875         glutSolidTeapot(0.5);
 876         glPopMatrix();
 877         glutSwapBuffers();
 878     }
 879 
 880     void onIdle() {
 881         rot += 0.1;
 882         glutPostRedisplay();
 883     }
 884 
 885     public static void main(String[] args) {
 886         try (Scope sc = opengl.gl_lib.scope().fork()) {
 887             Pointer<Integer> argc = sc.allocate(NativeTypes.INT32);
 888             argc.set(0);
 889             glutInit(argc, Pointer.ofNull());
 890             glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
 891             glutInitWindowSize(900, 900);
 892             glutCreateWindow(sc.allocateCString("Hello Panama!"));
 893             Teapot teapot = new Teapot(sc);
 894             glutDisplayFunc(sc.allocateCallback(teapot::display));
 895             glutIdleFunc(sc.allocateCallback(teapot::onIdle));
 896             glutMainLoop();
 897         }
 898     }
 899 }
 900 ```
 901 ### Running the Java code that uses OpenGL (Ubuntu 16.04)
 902 
 903 ```sh
 904 
 905 javac -cp opengl.jar Teapot.java
 906 


 982         -o tf.jar -t org.tensorflow.panama \
 983         /usr/local/include/tensorflow/c/c_api.h
 984 
 985 ```
 986 
 987 The caveat to extract tensorflow C API is that it declare function prototype
 988 without argument in C++ style, for example, TF_Version(), which is considered
 989 incomplete C function prototype instead of C style as in TF_Version(void). An
 990 incomplete function prototype will become vararg funciton. To avoid that, we
 991 need to pass clang '-x c++' options to jextract with '-C -x -C c++'
 992 
 993 ### Java sample code that uses tensorflow library
 994 
 995 ```java
 996 
 997 import java.foreign.NativeTypes;
 998 import java.foreign.Scope;
 999 import java.foreign.memory.Array;
1000 import java.foreign.memory.LayoutType;
1001 import java.foreign.memory.Pointer;
1002 import org.tensorflow.panama.c_api_h.TF_DataType;
1003 import org.tensorflow.panama.c_api_h.TF_Graph;
1004 import org.tensorflow.panama.c_api_h.TF_Operation;
1005 import org.tensorflow.panama.c_api_h.TF_OperationDescription;
1006 import org.tensorflow.panama.c_api_h.TF_Output;
1007 import org.tensorflow.panama.c_api_h.TF_Session;
1008 import org.tensorflow.panama.c_api_h.TF_SessionOptions;
1009 import org.tensorflow.panama.c_api_h.TF_Status;
1010 import org.tensorflow.panama.c_api_h.TF_Tensor;
1011 
1012 import static org.tensorflow.panama.c_api_lib.*;
1013 import static org.tensorflow.panama.c_api_lib.TF_DataType.*;
1014 
1015 public class TensorFlowExample {
1016     static Pointer<TF_Operation> PlaceHolder(Pointer<TF_Graph> graph, Pointer<TF_Status> status,
1017                                       @TF_DataType int dtype, String name) {
1018         try (var s = scope().fork()) {
1019             Pointer<TF_OperationDescription> desc = TF_NewOperation(graph,
1020                     s.allocateCString("Placeholder"), s.allocateCString(name));
1021             TF_SetAttrType(desc, s.allocateCString("dtype"), TF_FLOAT);
1022             return TF_FinishOperation(desc, status);
1023         }
1024     }
1025 
1026     static Pointer<TF_Operation> ConstValue(Pointer<TF_Graph> graph, Pointer<TF_Status> status,
1027                                 Pointer<TF_Tensor> tensor, String name) {
1028         try (var s = scope().fork()) {
1029             Pointer<TF_OperationDescription> desc = TF_NewOperation(graph,
1030                     s.allocateCString("Const"), s.allocateCString(name));
1031             TF_SetAttrTensor(desc, s.allocateCString("value"), tensor, status);
1032             TF_SetAttrType(desc, s.allocateCString("dtype"), TF_TensorType(tensor));
1033             return TF_FinishOperation(desc, status);


< prev index next >