Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- time ./run 1
- single threaded
- real 0m1.051s
- user 0m0.752s
- sys 0m0.019s
- time ./run
- 4 threads
- real 0m1.158s
- user 0m3.538s
- sys 0m0.025s
- */
- #include <random>
- #include <cmath>
- #include <iostream>
- #include <fstream>
- #include <chrono>
- #include <thread>
- #include <stdio.h>
- using namespace std;
- //--------------VECI-PRE-BITMAPY---------------//
- void generateBitmapImage(unsigned char *image, int height, int width, char* imageFileName);
- unsigned char* createBitmapFileHeader(int height, int width);
- unsigned char* createBitmapInfoHeader(int height, int width);
- const int bytesPerPixel = 3; /// red, green, blue
- const int fileHeaderSize = 14;
- const int infoHeaderSize = 40;
- //---PARKOVISKO-INTERGALAKTICKYCH-PREMENNYCH---//
- //---------------------------------------------//
- int a=666; // rozlisenie mriezky, malo by byt delitelne poctom jadier, ak chceme jednoduchsiu upravu na menej jadier
- long int M[666][666]; // mriezka
- double P[5]; // pravdepodobnosti otocenia spinov
- /*pozn.: do poli nejde dat priamo a ako dlzka, vraj nieco s casom, kedy sa to kompiluje*/
- int END=0; // kontrolka ukoncovania threadov
- random_device ran; // seed pre RNG
- mt19937_64 ra(ran()); // RNG engine
- uniform_int_distribution<int> gen(0,1); // RNG s rovnomernym rozdelenim pre generator
- uniform_int_distribution<int> pra(0,100); // RNG pre pravdepodobnost
- //-------------GENERATOR-MRIEZKY---------------//
- //---------------------------------------------//
- void generator() // generuje nahodne spiny
- {
- for (int i=0; i<a;i++)
- {
- for (int j=0; j<a; j++)
- {
- (gen(ra)<1)? M[i][j]=-1 : M[i][j]=1;
- }
- }
- }
- //-------------GENERATOR-BITMAPY---------------//
- //---------------------------------------------//
- // nehanebne ukradnuty, moj kod pokracuje pod nadpisom MAGNETIZATOR 3000
- void bmp(int v)
- {
- unsigned char image[a][a][bytesPerPixel];
- string str = to_string(v)+ ".bmp";
- vector <char> vec(str.begin(), str.end());
- vec.push_back('\0');
- char* imageFileName = &vec[0];
- int e, f;
- for(e=0; e<a; e++){
- for(f=0; f<a; f++){
- if(M[e][f]==1){
- image[e][f][2] = (unsigned char)(0); ///red
- image[e][f][1] = (unsigned char)(60); ///green
- image[e][f][0] = (unsigned char)(0); ///blue
- }
- else{
- image[e][f][2] = (unsigned char)(238); ///red
- image[e][f][1] = (unsigned char)(147); ///green
- image[e][f][0] = (unsigned char)(73); ///blue
- }
- }
- }
- generateBitmapImage((unsigned char *)image, a, a, imageFileName);
- }
- //-------------MAGNETIZATOR-3000---------------//
- //---------------------------------------------//
- /*note to self: Pocet cyklov sa meni tu, nezabudni, ze to chrume viac vlakien, tak neprehanaj*/
- void magnetizator()
- {
- random_device rad;
- thread_local mt19937_64 rd(rad());
- uniform_int_distribution<int> dis(0,a-1); // RNG pre magnetizator
- int sl,sr,sd,su;
- for (long int N=0;N<1250000;N++) // multithreading sa oplati az pri vysokych hraniciach N, pri N okolo
- {
- int x = dis(rd); // nahodne zvolenie x-ovej suradnice
- int y = dis(rd); //dis(ran); // nahodne zvolenie y-ovej suradnice
- long int t=M[x][y];
- (x==0)?sl=t:sl=M[x-1][y];
- (x==a-1)?sr=t:sr=M[x+1][y];
- (y==0)?sd=t:sd=M[x][y-1];
- (y==a-1)?su=t:su=M[x][y+1];
- (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
- }
- cout << "thread ended" << endl;
- END++;
- }
- //-----------THREAD-MANGLER-v4.0.2-------------//
- //--------------------MAIN---------------------//
- int main(int argc, char **argv)
- {
- int AA=0;
- long int v=0;
- long int MAG; // celkova magnetizacia
- double T=2; // teplota
- //-------pocitadlo-pravdepodobnosti--------//
- for (int pp = 0; pp < 5; pp++)
- {
- P[pp]=1.0/(1+exp((4*(pp+1)-12)/T));
- }
- cout<<"probabilities calculated"<<endl;
- //------------generator-mriezky------------//
- generator();
- cout<<"generated"<<endl;
- bmp(v); v++;
- //--------cyklus magnetizatora-3000--------//
- int numThreads=4;
- if (argc>1)
- {
- numThreads=1;
- cout << "running in single thread"<<endl;
- }
- while (AA<10)
- {
- cout<<"begin"<<endl;
- END=0;
- MAG=0;
- for (int i=0;i<numThreads;i++)
- {
- thread u1(magnetizator);
- u1.detach();
- cout<<"thread constructed"<<endl;
- }
- while(END<numThreads)
- {this_thread::sleep_for(chrono::milliseconds(50));
- }
- bmp(v);
- v++;
- AA++;
- cout << "cycle "<<AA<<" ended" << endl;
- }
- //-----pocitadlo-celkovej-magnetizacie-----//
- for (int R=0;R<a;R++) // pocita celkovu magnetizaciu
- {
- for (int Q=0;Q<a;Q++)
- (M[R][Q]<0)?MAG--:MAG++;
- }
- //-----------------------------------------//
- ofstream fout("out_10", ios_base::app);
- fout << T << "\t" << MAG <<endl;
- AA++;
- cout << "\7";
- }
- // nejaka funkcia k bmp, tiez ukradnute
- void generateBitmapImage(unsigned char *image, int height, int width, char* imageFileName)
- {
- unsigned char* fileHeader = createBitmapFileHeader(height, width);
- unsigned char* infoHeader = createBitmapInfoHeader(height, width);
- unsigned char padding[3] = {0, 0, 0};
- int paddingSize = (4-(width*bytesPerPixel)%4)%4;
- FILE* imageFile = fopen(imageFileName, "wb");
- fwrite(fileHeader, 1, fileHeaderSize, imageFile);
- fwrite(infoHeader, 1, infoHeaderSize, imageFile);
- int i;
- for(i=0; i<height; i++){
- fwrite(image+(i*width*bytesPerPixel), bytesPerPixel, width, imageFile);
- fwrite(padding, 1, paddingSize, imageFile);
- }
- fclose(imageFile);
- }
- // dalsia
- unsigned char* createBitmapFileHeader(int height, int width)
- {
- int fileSize = fileHeaderSize + infoHeaderSize + bytesPerPixel*height*width;
- static unsigned char fileHeader[] = {
- 0,0, /// signature
- 0,0,0,0, /// image file size in bytes
- 0,0,0,0, /// reserved
- 0,0,0,0, /// start of pixel array
- };
- fileHeader[ 0] = (unsigned char)('B');
- fileHeader[ 1] = (unsigned char)('M');
- fileHeader[ 2] = (unsigned char)(fileSize );
- fileHeader[ 3] = (unsigned char)(fileSize>> 8);
- fileHeader[ 4] = (unsigned char)(fileSize>>16);
- fileHeader[ 5] = (unsigned char)(fileSize>>24);
- fileHeader[10] = (unsigned char)(fileHeaderSize + infoHeaderSize);
- return fileHeader;
- }
- // dalsia
- unsigned char* createBitmapInfoHeader(int height, int width)
- {
- static unsigned char infoHeader[] = {
- 0,0,0,0, /// header size
- 0,0,0,0, /// image width
- 0,0,0,0, /// image height
- 0,0, /// number of color planes
- 0,0, /// bits per pixel
- 0,0,0,0, /// compression
- 0,0,0,0, /// image size
- 0,0,0,0, /// horizontal resolution
- 0,0,0,0, /// vertical resolution
- 0,0,0,0, /// colors in color table
- 0,0,0,0, /// important color count
- };
- infoHeader[ 0] = (unsigned char)(infoHeaderSize);
- infoHeader[ 4] = (unsigned char)(width );
- infoHeader[ 5] = (unsigned char)(width>> 8);
- infoHeader[ 6] = (unsigned char)(width>>16);
- infoHeader[ 7] = (unsigned char)(width>>24);
- infoHeader[ 8] = (unsigned char)(height );
- infoHeader[ 9] = (unsigned char)(height>> 8);
- infoHeader[10] = (unsigned char)(height>>16);
- infoHeader[11] = (unsigned char)(height>>24);
- infoHeader[12] = (unsigned char)(1);
- infoHeader[14] = (unsigned char)(bytesPerPixel*8);
- return infoHeader;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement