简体   繁体   English

C代码中的矩阵乘法

[英]matrix multiplication in c code

I have a code needs to do some matrix multiplication like 我有一个代码需要做一些矩阵乘法

    ML2=ML+uMc+c1+c2
    MC2=v*ML+(u*v+1)*Mc+c2

Where ML is MXM matrix of 其中ML是的MXM矩阵

    ML=[1 1 1 1....1;2 2 2 2...2......;M M M.....M]
    MC=[1 2 3 4 ...M;1 2 3 4...M......;1 2 3.....M]

u,v,c1 and c2 are constant of 8 bit. u,v,c1和c2为8位常数。

I want to find the values of ML2,MC2 in fast execution time using any fast library 我想使用任何快速库在快速执行时间内找到ML2,MC2的值

You did not state the platform you want this for but for matrix operations nothing is faster than the Intel Math Kernel Library for Intel CPUs 您没有说明要使用的平台,但对于矩阵运算,没有什么比用于Intel CPU的Intel Math Kernel Library快

http://software.intel.com/en-us/intel-mkl http://software.intel.com/zh-CN/intel-mkl

This gets as close as I have seen to the peak flops possible on the CPU. 正如我所见,这与CPU上可能出现的峰值触发器非常接近。 MKL, however, is expensive and closed source. 但是,MKL价格昂贵且封闭。 If you want a good open sourced and free alternative then check out Eigen. 如果您想要一个好的开源免费替代品,请查看Eigen。 This uses C++ but I don't know if you're really restricted to C only code. 它使用C ++,但我不知道您是否真的仅限于C语言代码。 Eigen also works well on other hardware such as AMD (Intel cripples it's library on AMD CPUs) and ARM. Eigen在其他硬件(例如AMD(英特尔削弱了其在AMD CPU上的库))和ARM上的性能也很好。

http://eigen.tuxfamily.org/index.php?title=3.0 http://eigen.tuxfamily.org/index.php?title=3.0

A third option to write one yourself. 自己写的第三个选择。 After a few weeks of effort it should not be too difficult to beat Eigen with AVX and OpenMP (Eigen only supports SSE) but it's highly unlikely you will beat MKL. 经过几周的努力,用AVX和OpenMP击败Eigen并不难(Eigen仅支持SSE),但是击败MKL的可能性很小。

For multiplication of matrixA(AxB) and matrixB(BxC) matrix to result in matrixC(AxC) 对于矩阵A(AxB)和matrixB(BxC)的乘法,得出矩阵C(AxC)

for(int i=0;i<l;i++)
{
    for(int j=0;j<n;j++)
    {
        matrixC[i][j]=0;
        for(int k=0;k<m;k++)
        {
            matrixC[i][j]=matrixC[i][j]+(matrixA[i][k] * matrixB[k][j]);
        }
    }
}

Since ML is bunch of identical vectors 1:M, and MC is just the transpose of ML, you don't need general matrix multiplication. 由于ML是一堆相同的向量1:M,而MC只是ML的转置,因此不需要通用矩阵乘法。 You can take algebraic short-cuts. 您可以采用代数捷径。

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

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