【问题标题】:Thrust+boost code compilation errorThrust+boost代码编译错误
【发布时间】:2018-06-15 14:31:12
【问题描述】:

我有一个我无法解决的奇怪问题。它与 boost+thrust 代码相关联。

代码:

#include <boost/config/compiler/nvcc.hpp>

#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/sort.h>
#include <thrust/copy.h>
#include <thrust/sequence.h>
#include <thrust/random.h>
#include <thrust/generate.h>
#include <thrust/detail/type_traits.h>

#include <cuda_runtime.h>

#include <cublas_v2.h>
#include <common/inc/helper_cuda.h>

#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/operation.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int_distribution.hpp>
#include <boost/compute/system.hpp>
#include <boost/compute/command_queue.hpp>
#include <boost/compute/algorithm/generate.hpp>
#include <boost/compute/algorithm/generate_n.hpp>


#include <algorithm>
#include <time.h>
#include <limits.h>
#include <algorithm>

using namespace boost::numeric::ublas;
using namespace boost::random;
using namespace boost::compute;


int main(int argc, char **argv)
{
    int N = 100000;

    unbounded_array<float> lineMatrix1(N*N);
    unbounded_array<float> lineMatrix2(N*N);    

    generate_n(lineMatrix1.begin(), N*N, []() { return (10 * rand() / RAND_MAX); });
    generate_n(lineMatrix2.begin(), N*N, []() { return (10 * rand() / RAND_MAX); });    

    matrix<float> matrix1(N, N, lineMatrix1);
    matrix<float> matrix2(N, N, lineMatrix2);
    matrix<float> zeroMatrix(N, N, 0);  
    matrix<float> zeroMatrix2(N, N, 0);

    //boost single core computation start

    auto matrix3 = prod(matrix1, matrix2);

    //boost single core computation finish

    //thrust computation start

    findCudaDevice(argc, (const char **)argv);

    cublasHandle_t handle;
    cublasCreate(&handle);

    float alpha = 1.0f;
    float beta = 0.0f;

    auto result = cublasSgemm(handle, CUBLAS_OP_N, CUBLAS_OP_N, N, N, N, &alpha, matrix1.data().cbegin(), N, matrix2.data().cbegin(), N, &beta, zeroMatrix.data().begin(), N);
    cudaDeviceSynchronize();

    thrust::device_vector<float> deviceMatrix1(N*N);
    thrust::device_vector<float> deviceMatrix2(N*N);
    thrust::device_vector<float> deviceZeroMatrix(N*N, 0);

    thrust::copy(matrix1.data().cbegin(), matrix1.data().cend(), deviceMatrix1.begin());
    thrust::copy(matrix2.data().cbegin(), matrix2.data().cend(), deviceMatrix2.begin());

    auto result2 = cublasSgemm(handle, CUBLAS_OP_N, CUBLAS_OP_N, N, N, N, &alpha, deviceMatrix1.data().get(), N, deviceMatrix2.data().get(), N, &beta, deviceZeroMatrix.data().get(), N);
    cudaDeviceSynchronize();

    thrust::copy(deviceZeroMatrix.cbegin(), deviceZeroMatrix.cend(), zeroMatrix2.data().begin());

    std::cout << result << std::endl;
    std::cout << result2 << std::endl;

    //thrust computation finish    

    float eps = 0.00001;
    int differCount1 = 0;
    int differCount2 = 0;

    for (int i = 0; i < matrix3.size1(); i++)
    {
        for (int j = 0; j < matrix3.size2(); j++)
        {
            if (std::abs(matrix3(i, j) != zeroMatrix(i, j)) > eps)
                differCount1++;

            if (std::abs(matrix3(i, j) != zeroMatrix2(i, j)) > eps)
                differCount2++;
        }
    }

    std::cout << differCount1 << std::endl;
    std::cout << differCount2 << std::endl;

    char c;
    std::cin >> c;

    return 0;
}

此文件的名称为“myFirstMatrixTest.cu”。

所以,我有编译器错误:

MSB3721 退出命令 ""C:\Program Files\NVIDIA GPU Computing 工具包\CUDA\v9.2\bin\nvcc.exe" -gencode=arch=compute_30,code=\"sm_30,compute_30\" -gencode=arch=compute_35,code=\"sm_35,compute_35\" -gencode=arch=compute_37,code=\"sm_37,compute_37\" -gencode =arch=compute_50,code=\"sm_50,compute_50\" -gencode=arch=compute_52,code=\"sm_52,compute_52\" -gencode=arch=compute_60,code=\"sm_60,compute_60\" -gencode=arch =compute_61,code=\"sm_61,compute_61\" -gencode=arch=compute_70,code=\"sm_70,compute_70\" --use-local-env -ccbin "C:\Program Files (x86)\Microsoft Visual Studio \2017\Community\VC\Tools\MSVC\14.14.26428\bin\HostX86\x64" -x cu -rdc=true -I./ -I../common/inc -I../../common/inc -I/common/inc -I../ -I./ -I"C:\Program Files \NVIDIA GPU 计算工具包\CUDA\v9.2/include" -I../../common/inc -I"C:\Program 文件\NVIDIA GPU 计算工具包\CUDA\v9.2\include" -G --keep-dir x64\Debug -maxrregcount=0 --machine 64 --compile -cudart static -Xcompiler "/wd 4819" -g -DWIN32 -DWIN32 -D_MBCS -D_MBCS -Xcompiler "/EHsc /W3 /nologo /Od /FS /Zi /RTC1 /MTd " -o x64/Debug/MyFirstMatrixTest.cu.obj "C:\User Root\Repository\CUDA Projects\MatrixMultiplicationThrust\MyFirstMatrixTest.cu"" 带代码 “2”。 MyFirstMatrixTest C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\VC\VCTargets\BuildCustomizations\CUDA 9.2.目标707

还有这个:

致命错误 C1012 括号不匹配:缺少字符 ")" MyFirstMatrixTest c:\local\boost\preprocessor\slot\detail\shared.hpp 27

为什么会出现这个错误?

谢谢。

【问题讨论】:

  • 要让thrust+boost 工作,您需要安装一个非常具体的版本——CUDA、visual studio 和boost 的组合。你检查过吗?
  • @JHBonarius 我已经安装了 CUDA 9.2 SDK、Boost 和 2017 工作室。 Boost 代码(没有任何推力代码)可以正常工作,推力示例也可以正常工作。
  • 为了测试你的代码,我已经将我的 Cuda 更新到 v9.2,现在一切都坏了 :'( 所以在我再次修复它之前无法帮助你。我在尝试让部件工作时遇到了很多问题以前在一起过。听说是通病。
  • 我对此进行了阅读,发现 nvcc 在编译 boost 代码时存在问题。您可能应该将主机代码和设备代码分开。

标签: c++ boost cuda thrust


【解决方案1】:

嗯,第一个问题是

int N = 100000;

所以 N^2 = 10,000,000,000...(永远不适合 int)。 即 10G*4 字节(浮点数)= 40 GB 数据。 对我来说,这会引发内存异常。

我遇到的下一个问题是unbounded_arraygenerate_n 的组合。只是没有用。但是由于您使用的是 Thrust,请使用 Thrust 类型和算法(我不确定为什么 Thrust 有自己的类型来替换 STL,但无论如何)。

我在 2015 模式下使用 Visual Studio 2017 v15.7(否则我收到不支持的错误)与 Cuda v9.2 和 Boost 1.67.0。

我修改了您的代码,直到它正确编译: (注意随机化仿函数中的更正,它首先只生成整数并将它们转换为浮点数)

#include <boost/config/compiler/nvcc.hpp>

#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/copy.h>
#include <thrust/generate.h>
#include <thrust/inner_product.h>

#include <cuda_runtime.h>

#include <cublas_v2.h>
#pragma comment(lib,"cublas.lib")
#include <helper_cuda.h>

#include <boost/numeric/ublas/matrix.hpp>
//#include <boost/numeric/ublas/io.hpp>
using boost::numeric::ublas::matrix;

#include <random>

