国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【矩陣乘法】C++實(shí)現(xiàn)外部矩陣乘法

這篇具有很好參考價(jià)值的文章主要介紹了【矩陣乘法】C++實(shí)現(xiàn)外部矩陣乘法。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

問(wèn)題描述

? 使用文件和內(nèi)存模擬系統(tǒng)緩存,并利用矩陣乘法驗(yàn)證實(shí)際和理論情況。

算法思想

設(shè)計(jì)一個(gè)Matrix類,其中Matrix是存在磁盤中的一個(gè)二進(jìn)制文件,類通過(guò)保存的矩陣屬性來(lái)讀取磁盤。前八個(gè)字節(jié)為兩個(gè)int32,保存矩陣的行列數(shù)。

矩陣乘法 測(cè)試用例,小型項(xiàng)目,矩陣,c++,線性代數(shù),緩存,大數(shù)據(jù)

Matrix中有一個(gè)buffer成員為讀取到的數(shù)據(jù)緩存,通過(guò)pos屬性來(lái)確定其在矩陣中的位置。其映射邏輯為 r o w = ? p o s ÷ c o l S i z e ? , c o l = p o s m o d ?? c o l S i z e row=\lfloor pos \div colSize\rfloor, col=pos \mod colSize row=?pos÷colSize?,col=posmodcolSize。而緩存的管理模型則是模仿CPU的內(nèi)存緩存模型,采用寫回法。當(dāng)讀取矩陣中rowcol列時(shí),判斷邏輯如下。

? 完成磁盤交互后,其余操作即為正常的矩陣操作

功能模塊設(shè)計(jì)

矩陣類的成員變量,:

    Buffer buffer;
    const int rowSize;
    const int colSize;
    long cacheMissCount; // cache miss 次數(shù)
    long cacheCount;     // cache 訪問(wèn)次數(shù)
    int pos;             // pos = -1 means invalid buffer
    const int offset = 2 * INT_BYTE;
    fstream file;

? 矩陣值獲取和修改的函數(shù)

    int get(int row, int col)
    {
        int index = row * colSize + col;
        assert(index < rowSize * colSize && index >= 0);
        cacheCount++;
        if (index >= pos && index < pos + buffer.size && pos != -1)
            return buffer.get(index - pos);
        else
        {
            cacheMissCount++;
            readBuffer(index);
            return buffer.get(0);
        }
    }
    void set(int row, int col, int val)
    {
        assert(row <= rowSize && col <= colSize);
        if (pos <= indexOf(row, col) && indexOf(row, col) < pos + buffer.size)
            buffer.set(indexOf(row, col) - pos, val);
        else
        {
            cacheMissCount++;
            readBuffer(indexOf(row, col));
            buffer.set(0, val);
        }
        buffer.dirty = true;
    }

? 磁盤操作函數(shù)

    void writeBuffer()
    {
        file.seekp(pos * INT_BYTE + offset, ios::beg);
        for (int i = 0; i < buffer.size; i++)
        {
            file.write((char *)&buffer.get(i), INT_BYTE);
        }
        buffer.dirty = false;
    }

    void readBuffer(int startIndex)
    {
        if (buffer.dirty)
        {
            writeBuffer();
        }

        file.seekg(startIndex * INT_BYTE + offset, ios::beg);
        buffer.size = 0;
        for (int i = 0; i < buffer.capacity && startIndex + i < rowSize * colSize; i++)
        {
            int value;
            file.read((char *)&value, INT_BYTE);
            buffer.set(i, value);
            buffer.size++;
        }
        pos = startIndex;
    }

? 矩陣乘法函數(shù)

    Matrix *multiple_ijk(Matrix &right)
    {
        Matrix *t = new Matrix(rowSize, right.colSize, buffer.capacity);
        int i, j, k;
        for (i = 0; i < rowSize; i++)
        {
            for (j = 0; j < right.colSize; j++)
            {
                for (k = 0; k < right.rowSize; k++)
                {
                    t->set(i, j, t->get(i, j) + get(i, k) * right.get(k, j));
                }
            }
        }
        return t;
    }

    Matrix *multiple_ikj(Matrix &right)
    {
        Matrix *t = new Matrix(rowSize, right.colSize, buffer.capacity);
        int i, j, k;
        for (i = 0; i < rowSize; i++)
        {
            for (k = 0; k < right.rowSize; k++)
            {
                for (j = 0; j < right.colSize; j++)
                {
                    t->set(i, j, t->get(i, j) + get(i, k) * right.get(k, j));
                }
            }
        }
        return t;
    }

測(cè)試結(jié)果與分析

測(cè)試用例

class MatrixTest
{
public:
    void test1()
    {
        Matrix m(10, 10, 3, "m.txt");
        m.randomize();
        cout << m << endl;
        cout << "cache miss:" << m.getCacheMissCount() << endl;
        Matrix m1(10, 10, 3);
        m1.randomize();
        cout << m1 << endl;
        cout << "cache miss:" << m1.getCacheMissCount() << endl;
    }

