- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在使用 mkl
cblas_dgemm
,目前它与 CblasRowMajor
、CblasNoTrans
、CblasNotrans
,用于我的矩阵。
我知道 c
是行主要语言,而 dgemm
是列主要算法。我很想知道如果我链接到 mkl
,切换矩阵的顺序是否会对 cblas_dgemm
算法产生任何影响。 mkl
是否足够聪明,可以在幕后做一些我想做的事情来优化矩阵乘法?如果不是,使用 mkl
执行矩阵乘法的最佳方法是什么?
最佳答案
TL;DR:简而言之使用行优先或列优先排序执行矩阵-矩阵乘法并不重要使用 MKL(和其他 BLAS 实现)。
I know that c is a row major language, whereas dgemm is a column major algorithm.
DGEMM 不是列优先算法,它是用于计算矩阵与一般矩阵的矩阵乘积的 BLAS 接口(interface)。 DGEMM(和大多数 BLAS)的通用引用实现是 Netlib's这是用 Fortran 语言编写的。它采用列优先排序的唯一原因是因为 Fortran 是一种列优先排序语言。 DGEMM(以及相应的 BLAS Level 3 函数)不是专门用于column-major数据。
基础数学中的 DGEMM 执行二维 matrix-matrix multiplication .标准二维矩阵相乘算法要求您沿行遍历一个矩阵,沿列遍历另一个矩阵。要执行矩阵-矩阵乘法,AB = C,我们会将 A 的行乘以 B 产生 C。因此,输入矩阵的顺序无关紧要,因为一个矩阵必须沿其行遍历,另一个矩阵必须沿其列遍历。
英特尔 MKL 非常智能,可以在底层利用这一点,并为行优先 和列优先 数据提供完全相同的性能。
cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, ...);
和
cblas_dgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, ...);
将以类似的性能执行。我们可以用一个相对简单的程序来测试一下
#include <float.h>
#include <mkl.h>
#include <omp.h>
#include <stdio.h>
void init_matrix(double *A, int n, int m, double d);
void test_dgemm(CBLAS_LAYOUT Layout, double *A, double *B, double *C, const MKL_INT m, const MKL_INT n, const MKL_INT k, int nSamples, double *timing);
void print_summary(const MKL_INT m, const MKL_INT n, const MKL_INT k, const int nSamples, const double *timing);
int main(int argc, char **argv) {
MKL_INT n, k, m;
double *a, *b, *c;
double *timing;
int nSamples = 1;
if (argc != 5){
fprintf(stderr, "Error: Wrong number of arguments!\n");
fprintf(stderr, "usage: %s mMatrix nMatrix kMatrix NSamples\n", argv[0]);
return -1;
}
m = atoi(argv[1]);
n = atoi(argv[2]);
k = atoi(argv[3]);
nSamples = atoi(argv[4]);
timing = malloc(nSamples * sizeof *timing);
a = mkl_malloc(m*k * sizeof *a, 64);
b = mkl_malloc(k*n * sizeof *a, 64);
c = mkl_calloc(m*n, sizeof *a, 64);
/** ROW-MAJOR ORDERING **/
test_dgemm(CblasRowMajor, a, b, c, m, n, k, nSamples, timing);
/** COLUMN-MAJOR ORDERING **/
test_dgemm(CblasColMajor, a, b, c, m, n, k, nSamples, timing);
mkl_free(a);
mkl_free(b);
mkl_free(c);
free(timing);
}
void init_matrix(double *A, int n, int m, double d) {
int i, j;
#pragma omp for schedule (static) private(i,j)
for (i = 0; i < n; ++i) {
for (j = 0; j < m; ++j) {
A[j + i*n] = d * (double) ((i - j) / n);
}
}
}
void test_dgemm(CBLAS_LAYOUT Layout, double *A, double *B, double *C, const MKL_INT m, const MKL_INT n, const MKL_INT k, int nSamples, double *timing) {
int i;
MKL_INT lda = m, ldb = k, ldc = m;
double alpha = 1.0, beta = 0.0;
if (CblasRowMajor == Layout) {
printf("\n*****ROW-MAJOR ORDERING*****\n\n");
} else if (CblasColMajor == Layout) {
printf("\n*****COLUMN-MAJOR ORDERING*****\n\n");
}
init_matrix(A, m, k, 0.5);
init_matrix(B, k, n, 0.75);
init_matrix(C, m, n, 0);
// First call performs any buffer/thread initialisation
cblas_dgemm(Layout, CblasNoTrans, CblasNoTrans, m, n, k, alpha, A, lda, B, ldb, beta, C, ldc);
double tmin = DBL_MAX, tmax = 0.0;
for (i = 0; i < nSamples; ++i) {
init_matrix(A, m, k, 0.5);
init_matrix(B, k, n, 0.75);
init_matrix(C, m, n, 0);
timing[i] = dsecnd();
cblas_dgemm(Layout, CblasNoTrans, CblasNoTrans, m, n, k, alpha, A, lda, B, ldb, beta, C, ldc);
timing[i] = dsecnd() - timing[i];
if (tmin > timing[i]) tmin = timing[i];
else if (tmax < timing[i]) tmax = timing[i];
}
print_summary(m, n, k, nSamples, timing);
}
void print_summary(const MKL_INT m, const MKL_INT n, const MKL_INT k, const int nSamples, const double *timing) {
int i;
double tavg = 0.0;
for(i = 0; i < nSamples; i++) {
tavg += timing[i];
}
tavg /= nSamples;
printf("#Loop | Sizes m n k | Time (s)\n");
for(i = 0; i < nSamples; i++) {
printf("%4d %12d %3d %3d %6.4f\n", i + 1 , m, n, k, timing[i]);
}
printf("Summary:\n");
printf("Sizes m n k | Avg. Time (s)\n");
printf(" %8d %3d %3d %12.8f\n", m, n, k, tavg);
}
在我的系统上产生
$ ./benchmark_dgemm 1000 1000 1000 5
*****ROW-MAJOR ORDERING*****
#Loop | Sizes m n k | Time (s)
1 1000 1000 1000 0.0589
2 1000 1000 1000 0.0596
3 1000 1000 1000 0.0603
4 1000 1000 1000 0.0626
5 1000 1000 1000 0.0584
Summary:
Sizes m n k | Avg. Time (s)
1000 1000 1000 0.05995692
*****COLUMN-MAJOR ORDERING*****
#Loop | Sizes m n k | Time (s)
1 1000 1000 1000 0.0597
2 1000 1000 1000 0.0610
3 1000 1000 1000 0.0581
4 1000 1000 1000 0.0594
5 1000 1000 1000 0.0596
Summary:
Sizes m n k | Avg. Time (s)
1000 1000 1000 0.05955171
我们可以看到column-major 排序时间和row-major 排序时间之间的差别很小。 列优先 0.0595 秒 与 行优先 0.0599 秒。再次执行此操作可能会产生以下结果,其中行优先计算快了 0.00003 秒。
$ ./benchmark_dgemm 1000 1000 1000 5
*****ROW-MAJOR ORDERING*****
#Loop | Sizes m n k | Time (s)
1 1000 1000 1000 0.0674
2 1000 1000 1000 0.0598
3 1000 1000 1000 0.0595
4 1000 1000 1000 0.0587
5 1000 1000 1000 0.0584
Summary:
Sizes m n k | Avg. Time (s)
1000 1000 1000 0.06075310
*****COLUMN-MAJOR ORDERING*****
#Loop | Sizes m n k | Time (s)
1 1000 1000 1000 0.0634
2 1000 1000 1000 0.0596
3 1000 1000 1000 0.0582
4 1000 1000 1000 0.0582
5 1000 1000 1000 0.0645
Summary:
Sizes m n k | Avg. Time (s)
1000 1000 1000 0.06078266
关于c - MKL 是否为*主要订单优化 cblas?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32445741/
我尝试编写一个有多个链接表的解决方案。现在我有另一个问题: 我想将返回的行数限制为 1000。但我想显示 ID 1-1000,下一页 1001-2000。ID可以以不规则的顺序存储在数据库中(ID 1
我已经尝试申请 Drupal 商业优惠券大约 2 天了。我已经负责验证优惠券,但目前在尝试兑换优惠券时遇到了困难。 所以在我的回调函数中,我正在调用: my_module_coupons_coupon
[问]请帮忙,比如有一个数据 tbl_user | Id | name | | 1 | Bayu | | 2 | Indra | | 3 | Rangga | tbl_data | Id | user
我在 Android 应用程序中使用的一些 Parcelable 自定义类遇到了问题,我设法以一种非常奇怪的方式解决了这个问题。 仅在少数特定情况下,我在读取 parcelable 时发生了崩溃(这让
我一直在做一个项目,我需要在数据库中存储订单列表(在本例中为食品)。 我曾尝试四处寻找存储此类列表的最佳方式,但找不到任何方法。 目前,我将数据存储在 phpMyAdmin/SQL 中,订单存储为要打
目录 1、背景简介 2、订单业务 1、订单体系 2、流程管理 2
HBase案例:客户/订单 假设HBase 用于存储客户和订单信息。有两种核心记录类型被摄取:客户记录类型和订单记录类型。 客户记录类型将包含您通常期望的所有内容: 客户编号 客户名称
C-x C-b 显示缓冲区列表。首先是自然顺序,最近使用的缓冲区在顶部,隐藏的缓冲区在底部。 在那里,我现在可以按名称、大小、模式和文件对缓冲区进行排序。但是一旦我点击了这样的选项,我就无法回到原来的
我为 Woocommerce 添加了一个新的排序选项,它将按最低价格排序。我所有的价格都存储在一个自定义属性中,连同一些其他序列化数据。我想要的是有一个回调函数来反序列化这些数据,检查最低价格并按该价
想象一下我有一张 table : ID field1 field2 --- ------- ------ 111 1 11113 112
Kotlin forEach 是按数组的实际顺序遍历数组还是有时可能按其他顺序遍历数组?我的意思是这是否总是打印 1,2,3,...9 或者它可能会打印类似 1,5,3,4,... val numbe
我在 woocommerce 3+ 上创建了 html 电子邮件模板,但我无法通过订单 ID 获取订单项。我试过这个,但对我不起作用。 get_items(); foreach
我对将我自己的内部广告与 AdMob 的广告一起展示并使用按重要性顺序设置 eCPM 值的问题感到有些困惑。 我目前只与 AdMobs 的网络一起转换一个自家广告。 从常见问题解答和 AdMob 帮助
我正在尝试构建一个电子商务数据库,但我不了解订单,产品和客户之间的关系。 有很多数据库示例,但是它们太复杂了。是否有关于可能的表和关系的更简单的解释或示例。 谢谢。 最佳答案 如果客户可以拥有多个订单
我必须对电子商务系统进行一些更改以添加一些附加信息,并希望借此机会进行一些改进并使其更加灵活。当客户下订单时,我们必须为每个订购的商品存储几项信息;例如,产品价格、运费、征收的税款、所做的任何调整。
我正在尝试新的 ASP.NET bundle 功能,但似乎无法让我的自定义排序正常工作。这是我的 JS 文件: bootstrap.js bootstrap.min.js jquery-1.7.2.i
我正在尝试以下代码,并希望获取日期之间的所有订单并打印它们 $orders = $my_query->posts; $order = wc_get_order( $order_id ); $or
我有 ORMLite 数据库对象,它有一个字段: @ForeignCollectionField(eager = true) public ForeignCollection blocks; 现在,当
除了调用 event_list_attendees 并寻呼与会者以尝试找到正确的用户匹配之外,是否有其他方法可以获取门票/订单的用户条形码 ID?这种方法会增加 eventbrite 服务器的负担,并
这个问题已经有答案了: What is a NullPointerException, and how do I fix it? (12 个回答) 已关闭 5 年前。 我制作了订单食品应用程序。当我单
我是一名优秀的程序员,十分优秀!