int main(int argc, char **argv)
{
    constexpr size_t N = 100;
    constexpr size_t NN = N * N;

    thrust::host_vector<float> lineMatrix1; lineMatrix1.reserve(NN);
    thrust::host_vector<float> lineMatrix2; lineMatrix2.reserve(NN);
    {
        std::random_device rd;  //Will be used to obtain a seed for the random number engine
        std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
        std::uniform_real_distribution<float> dis(0.0f, 10.0f);
        auto genRnd = [&]() { return dis(gen); };
        thrust::generate_n(std::back_inserter(lineMatrix1), NN, genRnd);
        thrust::generate_n(std::back_inserter(lineMatrix2), NN, genRnd);
    }

    matrix<float> matrix1(N, N);
    thrust::copy_n(std::cbegin(lineMatrix1), NN, std::begin(matrix1.begin1()));
    //std::cout << "Matrix 1:\n" << matrix1 << std::endl;

    matrix<float> matrix2(N, N);
    thrust::copy_n(std::cbegin(lineMatrix2), NN, std::begin(matrix2.begin1()));
    //std::cout << "Matrix 2:\n" << matrix2 << std::endl;

    //auto matrix3 = prod(matrix1, matrix2);
    auto matrix3 = trans(prod(trans(matrix1), trans(matrix2)));
    //std::cout << "Matrix 3:\n" << matrix3 << std::endl;

    thrust::host_vector<float> hostResult; hostResult.reserve(NN);
    for (auto rowIt = matrix3.cbegin1(); rowIt != matrix3.cend1(); rowIt++)
        for (const auto& element : rowIt)
            hostResult.push_back(element);
    std::cout << "Host Result:\n";
    for (const auto& el : hostResult) std::cout << el << " ";
    std::cout << std::endl;
    //////boost single core computation finish

    //////thrust computation start
    findCudaDevice(argc, (const char **)argv);
    cublasHandle_t handle;
    cublasCreate(&handle);

    const float alpha = 1.0f;
    const float beta = 0.0f;

    thrust::device_vector<float> deviceMatrix1; deviceMatrix1.reserve(NN);
    thrust::copy_n(std::cbegin(lineMatrix1), NN, std::back_inserter(deviceMatrix1));

    thrust::device_vector<float> deviceMatrix2; deviceMatrix2.reserve(NN);
    thrust::copy_n(std::cbegin(lineMatrix2), NN, std::back_inserter(deviceMatrix2));

    thrust::device_vector<float> deviceZeroMatrix(NN,0);
    auto result2 = cublasSgemm(handle,
        CUBLAS_OP_N, CUBLAS_OP_N, N, N, N,
        &alpha,
        deviceMatrix1.data().get(), N,
        deviceMatrix2.data().get(), N,
        &beta,
        deviceZeroMatrix.data().get(), N);
    cudaDeviceSynchronize();

    cublasDestroy(handle);

    thrust::host_vector<float> deviceResult; deviceResult.reserve(NN);
    thrust::copy_n(std::cbegin(deviceZeroMatrix), NN, std::back_inserter(deviceResult));
    std::cout << "Device Result:\n";
    for (const auto& el : deviceResult) std::cout << el << " ";
    std::cout << std::endl;
    //////thrust computation finish    

    auto accError = thrust::inner_product(std::cbegin(hostResult), std::cend(hostResult), std::cbegin(deviceResult), 0.0f, std::plus<float>(),
        [](auto val1, auto val2) { return std::abs(val1 - val2); });

    std::cout << "Accumulated error: " << accError << std::endl;
    std::cout << "Average error: " << accError/NN << std::endl;

    std::cin.ignore();

    return 0;
}

编辑:修复了代码。 ublas 矩阵存储的矩阵与向量不同,所以我不得不转置矩阵和结果。 此外,将 ublas 矩阵复制回向量变得很困难。

edit2:编译参数

"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.2\bin\nvcc.exe" -gencode=arch=compute_30,code=\"sm_30,compute_30\" --use-local-env -ccbin "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\x86_amd64" -x cu  -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.2\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.2\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:\Cpp\Cuda\SoHelp2\kernel.cu"

【讨论】:

  • 谢谢你的代码,伙计。正如俄语所说,我设置 N=100000 就像“天堂中的手指”一样。所以,当然,N 应该远小于 100k。 rand() 也不是一个好主意,我也会用 boost 的算法来改变它。所以,我的纯 boost 代码在我为测试创建的独立项目中运行良好。此外,thrust 有自己的标准算法实现,完全兼容 stl 代码(使用相同的 syntx 等)。使用标准算法而不是手工循环确实更好。但是,您已经向我展示了编译我的代码的方法,非常感谢!!我会检测代码集成的问题。
  • @Dmitriy 我正在尝试自己学习 boost/thrust/cuda,所以这是一个很好的手指练习。我不熟悉 ublas matrix 类型,所以我不知道如何使用 STD 填充它。对于随机发生器,我通常会使用std::uniform_real_distribution
  • @Dmitriy 让代码工作。输出错误比你eps大得多,所以我做了一个累积错误输出。请检查一下。
  • 我已尝试编译此代码并得到 101 个错误。))) 可能,我需要获取您的编译器参数。我在 Studio 中将版本更改为 2015。现在是上午 01:00,我非常需要睡一会儿。)))谢谢你,伙计,你的帮助!
  • @Dmitriy 添加了来自 Visual Studio 的 compile cmd。奇怪的是,没有显示 boost 库的包含,并且 CUDA 库被包含了两次……顺便说一下,代码会抛出几个警告。还没有时间修复它们。
【解决方案2】:

您正在使用 lambda - 将“--std=c++11”选项提供给 nvcc。

【讨论】:

  • nvcc 回复“cl : 命令行警告 D9002: ignoring unknown option '--std=c++11'”
  • 是的。我试图建立不同类型的标准——直到 17 岁。没有结果。此外,我添加了 scl no warning 以击败检查的迭代器以及 cl/cl.h 和 chrono lib 的附加目录。也没有结果。所以,只是 boost 代码编译和工作正常,但推力它不能被编译... SFMBE(((
  • 在 Windows 上 --std=c++11 隐含在任何最新版本的 nvcc
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-20
  • 2014-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多