JavaCPP Presets

repository·master·Indexed 25 days ago

https://github.com/bytedeco/javacpp-presets

Java configuration and interface classes for widely used C/C++ libraries, enabling seamless access to native APIs such as OpenCV, FFmpeg, TensorFlow, Apache Arrow, Bullet Physics SDK, ARToolKitPlus, ARPACK-NG, and the Arcade Learning Environment (ALE) from the Java platform, including Android.

Tokens
69K
Snippets
165
Records
229
Agent score
83%

What's inside JavaCPP Presets

  1. Overview of OpenVINO JavaCPP Preset

    master

    The OpenVINO preset packages the OpenVINO Runtime C API and its associated shared libraries for use in Java. It includes the CPU, GPU, and NPU plugin shared libraries sourced from the official OpenVINO wheel distribution.

    Supported platforms:

    • linux-x86_64
    • windows-x86_64
    • macosx-arm64
  2. Introduction to JavaCPP Presets for CUDA

    master

    This module provides JavaCPP Presets for several NVIDIA libraries, enabling Java access to high-performance GPU computing capabilities. Supported libraries include:

    • CUDA: 13.3.1
    • cuDNN (CUDA Deep Neural Network library): 9.24.0.43
    • NCCL (NVIDIA Collective Communications Library): 2.30.7
    • nvCOMP (NVIDIA nvCOMP): 5.2.0.10

    Detailed Java API documentation is available at: http://bytedeco.org/javacpp-presets/cuda/apidocs/

  3. Use cpu_features JavaCPP Presets

    master

    The cpu_features JavaCPP preset provides Java bindings for the Google cpu_features library (version 0.7.0), allowing you to check CPU features at runtime.

    To use this library, you can include the cpu_features-platform dependency in your Maven project. This dependency automatically handles the download and installation of both the Java class files and the necessary native binaries for your platform.

  4. Transform row-wise data into an Arrow Columnar Table

    master

    To convert row-wise data (like a list of objects) into an Apache Arrow Table, use specialized ArrayBuilder classes.

    Key steps:

    1. Initialize a MemoryPool (e.g., default_memory_pool()).
    2. Create builders for each type (e.g., Int64Builder, DoubleBuilder, ListBuilder).
    3. For nested structures like lists, use a ListBuilder and a nested value builder (e.g., new DoubleBuilder(components_builder.value_builder())).
    4. Iterate through your data, calling .Append() or .AppendValues() on the builders.
    5. Finalize the arrays using .Finish(Array) and combine them into a Table using Table.Make(schema, ArrayVector).
    // Example snippet for building a table
    MemoryPool pool = default_memory_pool();
    Int64Builder id_builder = new Int64Builder(int64(), pool);
    DoubleBuilder cost_builder = new DoubleBuilder(float64(), pool);
    ListBuilder components_builder = new ListBuilder(pool, new DoubleBuilder(float64(), pool));
    DoubleBuilder cost_components_builder = new DoubleBuilder(components_builder.value_builder());
    
    // ... loop and append data ...
    
    Array id_array = new Array(null);
    THROW_ON_FAILURE(id_builder.Finish(id_array));
    // ... finish other arrays ...
    
    FieldVector schema_vector = new FieldVector(
        new Field("id", int64()), 
        new Field("cost", float64()),
        new Field("cost_components", list(float64()))
    );
    Schema schema = new Schema(schema_vector);
    table[0] = Table.Make(schema, new ArrayVector(id_array, cost_array, cost_components_array));
  5. Install videoInput via Maven

    master

    To automatically download and install the necessary Java class files and native binaries for videoInput, add the videoinput-platform dependency to your Maven pom.xml. Ensure the version matches your requirements (e.g., 0.200-1.5.9).

    <dependency>
        <groupId>org.bytedeco</groupId>
        <artifactId>videoinput-platform</artifactId>
        <version>0.200-1.5.9</version>
    </dependency>
  6. Initialize CPython in Java

    master

    When embedding CPython in a Java application using these presets, it is recommended to call Py_Initialize(cachePackages()) instead of the standard Py_Initialize().

    Additionally, if you encounter OpenSSL issues, you may need to set the SSL_CERT_FILE environment variable to the full path of the cacert.pem file, which is extracted by default to ~/.javacpp/cache/.

  7. Install CUDA Presets via Maven

    master

    You can use Maven 3 to automatically download and install all necessary class files and native binaries. To run a sample project, create a pom.xml with the required dependencies for the CUDA platform and its redistributable components (cuBLAS, cuDNN, NCCL, etc.).

    <project>
        <modelVersion>4.0.0</modelVersion>
        <groupId>org.bytedeco.cuda</groupId>
        <artifactId>mnistcudnn</artifactId>
        <version>1.5.14-SNAPSHOT</version>
        <properties>
            <exec.mainClass>MNISTCUDNN</exec.mainClass>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.bytedeco</groupId>
                <artifactId>cuda-platform</artifactId>
                <version>13.3-9.24-1.5.14-SNAPSHOT</version>
            </dependency>
    
            <!-- Additional dependencies to use bundled CUDA, cuDNN, and NCCL -->
            <dependency>
                <groupId>org.bytedeco</groupId>
                <artifactId>cuda-platform-redist</artifactId>
                <version>13.3-9.24-1.5.14-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>org.bytedeco</groupId>
                <artifactId>cuda-platform-redist-cublas</artifactId>
                <version>13.3-9.24-1.5.14-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>org.bytedeco</groupId>
                <artifactId>cuda-platform-redist-cudnn</artifactId>
                <version>13.3-9.24-1.5.14-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>org.bytedeco</groupId>
                <artifactId>cuda-platform-redist-cusolver</artifactId>
                <version>13.3-9.24-1.5.14-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>org.bytedeco</groupId>
                <artifactId>cuda-platform-redist-cusparse</artifactId>
                <version>13.3-9.24-1.5.14-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>org.bytedeco</groupId>
                <artifactId>cuda-platform-redist-npp</artifactId>
                <version>13.3-9.24-1.5.14-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>org.bytedeco</groupId>
                <artifactId>cuda-platform-redist-nccl</artifactId>
                <version>13.3-9.24-1.5.14-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>org.bytedeco</groupId>
                <artifactId>cuda-platform-redist-nvcomp</artifactId>
                <version>13.3-9.24-1.5.14-SNAPSHOT</version>
            </dependency>
        </dependencies>
        <build>
            <sourceDirectory>.</sourceDirectory>
        </build>
    </project>

    After configuring the pom.xml and your Java source files, execute the following command to compile and run:

    $ mvn compile exec:java
  8. Run the TensorFlow Lite Minimal Example

    master

    To run the provided minimal inference example, create a pom.xml and a Minimal.java file as described in the documentation, then execute the following command in your terminal:

    $ mvn compile exec:java

    Note that the example requires a .tflite model file as a command-line argument. The usage pattern is: minimal <tflite model>.

  9. Install Caffe JavaCPP Presets via Maven

    master

    You can use Maven 3 to automatically download and install all necessary Java class files and native binaries for Caffe.

    To include Caffe with GPU support and bundled CUDA/cuDNN, use the following dependencies in your pom.xml:

    1. caffe-platform for core functionality.
    2. caffe-platform-gpu for GPU support.
    3. cuda-platform-redist for bundled CUDA and cuDNN binaries.

    To run a compiled Java application (like the sample caffe.java), use the following command:

    mvn compile exec:java -Dexec.args="..."
    <project>
        <modelVersion>4.0.0</modelVersion>
        <groupId>org.bytedeco.caffe</groupId>
        <artifactId>caffe</artifactId>
        <version>1.5.8</version>
        <properties>
            <exec.mainClass>caffe</exec.mainClass>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.bytedeco</groupId>
                <artifactId>caffe-platform</artifactId>
                <version>1.0-1.5.8</version>
            </dependency>
    
            <!-- Additional dependencies required to use CUDA and cuDNN -->
            <dependency>
                <groupId>org.bytedeco</groupId>
                <artifactId>caffe-platform-gpu</artifactId>
                <version>1.0-1.5.8</version>
            </dependency>
    
            <!-- Additional dependencies to use bundled CUDA and cuDNN -->
            <dependency>
                <groupId>org.bytedeco</groupId>
                <artifactId>cuda-platform-redist</artifactId>
                <version>11.8-8.6-1.5.8</version>
            </dependency>
    
        </dependencies>
        <build>
            <sourceDirectory>.</sourceDirectory>
        </build>
    </project>