Exemple #1
0
void Options::finish()
{
    // TODO: Consider how to customize these error messages based on context.
    ExceptionInitializer             errors("Invalid input values");
    Impl::OptionList::const_iterator i;
    for (i = impl_->options_.begin(); i != impl_->options_.end(); ++i)
    {
        AbstractOptionStorage &option = **i;
        try
        {
            option.finish();
        }
        catch (UserInputError &ex)
        {
            ex.prependContext("In option " + option.name());
            errors.addCurrentExceptionAsNested();
        }
    }
    Impl::SubSectionList::const_iterator j;
    for (j = impl_->subSections_.begin(); j != impl_->subSections_.end(); ++j)
    {
        Options &section = **j;
        try
        {
            section.finish();
        }
        catch (const UserInputError &)
        {
            errors.addCurrentExceptionAsNested();
        }
    }
    if (errors.hasNestedExceptions())
    {
        // TODO: This exception type may not always be appropriate.
        GMX_THROW(InvalidInputError(errors));
    }
}
Exemple #2
0
// main function
int main(int argc, const char * argv[])
{
  gettimeofday(&start_timeval_rd1, NULL);
  SparseMatrix<double> Gx_a(NX,NA);
  Gx_a.resize(NX,NA);
  SparseMatrix<double> Gx_b(NX,NB);
  Gx_b.resize(NX,NB);
  SparseMatrix<double> Gx_c(NX,NC);
  Gx_c.resize(NX,NC);
  Gx_a.makeCompressed();
  Gx_b.makeCompressed();
  Gx_c.makeCompressed();
  // reading the partitions
  Gx_a = read_G_sparse((char *) FILE_GA , "GX_A" ,NX, NA);
  Gx_b = read_G_sparse((char *) FILE_GB , "GX_B" ,NX, NB);
  Gx_c = read_G_sparse((char *) FILE_GC , "GX_C" ,NX, NC);

  gettimeofday(&stop_timeval_rd1, NULL);
  measure_stop_rd1 = stop_timeval_rd1.tv_usec + (timestamp_t)stop_timeval_rd1.tv_sec * 1000000;
  measure_start_rd1 = start_timeval_rd1.tv_usec + (timestamp_t)start_timeval_rd1.tv_sec * 1000000;
  time_rd1 = (measure_stop_rd1 - measure_start_rd1) / 1000000.0L;
  printf("Exec Time reading matrices before preproc = %5.25e (Seconds)\n",time_rd1);
  
  // initialize W, Z_B,Z_C, mu_a, mu_b, mu_c;
  SparseMatrix<double> W(NA,KHID); W.resize(NA,KHID); W.makeCompressed();
  SparseMatrix<double> Z_B(NA,NB); Z_B.resize(NA,NB); Z_B.makeCompressed();
  SparseMatrix<double> Z_C(NA,NC); Z_C.resize(NA,NC); Z_C.makeCompressed();
  VectorXd mu_a(NA);
  VectorXd mu_b(NB); 
  VectorXd mu_c(NC);
  
  cout << "----------------------------Before whitening--------------------------" << endl;
  gettimeofday(&start_timeval_pre, NULL);  // measuring start time for pre processing
  second_whiten(Gx_a,Gx_b,Gx_c,W,Z_B,Z_C,mu_a,mu_b,mu_c);
  // whitened datapoints
  SparseMatrix<double> Data_a_G = W.transpose() * Gx_a.transpose();
  VectorXd Data_a_mu  = W.transpose() * mu_a;
  SparseMatrix<double> Data_b_G = W.transpose() * Z_B * Gx_b.transpose();
  VectorXd Data_b_mu  = W.transpose() * Z_B * mu_b;
  SparseMatrix<double> Data_c_G = W.transpose() * Z_C * Gx_c.transpose();
  VectorXd Data_c_mu  = W.transpose() * Z_C * mu_c;
  gettimeofday(&stop_timeval_pre, NULL);   // measuring stop time for pre processing
  
  measure_stop_pre = stop_timeval_pre.tv_usec + (timestamp_t)stop_timeval_pre.tv_sec * 1000000;
  measure_start_pre = start_timeval_pre.tv_usec + (timestamp_t)start_timeval_pre.tv_sec * 1000000;
  time_pre = (measure_stop_pre - measure_start_pre) / 1000000.0L;
  printf("time taken by preprocessing = %5.25e (Seconds)\n",time_pre);
    
  cout << "----------------------------After whitening---------------------------" << endl;
  // stochastic updates
  VectorXd lambda(KHID); 
  MatrixXd phi_new(KHID,KHID);
  cout << "------------------------------Before tensor decomposition----------------" << endl;
  gettimeofday(&start_timeval_stpm, NULL); // measuring start time for stochastic updates
  tensorDecom_alpha0(Data_a_G,Data_a_mu,Data_b_G,Data_b_mu,Data_c_G,Data_c_mu,lambda,phi_new);
  gettimeofday(&stop_timeval_stpm, NULL);  // measuring stop time for stochastic updates
  cout << "after tensor decomposition" << endl;
  measure_stop_stpm = stop_timeval_stpm.tv_usec + (timestamp_t)stop_timeval_stpm.tv_sec * 1000000;
  measure_start_stpm = start_timeval_stpm.tv_usec + (timestamp_t)start_timeval_stpm.tv_sec * 1000000;
  time_stpm = (measure_stop_stpm - measure_start_stpm) / 1000000.0L;
  cout << "------------------------------After tensor decomposition----------------" << endl;  
  printf("time taken by stochastic tensor decomposition = %5.25e (Seconds)\n",time_stpm);
  
  //cout <<  phi_new << endl; // optionally print eigenvectors
  cout << "the eigenvalues are:" << endl;
  cout << lambda << endl;
  
  
  // post processing
  cout << "------------Reading Gb_a, Gc_a---------"<<endl;
  gettimeofday(&start_timeval_rd2, NULL);
#ifdef CalErrALL
  // read the matrix Gab and Gac
  SparseMatrix<double> Gb_a(NB,NA);Gb_a.resize(NB,NA);
  SparseMatrix<double> Gc_a(NC,NA);Gc_a.resize(NC,NA);
  Gb_a = read_G_sparse((char *) FILE_Gb_a, "GB_A" ,NB, NA); Gb_a.makeCompressed();
  Gc_a = read_G_sparse((char *) FILE_Gc_a ,"GC_A" ,NC, NA); Gc_a.makeCompressed();
    // releasing memory of Gx_a, Gx_b, Gx_c;
    Gx_b.resize(0,0);Gx_c.resize(0,0);
#endif
  MatrixXd Inv_Lambda = (pinv_vector(lambda)).asDiagonal();
  SparseMatrix<double> inv_lam_phi = (Inv_Lambda.transpose() * phi_new.transpose()).sparseView();
    
  gettimeofday(&stop_timeval_rd2, NULL);
  measure_stop_rd2 = stop_timeval_rd2.tv_usec + (timestamp_t)stop_timeval_rd2.tv_sec * 1000000;
  measure_start_rd2 = start_timeval_rd2.tv_usec + (timestamp_t)start_timeval_rd2.tv_sec * 1000000;
  time_rd2 = (measure_stop_rd2 - measure_start_rd2) / 1000000.0L;
  cout << "------------After reading Gb_a, Gc_a---------"<<endl;
  printf("time taken for reading matrices after post processing = %5.25e (Seconds)\n",time_rd2);
  
  
  
  cout << "---------------------------Computing pi matrices-----------------------------" << endl;
  gettimeofday(&start_timeval_post, NULL);  // measuring start time for post processing
  
  SparseMatrix<double> pi_x(KHID,NX);pi_x.reserve(KHID*NX);pi_x.makeCompressed();
  SparseMatrix<double> pi_x_tmp1 = inv_lam_phi * W.transpose();
    
#ifdef CalErrALL
  SparseMatrix<double> pi_a(KHID,NA);pi_a.reserve(KHID*NA);pi_a.makeCompressed();
  SparseMatrix<double> pi_b(KHID,NB);pi_b.reserve(KHID*NB);pi_b.makeCompressed();
  SparseMatrix<double> pi_c(KHID,NC);pi_c.reserve(KHID*NC);pi_c.makeCompressed();
  
  pi_a = pi_x_tmp1 * Z_B * Gb_a;
  MatrixXd pi_a_full = (MatrixXd) pi_a;pi_a.resize(0,0);
    
  pi_b = pi_x_tmp1 * Gb_a.transpose();
  MatrixXd pi_b_full = (MatrixXd) pi_b;pi_b.resize(0,0);
    
  pi_c = pi_x_tmp1 * Gc_a.transpose();
  MatrixXd pi_c_full = (MatrixXd) pi_c;pi_c.resize(0,0);
#endif
    
  pi_x =pi_x_tmp1 * Gx_a.transpose();Gx_a.resize(0,0);
  MatrixXd pi_x_full = (MatrixXd) pi_x;pi_x.resize(0,0);
  gettimeofday(&stop_timeval_post, NULL);  // measuring stop time for post processing
  measure_stop_post = stop_timeval_post.tv_usec + (timestamp_t)stop_timeval_post.tv_sec * 1000000;
  measure_start_post = start_timeval_post.tv_usec + (timestamp_t)start_timeval_post.tv_sec * 1000000;
  time_post = (measure_stop_post - measure_start_post) / 1000000.0L;
  cout << "---------After post processing------------" << endl;
  printf("time taken for post processing = %5.25e (Seconds)\n",time_post);
  cout<<"-------------------------Concatenation for pi_est-------------------- "<< endl;
  
  // store true_pi
#ifdef CalErrALL
  long PI_LEN =(long) NX+NA+NB+NC;
#else
    long PI_LEN =(long) NX;
#endif
    
  MatrixXd My_pi_true_mat(KTRUE,PI_LEN);
  MatrixXd My_pi_est_mat(KHID,PI_LEN);
#ifdef CalErrALL
  for (int kk = 0; kk < KHID; kk++)
  {
    // for My_pi_est;
    VectorXd My_pi_est1(NX+NA);
    My_pi_est1 = concatenation_vector (pi_x_full.row(kk), pi_a_full.row(kk));
    VectorXd My_pi_est2(NX+NA+NB);
    My_pi_est2 =concatenation_vector (My_pi_est1, pi_b_full.row(kk));
    VectorXd My_pi_est3(NX+NA+NB+NC);
    My_pi_est3 =concatenation_vector (My_pi_est2, pi_c_full.row(kk));
    My_pi_est_mat.row(kk) = My_pi_est3;
  }
    pi_a_full.resize(0,0);
    pi_b_full.resize(0,0);
    pi_c_full.resize(0,0);
#else
    My_pi_est_mat =pi_x_full;
#endif
    pi_x_full.resize(0,0);
  
  // converting them to stochastic matrix
  My_pi_est_mat = normProbMatrix(My_pi_est_mat);
  SparseMatrix<double> sparse_my_pi_est_mat = My_pi_est_mat.sparseView();

  cout << "-----------Before writing results: W, Z_B,Z_C and pi-----------"<<endl;
  write_pi(FILE_PI_WRITE, sparse_my_pi_est_mat);
  write_pi(FILE_WHITE_WRITE, W);
  write_pi(FILE_INVLAMPHI_WRITE, inv_lam_phi);
  cout << "-----------After writing results---------"<< endl;
  
#ifdef ErrCal // set error calculation flag if it needs to be computed
  cout << "--------------------------------Calculating error------------------------------" << endl;
  gettimeofday(&start_timeval_error, NULL);  // measuring start time for error calculation
#ifdef CalErrALL
  // calculate error
  Gb_a.resize(0,0); Gc_a.resize(0,0);
  // read pi_true, i.e., ground truth matrices
  SparseMatrix<double> Pi_true_a(KTRUE,NA);Pi_true_a.makeCompressed();Pi_true_a = read_G_sparse((char *) FILE_Pi_a , "Pi_true_A" ,KTRUE, NA);
  MatrixXd Pi_true_a_full = (MatrixXd) Pi_true_a;  Pi_true_a.resize(0,0);
  SparseMatrix<double> Pi_true_b(KTRUE,NB);Pi_true_b.makeCompressed();Pi_true_b = read_G_sparse((char *) FILE_Pi_b , "Pi_true_B" ,KTRUE, NB);
  MatrixXd Pi_true_b_full = (MatrixXd) Pi_true_b;  Pi_true_b.resize(0,0);
  SparseMatrix<double> Pi_true_c(KTRUE,NC);Pi_true_c.makeCompressed();Pi_true_c = read_G_sparse((char *) FILE_Pi_c , "Pi_true_C" ,KTRUE, NC);
  MatrixXd Pi_true_c_full = (MatrixXd) Pi_true_c;  Pi_true_c.resize(0,0);
#endif
  SparseMatrix<double> Pi_true_x(KTRUE,NX);Pi_true_x.makeCompressed();Pi_true_x = read_G_sparse((char *) FILE_Pi_x , "Pi_true_X" ,KTRUE, NX);
  MatrixXd Pi_true_x_full = (MatrixXd) Pi_true_x;  Pi_true_x.resize(0,0);
  
  /*
  // this is only for yelp, comment this for DBLP
  long PI_LEN = (long)NX;
  MatrixXd My_pi_true_mat(KTRUE,PI_LEN);
  My_pi_true_mat =  Pi_true_x_full;
  MatrixXd My_pi_est_mat(KHID,PI_LEN); 
  My_pi_est_mat = pi_x_full;
  */    
  
  cout<<"-------------------------Concatenation for pi_true-------------------- "<< endl;
#ifdef CalErrALL
  for ( int k = 0; k < KTRUE; k++)
  {
    // for My_pi_true;
    VectorXd My_pi_true1(NX+NA);
    My_pi_true1 = concatenation_vector ((Pi_true_x_full.row(k)),(Pi_true_a_full.row(k)));
    VectorXd My_pi_true2(NX+NA+NB);
    My_pi_true2 =concatenation_vector (My_pi_true1, (Pi_true_b_full.row(k)));
    VectorXd My_pi_true3(NX+NA+NB+NC);
    My_pi_true3 =concatenation_vector (My_pi_true2, (Pi_true_c_full.row(k)));
    My_pi_true_mat.row(k) = My_pi_true3;
  } 
  Pi_true_a_full.resize(0,0);
  Pi_true_b_full.resize(0,0);
  Pi_true_c_full.resize(0,0);
#else
    My_pi_true_mat = Pi_true_x_full;
#endif
  Pi_true_x_full.resize(0,0);
    
    
    double thresh_vec[NUM_THRESH] = thresh_vec_def;
    //{0.3, 0.25, 0.2, 0.18, 0.15, 0.12, 0.1,0.08};
  double error_vec[NUM_THRESH] = {0.0};
  double match_vec[NUM_THRESH] = {0.0};
  
  for (int tttt = 0; tttt < NUM_THRESH; tttt++)
  {
    MatrixXd p_values=MatrixXd::Zero(KTRUE,KHID);
    MatrixXd errors=MatrixXd::Zero(KTRUE,KHID);	  
    for ( int k = 0; k < KTRUE; k++)
    {
      VectorXd my_pi_true_eigen = My_pi_true_mat.row(k);
      double *my_pi_true = my_pi_true_eigen.data();
      for (int kk = 0; kk < KHID; kk++)
      {
	VectorXd my_pi_est_eigen = My_pi_est_mat.row(kk);
	double *my_pi_est = my_pi_est_eigen.data();
	
	for(long lltt = 0; lltt < PI_LEN; lltt++)
	{
	  if(my_pi_est[lltt] < thresh_vec[tttt])
	    my_pi_est[lltt] = 0;
	  else
	    my_pi_est[lltt] = 1;
	}
	// calculate p-values and error
	double correlation = Calculate_Correlation(my_pi_est, my_pi_true, (long)PI_LEN); //{long}
	if (correlation > 0)
	{
	  p_values(k,kk)=Calculate_Pvalue(my_pi_true, my_pi_est, (long)PI_LEN); //{long}
	  if (p_values(k,kk) < PVALUE_TOLE)
	  {
	    errors(k,kk)=(my_pi_true_eigen - my_pi_est_eigen).cwiseAbs().sum();
	  }
	  else
	  {
	    errors(k,kk)=0;
	  }
	}
	else
	{
	  p_values(k,kk)=-1;
	  errors(k,kk)=0;
	}
      }
    }
    VectorXd matched = errors.rowwise().sum();
    double nnz =0;
    for(long calc=0; calc <KTRUE; calc++)
    {
      if(matched(calc)>0)
	nnz++;
    }
    error_vec[tttt]=(double)errors.sum()/((double)PI_LEN*KTRUE);
    match_vec[tttt]=((double)nnz)/((double)KTRUE);
  }
  gettimeofday(&stop_timeval_error, NULL);  // measuring stop time for error calculation
  measure_stop_error = stop_timeval_error.tv_usec + (timestamp_t)stop_timeval_error.tv_sec * 1000000;
  measure_start_error = start_timeval_error.tv_usec + (timestamp_t)start_timeval_error.tv_sec * 1000000;
  time_error = (measure_stop_error - measure_start_error) / 1000000.0L;
  cout << "---------After error calculation------------"<<endl;
  printf("time taken for error calculation = %5.25e (Seconds)\n",time_error);
  
  furongprintVector(thresh_vec, NUM_THRESH, "thresh vector "); // outputs are printed
  furongprintVector(error_vec, NUM_THRESH, "error vector ");
  furongprintVector(match_vec, NUM_THRESH, "match vector ");
#endif
  cout << "Program over" << endl;    
  printf("\ntime taken for execution of the whole program = %5.25e (Seconds)\n", time_rd1 + time_pre + time_stpm + time_rd2 + time_post);
  return 0;
}
Exemple #3
0
std::pair<size_t, size_t> tune_params(
        const double* divs, size_t num_bags,
        const std::vector<label_type> &labels,
        const boost::ptr_vector<Kernel> &kernels,
        const std::vector<double> &c_vals,
        const svm_parameter &svm_params,
        size_t folds,
        size_t num_threads)
{
    typedef std::pair<size_t, size_t> config;

    size_t num_kernels = kernels.size();

    if (num_kernels == 0) {
        BOOST_THROW_EXCEPTION(std::domain_error(
                    "no kernels in the kernel group"));
    } else if (num_kernels == 1 && c_vals.size() == 1) {
        // only one option, we already know what's best
        return config(0, 0);
    }


    // want to be able to take sub-lists of kernels.
    // this is like c_array(), but constness is slightly different and
    // it works in old, old boosts.
    const Kernel * const * kern_array =
        reinterpret_cast<const Kernel* const*>(&kernels.begin().base()[0]);

    // how many threads are we using?
    num_threads = npdivs::get_num_threads(num_threads);
    if (num_threads > num_kernels)
        num_threads = num_kernels;

    if (num_threads == 1) {
        // don't actually make a new thread if it's just 1-threaded
        double score;
        return pick_rand(tune_params_single(divs, num_bags, labels,
                    kern_array, num_kernels, c_vals, svm_params, folds,
                    &score));
    }

    // grunt work to set up multithreading
    boost::ptr_vector< tune_params_worker<label_type> > workers;
    std::vector<boost::exception_ptr> errors(num_threads);
    boost::thread_group worker_threads;

    std::vector< std::vector<config> > results(num_threads);
    std::vector<double> scores(num_threads, 0);

    size_t kerns_per_thread = (size_t)
            std::ceil(double(num_kernels) / num_threads);
    size_t kern_start = 0;

    // give each thread a few kernels and get their most-accurate configs
    // TODO: better allocation algo
    for (size_t i = 0; i < num_threads; i++) {
        int n_kerns =
            (int)(std::min(kern_start+kerns_per_thread, num_kernels))
            - (int)(kern_start);

        if (n_kerns <= 0)
            break;

        workers.push_back(new tune_params_worker<label_type>(
                    divs, num_bags, labels,
                    kern_array + kern_start, n_kerns,
                    c_vals, svm_params, folds,
                    &results[i], &scores[i],
                    errors[i]
        ));

        worker_threads.create_thread(boost::ref(workers[i]));

        kern_start += kerns_per_thread;
    }
    worker_threads.join_all();
    for (size_t i = 0; i < num_threads; i++)
        if (errors[i])
            boost::rethrow_exception(errors[i]);

    // get all the best configs into one vector
    double best_score = *std::max_element(
            scores.begin(), scores.end());
    std::vector<config> best_configs;

    if (best_score == -std::numeric_limits<double>::infinity()) {
        FILE_LOG(logERROR) << "all kernels were terrible";
        BOOST_THROW_EXCEPTION(std::domain_error("all kernels were terrible"));
    }

    kern_start = 0;
    for (size_t i = 0; i < num_threads; i++) {
        if (scores[i] == best_score) {
            for (size_t j = 0; j < results[i].size(); j++) {
                config cfg = results[i][j];
                best_configs.push_back(
                        config(cfg.first + kern_start, cfg.second));
            }
        }
        kern_start += kerns_per_thread;
    }

    return pick_rand(best_configs);
}
VError VArchiveStream::Proceed()
{
	bool userAbort = false;
	VError result = VE_OK;

	//Iterate over our various list and check for duplicates

	/* ACI0077162, Jul 11th 2012, O.R.: add some calls to the progress callback here in order to
	allow user feedback, in case of lengthy process (e.g. many files/folders to add)*/
	
	if (fCallBack)
	{
		fCallBack(CB_OpenProgressUndetermined,0,0,userAbort);
	}
	
	VectorOfPaths::iterator folderIterator =  fFolderPathList.begin();
	VectorOfVString::iterator folderInfoIterator =  fFolderExtra.begin();
	xbox_assert(fFolderPathList.size() == fFolderExtra.size());

	for ( uLONG i = 0; i < fFolderPathList.size() && result == VE_OK && !userAbort; i++ )
	{
		const VFilePath& folderPath = fFolderPathList[i];//(*folderIterator);
		const VFilePath& sourceFolderPath = fSourceFolderForFolders[i];//(*folderIterator);
		const VString& folderExtraInfo = folderInfoIterator[i];//(*folderInfoIterator);
		VFolder currentFolder(folderPath);
		result = _AddOneFolder(currentFolder,sourceFolderPath,folderExtraInfo);
	}

	if ( fStream && result == VE_OK && !userAbort)
	{
		result = fStream->OpenWriting();
		if ( result == VE_OK )
		{
			sLONG8 offset;
			sLONG8 fileSize;
			sLONG8 byteCount;
			uLONG8 totalByteCount = 0;
			uLONG8 partialByteCount = 0;

			/* put the backup file signature */
			fStream->PutLong('FPBK');
			/* put the current version of the file */
			fStream->PutByte(4);
			/* put the number of file stored in this archive */
			sLONG8 storedObjCount = (sLONG8)fFileList.size();
			storedObjCount += (sLONG8)fFileDescList.size();

			fStream->PutLong8(storedObjCount /*(sLONG8)(fFileList.size()+fFileDescList.size())*/);
			
			/* put the header of the file listing */
			fStream->PutLong('LIST');
			
			for ( uLONG i = 0; i < fFileDescList.size() && result == VE_OK && !userAbort; i++ )
			{
				result = _WriteCatalog(fFileDescList[i]->GetParentVFile(),fSourceFolderForFileDescs[i],fFileDescExtra[i],totalByteCount);
				if ( fCallBack )
				{
					fCallBack(CB_UpdateProgress,0,0,userAbort);
				}
			}

			for ( uLONG i = 0; i < fFileList.size() && result == VE_OK && !userAbort; i++ )
			{
				result = _WriteCatalog(fFileList[i],fSourceFolderForFiles[i],fFileExtra[i],totalByteCount);
				if ( fCallBack )
				{
					fCallBack(CB_UpdateProgress,0,0,userAbort);
				}
			}

			//ACI0077162, Jul 11th 2012, O.R.: Close unbound session and open a bounded one for file processing
			if ( fCallBack )
			{
				fCallBack(CB_CloseProgress,0,0,userAbort);
				fCallBack(CB_OpenProgress,partialByteCount,totalByteCount,userAbort);
			}

			VSize bufferSize = 1024*1024;
			char *buffer = new char[bufferSize];

			/* writing file descriptor that we have to the archive files */
			/* nota : filedesc passed to the archivestream is considered as data fork */
			//ACI0077162, Jul 11th 2012, O.R.: _WriteFile() in charge of calling progress CB to give reactivity to upper layers
			for( VectorOfVFileDesc::iterator i = fFileDescList.begin() ; result == VE_OK && i != fFileDescList.end() && !userAbort ; ++i)
			{
				StErrorContextInstaller errors( false);
				result = fStream->PutLong('Fdat');
				if ( result == VE_OK )
				{
					result = (*i)->SetPos(0);
					if ( result == VE_OK )
						result = _WriteFile( (*i), buffer, bufferSize, partialByteCount, totalByteCount );
				}

				if ( result == VE_OK )
					result = fStream->PutLong('Frez');

				if ( result == VE_OK )
					result = fStream->PutLong8(0);

			}

			for( VectorOfVFile::iterator i = fFileList.begin() ; result == VE_OK && i != fFileList.end() && !userAbort ; ++i )
			{
				StErrorContextInstaller errors( false);
				VFileDesc *fileDesc = NULL;

				result = fStream->PutLong('Fdat');
				if ( result == VE_OK )
				{
					result = (*i)->Open(FA_READ,&fileDesc);
					if ( fileDesc )
					{
						result = _WriteFile( fileDesc, buffer, bufferSize, partialByteCount, totalByteCount );
						delete fileDesc;
						fileDesc = NULL;
					}
					else
					{
						result = fStream->PutLong8(0);
					}
				}

				if ( result == VE_OK )
					result = fStream->PutLong('Frez');

				if ( result == VE_OK )
				{
#if VERSIONMAC
					result = (*i)->Open(FA_READ,&fileDesc,FO_OpenResourceFork);
					if ( fileDesc )
					{
						result = _WriteFile( fileDesc, buffer, bufferSize, partialByteCount, totalByteCount );
						delete fileDesc;
						fileDesc = NULL;
					}
					else
#endif
					{
						result = fStream->PutLong8(0);
					}
				}
			}
			delete[] buffer;

			

			fStream->CloseWriting();
		}
	}
	//ACI0077162, Jul 11th 2012, O.R.: close remaining session (either unbounded one for check files or bounded one for processing files)
	if ( fCallBack )
		fCallBack(CB_CloseProgress,0,0,userAbort);
	if (userAbort)
	{
		result = XBOX::VE_STREAM_USER_ABORTED;
	}

	return result;
}
bool CEclDirectEx::onRunEclEx(IEspContext &context, IEspRunEclExRequest & req, IEspRunEclExResponse & resp)
{
    if (!context.validateFeatureAccess(ECLDIRECT_ACCESS, SecAccess_Full, false))
        throw MakeStringException(-1, "EclDirect access permission denied.");

    const char* eclText = req.getEclText();
    if (!eclText || !*eclText)
    {
        resp.setResults("<Exception><Source>ESP</Source><Message>No Ecl Text provided</Message></Exception>");
        return true;
    }

    StringBuffer user;
    if (!context.getUserID(user).length())
        user.append(req.getUserName());

    Owned <IWorkUnitFactory> factory = getWorkUnitFactory(context.querySecManager(), context.queryUser());
    Owned <IWorkUnit> workunit;
    if (!user.length())
        workunit.setown(factory->createWorkUnit(NULL, "ECL-Direct", ""));
    else
    {
        workunit.setown(factory->createWorkUnit(NULL, "ECL-Direct", user.str()));
        workunit->setUser(user.str());
    }

    Owned<IWUQuery> query = workunit->updateQuery();
    query->setQueryText(eclText);
    query.clear();

    const char* cluster = req.getCluster();
    if (!cluster || !*cluster || !stricmp(cluster, "default"))
        cluster = defaultCluster.str();

    if (!cluster || !*cluster)
        throw MakeStringException(-1, "No Cluster Specified");

    if (!isValidCluster(cluster))
        throw MakeStringException(-1, "Invalid TargetCluster %s Specified", cluster);

    workunit->setClusterName(cluster);

    const char* snapshot = req.getSnapshot();
    if (snapshot && *snapshot)
        workunit->setSnapshot(snapshot);

    if (req.getResultLimit())
        workunit->setResultLimit(req.getResultLimit());

    // Execute it
    SCMStringBuffer wuid;
    workunit->getWuid(wuid);
    workunit->setAction(WUActionRun);
    workunit->setState(WUStateSubmitted);
    workunit.clear();

    resp.setWuid(wuid.str());

    submitWorkUnit(wuid.str(), context.querySecManager(), context.queryUser());

    if (!waitForWorkUnitToComplete(wuid.str(), (req.getWait_isNull()) ? defaultWait : req.getWait()))
    {
        StringBuffer result;
        result.appendf("<Exception><Source>ESP</Source><Message>Timed out waiting for job to complete: %s</Message></Exception>", wuid.str());
        resp.setResults(result.str());
        return true;
    }

    if (!deleteWorkunits && context.queryRequestParameters()->hasProp("redirect"))
    {
        StringBuffer url("/WsWorkunits/WUInfo?Wuid=");
        resp.setRedirectUrl(url.append(wuid).str());
        return true;
    }

    Owned<IConstWorkUnit> cw = factory->openWorkUnit(wuid.str(), false);
    EclDirectWUExceptions errors(*cw);
    resp.setErrors(errors);

    if (req.getIncludeResults())
    {
        StringBuffer results;
        CRunEclExFormat outputFormat = req.getFormat();
        Owned<IWuWebView> web = createWuWebView(wuid.str(), NULL, NULL, getCFD(), true);
        if (!web)
            results.appendf("<Exception><Source>ESP</Source><Message>Failed loading result workunit %s</Message></Exception>", wuid.str());
        else if (outputFormat == CRunEclExFormat_Table)
        {
            StringBuffer xsltfile(getCFD());
            web->applyResultsXSLT(xsltfile.append("xslt/wsecl3_result.xslt").str(), results);
        }
        else
        {
            unsigned xmlflags = 0;
            if (outputFormat != CRunEclExFormat_ExtendedXml)
                xmlflags |= WWV_OMIT_SCHEMAS;
            if (context.queryRequestParameters()->hasProp("display_xslt"))
                xmlflags |= WWV_USE_DISPLAY_XSLT;
            else
                xmlflags |= WWV_OMIT_XML_DECLARATION;
            web->expandResults(results, xmlflags);
        }
        resp.setResults(results.str());
    }

    if (req.getIncludeGraphs())
    {
        Owned<IConstWUGraphIterator> it = &cw->getGraphs(GraphTypeAny);
        StringBuffer xgmml("<Graphs>");
        SCMStringBuffer s;
        ForEach(*it)
            xgmml.append(it->query().getXGMML(s, true).str());
        xgmml.append("</Graphs>");
        resp.setGraphsXGMML(xgmml.str());
    }

    if (deleteWorkunits)
        deleteEclDirectWorkunit(factory, wuid.str());

    return true;
}
Exemple #6
0
int main(int argc, char *argv[])
{
    char *user_config_dir = get_user_config_dir();
    int config_err = 0;

    f_loadfromfile = 1;
    int f_flag = 0;
    int i = 0;
    int f_use_ipv4 = 0;

    /* Make sure all written files are read/writeable only by the current user. */
    umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);

    for (i = 0; i < argc; ++i) {
        if (argv[i] == NULL)
            break;
        else if (argv[i][0] == '-') {
            if (argv[i][1] == 'f') {
                if (argv[i + 1] != NULL)
                    DATA_FILE = strdup(argv[i + 1]);
                else
                    f_flag = -1;
            } else if (argv[i][1] == 'n') {
                f_loadfromfile = 0;
            } else if (argv[i][1] == '4') {
                f_use_ipv4 = 1;
            }
        }
    }

    config_err = create_user_config_dir(user_config_dir);
    if (DATA_FILE == NULL ) {
        if (config_err) {
            DATA_FILE = strdup("data");
        } else {
            DATA_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("data") + 1);
            if (DATA_FILE != NULL) {
                strcpy(DATA_FILE, user_config_dir);
                strcat(DATA_FILE, CONFIGDIR);
                strcat(DATA_FILE, "data");     
            } else {
                endwin();
                fprintf(stderr, "malloc() failed. Aborting...\n");
                exit(EXIT_FAILURE);
            }
        }
    }

    free(user_config_dir);

    init_term();
    Tox *m = init_tox(f_use_ipv4);

    if (m == NULL) {
        endwin();
        fprintf(stderr, "Failed to initialize network. Aborting...\n");
        exit(EXIT_FAILURE);
    }

    prompt = init_windows(m);

    /* create new thread for ncurses stuff */
    if (pthread_mutex_init(&Winthread.lock, NULL) != 0) {
        endwin();
        fprintf(stderr, "Mutex init failed. Aborting...\n");
        exit(EXIT_FAILURE);
    }

    if (pthread_create(&Winthread.tid, NULL, thread_winref, (void *) m) != 0) {
        endwin();
        fprintf(stderr, "Thread creation failed. Aborting...\n");
        exit(EXIT_FAILURE);
    }

    uint8_t *msg;

