Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <omp.h>
- #include <stdio.h>
- #include <time.h>
- #define N 50 /* dimensiunea vectorilor; experimentati cu diverse valori pentru N */
- #define M 40
- #define R 60
- double a[N][M], b[M][R], c[N][R];
- double get_time();
- int main (int argc, char *argv[])
- {
- int i, j, k, tid, nthreads;
- /* aici puteti initializa vectorii a si b dupa dorinta, prin expresii sau prin citire din fisier */
- srand(time(NULL));
- printf("Generating the elements of the first matrix\n");
- for(i = 0; i < N; i++)
- {
- for(j = 0; j < M; j++)
- {
- a[i][j] = rand() % 100;
- }
- }
- printf("Generating the elements of the second matrix\n");
- for(i = 0; i < M; i++)
- {
- for(j = 0; j < R; j++)
- {
- b[i][j] = rand() % 100;
- }
- }
- display_matrix(a, N, N);
- display_matrix(b, N, N);
- double start_time = omp_get_wtime();
- #pragma omp parallel shared(a,b,c,nthreads) private(i,tid)
- {
- tid = omp_get_thread_num();
- if (tid == 0)
- {
- nthreads = omp_get_num_threads();
- printf("Number of threads = %d\n", nthreads);
- }
- printf("Thread %d starting...\n",tid);
- for (i=0;i<N;i++)
- {
- for (j=0;j<R;j++)
- {
- #pragma omp for
- for (k=0;k<M;k++)
- {
- c[i][j] += a[i][k] * b[k][j];
- printf("Thread %d: i = %d, j = %d, c[%d][%d] = %f\n", tid, i, j, i, j, c[i][j]);
- }
- }
- }
- /* end of for */
- printf ("Thread %d done!\n", tid);
- } /* end of parallel region */
- double end = omp_get_wtime();
- double duration = end - start_time;
- //printf("Parallel program duration: %d\n", duration);
- for (i = 0; i < N; i++) {
- for (j = 0; j < R; j++)
- c[i][j] = 0;
- }
- //se poate pune la omp for: schedule(static, 4) nowait - de incercat
- //collapse(n) extinde paralelizarea unei bucle pe mai multe bucle imbricate (asta reprezinta n-ul)
- //tine cont de scheduler - dynamic vs static, valoarea optima a chunk-ului, etc
- double start_time_secv = get_time();
- for (i=0;i<N;i++)
- {
- for (j=0;j<R;j++)
- {
- for (k=0;k<M;k++)
- {
- c[i][j] += a[i][k] * b[k][j];
- printf("i = %d, j = %d, c[%d][%d] = %f\n", i, j, i, j, c[i][j]);
- }
- }
- }
- double end_secv = get_time();
- double duration_secv = end_secv - start_time_secv;
- /*
- input data: a[50][40], b[40][60], c[50][60];
- --paralelizat primul for
- Parallel program duration: 805306368
- Secvential program duration: 2147483648
- Parallel program duration: 1879048192
- Secvential program duration: 1073741824
- Parallel program duration: 939524096
- Secvential program duration: 536870912
- Parallel program duration: 1610612736
- Secvential program duration: 268435456
- Parallel program duration: 671088640
- Secvential program duration: 268435456
- --paralelizat al doilea for
- Parallel program duration: 1207959552
- Secvential program duration: 1610612736
- Parallel program duration: 134217728
- Secvential program duration: 1879048192
- Parallel program duration: 671088640
- Secvential program duration: 1879048192
- Parallel program duration: 402653184
- Secvential program duration: 1610612736
- Parallel program duration: 2013265920
- Secvential program duration: 2147483648
- --paralelizat al treilea for
- Parallel program duration: 1073741824
- Secvential program duration: 1342177280
- */
- printf("Parallel program duration: %d\n", duration);
- printf("Secvential program duration: %d\n", duration_secv);
- printf("Program end.\n");
- }
- void display_matrix(long matrix[150][150], int r, int c)
- {
- int i, j;
- // Displaying the matrix
- for (i = 0; i < r; i++) {
- for (j = 0; j < c; j++)
- printf("%d\t", matrix[i][j]);
- printf("\n");
- }
- }
- double get_time()
- {
- struct timeval t;
- struct timezone tzp;
- gettimeofday(&t, &tzp);
- return t.tv_sec + t.tv_usec*1e-6;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement