无法将信号量从Vulkan导入Cuda

无法将信号量从Vulkan导入Cuda,cuda,interop,semaphore,vulkan,Cuda,Interop,Semaphore,Vulkan,我正在尝试导入Vulkan信号量,以供CUDA在windows平台上使用,但始终会得到一个cudaErrorInvalidValue错误。我想不出是什么引起了这个问题 考虑到我已经有了Vulkan信号量对象的句柄,Cuda端位于DLL1,如下所示: void test_createCudaImportSemaphor(HANDLE handle) { cudaExternalSemaphoreHandleDesc externalSemaphoreHandleDesc; ext

我正在尝试导入Vulkan信号量,以供CUDA在windows平台上使用,但始终会得到一个cudaErrorInvalidValue错误。我想不出是什么引起了这个问题

考虑到我已经有了Vulkan信号量对象的句柄,Cuda端位于DLL1,如下所示:

 void test_createCudaImportSemaphor(HANDLE handle)
{
    cudaExternalSemaphoreHandleDesc externalSemaphoreHandleDesc;
    externalSemaphoreHandleDesc.flags = 0;
    externalSemaphoreHandleDesc.type = cudaExternalSemaphoreHandleTypeOpaqueWin32;
    externalSemaphoreHandleDesc.handle.win32.handle = handle;

    cudaExternalSemaphore_t cudaExternalSemaphore;
    cudaError_t err = cudaImportExternalSemaphore(&cudaExternalSemaphore, &externalSemaphoreHandleDesc);
    ////////////////////////////////////////////////
    // err= 1 (cudaErrorInvalidValue )    why ????
    ////////////////////////////////////////////////

    // destory
    err = cudaDestroyExternalSemaphore(cudaExternalSemaphore);
}
void test_exportVulkanSemphoreToCuda(vk::Device device)
{
    // create semaphore that can be exported (windows platform)
    vk::ExportSemaphoreWin32HandleInfoKHR exportSemaphoreWin32HandleInfoKHR;
    WindowsSecurityAttributes cWinSecurityAttributes; // taken from cuda samples 02_graphics
    exportSemaphoreWin32HandleInfoKHR.pAttributes = &cWinSecurityAttributes;
    exportSemaphoreWin32HandleInfoKHR.dwAccess = DXGI_SHARED_RESOURCE_READ | DXGI_SHARED_RESOURCE_WRITE;

    vk::ExportSemaphoreCreateInfo exportSemaphoreCreateInfo;
    exportSemaphoreCreateInfo.pNext = &exportSemaphoreWin32HandleInfoKHR;
    exportSemaphoreCreateInfo.handleTypes = vk::ExternalSemaphoreHandleTypeFlagBits::eOpaqueWin32;

    vk::SemaphoreCreateInfo semaphoreCreateInfo;
    semaphoreCreateInfo.pNext = &exportSemaphoreCreateInfo;
    vk::UniqueSemaphore upSemaphore = device.createSemaphoreUnique(semaphoreCreateInfo);

    // get semaphore windows handle 
    vk::SemaphoreGetWin32HandleInfoKHR semaphoreGetWin32HandleInfoKHR = {};
    semaphoreGetWin32HandleInfoKHR.semaphore = upSemaphore.get();
    semaphoreGetWin32HandleInfoKHR.handleType = vk::ExternalSemaphoreHandleTypeFlagBits::eOpaqueWin32;
    HANDLE handle = device.getSemaphoreWin32HandleKHR(semaphoreGetWin32HandleInfoKHR);

    // Now try to import sempahore to cuda
    test_createCudaImportSemaphor(handle);

    CloseHandle(handle);
}
DLL2中测试vulkan信号量的创建并调用
test\u createCudaImportSemaphor
以导入cuda,如下所示:

 void test_createCudaImportSemaphor(HANDLE handle)
{
    cudaExternalSemaphoreHandleDesc externalSemaphoreHandleDesc;
    externalSemaphoreHandleDesc.flags = 0;
    externalSemaphoreHandleDesc.type = cudaExternalSemaphoreHandleTypeOpaqueWin32;
    externalSemaphoreHandleDesc.handle.win32.handle = handle;

    cudaExternalSemaphore_t cudaExternalSemaphore;
    cudaError_t err = cudaImportExternalSemaphore(&cudaExternalSemaphore, &externalSemaphoreHandleDesc);
    ////////////////////////////////////////////////
    // err= 1 (cudaErrorInvalidValue )    why ????
    ////////////////////////////////////////////////

    // destory
    err = cudaDestroyExternalSemaphore(cudaExternalSemaphore);
}
void test_exportVulkanSemphoreToCuda(vk::Device device)
{
    // create semaphore that can be exported (windows platform)
    vk::ExportSemaphoreWin32HandleInfoKHR exportSemaphoreWin32HandleInfoKHR;
    WindowsSecurityAttributes cWinSecurityAttributes; // taken from cuda samples 02_graphics
    exportSemaphoreWin32HandleInfoKHR.pAttributes = &cWinSecurityAttributes;
    exportSemaphoreWin32HandleInfoKHR.dwAccess = DXGI_SHARED_RESOURCE_READ | DXGI_SHARED_RESOURCE_WRITE;

    vk::ExportSemaphoreCreateInfo exportSemaphoreCreateInfo;
    exportSemaphoreCreateInfo.pNext = &exportSemaphoreWin32HandleInfoKHR;
    exportSemaphoreCreateInfo.handleTypes = vk::ExternalSemaphoreHandleTypeFlagBits::eOpaqueWin32;

    vk::SemaphoreCreateInfo semaphoreCreateInfo;
    semaphoreCreateInfo.pNext = &exportSemaphoreCreateInfo;
    vk::UniqueSemaphore upSemaphore = device.createSemaphoreUnique(semaphoreCreateInfo);

    // get semaphore windows handle 
    vk::SemaphoreGetWin32HandleInfoKHR semaphoreGetWin32HandleInfoKHR = {};
    semaphoreGetWin32HandleInfoKHR.semaphore = upSemaphore.get();
    semaphoreGetWin32HandleInfoKHR.handleType = vk::ExternalSemaphoreHandleTypeFlagBits::eOpaqueWin32;
    HANDLE handle = device.getSemaphoreWin32HandleKHR(semaphoreGetWin32HandleInfoKHR);

    // Now try to import sempahore to cuda
    test_createCudaImportSemaphor(handle);

    CloseHandle(handle);
}

正如@talonmies所评论的,实际上,结构cudaExternalSemaphoreHandleDesc实际上并没有使用所示的代码完全初始化。在将结构初始化为零时,问题得到解决:

 cudaExternalSemaphoreHandleDesc externalSemaphoreHandleDesc = {};     

memset或完全初始化
cudaExternalSemaphoreHandleDesc
。您有未初始化的成员,这些成员可能持有无效的非零值,并破坏了所有内容