c++11 - BLAS 库如何直接与 boost multiarrays 一起使用?

标签 c++11 boost matrix-multiplication

给定两个矩阵AB,以及C作为它们相乘的结果。

#include "boost/multi_array.hpp"
typedef boost::multi_array<double, 2> matrix;
int m=5;
int n=6;
int k=7;

matrix A(boost::extents[m][k]);
matrix B(boost::extents[k][n]);
matrix C(boost::extents[m][n]);

如何调用 blas 库中的 dgemm 函数来计算 AB 的矩阵乘积? 我知道 uBLAS 部分 boost 库、armadilloMTL 4eigen 以及其他一些为 blas 函数提供方便的包装器的库。这里的问题是如何直接在多数组上调用dgemm

最佳答案

您可以访问连续元素存储。

原型(prototype)是

void cblas_dgemm( CBLAS_LAYOUT layout, CBLAS_TRANSPOSE TransA, CBLAS_TRANSPOSE TransB, const int M, const int N, const int K, const double alpha, const double *A, const int lda, const double *B, const int ldb, const double beta, double *C, const int ldc )

那么,让我们填写一下:

cblas_dgemm(
    CBLAS_LAYOUT::CblasRowMajor,
    CBLAS_TRANSPOSE::CblasNoTrans,
    CBLAS_TRANSPOSE::CblasNoTrans,
    m, n, k,
    1.0, // alpha
    A.data(), A.shape()[1],
    B.data(), B.shape()[1],
    0.0, // beta
    C.data(), C.shape()[1]);

That was using the docs here for a bit of guidance on alpha/beta:

演示

#include <boost/multi_array.hpp>
typedef boost::multi_array<double, 2> matrix;

#include <iostream>
namespace io { // for debug output
    auto& dump(std::ostream& os, double v) { return os << v; }

    template <typename R> auto& dump(std::ostream& os, R const& r) {
        std::string_view sep = "";
        os << "{";
        for (auto const& el : r) { dump(os << sep, el); sep = ","; }
        return os << "}";
    }
}

std::ostream& operator<<(std::ostream& os, matrix const& m) { return io::dump(os, m); }

// demo
#include <cblas-netlib.h>
#include <numeric> // iota

int main() {
    constexpr auto m=5, n=6, k=7;

    matrix A(boost::extents[m][k]);
    matrix B(boost::extents[k][n]);
    matrix C(boost::extents[m][n]);

    std::iota(A.data(), A.data() + A.num_elements(), 0);
    std::iota(B.data(), B.data() + B.num_elements(), 50);
    std::iota(C.data(), C.data() + C.num_elements(), 100);

    std::cout << "A: " << A << "\nB: " << B << "\n";
    assert(A.storage_order().all_dims_ascending());

    /*
     * void cblas_dgemm(
     *   CBLAS_LAYOUT layout,
     *   CBLAS_TRANSPOSE TransA,
     *   CBLAS_TRANSPOSE TransB,
     *   const int M, const int N, const int K,
     *   const double alpha,
     *   const double *A, const int lda,
     *   const double *B, const int ldb,
     *   const double beta,
     *   double *C, const int ldc )
     */

    cblas_dgemm(
        CBLAS_LAYOUT::CblasRowMajor,
        CBLAS_TRANSPOSE::CblasNoTrans,
        CBLAS_TRANSPOSE::CblasNoTrans,
        m, n, k,
        1.0, // alpha
        A.data(), A.shape()[1],
        B.data(), B.shape()[1],
        0.0, // beta
        C.data(), C.shape()[1]);

    std::cout << "C:\n" << C << "\n";
}

打印内容:

A: {{0,1,2,3,4,5,6},{7,8,9,10,11,12,13},{14,15,16,17,18,19,20},{21,22,23,24,25,26,27},{28,29,30,31,32,33,34}}
B: {{50,51,52,53,54,55},{56,57,58,59,60,61},{62,63,64,65,66,67},{68,69,70,71,72,73},{74,75,76,77,78,79},{80,81,82,83,84,85},{86,87,88,89,90,91}}
C:
{{1596,1617,1638,1659,1680,1701},{4928,4998,5068,5138,5208,5278},{8260,8379,8498,8617,8736,8855},{11592,11760,11928,12096,12264,12432},{14924,15141,15358,15575,15792,16009}}

这与 Wolfram Alpha 进行检查:

enter image description here

关于c++11 - BLAS 库如何直接与 boost multiarrays 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63535943/

相关文章:

C - 矩阵乘法分割错误

c++ - 函数模板中的 lambda 闭包类型和默认参数

c++ - 在两个模板类型相同的情况下如何不重载 std::map::at 成员函数?

c++ - 模板类模板成员的模板特化

iphone - 如何将boost线程库添加到iPhone项目中?

big-o - 为什么方阵乘法的时间复杂度定义为 O(n^3)?

c++ - 将 decltype 与成员函数指针一起使用

c++ - 为什么 Boost.Regex 不能在一个字符串中找到多个匹配项?

c++ - Boost Geometry 中用户定义的空间谓词

c++ - GSL-GNU 中的矩阵乘法