如何使用hook劫持cudaSetDevice并修改设备id?;

如何使用hook劫持cudaSetDevice并修改设备id?;,cuda,gpu,hook,ld-preload,Cuda,Gpu,Hook,Ld Preload,我想用hook(LD_PRELOAD)劫持cudaSetDevice并修改设备id。劫持成功后,gpu任务提交将出错 我试图劫持驱动程序api中的cuCtxCreate函数,但无法劫持它。同样的方法劫持cuDevicePrimaryCtxRetain函数,该函数可能被劫持,修改设备id时会发生错误 // cuda 9.0 cuda runtime api typedef int(*cuda_set_device_fp)(int); // define dynamic library sa

我想用hook(LD_PRELOAD)劫持cudaSetDevice并修改设备id。劫持成功后,gpu任务提交将出错

我试图劫持驱动程序api中的cuCtxCreate函数,但无法劫持它。同样的方法劫持cuDevicePrimaryCtxRetain函数,该函数可能被劫持,修改设备id时会发生错误

// cuda 9.0 cuda runtime api   
typedef int(*cuda_set_device_fp)(int);

// define dynamic library same name function
int cudaSetDevice(int device)
{
  static void *handle = NULL;
  static cuda_set_device_fp orig_cuda_set_device = NULL;

  if( !handle )
  {
    handle = dlopen("libcuda.so", RTLD_LAZY);
    orig_cuda_set_device = (cuda_set_device_fp)dlsym(handle, "cudaSetDevice");
  }
  device = 1;
  printf("oops!!! hack function invoked. device = %d\n", device);
  return cudaSetDevice(device);
}

劫持成功,将用户映射到设备0上的gpu任务,并重新映射到设备1。

在函数末尾提供的源代码中,您再次递归调用函数,不要使用修改过的设备id调用orig_cuda_set_设备。这会导致无限递归。

请尝试提供。感谢您的回答。我试图不修改设备ID,劫持成功。但是,我的目标是劫持cudaSetDevice并修改设备ID。是否可以理解动态链接库劫持的功能参数不能修改?