950M 比 970M 快?

950M faster than a 970M?

我目前正在为我的一个朋友测试一些代码,因为我可以访问一堆不同的 MXM 模块,我得到了一些奇怪的结果,对 950M 和 970M 之间的一些代码进行了基准测试。

int main(void)
{
    static const int WORK_SIZE = 65530;
    float *data = new float[WORK_SIZE];

    float time;
    cudaEvent_t start, stop;
    cudaEventCreate(&start);
    cudaEventCreate(&stop);

    for (int i = 0; i < 1000; i++)
    {
        cudaEventRecord(start, 0);

        initialize (data, WORK_SIZE);

        float *recCpu = cpuReciprocal(data, WORK_SIZE);
        float *recGpu = gpuReciprocal(data, WORK_SIZE);
        float cpuSum = std::accumulate (recCpu, recCpu+WORK_SIZE, 0.0);
        float gpuSum = std::accumulate (recGpu, recGpu+WORK_SIZE, 0.0);

        cudaEventRecord(stop, 0);
        cudaEventSynchronize(stop);
        cudaEventElapsedTime(&time, start, stop);

        /* Verify the results */
        std::cout << i << ") gpuSum = "<<gpuSum<< " cpuSum = " <<cpuSum<< " time = " <<time<< std::endl;

        delete[] recCpu;
        delete[] recGpu;
    }

    /* Free memory */
    delete[] data;

    return 0;
}

我运行两张卡都测试了3次,950M平均7秒左右,970M平均9秒左右。此外,我 运行 基准程序包含在 CUDA 工具包中,在 970 上仅获得 3GB/s H-D 速度,而在 950 上获得 10GB/s。这是在 Skylake Xeon 运行 上完成的Ubuntu 14.04 和 CUDA 7.5。任何人都可以阐明这种差异吗? cpuReciprocal 和 gpuReciprocal 的代码如下。

/**
 * CUDA kernel that computes reciprocal values for a given vector
 */
__global__ void reciprocalKernel(float *data, unsigned vectorSize) {
    unsigned idx = blockIdx.x*blockDim.x+threadIdx.x;
    if (idx < vectorSize)
        data[idx] = 1.0/data[idx];
}

/**
 * Host function that copies the data and launches the work on GPU
 */
float *gpuReciprocal(float *data, unsigned size)
{
    float *rc = new float[size];
    float *gpuData;

    CUDA_CHECK_RETURN(cudaMalloc((void **)&gpuData, sizeof(float)*size));
    CUDA_CHECK_RETURN(cudaMemcpy(gpuData, data, sizeof(float)*size, cudaMemcpyHostToDevice));

    static const int BLOCK_SIZE = 256;
    const int blockCount = (size+BLOCK_SIZE-1)/BLOCK_SIZE;
    reciprocalKernel<<<blockCount, BLOCK_SIZE>>> (gpuData, size);

    CUDA_CHECK_RETURN(cudaMemcpy(rc, gpuData, sizeof(float)*size, cudaMemcpyDeviceToHost));
    CUDA_CHECK_RETURN(cudaFree(gpuData));
    return rc;
}

float *cpuReciprocal(float *data, unsigned size)
{
    float *rc = new float[size];
    for (unsigned cnt = 0; cnt < size; ++cnt) rc[cnt] = 1.0/data[cnt];
    return rc;
}

主机到设备的带宽由 PCI Express 性能决定。似乎 950 连接的是 PCI Express Gen 3 16x,而 970 似乎是 4x。 The hardware setup being not detailed 这是最有可能的解释。 NVIDIA 工具(例如 nvidia-smi)将为您提供此信息。 至于你的算法,最慢的部分是与主机的通信,接下来是设备内存带宽大约快 10 倍,最后是分界线。