-
Notifications
You must be signed in to change notification settings - Fork 206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Devicelist method to Tensorflow.java #171
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,21 +15,20 @@ | |
|
||
package org.tensorflow; | ||
|
||
import static org.tensorflow.internal.c_api.global.tensorflow.TF_DeleteBuffer; | ||
import static org.tensorflow.internal.c_api.global.tensorflow.TF_DeleteLibraryHandle; | ||
import static org.tensorflow.internal.c_api.global.tensorflow.TF_GetAllOpList; | ||
import static org.tensorflow.internal.c_api.global.tensorflow.TF_GetOpList; | ||
import static org.tensorflow.internal.c_api.global.tensorflow.TF_LoadLibrary; | ||
import static org.tensorflow.internal.c_api.global.tensorflow.TF_Version; | ||
|
||
import com.google.protobuf.InvalidProtocolBufferException; | ||
import org.bytedeco.javacpp.BytePointer; | ||
import org.bytedeco.javacpp.PointerScope; | ||
import org.tensorflow.exceptions.TensorFlowException; | ||
import org.tensorflow.internal.c_api.TF_Buffer; | ||
import org.tensorflow.internal.c_api.TF_Library; | ||
import org.tensorflow.internal.c_api.TF_Status; | ||
import org.tensorflow.internal.c_api.*; | ||
import org.tensorflow.proto.framework.OpList; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.tensorflow.internal.c_api.global.tensorflow.*; | ||
|
||
/** Static utility methods describing the TensorFlow runtime. */ | ||
public final class TensorFlow { | ||
/** Returns the version of the underlying TensorFlow runtime. */ | ||
|
@@ -103,6 +102,22 @@ private static OpList libraryOpList(TF_Library handle) { | |
} | ||
} | ||
|
||
public static List<DeviceSpec> listDevices(Optional<DeviceSpec.DeviceType> deviceType, TFE_Context ctx) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You shouldn't expose |
||
List<DeviceSpec> deviceList = new ArrayList(); | ||
TF_Status status = TF_Status.newStatus(); | ||
TF_DeviceList devices = TFE_ContextListDevices(ctx, status); | ||
for(int i = 0; i<TF_DeviceListCount(devices); i++){ | ||
BytePointer devName = TF_DeviceListName(devices,i, status); | ||
BytePointer devType = TF_DeviceListType(devices,i,status); | ||
DeviceSpec devSpec = DeviceSpec.newBuilder().deviceIndex(i).deviceType(DeviceSpec.DeviceType.valueOf(devType.getString())) | ||
.job(devName.getString()).build(); | ||
deviceList.add(devSpec); | ||
} | ||
TF_DeleteDeviceList(devices); | ||
if(deviceType.isPresent()) return deviceList; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just general comment, be careful to format your code according to Google Java Style Guide, I saw a bunch of missing spaces in the code that will fail the lint checks to pass when enabled. |
||
return deviceList.stream().filter(d -> d.deviceType().equals(deviceType.get())).collect(Collectors.toList()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Java streams tends to be slower than simple |
||
} | ||
|
||
private TensorFlow() {} | ||
|
||
/** Load the TensorFlow runtime C library. */ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No star imports in the main classes.