Created
January 16, 2013 23:24
-
-
Save LauraSuh/4551968 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** TECNICAS DE PROGRAMACAO */ | |
/** Primeiro semestre de 2012 */ | |
/** Aluna: Laura Barros Martins */ | |
/** Matricula: 10/0033440 */ | |
#include <stdio.h> | |
#include <time.h> | |
#include <unistd.h> | |
#include <assert.h> | |
#include <stdlib.h> | |
#include "BibMatriz_otm.h" | |
#define Ponter_MATRIX_ELEMENT(pMat,i,j) (*(&pMat->elems[i*pMat->ncols + j])) | |
//Ponter_MATRIX_ELEMENT aponta para o elemento a matriz pMat na posicao linha i coluna j | |
/** Esta funcao aloca espaco na memoria para uma matriz de nlinhas e ncoluas, e especifica que as nlinhas e n colunas serao as nlines e ncols da matriz respectivamente */ | |
void matrixNew(matrix *mat, int lines, int cols) | |
{ | |
if(lines > 0 && cols > 0) | |
{ | |
mat->elems = (float*)malloc(cols*lines*sizeof(float)); //reserva espaco na memoria para uma matriz lines x cols | |
assert(mat->elems!=NULL); | |
mat->ncols = cols; | |
mat->nlines = lines; | |
printf("\n Matriz %i x %i alocada\n", mat->nlines, mat->ncols); | |
} | |
else | |
{ | |
printf("\n Nao foi possivel construir matrix: tamanho linha/coluna = 0"); | |
return; | |
} | |
} | |
/** Libera o espaco alocado pelos elementos da matriz que esta sendo passada*/ | |
void matrixDispose(matrix *mat) | |
{ | |
assert(mat->elems!=NULL);//o assert é feito antes de definir qualquer variável. Caso falhe, nenhuma variavel foi declarada em vão. | |
free(mat->elems); | |
} | |
/** Insere um valor value na linha i e coluna j da matriz passada*/ | |
void matrixInsert(matrix *A, int i, int j, float value) | |
{ | |
assert(i<A->nlines); //verificacoes de seguranca | |
assert(j < A->ncols); | |
A->elems[i*A->ncols+j] = value; | |
} | |
/** Imprime todos os elementos da matriz passada*/ | |
void matrixPrint(matrix *A) | |
{ | |
int i, j; | |
for(i=0;i<A->nlines;++i) | |
{ | |
printf("\n"); | |
for(j=0;j<A->ncols;++j) | |
{ | |
printf("%f ",Ponter_MATRIX_ELEMENT(A,i,j)); | |
} | |
} | |
printf("\n"); | |
printf("\n"); | |
} | |
/** Especifica a estrutura group a ser passada para a thread e o numero de threads a serem usadas*/ | |
void matrixMult(matrix *A, matrix *B, matrix *C) | |
{ | |
assert(A->ncols==B->nlines); | |
int i = 0; | |
group mats; //declara um novo grupo | |
mats.totalThreads = 5; | |
pthread_t tid[mats.totalThreads]; //cria as threads ID | |
mats.matrixes[0] = A; | |
mats.matrixes[1] = B; | |
mats.matrixes[2] = C; | |
for(i=0;i<mats.totalThreads;i++) | |
{ | |
mats.currentLine = i; | |
pthread_create(&tid[i],NULL,thread_matrixMult,(void*)&mats); //chama a funcao que vai realizar o calculo, passando como parametro o grupo | |
usleep(200); | |
} | |
for(i=0;i<mats.totalThreads;i++) | |
{ | |
pthread_join(tid[i],NULL); //espera que cada tread termine para continuar o codigo | |
} | |
} | |
/** Multiplica a matriz primeiro parametro pela matriz segundo parametro e coloca a resposta na matriz terceiro parametro*/ | |
void thread_matrixMult(void *param) | |
{ | |
int i, j, k, temp; | |
float sum; | |
group *mats = (group*)param; | |
temp = mats->currentLine; | |
for(i = temp; i < mats->matrixes[0]->nlines; i = i+mats->totalThreads) | |
{ | |
for (j=0; j< mats->matrixes[1]->ncols; ++j) | |
{ | |
for (k=0; k< mats->matrixes[0]->nlines; ++k) | |
{ | |
sum += Ponter_MATRIX_ELEMENT(mats->matrixes[0],i,k)*Ponter_MATRIX_ELEMENT(mats->matrixes[1],k,j); | |
} | |
matrixInsert(mats->matrixes[2],i,j,sum); | |
sum = 0; | |
} | |
} | |
printf("\nthread [%d] finished ...", temp); | |
pthread_exit(NULL); | |
} | |
/** Especifica a estrutura group a ser passada para a thread e o numero de threads a serem usadas*/ | |
void matrixSum(matrix *A, matrix *B, matrix *C) | |
{ | |
assert((A->ncols == B->ncols)); | |
assert((A->nlines == B->nlines)); | |
int i; | |
group mats; | |
mats.totalThreads = 5; | |
pthread_t tid[mats.totalThreads]; | |
mats.matrixes[0] = A; | |
mats.matrixes[1] = B; | |
mats.matrixes[2] = C; | |
for(i=0;i<mats.totalThreads;i++) | |
{ | |
mats.currentLine = i; | |
pthread_create(&tid[i],NULL,thread_matrixSum,(void*)&mats); | |
usleep(200); | |
} | |
for(i=0;i<mats.totalThreads;i++) | |
{ | |
pthread_join(tid[i],NULL); | |
} | |
} | |
/** Soma os elementos da matriz primeiro parametro com os da matriz segundo parametro e os coloca na matriz terceiro parametro*/ | |
void thread_matrixSum(void *param) | |
{ | |
int i, j, temp; | |
group *mats = (group*)param; | |
temp = mats->currentLine; | |
for(i = temp; i < mats->matrixes[0]->nlines; i = i+mats->totalThreads) | |
{ | |
for (j=0; j< mats->matrixes[0]->ncols; ++j) | |
{ | |
matrixInsert(mats->matrixes[2],i,j,(Ponter_MATRIX_ELEMENT(mats->matrixes[0],i,j)+Ponter_MATRIX_ELEMENT(mats->matrixes[1],i,j))); | |
} | |
} | |
printf("\nthread [%d] finished ...", temp); | |
pthread_exit(NULL); | |
} | |
/** Especifica a estrutura group a ser passada para a thread e o numero de threads a serem usadas*/ | |
void matrixSub(matrix *A, matrix *B, matrix *C) | |
{ | |
assert((A->ncols == B->ncols)); | |
assert((A->nlines == B->nlines)); | |
int i; | |
group mats; | |
mats.totalThreads = 5; | |
mats.currentLine = 0; | |
pthread_t tid[mats.totalThreads]; | |
mats.matrixes[0] = A; | |
mats.matrixes[1] = B; | |
mats.matrixes[2] = C; | |
for(i=0;i<mats.totalThreads;i++) | |
{ | |
mats.currentLine = i; | |
pthread_create(&tid[i],NULL,thread_matrixSub,(void*)&mats); | |
usleep(200); | |
} | |
for(i=0;i<mats.totalThreads;i++) | |
{ | |
pthread_join(tid[i],NULL); | |
} | |
} | |
/** Subtrai os elementos da matriz primeiro parametro com os da matriz segundo parametro e os coloca na matriz terceiro parametro*/ | |
void thread_matrixSub(void *param) | |
{ | |
int i, j, temp; | |
group *mats = (group*)param; | |
temp = mats->currentLine; | |
for(i = temp; i < mats->matrixes[0]->nlines; i = i+mats->totalThreads) | |
{ | |
for (j=0; j< mats->matrixes[0]->ncols; ++j) | |
{ | |
matrixInsert(mats->matrixes[2],i,j,(Ponter_MATRIX_ELEMENT(mats->matrixes[0],i,j)-Ponter_MATRIX_ELEMENT(mats->matrixes[1],i,j))); | |
} | |
} | |
printf("\nthread [%d] finished ...", temp); | |
pthread_exit(NULL); | |
} | |
/** Especifica a estrutura group a ser passada para a thread e o numero de threads a serem usadas*/ | |
void matrixTrans(matrix *A, matrix *B) | |
{ | |
assert((A->ncols == B->nlines)); | |
assert((A->nlines == B->ncols)); | |
int i; | |
group mats; | |
mats.totalThreads = 5; | |
pthread_t tid[mats.totalThreads]; | |
mats.matrixes[0] = A; | |
mats.matrixes[1] = B; | |
for(i=0;i<mats.totalThreads;i++) | |
{ | |
mats.currentLine = i; | |
pthread_create(&tid[i],NULL,thread_matrixTrans,(void*)&mats); | |
usleep(200); | |
} | |
for(i=0;i<mats.totalThreads;i++) | |
{ | |
pthread_join(tid[i],NULL); | |
} | |
} | |
/** Realiza a transposta da matriz primeiro parametro na matriz segundo parametro*/ | |
void thread_matrixTrans(void *param) | |
{ | |
int i, j, temp; | |
group *mats = (group*)param; | |
temp = mats->currentLine; | |
for(i = temp; i < mats->matrixes[0]->nlines; i = i+mats->totalThreads) | |
{ | |
for (j=0; j< mats->matrixes[0]->ncols; ++j) | |
{ | |
matrixInsert(mats->matrixes[1],i,j, Ponter_MATRIX_ELEMENT(mats->matrixes[0],j,i)); | |
} | |
} | |
printf("\nthread [%d] finished ...", temp); | |
pthread_exit(NULL); | |
} | |
/** Especifica a estrutura group a ser passada para a thread e o numero de threads a serem usadas*/ | |
void matrixOnes(matrix *A) | |
{ | |
int i; | |
group mats; | |
mats.totalThreads = 5; | |
pthread_t tid[mats.totalThreads]; | |
mats.matrixes[0] = A; | |
for(i=0;i<mats.totalThreads;i++) | |
{ | |
mats.currentLine = i; | |
pthread_create(&tid[i],NULL,thread_matrixOnes,(void*)&mats); | |
usleep(200); | |
} | |
for(i=0;i<mats.totalThreads;i++) | |
{ | |
pthread_join(tid[i],NULL); | |
} | |
} | |
/** Preenche a matriz que foi passada para a funcao com uns*/ | |
void thread_matrixOnes(void *param) | |
{ | |
int i, j, temp; | |
group *mats = (group*)param; | |
temp = mats->currentLine; | |
for(i = temp; i < mats->matrixes[0]->nlines; i = i+mats->totalThreads) | |
{ | |
for (j=0; j< mats->matrixes[0]->ncols; ++j) | |
{ | |
matrixInsert(mats->matrixes[0],i,j, 1); | |
} | |
} | |
printf("\nthread [%d] finished ...", temp); | |
pthread_exit(NULL); | |
} | |
/** Especifica a estrutura group a ser passada para a thread e o numero de threads a serem usadas*/ | |
void matrixIdentity(matrix *A) | |
{ | |
int i; | |
group mats; | |
mats.totalThreads = 5; | |
pthread_t tid[mats.totalThreads]; | |
mats.matrixes[0] = A; | |
for(i=0;i<mats.totalThreads;i++) | |
{ | |
mats.currentLine = i; | |
pthread_create(&tid[i],NULL,thread_matrixIdentity,(void*)&mats); | |
usleep(200); | |
} | |
for(i=0;i<mats.totalThreads;i++) | |
{ | |
pthread_join(tid[i],NULL); | |
} | |
} | |
/** Preenche a matriz que foi passada para a funcao com 0 e somente sua diagonal recebe uns*/ | |
void thread_matrixIdentity(void *param) | |
{ | |
int i, j, temp; | |
group *mats = (group*)param; | |
temp = mats->currentLine; | |
for(i = temp; i < mats->matrixes[0]->nlines; i = i+mats->totalThreads) | |
{ | |
for (j=0; j< mats->matrixes[0]->ncols; ++j) | |
{ | |
matrixInsert(mats->matrixes[0],i,j, 0); | |
if(i == j) | |
{ | |
matrixInsert(mats->matrixes[0],i,j, 1); | |
} | |
} | |
} | |
printf("\nthread [%d] finished ...", temp); | |
pthread_exit(NULL); | |
} | |
/** Especifica a estrutura group a ser passada para a thread e o numero de threads a serem usadas*/ | |
float matrixMedia(matrix *A) | |
{ | |
int i; | |
group mats; | |
mats.totalThreads = 5; | |
pthread_t tid[mats.totalThreads]; | |
mats.matrixes[0] = A; | |
for(i=0;i<mats.totalThreads;i++) | |
{ | |
mats.currentLine = i; | |
pthread_create(&tid[i],NULL,thread_matrixMedia,(void*)&mats); | |
usleep(200); | |
} | |
for(i=0;i<mats.totalThreads;i++) | |
{ | |
pthread_join(tid[i],NULL); | |
} | |
} | |
/** Calcula a media aritimetica dos valores de cada linha da matriz*/ | |
void thread_matrixMedia(void *param) | |
{ | |
int i, j, temp; | |
float sum = 0; | |
group *mats = (group*)param; | |
temp = mats->currentLine; | |
for(i = temp; i < mats->matrixes[0]->nlines; i = i+mats->totalThreads) | |
{ | |
for (j=0; j< mats->matrixes[0]->ncols; ++j) | |
{ | |
sum += Ponter_MATRIX_ELEMENT(mats->matrixes[0],i,j); | |
} | |
printf("\nA media da linha [%d] é: [%f]", i+1, (sum/mats->matrixes[0]->ncols)); | |
sum = 0; | |
} | |
printf("\nthread [%d] finished ...", temp); | |
pthread_exit(NULL); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** TECNICAS DE PROGRAMACAO */ | |
/** Primeiro semestre de 2012 */ | |
/** Aluna: Laura Barros Martins */ | |
/** Matricula: 10/0033440 */ | |
#ifndef BIBMATRIZ_H | |
#define BIBMATRIZ_H | |
typedef struct{ | |
float * elems; | |
int nlines; | |
int ncols; | |
}matrix; | |
typedef struct{ | |
matrix *matrixes[3]; | |
int currentLine; | |
int totalThreads; | |
}group; | |
void matrixNew(matrix *mat, int nlinhas, int ncolunas); /** Esta funcao aloca espaco na memoria para uma matriz de nlinhas e ncoluas, e especifica que as nlinhas e n colunas serao as nlines e ncols da matriz respectivamente */ | |
void matrixDispose(matrix *mat); /** Libera o espaco alocado pelos elementos da matriz que esta sendo passada*/ | |
void matrixInsert(matrix *A, int i, int j, float value); /** Insere um valor value na linha i e coluna j da matriz passada*/ | |
void matrixPrint(matrix *A); /** Imprime todos os elementos da matriz passada*/ | |
void matrixMult(matrix *A, matrix *B, matrix *C); /** Especifica a estrutura group a ser passada para a thread e o numero de threads a serem usadas*/ | |
void thread_matrixMult(void *param); /** Multiplica a matriz primeiro parametro pela matriz segundo parametro e coloca a resposta na matriz terceiro parametro*/ | |
void matrixSum(matrix *A, matrix *B, matrix *C); /** Especifica a estrutura group a ser passada para a thread e o numero de threads a serem usadas*/ | |
void thread_matrixSum(void *param); /** Soma os elementos da matriz primeiro parametro com os da matriz segundo parametro e os coloca na matriz terceiro parametro*/ | |
void matrixSub(matrix *A, matrix *B, matrix *C); /** Especifica a estrutura group a ser passada para a thread e o numero de threads a serem usadas*/ | |
void thread_matrixSub(void *param); /** Subtrai os elementos da matriz primeiro parametro com os da matriz segundo parametro e os coloca na matriz terceiro parametro*/ | |
void matrixTrans(matrix *A, matrix *B); /** Especifica a estrutura group a ser passada para a thread e o numero de threads a serem usadas*/ | |
void thread_matrixTrans(void *param); /** Realiza a transposta da matriz primeiro parametro na matriz segundo parametro*/ | |
void matrixOnes(matrix *A); /** Especifica a estrutura group a ser passada para a thread e o numero de threads a serem usadas*/ | |
void thread_matrixOnes(void *param); /** Preenche a matriz que foi passada para a funcao com uns*/ | |
void matrixIdentity(matrix *A); /** Especifica a estrutura group a ser passada para a thread e o numero de threads a serem usadas*/ | |
void thread_matrixIdentity(void *param); /** Preenche a matriz que foi passada para a funcao com 0 e somente sua diagonal recebe uns*/ | |
float matrixMedia(matrix *A); /** Especifica a estrutura group a ser passada para a thread e o numero de threads a serem usadas*/ | |
void thread_matrixMedia(void *param); /** Calcula a media aritimetica dos valores de cada linha da matriz*/ | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** TECNICAS DE PROGRAMACAO */ | |
/** Primeiro semestre de 2012 */ | |
/** Aluna: Laura Barros Martins */ | |
/** Matricula: 10/0033440 */ | |
#include <stdio.h> | |
#include <time.h> | |
#include <unistd.h> | |
#include "BibMatriz_otm.c" | |
#include <stdlib.h> | |
int menu() //Esta funcao decide o numero de linhas e colunas das matrizes | |
{ | |
int option; | |
int n; | |
printf("\n\n\n***********************************************************************\n"); | |
printf("\t\tEsta e a biblioteca para solućão de matrizes nao otimizada\n\n"); | |
printf("\t\tEscolha a opcao desejada:\n"); | |
printf("\n\t(1) - Matriz 5x5"); | |
printf("\n\t(2) - Matriz 1000 x 1000"); | |
printf("\n\n***********************************************************************\n"); | |
scanf("%d",&option); | |
while (option < 1 || option > 2) //Validar a opcao | |
{ | |
printf("\n\nOpcao invalida. Por favor tente novamente, escolha entre:\n"); | |
printf("\n\t(1) - Matriz 5x5"); | |
printf("\n\t(2) - Matriz 1000 x 1000"); | |
printf("\n\nDigite 1 ou 2 e aperte enter."); | |
scanf("%d",&option); | |
} | |
if (option == 1){ | |
n = 5; | |
} | |
else{ | |
n = 1000; | |
} | |
return (n); | |
} | |
int choseOperation() //E a escolha da operacao a ser realizada | |
{ | |
int option; | |
printf("\n\n\tSelecione a operacao desejada:\n"); | |
printf("\n\t(1) - Adicao"); | |
printf("\n\t(2) - Subtracao"); | |
printf("\n\t(3) - Multiplicacao"); | |
printf("\n\t(4) - Transposicao"); | |
printf("\n\t(5) - Preencher matriz com 1"); | |
printf("\n\t(6) - Gerar Matriz Identidade"); | |
printf("\n\t(7) - Calcular a media de cada linha da matriz\n"); | |
scanf("%d",&option); | |
while (option < 0 || option > 7) | |
{ | |
printf("\n\nOpcao invalida. Por favor tente novamente, escolha entre:\n"); | |
printf("\n\t(1) - Adicao"); | |
printf("\n\t(2) - Subtracao"); | |
printf("\n\t(3) - Multiplicacao"); | |
printf("\n\t(4) - Transposicao"); | |
printf("\n\t(5) - Preencher matriz com 1"); | |
printf("\n\t(6) - Gerar Matriz Identidade"); | |
printf("\n\t(7) - Calcular a media de cada linha da matriz\n"); | |
printf("\n\nDigite 1, 2, 3, 4, 5, 6 ou 7 e aperte enter.\n"); | |
scanf("%d",&option); | |
} | |
return (option); | |
} | |
int inputMode() //E a escolha da forma que a matriz sera preenchida | |
{ | |
int option; | |
printf("\n\t(1) - Automaticamente [aleatoriamente]"); | |
printf("\n\t(2) - Manualmente\n"); | |
scanf("%d",&option); | |
while (option < 1 || option > 2) | |
{ | |
printf("\n\nOpcao invalida. Por favor tente novamente, escolha entre:\n"); | |
printf("\n\t(1) - Automaticamente [aleatoriamente]"); | |
printf("\n\t(2) - Manualmente"); | |
printf("\n\nDigite 1 ou 2 e aperte enter.\n"); | |
scanf("%d",&option); | |
} | |
} | |
void inputManual(matrix mat) //Pede ao usuario o valor de cada elemento da matriz | |
{ | |
int i, j, value; | |
for(i=0;i<mat.nlines;++i) | |
{ | |
for(j=0;j<mat.ncols;++j) | |
{ | |
printf("Digite o valor da Matriz[%d][%d]: ",i,j); | |
scanf("%d",&value); | |
matrixInsert(&mat, i, j, value); | |
} | |
} | |
} | |
void inputAuto(matrix mat) //Preenche automaticamente os elementos da matriz com valores de 0 a 99 aleatoriamente | |
{ | |
int random, i, j; | |
for(i=0;i<mat.nlines;++i) | |
{ | |
for(j=0;j<mat.ncols;++j) | |
{ | |
random = rand()%100; | |
matrixInsert(&mat, i, j, random); | |
} | |
} | |
} | |
int main() | |
{ | |
matrix A, B, C; // A matriz C sempre recebera o resultado caso necessario | |
clock_t start; | |
clock_t diff; | |
clock_t end; | |
int i,j, N, operation, input_mode, printOption; | |
N = menu(); | |
operation = choseOperation(); | |
printf("\n\n\nGostaria de habilitar a impressao?\n"); | |
printf("\n\t(1) - Sim"); | |
printf("\n\t(2) - Nao\n"); | |
scanf("%d",&printOption); | |
while (printOption < 1 || printOption > 2) | |
{ | |
printf("\n\nOpcao invalida. Por favor tente novamente, escolha entre:\n"); | |
printf("\n\t(1) - Sim"); | |
printf("\n\t(2) - Nao\n"); | |
printf("\n\nDigite 1 ou 2 e aperte enter.\n"); | |
scanf("%d",&printOption); | |
} | |
if (operation < 4) //Todas as operaçoes antes da transposicao[opcao 4] requerem 3 matrizes | |
{ | |
printf("\n\n\n\nEsta opcao requer duas matrizes, como gostaria de preenche-las?\n"); | |
input_mode = inputMode(); | |
matrixNew(&A, N, N); // cria uma matrix de N linhas e N colunas | |
matrixNew(&B, N, N); | |
matrixNew(&C, N, N); | |
if (input_mode == 1) | |
{ | |
inputAuto(A); | |
inputAuto(B); | |
} | |
else | |
{ | |
printf("\nMatriz A: \n\n"); | |
inputManual(A); | |
printf("\n\nMatriz B: \n\n"); | |
inputManual(B); | |
} | |
} | |
else //Caso a operacao seja transposicao ou adiante | |
{ | |
printf("\n\n\n\nEsta opcao requer uma matriz, como gostaria de preenche-la?\n"); | |
input_mode = inputMode(); | |
matrixNew(&A, N, N); | |
if (operation ==4) //somente a operacao de transposicao precisa de duas matrizes | |
{ | |
matrixNew(&C, A.ncols, A.nlines); | |
} | |
if (input_mode == 1) | |
{ | |
inputAuto(A); | |
} | |
else | |
{ | |
inputManual(A); | |
} | |
} | |
switch(operation) | |
{ | |
case 1: | |
start= clock(); //comeca a contar os clocks | |
matrixSum(&A, &B, &C); //realiza a operacao | |
end = clock(); //para a contagem de clocks | |
if (printOption ==1) //impressoes | |
{ | |
printf("\n\nMatriz A:\n\n"); | |
matrixPrint(&A); | |
printf("\n\nMatriz B:\n\n"); | |
matrixPrint(&B); | |
printf("\n\nMatriz C = Soma:\n\n"); | |
matrixPrint(&C); | |
} | |
matrixDispose(&A); //Libera a memoria alocada | |
matrixDispose(&B); | |
matrixDispose(&C); | |
break; | |
case 2: | |
start= clock(); | |
matrixSub(&A, &B, &C); | |
end = clock(); | |
if (printOption ==1) | |
{ | |
printf("\n\nMatriz A:\n\n"); | |
matrixPrint(&A); | |
printf("\n\nMatriz B:\n\n"); | |
matrixPrint(&B); | |
printf("\n\nMatriz C = Subtracao:\n\n"); | |
matrixPrint(&C); | |
} | |
matrixDispose(&A); | |
matrixDispose(&B); | |
matrixDispose(&C); | |
break; | |
case 3: | |
start= clock(); | |
matrixMult(&A, &B, &C); | |
end = clock(); | |
if (printOption ==1) | |
{ | |
printf("\n\nMatriz A:\n\n"); | |
matrixPrint(&A); | |
printf("\n\nMatriz B:\n\n"); | |
matrixPrint(&B); | |
printf("\n\nMatriz C = Multiplicacao:\n\n"); | |
matrixPrint(&C); | |
} | |
matrixDispose(&A); | |
matrixDispose(&B); | |
matrixDispose(&C); | |
break; | |
case 4: | |
start= clock(); | |
matrixTrans(&A, &C); | |
end = clock(); | |
if (printOption ==1) | |
{ | |
printf("\n\nMatriz A:\n\n"); | |
matrixPrint(&A); | |
printf("\n\nMatriz A^T = Transposta:\n\n"); | |
matrixPrint(&C); | |
} | |
matrixDispose(&A); | |
matrixDispose(&C); | |
break; | |
case 5: | |
start= clock(); | |
matrixOnes(&A); | |
end = clock(); | |
if (printOption ==1) | |
{ | |
printf("\n\nMatriz A:\n\n"); | |
matrixPrint(&A); | |
} | |
matrixDispose(&A); | |
break; | |
case 6: | |
start= clock(); | |
matrixIdentity(&A); | |
end = clock(); | |
if (printOption ==1) | |
{ | |
printf("\n\nMatriz A:\n\n"); | |
matrixPrint(&A); | |
} | |
matrixDispose(&A); | |
break; | |
case 7: | |
start= clock(); | |
matrixMedia(&A); | |
end = clock(); | |
if (printOption ==1) | |
{ | |
printf("\n\nMatriz A:\n\n"); | |
matrixPrint(&A); | |
} | |
/** TECNICAS DE PROGRAMACAO */ | |
/** Primeiro semestre de 2012 */ | |
/** Aluna: Laura Barros Martins */ | |
/** Matricula: 10/0033440 */ | |
#ifndef BIBMATRIZ_H | |
#define BIBMATRIZ_H | |
typedef struct{ | |
float * elems; | |
int nlines; | |
int ncols; | |
}matrix; | |
typedef struct{ | |
matrix *matrixes[3]; | |
int currentLine; | |
int totalThreads; | |
}group; | |
void matrixNew(matrix *mat, int nlinhas, int ncolunas); /** Esta funcao aloca espaco na memoria para uma matriz de nlinhas e ncoluas, e especifica que as nlinhas e n colunas serao as nlines e ncols da matriz respectivamente */ | |
void matrixDispose(matrix *mat); /** Libera o espaco alocado pelos elementos da matriz que esta sendo passada*/ | |
void matrixInsert(matrix *A, int i, int j, float value); /** Insere um valor value na linha i e coluna j da matriz passada*/ | |
void matrixPrint(matrix *A); /** Imprime todos os elementos da matriz passada*/ | |
void matrixMult(matrix *A, matrix *B, matrix *C); /** Especifica a estrutura group a ser passada para a thread e o numero de threads a serem usadas*/ | |
void thread_matrixMult(void *param); /** Multiplica a matriz primeiro parametro pela matriz segundo parametro e coloca a resposta na matriz terceiro parametro*/ | |
void matrixSum(matrix *A, matrix *B, matrix *C); /** Especifica a estrutura group a ser passada para a thread e o numero de threads a serem usadas*/ | |
void thread_matrixSum(void *param); /** Soma os elementos da matriz primeiro parametro com os da matriz segundo parametro e os coloca na matriz terceiro parametro*/ | |
void matrixSub(matrix *A, matrix *B, matrix *C); /** Especifica a estrutura group a ser passada para a thread e o numero de threads a serem usadas*/ | |
void thread_matrixSub(void *param); /** Subtrai os elementos da matriz primeiro parametro com os da matriz segundo parametro e os coloca na matriz terceiro parametro*/ | |
void matrixTrans(matrix *A, matrix *B); /** Especifica a estrutura group a ser passada para a thread e o numero de threads a serem usadas*/ | |
void thread_matrixTrans(void *param); /** Realiza a transposta da matriz primeiro parametro na matriz segundo parametro*/ | |
void matrixOnes(matrix *A); /** Especifica a estrutura group a ser passada para a thread e o numero de threads a serem usadas*/ | |
void thread_matrixOnes(void *param); /** Preenche a matriz que foi passada para a funcao com uns*/ | |
void matrixIdentity(matrix *A); /** Especifica a estrutura group a ser passada para a thread e o numero de threads a serem usadas*/ | |
void thread_matrixIdentity(void *param); /** Preenche a matriz que foi passada para a funcao com 0 e somente sua diagonal recebe uns*/ | |
float matrixMedia(matrix *A); /** Especifica a estrutura group a ser passada para a thread e o numero de threads a serem usadas*/ | |
void thread_matrixMedia(void *param); /** Calcula a media aritimetica dos valores de cada linha da matriz*/ | |
#endif | |
matrixDispose(&A); | |
break; | |
} | |
diff = end - start; | |
printf("\n\n Tempo de execucao: %f segundos\n\n", (float)diff/CLOCKS_PER_SEC); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment