Search code examples
c++matrixmultiplication

C++ matrix multiplication rows and cols


Hello guys i'm trying to implement large matrix multiplication with c++ here is the code :

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

the problem is that when i want to change number of rows and cols i need to change it in every header and even in main.cpp , how can i make it changeable from one header .


Solution

  • That's probably a bad design for a c++ program. You should be using objects or pointers (with dynamic memory allocation) instead of fixed arrays, allowing any size at runtime (except negatives!).

    Anyways, responding to your question, you can create a header file with globals, include that header on every required file, and use those constants:

    // 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_ */
    

    And use it in your code like this:

    #include "restaurer.h"
    #include "globals.h"
    
    void restorer(static float mat[MATRIX_ROWS][MATRIX_COLS], string filename) {
      // ...
    }
    

    Don't forget to replace and remove all of yours co and ro constants.