예제 #1
0
int main(int argc, char *argv[]) {
  MPI_Init(&argc, &argv);
  unsigned int dim = 10;
  std::string solver_name(rokko::parallel_dense_solver::default_solver());
  if (argc >= 2) solver_name = argv[1];
  if (argc >= 3) dim = boost::lexical_cast<unsigned int>(argv[2]);

  rokko::grid g;
  if (g.get_myrank() == 0) std::cout << "dimension = " << dim << std::endl;
  std::cout << std::flush;
  MPI_Barrier(g.get_comm());

  rokko::parallel_dense_solver solver(solver_name);
  solver.initialize(argc, argv);
  rokko::distributed_matrix<double, matrix_major> mat(dim, dim, g, solver);
  rokko::frank_matrix::generate(mat);
  mat.print();

  rokko::localized_matrix<double, matrix_major> lmat(dim, dim);
  for (int proc = 0; proc < g.get_nprocs(); ++proc) {
    rokko::gather(mat, lmat, proc);
    if (g.get_myrank() == proc) {
      std::cout << "root = " << proc << std::endl;
      std::cout << "lmat:" << std::endl << lmat << std::endl;
    }
    std::cout << std::flush;
    MPI_Barrier(g.get_comm());
  }

  solver.finalize();
  MPI_Finalize();
  return 0;
}
예제 #2
0
bool run_test(MPI_Comm comm, int dim, GRID_MAJOR const& grid_major, DIST_MAT_MAJOR const&, LOC_MAT_MAJOR const&) {
  int size, rank;
  MPI_Comm_size(comm, &size);
  MPI_Comm_rank(comm, &rank);

  // same seed for all processes
  boost::mt19937 eng;
  boost::variate_generator<boost::mt19937&, boost::uniform_real<> >
    rng(eng, boost::uniform_real<>());
  
  rokko::parallel_dense_solver solver(rokko::parallel_dense_solver::default_solver());
  rokko::grid g(comm, grid_major);
  rokko::distributed_matrix<double, DIST_MAT_MAJOR> mat(dim, dim, g, solver);
  for (int i = 0; i < dim; ++i) {
    for (int j = 0; j < dim; ++j) {
      double d = rng();
      if (mat.is_gindex(i, j)) mat.set_global(i, j, d);
    }
  }
#ifndef NDEBUG
  mat.print();
#endif
  
  int success_local = 1;
  rokko::localized_matrix<double, LOC_MAT_MAJOR> lmat(dim, dim);
  for (int r = 0; r < size; ++r) {
    rokko::gather(mat, lmat, r);
#ifndef NDEBUG
    if (rank == r) { 
      std::cout << lmat << std::endl;
      std::cout.flush();
    }
    MPI_Barrier(comm);
#endif
  }
  for (int i = 0; i < dim; ++i) {
    for (int j = 0; j < dim; ++j) {
      if (mat.is_gindex(i, j) && std::abs(mat.get_global(i, j) - lmat(i, j)) >  10e-12)
          success_local = 0;
    }
  }

  int success;
  MPI_Allreduce(&success_local, &success, 1, MPI_INT, MPI_PROD, comm);
  BOOST_CHECK_EQUAL(success, true);
  return success;
}
예제 #3
0
//checking all the sequence for the time being
McoStatus Cmysurface::findboundary(double *inlab, double *boundlab, double *boundcmy, int16* yes)
{

	double	r1,r2,r3;
	register double	a1,a2,a3,a4,a5,a6,a7,a8,a9;
	double	s1,s2,s3;
	double	determ;
	McoStatus status;
	int32 i,j,k;
	int32 total_tri;	//total # of triangles
	int32 start;
	double sdata[9];	//data of core matrix
	double rdata[3]; 	//data of right matrix
	Matrix rmat(3, 1);
	Matrix lmat(3, 1);
	Matrix s(3);
	double the1, the2, t;
	int32 two_d_offset, total_squares;
	int32 p1, p2, p3;
	double L;
	
	//assume no compression needed
	*yes = 0;
	
	//screen L value, min and max
	L = inlab[0];
	if( L > _maxgrayL){
		L = _maxgrayL - 0.1;
		*yes = 1;
	}	
	if( L < _mingrayL){
		L = _mingrayL + 0.1;	
		*yes = 1;
	}	
	
	//screen a,b value
	if( fabs(inlab[1]) < SCREEN_DELTA && fabs(inlab[2]) < SCREEN_DELTA ){
		boundlab[0] = L;
		boundlab[1] = inlab[1];
		boundlab[2] = inlab[2];
		return MCO_SUCCESS;
	}		
	
	//some optimization here to improve the speed
	//first guess which plane the point may cross
	/*
	if( inlab[1] >= 0 && inlab[2] >= 0){
		
		sq[0] = 0;
		sq[1] = 1;
		if(inlab[1] > inlab[2] ){
			sq[2] = 
	*/
	
	two_d_offset = 3*_one_d_points*_one_d_points;
	total_squares = (_one_d_points-1)*(_one_d_points-1);
	
	for(i = 0; i < 6; i++){//c=0,c=1,...
		for( j = 0; j < total_squares; j++){//squares in one plane
			for( k = 0; k <= 3; k += 3){//two triangles in one square
				start = i*two_d_offset + _start_id[j];
				
				//x0 - x3
				p3 = start + _tri_id[2+k];
				r1 = L - _slab[p3];
				r2 = -_slab[p3+1];
				r3 = -_slab[p3+2];
				//rmat.loadstruct(rdata);
				
				//core matrix
				p1 = start + _tri_id[k];
				p2 = start + _tri_id[1+k];
				
				//left col
				a1 = _slab[p1] - _slab[p3];
				a4 = _slab[p1+1] - _slab[p3+1];
				a7 = _slab[p1+2] - _slab[p3+2];
				
				//middle col
				a2 = _slab[p2] - _slab[p3];
				a5 = _slab[p2+1] - _slab[p3+1];
				a8 = _slab[p2+2] - _slab[p3+2];
				
				//right col
				a3 = 0;
				a6 = -inlab[1];
				a9 = -inlab[2];
				//s.loadstruct(sdata);
				
				determ = a1*(a5*a9 - a6*a8) - a2*(a4*a9 - a6*a7) + a3*(a4*a8 - a5*a7);
				
				if (determ == 0) return MCO_SINGULAR;
								
				s1 = (a5*a9 - a6*a8)*r1 - (a2*a9 - a3*a8)*r2 + (a2*a6 - a3*a5)*r3;
				s1 = s1/determ;
				
				s2 = - (a4*a9 - a6*a7)*r1 + (a1*a9 - a3*a7)*r2 - (a1*a6 - a3*a4)*r3;
				s2 = s2/determ;
				
				s3 = (a4*a8 - a5*a7)*r1 - (a1*a8 - a2*a7)*r2 + (a1*a5 - a2*a4)*r3;
				s3 = s3/determ;


				the1 = s1;
				the2 = s2;
				t = s3;
								
				//check where is the crossing point, or even it exists
				if( the1 >= 0 && the2 >= 0 && the1+the2 <= 1){ //cross the current triangle
					if( t > 1){ //inlab is inside the gamut
						boundlab[0] = L;
						boundlab[1] = inlab[1];
						boundlab[2] = inlab[2];
						return MCO_SUCCESS;
					}
					else if( t >= 0){ //inlab is outside the gamut, need compression
					
						//compute the crossing point
						boundlab[0] = L;
						boundlab[1] = inlab[1]*t;
						boundlab[2] = inlab[2]*t;
						*yes = 1;
						//for test only, find corresponding c,m,y value
						/*
						boundcmy[0] = _scmy[p1]*the1+_scmy[p2]*the2+_scmy[p3]*(1-the1-the2);
						boundcmy[1] = _scmy[p1+1]*the1+_scmy[p2+1]*the2+_scmy[p3+1]*(1-the1-the2);
						boundcmy[2] = _scmy[p1+2]*the1+_scmy[p2+2]*the2+_scmy[p3+2]*(1-the1-the2);
						*/
						return MCO_SUCCESS;
					}
					//else t < 0 on the reverse side of gray axis
				}	
			}	
		}
	}
	
	return MCO_FAILURE; //should change this return value
}						
예제 #4
0
//function to find the maxL and minL of gray scale
void Cmysurface::_findminmax(void)
{
	McoStatus status;
	int32 i,j,k;
	int32 total_tri;	//total # of triangles
	int32 start;
	double sdata[9];	//data of core matrix
	double rdata[3]; 	//data of right matrix
	Matrix rmat(3, 1);
	Matrix lmat(3, 1);
	Matrix s(3);
	double the1, the2, t;
	int32 two_d_offset, total_squares;
	int32 p1, p2, p3;
	double inlab[3];
	double L;
	
	//set inlab(p4 is always 100, p0 is always 0)
	inlab[0] = 100;
	inlab[1] = 0;
	inlab[2] = 0;
	
	
	_mingrayL = 100;
	_maxgrayL = 0;

		
	two_d_offset = 3*_one_d_points*_one_d_points;
	total_squares = (_one_d_points-1)*(_one_d_points-1);
	
	for(i = 0; i < 6; i++){//c=0,c=1,...
		for( j = 0; j < total_squares; j++){//squares in one plane
			for( k = 0; k <= 3; k += 3){//two triangles in one square
				start = i*two_d_offset + _start_id[j];
				
				//x0 - x3( y0 is 0 0 0
				p3 = start + _tri_id[2+k];
				rdata[0] = -_slab[p3];
				rdata[1] = -_slab[p3+1];
				rdata[2] = -_slab[p3+2];
				rmat.loadstruct(rdata);
				
				//core matrix
				p1 = start + _tri_id[k];
				p2 = start + _tri_id[1+k];
				
				//left col
				sdata[0] = _slab[p1] - _slab[p3];
				sdata[3] = _slab[p1+1] - _slab[p3+1];
				sdata[6] = _slab[p1+2] - _slab[p3+2];
				
				//middle col
				sdata[1] = _slab[p2] - _slab[p3];
				sdata[4] = _slab[p2+1] - _slab[p3+1];
				sdata[7] = _slab[p2+2] - _slab[p3+2];
				
				//right col
				sdata[2] = -inlab[0];
				sdata[5] = -inlab[1];
				sdata[8] = -inlab[2];
				s.loadstruct(sdata);
				
				//compute
				s.inv();
				if( (status = s.get_status()) == MCO_SUCCESS){ //singular or ?
					
					lmat = s*rmat;
					lmat.getval(1, 1, &the1);
					lmat.getval(2, 1, &the2);
					lmat.getval(3, 1, &t);
					
					//check where is the crossing point, or even it exists
					if( the1 >= 0 && the2 >= 0 && the1+the2 <= 1){ //cross the current triangle
						if( t >= 0 && t <= 1){
						
							L = t*inlab[0];
							if( L > _maxgrayL)
								_maxgrayL = L;
								
							if( L < _mingrayL)
								_mingrayL = L;	
						}
						//else t < 0 on the reverse side of gray axis
					}
				}		
			}	
		}
	}
	
	return;
}					
예제 #5
0
int main(int argc, char *argv[]) {
  MPI_Init(&argc, &argv);
  //typedef rokko::matrix_row_major matrix_major;
  typedef rokko::matrix_col_major matrix_major;

  if (argc <= 2) {
    std::cerr << "error: " << argv[0] << " solver_name alps_parameter_file_name" << std::endl;
    MPI_Abort(MPI_COMM_WORLD, 34);
  }

  std::string solver_name(argv[1]);
  std::ifstream  ifs(argv[2]);
  alps::Parameters params(ifs);
  std::ostream& out = std::cout;
  barista::Hamiltonian<> hamiltonian(params);
  int dim = hamiltonian.dimension();

  rokko::parallel_dense_solver solver(solver_name);
  solver.initialize(argc, argv);

  MPI_Comm comm = MPI_COMM_WORLD;
  rokko::grid g(comm, rokko::grid_col_major);
  int myrank = g.get_myrank();

  const int root = 0;

  rokko::distributed_matrix<matrix_major> mat(dim, dim, g, solver);
  hamiltonian.fill(mat);
  mat.print();

  rokko::localized_matrix<matrix_major> lmat(dim, dim);
  rokko::gather(mat, lmat, root);
  if (myrank == root)
    std::cout << "lmat:" << std::endl << lmat << std::endl;

  rokko::localized_vector w(dim);
  rokko::distributed_matrix<matrix_major> Z(dim, dim, g, solver);

  try {
    solver.diagonalize(mat, w, Z);
  }
  catch (const char *e) {
    std::cout << "Exception : " << e << std::endl;
    MPI_Abort(MPI_COMM_WORLD, 22);
  }

  // gather of eigenvectors
  rokko::localized_matrix<matrix_major> eigvec_global;
  rokko::localized_matrix<matrix_major> eigvec_sorted(dim, dim);
  rokko::localized_vector eigval_sorted(dim);
  rokko::gather(Z, eigvec_global, root);
  Z.print();
  if (myrank == root) {
    std::cout << "eigvec:" << std::endl << eigvec_global << std::endl;
  }

  std::cout.precision(20);
  /*
  std::cout << "w=" << std::endl;
  for (int i=0; i<dim; ++i) {
    std::cout << w[i] << " ";
  }
  std::cout << std::endl;
  */

  if (myrank == root) {
    rokko::sort_eigenpairs(w, eigvec_global, eigval_sorted, eigvec_sorted);
    std::cout << "Computed Eigenvalues= " << eigval_sorted.transpose() << std::endl;

    std::cout.precision(3);
    std::cout << "Check the orthogonality of Eigenvectors:" << std::endl
	 << eigvec_sorted * eigvec_sorted.transpose() << std::endl;   // Is it equal to indentity matrix?
    //<< eigvec_global.transpose() * eigvec_global << std::endl;   // Is it equal to indentity matrix?

    std::cout << "residual := A x - lambda x = " << std::endl
         << lmat * eigvec_sorted.col(1)  -  eigval_sorted(1) * eigvec_sorted.col(1) << std::endl;
    std::cout << "Are all the following values equal to some eigenvalue = " << std::endl
	 << (lmat * eigvec_sorted.col(0)).array() / eigvec_sorted.col(0).array() << std::endl;
    //cout << "lmat=" << std::endl << lmat << std::endl;
  }

  solver.finalize();
  MPI_Finalize();
  return 0;
}