Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # include <pthread.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/time.h>
- #include <string.h>
- #include <math.h>
- #include <omp.h>
- void *Exercitiul_2 (void *);
- int n = 1024;
- int p = 2;
- int mod = 0;
- int max_rep = 1;
- float **a, *b, *c;
- int main(int argc, char *argv[]) {
- int i, j, k, q, rep;
- int* params;
- pthread_t *ids;
- struct timeval t1, t2;
- if (argc >= 2) n = atoi(argv[1]);
- if (argc >= 3) p = atoi(argv[2]);
- if (argc >= 4) mod = atoi(argv[3]);
- if (argc >= 5) max_rep = atoi(argv[4]);
- a = (float**)malloc(sizeof(float)*n);
- b = (float*)malloc(sizeof(float)*n);
- c = (float*)malloc(sizeof(float)*n);
- for (i = 0; i < n; i++)
- {
- a[i] = (float*)malloc(sizeof(float)*n);
- }
- for (i = 0; i < n; i++) {
- b[i] = rand()/(float)RAND_MAX;
- c[i] = 0;
- for(j = 0; j < n; j++)
- {
- a[i][j] = rand()/(float)RAND_MAX;
- }
- }
- params = (int*)malloc(sizeof(int)*p);
- ids = (pthread_t*)malloc(sizeof(pthread_t)*p);
- gettimeofday(&t1, NULL);
- for (q = 0; q < p; q++)
- {
- params[q] = q;
- pthread_create(&ids[q], 0, Exercitiul_2, (void*)¶ms[q]);
- }
- for (q = 0; q < p; q++)
- {
- pthread_join(ids[q], 0);
- }
- gettimeofday(&t2, NULL);
- float tp = (t2.tv_sec - t1.tv_sec + 0.000001*(t2.tv_usec - t1.tv_usec));
- for (i = 0; i < n; i++)
- printf(" %f ", c[i]);
- printf("\n");
- printf("n=%d\np=%d\nTimp executie: %f\n", n, p, (float)tp);
- return 0;
- }
- void *Exercitiul_2(void *param)
- {
- int i, j, k, first, last;
- int q = *(int*)param;
- int s = (int) n/p;
- first = q*s;
- last = first + s;
- for (i = first; i < last; i++)
- for(j = 0; j < n; j++)
- {
- c[i] += a[i][j]*b[j];
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement