【问题标题】:Segmentation fault when using cudaMemcpy使用 cudaMemcpy 时出现分段错误
【发布时间】:2020-02-12 13:53:02
【问题描述】:

我正在尝试使用 cudaMemcpy 到 std::vector::data 到设备内核的数组,它给出了设置错误错误。我的做法是:

  cudaMemcpy(d_x, vx.data(), N*sizeof(float), cudaMemcpyHostToDevice);

其中 vx 是向量。以下是完整的示例。任何有关问题所在的提示将不胜感激。

#include <iostream>
#include <math.h>
#include <vector>

using namespace std;

// Kernel function to add the elements of two arrays
__global__
void add(int n, float *x, float *y)
{
    int i = blockIdx.x*blockDim.x + threadIdx.x;
    if(i < n) {
        y[i] = x[i] + y[i];
    }
}


int main(void)
{
    int N = 1<<10;
    float *d_x = NULL, *d_y = NULL;
    cudaMalloc((void **)&d_x, sizeof(float)*N);
    cudaMalloc((void **)&d_y, sizeof(float)*N);

    // Allocate Unified Memory – accessible from CPU or GPU
    vector<float> vx;
    vector<float> vy;

    // initialize x and y arrays on the host
    for (int i = 0; i < N; i++) {
        vx.push_back(1.0f);
        vy.push_back(2.0f);
    }

    cudaMemcpy(d_x, vx.data(), N*sizeof(float), cudaMemcpyHostToDevice);
    cudaMemcpy(d_y, vy.data(), N*sizeof(float), cudaMemcpyHostToDevice);
    //
    int blockSize;   // The launch configurator returned block size
    int minGridSize; // The minimum grid size needed to achieve the
    // maximum occupancy for a full device launch
    int gridSize;    // The actual grid size needed, based on input size

    cudaOccupancyMaxPotentialBlockSize( &minGridSize, &blockSize, add, 0, N);
    // Round up according to array size
    gridSize = (N + blockSize - 1) / blockSize;

    cout<<"blockSize: "<<blockSize<<" minGridSize: "<<minGridSize<<" gridSize: "<<gridSize<<endl;

    // calculate theoretical occupancy
    int maxActiveBlocks;
    cudaOccupancyMaxActiveBlocksPerMultiprocessor( &maxActiveBlocks, add, blockSize, 0);

    int device;
    cudaDeviceProp props;
    cudaGetDevice(&device);
    cudaGetDeviceProperties(&props, device);

    float occupancy = (maxActiveBlocks * blockSize / props.warpSize) /
        (float)(props.maxThreadsPerMultiProcessor /
                props.warpSize);

    printf("Launched blocks of size %d. Theoretical occupancy: %f\n",
            blockSize, occupancy);


    // Run kernel on 1M elements on the GPU
    add<<<gridSize, blockSize>>>(N, d_x, d_y);

    // Wait for GPU to finish before accessing on host
    cudaDeviceSynchronize();

    // Check for errors (all values should be 3.0f)
    float maxError = 0.0f;

    for (int i = 0; i < N; i++) {
        maxError = fmax(maxError, fabs(d_y[i]-3.0f));
    }
    std::cout << "Max error: " << maxError << std::endl;

    // Free memory
    cudaFree(d_x);
    cudaFree(d_y);

    return 0;
}


blockSize: 1024 minGridSize: 16 gridSize: 1
Launched blocks of size 1024. Theoretical occupancy: 1.000000
Segmentation fault (core dumped)

【问题讨论】:

    标签: c++ vector cuda


    【解决方案1】:

    问题出在这里:

    for (int i = 0; i < N; i++) {
       maxError = fmax(maxError, fabs(d_y[i]-3.0f));
                                      ^^^^^^
    }
    

    原因是您不能取消引用主机上的设备指针。

    解决方案是将设备内存复制到主机,类似于您对主机到设备所做的操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-04
      • 2012-05-05
      • 2016-04-26
      • 2017-06-06
      • 2017-08-26
      相关资源
      最近更新 更多