SHARE
TWEET

Untitled

a guest Jul 18th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Lv5
  2.  
  3. #include <omp.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. int main (int argc, char *argv[]) {         //argc-broj parametara koji predajemo program(kada predajemo, gdje)
  7. //argv-pokazivac na memorijske adrese koje sadrzavaju nazive parametara(kojih parametara, gdje se koristi)
  8.  
  9.   double *row = (double *)malloc(2000*2000*sizeof(double));
  10.   double *rez = (double *)malloc(2000*2000*sizeof(double));
  11.   double *matrix = (double *)malloc(2000*2000*sizeof(double));
  12. //takva je sintaksa?
  13.  
  14.   int i,j,k;
  15.   int count = 0;
  16.   double sum;
  17.  
  18.  
  19.   for (i=0;i<2000;i++)
  20.   {
  21.  
  22.       for (j=0;j<2000;j++)
  23.       {
  24.           *(row+i*2000+j)= ++count;
  25.           *(matrix+i*2000+j)= ++count;      //popunjavanje matrica valjda random double brojevima?
  26.       }
  27.   }
  28.  
  29.   double start;     //wtf?
  30.   omp_set_num_threads(8);       //kako znam da je 8 najoptimalnije, jer on najkrace traje?
  31.   start = omp_get_wtime();          //dohvacanje podataka za real cpu time?
  32. #pragma omp parallel for private(j,k)       //jel taj # slucajno ili njega treba makuti ?
  33.  
  34.   for (i=0;i<2000;i++)
  35.   {
  36.       for (j=0;j<2000;j++)
  37.       {
  38.           for(k=0;k<2000;k++){
  39. sum=sum + ((row+i*2000+j))(*(matrix+i*2000+j));     //zasto ima pokazivac prije matrix a nema prije row, I sta znaci row+I a sta je *2000+j)
  40.           }
  41.           *(rez+i*2000+j)= sum;     //jel to zapis suma u matricu rez?
  42.           sum = 0;          //zasto je sum=0 ako smo ga sada izracunali
  43.       }
  44.   }
  45.  
  46.   for (i=0;i<2000;i++)
  47.   {
  48.       printf("%d", rez[i]);     //jel to ispis reza?
  49.   }
  50.  
  51.   double end;       //zasto mi treba taj double start I double end, da je short jel bi isto trebalo?
  52.  
  53.   end = omp_get_wtime();
  54.   printf("\n Timer: %f \n", end-start);     //kao ispis real cpu time-a
  55.  
  56.  
  57.  return 0;
  58. }
  59.  
  60. Lv4
  61.  
  62. #include <omp.h>
  63. #include <stdio.h>
  64. #include <stdlib.h>
  65. int main (int argc, char *argv[]) {
  66.  
  67.   int row[100];
  68.   int rez[100];
  69.   int matrix[100][100];
  70.   int i,j;
  71.  
  72.   for (i=0;i<100;i++)
  73.   {
  74.       row[i]=11;        //zasto 11?
  75.       rez[i]=0;
  76.       for (j=0;j<100;j++)
  77.       {
  78.           matrix[i][j]=22;      //zasto 22?
  79.       }
  80.   }
  81.  
  82.   double start;
  83.   start = omp_get_wtime();
  84.  
  85.   for (i=0;i<100;i++)
  86.   {
  87.       for (j=0;j<100;j++)
  88.       {
  89. rez[i]=(row[j]*matrix[i][j]) + rez[i];  //jesu li to pokazivaci ili mnozenja, kako znam razliku i sta znaci cijeli taj ispis zapravo?
  90.       }
  91.   }
  92.  
  93.   for (i=0;i<100;i++)
  94.   {
  95.       printf("%d", rez[i]);     //ispis rez pomnozene matrice?
  96.   }
  97.  
  98.   double end;
  99.   end = omp_get_wtime();
  100.   printf("\n Timer: %f \n", end-start);
  101.  
  102.  
  103.   int numThreads, tid;      //ovo automatski stvara niti
  104.   /* This creates a team of threads; each thread has own copy of variables  */
  105. #pragma omp parallel private(numThreads, tid)       //jel # treba postojati?
  106.  {
  107.    tid = omp_get_thread_num();
  108.    printf("Hello World from thread number %d\n", tid);
  109.  
  110. /* The following is executed by the master thread only (tid=0) */       /// kako on zna da je to executed by master thread? Vjv jer je rang 0?
  111.    if (tid == 0)
  112.      {
  113.        numThreads = omp_get_num_threads();
  114.        printf("Number of threads is %d\n", numThreads);
  115.      }
  116.  }
  117.  return 0;
  118. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top