在CUDA中编译和链接多个文件

时间:2018-08-31 23:05:29

标签: c++ cuda

在使用CUDA工具包时,我在Visual Studio中遇到了问题。我想在多个.cu文件中编写代码。但是,它总是给我造成建筑故障。我对此进行了大量搜索,但找不到解决方案。这是我尝试过的:

  1. 确保项目类型为CUDA C / C ++而非C ++
  2. 设置可重定位的通用代码rcd + = true(大多数帖子的解决方案)
  3. 将主机运行时库更改为多线程调试(/ Mtd)

但是它们都不起作用,我遇到了同样的错误。

我的端点是创建一个可以在主机和设备上使用的矩阵类。为了使问题更简单,我仅将构造函数和打印函数保留在类中(但有相同的错误)。这是矩阵的标题:

#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <iostream>
#ifndef __alldev__
#define __alldev__ __host__ __device__
#endif
template <class T>
class Matrix {
    T* m = NULL;
    T* dev_m = NULL;
public:
    int rowNum = 0;
    int colNum = 0;
    Matrix(T* data, int rowNum, int colNum, int location = 0);
    void print();
    __alldev__ int ind(int i, int j) {
        return (i - 1) + (j - 1)*colNum;
    }
};

这是矩阵的.cu文件:

#include "Matrix.cuh"
template <class T>
Matrix<T>::Matrix(T* data, int rowNum, int colNum, int location = 0) {
    this->rowNum = rowNum;
    this->colNum = colNum;
    m = new T[rowNum*colNum];
    for (int i = 1; i <= rowNum; i++) {
        for (int j = 1; j <= colNum; j++) {
            m[ind(i, j)] = data[ind(i, j)];
        }
    }
}

template <class T>
void Matrix<T>::print() {
    for (int i = 1; i <= rowNum; i++) {
        for (int j = 1; j <= colNum; j++) {
            std::cout << m[ind(i, j)] << "  ";
        }
        std::cout << std::endl;
    }
}

主文件:

#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
#include "Matrix.cuh"
int main()
{
    int N = 10;
    float * data = new float[N];
    for (int i = 0; i < N; i++) {
        data[i] = i;
    }
    Matrix<float> test(data,N,1);
    test.print();
}

以上代码在同一文件中时可以正常工作,但在不同文件中时则无法工作。最奇怪的是,如果我注释掉内核函数调用,则错误消息将被更改。我完全不知道这一点。

这是VS的正常水平输出:

