daily pastebin goal
12%
SHARE
TWEET

Untitled

a guest May 23rd, 2018 122 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. time ./run 1
  3. single threaded
  4. real    0m1.051s
  5. user    0m0.752s
  6. sys     0m0.019s
  7.  
  8. time ./run
  9. 4 threads
  10. real    0m1.158s
  11. user    0m3.538s
  12. sys     0m0.025s
  13. */
  14.  
  15. #include <random>
  16. #include <cmath>
  17. #include <iostream>
  18. #include <fstream>
  19. #include <chrono>
  20. #include <thread>
  21. #include <stdio.h>
  22.  
  23. using namespace std;
  24. //--------------VECI-PRE-BITMAPY---------------//
  25.  
  26. void generateBitmapImage(unsigned char *image, int height, int width, char* imageFileName);
  27. unsigned char* createBitmapFileHeader(int height, int width);
  28. unsigned char* createBitmapInfoHeader(int height, int width);
  29.  
  30. const int bytesPerPixel = 3; /// red, green, blue
  31. const int fileHeaderSize = 14;
  32. const int infoHeaderSize = 40;
  33.  
  34. //---PARKOVISKO-INTERGALAKTICKYCH-PREMENNYCH---//
  35. //---------------------------------------------//
  36.  
  37. int a=666;       //  rozlisenie mriezky, malo by byt delitelne poctom jadier, ak chceme jednoduchsiu upravu na menej jadier
  38. long int M[666][666];  //  mriezka
  39. double P[5];  //  pravdepodobnosti otocenia spinov
  40. /*pozn.: do poli nejde dat priamo a ako dlzka, vraj nieco s casom, kedy sa to kompiluje*/
  41.  
  42. int END=0;  //  kontrolka ukoncovania threadov
  43.  
  44. random_device ran;  //  seed pre RNG
  45. mt19937_64 ra(ran()); //  RNG engine
  46. uniform_int_distribution<int> gen(0,1);   //  RNG s rovnomernym rozdelenim pre generator
  47. uniform_int_distribution<int> pra(0,100); //  RNG pre pravdepodobnost
  48.  
  49. //-------------GENERATOR-MRIEZKY---------------//
  50. //---------------------------------------------//
  51.  
  52. void generator()  // generuje nahodne spiny
  53. {
  54.   for (int i=0; i<a;i++)
  55.   {
  56.     for (int j=0; j<a; j++)
  57.     {
  58.       (gen(ra)<1)? M[i][j]=-1 : M[i][j]=1;
  59.     }
  60.   }
  61. }
  62.  
  63. //-------------GENERATOR-BITMAPY---------------//
  64. //---------------------------------------------//
  65.  
  66. //  nehanebne ukradnuty, moj kod pokracuje pod nadpisom MAGNETIZATOR 3000
  67. void bmp(int v)
  68. {
  69.  
  70.   unsigned char image[a][a][bytesPerPixel];
  71.   string str = to_string(v)+ ".bmp";
  72.   vector <char> vec(str.begin(), str.end());
  73.   vec.push_back('\0');
  74.   char* imageFileName = &vec[0];
  75.  
  76.   int e, f;
  77.   for(e=0; e<a; e++){
  78.     for(f=0; f<a; f++){
  79.       if(M[e][f]==1){
  80.         image[e][f][2] = (unsigned char)(0); ///red
  81.         image[e][f][1] = (unsigned char)(60); ///green
  82.         image[e][f][0] = (unsigned char)(0); ///blue
  83.       }
  84.       else{
  85.         image[e][f][2] = (unsigned char)(238); ///red
  86.         image[e][f][1] = (unsigned char)(147); ///green
  87.         image[e][f][0] = (unsigned char)(73); ///blue
  88.       }
  89.     }
  90.   }
  91.   generateBitmapImage((unsigned char *)image, a, a, imageFileName);
  92. }
  93.  
  94. //-------------MAGNETIZATOR-3000---------------//
  95. //---------------------------------------------//
  96. /*note to self: Pocet cyklov sa meni tu, nezabudni, ze to chrume viac vlakien, tak neprehanaj*/
  97. void magnetizator()
  98. {
  99.   random_device rad;
  100.   thread_local mt19937_64 rd(rad());
  101.   uniform_int_distribution<int> dis(0,a-1);   //  RNG pre magnetizator
  102.   int sl,sr,sd,su;
  103.   for (long int N=0;N<1250000;N++)  // multithreading sa oplati az pri vysokych hraniciach N, pri N okolo
  104.   {
  105.     int x = dis(rd);  //  nahodne zvolenie x-ovej suradnice
  106.     int y = dis(rd); //dis(ran);  //  nahodne zvolenie y-ovej suradnice
  107.     long int t=M[x][y];
  108.     (x==0)?sl=t:sl=M[x-1][y];
  109.     (x==a-1)?sr=t:sr=M[x+1][y];
  110.     (y==0)?sd=t:sd=M[x][y-1];
  111.     (y==a-1)?su=t:su=M[x][y+1];
  112.     (pra(rd)<P[int(0.5*t*(su+sd+sr+sl) + 3)]*100)? M[x][y]=-t:0;  //  ak je nahodne cislo od 0 po 1 mensie ako pravdepodobnost, otocime, ak nie, neotocime
  113.   }
  114.  
  115.  cout << "thread ended" << endl;
  116.  END++;
  117. }
  118.  
  119. //-----------THREAD-MANGLER-v4.0.2-------------//
  120. //--------------------MAIN---------------------//
  121. int main(int argc, char **argv)
  122. {
  123.   int AA=0;
  124.   long int v=0;
  125.   long int MAG;  // celkova magnetizacia
  126.   double T=2; //  teplota
  127.   //-------pocitadlo-pravdepodobnosti--------//
  128.   for (int pp = 0; pp < 5; pp++)
  129.   {
  130.     P[pp]=1.0/(1+exp((4*(pp+1)-12)/T));
  131.   }
  132.   cout<<"probabilities calculated"<<endl;
  133.   //------------generator-mriezky------------//
  134.   generator();
  135.   cout<<"generated"<<endl;
  136.   bmp(v); v++;
  137.  
  138.   //--------cyklus magnetizatora-3000--------//
  139.   int numThreads=4;
  140.   if (argc>1)
  141.   {
  142.    numThreads=1;
  143.    cout << "running in single thread"<<endl;
  144.   }
  145.   while (AA<10)
  146.   {
  147.     cout<<"begin"<<endl;
  148.     END=0;
  149.     MAG=0;
  150.  
  151.     for (int i=0;i<numThreads;i++)
  152.     {
  153.       thread u1(magnetizator);
  154.       u1.detach();
  155.       cout<<"thread constructed"<<endl;
  156.     }
  157.  
  158.     while(END<numThreads)
  159.       {this_thread::sleep_for(chrono::milliseconds(50));
  160.       }
  161.       bmp(v);
  162.       v++;
  163.       AA++;
  164.     cout << "cycle "<<AA<<" ended" << endl;
  165.   }
  166.   //-----pocitadlo-celkovej-magnetizacie-----//
  167.   for (int R=0;R<a;R++) // pocita celkovu magnetizaciu
  168.   {
  169.     for (int Q=0;Q<a;Q++)
  170.     (M[R][Q]<0)?MAG--:MAG++;
  171.   }
  172.   //-----------------------------------------//
  173.   ofstream fout("out_10", ios_base::app);
  174.   fout << T << "\t" << MAG <<endl;
  175.   AA++;
  176.   cout << "\7";
  177. }
  178. // nejaka funkcia k bmp, tiez ukradnute
  179. void generateBitmapImage(unsigned char *image, int height, int width, char* imageFileName)
  180. {
  181.  
  182.   unsigned char* fileHeader = createBitmapFileHeader(height, width);
  183.   unsigned char* infoHeader = createBitmapInfoHeader(height, width);
  184.   unsigned char padding[3] = {0, 0, 0};
  185.   int paddingSize = (4-(width*bytesPerPixel)%4)%4;
  186.  
  187.   FILE* imageFile = fopen(imageFileName, "wb");
  188.  
  189.   fwrite(fileHeader, 1, fileHeaderSize, imageFile);
  190.   fwrite(infoHeader, 1, infoHeaderSize, imageFile);
  191.  
  192.   int i;
  193.   for(i=0; i<height; i++){
  194.     fwrite(image+(i*width*bytesPerPixel), bytesPerPixel, width, imageFile);
  195.     fwrite(padding, 1, paddingSize, imageFile);
  196.   }
  197.  
  198.   fclose(imageFile);
  199. }
  200. //  dalsia
  201. unsigned char* createBitmapFileHeader(int height, int width)
  202. {
  203.   int fileSize = fileHeaderSize + infoHeaderSize + bytesPerPixel*height*width;
  204.  
  205.   static unsigned char fileHeader[] = {
  206.     0,0, /// signature
  207.     0,0,0,0, /// image file size in bytes
  208.     0,0,0,0, /// reserved
  209.     0,0,0,0, /// start of pixel array
  210.   };
  211.  
  212.   fileHeader[ 0] = (unsigned char)('B');
  213.   fileHeader[ 1] = (unsigned char)('M');
  214.   fileHeader[ 2] = (unsigned char)(fileSize    );
  215.   fileHeader[ 3] = (unsigned char)(fileSize>> 8);
  216.   fileHeader[ 4] = (unsigned char)(fileSize>>16);
  217.   fileHeader[ 5] = (unsigned char)(fileSize>>24);
  218.   fileHeader[10] = (unsigned char)(fileHeaderSize + infoHeaderSize);
  219.  
  220.   return fileHeader;
  221. }
  222. //  dalsia
  223. unsigned char* createBitmapInfoHeader(int height, int width)
  224. {
  225.   static unsigned char infoHeader[] = {
  226.     0,0,0,0, /// header size
  227.     0,0,0,0, /// image width
  228.     0,0,0,0, /// image height
  229.     0,0, /// number of color planes
  230.     0,0, /// bits per pixel
  231.     0,0,0,0, /// compression
  232.     0,0,0,0, /// image size
  233.     0,0,0,0, /// horizontal resolution
  234.     0,0,0,0, /// vertical resolution
  235.     0,0,0,0, /// colors in color table
  236.     0,0,0,0, /// important color count
  237.   };
  238.  
  239.   infoHeader[ 0] = (unsigned char)(infoHeaderSize);
  240.   infoHeader[ 4] = (unsigned char)(width    );
  241.   infoHeader[ 5] = (unsigned char)(width>> 8);
  242.   infoHeader[ 6] = (unsigned char)(width>>16);
  243.   infoHeader[ 7] = (unsigned char)(width>>24);
  244.   infoHeader[ 8] = (unsigned char)(height    );
  245.   infoHeader[ 9] = (unsigned char)(height>> 8);
  246.   infoHeader[10] = (unsigned char)(height>>16);
  247.   infoHeader[11] = (unsigned char)(height>>24);
  248.   infoHeader[12] = (unsigned char)(1);
  249.   infoHeader[14] = (unsigned char)(bytesPerPixel*8);
  250.  
  251.   return infoHeader;
  252. }
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