Loading src/main/java/tfhello.java +59 −6 Original line number Diff line number Diff line Loading @@ -8,6 +8,7 @@ import org.tensorflow.Operation; import org.tensorflow.framework.ConfigProto; import org.tensorflow.framework.GPUOptions; import org.tensorflow.framework.CallableOptions; import static jcuda.driver.JCudaDriver.cuCtxCreate; import static jcuda.driver.JCudaDriver.cuCtxSynchronize; Loading @@ -34,6 +35,10 @@ import jcuda.driver.CUdevice; import jcuda.driver.JCudaDriver; import jcuda.nvrtc.JNvrtc; import jcuda.runtime.cudaPointerAttributes; import jcuda.runtime.JCuda; import jcuda.runtime.cudaError; import java.nio.ByteBuffer; import java.lang.reflect.Field; Loading Loading @@ -148,6 +153,8 @@ public class tfhello{ System.out.println(TensorFlow.elphelVersion()); System.out.println("Test 4 end\n"); //callableOpts.newBuilder().putFeedDevices(key, value); try (Graph g = new Graph()) { final String value = "Hello from " + TensorFlow.version(); Loading @@ -171,18 +178,64 @@ public class tfhello{ System.out.println("Is CUDA tensor? "+String.valueOf(t.elphel_isCUDATensor())); System.out.println(t.elphelTestCUDAPointer()); long handle1; //session.makeCallable(handle1); try ( Session s = new Session(g, config.toByteArray()); Session s = new Session(g, config.toByteArray()) //s.runner().makeCallable("",handle1); //s.runner().runCallable(handle1); // Generally, there may be multiple output tensors, // all of them must be closed to prevent resource leaks. Tensor output = s.runner().fetch("array_tensor_out").feed("array_tensor_in", t).run().get(0); ){ Tensor output = s.runner().fetch("array_tensor_out").feed("array_tensor_in", t).run().get(0); System.out.println(output.numBytes()); int[] obuf = new int[output.numBytes()/Sizeof.INT]; output.copyTo(obuf); System.out.println(Arrays.toString(obuf)); // natively got GPU device name to insert into options // it's the same all the time String gpuDeviceName = s.elphelGPUDeviceName(); // that's for RunCallable() if it ever gets implemented CallableOptions callableOpts = CallableOptions.newBuilder() .addFetch("array_tensor_out:0") .addFeed("array_tensor_in:0") .putFeedDevices("array_tensor_in:0", gpuDeviceName) .build(); System.out.println(callableOpts); // GPU allocation: Tensor t3 = Tensor.elphelCreateGPUTensor(new long[]{256},DataType.INT32); //System.out.println(t2.nativeRef); long t3_gpuptr = t3.elphel_GetGPUTensorPointer(); System.out.println(String.format("0x%08x", t3_gpuptr)); CUdeviceptr ptr3 = longToCUdeviceptr(t3_gpuptr); cudaPointerAttributes attrs = new cudaPointerAttributes(); int res = JCuda.cudaPointerGetAttributes(attrs, ptr3); if (res==cudaError.cudaErrorInvalidValue) { System.out.println("Invalid pointer value"); } if (attrs.device==-1){ System.out.println("Not a CUDA device"); } System.out.println("cuda pointer attributes?! "+res); System.out.println(attrs.toString()); cuMemcpyHtoD(ptr3, Pointer.to(px_in), cuSize); cuMemcpyDtoH(Pointer.to(px_out), ptr3, cuSize); System.out.println(Arrays.toString(px_out)); // check if it a GPU pointer } /* Loading Loading
src/main/java/tfhello.java +59 −6 Original line number Diff line number Diff line Loading @@ -8,6 +8,7 @@ import org.tensorflow.Operation; import org.tensorflow.framework.ConfigProto; import org.tensorflow.framework.GPUOptions; import org.tensorflow.framework.CallableOptions; import static jcuda.driver.JCudaDriver.cuCtxCreate; import static jcuda.driver.JCudaDriver.cuCtxSynchronize; Loading @@ -34,6 +35,10 @@ import jcuda.driver.CUdevice; import jcuda.driver.JCudaDriver; import jcuda.nvrtc.JNvrtc; import jcuda.runtime.cudaPointerAttributes; import jcuda.runtime.JCuda; import jcuda.runtime.cudaError; import java.nio.ByteBuffer; import java.lang.reflect.Field; Loading Loading @@ -148,6 +153,8 @@ public class tfhello{ System.out.println(TensorFlow.elphelVersion()); System.out.println("Test 4 end\n"); //callableOpts.newBuilder().putFeedDevices(key, value); try (Graph g = new Graph()) { final String value = "Hello from " + TensorFlow.version(); Loading @@ -171,18 +178,64 @@ public class tfhello{ System.out.println("Is CUDA tensor? "+String.valueOf(t.elphel_isCUDATensor())); System.out.println(t.elphelTestCUDAPointer()); long handle1; //session.makeCallable(handle1); try ( Session s = new Session(g, config.toByteArray()); Session s = new Session(g, config.toByteArray()) //s.runner().makeCallable("",handle1); //s.runner().runCallable(handle1); // Generally, there may be multiple output tensors, // all of them must be closed to prevent resource leaks. Tensor output = s.runner().fetch("array_tensor_out").feed("array_tensor_in", t).run().get(0); ){ Tensor output = s.runner().fetch("array_tensor_out").feed("array_tensor_in", t).run().get(0); System.out.println(output.numBytes()); int[] obuf = new int[output.numBytes()/Sizeof.INT]; output.copyTo(obuf); System.out.println(Arrays.toString(obuf)); // natively got GPU device name to insert into options // it's the same all the time String gpuDeviceName = s.elphelGPUDeviceName(); // that's for RunCallable() if it ever gets implemented CallableOptions callableOpts = CallableOptions.newBuilder() .addFetch("array_tensor_out:0") .addFeed("array_tensor_in:0") .putFeedDevices("array_tensor_in:0", gpuDeviceName) .build(); System.out.println(callableOpts); // GPU allocation: Tensor t3 = Tensor.elphelCreateGPUTensor(new long[]{256},DataType.INT32); //System.out.println(t2.nativeRef); long t3_gpuptr = t3.elphel_GetGPUTensorPointer(); System.out.println(String.format("0x%08x", t3_gpuptr)); CUdeviceptr ptr3 = longToCUdeviceptr(t3_gpuptr); cudaPointerAttributes attrs = new cudaPointerAttributes(); int res = JCuda.cudaPointerGetAttributes(attrs, ptr3); if (res==cudaError.cudaErrorInvalidValue) { System.out.println("Invalid pointer value"); } if (attrs.device==-1){ System.out.println("Not a CUDA device"); } System.out.println("cuda pointer attributes?! "+res); System.out.println(attrs.toString()); cuMemcpyHtoD(ptr3, Pointer.to(px_in), cuSize); cuMemcpyDtoH(Pointer.to(px_out), ptr3, cuSize); System.out.println(Arrays.toString(px_out)); // check if it a GPU pointer } /* Loading