1>------ Build started: Project: test1, Configuration: Debug x64 ------
1>Build started 8/31/2018 7:27:07 PM.
1>Target InitializeBuildStatus:
1>  Creating "x64\Debug\test1.tlog\unsuccessfulbuild" because "AlwaysCreate" was specified.
1>Target AddCudaCompileDeps:
1>  Skipping target "AddCudaCompileDeps" because all output files are up-to-date with respect to the input files.
1>Target AddCudaCompileDeps:
1>  Skipping target "AddCudaCompileDeps" because all output files are up-to-date with respect to the input files.
1>Target AddCudaCompilePropsDeps:
1>  Skipping target "AddCudaCompilePropsDeps" because all output files are up-to-date with respect to the input files.
1>Target AddCudaCompilePropsDeps:
1>  Skipping target "AddCudaCompilePropsDeps" because all output files are up-to-date with respect to the input files.
1>Target AddCudaCompilePropsDeps:
1>  Skipping target "AddCudaCompilePropsDeps" because all output files are up-to-date with respect to the input files.
1>Target CudaBuild:
1>  Target CudaBuildCore:
1>    Compiling CUDA source file kernel.cu...
1>    Target CudaBuildCore:
1>      Skipping target "CudaBuildCore" because all output files are up-to-date with respect to the input files.
1>    Target CudaBuildCore:
1>      Skipping target "CudaBuildCore" because all output files are up-to-date with respect to the input files.
1>
1>    C:\Users\wangj\documents\visual studio 2017\Projects\test1\test1>"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.1\bin\nvcc.exe" -gencode=arch=compute_30,code=\"sm_30,compute_30\" --use-local-env --cl-version 2017 -ccbin "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.10.25017\bin\HostX86\x64" -x cu -rdc=true -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.1\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.1\include"  -G   --keep-dir x64\Debug -maxrregcount=0  --machine 64 --compile -cudart static  -g   -DWIN32 -DWIN64 -D_DEBUG -D_CONSOLE -D_MBCS -Xcompiler "/EHsc /W3 /nologo /Od /FS /Zi /RTC1 /MDd " -o x64\Debug\kernel.cu.obj "C:\Users\wangj\documents\visual studio 2017\Projects\test1\test1\kernel.cu"
1>    c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h : warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>    c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(838): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>    c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(1772): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>    c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(2628): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>    c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(3477): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>    c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(4417): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>    c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(5319): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>    c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(6229): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>    c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(7104): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>    c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(7914): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>    c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt/device_functions.h : warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>    c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt/device_functions.h(776): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>    c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt/device_functions.h(1636): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>    c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\device_double_functions.h : warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>    c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\sm_20_intrinsics.h : warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>    c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\sm_20_intrinsics.h(925): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>    c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h : warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>    c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(838): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>    c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(1772): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>    c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(2628): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>    c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(3477): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>    c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(4417): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>    c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(5319): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>    c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(6229): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>    c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(7104): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>    c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(7914): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>    c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt/device_functions.h : warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>    c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt/device_functions.h(776): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>    c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt/device_functions.h(1636): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>    c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\device_double_functions.h : warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>    c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\sm_20_intrinsics.h : warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>    c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\sm_20_intrinsics.h(925): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>    kernel.cu
1>    C:/Users/wangj/documents/visual studio 2017/Projects/test1/test1/kernel.cu(16): warning C4244: '=': conversion from 'int' to 'float', possible loss of data
1>    c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h : warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>    c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(838): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>    c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(1772): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>    c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(2628): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>    c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(3477): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>    c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(4417): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>    c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(5319): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>    c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(6229): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>    c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(7104): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>    c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(7914): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>  Done building target "CudaBuildCore" in project "test1.vcxproj".
1>
1>  Done building project "test1.vcxproj".
1>Target CudaLink:
1>  C:\Users\wangj\documents\visual studio 2017\Projects\test1\test1>"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.1\bin\nvcc.exe" -dlink -o x64\Debug\test1.device-link.obj -Xcompiler "/EHsc /W3 /nologo /Od /Zi /RTC1 /MDd " -L"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.1\lib\x64" cudart.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib  -gencode=arch=compute_30,code=sm_30 -G --machine 64 x64\Debug\kernel.cu.obj x64\Debug\Matrix.cu.obj x64\Debug\Matrix.cuh.obj
1>  cudart.lib
1>  kernel32.lib
1>  user32.lib
1>  gdi32.lib
1>  winspool.lib
1>  comdlg32.lib
1>  advapi32.lib
1>  shell32.lib
1>  ole32.lib
1>  oleaut32.lib
1>  uuid.lib
1>  odbc32.lib
1>  odbccp32.lib
1>  kernel.cu.obj
1>  Matrix.cu.obj
1>  Matrix.cuh.obj
1>Target Link:
1>  kernel.cu.obj : error LNK2019: unresolved external symbol "public: __cdecl Matrix<float>::Matrix<float>(float *,int,int,int)" (??0?$Matrix@M@@QEAA@PEAMHHH@Z) referenced in function main
1>  kernel.cu.obj : error LNK2019: unresolved external symbol "public: void __cdecl Matrix<float>::print(void)" (?print@?$Matrix@M@@QEAAXXZ) referenced in function main
1>  C:\Users\wangj\documents\visual studio 2017\Projects\test1\x64\Debug\test1.exe : fatal error LNK1120: 2 unresolved externals
1>Done building target "Link" in project "test1.vcxproj" -- FAILED.
1>
1>Done building project "test1.vcxproj" -- FAILED.
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:06.70
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

如果有人可以提出任何建议,我将不胜感激。

0 个答案:

没有答案