2016-06-14 132 views
-2

好家伙我试图实现与C++在这里大矩阵相乘是代码:C++矩阵乘法行列数

的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++; 
    } 
} 

的问题是,当我想换行和cols我需要改变它在每一个头,甚至在main.cpp中的号码,我怎样才能使它从一个多变头。

+0

为什么不让程序能够在运行时调整行数和列数?你想有一个不同的程序,唯一的区别是行数和列数? – PaulMcKenzie

+2

另外,你可以为你的数组使用一点模板包装。 'template struct Mat { Mat(){ \t \t data = new T [N]; \t \t size = N; \t}; \t int size; \t T * data; };'所以这是一维情况,但这种方法允许您设置维度并通过大小字段检索它。 – 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常数。