Jim

matrix solver

CC = gcc
CFLAGS=-I. -O2 -std=c99 -pedantic -Wall -g -fopenmp
LDFLAGS=-lpthread
%.o: %.c
$(CC) -c -o $@ $< $(CFLAGS)
main_omp: main_omp.o matrix.o
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
... ...
No preview for this file type
#include "matrix.h"
#include <stdio.h>
#include <stdlib.h>
int main(){
matrix *m1,*m2, *m3;
m1 = matrix_init (3,3);
m2 = matrix_init (3,3);
for(int i=0; i < matrix_get_rows(m1); i++){
for (int j=0; j < matrix_get_columns(m1); j++){
set(m1, i, j, 1);
}
}
//it has to be special (1) matrix
matrix_null (m2);
int n = matrix_get_rows(m2);
if (matrix_get_columns(m2) < n )
n = matrix_get_columns(m2);
for(int i=0; i < n; i++){
set(m2, i, i, 1);
}
m3 = matrix_summ (m1, m2);
matrix_print (m1);
matrix_print (m2);
matrix_print (m3);
matrix_free (m1);
matrix_free (m2);
matrix_free (m3);
return 0;
}
... ...
#include "matrix.h"
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
struct worker_data{
matrix *in1, *in2, *out;
pthread_cond_t work;
pthread_mutex_t work_mutex;
pthread_cond_t done;
pthread_mutex_t done_mutex;
};
typedef struct worker_data worker_data;
void * worker (void * _arg){
worker_data* arg=(struct worker_data*)_arg;
while(1){
//ready to work
pthread_mutex_lock(&arg->work_mutex);
//expection command to work
// pthread_cond_wait(&arg->work, &arg->work_mutex);
arg->out = matrix_summ (arg->in1, arg->in2);
pthread_cond_signal(&arg->done);
pthread_mutex_unlock(&arg->work_mutex);
}
// pthread_cond_signal(&arg->done);
return 0;
}
int main(){
worker_data arg;
// pthread_cond_init(&arg.work, NULL);
pthread_mutex_init(&arg.work_mutex, NULL);
pthread_cond_init(&arg.done, NULL);
pthread_mutex_init(&arg.done_mutex, NULL);
pthread_mutex_lock(&arg.work_mutex);
pthread_t tidy;
pthread_create (&tidy, NULL, worker, (void *)&arg);
arg.in1 = matrix_init (3,3);
arg.in2 = matrix_init (3,3);
for(int i=0; i < matrix_get_rows(arg.in1); i++){
for (int j=0; j < matrix_get_columns(arg.in1); j++){
set(arg.in1, i, j, 1);
}
}
//it has to be special (1) matrix
matrix_null (arg.in2);
int n = matrix_get_rows(arg.in2);
if (matrix_get_columns(arg.in2) < n )
n = matrix_get_columns(arg.in2);
for(int i=0; i < n; i++){
set(arg.in2, i, i, 1);
}
while (1){
pthread_mutex_unlock(&arg.work_mutex);
// matrix_print(arg.in1);
pthread_mutex_lock(&arg.done_mutex);
pthread_cond_wait(&arg.done, &arg.done_mutex);
pthread_mutex_unlock(&arg.done_mutex);
// pthread_cond_signal(&arg.work);
pthread_mutex_lock(&arg.work_mutex);
// pthread_cond_wait(&arg.done, &arg.done_mutex);
// pthread_mutex_unlock(&arg.done_mutex);
matrix_print(arg.out);
sleep(3);
//release first matrix (in1)
matrix_free(arg.in1);
arg.in1 = arg.out;
}
pthread_join (tidy, NULL);
matrix_print (arg.out);
matrix_free (arg.in1);
matrix_free (arg.in2);
matrix_free (arg.out);
pthread_cond_destroy(&arg.work);
pthread_mutex_destroy(&arg.work_mutex);
pthread_cond_destroy(&arg.done);
pthread_mutex_destroy(&arg.done_mutex);
return 0;
}
... ...
No preview for this file type
#include "matrix.h"
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
struct worker_data{
matrix *in1, *in2, *out;
pthread_cond_t work;
pthread_mutex_t work_mutex;
pthread_cond_t done;
pthread_mutex_t done_mutex;
};
typedef struct worker_data worker_data;
void * worker (void * _arg){
worker_data* arg=(struct worker_data*)_arg;
while(1){
//ready to work
pthread_cond_signal(&arg->done);
pthread_mutex_lock(&arg->work_mutex);
printf("Worker waiting\n");
//expection command to work
pthread_cond_wait(&arg->work, &arg->work_mutex);
printf("Worker waiting done\n");
arg->out = matrix_summ (arg->in1, arg->in2);
pthread_mutex_unlock(&arg->work_mutex);
}
pthread_cond_signal(&arg->done);
return 0;
}
int main(){
worker_data arg;
pthread_cond_init(&arg.work, NULL);
pthread_mutex_init(&arg.work_mutex, NULL);
pthread_cond_init(&arg.done, NULL);
pthread_mutex_init(&arg.done_mutex, NULL);
pthread_t tidy;
pthread_create (&tidy, NULL, worker, (void *)&arg);
arg.in1 = matrix_init (3,3);
arg.in2 = matrix_init (3,3);
for(int i=0; i < matrix_get_rows(arg.in1); i++){
for (int j=0; j < matrix_get_columns(arg.in1); j++){
set(arg.in1, i, j, 1);
}
}
//it has to be special (1) matrix
matrix_null (arg.in2);
int n = matrix_get_rows(arg.in2);
if (matrix_get_columns(arg.in2) < n )
n = matrix_get_columns(arg.in2);
for(int i=0; i < n; i++){
set(arg.in2, i, i, 1);
}
while (1){
// matrix_print(arg.in1);
pthread_mutex_lock(&arg.done_mutex);
write(2, "Main waiting\n", 13);
pthread_cond_wait(&arg.done, &arg.done_mutex);
pthread_mutex_unlock(&arg.done_mutex);
printf("Main waiting done\n");
pthread_cond_signal(&arg.work);
pthread_mutex_lock(&arg.done_mutex);
pthread_cond_wait(&arg.done, &arg.done_mutex);
pthread_mutex_unlock(&arg.done_mutex);
matrix_print(arg.out);
//release first matrix (in1)
matrix_free(arg.in1);
arg.in1 = arg.out;
}
pthread_join (tidy, NULL);
matrix_print (arg.out);
matrix_free (arg.in1);
matrix_free (arg.in2);
matrix_free (arg.out);
pthread_cond_destroy(&arg.work);
pthread_mutex_destroy(&arg.work_mutex);
pthread_cond_destroy(&arg.done);
pthread_mutex_destroy(&arg.done_mutex);
return 0;
}
... ...
No preview for this file type
#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;
}
... ...
No preview for this file type
#include "matrix.h"
#include <stdio.h>
#include <stdlib.h>
matrix* matrix_init(int rows, int columns){
matrix* ret;
if ( (ret = (matrix*) malloc (sizeof(matrix))) == NULL){
return 0;
}
ret->rows = rows;
ret->columns = columns;
if ( (ret->data = (double **) malloc(rows * sizeof(double *))) == NULL ) {
free(ret);
return 0;
}
for (int i = 0; i < rows; i++){
if ( ( ret->data[i] = (double *)malloc (columns * sizeof(double))) == NULL ) {
for (int j = 0; j < i; j++){
free(ret->data[j]);
}
free(ret->data);
free(ret);
return 0;
}
}
return ret;
}
void matrix_free(matrix* m)
{
for (int i = 0; i < m->rows; i++){
free (m->data[i]);
}
free(m->data);
free(m);
}
void set(matrix* m, int i, int j, double value)
{
m->data[i][j] = value;
}
double get(matrix* m, int i, int j){
return m->data[i][j];
}
int matrix_get_rows (matrix *m){
return m->rows;
}
int matrix_get_columns (matrix *m){
return m->columns;
}
void matrix_null (matrix* m){
int rows = matrix_get_rows(m);
int columns = matrix_get_columns(m);
for (int i = 0; i < rows; i++){
for (int j = 0; j < columns; j++){
set (m, i, j, 0);
}
}
}
void matrix_print(matrix* m){
for (int i=0; i < m->rows; i++){
for (int j=0; j < m->columns; j++){
printf ("%lf ", m->data[i][j]);
}
printf("\n");
}
}
matrix* matrix_summ (matrix *m1,matrix *m2){
matrix *m3;
int rows = matrix_get_rows(m1);
int columns = matrix_get_columns(m1);
int m2_columns = matrix_get_columns(m2);
int m2_rows = matrix_get_rows(m2);
//Matrix dimension check
if (rows != m2_rows || columns != m2_columns ){
return 0;
}
m3 = matrix_init(rows, columns);
for (int i = 0; i < rows; i++){
for (int j=0; j < columns; j++){
double res = get(m1, i, j);
res += get(m2, i,j);
set(m3, i, j, res);
}
}
return m3;
}
... ...
#ifndef MATRIX_H
#define MATRIX_H
struct matrix {
int rows;
int columns;
double **data;
};
typedef struct matrix matrix;
matrix* matrix_init(int rows, int columns);
void matrix_free(matrix* m);
void set(matrix* m, int i, int j, double value);
double get(matrix* m, int i, int j);
int matrix_get_rows (matrix *m);
int matrix_get_columns (matrix *m);
void matrix_null (matrix* m);
void matrix_print(matrix* m);
matrix* matrix_summ (matrix *m1,matrix *m2);
#endif
... ...
No preview for this file type
No preview for this file type