简体   繁体   English

推广到多个 BLAS/LAPACK 库

[英]Generalizing to multiple BLAS/LAPACK Libraries

I am developing a linear algebra tool in C++, which relies heavily on matrix multiplication and decompositions (like LU, SVD), and is meant to be applied to large matrices.我正在用 C++ 开发一个线性代数工具,它在很大程度上依赖于矩阵乘法和分解(如 LU、SVD),并且旨在应用于大型矩阵。 I developed it using Intel MKL for peak performance, but I don't want to release an Intel MKL only version, as I assume it will not work for people without Intel or who don't want to install MKL.我使用英特尔 MKL 开发它以获得最佳性能,但我不想发布仅英特尔 MKL 的版本,因为我认为它不适用于没有英特尔或不想安装 MKL 的人。 Instead, I should release a more general code that is not Intel MKL-specific, but rather allows the user to specify which implementation of BLAS and LAPACK they would like to use (eg OpenBLAS, or ATLAS).相反,我应该发布一个更通用的代码,它不是英特尔 MKL 特定的,而是允许用户指定他们想要使用的 BLAS 和 LAPACK 的实现(例如 OpenBLAS 或 ATLAS)。

Although the function prototypes seem to be the same across implementations, there are several (helper?) functions and types that are specific to Intel MKL.尽管各个实现的函数原型似乎都相同,但有几个(帮助程序?)函数和类型是英特尔 MKL 特有的。 For example, there is the MKL_INT type that I use, and also the mkl_malloc .例如,我使用了 MKL_INT 类型,还有mkl_malloc This article suggests using macros to redefine the types, which was also my first thought.这篇文章建议使用宏来重新定义类型,这也是我的第一个想法。 I assume I would also then have macros for the headers as well.我假设我也会有标题的宏。

I believe it is standard for code to be written such that it is agnostic to the BLAS/LAPACK implementation, and I wanted to know if there was a cleaner way than relying on macros--particularly since the latter would require recompiling the code to switch, which does not seem to be necessary for other tools I have used.我相信编写代码是标准的,以至于它与 BLAS/LAPACK 实现无关,我想知道是否有比依赖宏更干净的方法——特别是因为后者需要重新编译代码才能切换,这对于我使用过的其他工具似乎不是必需的。

Most scientific codes that rely on BLAS/LAPACK calls are implementation-agnostic.大多数依赖 BLAS/LAPACK 调用的科学代码与实现无关。 They usually require that the library is just linked as appropriate.他们通常要求库只是适当地链接。

You've commented that the function prototypes are the same across implementations.您已经评论过不同实现的函数原型是相同的。 This allows you to just have the prototypes in some myblas.h and mylapack.h headers then link whichever library you'd like to use.这允许您只在一些myblas.hmylapack.h头文件中有原型,然后链接您想要使用的任何库。

It sounds like your primary concern is the implementation-specific stuff that you've utilized for MKL.听起来您主要关心的是您用于 MKL 的特定于实现的东西。 The solution is to just not use this stuff.解决方案是不使用这些东西。 For example, the MKL types like MKL_INT are not special.例如,像MKL_INT这样的 MKL 类型并不特殊。 They are C datatypes that have been defined to allow generalize between LP32/LP64/ILP64 libraries which MKL provides.它们是已定义为允许在 MKL 提供的 LP32/LP64/ILP64 库之间进行泛化的 C 数据类型。 See this table .请参阅此表

Also, stuff like mkl_malloc isn't special.此外,像mkl_malloc这样的东西并不特别。 It was introduced before the C standard had a thread-safe aligned alloc.它是在 C 标准具有线程安全对齐分配之前引入的。 In fact, that is all mkl_malloc is.事实上,这就是mkl_malloc全部mkl_malloc So instead, just use aligned_alloc , or if you don't want to commit to C11 use _mm_malloc , memalign , etc...因此,相反,只需使用aligned_alloc ,或者如果您不想提交C11,请使用_mm_mallocmemalign等...

On the other hand, MKL does provide some useful extensions to BLAS/LAPACK which aren't standardized (like transpositions, for example).另一方面,MKL 确实为 BLAS/LAPACK 提供了一些有用的扩展,这些扩展不是标准化的(例如换位)。 However, this type of stuff is usually easy to implement with a special case BLAS/LAPACK call or easy enough to implement by yourself.然而,这种类型的东西通常很容易通过特殊情况的 BLAS/LAPACK 调用来实现,或者很容易自己实现。 MKL also has internal threading if you choose to use it, however, many BLAS/LAPACK libraries offer this.如果您选择使用 MKL,它也有内部线程,但是,许多 BLAS/LAPACK 库都提供了这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM