10000 X10000の行列乗算

エージェイ・カルティク

10,000 X 10,000の次元の2つの行列の行列乗算を実行する必要があります。各要素は、1から10,000の範囲でランダムに生成されます。スレッドあり(25スレッド)とスレッドなしでそれを行い、時間を比較する必要があります。

単純な行列乗算アルゴリズムO(n 3)を使用しています。スレッドのないプログラムは永久に(1日以上)実行されており、実行しようとするとスレッドバージョンが異常終了します。1000 X1000マトリックスで正常に動作します

CC prog.cc -lpthread -lposix4大学のサーバーでコンパイルしましたこれがスレッド化されていないバージョンです

/*
Compiler: Unix Server 

This program performs matrix multiplication of two 10000*10000 matrices without threads
The purpose of the program is to demonstrate the performance gain using threads 
against not using threads to perform the same computation on different data.
*/

    #include <pthread.h>
    #include <iostream.h>
    #include <semaphore.h>
    #include <unistd.h>
    #include<math.h>
    
    int main()
    {
        double **A;//Matrix A
        double **B;//Matrix B
        double **C;//Output Matrix C
        const int MATRIX_DIMENSION = 5000;
    
        //Assign Matrix A first dimension
        //-----------------------------------------------------------
        A = new double*[MATRIX_DIMENSION];
        //Assign second dimension
        for(int i = 0; i < MATRIX_DIMENSION; i++)
        {
            A[i] = new double[MATRIX_DIMENSION];
        }
        //Assign Matrix B first dimension
        B = new double*[MATRIX_DIMENSION];
        //Assign second dimension
        for(int i = 0; i < MATRIX_DIMENSION; i++)
        {
            B[i] = new double[MATRIX_DIMENSION];
        }
        //Assign Matrix C first dimension
        C = new double*[MATRIX_DIMENSION];
        //Assign second dimension
        for(int i = 0; i < MATRIX_DIMENSION; i++)
        {
            C[i] = new double[MATRIX_DIMENSION];
        }
        //-----------------------------------------------------------
        
        //Generate random numbers for matrices A and B and assign C to 0
        for(int i=0;i<MATRIX_DIMENSION;i++)
        {
            for(int j=0;j<MATRIX_DIMENSION;j++)
            {
                A[i][j] = rand() % 10000;
                B[i][j] = rand() % 10000;
                C[i][j] = 0; // initialize C to zero
            }
        }
        //-----------------------------------------------------------
        //Do the matrix multiplication
                                                                                                                                                                                                                                            
        for(int i=0;i<MATRIX_DIMENSION;i++)
        {
            for(int j=0 ;j<MATRIX_DIMENSION; j++)
            {
                for(int k=0;k<MATRIX_DIMENSION;k++)
                {
                    C[i][j]+=A[i][k]*B[k][j];
                }
            }                                                                                                                                                                                                                                                                                                                                           
        }                               
        
        //-----------------------------------------------------------
        //delete the dynamic memory of A
        for (int i = 0; i < MATRIX_DIMENSION; i++)
        {
            delete[] A[i];
        }
        delete[] A;
        //delete the dynamic memory of B
        for (int i = 0; i < MATRIX_DIMENSION; i++)
        {
            delete[] B[i];
        }
        delete[] B;
        //delete the dynamic memory of C
        for (int i = 0; i < MATRIX_DIMENSION; i++)
        {
            delete[] C[i];
        }
        delete[] C;
        //-----------------------------------------------------------
        return(0);
    }

これがスレッドバージョンです

