ConfigFile.cc

Go to the documentation of this file.
00001 
00006 extern "C"
00007 {
00008  #include <sys/types.h>
00009  #include <sys/stat.h>
00010  #include <unistd.h>
00011 }
00012 
00013 
00014 // a few parameters for easy-factorization (and their default values)
00015 bool SkipFermat    = false; // try the fermat method?
00016 bool UsePhimat     = false; // use the phimat method? (takes very long!)
00017 bool SkipPhi       = false; // try Pollard-Phi-Method?
00018 bool SkipFibonacci = false; // try Fibonacci-Method?
00019 bool SkipEasyECM   = false; // try elliptic curve method im easy-mode?
00020 bool SkipECM       = false; // try elliptic curve method?
00021 bool PrintSummary  = false; // do we a summary when factorization is complete?
00022 int rho_Phase      =   150000; // main initialization for Rho-Phase
00023 int phi_Phase1     =   500000; // upper bound for Phase 1 in phi-Method
00024 double phi_Phase2  = 10000000.0; // upper bound for phi-Method Phase 2 (=isolated factor)
00025 double elcu_Phase1 =    20000.0; // upper bound for elliptic curve Phase 1
00026 double elcu_Phase2 =  2000000.0; // upper bound for elliptic curve Phase 2
00027 int elcu_Kurven    =      100; // number of elliptic curves to try
00028 int ecm_curves_processed = 0; // number of curves done so far
00029 
00030 double Factor_Threshold = 3.0; /* exponent, which allows bigger factors (which are not part of the static factorbase)
00031                                   suggestive values: 1.0 up to 3.0
00032                                   1.0: no additional factors outside the static factorbase
00033                                   up to 2.0: prime factors outside the static factorbase
00034                                              up to the square of the biggest factor inside the static FB.
00035                                   >2.0: factors up to (biggest factor inside the static FB) raised to the power of Factor_Threshold.
00036                                */
00037 
00038 
00039 string XML_StatusFile;
00040 
00041 string TempDir              = "/tmp"; // default directory for temporary files
00042 string ConfigFile           = "qsieve.cfg"; // default, may be overwritten by getenv("QSIEVE_CFG")
00043 string DynamicRelationsFile = "dynamic_relations.dat"; // default, may be overwritten by entry in configfile
00044 
00045 string ClientAccountName    = ""; // can be set by ConfigFile. If empty, then socket info will be used instead of an account name
00046 
00047 unsigned long int FFT_MAX_MEM_USAGE = 32*1024;
00048 
00049 
00050 #ifndef IS_CLIENT
00051 const string FactorizationsFile   = "factorizations.txt";
00052 std::ofstream Factorization_to_file;
00053 /* Factorizations are logged to this file. Numbers to factorize and any
00054    found factors are appended. The file can be deleted, if the logged data
00055    is of no use anymore.
00056 */
00057 #endif
00058 
00059 
00061 const string read_restrictedName(istream &is)
00062 {
00063   string s;
00064   while (is.good() && is.peek()!=EOF && isblank(is.peek())) is.get();
00065   while (is.good() && is.peek()!=EOF && s.length()<=40)
00066    {
00067      int i=is.get();
00068      if (isalnum(i) || i==' ' || i=='@' || i=='#' || i==',' || i=='.' || i=='+' || i=='-') s+=static_cast<char>(i);
00069      else return s;
00070    }
00071   return s;
00072 }
00073 
00074 void Get_ConfigFile()
00075 {
00076   // resolve ConfigFile
00077   if (getenv("QSIEVE_CFG"))
00078    {
00079      ConfigFile=getenv("QSIEVE_CFG");
00080 #ifdef VERBOSE_NOTICE
00081      cout << "Setting ConfigFile to \"" << ConfigFile << "\"" << endl;
00082 #endif
00083    }
00084   // and convert it to real/absolute path
00085 #ifdef PATH_MAX
00086   char resolved_path[PATH_MAX];
00087 #else
00088   char resolved_path[4096];
00089 #endif /* PATH_MAX */
00090   struct stat buf;
00091   // for Cygwin: realpath doesn't return an error, if file doesn't exist
00092   // we ignore file permission issues and use stat to check for existence
00093   if (realpath(ConfigFile.c_str(),resolved_path) && (stat(resolved_path,&buf)==0))
00094    ConfigFile=resolved_path;
00095   else
00096    {
00097      cerr << "Cannot find ConfigFile " << "\"" << ConfigFile << "\"" << endl;
00098 
00099 #if defined(etc_ConfigFile)
00100      cout << "trying to use \"" << etc_ConfigFile << "\"" << endl;
00101      if (realpath(etc_ConfigFile,resolved_path) && (stat(resolved_path,&buf)==0))
00102        {
00103          ConfigFile=resolved_path;
00104          return;
00105        }
00106      else
00107        cerr << "no ConfigFile found!" << endl;
00108 #endif
00109 
00110      // server & standalone versions need a configuration file,
00111      // clients do not need it (but they make use of it as well)
00112 #ifndef IS_CLIENT
00113      exit(1);
00114 #endif
00115    }
00116 }
00117 
00118 
00119 void Read_ConfigFile(void)
00120 {
00121   Get_ConfigFile(); // get position of configfile (evaluate environment, etc.)
00122 
00123   ifstream in(ConfigFile.c_str());
00124   char InputLine[200];
00125   string zeile;
00126   if (in)
00127     {
00128 #ifdef VERBOSE_INFO
00129       cout << "Reading configuration file" << endl;
00130 #ifdef IS_CLIENT
00131       cout << "Some server related options may have no effect to clients." << endl;
00132 #endif
00133 #endif
00134       while (!in.eof())
00135         {
00136           in.getline(InputLine,sizeof(InputLine),'\n');
00137           if (InputLine[0]=='#') continue; // read over comment lines
00138           zeile=InputLine;
00139           while (zeile[0]==' ') zeile.erase(0,1);
00140           if (zeile.empty() || zeile[0]=='#') continue; // read over empty lines and comment lines
00141 
00142           // process keywords
00143           istringstream is(zeile);
00144           string Keyword;
00145           is >> Keyword;
00146           if (Keyword=="DynamicRelationsFile")
00147            {
00148              string s;
00149              is >> s;
00150              if (s!="=")
00151               {
00152                 cerr << "Error in " << ConfigFile  << "!" << endl;
00153                 exit(1);
00154               }
00155              is >> DynamicRelationsFile;
00156              continue;
00157            }
00158           if (Keyword=="WorkDir")
00159            {
00160              string s;
00161              is >> s;
00162              if (s!="=")
00163               {
00164                 cerr << "Error in " << ConfigFile  << "!" << endl;
00165                 exit(1);
00166               }
00167              is >> s;
00168 #ifdef VERBOSE_NOTICE
00169              cout << "Setting current working directory to \"" << s << "\"." << endl;
00170 #endif
00171              if (chdir(s.c_str())!=0)
00172               {
00173                 cerr << "Changing working directory failed!" << endl;
00174                 exit(2);
00175               };
00176              continue;
00177            }
00178           if (Keyword=="TempDir")
00179            {
00180              string s;
00181              is >> s;
00182              if (s!="=")
00183               {
00184                 cerr << "Error in " << ConfigFile  << "!" << endl;
00185                 exit(1);
00186               }
00187              is >> s;
00188 #ifdef VERBOSE_NOTICE
00189              cout << "Setting TempDir to \"" << s << "\"." << endl;
00190 #endif
00191              TempDir=s; 
00192              continue;
00193            }
00194           if (Keyword=="XMLStatusFile")
00195            {
00196              string s;
00197              is >> s;
00198              if (s!="=")
00199               {
00200                 cerr << "Error in " << ConfigFile  << "!" << endl;
00201                 exit(1);
00202               }
00203              is >> s;
00204 #ifdef VERBOSE_NOTICE
00205              cout << "Setting current XMLStatusFile to \"" << s << "\"." << endl;
00206 #endif
00207              XML_StatusFile=s; 
00208              continue;
00209            }
00210           if (Keyword=="ClientAccount")
00211            {
00212              string s;
00213              is >> s;
00214              if (s!="=")
00215               {
00216                 cerr << "Error in " << ConfigFile  << "!" << endl;
00217                 exit(1);
00218               }
00219              s=read_restrictedName(is);
00220 #ifdef VERBOSE_NOTICE
00221              cout << "Setting current ClientAccount to \"" << s << "\"." << endl;
00222 #endif
00223              ClientAccountName=s; 
00224              continue;
00225            }
00226           if (Keyword=="SkipFermat")
00227            {
00228              SkipFermat=true;
00229              continue;
00230            }
00231           if (Keyword=="UsePhimat")
00232            {
00233              UsePhimat=true;
00234              continue;
00235            }
00236           if (Keyword=="SkipPhi")
00237            {
00238              SkipPhi=true;
00239              continue;
00240            }
00241           if (Keyword=="SkipFibonacci")
00242            {
00243              SkipFibonacci=true;
00244              continue;
00245            }
00246           if (Keyword=="SkipEasyECM")
00247            {
00248              SkipEasyECM=true;
00249              continue;
00250            }
00251           if (Keyword=="SkipECM")
00252            {
00253              SkipECM=true;
00254              continue;
00255            }
00256           if (Keyword=="PrintSummary")
00257            {
00258              PrintSummary=true;
00259              continue;
00260            }
00261           if (Keyword=="MemoryLimit")
00262            {
00263              string s;
00264              is >> s;
00265              if (s!="=")
00266               {
00267                 cerr << "Error in " << ConfigFile  << "!" << endl;
00268                 exit(1);
00269               }
00270              is >> FFT_MAX_MEM_USAGE;
00271              FFT_MAX_MEM_USAGE<<=10; // MB -> KB
00272 #ifdef VERBOSE_NOTICE
00273              cout << "Setting FFT MemoryLimit to " << (FFT_MAX_MEM_USAGE>>10) << "MB." << endl;
00274 #endif
00275              continue;
00276            }
00277         }
00278     }
00279   else
00280     {
00281        cout << ConfigFile << " not found! Using default values!" << endl;
00282 #ifndef IS_CLIENT
00283        static char ch = 'N';
00284        if (ch!='Y')
00285          {
00286            cout << "Please confirm with [Y]es." << endl;
00287            cin >> ch;
00288            if (ch!='Y') exit(1);
00289          }
00290 #endif
00291     }
00292 
00293 #ifdef VERBOSE_INFO
00294   char cwd[4096];
00295   cout << "Current working directory: " << getcwd(cwd,4096) << endl;
00296   cout << "Configuration file: " << ConfigFile << endl;
00297   cout << "temp directory: " << TempDir << endl;
00298   cout << "DynamicRelationsFile: " << DynamicRelationsFile << endl;
00299 #endif
00300 #if !defined(IS_CLIENT) && !defined(IS_VALIDATOR)
00301   // clients do report factors to the server and not to this file,
00302   // so only stand-alone versions and servers need this:
00303   if (!Factorization_to_file.is_open()) Factorization_to_file.open(FactorizationsFile.c_str(),ios::out|ios::app);
00304 #endif
00305 }
00306 
00307 
00308 #ifndef IS_CLIENT
00309 // clients will be configured by the server, so only stand-alone and server versions need this...
00310 void tune_parameters(const unsigned int DecimalDigits)
00311 {
00312   ifstream in(ConfigFile.c_str());
00313   char InputLine[200];
00314   string zeile;
00315   unsigned int Stellen, gewaehlte_Stellenzahl=0;
00316   if (in)
00317     {
00318 #ifdef VERBOSE_INFO
00319       cout << "Reading configuration file" << endl; 
00320 #endif
00321       while (!in.eof())
00322         {
00323           in.getline(InputLine,sizeof(InputLine),'\n');
00324           if (InputLine[0]=='#') continue; // read over comment lines
00325           zeile=InputLine;
00326           while (zeile[0]==' ') zeile.erase(0,1);
00327           if (zeile.empty() || zeile[0]=='#') continue; // read over empty lines / comment lines
00328 
00329           // process keywords
00330           istringstream is(zeile);
00331           string Keyword;
00332           is >> Keyword;
00333           if (Keyword=="SkipFermat") continue;
00334           if (Keyword=="UsePhimat") continue;
00335           if (Keyword=="SkipPhi") continue;
00336           if (Keyword=="SkipFibonacci") continue;
00337           if (Keyword=="SkipEasyECM") continue;
00338           if (Keyword=="SkipECM") continue;
00339           if (Keyword=="PrintSummary") continue;
00340           if (Keyword=="MemoryLimit") continue;
00341           if (Keyword=="DynamicRelationsFile" || Keyword=="WorkDir" || Keyword=="TempDir" || Keyword=="XMLStatusFile" || Keyword=="ClientAccount")
00342            {
00343              string s;
00344              is >> s;
00345              if (s!="=")
00346               {
00347                 cerr << "Error in " << ConfigFile  << "!" << endl;
00348                 exit(1);
00349               }
00350              // is >> DynamicRelationsFile; // do not read anything here!
00351              continue;
00352            }
00353 
00354           // otherwise: line contains parameter for tuning
00355           Stellen = atoi(zeile.substr(0, zeile.find(":")).c_str());
00356 
00357           if (Stellen==0)
00358            {
00359              cerr << "Error in " << ConfigFile  << "!" << endl;
00360              cerr << "-> " << zeile << endl;
00361              exit(1);
00362            }
00363 
00364           if (Stellen > DecimalDigits || Stellen==0) break;
00365           gewaehlte_Stellenzahl=Stellen;
00366           zeile.erase(0, zeile.find(":")+1);
00367           rho_Phase=atoi(zeile.substr(0, zeile.find(",")).c_str());
00368           zeile.erase(0, zeile.find(",")+1);
00369           phi_Phase1=atoi(zeile.substr(0, zeile.find(",")).c_str());
00370           zeile.erase(0, zeile.find(",")+1);
00371           phi_Phase2=strtod(zeile.substr(0, zeile.find(",")).c_str(),NULL);
00372           zeile.erase(0, zeile.find(",")+1);
00373           elcu_Phase1=strtod(zeile.substr(0, zeile.find(",")).c_str(),NULL);
00374           zeile.erase(0, zeile.find(",")+1);
00375           elcu_Phase2=strtod(zeile.substr(0, zeile.find(",")).c_str(),NULL);
00376           zeile.erase(0, zeile.find(",")+1);
00377           elcu_Kurven=atoi(zeile.substr(0, zeile.find(",")).c_str());
00378           zeile.erase(0, zeile.find(",")+1);
00379           StaticFactorbase::Size_StaticFactorbase=atoi(zeile.substr(0, zeile.find(":")).c_str());
00380           zeile.erase(0, zeile.find(":")+1);
00381           Factor_Threshold = atof(zeile.substr(0, zeile.find(":")).c_str());
00382           if (Factor_Threshold<=0.01 || Factor_Threshold>10.0)
00383            {
00384              MARK;
00385              cerr << "invalid value of Factor_Threshold: " << Factor_Threshold << endl;
00386              exit(1);
00387            }
00388           zeile.erase(0, zeile.find(":")+1);
00389 
00390           LogicalSieveSize = atoi(zeile.c_str());
00391           LogicalSieveSize = MAX(LogicalSieveSize,PhysicalSieveSize);
00392         }
00393     }
00394   else
00395     {
00396        cout << ConfigFile << " not found! Using default values!" << endl;
00397        static char ch = 'N';
00398        if (ch!='Y')
00399          {
00400            cout << "Please confirm with [Y]es." << endl;
00401            cin >> ch;
00402            if (ch!='Y') exit(1);
00403          }
00404     }
00405 
00406 #ifdef VERBOSE_INFO  
00407   cout << "Configuration:" << endl;
00408   cout << "#decimal digits: " << DecimalDigits << endl;
00409   cout << "actual parameters for " << gewaehlte_Stellenzahl << " digits" << endl;
00410   cout << "Pollard-Rho: " << rho_Phase << endl;
00411   cout << "Pollard-Phi: phase 1: " << phi_Phase1 << " phase 2: " << setprecision(1) << phi_Phase2 << endl;
00412   cout << "elliptic curves: phase 1: " << elcu_Phase1
00413        << " phase 2: " << elcu_Phase2
00414        << " #curves: " << elcu_Kurven << endl; 
00415   cout << "Size of static factorbase: " << StaticFactorbase::Size() << endl;
00416   cout << "factor-threshold (exponent): " << Factor_Threshold << endl;
00417   cout << "sieve interval per polynomial: [" << -LogicalSieveSize << "," << LogicalSieveSize << "]" << endl;
00418 #endif
00419 
00420   if (StaticFactorbase::Size()>StaticFactorbase::MaxSize)
00421     {
00422       cerr << "MaxSize_StaticFactorbase is too small (or Size_StaticFactorbase is to big)!" << endl;
00423       exit(1);
00424     }
00425 }
00426 #endif /* not defined(IS_CLIENT) */

Generated on Wed Nov 7 23:29:25 2007 for Qsieve by  doxygen 1.5.4