jcuda.runtime.JCuda.cudaSetDevice()方法的使用及代码示例

x33g5p2x  于2022-01-22 转载在 其他  
字(3.0k)|赞(0)|评价(0)|浏览(169)

本文整理了Java中jcuda.runtime.JCuda.cudaSetDevice()方法的一些代码示例,展示了JCuda.cudaSetDevice()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JCuda.cudaSetDevice()方法的具体详情如下:
包路径:jcuda.runtime.JCuda
类名称:JCuda
方法名:cudaSetDevice

JCuda.cudaSetDevice介绍

[英]Set device to be used for GPU executions.

cudaError_t cudaSetDevice ( 
int  device )

Set device to be used for GPU executions. Sets device as the current device for the calling host thread.

Any device memory subsequently allocated from this host thread using cudaMalloc(), cudaMallocPitch() or cudaMallocArray() will be physically resident on device. Any host memory allocated from this host thread using cudaMallocHost() or cudaHostAlloc() or cudaHostRegister() will have its lifetime associated with device. Any streams or events created from this host thread will be associated with device. Any kernels launched from this host thread using the <<<>>> operator or cudaLaunch() will be executed on device.

This call may be made from any host thread, to any device, and at any time. This function will do no synchronization with the previous or new device, and should be considered a very low overhead call.
Note:

Note that this function may also return error codes from previous, asynchronous launches.
[中]设置用于GPU执行的设备

cudaError_t cudaSetDevice ( 
int  device )

设置用于GPU执行的设备。将设备设置为调用主机线程的当前设备。
随后使用cudaMalloc()、CUDAMALLOCITCH()或cudaMallocArray()从此主机线程分配的任何设备内存都将物理驻留在设备上。使用cudaMallocHost()或cudaHostAlloc()或CUDAHOSTERGISTER()从此主机线程分配的任何主机内存的生存期都将与设备关联。从此主机线程创建的任何流或事件都将与设备关联。使用<>>>运算符或cudaLaunch()从此主机线程启动的任何内核都将在设备上执行。
此调用可以在任何时间从任何主机线程向任何设备发出。此函数不会与以前的设备或新设备进行同步,应视为开销非常低的调用。
注:
请注意,此函数还可能返回以前异步启动的错误代码。

代码示例

代码示例来源:origin: com.simiacryptus/mindseye

/**
 * Sets device.
 *
 * @param cudaDeviceId the cuda device id
 */
public static void setDevice(final int cudaDeviceId) {
 if (cudaDeviceId < 0) throw new IllegalArgumentException("cudaDeviceId=" + cudaDeviceId);
 if (!isThreadDeviceId(cudaDeviceId)) {
  long startTime = System.nanoTime();
  final int result = JCuda.cudaSetDevice(cudaDeviceId);
  setDevice_execution.accept((System.nanoTime() - startTime) / 1e9);
  log("cudaSetDevice", result, new Object[]{cudaDeviceId});
  CudaSystem.handle(result);
  CudaSystem.currentDeviceId.set(cudaDeviceId);
 }
}

代码示例来源:origin: com.simiacryptus/mindseye-cudnn

/**
 * Sets device.
 *
 * @param cudaDeviceId the cuda device id
 */
public static void setDevice(final int cudaDeviceId) {
 if (cudaDeviceId < 0) throw new IllegalArgumentException("cudaDeviceId=" + cudaDeviceId);
 if (!isThreadDeviceId(cudaDeviceId)) {
  long startTime = System.nanoTime();
  final int result = JCuda.cudaSetDevice(cudaDeviceId);
  setDevice_execution.accept((System.nanoTime() - startTime) / 1e9);
  log("cudaSetDevice", result, new Object[]{cudaDeviceId});
  CudaSystem.handle(result);
  CudaSystem.currentDeviceId.set(cudaDeviceId);
 }
}

代码示例来源:origin: org.nd4j/nd4j-jcublas-common

/**
 * Sync the context for the current thread.
 */
public static  void setContext() {
  JCudaDriver.cuCtxSetCurrent(ContextHolder.getInstance().getContext());
  JCuda.cudaSetDevice(ContextHolder.getInstance().getDeviceForThread());
  JCudaDriver.cuCtxSynchronize();
  JCuda.cudaDeviceSynchronize();
}

相关文章

JCuda类方法