/*
Name: 
Compiler: Unix Server 

This program performs matrix multiplication of two 10000*10000 matrices without threads
The purpose of the program is to demonstrate the performance gain using threads 
against not using threads to perform the same computation on different data.
*/

    #include <pthread.h>
    #include <iostream.h>
    #include <semaphore.h>
    #include <unistd.h>
    #include<math.h>
    
    //Global variables
        double **A;//Matrix A
        double **B;//Matrix B
        double **C;//Output Matrix C
        const int MATRIX_DIMENSION = 10000; //We need a 10000 X 10000 Matrix
        const int NUM_THREADS = 25; // One thread completes 1/25th of the work
        const int THREAD_DIMENSION = MATRIX_DIMENSION / NUM_THREADS; //Array that each thread will operate on
        pthread_t * thread[NUM_THREADS];
        
        /***************************************************************************
        Function that does matrix multiplication of 1/25th of the whole matrix,
        The division is done by dividing the Matrix into row's all 1/25 of the total matrix
        Each row of Matrix A operates on all the columns of Matrix B to get corresponding elements of Matrix C
        Parameter : arg, this is used as and index for which part of the Matrix this particular thread operates on
        Return type: void
        ****************************************************************************/
    void *MatrixMul (void * arg)
    {
        int index;
        index = (int) arg;
        int operation_Lower_Limit = ((index+1) * THREAD_DIMENSION) - THREAD_DIMENSION  ; //Multiplication starting row
        int operation_Upper_Limit = ((index+1) * THREAD_DIMENSION) - 1; //Multiplication ending row
        
        for(int i=operation_Lower_Limit;i<=operation_Upper_Limit;i++) //only 1/25th of Matrix A is used
        {
            for(int j=0 ;j<MATRIX_DIMENSION; j++) // The whole B matrix is used
            {
                for(int k=0;k<MATRIX_DIMENSION;k++)
                {
                    C[i][j]+=A[i][k]*B[k][j];
                }
            }                                                                                                                                                                                                                                                                                                                                           
        }
        return NULL;
    }
    
    int main()
    {
        
        srand(time(0));
        //Assign memory for threads
        for(int i=0;i < NUM_THREADS;i++)
        {
        thread[i] = new pthread_t;
        }
    
        //Assign Matrix A first dimension
        //-----------------------------------------------------------
        A = new double*[MATRIX_DIMENSION];
        //Assign second dimension
        for(int i = 0; i < MATRIX_DIMENSION; i++)
        {
            A[i] = new double[MATRIX_DIMENSION];
        }
        //Assign Matrix B first dimension
        B = new double*[MATRIX_DIMENSION];
        //Assign second dimension
        for(int i = 0; i < MATRIX_DIMENSION; i++)
        {
            B[i] = new double[MATRIX_DIMENSION];
        }
        //Assign Matrix C first dimension
        C = new double*[MATRIX_DIMENSION];
        //Assign second dimension
        for(int i = 0; i < MATRIX_DIMENSION; i++)
        {
            C[i] = new double[MATRIX_DIMENSION];
        }
        //-----------------------------------------------------------
        
        
        
        //Generate random numbers for matrices A and B and assign C to 0
        for(int i=0;i<MATRIX_DIMENSION;i++)
        {
            for(int j=0;j<MATRIX_DIMENSION;j++)
            {
                A[i][j] = rand() % 10000;
                B[i][j] = rand() % 10000;
                C[i][j] = 0; // initialize C to zero
            }
        }
        //-----------------------------------------------------------
        //Do the matrix multiplication
        
        for(int i=0;i<NUM_THREADS;i++)
        {
        pthread_create( thread[ i ], NULL, (MatrixMul), (void *) (i) );
        }
            
        
        //wait for all the threads to complete execution
        for(int i=0;i<NUM_THREADS;i++)
        {
        pthread_join(*thread[i],NULL);
        }
        
        //-----------------------------------------------------------
        //delete the dynamic memory of A
        for (int i = 0; i < MATRIX_DIMENSION; i++)
        {
            delete[] A[i];
        }
        delete[] A;
        //delete the dynamic memory of B
        for (int i = 0; i < MATRIX_DIMENSION; i++)
        {
            delete[] B[i];
        }
        delete[] B;
        //delete the dynamic memory of C
        for (int i = 0; i < MATRIX_DIMENSION; i++)
        {
            delete[] C[i];
        }
        delete[] C;
        //-----------------------------------------------------------
        return(0);
    }
martijn_himself

プログラムのメモリが不足していませんか?もしそうなら、乗算を実行しているときに、おそらくメモリの一部を解放できますか?

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Cで10000階乗

分類Dev

SQLite Delete 10000 rows

分類Dev

Underfull \hbox (badness 10000) in table

分類Dev