#ifdef _SUPPORT_AUDIO

    av = init_audio(prompt, m);

    if ( errors() == NoError )
        msg = "Audio started with no problems.";
    else /* Get error code and stuff */
        msg = "Error starting audio!";

    line_info_add(prompt, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);

#endif /* _SUPPORT_AUDIO */

    if (f_loadfromfile)
        load_data(m, DATA_FILE);

    if (f_flag == -1) {
        msg = "You passed '-f' without giving an argument. Defaulting to 'data' for a keyfile...";
        line_info_add(prompt, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
    }

    if (config_err) {
        msg = "Unable to determine configuration directory. Defaulting to 'data' for a keyfile...";
        line_info_add(prompt, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
    }

    sort_friendlist_index();
    prompt_init_statusbar(prompt, m);

    while (true) {
        update_unix_time();
        do_toxic(m, prompt);
        usleep(10000);
    }
        
    return 0;
}
Exemple #7
0
int CComm::init(const char *l,int baud)
{
  h = CreateFile(l,GENERIC_READ|GENERIC_WRITE,0,NULL,
        OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);

  if (h == INVALID_HANDLE_VALUE) {
    h=NULL;
    throw errors("port not opened");
  }

  COMMTIMEOUTS cto = { 2, 1, 1, 0, 0 };
  DCB dcb;
  if(!SetCommTimeouts(h,&cto)){
    CloseHandle(h);
    h=NULL;
    throw errors("SetCommTimeouts error");
  }

  memset(&dcb,0,sizeof(dcb));
  dcb.DCBlength = sizeof(dcb);
  dcb.BaudRate = baud;
  dcb.fBinary = 1;
  dcb.fDtrControl = DTR_CONTROL_ENABLE;

  dcb.fOutxCtsFlow = 1;
  dcb.fRtsControl = DTR_CONTROL_HANDSHAKE;

  dcb.Parity = NOPARITY;
  dcb.StopBits = ONESTOPBIT;
  dcb.ByteSize = 8;

  if(!SetCommState(h,&dcb)){
    CloseHandle(h);
    h=NULL;
    throw errors("SetCommState error");
  }

  ZeroMemory(&ovr,sizeof(ovr));
  ovr.hEvent = CreateEvent(NULL,FALSE,FALSE,NULL);
  if(ovr.hEvent == INVALID_HANDLE_VALUE){
    CloseHandle(h);
    h=NULL;
    throw errors("CreateEvent error");
  }

  ZeroMemory(&ovw,sizeof(ovw));
  ovw.hEvent = CreateEvent(NULL,FALSE,FALSE,NULL);
  if(ovw.hEvent == INVALID_HANDLE_VALUE){
    CloseHandle(ovr.hEvent);CloseHandle(h);h=NULL;
    throw errors("CreateEvent error");
  }

  if(!SetCommMask(h,EV_RXCHAR|EV_TXEMPTY|EV_RLSD|EV_DSR|EV_CTS)){
    CloseHandle(h);CloseHandle(ovr.hEvent);h=NULL;
    throw errors("SetCommMask error");
  }

  txenable = CreateEvent(NULL,FALSE,TRUE,NULL);
  if (txenable == NULL){
    CloseHandle(h);CloseHandle(ovr.hEvent);h=NULL;
    throw errors("CreateEvent error");
  }

  SetEvent(txenable);

  unsigned int tid;
  threadalive = (HANDLE)_beginthreadex(NULL,0,mthread,this,0,&tid);
  if( threadalive == NULL ) {
    CloseHandle(h);CloseHandle(ovr.hEvent);h=NULL;
    throw errors("_beginthread error");
  }

  return 0;
}
Exemple #8
0
int main(void)
{
	char * name=loadFile("name.txt");
	char * buf= init(name); //get starting message
	int sockfd, numbytes;
	struct addrinfo hints, *servinfo, *p;
	int rv;
	char s[INET6_ADDRSTRLEN];

	memset(&hints, 0, sizeof hints);
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;

	if ((rv = getaddrinfo(HOST, PORT, &hints, &servinfo)) != 0) {
		fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
		return 1;
	}

	// loop through all the results and connect to the first we can
	for(p = servinfo; p != NULL; p = p->ai_next) {
		if ((sockfd = socket(p->ai_family, p->ai_socktype,
				p->ai_protocol)) == -1) {
			perror("client: socket");
			continue;
		}

		if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
			close(sockfd);
			perror("client: connect");
			continue;
		}

		break;
	}

	if (p == NULL) {
		fprintf(stderr, "client: failed to connect\n");
		return 2;
	}

	inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr),
			s, sizeof s);

	freeaddrinfo(servinfo); // all done with this structure

	send(sockfd, buf, strlen(buf)+1, 0);

	char str[MAXDATASIZE];
	if ((numbytes = recv(sockfd, str, MAXDATASIZE-1, 0)) == -1) {
	    perror("recv");
	    exit(1);
	}

	str[numbytes] = '\0';

	//Determine my player token
	char * temptok = strstr(strstr(strstr(str, "playerToken"), ":"), "\"")+1;
	char * tempend = strstr(temptok, "\"");
	tempend[0]=0;
	char * token=malloc(tempend-temptok+1);
	strcpy(token, temptok);
	tempend[0]='\"';
	int res;

	while(1){
		res=getTurn(buf, token, responseCode(str), resources(str), ships(str), errors(str), hits(str), actionResponses(str), pings(str));
		if(res==1) break;
		if(res==2) {
			send(sockfd, buf, strlen(buf)+1, 0);
		}

		if ((numbytes = recv(sockfd, str, MAXDATASIZE-1, 0)) == -1) {
		    perror("recv");
		    exit(1);
		}

		str[numbytes] = '\0';
	}

	close(sockfd);
	free(buf);
	free(token);
	free(name);

	return 0;
}
Exemple #9
0
void ProjectImpl::dumpErrors() {
    errors()->dumpErrors();
}
Exemple #10
0
Driver::Driver(const char *dname) :
keyfile()
{
    Script *img = NULL, *def = NULL;
    char dirpath[256];
    size_t len;
    dir_t dir;
    const char *cp = env("config");
    const char *dpath = env("configs");

    shell::debug(2, "reloading config from %s", cp);
    load(cp);

    server::load();

    snprintf(dirpath, sizeof(dirpath), "%s/%s" CONFIG_EXTENSION, dpath, dname);
    if(fsys::is_file(dirpath)) {
        shell::debug(2, "reloading registrations from %s", dirpath);
        regfile.load(dirpath);
    }

    image_services = NULL;
    activations = NULL;

    String::set(dirpath, sizeof(dirpath), env("definitions"));
    dir.open(dirpath);
    if(!is(dir)) {
        shell::log(shell::ERR, "cannot compile definitions from %s", dirpath);
        goto noscr;
    }

    len = strlen(dirpath);
    shell::debug(2, "compiling definitions from %s", dirpath);
    dirpath[len++] = '/';

    while(is(dir) && dir.read(dirpath + len, sizeof(dirpath) - len) > 0) {
        char *ep = strrchr(dirpath + len, '.');
        if(!ep)
            continue;
        if(!eq(ep, ".bcs"))
            continue;
        shell::log(shell::INFO, "compiling %s", dirpath + len);
        def = Script::compile(def, dirpath, NULL);
    }
    dir.close();

noscr:
    String::set(dirpath, sizeof(dirpath), env("scripts"));
    dir.open(dirpath);
    if(!is(dir)) {
        shell::log(shell::ERR, "cannot compile from %s", dirpath);
        return;
    }

    len = strlen(dirpath);
    shell::debug(2, "compiling services from %s", dirpath);
    dirpath[len++] = '/';

    image_definitions = def;

    while(is(dir) && dir.read(dirpath + len, sizeof(dirpath) - len) > 0) {
        char *ep = strrchr(dirpath + len, '.');
        void *mp;
        if(!ep)
            continue;
        if(!eq(ep, ".bcs"))
            continue;
        shell::log(shell::INFO, "compiling %s", dirpath + len);
        img = Script::compile(NULL, dirpath, *image_definitions);
        if(!img) {
            shell::log(shell::ERR, "%s: failed", dirpath + len);
            continue;
        }

        if(errors(img)) {
            delete img;
            continue;
        }

        *(ep++) = 0;
        mp = zalloc(sizeof(image));
        new(mp) image(img, &image_services, dup(dirpath + len));
    }
    dir.close();
}
Exemple #11
0
/***********************************************************************//**
 * @brief Run all tests in test suite
 *
 * @return True if all tests were successful, false otherwise.
 *
 * Executes all test functions that have been appended to the test suite.
 * For each test function a test case is added to the test suite.
 ***************************************************************************/
bool GTestSuite::run(void)
{
    // Setup the test functions. This is a pure virtual function that needs
    // to be implemented in the derived class. It sets the function
    // pointers and function names for all test functions that should be
    // executed.
    set();

    // Initialise success flag
    bool success = true;

    // Loop over all functions in suite
    for (m_index = 0; m_index < m_functions.size(); ++m_index) {

        // Continue only if function is valid
        if (m_functions[m_index] != NULL) {

            // Save the number of errors and failures before test
            // execution. We use this after the test to see if
            // any failures occured.
            int old_errors   = errors();
            int old_failures = failures();

            // Log the name of the test
            std::cout << m_names[m_index] << ": ";

            // Create a test of error type for function testing
            GTestCase* test = new GTestCase(GTestCase::ERROR_TEST, m_names[m_index]);

            // Add test case to test suite
            m_tests.push_back(test);

            // Set start time
            #ifdef _OPENMP
            double t_start = omp_get_wtime();
            #else
            clock_t t_start = clock();
            #endif

            // Execute test function
            try {
                (this->*(m_functions[m_index]))();
            }
            catch (std::exception& e) {

                // Signal that test did not succeed
                test->has_passed(false);

                // Set test message to exception message
                test->message(e.what());

                // Set type as class name
                test->type(typeid(e).name());

            }
            catch (...)
            {
                // For other exceptions
                test->has_passed(false);
                test->message("Non-standard C++ exception thrown");
            }

            // Compute elapsed time
            #ifdef _OPENMP
            double t_elapse = omp_get_wtime()-t_start;
            #else
            double t_elapse = (double)(clock() - t_start) / (double)CLOCKS_PER_SEC;
            #endif

            // Set test duration
            test->duration(t_elapse);

            // Increment number of errors if the test did not pass
            if (!test->has_passed()) {
                m_errors++;
            }

            // Log the result (".","F" or, "E")
            std::cout << test->print();

            // Log if there are errors or failures
            if ((m_errors == old_errors && m_failures == old_failures)) {
                std::cout << " ok" << std::endl;
            }
            else {
                std::cout << " NOK" << std::endl;
                success = false;
            }

        } // endif: test case has a function pointer

    } // endfor: looped over tests on the stack

    // Reset index
    m_index = 0;

    // Return success flag
    return success;
}
void PointByPointVCorrection::exec()
{
  // Get the input workspace and output workspace
  MatrixWorkspace_const_sptr inputWS1 = getProperty("InputW1");
  MatrixWorkspace_const_sptr inputWS2 = getProperty("InputW2");
  MatrixWorkspace_sptr outputWS= getProperty("OutputWorkspace");

  // Check that everything is OK.
  check_validity(inputWS1,inputWS2,outputWS);

  // Now do the normalisation
  const int size=static_cast<int>(inputWS1->readX(0).size());
  const int nHist=static_cast<int>(inputWS1->getNumberHistograms());
  Progress prog(this,0.0,1.0,nHist);

  PARALLEL_FOR3(inputWS1,inputWS2,outputWS)
  for (int i=0;i<nHist;i++) //Looping on all histograms
  {
		PARALLEL_START_INTERUPT_REGION

    const MantidVec& X=inputWS1->readX(i);
    outputWS->setX( i, inputWS1->refX(i) );

    const MantidVec& Y1=inputWS1->readY(i);
    const MantidVec& Y2=inputWS2->readY(i);
    const MantidVec& E1=inputWS1->readE(i);
    const MantidVec& E2=inputWS2->readE(i);
    MantidVec& resultY=outputWS->dataY(i);
    MantidVec& resultE=outputWS->dataE(i);


    // Work on the Y data
    MantidVec binwidths(size); //MantidVec for bin widths
    MantidVec errors(size-1); //MantidVec for temporary errors
    std::adjacent_difference(X.begin(),X.end(),binwidths.begin()); //Calculate the binwidths
    std::transform(binwidths.begin()+1,binwidths.end(),Y2.begin(),resultY.begin(),VectorHelper::DividesNonNull<double>());
    std::transform(Y1.begin(),Y1.end(),resultY.begin(),resultY.begin(),std::multiplies<double>()); // Now resultY contains the A_i=s_i/v_i*Dlam_i

    // Calculate the errors squared related to A_i at this point
    for (int j=0;j<size-1;j++)
    {
      double r=0.0;
      if (std::abs(Y1[j])>1e-7)
        r+=std::pow(E1[j]/Y1[j],2);
      if (std::abs(Y2[j])>1e-7)
        r+=std::pow(E2[j]/Y2[j],2);
      errors[j]=r; // This are the errors^2 of S_i/v_i*Dlam_i
      if (errors[j]>DBL_MAX || errors[j]<-DBL_MAX)
        errors[j]=0;
    }

    // Calculate the normaliser
    double factor1=std::accumulate(Y1.begin(),Y1.end(),0.0);
    double factor2=std::accumulate(resultY.begin(),resultY.end(),0.0);
    double factor=factor1/factor2;

    // Now propagate the error bars due to the normaliser
    double error2_factor1=std::inner_product(E1.begin(),E1.end(),E1.begin(),0.0);
    double error2_factor2=0;

    for (int j=0;j<size-1;j++)
    {
      double test=std::abs(std::pow(resultY[j],2));
      if (test>DBL_MAX)
        test=0;
      error2_factor2+=errors[j]*test/factor2/factor2;
    }
    double error2_factor=(error2_factor1/factor1/factor1+error2_factor2);
    
    // Calculate the normalized Y values
    // NOTE: Previously, we had been using std::transform with std::bind2nd(std::multiplies<double>(),factor)
    //       here, but that seemed to have strange effects in Windows Debug builds which caused the unit tests
    //       to sometimes fail.  Maybe this is some compiler bug to do with using bind2nd within the parrallel macros.
    for( auto rY = resultY.begin(); rY != resultY.end(); ++rY )
    {
      *rY *= factor; // Now result is s_i/v_i*Dlam_i*(sum_i s_i)/(sum_i S_i/v_i*Dlam_i)
    }
    
    //Finally get the normalized errors
    for (int j=0;j<size-1;j++)
      resultE[j]=resultY[j]*sqrt(errors[j]+error2_factor);

    // Check that any masking matches, print a warning if not
    check_masks(inputWS1,inputWS2,i);

    prog.report();
		PARALLEL_END_INTERUPT_REGION
  }
	PARALLEL_CHECK_INTERUPT_REGION

  outputWS->setYUnitLabel("Counts normalised to a vanadium");
  outputWS->isDistribution(false);
}
Exemple #13
0
void TCP::open(Error& error, const Ascii& sHost, UInt16 iPort)
{
	m_socket = INVALID_SOCKET;

	ErrorList errors(error);

	Ascii sPort = Ascii::format("%hu", iPort);

	addrinfo getaddrinfo_hints;
	Memory::zero(&getaddrinfo_hints, sizeof(getaddrinfo_hints));
	getaddrinfo_hints.ai_family = AF_INET;
	getaddrinfo_hints.ai_socktype = SOCK_STREAM;
	getaddrinfo_hints.ai_protocol = IPPROTO_TCP;

	addrinfo* p_getaddrinfo_response_list = NULL;

	int i_getaddrinfo_result =
		getaddrinfo(sHost.ptr(), sPort.ptr(), &getaddrinfo_hints, &p_getaddrinfo_response_list);

	if (i_getaddrinfo_result != 0)
	{
		int i_getaddrinfo_error = WSAGetLastError();
		UTF16 s_getaddrinfo_error = WindowsError::codeToString(i_getaddrinfo_error);
		errors.record(UTF16::format(L"getaddrinfo: %s", s_getaddrinfo_error.ptr()));
		return;
	}

	UTF16 s_socket_error_combined = L"";
	for (addrinfo* p_getaddrinfo_response = p_getaddrinfo_response_list;
		p_getaddrinfo_response != NULL;
		p_getaddrinfo_response = p_getaddrinfo_response->ai_next)
	{
		m_socket = socket
			( p_getaddrinfo_response->ai_family
			, p_getaddrinfo_response->ai_socktype
			, p_getaddrinfo_response->ai_protocol);

		if (m_socket == INVALID_SOCKET)
		{
			int i_socket_error = WSAGetLastError();
			UTF16 s_socket_error = WindowsError::codeToString(i_socket_error);
			errors.record(UTF16::format(L"socket: %s", s_socket_error.ptr()));
			continue;
		}

		int i_connect_result = connect
			( m_socket
			, p_getaddrinfo_response->ai_addr
			, (int) p_getaddrinfo_response->ai_addrlen);

		if (i_connect_result != 0)
		{
			int i_connect_error = WSAGetLastError();
			UTF16 s_connect_error = WindowsError::codeToString(i_connect_error);
			errors.record(UTF16::format(L"connect: %s", s_connect_error.ptr()));

			closesocket(m_socket);
			m_socket = INVALID_SOCKET;

			continue;
		}

		break;
	}

	freeaddrinfo(p_getaddrinfo_response_list);
	
	if (m_socket == INVALID_SOCKET)
	{
		errors.record(L"Invalid socket");
		return;
	}
}
Exemple #14
0
/******************************************************************************
* Send the email message specified in an event.
* Reply = 1 if the message was sent - 'errmsgs' may contain copy error messages.
*       = 0 if the message is queued for sending.
*       = -1 if the message was not sent - 'errmsgs' contains the error messages.
*/
int KAMail::send(JobData& jobdata, QStringList& errmsgs)
{
    QString err;
    KPIMIdentities::Identity identity;
    if (!jobdata.event.emailFromId())
        jobdata.from = Preferences::emailAddress();
    else
    {
        identity = Identities::identityManager()->identityForUoid(jobdata.event.emailFromId());
        if (identity.isNull())
        {
            kError() << "Identity" << jobdata.event.emailFromId() << "not found";
            errmsgs = errors(i18nc("@info", "Invalid 'From' email address.<nl/>Email identity <resource>%1</resource> not found", jobdata.event.emailFromId()));
            return -1;
        }
        if (identity.emailAddr().isEmpty())
        {
            kError() << "Identity" << identity.identityName() << "uoid" << identity.uoid() << ": no email address";
            errmsgs = errors(i18nc("@info", "Invalid 'From' email address.<nl/>Email identity <resource>%1</resource> has no email address", identity.identityName()));
            return -1;
        }
        jobdata.from = identity.fullEmailAddr();
    }
    if (jobdata.from.isEmpty())
    {
        switch (Preferences::emailFrom())
        {
#ifdef KMAIL_SUPPORTED
        case Preferences::MAIL_FROM_KMAIL:
            errmsgs = errors(i18nc("@info", "<para>No 'From' email address is configured (no default email identity found)</para>"
                                   "<para>Please set it in <application>KMail</application> or in the <application>KAlarm</application> Configuration dialog.</para>"));
            break;
#endif
        case Preferences::MAIL_FROM_SYS_SETTINGS:
            errmsgs = errors(i18nc("@info", "<para>No 'From' email address is configured.</para>"
                                   "<para>Please set it in the KDE System Settings or in the <application>KAlarm</application> Configuration dialog.</para>"));
            break;
        case Preferences::MAIL_FROM_ADDR:
        default:
            errmsgs = errors(i18nc("@info", "<para>No 'From' email address is configured.</para>"
                                   "<para>Please set it in the <application>KAlarm</application> Configuration dialog.</para>"));
            break;
        }
        return -1;
    }
    jobdata.bcc  = (jobdata.event.emailBcc() ? Preferences::emailBccAddress() : QString());
    kDebug() << "To:" << jobdata.event.emailAddresses(",")
             << endl << "Subject:" << jobdata.event.emailSubject();

    MailTransport::TransportManager* manager = MailTransport::TransportManager::self();
    MailTransport::Transport* transport = 0;
    if (Preferences::emailClient() == Preferences::sendmail)
    {
        kDebug() << "Sending via sendmail";
        const QList<MailTransport::Transport*> transports = manager->transports();
        for (int i = 0, count = transports.count();  i < count;  ++i)
        {
            if (transports[i]->type() == MailTransport::Transport::EnumType::Sendmail)
            {
                // Use the first sendmail transport found
                transport = transports[i];
                break;
            }
        }
        if (!transport)
        {
            QString command = KStandardDirs::findExe(QLatin1String("sendmail"),
                              QLatin1String("/sbin:/usr/sbin:/usr/lib"));
            transport = manager->createTransport();
            transport->setName(QLatin1String("sendmail"));
            transport->setType(MailTransport::Transport::EnumType::Sendmail);
            transport->setHost(command);
            transport->setRequiresAuthentication(false);
            transport->setStorePassword(false);
            manager->addTransport(transport);
            transport->writeConfig();
            kDebug() << "Creating sendmail transport, id=" << transport->id();
        }
    }
    else
    {
        kDebug() << "Sending via KDE";
        transport = manager->transportByName(identity.transport(), true);
        if (!transport)
        {
            kError() << "No mail transport found for identity" << identity.identityName() << "uoid" << identity.uoid();
            errmsgs = errors(i18nc("@info", "No mail transport configured for email identity <resource>%1</resource>", identity.identityName()));
            return -1;
        }
    }
    kDebug() << "Using transport" << transport->name() << ", id=" << transport->id();
    MailTransport::TransportJob* mailjob = manager->createTransportJob(transport->id());
    if (!mailjob)
    {
        kError() << "Failed to create mail transport job for identity" << identity.identityName() << "uoid" << identity.uoid();
        errmsgs = errors(i18nc("@info", "Unable to create mail transport job"));
        return -1;
    }
    KMime::Message message;
    initHeaders(message, jobdata);
    err = appendBodyAttachments(message, jobdata);
    if (!err.isNull())
    {
        kError() << "Error compiling message:" << err;
        errmsgs = errors(err);
        return -1;
    }
    mailjob->setSender(KPIMUtils::extractEmailAddress(jobdata.from));
    mailjob->setTo(jobdata.event.emailPureAddresses());
    if (!jobdata.bcc.isEmpty())
        mailjob->setBcc(QStringList(KPIMUtils::extractEmailAddress(jobdata.bcc)));
    mailjob->setData(message.encodedContent());
    mJobs.enqueue(mailjob);
    mJobData.enqueue(jobdata);
    if (mJobs.count() == 1)
    {
        // There are no jobs already active or queued, so send now
        connect(mailjob, SIGNAL(result(KJob*)), instance(), SLOT(slotEmailSent(KJob*)));
        mailjob->start();
    }
    return 0;
}
Exemple #15
0
arma::vec DIIS::get_w_diis_wrk(const arma::mat & errs) const {
  // Size of LA problem
  int N=(int) errs.n_cols;
  
  // DIIS weights
  arma::vec sol(N);
  sol.zeros();
  
  if(c1diis) {
    // Original, Pulay's DIIS (C1-DIIS)
    
    // Array holding the errors
    arma::mat B(N+1,N+1);
    B.zeros();
    // Compute errors
    for(int i=0;i<N;i++)
      for(int j=0;j<N;j++) {
	B(i,j)=arma::dot(errs.col(i),errs.col(j));
      }
    // Fill in the rest of B
    for(int i=0;i<N;i++) {
      B(i,N)=-1.0;
      B(N,i)=-1.0;
    }
    
    // RHS vector
    arma::vec A(N+1);
    A.zeros();
    A(N)=-1.0;
    
    // Solve B*X = A
    arma::vec X;
    bool succ;
    
    succ=arma::solve(X,B,A);
    
    if(succ) {
      // Solution is (last element of X is DIIS error)
      sol=X.subvec(0,N-1);
      
      // Check that weights are within tolerance
      if(arma::max(arma::abs(sol))>=MAXWEIGHT) {
	printf("Large coefficient produced by DIIS. Reducing to %i matrices.\n",N-1);
	arma::vec w0=get_w_diis_wrk(errs.submat(1,1,errs.n_rows-1,errs.n_cols-1));
	
	// Helper vector
	arma::vec w(N);
	w.zeros();
	w.subvec(w.n_elem-w0.n_elem,w.n_elem-1)=w0;
	return w;
      }
    }
    
    if(!succ) {
      // Failed to invert matrix. Use the two last iterations instead.
      printf("C1-DIIS was not succesful, mixing matrices instead.\n");
      sol.zeros();
      sol(0)=0.5;
      sol(1)=0.5;
    }
  } else {
    // C2-DIIS

    // Array holding the errors
    arma::mat B(N,N);
    B.zeros();
    // Compute errors
    for(int i=0;i<N;i++)
      for(int j=0;j<N;j++) {
	B(i,j)=arma::dot(errs.col(i),errs.col(j));
      }

    // Solve eigenvectors of B
    arma::mat Q;
    arma::vec lambda;
    eig_sym_ordered(lambda,Q,B);

    // Normalize weights
    for(int i=0;i<N;i++) {
      Q.col(i)/=arma::sum(Q.col(i));
    }

    // Choose solution by picking out solution with smallest error
    arma::vec errors(N);
    arma::mat eQ=errs*Q;
    // The weighted error is
    for(int i=0;i<N;i++) {
      errors(i)=arma::norm(eQ.col(i),2);
    }

    // Find minimal error
    double mine=DBL_MAX;
    int minloc=-1;
    for(int i=0;i<N;i++) {
      if(errors[i]<mine) {
	// Check weights
	bool ok=arma::max(arma::abs(Q.col(i)))<MAXWEIGHT;
	if(ok) {
	  mine=errors(i);
	  minloc=i;
	}
      }
    }

    if(minloc!=-1) {
      // Solution is
      sol=Q.col(minloc);
    } else {
      printf("C2-DIIS did not find a suitable solution. Mixing matrices instead.\n");
      
      sol.zeros();
      sol(0)=0.5;
      sol(1)=0.5;
    }
  }
  
  return sol;
}
Exemple #16
0
int main(int argc, char *argv[])
{
    auto log = std::unique_ptr<sammy::log>(new sammy::log());
    log->info("Starting server.");

    unsigned int request_id = 1;

    addrinfo* res = create_addrinfo();
    int sockfd = create_socket(res);
    
    set_socket_reusable(sockfd);
    set_socket_bind(sockfd, res);
    set_socket_non_blocking(sockfd);
    set_socket_listen(sockfd);
    
    int efd = create_epoll();

    set_epoll_interface(efd, sockfd);
    
    // Init the domain config loading
    auto domain_storage = std::make_shared<sammy::domain_storage>();
    if(domain_storage->errors())
    {
        log->error("Error loading config.");
        exit(1);
    }

    // Init cache
    auto cache = std::make_shared<sammy::cache_storage>();

    // Init events
    epoll_event* events = new epoll_event[MAX_EPOLL_EVENTS];

    sammy::thread::pool thread_pool;
    
    std::map<int, std::string> client_ip_address;

    log->info("Server started");

    while( true )
    {
        const int event_count = epoll_wait(efd, events, MAX_EPOLL_EVENTS, -1);

        for(int i = 0; i < event_count; ++i)
        {
            const epoll_event& event = events[i];
            
            if( (event.events & EPOLLERR) ||
                (event.events & EPOLLHUP) ||
                (!(event.events & EPOLLIN)))
            {
                std::cout << "Error in the file descriptor." << std::endl;
                close(event.data.fd);
                continue;
            }
            else if(sockfd == event.data.fd)
            {
                while( true )
                {
                    sockaddr_in cli_addr;
                    socklen_t clilen{ sizeof(cli_addr) };

                    // Accept the the request
                    int newsockfd = accept(sockfd, (sockaddr*)&cli_addr, &clilen);
                    if(newsockfd == -1)
                    {
                        break;
                    }

                    set_socket_non_blocking(sockfd);
                    set_epoll_interface(efd, newsockfd);
                    
                    std::string out;
                    inet_ntop(AF_INET, &(cli_addr.sin_addr), &out[0], INET_ADDRSTRLEN);
                    client_ip_address[newsockfd] = out;
                }
                
                continue;
            }
            else
            {
                int newsockfd = event.data.fd;
                
                std::string client_address = client_ip_address[newsockfd];
                
                std::string request_str = "";
                size_t read_result = 0;
                read_request(newsockfd, read_result, request_str);

                // If the read result isn't ok
                if(read_result == -1)
                {
                    log->error("Error in read result, error #" + std::to_string(errno));
                    close(newsockfd);
                    continue;
                }

                // The request string is empty
                if(request_str.empty())
                {
                    log->error("Request string empty.");
                    close(newsockfd);
                    continue;
                }

                // Parse request string
                auto client_request = std::make_shared<sammy::request>(request_id, request_str);
                if(client_request->errors())
                {
                    log->error("Request parse error: " + client_request->error_text());
                    close(newsockfd);
                    continue;
                }

                request_id++;
                
                // Load domain configuration
                std::string response = "";
                auto domain = domain_storage->get_domain(client_request->get_host());

                // If this server does not serve the domain being asked for, we close the connection.
                if(!domain)
                {
                    log->error(client_address + " : " + client_request->get_host() + " : " + client_request->get_method() + " : " + client_request->get_path() + " : Domain not served.");
                    close(newsockfd);
                    continue;
                }

                // If the client address is blacklisted, we close the connection.
                if(domain->is_blacklisted(client_address))
                {
                    log->error(client_address + " : " + client_request->get_host() + " : " + client_request->get_method() + " : " + client_request->get_path() + " : IP blacklisted.");
                    close(newsockfd);
                    continue;
                }

                // Add the task, to be processed by the thread pool when possible
                thread_pool.add_task(std::make_shared<sammy::thread::task>(newsockfd, domain, client_request, cache, client_address));
                
                continue;
            }
        }
    }

    delete [] events;

    return 0;
}
Exemple #17
0
void Network::computeTraining(double alpha, 
        const std::vector<double> & data,
        const std::vector<double> & knowledge)
{
    assert(_outputNeurons.size() == knowledge.size());

    // evaluate network
    computeEvaluation(data);

    // compute training errors of all neurons
    std::set<int> set1;
    std::set<int> set2;
    std::set<int> * ptrCurrentSet = &set1;
    std::set<int> * ptrNextSet = &set2;
    std::vector<double> errors(_neuronValues.size(), 0.0);
 
    // compute local training for output neurons
    for (unsigned x=0; x<_outputNeurons.size(); x++)
    {
        int j=_outputNeurons[x];
        computeLocalOutputTraining(j, knowledge[x], errors);
        ptrCurrentSet->insert(j);
    }

    // update neurons using multiple passes
    // from output neurons to input neurons
    while (not ptrCurrentSet->empty())
    {
        // for each neuron of the set to update
        for (int j : *ptrCurrentSet)
        {
            // update error for j
            computeLocalHiddenTraining(j, errors);

            // get input neurons for the next updating pass
            for (int i=0; i<_synapseConnections.rows(); i++)
            {
                if (_synapseConnections(i,j) != 0)
                {
                    ptrNextSet->insert(i);
                }
            }
        }

        // go to next updating pass
        std::swap(ptrCurrentSet, ptrNextSet);
        ptrNextSet->clear();
    }

    // update synapse weights
    for (int i=0; i<_synapseWeights.rows(); i++)
    {
        for (int j=0; j<_synapseWeights.cols(); j++)
        {
            if (_synapseConnections(i,j) != 0)
            {
                _synapseWeights(i,j) += alpha * errors[j] * _neuronValues(i);
            }
        }
    }
}
Exemple #18
0
int main(int argc, char *argv[])
{
    char *user_config_dir = get_user_config_dir();
    int config_err = 0;

    parse_args(argc, argv);

    /* Make sure all written files are read/writeable only by the current user. */
    umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);

    signal(SIGINT, ignore_SIGINT);

    config_err = create_user_config_dir(user_config_dir);

    if (DATA_FILE == NULL ) {
        if (config_err) {
            DATA_FILE = strdup("data");
        } else {
            DATA_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("data") + 1);

            if (DATA_FILE != NULL) {
                strcpy(DATA_FILE, user_config_dir);
                strcat(DATA_FILE, CONFIGDIR);
                strcat(DATA_FILE, "data");
            } else {
                endwin();
                fprintf(stderr, "malloc() failed. Aborting...\n");
                exit(EXIT_FAILURE);
            }
        }
    }

    free(user_config_dir);

    /* init user_settings struct and load settings from conf file */
    user_settings = malloc(sizeof(struct user_settings));

    if (user_settings == NULL) {
        endwin();
        fprintf(stderr, "malloc() failed. Aborting...\n");
        exit(EXIT_FAILURE);
    }

    memset(user_settings, 0, sizeof(struct user_settings));

    char *p = arg_opts.config_path[0] ? arg_opts.config_path : NULL;
    int settings_err = settings_load(user_settings, p);

    Tox *m = init_tox(arg_opts.use_ipv4);
    init_term();

    if (m == NULL) {
        endwin();
        fprintf(stderr, "Failed to initialize network. Aborting...\n");
        exit(EXIT_FAILURE);
    }

    if (!arg_opts.ignore_data_file)
        load_data(m, DATA_FILE);

    prompt = init_windows(m);

    /* create new thread for ncurses stuff */
    if (pthread_mutex_init(&Winthread.lock, NULL) != 0) {
        endwin();
        fprintf(stderr, "Mutex init failed. Aborting...\n");
        exit(EXIT_FAILURE);
    }

    if (pthread_create(&Winthread.tid, NULL, thread_winref, (void *) m) != 0) {
        endwin();
        fprintf(stderr, "Thread creation failed. Aborting...\n");
        exit(EXIT_FAILURE);
    }

    uint8_t *msg;

