Advertisement
Digonto

explosive percolator openacc

Oct 31st, 2017
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.27 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #include "openacc_curand.h"
  3. #include<curand.h>
  4. using namespace std;
  5.  
  6. int EMPTY = 0;
  7. int connectionNumber = 0;
  8. //int cao = 0;
  9.  
  10. #pragma acc routine seq
  11. int findroot(int ptr[],int i)
  12. {
  13.     if(ptr[i]<0) return i;
  14.    
  15.     return ptr[i]=findroot(ptr,ptr[i]);
  16. }  
  17.  
  18. /*
  19. int findroot(int ptr[],int i)
  20. {
  21.     //cao = 1;
  22.     int r,s;
  23.     r = s = i;
  24.     while (ptr[r]>=0)
  25.     {
  26.         ptr[s] = ptr[r];
  27.         s = r;
  28.         r = ptr[r];
  29.     }
  30.     return r;
  31. }
  32. */
  33.  
  34. int main()
  35. {
  36.     int seed,vertices,m,run,filenum;
  37.    
  38.     cout<<"enter seed size: ";
  39.     cin>>seed;
  40.     cout<<endl;
  41.     cout<<"enter vertice number: ";
  42.     cin>>vertices;
  43.     cout<<endl;
  44.     cout<<"order number: ";
  45.     cin>>m;
  46.     cout<<endl;
  47.     cout<<"enter ensemble number: ";
  48.     cin>>run;
  49.     cout<<endl;
  50.     cout<<"enter filenumber: ";
  51.     cin>>filenum;
  52.     cout<<endl;
  53.  
  54.     int con = 0;
  55.  
  56.     for(int i=1;i<seed;i++)
  57.     {
  58.         con = con + i;
  59.     }
  60.    
  61.     con = con + (vertices-seed)*m;
  62.  
  63.     int* netmap1 = new int[con+1];
  64.     int* netmap2 = new int[con+1];
  65.    
  66.     for(int i=1;i<=con;i++)
  67.     {
  68.         netmap1[i] = 0;
  69.         netmap2[i] = 0;
  70.     }
  71.  
  72.     connectionNumber = con;
  73.  
  74.     srand(time(NULL));
  75.     int A,B,C;
  76.     A = vertices;
  77.     B = run;
  78.     C = filenum;
  79.  
  80.     string filename1;
  81.     filename1 = "maxclus_";
  82.     string filename2;
  83.     filename2 = "delmx_";
  84.     string filename3;
  85.     filename3 = "entropy_";
  86.  
  87.     filename1 = filename1+to_string(A)+"node_"+to_string(m)+"M_"+to_string(B)+"ens"+to_string(C)+".dat";
  88.     filename2 = filename2+to_string(vertices)+"node_"+to_string(m)+"M_"+to_string(run)+"ens"+to_string(filenum)+".dat";
  89.     filename3 = filename3+to_string(vertices)+"node_"+to_string(m)+"M_"+to_string(run)+"ens"+to_string(filenum)+".dat";
  90.  
  91.     ofstream fout1,fout2,fout3;
  92.    
  93.     int* random = NULL;
  94.     random = new int[connectionNumber+1];
  95.    
  96.     double* maxclus = NULL;
  97.     maxclus = new double[connectionNumber+1];
  98.    
  99.     double* delmx = NULL;
  100.     delmx = new double[connectionNumber+1];
  101.    
  102.     double* entropycalc = NULL;
  103.     entropycalc = new double[connectionNumber+1];
  104.  
  105.     for(int i=0;i<=connectionNumber;i++)
  106.     {
  107.         maxclus[i]=0;
  108.         delmx[i]=0;
  109.         entropycalc[i]=0;
  110.     }
  111.  
  112.     for(int i=0;i<=connectionNumber;i++)
  113.     {
  114.         random[i] = i;
  115.     }
  116.     int* ptr = new int[vertices+1];
  117.     for(int i=0;i<vertices+1;i++) ptr[i]=0;
  118.  
  119.  
  120.     for(int i=1;i<=25;i++)
  121.     {
  122.         cout<<"run : "<<i<<endl;
  123.         vector<int> network;   
  124.         connectionNumber = 0;  
  125.    
  126.         for(int i=1;i<=seed;i++)
  127.         {
  128.             for(int j=1;j<=seed;j++)
  129.             {
  130.                 if(j>i)
  131.                 {
  132.                
  133.                     connectionNumber=connectionNumber + 1;
  134.                     netmap1[connectionNumber]=i;
  135.                     netmap2[connectionNumber]=j;
  136.                     network.push_back(i);
  137.                     network.push_back(j);
  138.                 }
  139.             }
  140.         }  
  141.  
  142.         int concheck = 0;
  143.         int ab[m];
  144.         int countm = 0;
  145.  
  146.         for(int i = seed+1;i<=vertices; i++)
  147.         {
  148.             countm = 0;
  149.             for(int k=0;k<m;k++) ab[k] = 0;
  150.             //if(i%10000==0)random_shuffle( network.begin(),network.end() );
  151.             //if(i%50000==0)cout<<"connection given to : "<<i<<endl;
  152.             for(int j = 1; ;j++)
  153.             {
  154.                 concheck = 0;
  155.                 int N1=network.size() ;
  156.                 int M1=0;                  
  157.                 int u = M1 + rand()/(RAND_MAX/(N1-M1+1) + 1);
  158.                 /*if(j==1)cout<<"netsize:"<<network.size()<<endl;
  159.                 if(j==1)cout<<"u:"<<u<<endl;
  160.                 if(j==1)cout<<"node:"<<network[u]<<endl;*/
  161.                 for(int n=0;n<m;n++)
  162.                 {
  163.                     if(ab[n] == network[u]) concheck = 1;
  164.                 }
  165.    
  166.                 if(concheck == 0 && network[u]!=i)  
  167.                 {
  168.                     ab[countm] = network[u];
  169.                     countm=countm+1;
  170.    
  171.                    
  172.                
  173.                     connectionNumber=connectionNumber+1;
  174.                     netmap1[connectionNumber] = i;
  175.                     netmap2[connectionNumber] = network[u];
  176.  
  177.                     network.push_back(i);
  178.                     network.push_back(network[u]);
  179.                 }
  180.  
  181.                 if(countm==m) break;
  182.  
  183.             }
  184.         }
  185.         random_shuffle(&random[1],&random[connectionNumber]);
  186.        
  187.         double rand_seed = time(NULL);
  188.        
  189.         #pragma acc data copy(maxclus[0:connectionNumber],delmx[0:connectionNumber],entropycalc[0:connectionNumber]), copyin(netmap1[0:connectionNumber],netmap2[0:connectionNumber],random[0:connectionNumber])
  190.         #pragma acc enter data copyin(ptr[0:vertices+1])
  191.         #pragma acc parallel loop independent private(random[0:connectionNumber], ptr[0:vertices+1])
  192.         for(int rx=1;rx<=1000;rx++)
  193.         {  
  194.            
  195.             int index=0,big=0,bigtemp=0,jump=0,en1=0,en2=0;
  196.             int node1=0,node2=0,node3=0,node4=0,nodeA=0,nodeB=0;
  197.             int clus1=0,clus2=0,clus3=0,clus4=0;
  198.             double entropy = log(vertices);
  199.            
  200.             curandState_t state;
  201.             curand_init(rand_seed*rx,0,0,&state);
  202.            
  203.            
  204.             for(int i=1;i<=connectionNumber;i++)
  205.             {
  206.                 //cout<<"con number : "<<i<<endl;
  207.                 if(i!=connectionNumber)
  208.                 {
  209.                    
  210.                     node1 = netmap1[random[i]];
  211.                     node2 = netmap2[random[i]];
  212.                     node3 = netmap1[random[i+1]];
  213.                     node4 = netmap2[random[i+1]];
  214.                
  215.                     if(ptr[node1]==EMPTY) clus1 = 1;
  216.                     else
  217.                     {
  218.                         int x = findroot(ptr,node1);
  219.                         clus1 = -ptr[x];
  220.                     }
  221.                    
  222.                     if(ptr[node2]==EMPTY) clus2 = 1;
  223.                     else
  224.                     {
  225.                         int b = findroot(ptr,node2);
  226.                         clus2 = -ptr[b];
  227.                     }
  228.                
  229.                     if(ptr[node3]==EMPTY) clus3 = 1;
  230.                     else
  231.                     {
  232.                         int c = findroot(ptr,node3);
  233.                         clus3 = -ptr[c];
  234.                     }
  235.  
  236.                     if(ptr[node4]==EMPTY) clus4 = 1;
  237.                     else
  238.                     {
  239.                         int d = findroot(ptr,node4);
  240.                         clus4 = -ptr[d];
  241.                     }
  242.                
  243.                     if(clus1*clus2<clus3*clus4)
  244.                     {
  245.                         nodeA = node1;
  246.                         nodeB = node2;
  247.                
  248.                    
  249.                         int N1=connectionNumber;
  250.                         int M1=i+1;
  251.                          //int u = M1 + rand()/(RAND_MAX/(N1-M1+1) + 1);
  252.                         int u = M1 + (curand_uniform(&state)*(N1-M1));                 
  253.                        
  254.                         int temp = random[u];
  255.                         random[u] = random[i+1];
  256.                         random[i+1] = temp;
  257.                     }
  258.                     else
  259.                     {
  260.                         nodeA = node3;
  261.                         nodeB = node4;
  262.                    
  263.                         int temp = random[i+1];
  264.                         random[i+1] = random[i];
  265.                         random[i] = temp;
  266.                         int N1=connectionNumber;
  267.                         int M1=i+1;                
  268.                         //int u = M1 + rand()/(RAND_MAX/(N1-M1+1) + 1);
  269.                         int u = M1 + (curand_uniform(&state)*(N1-M1));
  270.                                    
  271.                         temp = random[u];
  272.                         random[u] = random[i+1];
  273.                         random[i+1] = temp;
  274.                     }
  275.                 }
  276.                 else
  277.                 {
  278.                     nodeA = netmap1[random[i]];
  279.                     nodeB = netmap2[random[i]];
  280.                 }
  281.    
  282.                
  283.  
  284.                 if(ptr[nodeA]==EMPTY && ptr[nodeB]==EMPTY)
  285.                 {
  286.                
  287.                 en1=1;
  288.                 en2=1;
  289.                 ptr[nodeA] = -2;
  290.                 ptr[nodeB] = nodeA;
  291.                 index = nodeA;
  292.                 entropy = (double)(entropy-(-2.0/vertices*log(1.0/vertices))+(-2.0/vertices*log(2.0/vertices)));
  293.                 if(entropy<0) entropy = 0;
  294.                 }
  295.                 else if(ptr[nodeA]==EMPTY && ptr[nodeB]!=EMPTY)
  296.                 {
  297.                
  298.                 en1=1;
  299.                 int e = findroot(ptr,nodeB);
  300.                 en2 = -(ptr[e]);
  301.                 ptr[nodeA] = e;
  302.                 ptr[e] += -1;
  303.                 index = e;
  304.                 entropy = entropy-(-(double)1.0/vertices*log(1.0/(double)vertices))-(-(double)en2/vertices*log((double)en2/vertices))+(-( double)(-ptr[index])/vertices*log((-ptr[index])/(double)vertices));
  305.                 if(entropy<0) entropy = 0;
  306.                
  307.                
  308.                 }
  309.                 else if(ptr[nodeA]!=EMPTY && ptr[nodeB]==EMPTY)
  310.                 {
  311.                
  312.                 en2 = 1;
  313.                 int f = findroot(ptr,nodeA);
  314.                 en1 = -(ptr[f]);
  315.                 ptr[nodeB] = f;
  316.                 ptr[f] += -1;
  317.                 index = f;
  318.                 entropy = entropy-(-(double)1.0/(double)vertices*log(1.0/(double)vertices))-(-(double)en1/(double)vertices*log((double)en1/vertices))+(-(double)(-ptr[index])/vertices*log((-ptr[index])/(double)vertices));
  319.                 if(entropy<0) entropy = 0;
  320.                 }
  321.                 else if(ptr[nodeA]!=EMPTY && ptr[nodeB]!=EMPTY)
  322.                 {
  323.                
  324.                     int g,h;
  325.                     g = findroot(ptr,nodeA);
  326.                     en1 = -(ptr[g]);
  327.                     h = findroot(ptr,nodeB);
  328.                     en2 = -(ptr[h]);
  329.                     if(g!=h)
  330.                     {
  331.                         if(ptr[g]<ptr[h])
  332.                         {
  333.                
  334.                             ptr[g] += ptr[h];
  335.                             ptr[h] = g;
  336.                             index = g;
  337.                         }
  338.                         else
  339.                         {
  340.                
  341.                             ptr[h] += ptr[g];
  342.                             ptr[g] = h;
  343.                             index = h;
  344.                         }
  345.                         entropy = entropy-(-(double)en1/(double)vertices*log((double)en1/(double)vertices))-(-(double)en2/vertices*log((double)en2/(double)vertices))+(-(double)(-ptr[index])/vertices*log((double)(-ptr[index])/(double)vertices));
  346.                     if(entropy<0) entropy = 0;
  347.                     }
  348.                     else
  349.                     {
  350.                
  351.                         jump=big-bigtemp;
  352.                         #pragma acc atomic update
  353.                         maxclus[i] += big;
  354.                         #pragma acc atomic update
  355.                         delmx[i] += jump;
  356.                         if(entropy<0) entropy = 0;
  357.                         #pragma acc atomic update
  358.                         entropycalc[i] += entropy;
  359.                         bigtemp = big;
  360.                        
  361.                        
  362.                         continue;
  363.                     }
  364.                 }  
  365.        
  366.                 if(-ptr[index]>big) big = -ptr[index];
  367.            
  368.                 jump = big - bigtemp;
  369.                 #pragma acc atomic update
  370.                 maxclus[i] += big;
  371.                 #pragma acc atomic update
  372.                 delmx[i] += jump;
  373.                 #pragma acc atomic update
  374.                 entropycalc[i] += entropy;
  375.                 bigtemp = big;
  376.        
  377.                
  378.             }
  379.         }
  380.             /*for(int i=0;i<connectionNumber;i++)
  381.             {cout<<maxclus[i]<<'\t'<<delmx[i]<<'\t'<<entropycalc[i]<<'\t'<<endl;}//<<"ptr "<<ptr[i]<<endl;}*/
  382.     }
  383.  
  384.     fout1.open(filename1.c_str());
  385.     fout2.open(filename2.c_str());
  386.     fout3.open(filename3.c_str());
  387.    
  388.     connectionNumber = con;
  389.    
  390.     for(int i=1;i<=connectionNumber;i++)
  391.     {
  392.         fout1<<(double)i/vertices<<'\t'<<(double)maxclus[i]/vertices/run<<endl;
  393.         fout2<<(double)i/vertices<<'\t'<<(double)delmx[i]/run<<endl;
  394.         fout3<<(double)i/vertices<<'\t'<<(double)entropycalc[i]/run<<endl;
  395.     }
  396.  
  397.     fout1.close();
  398.     fout2.close();
  399.     fout3.close();
  400.        
  401.  
  402.     delete[] random;
  403.     random = NULL;
  404.     delete[] maxclus;
  405.     maxclus = NULL;
  406.     delete[] delmx;
  407.     delmx = NULL;
  408.     delete[] entropycalc;
  409.     entropycalc = NULL;
  410.    
  411.     delete [] netmap1;
  412.     netmap1 = NULL;
  413.     delete [] netmap2;
  414.     netmap2 = NULL;
  415.  
  416.     delete [] ptr;
  417.     ptr = NULL;
  418.    
  419.     return 0;
  420.    
  421. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement