【问题标题】:cudaErrorLaunchFailure on cudaMemcpy from device to host从设备到主机的 cudaMemcpy 上的 cudaErrorLaunchFailure
【发布时间】:2017-08-24 11:15:07
【问题描述】:

如果内核初始化后,将 bools d_unique 数组从设备复制到主机时,以下程序为何崩溃,我将不胜感激。

我的 GPU 是 Quadro K1000M(移动 - 计算能力 3.0)。我正在使用 CUDA 版本 8。

#include <iostream>

// nvcc -ccbin g++ -g -m64 -gencode arch=compute_30,code=sm_30 -o Bug Bug.cu

// Helper Functions Decl
void allocateDeviceMemory( void* devPtr , unsigned size , int lineNumber );
void copyDataToHost( void* hostPtr , void* devPtr , unsigned size , int lineNumber );
void copyDataToDevice( void* devPtr , void* hostPtr , unsigned size , int lineNumber );
void initializeDeviceMemory( void* devPtr , unsigned size , unsigned initValue , int lineNumber );

__global__ void myKernel( const ushort* __restrict__ dataPtr , const ushort* __restrict__ proxyId , bool* unique , unsigned size , const ushort dim )
{
    int N = threadIdx.x + ( blockIdx.x * blockDim.x );

    if( N < size - 1 )
    {
        unsigned offset;
        ushort countPtr = 0;
        ushort id1 = proxyId[N];
        ushort id2 = proxyId[N + 1];

        for( ushort i = 0; i < dim; ++i )
        {
            if( dataPtr[offset + id1] == dataPtr[offset + id2] ) ++countPtr;
            offset += size;
        }

        unique[N + 1] = ( countPtr != dim );    // No crash if commented out
    }
}

int main(int argc, char** argv)
{
    ushort dim = 2;
    static const unsigned SIZE = 10;

    ushort h_proxyId[SIZE] = { 6 , 3 , 1 , 0 , 7 , 4 , 2 , 8 , 5 , 9 };


    ushort h_dataPtr[ 2 * SIZE] = { 1 , 1 , 2 , 1 , 2 , 3 , 1 , 2 , 3 , 4 ,
                                    4 , 3 , 3 , 2 , 2 , 2 , 1 , 1 , 1 , 1 };

    ushort* d_proxyId = 0;
    ushort* d_dataPtr = 0;

    bool* d_unique = 0; 
    bool* h_unique = new bool[SIZE];

    allocateDeviceMemory( &d_unique , SIZE , __LINE__ );    
    allocateDeviceMemory( &d_proxyId , SIZE * sizeof(ushort) , __LINE__ );
    allocateDeviceMemory( &d_dataPtr , SIZE * sizeof(ushort) * 2 , __LINE__ );

    copyDataToDevice( d_proxyId , h_proxyId , SIZE * sizeof(ushort) , __LINE__ );
    copyDataToDevice( d_dataPtr , h_dataPtr , SIZE * sizeof(ushort) * 2 , __LINE__ );

    initializeDeviceMemory( d_unique , SIZE , 1 , __LINE__ );

    myKernel<<<1,SIZE>>>( d_dataPtr , d_proxyId , d_unique , SIZE , dim );  // No crash if commented out

    copyDataToHost( h_unique , d_unique , SIZE , __LINE__ );    // Crashes here

    return 0;
}

// Helper Functions Impl
void allocateDeviceMemory( void* devPtr , unsigned size , int lineNumber )
{
    cudaError_t error = cudaMalloc( (void**) devPtr , size );
    if( error != cudaSuccess )
    {
        std::cout << "[Line " << lineNumber << " -- Error " << error << " : Unable to allocate device memory] " << cudaGetErrorString( error ) << std::endl;
        exit(-1);
    }
}

void initializeDeviceMemory( void* devPtr , unsigned size , unsigned initValue , int lineNumber  )
{
    cudaError_t error = cudaMemset( devPtr , initValue , size );
    if( error != cudaSuccess )
    {
        std::cout << "[Line " << lineNumber << " -- Error " << error << " : Unable to initialize device memory to default value] " << cudaGetErrorString( error ) << std::endl;
        exit(-1);
    }        
}

void copyDataToHost( void* hostPtr , void* devPtr , unsigned size , int lineNumber  )
{
    cudaError_t error = cudaMemcpy( hostPtr , devPtr , size , cudaMemcpyDeviceToHost );
    if( error != cudaSuccess )
    {
        std::cout << "[Line " << lineNumber << " -- Error " << error << " : Unable to copy device data to host memory] " << cudaGetErrorString( error ) << std::endl;
        exit(-1);
    }
}

void copyDataToDevice( void* devPtr , void* hostPtr , unsigned size , int lineNumber  )
{
    cudaError_t error = cudaMemcpy( devPtr , hostPtr , size , cudaMemcpyHostToDevice );
    if( error != cudaSuccess )
    {
        std::cout << "[Line " << lineNumber << " -- Error " << error << " : Unable to copy host data to device memory] " << cudaGetErrorString( error ) << std::endl;
        exit(-1);
    }
}

我在二进制文件上运行了 cuda-memcheck,但它没有产生任何信息,即

ThinkPad-W530:~/tmp/CUDA/Prototype$ cuda-memcheck ./Bug
========= CUDA-MEMCHECK
[Line 59 -- Error 4 : Unable to copy device data to host memory] unspecified launch failure
========= Internal error (7)
========= No CUDA-MEMCHECK results found

【问题讨论】:

  • 您永远不会将offset 初始化为内核中的已知值。
  • @RobertCrovella 你说得对,谢谢。请发表您的评论作为答案,我会将其标记为此类。

标签: cuda


【解决方案1】:

这实际上可能不是 cudaMemcpy 上的错误,而是您的内核启动。 Cuda 错误是持久的,您会从内核启动而不是内存副本中获得潜在错误。您可以在内核之后运行 cudaGetLastError() 进行验证。

未指定的启动失败特定于内核。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-09
    • 2011-09-09
    • 2021-01-25
    • 2018-02-06
    • 2011-08-24
    • 2012-05-05
    • 1970-01-01
    相关资源
    最近更新 更多