Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ interface ScopedAccess extends AutoCloseable {
*/
ScopedAccess access();

/**
* Get the logical id of the device associated with this resources object.
* Information about the device id is immutable, so it is safe to expose it without getting {@link ScopedAccess}
* to the enclosing resources.
*/
int deviceId();

/**
* Closes this CuVSResources object and releases any resources associated with it.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public void close() {
};
}

@Override
public int deviceId() {
Comment thread
ldematte marked this conversation as resolved.
return inner.deviceId();
}

@Override
public void close() {
inner.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
package com.nvidia.cuvs.internal;

import static com.nvidia.cuvs.internal.common.Util.checkCuVSError;
import static com.nvidia.cuvs.internal.panama.headers_h.cuvsResourcesCreate;
import static com.nvidia.cuvs.internal.panama.headers_h.cuvsResourcesDestroy;
import static com.nvidia.cuvs.internal.panama.headers_h.cuvsResources_t;
import static com.nvidia.cuvs.internal.panama.headers_h.*;
import static com.nvidia.cuvs.internal.panama.headers_h_1.C_INT;

import com.nvidia.cuvs.CuVSResources;
import java.lang.foreign.Arena;
Expand All @@ -34,6 +33,7 @@ public class CuVSResourcesImpl implements CuVSResources {
private final Path tempDirectory;
private final long resourceHandle;
private final ScopedAccess access;
private final int deviceId;

/**
* Constructor that allocates the resources needed for cuVS
Expand All @@ -43,9 +43,11 @@ public CuVSResourcesImpl(Path tempDirectory) {
this.tempDirectory = tempDirectory;
try (var localArena = Arena.ofConfined()) {
var resourcesMemorySegment = localArena.allocate(cuvsResources_t);
int returnValue = cuvsResourcesCreate(resourcesMemorySegment);
checkCuVSError(returnValue, "cuvsResourcesCreate");
checkCuVSError(cuvsResourcesCreate(resourcesMemorySegment), "cuvsResourcesCreate");
this.resourceHandle = resourcesMemorySegment.get(cuvsResources_t, 0);
var deviceIdPtr = localArena.allocate(C_INT);
checkCuVSError(cuvsDeviceIdGet(resourceHandle, deviceIdPtr), "cuvsDeviceIdGet");
this.deviceId = deviceIdPtr.get(C_INT, 0);
this.access =
new ScopedAccess() {
@Override
Expand All @@ -64,6 +66,11 @@ public ScopedAccess access() {
return this.access;
}

@Override
public int deviceId() {
return this.deviceId;
}

@Override
public void close() {
synchronized (this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@
*/
package com.nvidia.cuvs.internal;

import static com.nvidia.cuvs.internal.common.Util.checkCuVSError;
import static com.nvidia.cuvs.internal.common.Util.checkCudaError;
import static com.nvidia.cuvs.internal.panama.headers_h.cudaMemGetInfo;
import static com.nvidia.cuvs.internal.panama.headers_h.cuvsDeviceIdGet;
import static com.nvidia.cuvs.internal.panama.headers_h_1.*;

import com.nvidia.cuvs.CuVSResources;
Expand All @@ -45,29 +43,24 @@ public List<GPUInfo> compatibleGPUs() throws Throwable {

@Override
public CuVSResourcesInfo getCurrentInfo(CuVSResources resources) {
try (var resourcesAccess = resources.access()) {
try (var localArena = Arena.ofConfined()) {
var deviceIdPtr = localArena.allocate(C_INT);
checkCudaError(cudaGetDevice(deviceIdPtr), "cudaGetDevice");
var currentDeviceId = deviceIdPtr.get(C_INT, 0);
try (var localArena = Arena.ofConfined()) {
var deviceIdPtr = localArena.allocate(C_INT);
checkCudaError(cudaGetDevice(deviceIdPtr), "cudaGetDevice");
var currentDeviceId = deviceIdPtr.get(C_INT, 0);

checkCuVSError(cuvsDeviceIdGet(resourcesAccess.handle(), deviceIdPtr), "cuvsDeviceIdGet");
var resourcesDeviceId = deviceIdPtr.get(C_INT, 0);

if (resourcesDeviceId != currentDeviceId) {
checkCudaError(cudaSetDevice(resourcesDeviceId), "cudaSetDevice");
}

MemorySegment freeMemoryPtr = localArena.allocate(size_t);
MemorySegment totalMemoryPtr = localArena.allocate(size_t);
checkCudaError(cudaMemGetInfo(freeMemoryPtr, totalMemoryPtr), "cudaMemGetInfo");
if (resources.deviceId() != currentDeviceId) {
checkCudaError(cudaSetDevice(resources.deviceId()), "cudaSetDevice");
}

if (resourcesDeviceId != currentDeviceId) {
checkCudaError(cudaSetDevice(currentDeviceId), "cudaSetDevice");
}
MemorySegment freeMemoryPtr = localArena.allocate(size_t);
MemorySegment totalMemoryPtr = localArena.allocate(size_t);
checkCudaError(cudaMemGetInfo(freeMemoryPtr, totalMemoryPtr), "cudaMemGetInfo");

return new CuVSResourcesInfo(freeMemoryPtr.get(size_t, 0), totalMemoryPtr.get(size_t, 0));
if (resources.deviceId() != currentDeviceId) {
checkCudaError(cudaSetDevice(currentDeviceId), "cudaSetDevice");
}

return new CuVSResourcesInfo(freeMemoryPtr.get(size_t, 0), totalMemoryPtr.get(size_t, 0));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public void close() {
};
}

@Override
public int deviceId() {
return inner.deviceId();
}

@Override
public void close() {
destroyed = true;
Expand Down
Loading