矩阵和向量乘法优化算法

时间:2012-03-27 20:48:47

标签: c optimization openmp matrix-multiplication divide-and-conquer

假设尺寸非常大(矩阵中最多10亿个元素)。我如何为矩阵向量产品实现缓存遗忘算法?基于维基百科,我需要递归划分和征服,但我觉得会有很多开销......这样做会有效吗?

跟进问答:OpenMP with matrices and vectors

1 个答案:

答案 0 :(得分:3)

问题的答案,"如何快速进行基本线性代数运算",随时随地查找并链接到适用于您平台的调谐BLAS库。例如,GotoBLAS(其工作正在OpenBLAS中继续),或较慢的自动调整ATLAS,或商业软件包,如英特尔的MKL。线性代数对于许多其他操作来说是如此基础,以至于为各种平台优化这些软件包需要付出巨大的努力,而且在下午几天你就没有机会想出一些东西&#39那将要竞争的工作。特定子程序调用您正在寻找一般密集矩阵 - 向量乘法是SGEMV / DGEMV / CGEMV / ZGEMV。

缓存无关的算法或自动调整,适用于您无法针对系统的特定缓存架构进行调整的情况 - 通常情况下这可能很好,但由于人们 愿意为BLAS例程做到这一点,然后使调优结果可用,意味着你最好只使用这些例程。

GEMV的内存访问模式非常简单,你不需要分而治之(对于矩阵转置的标准情况也一样) - 你只需找到缓存阻塞大小并使用它。在GEMV(y = Ax)中,您仍然需要扫描整个矩阵一次,因此没有什么可以重复使用(因此有效的缓存使用),但您可以尽可能多地尝试重用x所以你加载一次而不是(行数)次 - 你仍然希望访问A是缓存友好的。所以明显的缓存阻塞要做的就是打破块:

  A x -> [ A11 | A12 ] | x1 | = | A11 x1 + A12 x2 |
         [ A21 | A22 ] | x2 |   | A21 x1 + A22 x2 |

你当然可以递归地做到这一点。但是做一个天真的实现,它比简单的双循环慢,并且比正确的SGEMV库调用慢:

$ ./gemv
Testing for N=4096
Double Loop: time = 0.024995, error = 0.000000
Divide and conquer: time = 0.299945, error = 0.000000
SGEMV: time = 0.013998, error = 0.000000

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include "mkl.h"

float **alloc2d(int n, int m) {
    float *data = malloc(n*m*sizeof(float));
    float **array = malloc(n*sizeof(float *));
    for (int i=0; i<n; i++)
        array[i] = &(data[i*m]);
    return array;
}

void tick(struct timeval *t) {
    gettimeofday(t, NULL);
}

/* returns time in seconds from now to time described by t */
double tock(struct timeval *t) {
    struct timeval now;
    gettimeofday(&now, NULL);
    return (double)(now.tv_sec - t->tv_sec) + ((double)(now.tv_usec - t->tv_usec)/1000000.);
}

float checkans(float *y, int n) {
    float err = 0.;
    for (int i=0; i<n; i++)
        err += (y[i] - 1.*i)*(y[i] - 1.*i);
    return err;
}

/* assume square matrix */
void divConquerGEMV(float **a, float *x, float *y, int n,
                    int startr, int endr, int startc, int endc) {

    int nr = endr - startr + 1;
    int nc = endc - startc + 1;

    if (nr == 1 && nc == 1) {
        y[startc] += a[startr][startc] * x[startr];
    } else {
        int midr = (endr + startr+1)/2;
        int midc = (endc + startc+1)/2;
        divConquerGEMV(a, x, y, n, startr, midr-1, startc, midc-1);
        divConquerGEMV(a, x, y, n, midr,   endr,   startc, midc-1);
        divConquerGEMV(a, x, y, n, startr, midr-1, midc,   endc);
        divConquerGEMV(a, x, y, n, midr,   endr,   midc,   endc);
    }
}
int main(int argc, char **argv) {
    const int n=4096;
    float **a = alloc2d(n,n);
    float *x  = malloc(n*sizeof(float));
    float *y  = malloc(n*sizeof(float));
    struct timeval clock;
    double eltime;

    printf("Testing for N=%d\n", n);

    for (int i=0; i<n; i++) {
        x[i] = 1.*i;
        for (int j=0; j<n; j++)
            a[i][j] = 0.;
        a[i][i] = 1.;
    }

    /* naive double loop */
    tick(&clock);
    for (int i=0; i<n; i++) {
        y[i] = 0.;
        for (int j=0; j<n; j++) {
            y[i] += a[i][j]*x[j];
        }
    }
    eltime = tock(&clock);
    printf("Double Loop: time = %lf, error = %f\n", eltime, checkans(y,n));

    for (int i=0; i<n; i++) y[i] = 0.;

    /* naive divide and conquer */
    tick(&clock);
    divConquerGEMV(a, x, y, n, 0, n-1, 0, n-1);
    eltime = tock(&clock);
    printf("Divide and conquer: time = %lf, error = %f\n", eltime, checkans(y,n));

    /* decent GEMV implementation */
    tick(&clock);

    float alpha = 1.;
    float beta =  0.;
    int incrx=1;
    int incry=1;
    char trans='N';

    sgemv(&trans,&n,&n,&alpha,&(a[0][0]),&n,x,&incrx,&beta,y,&incry);
    eltime = tock(&clock);
    printf("SGEMV: time = %lf, error = %f\n", eltime, checkans(y,n));

    return 0;
}