    void test2()
    {
        Matrix m(10, 10, 3);
        cout << m << endl;
        m.set(0, 0, 9999);
        cout << m << endl;
        m.set(5, 0, 9999);
        cout << m << endl;
        m.set(3, 7, 9999);
        cout << m << endl;
    }
    void test3()
    {
        Matrix m1(2, 3, 5, "m1.txt");
        m1.randomize(100);
        cout << m1 << endl;
        Matrix m2(3, 4, 7, "m2.txt");
        m2.randomize(100);
        cout << m2 << endl;
        Matrix *m3 = m1.multiple_ijk(m2);
        cout << *m3 << endl;
        Matrix *m4 = m1.multiple_ikj(m2);
        cout << *m4 << endl;
        assert(m3->toString() == m4->toString());
    }
    void test4(int n, int cacheSize)
    {
        cout << "cache size is " << cacheSize << endl;
        Matrix m1(n, n, cacheSize, "m1.txt");
        cout << "initial m1(size is " << n << ")finished" << endl;
        Matrix m2(n, n, cacheSize, "m2.txt");
        cout << "initial m2(size is " << n << ")finished" << endl;
        Matrix *m3 = m1.multiple_ijk(m2);
        cout << "m1 ijk m2 finished" << endl;
        cout << "cache coutnt of m1: " << m1.getCacheCount() << ",	cache miss count of m1:" << m1.getCacheMissCount() << endl;
        cout << "cache coutnt of m2: " << m2.getCacheCount() << ",	cache miss count of m2:" << m2.getCacheMissCount() << endl;
        cout << "cache coutnt of m3: " << m3->getCacheCount() << ",	cache miss count of m3:" << m3->getCacheMissCount() << endl;
        cout << endl;
    }
    void test5(int n, int cacheSize)
    {
        cout << "cache size is " << cacheSize << endl;
        Matrix m1(n, n, cacheSize, "m1.txt");
        cout << "initial m1(size is " << n << ")finished" << endl;
        Matrix m2(n, n, cacheSize, "m2.txt");
        cout << "initial m2 finished" << endl;
        Matrix *m3 = m1.multiple_ikj(m2);
        cout << "m1 ijk m2(size is " << n << ")finished" << endl;
        cout << "cache coutnt of m1: " << m1.getCacheCount() << ",	cache miss count of m1:" << m1.getCacheMissCount() << endl;
        cout << "cache coutnt of m2: " << m2.getCacheCount() << ",	cache miss count of m2:" << m2.getCacheMissCount() << endl;
        cout << "cache coutnt of m3: " << m3->getCacheCount() << ",	cache miss count of m3:" << m3->getCacheMissCount() << endl;
        cout << endl;
    }
    void runTest()
    {
        for (int i = 5; i < 35; i += 5)
        {
            for (int j = 1; j < 5; j++)
            {
                test4(i, i / j);
                test5(i, i / j);
            }
        }
    }
};

實(shí)驗(yàn)測(cè)試

矩陣乘法 測(cè)試用例,小型項(xiàng)目,矩陣,c++,線性代數(shù),緩存,大數(shù)據(jù)

矩陣乘法 測(cè)試用例,小型項(xiàng)目,矩陣,c++,線性代數(shù),緩存,大數(shù)據(jù)

實(shí)驗(yàn)分析

假設(shè)矩陣為 C = A × B C=A\times B C=A×B,且cache大小小于矩陣的一行或一列,且A、B、C都是大小為n的方陣

對(duì)于ijk的情況:

? 每次讀取時(shí)發(fā)生一次磁盤訪問(wèn),而若cache當(dāng)中有臟數(shù)據(jù),則有另一次的寫入訪問(wèn)。對(duì)于矩陣C,因?yàn)槠渥鳛椴僮骶仃?(總是進(jìn)行+=操作)*,所以只要是C矩陣發(fā)生了cache miss其實(shí)是進(jìn)行了兩次磁盤訪問(wèn),而對(duì)于AB矩陣,發(fā)生cache miss 時(shí)只發(fā)生一次磁盤訪問(wèn)。