#ifdef _SUPPORT_AUDIO

    av = init_audio(prompt, m);
    
    device_set(prompt, input, user_settings->audio_in_dev);
    device_set(prompt, output, user_settings->audio_out_dev);

    if ( errors() == NoError )
        msg = "Audio initiated with no problems.";
    else /* Get error code and stuff */
        msg = "Error initiating audio!";

    line_info_add(prompt, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);

#endif /* _SUPPORT_AUDIO */

    if (config_err) {
        msg = "Unable to determine configuration directory. Defaulting to 'data' for a keyfile...";
        line_info_add(prompt, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
    }


    if (settings_err == -1) {
        msg = "Failed to load user settings";
        line_info_add(prompt, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
    }


    sort_friendlist_index();
    prompt_init_statusbar(prompt, m);

    while (true) {
        update_unix_time();
        do_toxic(m, prompt);
        usleep(10000);
    }

    return 0;
}
Exemple #19
0
    void UnitCoarsening::compute(
        std::shared_ptr<RBFFunctionInterface> rbfFunction,
        std::unique_ptr<ElDistVector> positions,
        std::unique_ptr<ElDistVector> positionsInterpolation
        )
    {
        // selection is only performed once. Thereafter, the selected points are used for the
        // interpolation.

        selectedPositions.clear();

        // An initial selection is needed before the greedy algorithm starts
        // adding points to the selection.
        // The first point is the point with the largest radius from the origin.
        // The second point is the point with the largest distance from the
        // first point.

        {
            // Find first point: largest radius from origin
            ElDistVector norms( positions->Grid() );
            norms.AlignWith( *positions );
            El::RowTwoNorms( *positions, norms );
            El::Entry<double> locMax = El::MaxAbsLoc( norms );
            selectedPositions.push_back( locMax.i );

            // Find second point: largest distance from the first point
            ElDistVector distance = *positions;
            ElDistVector tmp( distance.Grid() );
            tmp.AlignWith( distance );
            El::Ones( tmp, distance.Height(), distance.Width() );

            for ( int iColumn = 0; iColumn < tmp.Width(); iColumn++ )
            {
                ElDistVector view( tmp.Grid() );
                El::View( view, tmp, 0, iColumn, tmp.Height(), 1 );
                El::Scale( positions->Get( locMax.i, iColumn ), view );
            }

            El::Axpy( -1, tmp, distance );

            El::RowTwoNorms( distance, norms );
            locMax = El::MaxAbsLoc( norms );
            selectedPositions.push_back( locMax.i );
        }

        int maxPoints = std::min( this->maxPoints, positions->Height() );
        int minPoints = std::min( this->minPoints, positions->Height() );

        double largestError = 0;

        for ( int i = 0; i < maxPoints; i++ )
        {
            // Build the matrices for the RBF interpolation
            std::unique_ptr<ElDistVector> positionsCoarse( new ElDistVector( positions->Grid() ) );
            std::unique_ptr<ElDistVector> valuesCoarse( new ElDistVector( positions->Grid() ) );
            positionsCoarse->AlignWith( *positions );
            valuesCoarse->AlignWith( *positions );
            El::Ones( *valuesCoarse, selectedPositions.size(), positions->Width() );
            El::Zeros( *positionsCoarse, selectedPositions.size(), positions->Width() );

            selectData( positions, positionsCoarse );

            // Perform the RBF interpolation
            std::unique_ptr<ElDistVector> positionsInterpolationCoarse( new ElDistVector( positions->Grid() ) );

            *positionsInterpolationCoarse = *positions;

            ElRBFInterpolation rbf( rbfFunction, std::move( positionsCoarse ), std::move( positionsInterpolationCoarse ) );

            std::unique_ptr<ElDistVector> result = rbf.interpolate( valuesCoarse );

            assert( result->Height() == positions->Height() );
            assert( result->Width() == positions->Width() );

            // Compute the error
            ElDistVector diff( result->Grid() );
            diff.AlignWith( *result );
            El::Ones( diff, result->Height(), result->Width() );
            El::Axpy( -1, *result, diff );
            ElDistVector errors( diff.Grid() );
            errors.AlignWith( diff );
            El::RowTwoNorms( diff, errors );

            // Get location of max error
            El::Entry<double> locMax = El::MaxAbsLoc( errors );
            largestError = locMax.value;

            // Break if maximum points are reached
            if ( int( selectedPositions.size() ) >= maxPoints )
                break;

            bool convergence = locMax.value < tol && int( selectedPositions.size() ) >= minPoints;

            if ( convergence )
                break;

            selectedPositions.push_back( locMax.i );
        }

        if ( El::mpi::Rank() == 0 )
        {
            std::cout << "RBF interpolation coarsening: selected ";
            std::cout << selectedPositions.size() << "/" << positions->Height() << " points,";
            std::cout << " error = " << largestError << ", tol = " << tol << std::endl;
        }

        std::unique_ptr<ElDistVector> positionsCoarse( new ElDistVector( positions->Grid() ) );
        positionsCoarse->AlignWith( *positions );
        El::Zeros( *positionsCoarse, selectedPositions.size(), positions->Width() );

        selectData( positions, positionsCoarse );

        rbf.compute( rbfFunction, std::move( positionsCoarse ), std::move( positionsInterpolation ) );
    }
KSMShutdownDlg::KSMShutdownDlg( QWindow* parent,
                                bool maysd, bool choose, KWorkSpace::ShutdownType sdtype,
                                const QString& theme)
  : QQuickView(parent),
    m_result(false)
    // this is a WType_Popup on purpose. Do not change that! Not
    // having a popup here has severe side effects.
{
    // window stuff
    setClearBeforeRendering(true);
    setColor(QColor(Qt::transparent));
    setFlags(Qt::FramelessWindowHint | Qt::BypassWindowManagerHint);

    QPoint globalPosition(QCursor::pos());
    foreach (QScreen *s, QGuiApplication::screens()) {
        if (s->geometry().contains(globalPosition)) {
            setScreen(s);
            break;
        }
    }


    // Qt doesn't set this on unmanaged windows
    //FIXME: or does it?
    XChangeProperty( QX11Info::display(), winId(),
        XInternAtom( QX11Info::display(), "WM_WINDOW_ROLE", False ), XA_STRING, 8, PropModeReplace,
        (unsigned char *)"logoutdialog", strlen( "logoutdialog" ));


    //QQuickView *windowContainer = QQuickView::createWindowContainer(m_view, this);
    //windowContainer->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    QQmlContext *context = rootContext();
    context->setContextProperty(QStringLiteral("maysd"), maysd);
    context->setContextProperty(QStringLiteral("choose"), choose);
    context->setContextProperty(QStringLiteral("sdtype"), sdtype);

    QQmlPropertyMap *mapShutdownType = new QQmlPropertyMap(this);
    mapShutdownType->insert(QStringLiteral("ShutdownTypeDefault"), QVariant::fromValue<int>(KWorkSpace::ShutdownTypeDefault));
    mapShutdownType->insert(QStringLiteral("ShutdownTypeNone"), QVariant::fromValue<int>(KWorkSpace::ShutdownTypeNone));
    mapShutdownType->insert(QStringLiteral("ShutdownTypeReboot"), QVariant::fromValue<int>(KWorkSpace::ShutdownTypeReboot));
    mapShutdownType->insert(QStringLiteral("ShutdownTypeHalt"), QVariant::fromValue<int>(KWorkSpace::ShutdownTypeHalt));
    mapShutdownType->insert(QStringLiteral("ShutdownTypeLogout"), QVariant::fromValue<int>(KWorkSpace::ShutdownTypeLogout));
    context->setContextProperty(QStringLiteral("ShutdownType"), mapShutdownType);

    QQmlPropertyMap *mapSpdMethods = new QQmlPropertyMap(this);
    QSet<Solid::PowerManagement::SleepState> spdMethods = Solid::PowerManagement::supportedSleepStates();
    mapSpdMethods->insert(QStringLiteral("StandbyState"), QVariant::fromValue(spdMethods.contains(Solid::PowerManagement::StandbyState)));
    mapSpdMethods->insert(QStringLiteral("SuspendState"), QVariant::fromValue(spdMethods.contains(Solid::PowerManagement::SuspendState)));
    mapSpdMethods->insert(QStringLiteral("HibernateState"), QVariant::fromValue(spdMethods.contains(Solid::PowerManagement::HibernateState)));
    context->setContextProperty(QStringLiteral("spdMethods"), mapSpdMethods);

    QString bootManager = KConfig(QStringLiteral(KDE_CONFDIR "/kdm/kdmrc"), KConfig::SimpleConfig)
                          .group("Shutdown")
                          .readEntry("BootManager", "None");
    context->setContextProperty(QStringLiteral("bootManager"), bootManager);

    QStringList options;
    int def, cur;
    if ( KDisplayManager().bootOptions( rebootOptions, def, cur ) ) {
        if ( cur > -1 ) {
            def = cur;
        }
    }
    QQmlPropertyMap *rebootOptionsMap = new QQmlPropertyMap(this);
    rebootOptionsMap->insert(QStringLiteral("options"), QVariant::fromValue(rebootOptions));
    rebootOptionsMap->insert(QStringLiteral("default"), QVariant::fromValue(def));
    context->setContextProperty(QStringLiteral("rebootOptions"), rebootOptionsMap);
    context->setContextProperty(QStringLiteral("screenGeometry"), screen()->geometry());

    setModality(Qt::ApplicationModal);

    // engine stuff
    KDeclarative::KDeclarative kdeclarative;
    kdeclarative.setDeclarativeEngine(engine());
    kdeclarative.initialize();
    kdeclarative.setupBindings();
//    windowContainer->installEventFilter(this);

    QString fileName;
    if(theme.isEmpty()) {
        KPackage::Package package = KPackage::PackageLoader::self()->loadPackage(QStringLiteral("Plasma/LookAndFeel"));
        KConfigGroup cg(KSharedConfig::openConfig(QStringLiteral("kdeglobals")), "KDE");
        const QString packageName = cg.readEntry("LookAndFeelPackage", QString());
        if (!packageName.isEmpty()) {
            package.setPath(packageName);
        }

        fileName = package.filePath("logoutmainscript");
    } else
        fileName = theme;

    if (QFile::exists(fileName)) {
        //qCDebug(KSMSERVER) << "Using QML theme" << fileName;
        setSource(QUrl::fromLocalFile(fileName));
    } else {
        qWarning() << "Couldn't find a theme for the Shutdown dialog" << fileName;
        return;
    }

    setPosition(screen()->virtualGeometry().center().x() - width() / 2,
                screen()->virtualGeometry().center().y() - height() / 2);

    if(!errors().isEmpty()) {
        qWarning() << errors();
    }

    connect(rootObject(), SIGNAL(logoutRequested()), SLOT(slotLogout()));
    connect(rootObject(), SIGNAL(haltRequested()), SLOT(slotHalt()));
    connect(rootObject(), SIGNAL(suspendRequested(int)), SLOT(slotSuspend(int)) );
    connect(rootObject(), SIGNAL(rebootRequested()), SLOT(slotReboot()));
    connect(rootObject(), SIGNAL(rebootRequested2(int)), SLOT(slotReboot(int)) );
    connect(rootObject(), SIGNAL(cancelRequested()), SLOT(reject()));
    connect(rootObject(), SIGNAL(lockScreenRequested()), SLOT(slotLockScreen()));

    show();
    requestActivate();

    KWindowSystem::setState(winId(), NET::SkipTaskbar|NET::SkipPager);
}
Exemple #21
0
static UniValue estimaterawfee(const JSONRPCRequest& request)
{
    if (request.fHelp || request.params.size() < 1 || request.params.size() > 2)
        throw std::runtime_error(
            "estimaterawfee conf_target (threshold)\n"
            "\nWARNING: This interface is unstable and may disappear or change!\n"
            "\nWARNING: This is an advanced API call that is tightly coupled to the specific\n"
            "         implementation of fee estimation. The parameters it can be called with\n"
            "         and the results it returns will change if the internal implementation changes.\n"
            "\nEstimates the approximate fee per kilobyte needed for a transaction to begin\n"
            "confirmation within conf_target blocks if possible. Uses virtual transaction size as\n"
            "defined in BIP 141 (witness data is discounted).\n"
            "\nArguments:\n"
            "1. conf_target (numeric) Confirmation target in blocks (1 - 1008)\n"
            "2. threshold   (numeric, optional) The proportion of transactions in a given feerate range that must have been\n"
            "               confirmed within conf_target in order to consider those feerates as high enough and proceed to check\n"
            "               lower buckets.  Default: 0.95\n"
            "\nResult:\n"
            "{\n"
            "  \"short\" : {            (json object, optional) estimate for short time horizon\n"
            "      \"feerate\" : x.x,        (numeric, optional) estimate fee rate in " + CURRENCY_UNIT + "/kB\n"
            "      \"decay\" : x.x,          (numeric) exponential decay (per block) for historical moving average of confirmation data\n"
            "      \"scale\" : x,            (numeric) The resolution of confirmation targets at this time horizon\n"
            "      \"pass\" : {              (json object, optional) information about the lowest range of feerates to succeed in meeting the threshold\n"
            "          \"startrange\" : x.x,     (numeric) start of feerate range\n"
            "          \"endrange\" : x.x,       (numeric) end of feerate range\n"
            "          \"withintarget\" : x.x,   (numeric) number of txs over history horizon in the feerate range that were confirmed within target\n"
            "          \"totalconfirmed\" : x.x, (numeric) number of txs over history horizon in the feerate range that were confirmed at any point\n"
            "          \"inmempool\" : x.x,      (numeric) current number of txs in mempool in the feerate range unconfirmed for at least target blocks\n"
            "          \"leftmempool\" : x.x,    (numeric) number of txs over history horizon in the feerate range that left mempool unconfirmed after target\n"
            "      },\n"
            "      \"fail\" : { ... },       (json object, optional) information about the highest range of feerates to fail to meet the threshold\n"
            "      \"errors\":  [ str... ]   (json array of strings, optional) Errors encountered during processing\n"
            "  },\n"
            "  \"medium\" : { ... },    (json object, optional) estimate for medium time horizon\n"
            "  \"long\" : { ... }       (json object) estimate for long time horizon\n"
            "}\n"
            "\n"
            "Results are returned for any horizon which tracks blocks up to the confirmation target.\n"
            "\nExample:\n"
            + HelpExampleCli("estimaterawfee", "6 0.9")
            );

    RPCTypeCheck(request.params, {UniValue::VNUM, UniValue::VNUM}, true);
    RPCTypeCheckArgument(request.params[0], UniValue::VNUM);
    unsigned int conf_target = ParseConfirmTarget(request.params[0]);
    double threshold = 0.95;
    if (!request.params[1].isNull()) {
        threshold = request.params[1].get_real();
    }
    if (threshold < 0 || threshold > 1) {
        throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid threshold");
    }

    UniValue result(UniValue::VOBJ);

    for (const FeeEstimateHorizon horizon : {FeeEstimateHorizon::SHORT_HALFLIFE, FeeEstimateHorizon::MED_HALFLIFE, FeeEstimateHorizon::LONG_HALFLIFE}) {
        CFeeRate feeRate;
        EstimationResult buckets;

        // Only output results for horizons which track the target
        if (conf_target > ::feeEstimator.HighestTargetTracked(horizon)) continue;

        feeRate = ::feeEstimator.estimateRawFee(conf_target, threshold, horizon, &buckets);
        UniValue horizon_result(UniValue::VOBJ);
        UniValue errors(UniValue::VARR);
        UniValue passbucket(UniValue::VOBJ);
        passbucket.pushKV("startrange", round(buckets.pass.start));
        passbucket.pushKV("endrange", round(buckets.pass.end));
        passbucket.pushKV("withintarget", round(buckets.pass.withinTarget * 100.0) / 100.0);
        passbucket.pushKV("totalconfirmed", round(buckets.pass.totalConfirmed * 100.0) / 100.0);
        passbucket.pushKV("inmempool", round(buckets.pass.inMempool * 100.0) / 100.0);
        passbucket.pushKV("leftmempool", round(buckets.pass.leftMempool * 100.0) / 100.0);
        UniValue failbucket(UniValue::VOBJ);
        failbucket.pushKV("startrange", round(buckets.fail.start));
        failbucket.pushKV("endrange", round(buckets.fail.end));
        failbucket.pushKV("withintarget", round(buckets.fail.withinTarget * 100.0) / 100.0);
        failbucket.pushKV("totalconfirmed", round(buckets.fail.totalConfirmed * 100.0) / 100.0);
        failbucket.pushKV("inmempool", round(buckets.fail.inMempool * 100.0) / 100.0);
        failbucket.pushKV("leftmempool", round(buckets.fail.leftMempool * 100.0) / 100.0);

        // CFeeRate(0) is used to indicate error as a return value from estimateRawFee
        if (feeRate != CFeeRate(0)) {
            horizon_result.pushKV("feerate", ValueFromAmount(feeRate.GetFeePerK()));
            horizon_result.pushKV("decay", buckets.decay);
            horizon_result.pushKV("scale", (int)buckets.scale);
            horizon_result.pushKV("pass", passbucket);
            // buckets.fail.start == -1 indicates that all buckets passed, there is no fail bucket to output
            if (buckets.fail.start != -1) horizon_result.pushKV("fail", failbucket);
        } else {
            // Output only information that is still meaningful in the event of error
            horizon_result.pushKV("decay", buckets.decay);
            horizon_result.pushKV("scale", (int)buckets.scale);
            horizon_result.pushKV("fail", failbucket);
            errors.push_back("Insufficient data or no feerate found which meets threshold");
            horizon_result.pushKV("errors",errors);
        }
        result.pushKV(StringForFeeEstimateHorizon(horizon), horizon_result);
    }
    return result;
}
Exemple #22
0
static UniValue estimatesmartfee(const JSONRPCRequest& request)
{
    if (request.fHelp || request.params.size() < 1 || request.params.size() > 2)
        throw std::runtime_error(
            RPCHelpMan{"estimatesmartfee",
                "\nEstimates the approximate fee per kilobyte needed for a transaction to begin\n"
                "confirmation within conf_target blocks if possible and return the number of blocks\n"
                "for which the estimate is valid. Uses virtual transaction size as defined\n"
                "in BIP 141 (witness data is discounted).\n",
                {
                    {"conf_target", RPCArg::Type::NUM, RPCArg::Optional::NO, "Confirmation target in blocks (1 - 1008)"},
                    {"estimate_mode", RPCArg::Type::STR, /* default */ "CONSERVATIVE", "The fee estimate mode.\n"
            "                   Whether to return a more conservative estimate which also satisfies\n"
            "                   a longer history. A conservative estimate potentially returns a\n"
            "                   higher feerate and is more likely to be sufficient for the desired\n"
            "                   target, but is not as responsive to short term drops in the\n"
            "                   prevailing fee market.  Must be one of:\n"
            "       \"UNSET\"\n"
            "       \"ECONOMICAL\"\n"
            "       \"CONSERVATIVE\""},
                },
                RPCResult{
            "{\n"
            "  \"feerate\" : x.x,     (numeric, optional) estimate fee rate in " + CURRENCY_UNIT + "/kB\n"
            "  \"errors\": [ str... ] (json array of strings, optional) Errors encountered during processing\n"
            "  \"blocks\" : n         (numeric) block number where estimate was found\n"
            "}\n"
            "\n"
            "The request target will be clamped between 2 and the highest target\n"
            "fee estimation is able to return based on how long it has been running.\n"
            "An error is returned if not enough transactions and blocks\n"
            "have been observed to make an estimate for any number of blocks.\n"
                },
                RPCExamples{
                    HelpExampleCli("estimatesmartfee", "6")
                },
            }.ToString());

    RPCTypeCheck(request.params, {UniValue::VNUM, UniValue::VSTR});
    RPCTypeCheckArgument(request.params[0], UniValue::VNUM);
    unsigned int max_target = ::feeEstimator.HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE);
    unsigned int conf_target = ParseConfirmTarget(request.params[0], max_target);
    bool conservative = true;
    if (!request.params[1].isNull()) {
        FeeEstimateMode fee_mode;
        if (!FeeModeFromString(request.params[1].get_str(), fee_mode)) {
            throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid estimate_mode parameter");
        }
        if (fee_mode == FeeEstimateMode::ECONOMICAL) conservative = false;
    }

    UniValue result(UniValue::VOBJ);
    UniValue errors(UniValue::VARR);
    FeeCalculation feeCalc;
    CFeeRate feeRate = ::feeEstimator.estimateSmartFee(conf_target, &feeCalc, conservative);
    if (feeRate != CFeeRate(0)) {
        result.pushKV("feerate", ValueFromAmount(feeRate.GetFeePerK()));
    } else {
        errors.push_back("Insufficient data or no feerate found");
        result.pushKV("errors", errors);
    }
    result.pushKV("blocks", feeCalc.returnedTarget);
    return result;
}
Exemple #23
0
/////////////////////////
// Read data from the socket and process it
void IRCClient::readData()
{
	// Get data from the socket
	char buf[1024];

	int read = 1;
	while (read > 0)  {
		int read = m_chatSocket.Read(buf, sizeof(buf)); // HINT: non-blocking

		// Nothing yet
		if (read == 0)
			break;

		// Error
		if(read < 0)  {
			if (!IsMessageEndSocketErrorNr(GetSocketErrorNr()))  {
				errors("IRC: network error - " + GetSocketErrorStr(GetSocketErrorNr()) + "\n");
				disconnect();
			}
			break;
		}

		// Add the data to the buffer
		m_netBuffer.append(buf, read);
	}

	if (m_netBuffer.empty())
		return;

	size_t pos = m_netBuffer.find("\r\n");
	while(pos != std::string::npos)  {
		std::string line = m_netBuffer.substr(0, pos);

		//printf("IRC: %s\n", line.c_str());

		// Check if the sender is specified
		m_netBuffer.erase(0, pos + 2);
		IRCCommand cmd;
		if(line.size() && line[0] == ':')  {
			size_t pos2 = line.find(' ');
			cmd.sender = line.substr(1, pos2);

			// Check for exclamation mark in the nick as a special character
			size_t excl_pos = cmd.sender.find('!');
			if (excl_pos != std::string::npos)  {
				cmd.sender.erase(excl_pos); // Erase anything (usually IP + ISP info) from the nick
			}

			line.erase(0, pos2 + 1);
		}

		// Get the command and parameters
		cmd.cmd = line.substr(0, line.find(' '));
		while (line.find(' ') != std::string::npos )  {
			line = line.substr(line.find(' ') + 1);
			if (line.size() == 0)
				break;
			if (line[0] == ':')  {
				cmd.params.push_back(line.substr(1));
				break;
			}

			cmd.params.push_back(line.substr(0, line.find(' ')));
		}

		// Parse the command
		parseCommand(cmd);

		pos = m_netBuffer.find("\r\n");
	}
}
    SwaptionVolCube1::Cube
    SwaptionVolCube1::sabrCalibration(const Cube& marketVolCube) const {

        const std::vector<Time>& optionTimes = marketVolCube.optionTimes();
        const std::vector<Time>& swapLengths = marketVolCube.swapLengths();
        const std::vector<Date>& optionDates = marketVolCube.optionDates();
        const std::vector<Period>& swapTenors = marketVolCube.swapTenors();
        Matrix alphas(optionTimes.size(), swapLengths.size(),0.);
        Matrix betas(alphas);
        Matrix nus(alphas);
        Matrix rhos(alphas);
        Matrix forwards(alphas);
        Matrix errors(alphas);
        Matrix maxErrors(alphas);
        Matrix endCriteria(alphas);

        const std::vector<Matrix>& tmpMarketVolCube = marketVolCube.points();

        std::vector<Real> strikes(strikeSpreads_.size());
        std::vector<Real> volatilities(strikeSpreads_.size());

        for (Size j=0; j<optionTimes.size(); j++) {
            for (Size k=0; k<swapLengths.size(); k++) {
                Rate atmForward = atmStrike(optionDates[j], swapTenors[k]);
                strikes.clear();
                volatilities.clear();
                for (Size i=0; i<nStrikes_; i++){
                    Real strike = atmForward+strikeSpreads_[i];
                    if(strike>=MINSTRIKE) {
                        strikes.push_back(strike);
                        volatilities.push_back(tmpMarketVolCube[i][j][k]);
                    }
                }

                const std::vector<Real>& guess = parametersGuess_.operator()(
                    optionTimes[j], swapLengths[k]);

                const boost::shared_ptr<SABRInterpolation> sabrInterpolation =
                    boost::shared_ptr<SABRInterpolation>(new
                        SABRInterpolation(strikes.begin(), strikes.end(),
                                          volatilities.begin(),
                                          optionTimes[j], atmForward,
                                          guess[0], guess[1],
                                          guess[2], guess[3],
                                          isParameterFixed_[0],
                                          isParameterFixed_[1],
                                          isParameterFixed_[2],
                                          isParameterFixed_[3],
                                          vegaWeightedSmileFit_,
                                          endCriteria_,
                                          optMethod_,
                                          errorAccept_,
                                          useMaxError_,
                                          maxGuesses_));
                sabrInterpolation->update();

                Real rmsError = sabrInterpolation->rmsError();
                Real maxError = sabrInterpolation->maxError();
                alphas     [j][k] = sabrInterpolation->alpha();
                betas      [j][k] = sabrInterpolation->beta();
                nus        [j][k] = sabrInterpolation->nu();
                rhos       [j][k] = sabrInterpolation->rho();
                forwards   [j][k] = atmForward;
                errors     [j][k] = rmsError;
                maxErrors  [j][k] = maxError;
                endCriteria[j][k] = sabrInterpolation->endCriteria();

                QL_ENSURE(endCriteria[j][k]!=EndCriteria::MaxIterations,
                          "global swaptions calibration failed: "
                          "MaxIterations reached: " << "\n" <<
                          "option maturity = " << optionDates[j] << ", \n" <<
                          "swap tenor = " << swapTenors[k] << ", \n" <<
                          "error = " << io::rate(errors[j][k])  << ", \n" <<
                          "max error = " << io::rate(maxErrors[j][k]) << ", \n" <<
                          "   alpha = " <<  alphas[j][k] << "n" <<
                          "   beta = " <<  betas[j][k] << "\n" <<
                          "   nu = " <<  nus[j][k]   << "\n" <<
                          "   rho = " <<  rhos[j][k]  << "\n"
                          );

                QL_ENSURE(useMaxError_ ? maxError : rmsError < maxErrorTolerance_,
                      "global swaptions calibration failed: "
                      "option tenor " << optionDates[j] <<
                      ", swap tenor " << swapTenors[k] <<
                      (useMaxError_ ? ": max error " : ": error") <<
                      (useMaxError_ ? maxError : rmsError) <<
                          "   alpha = " <<  alphas[j][k] << "n" <<
                          "   beta = " <<  betas[j][k] << "\n" <<
                          "   nu = " <<  nus[j][k]   << "\n" <<
                          "   rho = " <<  rhos[j][k]  << "\n" <<
                      (useMaxError_ ? ": error" : ": max error ") <<
                      (useMaxError_ ? rmsError :maxError)
                );

            }
        }
        Cube sabrParametersCube(optionDates, swapTenors,
                                optionTimes, swapLengths, 8);
        sabrParametersCube.setLayer(0, alphas);
        sabrParametersCube.setLayer(1, betas);
        sabrParametersCube.setLayer(2, nus);
        sabrParametersCube.setLayer(3, rhos);
        sabrParametersCube.setLayer(4, forwards);
        sabrParametersCube.setLayer(5, errors);
        sabrParametersCube.setLayer(6, maxErrors);
        sabrParametersCube.setLayer(7, endCriteria);

        return sabrParametersCube;

    }
Exemple #25
0
//------------------------------------------------------------------------------
//
std::auto_ptr<XxDiffs> XxBuilderDirs2::process( 
   const QString& command,
   XxBuffer&      buffer1,
   XxBuffer&      buffer2
)
{
   initLines();

   QString path1 = buffer1.getName();
   QString path2 = buffer2.getName();

   QStringList filenames;
   filenames.append( path1 );
   filenames.append( path2 );
   QStringList out_args;
   QString executable;
   XxUtil::splitArgs( command, filenames, executable, out_args );

   QProcess diffProc;
   diffProc.start( executable, out_args );
   if ( ! diffProc.waitForStarted() ) {
      throw XxIoError( XX_EXC_PARAMS );
   }
   diffProc.waitForReadyRead();
   diffProc.setReadChannel( QProcess::StandardOutput );

   std::vector<DirDiffType> types1;
   std::vector<DirDiffType> types2;

   QTextStream errors( &_errors );

   // Note: for now we don't support recursive diffs built against a directory.
   if ( _buildSolelyFromOutput || _isDiffRecursive ) {
      buildSolelyFromOutput(
         diffProc, errors, buffer1, buffer2, types1, types2
      );
   }
   else {
      buildAgainstReadDirectory( 
         diffProc, errors, buffer1, buffer2, types1, types2
      );
   }

#ifdef LOCAL_TRACE
   XX_TRACE( "------------------------------" );
   for ( unsigned int ii = 0; ii < types1.size(); ++ii ) {
      XX_TRACE( typeString[ types1[ii] ] );
   }
   XX_TRACE( "------------------------------" );
   XX_TRACE( "------------------------------" );
   for ( unsigned int ii = 0; ii < types2.size(); ++ii ) {
      XX_TRACE( typeString[ types2[ii] ] );
   }
   XX_TRACE( "------------------------------" );
#endif
      
   XxFln fline1 = 1;
   XxFln fline2 = 1;
   {
      // Create regions with it. Hopefully our searches resulted in something
      // coherent w.r.t. to the quantities and pair matching of entries. Barf if
      // it doesn't.
      std::vector<DirDiffType>::const_iterator it1 = types1.begin();
      std::vector<DirDiffType>::const_iterator it2 = types2.begin();
   
      while ( it1 != types1.end() || it2 != types2.end() ) {
         if ( it1 == types1.end() ) {
            if ( *it2 != ONLY_IN ) {
               throw XxInternalError( XX_EXC_PARAMS );
            }
            XxLine line( XxLine::INSERT_2, -1, fline2++ );
            addLine( line );
            ++it2;
            continue;
         }
         if ( it2 == types2.end() ) {
            if ( *it1 != ONLY_IN ) {
               throw XxInternalError( XX_EXC_PARAMS );
            }
            XxLine line( XxLine::INSERT_1, fline1++, -1 );
            addLine( line );
            ++it1;
            continue;
         }

         if ( *it1 == ONLY_IN ) {
            XxLine line( XxLine::INSERT_1, fline1++, -1 );
            addLine( line );
            ++it1;
         }
         else if ( *it2 == ONLY_IN ) {
            XxLine line( XxLine::INSERT_2, -1, fline2++ );
            addLine( line );
            ++it2;
         }
         else if ( *it1 == DIFFER ) {
            if ( *it2 != *it1 ) {
               throw XxInternalError( XX_EXC_PARAMS );
            }
            XxLine::Type dtype = 
               _ignoreFileChanges == true ? XxLine::SAME : XxLine::DIFF_ALL;
            XxLine line( dtype, fline1++, fline2++ );
            addLine( line );
            ++it1;
            ++it2;
         }
         else if ( *it1 == IDENTICAL ) {
            if ( *it2 != *it1 ) {
               throw XxInternalError( XX_EXC_PARAMS );
            }
            XxLine line( XxLine::SAME, fline1++, fline2++ );
            addLine( line );
            ++it1;
            ++it2;
         }
         else if ( *it1 == COMMON_SUBDIR ) {
            if ( *it2 != *it1 ) {
               throw XxInternalError( XX_EXC_PARAMS );
            }
            XxLine line( XxLine::DIRECTORIES, fline1++, fline2++ );
            addLine( line );
            ++it1;
            ++it2;
         }
         else {
            XX_ASSERT( false );
         }
      }
   }

   // Create hunks.
   if ( _lines.size() > 0 ) {
      XxHunk curHunk = 0;
      XxLine::Type prevType = _lines[0].getType();
      _lines[0].setHunkId( curHunk );
      for ( XxDln ii = 1; ii < XxDln(_lines.size()); ++ii ) {
         XxLine& cline = _lines[ii];
         if ( prevType != cline.getType() ) {
            ++curHunk;
            prevType = cline.getType();
         }
         cline.setHunkId( curHunk );
      }
   }

   diffProc.waitForFinished();

   // Collect stderr.
   QString errstr = diffProc.readAllStandardError();
   if ( ! errstr.isEmpty() ) {
      errors << errstr << endl;
   }
   _status = ( diffProc.exitStatus() == QProcess::NormalExit ) ? diffProc.exitCode() : 2;

   // Saved error text.
   errors << flush;
   XX_LOCAL_TRACE( "Errors: " << _errors );

   // If we've read no lines and there are diff errors then blow off
   if ( ( fline1 == 1 ) && ( fline2 == 1 ) && hasErrors() ) {
      throw XxIoError( XX_EXC_PARAMS );
   }

   std::auto_ptr<XxDiffs> ap( new XxDiffs( _lines, true ) );
   return ap;
}
void CommandLineParser::parse(int *argc, char *argv[])
{
    ExceptionInitializer errors("Invalid command-line options");
    std::string          currentContext;
    bool                 bInOption = false;

    impl_->assigner_.start();
    int newi = 1;
    for (int i = 1; i != *argc; ++i)
    {
        const char *const arg        = argv[i];
        const char *const optionName = impl_->toOptionName(arg);
        if (optionName != NULL)
        {
            if (bInOption)
            {
                try
                {
                    impl_->assigner_.finishOption();
                }
                catch (UserInputError &ex)
                {
                    ex.prependContext(currentContext);
                    errors.addCurrentExceptionAsNested();
                }
            }
            currentContext = "In command-line option " + std::string(arg);
            try
            {
                bInOption = impl_->assigner_.tryStartOption(optionName);
                if (!bInOption)
                {
                    currentContext.clear();
                    if (!impl_->bSkipUnknown_)
                    {
                        std::string message =
                            "Unknown command-line option " + std::string(arg);
                        GMX_THROW(InvalidInputError(message));
                    }
                }
            }
            catch (UserInputError &ex)
            {
                // If tryStartOption() throws, make sure that the rest gets
                // ignored.
                // TODO: Consider whether we should remove the option from the
                // command line nonetheless, as it is recognized, but just
                // invalid.
                bInOption = false;
                ex.prependContext(currentContext);
                errors.addCurrentExceptionAsNested();
                currentContext.clear();
            }
        }
        else if (bInOption)
        {
            try
            {
                impl_->assigner_.appendValue(arg);
            }
            // TODO: Consider if some types of exceptions would be better left
            // unhandled.
            catch (GromacsException &ex)
            {
                ex.prependContext(currentContext);
                errors.addCurrentExceptionAsNested();
            }
        }
        // Remove recognized options if applicable.
        if (!bInOption && impl_->bSkipUnknown_)
        {
            argv[newi] = argv[i];
            ++newi;
        }
    }
    // Update the argc count if argv was modified.
    if (impl_->bSkipUnknown_)
    {
        *argc      = newi;
        argv[newi] = NULL;
    }
    // Finish the last option.
    if (bInOption)
    {
        try
        {
            impl_->assigner_.finishOption();
        }
        catch (UserInputError &ex)
        {
            ex.prependContext(currentContext);
            errors.addCurrentExceptionAsNested();
        }
    }
    impl_->assigner_.finish();
    if (errors.hasNestedExceptions())
    {
        // TODO: This exception type may not always be appropriate.
        GMX_THROW(InvalidInputError(errors));
    }
}
Exemple #27
0
bool TestRunner::passed() const
{
    return errors().empty();
}
    void
    run(int)
    {
        try
        {
            cv::Mat_<unsigned char> plane_mask;
            for (unsigned char i = 0; i < 3; ++i)
            {
                cv::RgbdNormals::RGBD_NORMALS_METHOD method;
                // inner vector: whether it's 1 plane or 3 planes
                // outer vector: float or double
                std::vector<std::vector<float> > errors(2);
                errors[0].resize(2);
                errors[1].resize(2);
                switch (i)
                {
                case 0:
                    method = cv::RgbdNormals::RGBD_NORMALS_METHOD_FALS;
                    std::cout << std::endl << "*** FALS" << std::endl;
                    errors[0][0] = 0.006;
                    errors[0][1] = 0.03;
                    errors[1][0] = 0.00008;
                    errors[1][1] = 0.02;
                    break;
                case 1:
                    method = cv::RgbdNormals::RGBD_NORMALS_METHOD_LINEMOD;
                    std::cout << std::endl << "*** LINEMOD" << std::endl;
                    errors[0][0] = 0.03;
                    errors[0][1] = 0.07;
                    errors[1][0] = 0.05;
                    errors[1][1] = 0.08;
                    break;
                case 2:
                    method = cv::RgbdNormals::RGBD_NORMALS_METHOD_SRI;
                    std::cout << std::endl << "*** SRI" << std::endl;
                    errors[0][0] = 0.02;
                    errors[0][1] = 0.04;
                    errors[1][0] = 0.02;
                    errors[1][1] = 0.04;
                    break;
                }

                for (unsigned char j = 0; j < 2; ++j)
                {
                    int depth = (j % 2 == 0) ? CV_32F : CV_64F;
                    if (depth == CV_32F)
                        std::cout << "* float" << std::endl;
                    else
                        std::cout << "* double" << std::endl;

                    cv::RgbdNormals normals_computer(H, W, depth, K, 5, method);
                    normals_computer.initialize();

                    std::vector<Plane> plane_params;
                    cv::Mat points3d, ground_normals;
                    // 1 plane, continuous scene, very low error..
                    std::cout << "1 plane" << std::endl;
                    float err_mean = 0;
                    for (int ii = 0; ii < 5; ++ii)
                    {
                        gen_points_3d(plane_params, plane_mask, points3d, ground_normals, 1);
                        err_mean += testit(points3d, ground_normals, normals_computer);
                    }
                    std::cout << "mean diff: " << (err_mean / 5) << std::endl;
                    EXPECT_LE(err_mean/5, errors[j][0])<< " thresh: " << errors[j][0] << std::endl;

                    // 3 discontinuities, more error expected.
                    std::cout << "3 planes" << std::endl;
                    err_mean = 0;
                    for (int ii = 0; ii < 5; ++ii)
                    {
                        gen_points_3d(plane_params, plane_mask, points3d, ground_normals, 3);
                        err_mean += testit(points3d, ground_normals, normals_computer);
                    }
                    std::cout << "mean diff: " << (err_mean / 5) << std::endl;
                    EXPECT_LE(err_mean/5, errors[j][1])<< "mean diff: " << (err_mean/5) << " thresh: " << errors[j][1] << std::endl;
                }
            }

            //TODO test NaNs in data

        } catch (...)
        {
            ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
        }
        ts->set_failed_test_info(cvtest::TS::OK);
    }
Exemple #29
0
int main(int argc, char* argv[]) {
	uint64_t limit(100000);
	
	if (argc == 2) {
		limit = static_cast<uint64_t>(atol(argv[1]));
	} else if (argc > 2) {
		std::cout << "Usage: " << argv[0] << " [number of hashes]" << std::endl;
	}
	
	std::vector<Simhash::hash_t> masks;
	
	/* This is a vector of bitmasks describing a valid permutation. This 
	 * particular permutation has 6 blocks, where each of the blocks is kept in
	 * the order that you'd expect. Changing the order of these items has the
	 * effect of reordering the blocks. */
	masks.push_back(0xFFE0000000000000);
    masks.push_back(0x1FFC0000000000  );
	masks.push_back(0x3FF80000000     );
	masks.push_back(0x7FF00000        );
	masks.push_back(0xFFE00           );
	masks.push_back(0x1FF             );
	
	/* Create a table where we're interested in matches with 3 or fewer bytes
	 * differing between two near-duplicate documents. */
	Simhash::Table duplicates(3, masks);
	
	/* We're going to insert a bunch of hashes, and then run a bunch of queries
	 * on some hashes that should be considered near duplicates and some that
	 * should not */
	std::cout << "Inserting " << limit << " hashes..." << std::endl;
	for (uint64_t i = 1; i < limit; ++i) {
		duplicates.insert(i << 28);
	}
	
	std::cout << "Running " << limit * 4 << " queries..." << std::endl;
	Simhash::hash_t query(0), first(0), errors(0);
	for (uint64_t i = 1; i < limit; ++i) {
		query   = (i << 28) | 3;
		first   = duplicates.find(query);
		errors += first ? 0 : 1;
		
		query   = (i << 28) | 9;
		first   = duplicates.find(query);
		errors += first ? 0 : 1;
		
		query   = (i << 28) | 65;
		first   = duplicates.find(query);
		errors += first ? 0 : 1;
		
		query   = (i << 28) | 15;
		first   = duplicates.find(query);
		if (first) {
			std::cout << "Found " << first << " when looking for " << query << "(" << Simhash::num_differing_bits(first, query) << ")" << std::endl;
		}
		errors += !first ? 0 : 1;
	}
	
	std::cout << "Queries complete with " << errors << " errors" << std::endl;
	
	size_t count(0);
	Simhash::Table::const_iterator it( duplicates.begin());
	Simhash::Table::const_iterator end(duplicates.end());
	for(; it != end; ++it, ++count) {}
	
	std::cout << "There are " << count << " items in the table" << std::endl;
	
	return 0;

}
Exemple #30
0
int main(int argc, char *argv[])
{
    char *user_config_dir = get_user_config_dir();
    int config_err = 0;

    f_loadfromfile = 1;
    int f_flag = 0;
    int i = 0;
    int f_use_ipv4 = 0;

    /* Make sure all written files are read/writeable only by the current user. */
    umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);

    for (i = 0; i < argc; ++i) {
        if (argv[i] == NULL)
            break;
        else if (argv[i][0] == '-') {
            if (argv[i][1] == 'f') {
                if (argv[i + 1] != NULL)
                    DATA_FILE = strdup(argv[i + 1]);
                else
                    f_flag = -1;
            } else if (argv[i][1] == 'n') {
                f_loadfromfile = 0;
            } else if (argv[i][1] == '4') {
                f_use_ipv4 = 1;
            }
        }
    }

    config_err = create_user_config_dir(user_config_dir);
    if (DATA_FILE == NULL ) {
        if (config_err) {
            DATA_FILE = strdup("data");
        } else {
            DATA_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("data") + 1);
            if (DATA_FILE != NULL) {
                strcpy(DATA_FILE, user_config_dir);
                strcat(DATA_FILE, CONFIGDIR);
                strcat(DATA_FILE, "data");     
            } else {
                endwin();
                fprintf(stderr, "malloc() failed. Aborting...\n");
                exit(EXIT_FAILURE);
            }
        }
    }

    free(user_config_dir);

    init_term();
    Tox *m = init_tox(f_use_ipv4);

    if (m == NULL) {
        endwin();
        fprintf(stderr, "Failed to initialize network. Aborting...\n");
        exit(EXIT_FAILURE);
    }

    prompt = init_windows(m);

