2016-06-14 6 views
-2
私はCを持つ大規模な行列の乗算を実装しようとしている

こんにちはみんな++ここのコードです:C++行列乗算rowsとcols

main.cppに

#include <iostream> 
#include <ctime> 
#include "sauvegarder.h" 
#include "restaurer.h" 
using namespace std; 

const int ligne = 2048; 
const int colonne = 2048; 
int main() 
{ 
    static float host_matrice_1[ligne][colonne]; 
    static float host_matrice_2[ligne][colonne]; 
    static float host_matrice_3[ligne][colonne]; 
    clock_t sequentiel; 
    cudaEvent_t start, stop; 
    cudaEventCreate(&start); 
    cudaEventCreate(&stop); 
    //clock_t parallele; 

    //création des matrices avec des valeurs aléatoire 

    for (int i = 0; i < ligne; i++) 
    { 
     for (int j = 0; j < colonne; j++) 
     { 
      host_matrice_1[i][j] = rand() * 1000; 
      host_matrice_2[i][j] = rand() * 1000; 
     } 
    } 

    sauvegarder(host_matrice_1, "matrice_1.txt"); 
    sauvegarder(host_matrice_2, "matrice_2.txt"); 

    //debut de calcul de temps + multiplication 
    sequentiel = clock(); 

    for (int i = 0; i < ligne; i++) 
    { 
     for (int j = 0; j < colonne; j++) 
     { 
      host_matrice_3[i][j] = 0; 
      for (int k = 0; k < ligne; k++) 
      { 
       host_matrice_3[i][j] = host_matrice_3[i][j] + host_matrice_1[i][k] * host_matrice_2[k][j]; 
      } 
     } 
    } 

    sequentiel = clock() - sequentiel; 

    cout << "Temps Cpu: " << ((float)sequentiel)/CLOCKS_PER_SEC * 1000 << "ms" << endl; 
sauvegarde.h 

#include <iostream> 
#include <fstream> 

using namespace std; 
const int rows = 2048; 
const int cols = 2048; 
void sauvegarder(static float Mat[rows][cols], string filename); 

sauvegarde.cpp

#include "sauvegarder.h" 

void sauvegarder(static float Mat[rows][cols], string filename) 
{ 
    ofstream output_file(filename); 

    for (int ligne = 0; ligne != rows; ligne++) 
    { 
     if (ligne != 0) 
     { 
      output_file << '\n'; 
     } 
     for (int col = 0; col != cols; col++) 
     { 
      if (col != 0) 
      { 
       output_file << '\t'; 
      } 
      output_file << Mat[ligne][col]; 
     } 
    } 
} 

restaurer.h

#include <iostream> 
#include <fstream> 
#include <sstream> 
#include <string> 

using namespace std; 
const int ro = 2048; 
const int co = 2048; 
void restorer(static float mat[ro][co], string filename); 

restaurer.cpp

#include "restaurer.h" 
void restorer(static float mat[ro][co], string filename) 
{ 
    float x; 
    int row = 0; 
    int col = 0; 
    string lineA; 
    ifstream fileIN(filename); 

    //static float tmp; 
    while (getline(fileIN, lineA)) 
    { 
     //Pour les chaines de caracteres et pas caractere. 
     istringstream streamA(lineA); 
     col = 0; 
     while (streamA >> x) 
     { 
      mat[row][col] = x; 
      col++; 
     } 
     row++; 
    } 
} 

問題は、私はすべてのヘッダーにもmain.cppに、それを変更する必要がrowsとcolsの数を変更したい場合、どのように私は1つからそれを変更させることができるということですヘッダー。

+0

プログラムで実行時に行と列の数を調整できないのはなぜですか?たくさんの異なるプログラムが必要ですが、唯一の違いは行と列の数ですか? – PaulMcKenzie

+2

代わりに、あなたの配列のための小さなテンプレートのラッパーを使用することができます。 'template struct Mat { \t Mat(){ \t \t data = new T [N]; \t \tサイズ= N; \t}; \t intサイズ; \t T * data;}; 'これは1Dの場合ですが、この方法では次元を設定してサイズフィールドから取り出すことができます。 – Christoph

答えて

1

これはおそらく、C++プログラムの設計上の問題です。固定配列の代わりにオブジェクトまたはポインタ(動的メモリ割り当てを使用)を使用する必要があります。実行時に任意のサイズ(ネガを除く)を許可してください。

とにかく、あなたの質問に応え、あなたは、グローバルでのヘッダファイルを作成し、すべての必要なファイルにそのヘッダーを含めると、これらの定数を使用することができます。

// globals.h 
// Use preprocessor directives to define the constants once (you can also youse `#pragma once`) 
#ifndef __GLOBALS_H_ 
#define __GLOBALS_H_ 

const int MATRIX_COLS = 2048; 
const int MATRIX_ROWS = 2048; 

#endif /* __GLOBALS_H_ */ 

そして、このようなあなたのコードでそれを使用します。

#include "restaurer.h" 
#include "globals.h" 

void restorer(static float mat[MATRIX_ROWS][MATRIX_COLS], string filename) { 
    // ... 
} 

coroの定数をすべて置き換えて削除することを忘れないでください。