? cache miss 發(fā)生次數(shù)如下
C c a c h e ? m i s s = n 2 c a c h e ? s i z e A c a c h e ? m i s s = n 3 c a c h e ? s i z e B c a c h e ? m i s s = n 3 C_{cache\ miss} = \frac{n^2}{cache\ size}\\ A_{cache\ miss} = \frac{n^3}{cache\ size}\\ B_{cache\ miss} = n^3 Ccache?miss?=cache?sizen2?Acache?miss?=cache?sizen3?Bcache?miss?=n3
? 所消耗的總時(shí)間
t t o t a l = 2 T d i s k ? a c c e s s ? t i m e C c a c h e ? m i s s + T d i s k ? a c c e s s ? t i m e A c a c h e ? m i s s + T d i s k ? a c c e s s ? t i m e B c a c h e ? m i s s = n 3 T d i s k ? a c c e s s ? t i m e c a c h e ? s i z e ( 1 + 1 n + c a c h e ? s i z e ) t_{total}=2T_{disk\ access\ time}C_{cache\ miss}+T_{disk\ access\ time}A_{cache\ miss}+T_{disk\ access\ time}B_{cache\ miss}\\ =\frac{n^3T_{disk\ access\ time}}{cache\ size(1+\frac{1}{n}+cache\ size)} ttotal?=2Tdisk?access?time?Ccache?miss?+Tdisk?access?time?Acache?miss?+Tdisk?access?time?Bcache?miss?=cache?size(1+n1?+cache?size)n3Tdisk?access?time??
? 而當(dāng)順序化為ijk的時(shí)候,B矩陣的cache hit 率有所提高
C c a c h e ? m i s s = n 3 c a c h e ? s i z e A c a c h e ? m i s s = n 2 c a c h e ? s i z e B c a c h e ? m i s s = n 3 c a c h e ? s i z e C_{cache\ miss} = \frac{n^3}{cache\ size}\\ A_{cache\ miss} = \frac{n^2}{cache\ size}\\ B_{cache\ miss} = \frac{n^3}{cache\ size} Ccache?miss?=cache?sizen3?Acache?miss?=cache?sizen2?Bcache?miss?=cache?sizen3?
所消耗的總時(shí)間變?yōu)榱?br> t t o t a l = 2 T d i s k ? a c c e s s ? t i m e C c a c h e ? m i s s + T d i s k ? a c c e s s ? t i m e A c a c h e ? m i s s + T d i s k ? a c c e s s ? t i m e B c a c h e ? m i s s = n 3 T d i s k ? a c c e s s ? t i m e c a c h e ? s i z e ( 2 + 1 n ) t_{total}=2T_{disk\ access\ time}C_{cache\ miss}+T_{disk\ access\ time}A_{cache\ miss}+T_{disk\ access\ time}B_{cache\ miss}\\ =\frac{n^3T_{disk\ access\ time}}{cache\ size(2+\frac{1}{n})} ttotal?=2Tdisk?access?time?Ccache?miss?+Tdisk?access?time?Acache?miss?+Tdisk?access?time?Bcache?miss?=cache?size(2+n1?)n3Tdisk?access?time??文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-678391.html

