首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用CUB库编译C++?

如何用CUB库编译C++?
EN

Stack Overflow用户
提问于 2020-03-30 22:33:19
回答 1查看 968关注 0票数 0

我使用的是CUB设备函数,就像这里的示例(https://forums.developer.nvidia.com/t/cub-library/37675/2)。在上面的示例中,我能够使用.cu编译nvcc源文件。

但是,我想知道是否可以在CUB源文件中调用.cpp设备函数并编译.cpp源文件(使用nvccg++)?我知道这对thrust来说是可能的,因为这里这个例子适合我。

目前,我只是将主函数移到一个新的main.cpp文件中,并在main.cpp中包含了cub头文件,但是由于相同的错误,错误消息的一部分,我无法使用nvcc或g++编译它:

代码语言:javascript
复制
/home/xx/cub/cub/block/specializations/../../block/../util_type.cuh:261:5: error: ‘__host__’ does not name a type; did you mean ‘__loff_t’?
         __host__ __device__ __forceinline__ NullType& operator =(const T&) { return *this; }
         ^~~~~~~~
         __loff_t
/home/xx/cub/cub/block/specializations/../../block/../util_type.cuh:316:19: error: ‘short4’ was not declared in this scope
     __CUB_ALIGN_BYTES(short4, 8)
                       ^
/home/xx/cub/cub/block/specializations/../../block/../util_type.cuh:314:52: error: ISO C++ forbids declaration of ‘__align__’ with no type [-fpermissive]
         { enum { ALIGN_BYTES = b }; typedef __align__(b) t Type; };
                                                        ^
/home/xx/cub/cub/block/specializations/../../block/../util_type.cuh:545:9: error: ‘__host__’ does not name a type; did you mean ‘__loff_t’?
             __host__ __device__ __forceinline__ CubVector operator+(const CubVector &other) const {         \
             ^
/home/xx/cub/cub/block/specializations/../../block/../util_arch.cuh:64:38: error: ‘__host__’ does not name a type; did you mean ‘CUhostFn’?
         #define CUB_RUNTIME_FUNCTION __host__ __device__
                                      ^
/home/xx/cub/cub/device/../iterator/arg_index_input_iterator.cuh:144:25: error: ‘__forceinline__’ does not name a type; did you mean ‘__thrust_forceinline__’?
     __host__ __device__ __forceinline__ ArgIndexInputIterator(
                         ^~~~~~~~~~~~~~~
                         __thrust_forceinline__
/home/xx/cub/cub/device/device_reduce.cuh:148:12: error: ‘cudaError_t’ does not name a type; did you mean ‘cudaError_enum’?
     static cudaError_t Reduce(
            ^~~~~~~~~~~
            cudaError_enum

以下是我的源文件:

device.h

代码语言:javascript
复制
#pragma once

#include <cub/cub.cuh>

void scan_on_device();

device.cu

代码语言:javascript
复制
#include "device.h"

void scan_on_device()
{
  // Declare, allocate, and initialize device pointers for input and output
  int num_items = 7;
  int *d_in;
  int h_in[]  = {8, 6, 7, 5, 3, 0, 9};
  int sz = sizeof(h_in)/sizeof(h_in[0]);
  int *d_out; // e.g., [ , , , , , , ]
  cudaMalloc(&d_in,  sz*sizeof(h_in[0]));
  cudaMalloc(&d_out, sz*sizeof(h_in[0]));
  cudaMemcpy(d_in, h_in, sz*sizeof(h_in[0]), cudaMemcpyHostToDevice);
  printf("\nInput:\n");
  for (int i = 0; i < sz; i++) printf("%d ", h_in[I]);
  // Determine temporary device storage requirements
  void *d_temp_storage = NULL;
  size_t temp_storage_bytes = 0;
  cub::DeviceScan::InclusiveSum(d_temp_storage, temp_storage_bytes, d_in, d_out, num_items);
  // Allocate temporary storage
  cudaMalloc(&d_temp_storage, temp_storage_bytes);
  // Run inclusive prefix sum
  cub::DeviceScan::InclusiveSum(d_temp_storage, temp_storage_bytes, d_in, d_out, num_items);
// d_out s<-- [8, 14, 21, 26, 29, 29, 38]
  cudaMemcpy(h_in, d_out, sz*sizeof(h_in[0]), cudaMemcpyDeviceToHost);
  printf("\nOutput:\n");
  for (int i = 0; i < sz; i++) printf("%d ", h_in[i]);
  printf("\n");
}

host.cpp

代码语言:javascript
复制
#include "device.h"
#include <cub/cub.cuh>

int main(void)
{
    scan_on_device();

    return 0;
}

我试着分三个步骤编译它们:

代码语言:javascript
复制
nvcc -O2 -c device.cu -I/home/xx/cub
g++  -O2 -c host.cpp  -I/usr/local/cuda/include/ -I/home/xx/cub
g++ -o tester device.o host.o -L/usr/local/cuda/lib64 -lcudart

第一步进行得很顺利,但是第二步给出了上述错误。任何想法都会受到赞赏。也许我搞砸了一些链接(到库达或幼崽)?

EN

回答 1

Stack Overflow用户

发布于 2020-03-31 13:48:06

cub头库(例如cub.cuh)包含CUDA C++代码。这样的代码不能由像g++这样的普通主机编译器编译。如果尝试这样做,您将得到编译错误。

但是,您的项目不需要cub.cuh在您的device.h头文件中,也不要求cub.cuh由g++编译。device.h头文件中唯一需要的是scan_on_device()的函数原型。

因此,如果您在函数实现文件device.cu中包含了cub头文件,并将其移除到项目的其他地方,那么您的代码将编译。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60940597

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档