main_omp.c 1.3 KB
#include "matrix.h"
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>

matrix* matrix_mult (matrix *m1, matrix *m2){
	
	matrix *m3;
	int rows = matrix_get_rows(m1);
	int columns = matrix_get_columns(m1);
	int m2_rows = matrix_get_rows(m2);
	int m2_columns = matrix_get_columns(m2);
	double temp; // temp for multi

	//mult dimension check
	if (columns != m2_rows){
		return 0;
	}

	m3 = matrix_init (m2_columns, rows);

	#pragma omp parallel for private (temp)
	for (int i = 0; i < rows; i++){
		for (int j = 0; j < m2_columns; j++){
			temp = 0;
			for (int r = 0; r < columns; r++){
				temp += get (m1, i, r) * get (m2, r, j);
			}
			set (m3, i, j, temp);
		}
	}
	return m3;
}



int main(){

	matrix *in1, *in2, *out;

	in1 = matrix_init (10,10);
	in2 = matrix_init (10,10);	
	for(int i=0; i < matrix_get_rows(in1); i++){
		for (int j=0; j < matrix_get_columns(in1); j++){
			set(in1, i, j, i*j);
		}
	}

	//it has to be special (1) matrix
	matrix_null (in2);
	int n = matrix_get_rows(in2);
	if (matrix_get_columns(in2) < n ) 
		n = matrix_get_columns(in2);

	for(int i=0; i < n; i++){
		set(in2, i, i, 1);
	}

	out = matrix_mult(in1, in2);
	
//	matrix_print (in1);
//	printf("\n");
//	matrix_print (in2);
//	printf("\n");
	matrix_print (out);

	matrix_free(in1);
	matrix_free(in2);
	matrix_free(out);

	return 0;


}