Baixe o app para aproveitar ainda mais
Prévia do material em texto
multiplicamatrizes.c Página 1 de 2 1 /* 2 * multiplicamatrizes.c 3 * 4 * Copyright 2012 Thassae Lander Silva dos Santos <thassae@hotmail.com> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 * MA 02110-1301, USA. 20 * 21 * 22 */ 23 24 #include <stdio.h> 25 #include <stdlib.h> 26 #include <pthread.h> 27 28 #define MAX_THREADS 10 // Define o número máximo de threads 29 #define N 10 // Define o "N" da matriz quadrada 30 31 int A[N][N], B[N][N], C[N][N]; // Declaraçã o das matrizes que eu vou utiliz ar 32 33 void *multiplica(void *id) // Funçã o que eu quero paralelizar 34 { 35 int threadId = (int)id; 36 int i,j,k; 37 for (i = (threadId*(N/MAX_THREADS)); i < ((threadId+1)*(N/MAX_THREADS)) ; i++) 38 for (j = 0; j < N; j++) 39 { 40 C[i][j] = 0; 41 for (k = 0; k < N; k++) 42 C[i][j] += A[i][k] * B[k][j]; 43 } 44 pthread_exit(NULL); 45 } 46 47 void preenche(int A[N][N], int m, int n) // Preenche matriz com '1' 48 { 49 int i,j; 50 for (i = 0; i < m; i++) 51 for (j = 0; j < n; j++) 52 A[i][j] = 1; 53 } 54 55 void imprime(int A[N][N], int m, int n) // Exibe a matriz na tela 56 { 57 int i,j; 58 for (i = 0; i < m; i++) 59 { 60 for (j = 0; j < n; j++) - 1 - multiplicamatrizes.c Página 2 de 2 61 { 62 printf("|%d|\t", A[i][j]); 63 } 64 printf("\n"); 65 } 66 printf("-----------------------------------------------------\n"); 67 } 68 69 int main(int argc, char **argv) 70 { 71 // Declaraçã o dos parâmetros do pthread 72 pthread_t threads[MAX_THREADS]; 73 pthread_attr_t atributos; 74 pthread_attr_init(&atributos); 75 pthread_attr_setdetachstate(&atributos, PTHREAD_CREATE_JOINABLE); 76 pthread_attr_setscope(&atributos,PTHREAD_SCOPE_SYSTEM); 77 78 int i,flag; 79 preenche(A,N,N); //Preenche toda a matriz A[N][N] com '1' 80 preenche(B,N,N); //Preenche toda a matriz B[N][N] com '1' 81 82 // Criaçã o das threads. Se houver erro de criaçã o, o programa para de e xecutar. 83 for (i = 0; i < MAX_THREADS; i++) 84 flag = pthread_create(&threads[i], &atributos, multiplica, (void *) i); 85 if (flag) 86 exit(-1); 87 88 // Junçã o dos resultados obtidos por cada thread 89 for (i = 0; i < MAX_THREADS; i++) 90 pthread_join(threads[i], NULL); 91 92 //Imprime as matrizes na tela 93 imprime(A,N,N); 94 imprime(B,N,N); 95 imprime(C,N,N); 96 97 pthread_exit(NULL); 98 } 99 100 - 2 -
Compartilhar