-
-
Save simondegheselle/b3bcfaef3483568cf10e77d0e9dbb3cd to your computer and use it in GitHub Desktop.
3.c
This file contains hidden or 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
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <pthread.h> | |
| #define DIM 10 | |
| int a[DIM][DIM]; | |
| int b[DIM][DIM]; | |
| int c[DIM][DIM]; | |
| struct argumenten { | |
| int dim; | |
| int rij; | |
| }; | |
| void * bepaal_rij_van_resultaten_matrix(void * arg){ | |
| struct argumenten * args = (struct argumenten *) arg; | |
| int dim = args->dim; | |
| int rij = args->rij; | |
| int i,j; | |
| for (i=0;i<dim;i++){ | |
| for (j=0;j<dim;j++){ | |
| c[rij][i]+=a[rij][j]*b[j][i]; | |
| } | |
| } | |
| } | |
| void bepaal_product_matrices(int ** matrixA,int ** matrixB,int ** matrixC,int dim){ | |
| int i; | |
| pthread_t threads[dim]; | |
| for (i = 0; i < dim; i++) { | |
| struct argumenten args; | |
| args.dim = dim; | |
| args.rij = i; | |
| pthread_create(&threads[i], NULL, &bepaal_rij_van_resultaten_matrix, &args); | |
| } | |
| for (i = 0; i < dim; i++) { | |
| pthread_join(threads[i], NULL); | |
| } | |
| } | |
| void schrijf_matrix(){ | |
| int i,j; | |
| for (i=0;i<DIM;i++){ | |
| for (j=0;j<DIM;j++){ | |
| printf("%-5d ",c[i][j]); | |
| } | |
| printf("\n"); | |
| } | |
| } | |
| void init_matrix(){ | |
| int i,j; | |
| for (i=0;i<DIM;i++){ | |
| for (j=0;j<DIM;j++){ | |
| a[i][j]=i+j; | |
| b[i][j]=i+j; | |
| c[i][j]=0; | |
| } | |
| } | |
| } | |
| main(){ | |
| init_matrix(); | |
| bepaal_product_matrices(a,b,c,DIM); | |
| schrijf_matrix(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment