gsl :: gsl_vector vs std :: vector开销和效率 [英] gsl::gsl_vector vs std::vector overheads and efficiency

查看:570
本文介绍了gsl :: gsl_vector vs std :: vector开销和效率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑实现像容器的数组,我不知道是否使用gsl :: gsl_vector或std :: vector。容器需要节省空间,但调用值也非常快。容器将在主程序中不断地被引用,以例如将输入值转换为张量函数等等。我从容器中调用数十亿次。

I am considering implementing an array like container, and I'm not sure whether to use a gsl::gsl_vector or a std::vector. The container needs to be space efficient but also very fast at calling values. The container will be referenced constantly in the main program to, for example, input values into tensor functions, amongst other things. I am calling from the container literally billions of times.

这里是我已经考虑到的优点和缺点:$ b​​ $ b gsl_vector很方便,因为它将允许我偶尔使用gsl BLAS库,并且 gsl_vector_get(...)调用是非常有效的。另一方面,我能够使用stl迭代器得到几乎相同的调用速度,并且stl向量有一个我觉得很自然的接口。

Here are the pros and cons that I've considered so far: The gsl_vector is convenient because it will allow me to use the gsl BLAS libraries on occasion, and the gsl_vector_get(...) call is very efficient. On the other hand, I am able to get almost the same speed of calls using stl iterators, and the stl vector has an interface that I find quite natural.

此外,我使用的是一个 std: :vector< gsl_vector *> 此时的实现,以及遍历std :: vector的迭代器。在这里使用gsl_matrix会更聪明吗?这个想法是使用gsl_vector_views获取正确的向量,而不是迭代器。这将更有效吗?

Also, I am using a std::vector<gsl_vector*> implementation at the moment, and an iterator to traverse the std::vector. Would it be smarter to use a gsl_matrix here? The idea would be to use gsl_vector_views to get the right vector, rather than the iterator. Would this be more efficient?

推荐答案

一方面,使用gsl_vector可以使用gsl BLAS是一个大优点。另一方面,gsl接口对于c ++程序员也是相当麻烦的。因此,这两种解决方案都不能令人满意。但是,我更喜欢使用gsl_matrix,因为

On one hand, it is true that with gsl_vector you can use gsl BLAS which is a big advantage. On the other hand, it is also true that gsl interface is quite cumbersome for a c++ programmer. So, neither solutions are truly satisfactory. However, I strongly prefer the use of gsl_matrix because

(i)有些努力你可以写一个小的包装类,以改善gsl_matrix的繁琐的C接口更难以处理缺乏BLAS库在std :: vector)。

(i) with some effort you can write a small wrapper class that ameliorate the cumbersome C interface of gsl_matrix (it is much harder to deal with the lack of BLAS library in std::vector).

(ii)gsl_matrix只是一维连续数组的包装,其中 m(i,j)= array [i * N + j ] 用于方阵(即使矩阵不是正方形,gsl_matrix仍然将其实现为一维数组)。在 std :: vector< gsl_vector *> 中,您将需要单独malloc每个gsl_vector,这意味着内存不会是连续的。这点击性能,因为在内存分配中缺少空间局部性通常会大大增加缓存未命中。

(ii) gsl_matrix is just a wrapper to an one dimensional continuous array where m(i,j) = array[i*N + j] for square matrix (even if the matrix is not square gsl_matrix still implements it as one dimensional array). In std::vector<gsl_vector*>, you will need to "malloc" each gsl_vector individually and this implies that memory won't be contiguous. This hits performance because the lack of "spatial locality" in memory allocation usually increases cache misses substantially.

如果你有选择使用完全不同的解决方案,在Blaze中使用StaticMatrix或DynamicMatrix类进行张量计算

If you have the choice to use a completely different solution, I would implement the tensor calculation using StaticMatrix or DynamicMatrix classes in Blaze lib

Blaze

为什么选择Blaze?

Why Blaze?

(i)StaticMatrix或DynamicMatrix接口比 std :: vector< gsl_vector *> gsl_matrix

(i) The StaticMatrix or DynamicMatrix interface is much better than std::vector<gsl_vector*> or gsl_matrix

(ii)Blaze是C ++中最快的BLAS库。如果你有可用的英特尔MKL(记住,英特尔MKL比gsl BLAS),它比gsl更快。为什么这样?因为Blaze使用了一种称为智能表达模板的新技术。基本上,德国的研究人员在一系列文章中展示了一系列文章 paper 1 < a> 论文2 表达模板技术是许多C ++ BLAS中的标准技术库,对于矩阵运算(BLAS 3运算)是可怕的,因为编译器不能比低级代码更聪明。然而,表达式模板可以作为一个更聪明的包装器到低级BLAS库,如intel MKL。所以他们创建智能表达模板技术,这只是一个包装你选择的低级blas lib。他们的基准惊人。

(ii) Blaze is the fastest BLAS lib available in C++. It is faster than gsl if you have available Intel MKL (remember that Intel MKL is faster than gsl BLAS). Why so? Because Blaze uses a new technique called "Smart Expression Template" . Basically, researchers in Germany showed in a series of articles paper 1 paper 2 that the "Expression Template" technique, which is the standard technique in many C++ BLAS libraries, is terrible for matrix operations (BLAS 3 operations) because compiler can't be smarter than low level code. However, "Expression Template" can be used as a smarter wrapper to low level BLAS libraries like intel MKL. So they create "Smart Expression Template" technique which is just a wrapper to your choice of low level blas lib. Their benchmarks are astonishing

基准

这篇关于gsl :: gsl_vector vs std :: vector开销和效率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