Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
268 changes: 138 additions & 130 deletions java/cuvs-java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,144 +14,152 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-->
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.nvidia.cuvs</groupId>
<artifactId>cuvs-java</artifactId>
<version>24.12.1</version>
<name>cuvs-java</name>
<packaging>jar</packaging>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.nvidia.cuvs</groupId>
<artifactId>cuvs-java</artifactId>
<version>24.12.1</version>
<name>cuvs-java</name>
<packaging>jar</packaging>

<properties>
<maven.compiler.target>22</maven.compiler.target>
<maven.compiler.source>22</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<properties>
<maven.compiler.target>22</maven.compiler.target>
<maven.compiler.source>22</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<dependencies>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.15.1</version>
</dependency>
<dependencies>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.15.1</version>
</dependency>

<dependency>
<groupId>com.github.fommil</groupId>
<artifactId>jniloader</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>com.github.fommil</groupId>
<artifactId>jniloader</artifactId>
<version>1.1</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.13</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.13</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.13</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.13</version>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>

</dependencies>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-test-framework</artifactId>
<version>9.12.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.7</version>
<configuration>
<systemPropertyVariables>
<java.library.path>${project.build.directory}/classes</java.library.path>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy</id>
<phase>compile</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.nvidia.cuvs</groupId>
<artifactId>cuvs-java-internal</artifactId>
<version>24.12</version>
<type>so</type>
<overWrite>false</overWrite>
<outputDirectory>
${project.build.directory}/classes</outputDirectory>
<destFileName>libcuvs_java.so</destFileName>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.7</version>
<configuration>
<systemPropertyVariables>
<java.library.path>${project.build.directory}/classes</java.library.path>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy</id>
<phase>compile</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.nvidia.cuvs</groupId>
<artifactId>cuvs-java-internal</artifactId>
<version>24.12</version>
<type>so</type>
<overWrite>false</overWrite>
<outputDirectory>
${project.build.directory}/classes</outputDirectory>
<destFileName>libcuvs_java.so</destFileName>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.4.2</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archiverConfig>
<duplicateBehavior>add</duplicateBehavior>
</archiverConfig>
</configuration>
<executions>
<execution>
<id>assemble-all</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>
com.nvidia.cuvs.examples.CagraExample</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.6.2</version>
<configuration>
<excludePackageNames>com.nvidia.cuvs.examples,com.nvidia.cuvs.panama</excludePackageNames>
<reportOutputDirectory>${project.build.directory}</reportOutputDirectory>
</configuration>
</plugin>
</plugins>
</build>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.4.2</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archiverConfig>
<duplicateBehavior>add</duplicateBehavior>
</archiverConfig>
</configuration>
<executions>
<execution>
<id>assemble-all</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>
com.nvidia.cuvs.examples.CagraExample</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.6.2</version>
<configuration>
<excludePackageNames>
com.nvidia.cuvs.examples,com.nvidia.cuvs.panama</excludePackageNames>
<reportOutputDirectory>${project.build.directory}</reportOutputDirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>
14 changes: 14 additions & 0 deletions java/cuvs-java/src/main/java/com/nvidia/cuvs/CuVSResources.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class CuVSResources {

private final MethodHandle createResourceMethodHandle;
private final MemorySegment memorySegment;
private final MethodHandle getGpuDetailsHandle;

/**
* Constructor that allocates the resources needed for cuVS
Expand All @@ -61,6 +62,10 @@ public CuVSResources() throws Throwable {
MemorySegment returnValueMemorySegment = arena.allocate(returnValueMemoryLayout);

memorySegment = (MemorySegment) createResourceMethodHandle.invokeExact(returnValueMemorySegment);
getGpuDetailsHandle = linker.downcallHandle(
libcuvsNativeLibrary.find("get_gpu_details")
.orElseThrow(() -> new IllegalStateException("get_gpu_details not found in library")),
FunctionDescriptor.of(ValueLayout.JAVA_INT, ValueLayout.ADDRESS, ValueLayout.JAVA_INT, ValueLayout.JAVA_INT));
}

/**
Expand All @@ -78,4 +83,13 @@ protected MemorySegment getMemorySegment() {
protected SymbolLookup getLibcuvsNativeLibrary() {
return libcuvsNativeLibrary;
}

/**
* Gets the MethodHandle for the `get_gpu_details` function.
*
* @return MethodHandle for `get_gpu_details`
*/
public MethodHandle getGpuDetailsHandle() {
return getGpuDetailsHandle;
}
}
30 changes: 30 additions & 0 deletions java/cuvs-java/src/main/java/com/nvidia/cuvs/common/GpuDetail.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.nvidia.cuvs.common;

public class GpuDetail {
private final String name;
private final long totalMemory;
private final long freeMemory;

public GpuDetail(String name, long totalMemory, long freeMemory) {
this.name = name;
this.totalMemory = totalMemory;
this.freeMemory = freeMemory;
}

public String getName() {
return name;
}

public long getTotalMemory() {
return totalMemory;
}

public long getFreeMemory() {
return freeMemory;
}

@Override
public String toString() {
return "GpuDetail{" + "name='" + name + '\'' + ", totalMemory=" + totalMemory + ", freeMemory=" + freeMemory + '}';
}
}
34 changes: 34 additions & 0 deletions java/cuvs-java/src/main/java/com/nvidia/cuvs/common/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,50 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.foreign.Arena;
import java.lang.foreign.FunctionDescriptor;
import java.lang.foreign.Linker;
import java.lang.foreign.MemoryLayout;
import java.lang.foreign.MemoryLayout.PathElement;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.ValueLayout;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.VarHandle;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.nvidia.cuvs.CuVSResources;
import com.nvidia.cuvs.panama.GpuDetailLayout;

public class Util {

private static final Logger log = LoggerFactory.getLogger(Util.class);

public static GpuDetail[] getGpuDetails(CuVSResources resources, int maxGpus, int maxDetailLength) {
try {

MemorySegment detailsSegment = resources.arena.allocate(maxGpus * maxDetailLength);

int gpuCount = (int) resources.getGpuDetailsHandle().invoke(detailsSegment, maxGpus, maxDetailLength);

if (gpuCount < 0) {
throw new RuntimeException("Failed to retrieve GPU details");
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary to log anything here. Let's not be do judgemental here, this function just returns the ground reality.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will remove this log.


GpuDetail[] gpuDetails = new GpuDetail[gpuCount];
for (int i = 0; i < gpuCount; i++) {
MemorySegment structSegment = detailsSegment.asSlice(i * GpuDetailLayout.LAYOUT.byteSize(),
GpuDetailLayout.LAYOUT.byteSize());
gpuDetails[i] = GpuDetailLayout.fromMemorySegment(structSegment);
}

return gpuDetails;
} catch (Throwable e) {
throw new RuntimeException("Failed to invoke get_gpu_details", e);
}
}

/**
* A utility method for getting an instance of {@link MemorySegment} for a
* {@link String}.
Expand Down
Loading
Loading