Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <omp.h>
- #include <iostream>
- #include <stdlib.h>
- #include <time.h>
- #include <iomanip>
- using namespace std;
- void matrixPrint(double **matrix, int rows, int columns);
- double** newRandomMatrix(int rows, int columns, double low, double high);
- double** newMatrix(int rows, int columns);
- int main(int argc, char *argv[])
- {
- int i, j, k;
- int dim = 5;
- omp_set_num_threads(4);
- double **matrix1 = newMatrix(dim, dim);
- double **matrix2 = newMatrix(dim, dim);
- for(i = 0; i < dim; i++) {
- for(j = 0; j < dim; j++) {
- matrix1[i][j] = i + 3;
- matrix2[i][j] = i + 2;
- }
- }
- double **matrix3 = newMatrix(dim, dim);
- #pragma omp parallel for private(i,j,k)
- for (i = 0; i < dim; i++)
- {
- for (j = 0; j < dim; j++)
- {
- for (k = 0; k < dim; k++)
- {
- matrix3[i][j] += matrix1[i][k] * matrix2[k][j];
- }
- }
- }
- cout << "Matrica 1:" << endl;
- matrixPrint(matrix1, dim, dim);
- cout << "Matrica 2:" << endl;
- matrixPrint(matrix2, dim, dim);
- cout << "Matrica 3:" << endl;
- matrixPrint(matrix3, dim, dim);
- return 0;
- }
- void matrixPrint(double **matrix, int rows, int columns) {
- int i, j;
- std::cout << std::setprecision(2) << std::fixed;
- std::cout << "Matrix: " << rows << "x" << columns << std::endl;
- if (matrix == NULL)
- std::cout << "Matrix is NULL!";
- else {
- for (i = 0; i < rows; i++) {
- for (j = 0; j < columns; j++) {
- std::cout << matrix[i][j] << "\t";
- }
- std::cout << std::endl;
- }
- }
- std::cout << std::endl;
- }
- double** newMatrix(int rows, int columns) {
- int i, j;
- double **matrix = new double*[rows];
- for(i = 0;i < rows; i++)
- {
- matrix[i] = new double[columns];
- for(j = 0; j < columns; j++)
- {
- matrix[i][j] = 0;
- }
- }
- return matrix;
- }
- double** newRandomMatrix(int rows, int columns, double low, double high) {
- double **data;
- int i, j;
- data = newMatrix(rows, columns);
- for (i = 0; i < rows; i++) {
- for (j = 0; j < columns; j++) {
- data[i][j] = (high - low)*((double)rand()/RAND_MAX) + low;
- }
- }
- return data;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement