use*_*020 33 c++ boost-multi-array multidimensional-array blitz++ armadillo
我在blitz ++,armadillo,boost :: MultiArray之间用以下代码进行了比较(借用旧帖子)
#include <iostream>
using namespace std;
#include <windows.h>
#define _SCL_SECURE_NO_WARNINGS
#define BOOST_DISABLE_ASSERTS
#include <boost/multi_array.hpp>
#include <blitz/array.h>
#include <armadillo>
int main(int argc, char* argv[])
{
const int X_SIZE = 1000;
const int Y_SIZE = 1000;
const int ITERATIONS = 100;
unsigned int startTime = 0;
unsigned int endTime = 0;
// Create the boost array
//------------------Measure boost Loop------------------------------------------
{
typedef boost::multi_array<double, 2> ImageArrayType;
ImageArrayType boostMatrix(boost::extents[X_SIZE][Y_SIZE]);
startTime = ::GetTickCount();
for (int i = 0; i < ITERATIONS; ++i)
{
for (int x = 0; x < X_SIZE; ++x)
{
for (int y = 0; y < Y_SIZE; ++y)
{
boostMatrix[x][y] = 1.0001;
}
}
}
endTime = ::GetTickCount();
printf("[Boost Loop] Elapsed time: %6.3f seconds\n", (endTime - startTime) / 1000.0);
}
//------------------Measure blitz Loop-------------------------------------------
{
blitz::Array<double, 2> blitzArray( X_SIZE, Y_SIZE );
startTime = ::GetTickCount();
for (int i = 0; i < ITERATIONS; ++i)
{
for (int x = 0; x < X_SIZE; ++x)
{
for (int y = 0; y < Y_SIZE; ++y)
{
blitzArray(x,y) = 1.0001;
}
}
}
endTime = ::GetTickCount();
printf("[Blitz Loop] Elapsed time: %6.3f seconds\n", (endTime - startTime) / 1000.0);
}
//------------------Measure armadillo loop----------------------------------------
{
arma::mat matArray( X_SIZE, Y_SIZE );
startTime = ::GetTickCount();
for (int i = 0; i < ITERATIONS; ++i)
{
for (int y = 0; y < Y_SIZE; ++y)
{
for (int x = 0; x < X_SIZE; ++x)
{
matArray(x,y) = 1.0001;
}
}
}
endTime = ::GetTickCount();
printf("[arma Loop] Elapsed time: %6.3f seconds\n", (endTime - startTime) / 1000.0);
}
//------------------Measure native loop----------------------------------------
// Create the native array
{
double *nativeMatrix = new double [X_SIZE * Y_SIZE];
startTime = ::GetTickCount();
for (int i = 0; i < ITERATIONS; ++i)
{
for (int y = 0; y < Y_SIZE*X_SIZE; ++y)
{
nativeMatrix[y] = 1.0001;
}
}
endTime = ::GetTickCount();
printf("[Native Loop]Elapsed time: %6.3f seconds\n", (endTime - startTime) / 1000.0);
delete[] nativeMatrix;
}
//------------------Measure boost computation-----------------------------------
{
typedef boost::multi_array<double, 2> ImageArrayType;
ImageArrayType boostMatrix(boost::extents[X_SIZE][Y_SIZE]);
for (int x = 0; x < X_SIZE; ++x)
{
for (int y = 0; y < Y_SIZE; ++y)
{
boostMatrix[x][y] = 1.0001;
}
}
startTime = ::GetTickCount();
for (int i = 0; i < ITERATIONS; ++i)
{
for (int x = 0; x < X_SIZE; ++x)
{
for (int y = 0; y < Y_SIZE; ++y)
{
boostMatrix[x][y] += boostMatrix[x][y] * 0.5;
}
}
}
endTime = ::GetTickCount();
printf("[Boost computation] Elapsed time: %6.3f seconds\n", (endTime - startTime) / 1000.0);
}
//------------------Measure blitz computation-----------------------------------
{
blitz::Array<double, 2> blitzArray( X_SIZE, Y_SIZE );
blitzArray = 1.0001;
startTime = ::GetTickCount();
for (int i = 0; i < ITERATIONS; ++i)
{
blitzArray += blitzArray*0.5;
}
endTime = ::GetTickCount();
printf("[Blitz computation] Elapsed time: %6.3f seconds\n", (endTime - startTime) / 1000.0);
}
//------------------Measure armadillo computation-------------------------------
{
arma::mat matArray( X_SIZE, Y_SIZE );
matArray.fill(1.0001);
startTime = ::GetTickCount();
for (int i = 0; i < ITERATIONS; ++i)
{
//matArray.fill(1.0001);
matArray += matArray*0.5;
}
endTime = ::GetTickCount();
printf("[arma computation] Elapsed time: %6.3f seconds\n", (endTime - startTime) / 1000.0);
}
//------------------Measure native computation------------------------------------------
// Create the native array
{
double *nativeMatrix = new double [X_SIZE * Y_SIZE];
for (int y = 0; y < Y_SIZE*X_SIZE; ++y)
{
nativeMatrix[y] = 1.0001;
}
startTime = ::GetTickCount();
for (int i = 0; i < ITERATIONS; ++i)
{
for (int y = 0; y < Y_SIZE*X_SIZE; ++y)
{
nativeMatrix[y] += nativeMatrix[y] * 0.5;
}
}
endTime = ::GetTickCount();
printf("[Native computation]Elapsed time: %6.3f seconds\n", (endTime - startTime) / 1000.0);
delete[] nativeMatrix;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在Windows上,VS2010,结果是
[Boost Loop] Elapsed time: 1.217 seconds
[Blitz Loop] Elapsed time: 0.046 seconds
[arma Loop] Elapsed time: 0.078 seconds
[Native Loop]Elapsed time: 0.172 seconds
[Boost computation] Elapsed time: 2.152 seconds
[Blitz computation] Elapsed time: 0.156 seconds
[arma computation] Elapsed time: 0.078 seconds
[Native computation]Elapsed time: 0.078 seconds
Run Code Online (Sandbox Code Playgroud)
在Windows上,intel c ++,结果是
[Boost Loop] Elapsed time: 0.468 seconds
[Blitz Loop] Elapsed time: 0.125 seconds
[arma Loop] Elapsed time: 0.046 seconds
[Native Loop]Elapsed time: 0.047 seconds
[Boost computation] Elapsed time: 0.796 seconds
[Blitz computation] Elapsed time: 0.109 seconds
[arma computation] Elapsed time: 0.078 seconds
[Native computation]Elapsed time: 0.062 seconds
Run Code Online (Sandbox Code Playgroud)
奇怪的东西:
(1) with VS2010, native computation (including loop) is faster than native loop
(2) blitz loop behave so different under VS2010 and intel C++.
Run Code Online (Sandbox Code Playgroud)
要使用intel c ++编译器编译blitz ++,blitz/intel /文件夹中需要一个名为bzconfig.h的文件.但事实并非如此.我只是复制了blitz/ms/bzconfig.h中的那个.这可能会给出一个非最佳配置.谁能告诉我如何使用intel c ++编译器编译blitz ++?在手册中,它说运行bzconfig脚本来获取正确的bzconfig.h.但我不明白这意味着什么.
非常感谢!
加上我的一些结论:
1. Boost multi array is the slowest.
2. With intel c++ compiler, native pointers are very fast.
3. With intel c++ compiler, armadillo can achieve the performance of native pointers.
4. Also test eigen, it is x0% slower than armadillo in my simple cases.
5. Curious about blitz++'s behavior in intel c++ compiler with proper configuration.
Please see my question.
Run Code Online (Sandbox Code Playgroud)
简短回答:./configure CXX=icpc,通过阅读 Blitz++ 用户指南找到。
长答案:
要使用 intel c++ 编译器编译 blitz++,在 blitz/intel/ 文件夹中需要一个名为 bzconfig.h 的文件。但没有。
是的,是的。Blitz++ 应该自己生成文件。根据 Blitz++ User's Guideblitz.pdf中的blitz-0.10.tar.gz“安装”部分,
Blitz++ 使用 GNU Autoconf,它处理为各种平台和编译器重写 Makefile。
更准确地说,Blitz++ 使用了 GNU autotools 工具链(automake、autoconf、configure),它可以生成 makefile、配置脚本、头文件等。这些bzconfig.h文件应该由configureBlitz++ 附带的脚本生成,随时可以使用。
我只是复制了 blitz/ms/bzconfig.h 中的那个。这可能会给出一个非最佳配置。
如果“非最佳”对您来说意味着“不工作”,那么是的。:-) 你需要一个能intel/bzconfig.h准确代表你的编译器的。
谁能告诉我如何用 intel c++ 编译器编译 blitz++?
阅读并遵循精美的手册,特别是上面提到的“安装”部分。
进入 'blitz-VERSION' 目录,然后输入:
./configure CXX=[compiler]其中 [compiler] 是 xlc++、icpc、pathCC、xlC、cxx、aCC、CC、g++、KCC、pgCC 或 FCC 之一。(如果您不选择 C++ 编译器,配置脚本将尝试为当前平台找到合适的编译器。)
你这样做了吗?对于英特尔编译器,您需要使用
./configure CXX=icpc.
在手册中,它说运行 bzconfig 脚本以获得正确的 bzconfig.h。但我不明白这是什么意思。
我认为“它”是指“那个”。你说的“手动”是什么意思?我的 Blitz++ 用户指南副本没有提到bzconfig. 您确定您使用的是与您的 Blitz++ 版本相对应的手册吗?
PS:在blitz-0.10的内容中寻找“bzconfig” ,看起来“bzconfig”不再是 Blitz++ 的一部分,而是曾经是:
find . -name bzconfig -> 没有结果
find . -print0 | xargs -0 grep -a -i -n -e bzconfig:
./blitz/compiler.h:44: #error In <blitz/config.h>: A working template implementation is required by Blitz++ (you may need to rerun the compiler/bzconfig script)
Run Code Online (Sandbox Code Playgroud)
那需要更新。
./blitz/gnu/bzconfig.h:4:/* blitz/gnu/bzconfig.h. Generated automatically at end of configure. */
./configure.ac:159:# autoconf replacement of bzconfig
Run Code Online (Sandbox Code Playgroud)
好了,这些bzconfig.h文件应该由configure.
./ChangeLog.1:1787: will now replace the old file that was generate with the bzconfig
Run Code Online (Sandbox Code Playgroud)
这可能是切换到 autoconf 的更改。
./INSTALL:107: 2. Go into the compiler subdirectory and run the bzconfig
Run Code Online (Sandbox Code Playgroud)
那需要更新。这就是你寻找的原因bzconfig吗?
./README:27:compiler Compiler tests (used with obsolete bzconfig script)
Run Code Online (Sandbox Code Playgroud)
需要更新,compiler不再包含目录。
据我所知,您通过测量单个矩阵乘以标量的速度来判断每个矩阵库的性能。由于其基于模板的策略,Armadillo 将在这方面做得非常好,将每个乘法分解为大多数编译器的可并行代码。
但我建议您需要重新考虑您的测试范围和方法。例如,您省略了每个BLAS实现。您需要的 BLAS 函数是dscal。供应商为您的特定 CPU 提供的实现可能会做得很好。
更相关的是,任何合理的向量库都需要能够执行更多的操作:矩阵乘法、点积、向量长度、转置等等,这些是您的测试无法解决的。您的测试恰好解决了两件事:元素分配(实际上来说,它从来都不是向量库的瓶颈)和标量/向量乘法(这是每个 CPU 制造商提供的 BLAS 1 级函数)。
这里讨论了 BLAS 1 级与编译器发出的代码。
TL:博士;将 Armadillo 与链接到您平台的 BLAS 和 LAPACK 本机库一起使用。