You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何查看C++指针指向的内存实际值?附MKL矩阵打印方案

打印MKL矩阵乘法中的指针式矩阵元素内容

由于Intel MKL的矩阵通常以一维数组形式存储,且支持**行主序(CblasRowMajor)或列主序(CblasColMajor)**两种布局(对应C和Fortran的存储习惯),打印时需要根据存储顺序遍历数组。下面是一个通用的矩阵打印函数,以及整合到cblas_dgemm示例中的完整代码:

矩阵打印函数

#include <iostream>
#include <iomanip> // 用于格式化输出对齐
#include "mkl_cblas.h"

// 打印矩阵:ptr为矩阵指针,m行数,n列数,layout指定存储布局
void print_matrix(const double* ptr, int m, int n, CBLAS_LAYOUT layout) {
    std::cout << std::fixed << std::setprecision(2); // 保留两位小数,方便阅读
    if (layout == CblasRowMajor) {
        // 行主序:每一行连续存储
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                std::cout << std::setw(8) << ptr[i * n + j];
            }
            std::cout << std::endl;
        }
    } else {
        // 列主序:每一列连续存储
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                std::cout << std::setw(8) << ptr[j * m + i];
            }
            std::cout << std::endl;
        }
    }
    std::cout << std::endl; // 打印完矩阵空一行分隔
}

整合到矩阵乘法示例中的完整代码

#include <iostream>
#include <iomanip>
#include <chrono>
#include "mkl_cblas.h"

void print_matrix(const double* ptr, int m, int n, CBLAS_LAYOUT layout) {
    std::cout << std::fixed << std::setprecision(2);
    if (layout == CblasRowMajor) {
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                std::cout << std::setw(8) << ptr[i * n + j];
            }
            std::cout << std::endl;
        }
    } else {
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                std::cout << std::setw(8) << ptr[j * m + i];
            }
            std::cout << std::endl;
        }
    }
    std::cout << std::endl;
}

int main() {
    const int size = 4;
    double A[size*size] = {1.0,2.0,3.0,4.0,
                           5.0,6.0,7.0,8.0,
                           9.0,10.0,11.0,12.0,
                           13.0,14.0,15.0,16.0};
    double B[size*size] = {1.0,0.0,0.0,0.0,
                           0.0,1.0,0.0,0.0,
                           0.0,0.0,1.0,0.0,
                           0.0,0.0,0.0,1.0};
    double C[size*size] = {0.0}; // 初始化结果矩阵为0

    CBLAS_LAYOUT layout = CblasRowMajor; // 这里用行主序,可根据你的代码调整
    std::cout << "=== 矩阵A ===" << std::endl;
    print_matrix(A, size, size, layout);

    std::cout << "=== 矩阵B ===" << std::endl;
    print_matrix(B, size, size, layout);

    // 计时并执行矩阵乘法
    auto start = std::chrono::high_resolution_clock::now();
    cblas_dgemm(layout, CblasNoTrans, CblasNoTrans,
                size, size, size,
                1.0, A, size,
                B, size,
                0.0, C, size);
    auto end = std::chrono::high_resolution_clock::now();
    std::chrono::duration<double> elapsed = end - start;

    std::cout << "=== 乘积矩阵C ===" << std::endl;
    print_matrix(C, size, size, layout);

    std::cout << "计算耗时:" << elapsed.count() << " 秒" << std::endl;

    return 0;
}

关键说明

  • std::setw(8) 和 std::fixed 是为了让矩阵元素对齐输出,可读性更强;
  • 调用cblas_dgemm时的layout参数必须和打印函数的layout保持一致,否则打印顺序会出错;
  • 如果你的代码用的是列主序(CblasColMajor),只需修改layout变量即可,打印函数会自动适配。

内容的提问来源于stack exchange,提问作者Anthony J. B.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 00:30:54