Пример #1
0
int
main(int argc, const char ** argv)
{
#ifdef VISP_HAVE_LAPACK_C
  try {
    unsigned int nb_matrices=1000;
    unsigned int nb_iterations=10;
    unsigned int nb_rows = 6;
    unsigned int nb_cols = 6;
    bool verbose = false;
    std::string plotfile("plot.txt");
    bool use_plot_file=false;
    std::ofstream of;

    double t, qr_time, lu_time,pi_time,chol_time;
    // Read the command line options
    if (getOptions(argc, argv, nb_matrices,nb_iterations,use_plot_file,plotfile,nb_rows,nb_cols,verbose) == false) {
      exit (-1);
    }

    if(use_plot_file){
      of.open(plotfile.c_str());
    }

    for(unsigned int iter=0;iter<nb_iterations;iter++){
      std::vector<vpMatrix> benchQR;
      std::vector<vpMatrix> benchLU;
      std::vector<vpMatrix> benchCholesky;
      std::vector<vpMatrix> benchPseudoInverse;
      if(verbose)
        std::cout << "********* generating matrices for iteration " << iter << "." << std::endl;
      for(unsigned int i=0;i<nb_matrices;i++){
        vpMatrix cur;
        double det=0.;
        //don't put singular matrices in the benchmark
        for(cur=makeRandomMatrix(nb_rows,nb_cols);std::abs(det=cur.AtA().det())<.01;cur = makeRandomMatrix(nb_rows,nb_cols))
          if(verbose){
            std::cout << "Generated random matrix A*tA=" << std::endl << cur.AtA() << std::endl;
            std::cout << "generated random matrix not invertibleL: det="<<det<< ". Retrying..." << std::endl;
          }
        benchCholesky.push_back(cur);
        benchQR.push_back(cur);
        benchLU.push_back(cur);
        benchPseudoInverse.push_back(cur);
      }

      if(verbose)
        std::cout << "\t Inverting " << benchCholesky[0].AtA().getRows() << "x" << benchCholesky[0].AtA().getCols() << " matrix using cholesky decomposition." << std::endl;
      t = vpTime::measureTimeMs() ;
      for(unsigned int i=0;i<nb_matrices;i++){
        benchCholesky[i]=benchCholesky[i].AtA().inverseByCholesky()*benchCholesky[i].transpose();
      }
      chol_time = vpTime::measureTimeMs() - t ;

      if(verbose)
        std::cout << "\t Inverting " << benchLU[0].AtA().getRows() << "x" << benchLU[0].AtA().getCols() << " matrix using LU decomposition." << std::endl;
      t = vpTime::measureTimeMs() ;
      for(unsigned int i=0;i<nb_matrices;i++)
        benchLU[i] = benchLU[i].AtA().inverseByLU()*benchLU[i].transpose();
      lu_time = vpTime::measureTimeMs() -t ;

      if(verbose)
        std::cout << "\t Inverting " << benchQR[0].AtA().getRows() << "x" << benchQR[0].AtA().getCols() << " matrix using QR decomposition." << std::endl;
      t = vpTime::measureTimeMs() ;
      for(unsigned int i=0;i<nb_matrices;i++){
        benchQR[i]=benchQR[i].AtA().inverseByQR()*benchQR[i].transpose();
      }
      qr_time = vpTime::measureTimeMs() - t ;

      if(verbose)
        std::cout << "\t Inverting " << benchPseudoInverse[0].AtA().getRows() << "x" << benchPseudoInverse[0].AtA().getCols() << " matrix while computing pseudo-inverse." << std::endl;
      t = vpTime::measureTimeMs() ;
      for(unsigned int i=0;i<nb_matrices;i++){
        benchPseudoInverse[i]=benchPseudoInverse[i].pseudoInverse();
      }
      pi_time = vpTime::measureTimeMs() - t ;

      double avg_err_lu_qr=0.;
      double avg_err_lu_pi=0.;
      double avg_err_lu_chol=0.;
      double avg_err_qr_pi=0.;
      double avg_err_qr_chol=0.;
      double avg_err_pi_chol=0.;

      for(unsigned int i=0;i<nb_matrices;i++){
        avg_err_lu_qr+= (benchQR[i]-benchLU[i]).euclideanNorm();
        avg_err_lu_pi+= (benchPseudoInverse[i]-benchLU[i]).euclideanNorm();
        avg_err_qr_pi+= (benchPseudoInverse[i]-benchQR[i]).euclideanNorm();
        avg_err_qr_chol+= (benchCholesky[i]-benchQR[i]).euclideanNorm();
        avg_err_lu_chol+= (benchCholesky[i]-benchLU[i]).euclideanNorm();
        avg_err_pi_chol+= (benchCholesky[i]-benchPseudoInverse[i]).euclideanNorm();
      }

      avg_err_lu_qr/=nb_matrices;
      avg_err_lu_pi/=nb_matrices;
      avg_err_qr_pi/=nb_matrices;

      if(use_plot_file){
        of << iter << "\t" << lu_time << "\t" << qr_time << "\t" << pi_time << "\t" << chol_time << "\t" << avg_err_lu_qr << "\t" << avg_err_qr_pi << "\t" << avg_err_lu_pi << "\t" << avg_err_qr_chol << "\t" << avg_err_lu_chol << "\t" <<  avg_err_pi_chol << std::endl;
      }
      if(verbose || !use_plot_file){
        writeTime("LU",lu_time);
        writeTime("QR",qr_time);
        writeTime("Pseudo-inverse",pi_time);
        writeTime("Cholesky",chol_time);
      }
    }
    return 0;
  }
  catch(vpException e) {
    std::cout << "Catch an exception: " << e << std::endl;
    return 1;
  }

#else
  (void)argc;
  (void)argv;
  std::cout << "You don't have lapack installed" << std::endl;
  return 0;
#endif
}
Пример #2
0
static void go_ray(struct initializations &init, Spectrum &spec,
		Metric &metric, Particle &particle, EMField &emfield,
		string &plotfilename)
{
	myfloat dtau = init.dtau;

	ofstream plotfile(plotfilename.c_str());
	ofstream dtaufile("globaldtau.dat");
	ofstream wrongfile("globalwrong.dat");
	plotfile << scientific;
	dtaufile << scientific;
	wrongfile << scientific;

	// Print important information to plotfile
	plotfile << "# Metric: " << metric.name << endl;
	plotfile << "#	m = " << metric.m << endl
		<< "#	a = " << metric.a << endl
		<< "#	q = " << metric.q << endl;
	plotfile << "# particle:" << endl
		<< "#	m = " << init.teilchen_masse << endl
		<< "#	q = " << init.teilchen_ladung << endl;
	plotfile << "# length_4velocity = "
		<< scalar(metric, particle.x, particle.u, particle.u)
		<< endl
		<< "# dtau = " << dtau << endl
		<< "# tau_max = " << init.tau_max << endl
		<< "# max_wrongness = " << init.max_wrongness << endl
		<< "# max_x1 = " << init.max_x1 << endl;
	plotfile << "# x3 x1 x2 x0	wrong	u3 u1 u2 u0" << endl;

	myfloat wrong = wrongness(metric, &init, particle.x,
			particle.u);
	myfloat wrong_old = wrong;

	// Iterate
	int absorb_last = 0;
	int taun = -1;
	myfloat tau = 0.0;
	while ((absol(wrong) <= init.max_wrongness)
		&& (tau <= init.tau_max + dtau))
	{
		dtau = init.dtau * pow(absol(particle.x[1] - 2 * init.m), sqrt(3.0));

		if (++taun % 256 == 0) {
			info(metric, taun, tau, dtau, wrong, particle);
			if (taun >= 10000) {
				cerr << "WARNING: Aborting prematurely: taking too long" << endl;
				break;
			}
		}

		// print to file
		plotfile<< particle.x[3] << ' '
			<< particle.x[1] << ' '
			<< particle.x[2] << ' '
			<< particle.x[0] << '\t'
			<< wrong	 << '\t'
			<< particle.u[3] << ' '
		        << particle.u[1] << ' '
		        << particle.u[2] << ' '
		        << particle.u[0] << '\t'
			<< tau << '\t'
			<< dtau
			<< endl;

		dtaufile << tau << "\t" << dtau << endl;
		wrongfile << tau << "\t" << wrong << endl;

		tau += dtau;


		// Berechne x, u
		x_and_u(metric, emfield, dtau, particle);

		// Update spectrum
		absorb += spec.inc_cnts(particle.x, particle.u);

		wrong_old = wrong;
		wrong = wrongness(metric, &init, particle.x, particle.u);

		if (absorb) {
			if (absorb >= absorb_max)
				break;
			if (absorb == absorb_last)
				// error, currently
				break;
			absorb_last = absorb;
		}
		if ((particle.x[1] > init.max_x1)
				|| (particle.x[1] < init.min_x1))
			break;
	}
	plotfile << endl;
	plotfile << "# length_4velocity = "
		<< scalar(metric, particle.x, particle.u, particle.u)
		<< endl;

	plotfile.close();
	dtaufile.close();
	wrongfile.close();

	info(metric, taun, tau, dtau, wrong, particle);

	append_file(taun);
}