#ifdef _SUPPORT_AUDIO 
    
    attron(COLOR_PAIR(RED) | A_BOLD);
    wprintw(prompt->window, "Starting audio...\n");
    attroff(COLOR_PAIR(RED) | A_BOLD);
    
    av = init_audio(prompt, m);
        
    if ( errors() == NoError )
        wprintw(prompt->window, "Audio started with no problems.\n");
    else /* Get error code and stuff */
        wprintw(prompt->window, "Error starting audio!\n");
    
    
#endif /* _SUPPORT_AUDIO */
    
    if (f_loadfromfile)
        load_data(m, DATA_FILE);

    if (f_flag == -1) {
        attron(COLOR_PAIR(RED) | A_BOLD);
        wprintw(prompt->window, "You passed '-f' without giving an argument.\n"
                "defaulting to 'data' for a keyfile...\n");
        attroff(COLOR_PAIR(RED) | A_BOLD);
    }

    if (config_err) {
        attron(COLOR_PAIR(RED) | A_BOLD);
        wprintw(prompt->window, "Unable to determine configuration directory.\n"
                "defaulting to 'data' for a keyfile...\n");
        attroff(COLOR_PAIR(RED) | A_BOLD);
    }

    prompt_init_statusbar(prompt, m);
    sort_friendlist_index(m);

    while (true) 
        do_toxic(m, prompt);
        
    return 0;
}