【问题标题】:CUDA __device__ Unresolved extern function [duplicate]CUDA __device__未解析的外部函数[重复]
【发布时间】:2015-06-23 23:20:47
【问题描述】:

我正在尝试了解如何在单独的头文件中解耦 CUDA __device__ 代码。

我有三个文件。

文件:1:int2.cuh

#ifndef INT2_H_
#define INT2_H_

#include "cuda.h"
#include "cuda_runtime.h"
#include "device_launch_parameters.h"

__global__ void kernel();
__device__ int k2(int k);

int launchKernel(int dim);

#endif /* INT2_H_ */

文件 2:int2.cu

#include "int2.cuh"
#include "cstdio"

__global__ void kernel() {
    int tid = threadIdx.x;
    printf("%d\n", k2(tid));
}

__device__ int k2(int i) {
    return i * i;
}

int launchKernel(int dim) {
    kernel<<<1, dim>>>();
    cudaDeviceReset();
    return 0;
}

文件 3:CUDASample.cu

include <stdio.h>
#include <stdlib.h>
#include "int2.cuh"
#include "iostream"

using namespace std;

static const int WORK_SIZE = 256;

__global__ void sampleCuda() {
    int tid = threadIdx.x;
//    printf("%d\n", k2(tid)); //Can not call k2
    printf("%d\n", tid * tid);
}

int main(void) {

    int var;
    var = launchKernel(16);

    kernel<<<1, 16>>>();
    cudaDeviceReset();

    sampleCuda<<<1, 16>>>();
    cudaDeviceReset();

    return 0;
}

代码工作文件。我可以调用sampleCuda()内核(在同一个文件中),调用C函数launchKernel()(在其他文件中),直接调用kernel()(在其他文件中)。

我面临的问题是从sampleCuda() 内核调用__device__ 函数。然后它显示以下错误。但是,kernel() 中可以调用相同的函数。

10:58:11 **** Incremental Build of configuration Debug for project CUDASample ****
make all 
Building file: ../src/CUDASample.cu
Invoking: NVCC Compiler
/Developer/NVIDIA/CUDA-6.5/bin/nvcc -G -g -O0 -gencode arch=compute_20,code=sm_20  -odir "src" -M -o "src/CUDASample.d" "../src/CUDASample.cu"
/Developer/NVIDIA/CUDA-6.5/bin/nvcc -G -g -O0 --compile --relocatable-device-code=false -gencode arch=compute_20,code=compute_20 -gencode arch=compute_20,code=sm_20  -x cu -o  "src/CUDASample.o" "../src/CUDASample.cu"
../src/CUDASample.cu(18): warning: variable "var" was set but never used

../src/CUDASample.cu(8): warning: variable "WORK_SIZE" was declared but never referenced

../src/CUDASample.cu(18): warning: variable "var" was set but never used

../src/CUDASample.cu(8): warning: variable "WORK_SIZE" was declared but never referenced

ptxas fatal   : Unresolved extern function '_Z2k2i'
make: *** [src/CUDASample.o] Error 255

10:58:14 Build Finished (took 2s.388ms)

【问题讨论】:

    标签: c++ c cuda linker-errors


    【解决方案1】:

    问题是您在与调用它的__global__ 不同的编译单元中定义了一个__device__ 函数。您需要通过添加-dc 标志来明确启用可重定位设备代码模式,或者将您的定义移动到同一单元。

    来自nvcc 文档:

    --device-c|-dc 将每个 .c/.cc/.cpp/.cxx/.cu 输入文件编译为包含可重定位设备代码的目标文件。它相当于 --relocatable-device-code=true --compile.

    更多信息请参见Separate Compilation and Linking of CUDA C++ Device Code

    【讨论】:

    • 我一直在使用 Nsight 进行构建过程。 --relocatable-device-code=false 在那里设置。我修改了它,但它不能立即工作。我会再做一些实验。
    • 谢谢,它可以修改 makefile 脚本。
    • nsight EE 有一个项目选项,您可以在创建项目时使用该选项选择“单独编译”项目类型。如果以这种方式创建项目,可能比直接修改 makefile 脚本更容易。
    • 是的,我刚刚找到了。
    猜你喜欢
    • 2014-12-02
    • 2013-06-15
    • 2020-01-31
    • 2016-05-10
    • 2019-09-03
    • 2010-11-12
    • 2015-05-04
    • 2021-09-24
    • 2012-10-28
    相关资源
    最近更新 更多