トーチで2つの10000 * 10000行列をほぼゼロの時間で乗算するにはどうすればよいですか?なぜ速度が349 msから999 µsに大きく変化するのですか?

分類Dev

SQLite10000行を削除

分類Dev

Using a grammar of 10000 words with micorosft.speech

分類Dev

Retrieve first 10000 records from the database - grails

分類Dev

How to spilt the 10000 records in batch using PHP

分類Dev

Math.pow(10、n)の計算方法、n> = 10000

分類Dev

{タイムアウト:10000}の代替

分類Dev

Firestores 10000書き込み/秒の制限

分類Dev

Tensorflow:MNISTでのInvalidArgumentError、[55000]と[10000]

分類Dev

10000x10000の画像シーケンスをビデオにトリミングします

分類Dev

10000を超える半数体、小数を削除

分類Dev

10000を超える半数体、小数を削除

分類Dev

2 ^(n)を計算します。ここで0 <n <10000

分類Dev

IndexError:インデックス10000は、サイズ10000の軸0の範囲外です

分類Dev

10000レコードからx個の最小値を選択するExcel

分類Dev

iOSアプリで10000x8000ピクセルの画像を表示する

分類Dev

Numpy の「ValueError: operands could not beブロードキャスト with shape (10000,) (10000,10) 」に対処する方法

分類Dev

Doing a binary classification using xgboost, getting a 10000x2 yhat

分類Dev

What does write_cr0(read_cr0() | 0x10000) do?

分類Dev

この2つの違い:BigInteger.valueOf(10000)とBigInteger.valueOf(0010000)?

分類Dev

Freebaseクエリ-10000人の最も人気のある人々

分類Dev

R:文字制限(10000バイト)に対するreadBinの回避策?

分類Dev

SQLインポート10000以上の.csvファイル

分類Dev

10000から1800000までの正規表現

分類Dev

テーブル内の不足している\ hbox(badness 10000)

分類Dev

約10000クラスのC ++プログラム

Related 関連記事

  1. 1

    Cで10000階乗

  2. 2

    SQLite Delete 10000 rows

  3. 3

    Underfull \hbox (badness 10000) in table

  4. 4

    トーチで2つの10000 * 10000行列をほぼゼロの時間で乗算するにはどうすればよいですか?なぜ速度が349 msから999 µsに大きく変化するのですか?

  5. 5

    SQLite10000行を削除

  6. 6

    Using a grammar of 10000 words with micorosft.speech

  7. 7

    Retrieve first 10000 records from the database - grails

  8. 8

    How to spilt the 10000 records in batch using PHP

  9. 9

    Math.pow(10、n)の計算方法、n> = 10000

  10. 10

    {タイムアウト:10000}の代替

  11. 11

    Firestores 10000書き込み/秒の制限

  12. 12

    Tensorflow:MNISTでのInvalidArgumentError、[55000]と[10000]

  13. 13

    10000x10000の画像シーケンスをビデオにトリミングします

  14. 14

    10000を超える半数体、小数を削除

  15. 15

    10000を超える半数体、小数を削除

  16. 16

    2 ^(n)を計算します。ここで0 <n <10000

  17. 17

    IndexError:インデックス10000は、サイズ10000の軸0の範囲外です

  18. 18

    10000レコードからx個の最小値を選択するExcel

  19. 19

    iOSアプリで10000x8000ピクセルの画像を表示する

  20. 20

    Numpy の「ValueError: operands could not beブロードキャスト with shape (10000,) (10000,10) 」に対処する方法

  21. 21

    Doing a binary classification using xgboost, getting a 10000x2 yhat

  22. 22

    What does write_cr0(read_cr0() | 0x10000) do?

  23. 23

    この2つの違い:BigInteger.valueOf(10000)とBigInteger.valueOf(0010000)?

  24. 24

    Freebaseクエリ-10000人の最も人気のある人々

  25. 25

    R:文字制限(10000バイト)に対するreadBinの回避策?

  26. 26

    SQLインポート10000以上の.csvファイル

  27. 27

    10000から1800000までの正規表現

  28. 28

    テーブル内の不足している\ hbox(badness 10000)

  29. 29

    約10000クラスのC ++プログラム

ホットタグ

アーカイブ