Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <errno.h>
- #include <pthread.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <string.h>
- #include <sys/time.h>
- #include <sys/wait.h>
- #include <sys/stat.h>
- double *a,*b,*c;
- void print_matrix(int M, double *m)
- {
- int i,j;
- for(i=0; i<M; i++)
- {
- for(j=0; j<M; j++)
- {
- printf("%g\t" ,*(m+M*i+j));
- }
- printf("\n");
- }
- printf("\n");
- }
- void calc_row(int *args)
- {
- int row = args[0];
- int M = args[1];
- int j,k;
- for(j=0; j<M; j++)
- {
- *(c+row*M+j)=0;
- for(k=0; k<M; k++)
- {
- *(c+row*M+j)=*(c+row*M+j)+*(a+row*M+k)* *(b+k*M+j);
- }
- }
- }
- void generate_matrix(int M, double *A)
- {
- int i;
- for(i=0; i<M*M; i++)
- {
- m[i] = 1+rand()%100;
- }
- }
- int main(int argc, char *argv[])
- {
- double t1,t2;
- int i;
- int args[2];
- int M;
- M = args[1] = atoi(argv[1]);
- pthread_t pthread[M];
- t1 = clock();
- printf("i will generate\n");
- generate_matrix(M, a);
- generate_matrix(M, b);
- printf("generated !\n");
- for(i=0 ; i< M ; i++){
- args[0] = i;
- if (pthread_create(&pthread[i], NULL, (void *) calc_row, (void *) &args) != 0)
- {
- fprintf(stderr, "pthread create thread error");
- exit(1);
- }
- }
- for(i = 0 ;i < M ; i++)
- {
- (void)pthread_join(pthread[i], NULL);
- }
- print_matrix(M, a);
- print_matrix(M, b);
- print_matrix(M, c);
- t2= clock();
- printf("\n\ntime taken = %f",t2-t1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement