代码之家  ›  专栏  ›  技术社区  ›  ksm001

CUDA和Thrust库:将.cuh.cu和.cpp文件与-std=c++0x一起使用时出现问题

  •  2
  • ksm001  · 技术社区  · 13 年前

    我想要一个 .cuh 文件,我可以在其中声明内核函数和主机函数。这些功能的实现将在 .cu 文件实施将包括使用 Thrust 图书馆

    在 main.cpp 文件我想使用 .cu 文件假设我们有这样的东西:

    myFunctions.cuh

    #include <thrust/sort.h>
    #include <thrust/device_vector.h>
    #include <thrust/remove.h>
    #include <thrust/host_vector.h>
    #include <iostream>
    
    __host__ void show();
    

    myFunctions.cu

    #include "myFunctions.cuh"
    
    __host__ void show(){
       std::cout<<"test"<<std::endl;
    }
    

    主.cpp

    #include "myFunctions.cuh"
    
    int main(void){
    
        show();
    
        return 0;
    }
    

    如果我通过这样做进行编译:

    nvcc myFunctions.cu main.cpp -O3
    

    然后通过键入 ./a.out

    这个 test 将打印文本。

    但是,如果我决定将 -std=c++0x 通过使用以下命令:

    nvcc myFunctions.cu main.cpp -O3 --compiler-options "-std=c++0x"
    

    我犯了很多错误,其中一些错误如下:

    /usr/include/c++/4.6/x86_64-linux-gnu/./bits/c++config.h(159): error: identifier "nullptr" is undefined
    
    /usr/include/c++/4.6/x86_64-linux-gnu/./bits/c++config.h(159): error: expected a ";"
    
    /usr/include/c++/4.6/bits/exception_ptr.h(93): error: incomplete type is not allowed
    
    /usr/include/c++/4.6/bits/exception_ptr.h(93): error: expected a ";"
    
    /usr/include/c++/4.6/bits/exception_ptr.h(112): error: expected a ")"
    
    /usr/include/c++/4.6/bits/exception_ptr.h(114): error: expected a ">"
    
    /usr/include/c++/4.6/bits/exception_ptr.h(114): error: identifier "__o" is undefined
    

    这些错误意味着什么?我如何避免这些错误?

    提前谢谢

    1 回复  |  直到 13 年前
        1
  •  5
  •   Community Mohan Dere    9 年前

    如果你看 this specific answer ,你会看到用户正在用你正在使用的开关编译一个空的虚拟应用程序,并得到一些完全相同的错误。如果将该开关的使用限制为编译.cpp文件,则可能会得到更好的结果:

    myFunctions.h:

    void show();
    

    myFunctions.cu:

    #include <thrust/sort.h>
    #include <thrust/device_vector.h>
    #include <thrust/remove.h>
    #include <thrust/host_vector.h>
    #include <thrust/sequence.h>
    #include <iostream>
    
    #include "myFunctions.h"
    
    void show(){
      thrust::device_vector<int> my_ints(10);
      thrust::sequence(my_ints.begin(), my_ints.end());
      std::cout<<"my_ints[9] = "<< my_ints[9] << std::endl;
    }
    

    主要.cpp:

    #include "myFunctions.h"
    
    int main(void){
    
        show();
    
        return 0;
    }
    

    生成:

    g++ -c -std=c++0x main.cpp
    nvcc -arch=sm_20 -c myFunctions.cu 
    g++ -L/usr/local/cuda/lib64 -lcudart -o test main.o myFunctions.o