到了這里,關(guān)于【矩陣乘法】C++實(shí)現(xiàn)外部矩陣乘法的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 【C++】矩陣的乘法

    【C++】矩陣的乘法

    先復(fù)習(xí)一下矩陣的乘法 。 已知 求AB。? 因?yàn)榫仃嘇是2×3矩陣,矩陣B是3×3矩陣,A的列數(shù)等于B的行數(shù),所以矩陣A與B可以相乘,乘積AB是一個(gè)2×3矩陣。 矩陣相乘時(shí)需要注意兩點(diǎn),一點(diǎn)是矩陣1的列數(shù)要等與矩陣2的行數(shù),一點(diǎn)是矩陣相乘后的矩陣 c[i][j] = a[i][k]*b[k][j]? ?由矩陣相

    2024年02月12日
    瀏覽(16)
  • 簡(jiǎn)單的小型C++項(xiàng)目怎么用CMAKE進(jìn)行管理

    簡(jiǎn)單的小型C++項(xiàng)目怎么用CMAKE進(jìn)行管理

    根目錄下共有兩個(gè)文件夾,分別為include、src,有兩個(gè)文件,分別為CMakeLists.txt和main.cpp 可以看出,include了func.h,且func.h的聲明在include文件夾下,定義在src文件夾下的func.cpp中 add_library 表示創(chuàng)建了一個(gè)靜態(tài)庫(kù),名字是func,用的是func.cpp這個(gè)文件 target_include_directories 表示讓 ..

    2023年04月22日
    瀏覽(20)
  • 【矩陣快速冪】封裝類及測(cè)試用例及樣例

    【矩陣快速冪】封裝類及測(cè)試用例及樣例

    視頻算法專題 通俗的說(shuō),就是矩陣的乘方。 題目、分析和原理見: 【動(dòng)態(tài)規(guī)劃】【矩陣快速冪】【滾動(dòng)向量】C++算法552. 學(xué)生出勤記錄 II 原解法用二維表示狀態(tài),改成一維。 i是缺勤數(shù)量,j是連續(xù)遲到數(shù),新的狀態(tài)為:3*i+j 6種狀態(tài),故轉(zhuǎn)移矩陣為6行6列,故結(jié)果矩陣為6列,

    2024年01月22日
    瀏覽(13)
  • 矩陣乘法,python簡(jiǎn)易實(shí)現(xiàn)

    矩陣乘法,python簡(jiǎn)易實(shí)現(xiàn)

    1.首先,先了解下矩陣乘法最基本的工作原理,可簡(jiǎn)易得理解成 C矩陣(i, j)的值是 由A矩陣 i 行依次與B矩陣 j 列相乘的求和,即: ?2.demo實(shí)現(xiàn) 3、基于矩陣結(jié)果是行和列的對(duì)應(yīng)相乘的累和迭代,所以選擇依次增加,核心算法: ? ? ?其中,選取 i、j、k進(jìn)行循環(huán)與迭代,k作為中

    2024年02月11日
    瀏覽(22)
  • markdown實(shí)現(xiàn)矩陣乘法

    [ x y 1 ] = [ f 0 O x 0 f O y 0 0 1 ] [ x ˉ y ˉ z ] (1) left[ begin{matrix} x \\\\ y \\\\ 1 end{matrix} right] = left[ begin{matrix} f 0 O_x\\\\ 0 f O_y\\\\ 0 0 1 end{matrix} right] left[ begin{matrix} bar{x} \\\\ bar{y} \\\\ z end{matrix} right] tag{1} ? x y 1 ? ? = ? f 0 0 ? 0 f 0 ? O x ? O y ? 1 ? ? ? x ˉ y ˉ ? z ? ? ( 1

    2024年02月09日
    瀏覽(19)
  • MPI實(shí)現(xiàn)矩陣向量乘法

    (1)問(wèn)題 MPI實(shí)現(xiàn)矩陣向量:Ab的乘積。其中A:100行100列,b為列向量。 (2)思路 將所有進(jìn)程分為兩部分,rank=0的進(jìn)程為master節(jié)點(diǎn),其余進(jìn)程為worker節(jié)點(diǎn)。 master節(jié)點(diǎn): (1)對(duì)A,b賦值,同時(shí)將b廣播出去(這里涉及一個(gè)對(duì)廣播這個(gè)函數(shù)不太熟悉的點(diǎn)) (2)對(duì)A進(jìn)行劃分,使其

    2024年02月11日
    瀏覽(17)
  • 使用cublas實(shí)現(xiàn)矩陣乘法

    使用cublas實(shí)現(xiàn)矩陣乘法

    使用CUDA寫一個(gè)矩陣乘法 C = A X B (矩陣維度: A: M X K, B: K X N, C: M X N ),當(dāng)然可以自己寫核函數(shù),但效率不如CUDA自帶的 cublas 算法效率高。使用 cublas 唯一值得注意的地方是, 在CPU中的矩陣數(shù)據(jù)存儲(chǔ)是行優(yōu)先存儲(chǔ),而在GPU中是列優(yōu)先存儲(chǔ) ,這相當(dāng)于對(duì)原矩陣做了一次轉(zhuǎn)置,我

    2024年02月16日
    瀏覽(18)
  • 矩陣乘法實(shí)現(xiàn)卷積運(yùn)算

    矩陣乘法實(shí)現(xiàn)卷積運(yùn)算

    ????????矩陣根據(jù)卷積核的大小進(jìn)行,從左到右、從上到i 下 的移動(dòng),對(duì)應(yīng)數(shù)據(jù)相乘再相加得到的數(shù)據(jù)為該區(qū)域的值。 ??????? ??????? ? ? ? ? 原理:根據(jù)對(duì)于相乘相加的機(jī)制,發(fā)現(xiàn)通過(guò)對(duì)卷積核填零構(gòu)成和輸入矩陣大小一致的矩陣,然后展平拼接起來(lái),

    2024年02月12日
    瀏覽(16)
  • 用Java實(shí)現(xiàn)矩陣乘法

    矩陣相乘需注意: ????????1、當(dāng)矩陣A的列數(shù)(column)等于矩陣B的行數(shù)(row)時(shí),A與B可以相乘。 ????????2、矩陣C的行數(shù)等于矩陣A的行數(shù),C的列數(shù)等于B的列數(shù)。 ????????3、乘積C的第m行第n列的元素等于矩陣A的第m行的元素與矩陣B的第n列對(duì)應(yīng)元素乘積之和 設(shè)a為

    2023年04月08日
    瀏覽(18)
  • 矩陣乘法(C語(yǔ)言實(shí)現(xiàn)),超詳細(xì)

    矩陣乘法(C語(yǔ)言實(shí)現(xiàn)),超詳細(xì)

    分別求得兩個(gè)矩陣的行數(shù)a1,b1以及列數(shù)a2,b2。 如果a1 == b1,并且a2 == b2則進(jìn)行乘法運(yùn)算

    2024年02月05日
    瀏覽(14)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包