我有一个奇怪的问题,我无法解决。它与助推推力代码有关。
法典:
#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 Toolkit \ CUDA \ v 9.2 \ bin \ nvcc . exe "-gen code = arch = compute _ 30,code=\"sm_30,compute _ 30 \ "-gen code = arch = compute _ 35,code=\"sm_35,compute _ 35 \ "-gen code = arch = compute _ 37,code=\"sm_37,compute _ 37 \ "-gen code = arch = compute _ 50,code=\"sm_50../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 Files \ NVIDIA GPU Computing Toolkit \ CUDA \ v 9.2 \ include "-G-keep-dir x64 \ Debug-maxrregcount = 0-machine 64-compile-cudart static-Xcompiler "/wd 4819 "-G-dwin 32-dwin 32-D _ MBCS-D _ MBCS-Xcompiler "/EHsc/W3/no logo/Od/FS/Zi/rt C1/MTd "-o x64/Debug/myfirstmatrilerMyFirstMatrixTest C:\ Program Files(x86)\ Microsoft Visual Studio \ 2017 \ Community \ common 7 \ IDE \ VC \ VC targets \ build customizations \ CUDA 9.2 . targets 707
还有这个:
致命错误C1012不匹配的括号:缺少字符")" MyFirstMatrixTest c:\ local \ boost \ preprocessor \ slot \ detail \ shared . HPP 27
为什么会发生此错误?
谢谢你。
您正在使用lambdas-将'--std=c 11'选项提供给nvcc。
嗯,第一个问题是
int N = 100000;
所以n^2 = 100亿...(永远不会适合< code>int)。也就是10G*4字节(浮点)= 40gb的数据。对我来说,这引发了一个内存异常。
我遇到的下一个问题是< code>unbounded_array和< code>generate_n的组合。就是不管用。但是既然你使用了推力,就使用推力类型和算法(我不确定为什么推力有它自己的类型来代替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 矩阵复制回向量是很困难的。
编辑2:编译参数
"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"
我有个问题。为什么我不能编译这个?怎么了? //此代码来自我之前帖子的答案 我有这样的错误: c:\ Program Files(x86)\ Microsoft Visual Studio 8 \ VC \ include \ algorithm(40):错误c 2784:“bool STD::operator = =(const _ Ty C:\Program Files (x86)\Micro
我最近在部署到我们的共享主机时遇到了问题。从 Visual Studio 通过 Web 部署进行部署时,99% 的时间我在登录后在登录页面或主页上收到此错误。有时我可能会点击几下不同的部分,然后它就会击中。它通常会在几分钟到90分钟后消失。 运行Windows Server 2012 R2 Standard、IIS8和.NET 4.5的Web主机。 Web主机建议我需要使用Visual Studi
1.1. 代码编译 1.1.1. Openwrt编译 1.1.2. Kernel编译 1.1.3. Uboot编译 1.1.4. VSP编译 1.1. 代码编译 1.1.1. Openwrt编译 作为Kamino18 YODAOS的整体编译环境,使用openwrt可以编译出系统正常运行所需的主要image如下: 镜像名字 镜像运行位置 镜像说明 镜像生成位置 mcu.bin MCU The ima
null 完整的信息是这样的
* *我正在使用JSP、JavaServlet和Hibernate开发一个电子商务应用程序。执行此代码时出现以下错误。我得到了这个错误需要帮助调试它**
ucore 代码编译 (1) 编译过程:在解压缩后的 ucore 源码包中使用 make 命令即可。例如 lab1中: [email protected]: ~/lab1$ make 在lab1目录下的bin目录中,生成一系列的目标文件: ucore.img:被qemu访问的虚拟硬盘文件 kernel: ELF格式的toy ucore kernel执行文,被嵌入到了ucore.img中