C++;矩阵乘法行和列 大家好,我尝试用C++实现大矩阵乘法,这里是代码:
main.cppC++;矩阵乘法行和列 大家好,我尝试用C++实现大矩阵乘法,这里是代码:,c++,matrix,multiplication,C++,Matrix,Multiplication,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
#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);
问题是,当我想改变行和COL的数量时,我需要在每个报头中改变它,甚至在MIN .CPP中,我如何才能使它从一个报头改变。 < P>这可能是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) {
// ...
}
不要忘记替换和删除所有的
co
和ro
常量。为什么程序不能在运行时调整行数和列数?您希望有无数不同的程序,唯一的区别是行和列的数量?或者,您可以为数组使用一个小的模板包装器<代码>模板结构Mat{Mat(){data=newt[N];size=N;};int size;T*data;}代码>这是1D的情况,但这种方法允许您设置维度并通过大小字段检索它。
#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++;
}
}
// 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) {
// ...
}