CUDA托管内存无法与其他计算功能一起使用

时间:2019-10-25 07:28:25

标签: cuda

使用不同的计算功能进行编译时,此代码无法以相同的方式工作:

#include <cuda.h>
#include <stdio.h>

__managed__ int m;

int main() {
    printf("hi 1\n");
    m = -123;
    printf("hi 2\n");
}

具有计算能力6.0的设备:

$ nvcc main.cu -gencode arch=compute_60,code=sm_60 -rdc=true && ./a.out
hi 1
hi 2

具有7.0计算能力的设备:

$ nvcc main.cu -gencode arch=compute_60,code=sm_60 -rdc=true && ./a.out
hi 1
Segmentation fault

具有7.0计算能力的设备:

$ nvcc main.cu -gencode arch=compute_70,code=sm_70 -rdc=true && ./a.out
hi 1
hi 2

为什么在使用计算能力6.0进行构建并在具有计算能力7.0的GPU上运行时出现分段错误?

1 个答案:

答案 0 :(得分:0)

根据评论中的讨论,我有这个问题,因为在构建过程中必须使用与GPU完全相同的相同的计算功能。 我没有收到任何错误的原因是我应该手动检查它们(What is the canonical way to check for errors using the CUDA runtime API?)。

如果我扩展这段代码:

#include <cuda.h>
#include <stdio.h>

__managed__ int m;

__global__ void foo() {
    printf("from foo: %d %d\n", blockIdx.x, threadIdx.x);
}

int main() {
    foo<<<2,2>>>();
    printf("001\n");
    if (cudaPeekAtLastError() != cudaSuccess) abort();
    printf("002\n");
    if (cudaDeviceSynchronize() != cudaSuccess) abort();
    printf("hi 1\n");
    m = -123;
    printf("hi 2\n");
}

具有7.0计算能力的设备:

$ nvcc main.cu -gencode arch=compute_70,code=sm_70 -rdc=true && ./a.out
001
002
from foo: 0 0
from foo: 0 1
from foo: 1 0
from foo: 1 1
hi 1
hi 2

具有7.0计算能力的设备:

$ nvcc main.cu -gencode arch=compute_60,code=sm_60 -rdc=true && ./a.out
001
Aborted