int showTable(const vector<vector<char>>& record, const vector<int>& type, const vector<string>& attr_show){
	vector<vector<string>> table;
	vector<string> column;
	vector<int> maxSize;
	int size;
	//record确定行数,type&attr_show确定列数
	int n = 0;
	for (int i = 0; i < attr_show.size(); i++){
		column.clear();
		column.push_back(attr_show[i]);
		size = attr_show[i].size();

		for (int j = 0; j < record.size(); j++){
			string t = "";
			if (type[i] == 1){//int
				int x = char4ToInt(subVector(record[j], n, n + 4));
				stringstream ss;
				ss << x;
				t = ss.str();
			}
			else if (type[i] == 2){//float
				float x = char4ToFloat(subVector(record[j], n, n + 4));
				stringstream ss;
				ss << x;
				t = ss.str();
			}
			else{
				vector<char> vt = subVector(record[j], n, n + type[i] - 3);
				for (int k = 0; k < vt.size(); k++){
					if (vt[k])
						t += vt[k];
				}
			}

			if (t.size()>size){
				size = t.size();
			}
			column.push_back(t);
		}
		table.push_back(column);
		maxSize.push_back(size);
		if (type[i] == 1 || type[i] == 2)
			n += 4;
		else
			n += (type[i] - 3);
	}

	drawTable(table, maxSize);

	return record.size();
}
Exemple #2
0
vector velToGhost(BALL ghost,vector currentPos)
{
	vector v;
	vector vfinal;
	vector distance;
	float dot;
	float tempmod;
	char isCorrect;
	int i;
	
	distance = subVector(ghost.pos , currentPos);
	if(modValue(distance) == 0)
	{
		return ghost.vel;
	}
	
	dot = dotProduct(distance,ghost.vel)/(modValue(distance)*modValue(ghost.vel));
	if(dot <= 0)
	{
		v.x =0;
		v.y =0;
		v.z =0;
		return v;
	}
	isCorrect =1;
	for(i =1;i<NOOFCOINS;i++)
	{
		if(distanceLinePoint(distance,currentPos,coins[i].pos) < coins[i].radius + coins[0].radius)
		{
			if(dotProduct(subVector(coins[i].pos,currentPos),distance)*dotProduct(subVector(coins[i].pos,ghost.pos),distance) < 0)
			{
				isCorrect = 0;
			}
		
		}
	}
	if(isCorrect == 0)
	{
		v.x =0;
		v.y =0;
		v.z =0;
		return v;
	}
	tempmod = modValue(ghost.vel)/dot;
	tempmod = sqrt(tempmod*tempmod + 2*GRAV*COEF_OF_FRIC*modValue(distance));
	v = multConstant(distance , (1.1)*tempmod/modValue(distance));//1.1 is safety factor
	return v;
}
Exemple #3
0
 /* --- Return the rectangle produced by the overlap
        of two other rectangles ---- */
RECT subRectangle(RECT r1, RECT r2)
{
    RECT r = {0,0,0,0};
    subVector((int *) &RectLeft(r), (int *) &RectRight(r),
        RectLeft(r1), RectRight(r1),
        RectLeft(r2), RectRight(r2));
    subVector((int *) &RectTop(r), (int *) &RectBottom(r),
        RectTop(r1), RectBottom(r1),
        RectTop(r2), RectBottom(r2));
    if (RectRight(r) == -1 || RectTop(r) == -1)
        RectRight(r) =
        RectLeft(r) =
        RectTop(r) =
        RectBottom(r) = 0;
    return r;
}
Exemple #4
0
vector findValidPosition(float Radius,BALL coins[],BALL wall[],BALL pocket[])
{
	char isCorrect = 0;
	vector v;
	v.z =0;
	float y;
	int i;
	while(isCorrect == 0)
	{
		isCorrect = 1;
		v.x = (random()%100)/100.0;
		v.y = (random()%100)/100.0;
		for(i= 0 ;i<10;i++)
		{
			 y = modValue(subVector(v, coins[i].pos));
			 if(y < Radius + coins[i].radius)
			 {
			 	isCorrect = 0;
			 }
		}
		if(v.x - wall[0].pos.x < Radius )
		{				
			isCorrect =0;
		}
		if(wall[1].pos.x - v.x  < Radius )
		{				
			isCorrect =0;
		}
		if(v.y - wall[2].pos.y < Radius )
		{				
			isCorrect =0;
		}
		if(wall[3].pos.y - v.y  < Radius )
		{				
			isCorrect =0;
		}
		for(i = 0;i<4;i++)
		{
			y = modValue(subVector(v, pocket[i].pos));
			if(y < pocket[i].radius)
			{
				isCorrect = 0;
			}
		}			
	}
	return v;
}
Exemple #5
0
// Ray and Sphere Intersection---------------------------------------------------------------------
float raySphereIntersection(Ray ray, SPHERE sphere, Point3dPtr n1, Point3dPtr interp1, float* kd1)
{
	float t;
	float radius;
	Point3d center;
	Point3dPtr S;
	S = (Point3dPtr)malloc(sizeof(Point3d));
	*kd1 = sphere.kd;

	radius = sphere.radius;
	center.x = sphere.x;
	center.y = sphere.y;
	center.z = sphere.z;
	subVector(&center, ray.a, S);	//S = Sphere Center Translated into Coordinate Frame of Ray Origin

	//Intersection of Sphere and Line     =       Quadratic Function of Distance 
	// A ray is defined by: R(t) = R0 + t * Rd , t > 0 with R0 = [X0, Y0, Z0] and Rd = [Xd, Yd, Zd]
	float A = dotProduct(ray.b, ray.b);		//A = Xd^2 + Yd^2 + Zd^2
	float B = -2.0*dotProduct(S, ray.b);		//B = 2 * (Xd * (X0 - Xc) + Yd * (Y0 - Yc) + Zd * (Z0 - Zc))
	float C = dotProduct(S, S) - radius*radius;	//C = (X0 - Xc)^2 + (Y0 - Yc)^2 + (Z0 - Zc)^2 - Sr^2
	float D = B*B - 4 * A*C;					//Precompute Discriminant

	if (D >= 0.0)
	{
		// if there is a shorter one just use this one, if not use another(longer one).
		int sign = (C < 0.0) ? 1 : -1;
		t = (-B + sign*sqrt(D)) / 2.f; // A should be equal to 1

		// The surface normal
		n1->x = (ray.a->x + ray.b->x*t - center.x) / radius;
		n1->y = (ray.a->y + ray.b->y*t - center.y) / radius;
		n1->z = (ray.a->z + ray.b->z*t - center.z) / radius;

		// The intersection point
		interp1->x = ray.a->x + ray.b->x*t;
		interp1->y = ray.a->y + ray.b->y*t;
		interp1->z = ray.a->z + ray.b->z*t;

		return t;	//The distance
	}
	return 0.0;
	free(S);
}
Exemple #6
0
int main(int argc, char* argv[])
{
  // Initialization of the network, the communicator and the allocation of
  // the GPU is done as in previous tutorials.
  agile::NetworkEnvironment environment(argc, argv);
  typedef agile::GPUCommunicator<unsigned, float, float> communicator_type;
  communicator_type com;
  com.allocateGPU();
  agile::GPUEnvironment::printInformation(std::cout);
  std::cout << std::endl;

  // We are interested in solving the linear problem \f$ Ax = y \f$, with a
  // given matrix \f$ A \f$ and a right-hand side vector \f$ y \f$. The unknown
  // is the vector \f$ x \f$.
  // Now, we can generate a matrix that shall be inverted (actually we do not
  // invert the matrix but use the CG algorithm). Note that CG requires a
  // symmetric positive definite (SPD) matrix and it is not too trivial to
  // write down a SPD matrix. If you fail to provide a SPD matrix to the CG
  // algorithm there is no guarantee that it will converge. You might be lucky,
  // you might be not...
  const unsigned SIZE = 20;
  float A_host[SIZE][SIZE];
  for (unsigned row = 0; row < SIZE; ++row)
    for (unsigned column = 0; column <= row; ++column)
    {
      A_host[row][column] = (float(SIZE) - float(row) + float(SIZE) / 2.0)
                            * (float(column) + 1.0);
      A_host[column][row] = A_host[row][column];
      if (row == column)
        A_host[row][column] = 2.0 * float(SIZE) + float(row) + float(column);
    }

  // The matrix is still in the host's memory and has to be transfered to the
  // GPU. This is done automatically by the constructor of \p GPUMatrixPitched.
  agile::GPUMatrixPitched<float> A(SIZE, SIZE, (float*)A_host);

  // Next we need a reference solution. We can create any vector we like at
  // this place.
  std::vector<float> x_reference_host(SIZE);
  for (unsigned counter = 0; counter < SIZE; ++counter)
    x_reference_host[counter] = float(SIZE) - float(counter) + float(SIZE/3);

  // This vector has to be transfered to the GPU memory too. For vectors, this
  // can be achieved by the member function \p assignFromHost.
  agile::GPUVector<float> x_reference;
  x_reference.assignFromHost(x_reference_host.begin(), x_reference_host.end());

  // We wrap the GPU matrix from above into a forward operator called
  // \p ForwardMatrix. Forward operators are simply objects that implement
  // the parenthesis-operator \p operator() which takes an
  // \p accumulated vector and returns a \p distributed one. In all other
  // respects the operator is a black box for us.
  // The \p ForwardMatrix operator requires a reference to the communicator
  // when constructing the object so that it has access to the network.
  typedef agile::ForwardMatrix<communicator_type, agile::GPUMatrixPitched<float> >
    forward_type;
  forward_type forward(com, A);

  // What we also want to use a preconditioner, which means that we change from
  // the original problem \f$ Ax = y \f$ to the equivalent one
  // \f$ PAx = Py \f$, where \f$ P \f$ is a preconditioner. The rationale is
  // that most often the matrix \f$ A \f$ is ill-conditioned and the CG algorithm
  // does not converge properly at all or it needs many iterations. The use of
  // a preconditioner makes the whole system better conditioned. The simplest
  // choice is to use the identity \f$ P = I \f$ (which means no preconditioning
  // at all). The best choice would be \f$ P = A^{-1} \f$ as we would have the
  // solution for \f$ x \f$ in the first step already (but then we need again
  // to find the inverse of \f$ A \f$ which we wanted to avoid). An
  // 'intermediate' possibility is to take \f$ P = diag(A)^{-1} \f$ which is
  // easy and fast to invert and gives better results than the identity.
  // A preconditioner belongs to the inverse operator. All inverse operators
  // implement a parenthesis-operator which takes a \p distributed vector
  // as input and returns an \p accumulated one (opposite to the forward
  // operators, thus).
#if JACOBI_PRECONDITIONER
  typedef agile::JacobiPreconditioner<communicator_type, float>
    preconditioner_type;
  std::vector<float> diagonal(SIZE);
  for (unsigned row = 0; row < SIZE; ++row)
    diagonal[row] = A_host[row][row];
  preconditioner_type preconditioner(com, diagonal);
#else
  typedef agile::InverseIdentity<communicator_type> preconditioner_type;
  preconditioner_type preconditioner(com);
#endif

  // The last operator needed is a measure. A measure operator has again
  // a parenthesis-operator. This timeis takes an \p accumulated vector as first
  // input and a \p distributed one as second input and returns a scalar
  // measuring somehow the size of the vectors. An example is the scalar
  // product operator.
  typedef agile::ScalarProductMeasure<communicator_type> measure_type;
  measure_type scalar_product(com);

  // Finally, generate the PCG solver. It needs the absolute and relative
  // tolerances as input so that it knows when the solution is good enough for
  // our purposes. Furthermore it requires the maximum amount of iterations
  // after which it simply capitulates without having found a solution.
  const double REL_TOLERANCE = 1e-12;
  const double ABS_TOLERANCE = 1e-6;
  const unsigned MAX_ITERATIONS = 100;
  agile::PreconditionedConjugateGradient<communicator_type, forward_type,
                                           preconditioner_type, measure_type>
    pcg(com, forward, preconditioner, scalar_product,
        REL_TOLERANCE, ABS_TOLERANCE, MAX_ITERATIONS);

  // What we have not generated, yet, is the right hand side \f$ y \f$. This is
  // simply one call to our forward operator.
  agile::GPUVector<float> y(SIZE);
  forward(x_reference, y);

  // We need one more vector to hold the result of the CG algorithm. Note that
  // we also supply the initial guess for the solution via this vector.
  agile::GPUVector<float> x(SIZE);

  // Finally, we have constructed, initialized, wrapped... everything. The only
  // thing left to do is to call the CG operator.
  pcg(y, x);

  // Print some statistics (and hope that the operator actually converged).
  if (pcg.convergence())
    std::cout << "CG converged in ";
  else
    std::cout << "Error: CG did not converge in ";
  std::cout << pcg.getIteration() + 1 << " iterations." << std::endl;
  std::cout << "Initial residual    = " << pcg.getRho0() << std::endl;
  std::cout << "Final residual      = " << pcg.getRho() << std::endl;
  std::cout << "Ratio rho_k / rho_0 = " << pcg.getRho() / pcg.getRho0()
            << std::endl;

  // As the vectors in this example were quite small we can even print them to
  // standard output.
  std::cout << "Reference: " << std::endl << "  ";
  for (unsigned counter = 0; counter < x_reference_host.size(); ++counter)
    std::cout << x_reference_host[counter] << " ";
  std::cout << std::endl;

  // The solution is still on the GPU and has to be transfered to the CPU memory.
  // This is accomplished using \p copyToHost.
  std::vector<float> x_host;
  x.copyToHost(x_host);

  // Output the solution, too.
  std::cout << "CG solution: " << std::endl << "  ";
  for (unsigned counter = 0; counter < x_host.size(); ++counter)
    std::cout << x_host[counter] << " ";
  std::cout << std::endl;

  // Finally, we also compute the difference between the reference solution and
  // the true solution (of course, we do this on the GPU).
  agile::GPUVector<float> difference(SIZE);
  subVector(x_reference, x, difference);

  // To measure the distance, we use the scalar product measure we have
  // introduced above. Note, that this operator wants the first vector in
  // accumulated format and the second one in distributed format. The solution
  // we got from the CG algorithm is accumulated (because CG is an inverse
  // operator). This means, we have to distribute the solution to have mixed
  // formats.
  agile::GPUVector<float> difference_dist(difference);
  com.distribute(difference_dist);
  std::cout << "L2 of difference: "
            << std::sqrt(std::abs(scalar_product(difference, difference_dist)))
            << std::endl;

  // So, that's it.
  return 0;
}
Exemple #7
0
vector findValidCentralPosition(float Radius,BALL coins[],BALL wall[],BALL pocket[])
{
	char isCorrect = 0;
	vector v;
	v.z =0;
	v.x = 0;
	v.y = 0;
	float y;
	int i;
	char isNotCorrectCount = 0;
	while(isCorrect == 0)
	{
		isCorrect = 1;
		
		for(i= 0 ;i<6;i++)
		{
			 y = modValue(subVector(v, coins[i].pos));
			 if(y < Radius + coins[i].radius)
			 {
			 	isCorrect = 0;
			 }
		}
		if(v.x - wall[0].pos.x < Radius )
		{				
			isCorrect =0;
		}
		if(wall[1].pos.x - v.x  < Radius )
		{				
			isCorrect =0;
		}
		if(v.y - wall[2].pos.y < Radius )
		{				
			isCorrect =0;
		}
		if(wall[3].pos.y - v.y  < Radius )
		{				
			isCorrect =0;
		}
		for(i = 0;i<4;i++)
		{
			y = modValue(subVector(v, pocket[i].pos));
			if(y < pocket[i].radius)
			{
				isCorrect = 0;
			}
		}
		if(isCorrect == 0)
		{
		
			if(isNotCorrectCount == 0)
			{
				v.x = -1*v.x;
				isNotCorrectCount++;
			}
			if(isNotCorrectCount == 1)
			{
				v.y = -1*v.y;
				isNotCorrectCount++;
			}
			if(isNotCorrectCount == 2)
			{
				v.x = -1*v.x;
				isNotCorrectCount++;
			}
			if(isNotCorrectCount == 3)
			{
				isNotCorrectCount =0;
				v.y = -1*v.y;
				v.y +=Radius;
				v.x +=Radius;
			}
		}		
	}
	return v;
}
Exemple #8
0
void ghostBall(BALL target[],int targetcoin,BALL coins[],BALL pocket[])
{
	bzero(target,4*sizeof(BALL));
	vector v1;
	vector vball;
	int i;
	int j;
	char isCorrect = 0;
	float y;
	
	for(i=0;i<4;i++)
	{
		isCorrect = 1;
		v1 =subVector(pocket[i].pos,coins[targetcoin].pos);
		for(j=1;j<NOOFCOINS;j++)
		{
			if(j == targetcoin)
		 	{
				continue;
			}
			
			if(pocket[i].pos.x>coins[targetcoin].pos.x && pocket[i].pos.y >coins[targetcoin].pos.y)
			{
				if(coins[j].pos.x > coins[targetcoin].pos.x && pocket[j].pos.y >coins[targetcoin].pos.y)
				{
					y = distanceLinePoint(v1,coins[targetcoin].pos,coins[j].pos);
					if(y < coins[targetcoin].radius + coins[j].radius)
					{
						isCorrect = 0;
						
					}					
				}
			}
			if(pocket[i].pos.x<coins[targetcoin].pos.x && pocket[i].pos.y >coins[targetcoin].pos.y)
			{
				if(coins[j].pos.x < coins[targetcoin].pos.x && pocket[j].pos.y >coins[targetcoin].pos.y)
				{
					y = distanceLinePoint(v1,coins[targetcoin].pos,coins[j].pos);
					if(y < coins[targetcoin].radius + coins[j].radius)
					{
						isCorrect = 0;
						
					}					
				}
			}
			if(pocket[i].pos.x>coins[targetcoin].pos.x && pocket[i].pos.y <coins[targetcoin].pos.y)
			{
				if(coins[j].pos.x > coins[targetcoin].pos.x && pocket[j].pos.y <coins[targetcoin].pos.y)
				{
					y = distanceLinePoint(v1,coins[targetcoin].pos,coins[j].pos);
					if(y < coins[targetcoin].radius + coins[j].radius)
					{
						isCorrect = 0;
						
					}					
				}
			}
			if(pocket[i].pos.x<coins[targetcoin].pos.x && pocket[i].pos.y <coins[targetcoin].pos.y)
			{
				if(coins[j].pos.x < coins[targetcoin].pos.x && pocket[j].pos.y <coins[targetcoin].pos.y)
				{
					y = distanceLinePoint(v1,coins[targetcoin].pos,coins[j].pos);
					if(y < coins[targetcoin].radius + coins[j].radius)
					{
						isCorrect = 0;
						
					}					
				}
			}
		}
		if(isCorrect == 0)
		{
			target[i].pos.x =9999;
			target[i].pos.y = 9999;
			target[i].pos.z = 9999;
		
		}
		else
		{
			target[i].pos = subVector(coins[targetcoin].pos,multConstant(v1,(coins[targetcoin].radius +coins[0].radius)/modValue(v1)));
			vball = multConstant(v1,sqrt(2*(GRAV*COEF_OF_FRIC)/modValue(v1)));
			target[i].vel = multConstant(vball,(2*coins[targetcoin].mass + coins[0].mass)/(coins[0].mass*(1+COEF_OF_REST)));//1.1 here is the correction factor
		}
	}
}
Exemple #9
0
BALL aiHunter(int targetcoin)
{
	vector posprev;
	vector mostfitpos;
	vector mostfitvel;
	vector distance;
	float maxfitness = 0;
	float fitness = 0;
	BALL b;
	bzero(&b,sizeof(BALL));
	b.pos.z =0;
	BALL target[4];
	int i;
	char foundOne = 0;
	ghostBall(target,targetcoin,coins,pocket);
	char goneOneWay =0;
	while(1)
	{
		for(i= 0;i<4;i++)	
		{
			b.pos.x = coins[0].pos.x;
			b.pos.y = coins[0].pos.y;
			if(target[i].pos.x == 9999 && target[i].pos.y == 9999)
			{
				continue;
			}
			b.vel = velToGhost(target[i],b.pos);
			if(modValue(b.vel) > VEL_MAX)
			{
				//printf("GOING BEYOND GAME VEL");
			}
			if(b.vel.x == 0 && b.vel.y == 0)
			{
				continue;
			}
			distance = subVector(coins[targetcoin].pos,coins[0].pos);
			fitness = dotProduct(distance,b.vel);///(modValue(distance)*modValue(b.vel));
			fitness = fitness/(modValue(distance)*modValue(b.vel)*modValue(b.vel));
			if(fitness < 0)
			{
				fitness = -1*fitness;
			}
			if(fitness > maxfitness)
			{
				maxfitness = fitness;
				foundOne = 1;
				mostfitpos.x = b.pos.x;
				mostfitpos.y = b.pos.y;
				mostfitvel.x = b.vel.x;
				mostfitvel.y = b.vel.y;
			}
		}
		posprev.x = coins[0].pos.x;
		posprev.y = coins[0].pos.y;
		if(goneOneWay == 0)
		{
			//printf("BEEN HERE AT LEAST THAT MANY TIMES\n");
			handleKeypress('l',1,1);
		}
		if(goneOneWay == 1)
		{
			//printf("BEEN HERE AT LEAST THAT HOW MANY TIMES\n");
			handleKeypress('j',1,1);
		}
		if(posprev.x == coins[0].pos.x && posprev.y == coins[0].pos.y)
		{
			if(goneOneWay == 0)
			{
				goneOneWay =1;
			}
			else
			{
				break;
			}
		}
	}
	if(foundOne == 1)
	{
		//printf("MOST FIT POS : %f %f \n",mostfitpos.x,mostfitpos.y);
		b.pos = mostfitpos;
		b.vel = mostfitvel;
	}
	return b;
}
Exemple #10
0
float distanceLinePoint(vector vline,vector pointOnLine , vector targetPoint)
{	
	return (modValue(crossVector(subVector(targetPoint,pointOnLine),multConstant(vline,1/modValue(vline)))));
}
int main (int argc, char **argv) 
{
    /* Matrix data. */

   CRS_MATRIX *M1, *M2;
   double *b1, *x1, *b2, *x2, *r;
   int n;
   int nvals;

   FILE *fp;

   if (argc == 1) {
     fp = stdin;
   }
   else if (argc == 2) {
     fp = fopen (*argv, "r");
   }
   else {
     fprintf (stderr, "Usage: pardisoTestExample [<fileName>]\n");
     exit (1);
   }

   M1 = readCRSMatrix (fp, /*symmetric=*/1);
   n = M1->nrows;
   b1 = readVector (fp, n);
   x1 = (double*)malloc(n*sizeof(double));
   r = (double*)malloc(n*sizeof(double));

   // ja -> M1->colIdxs;
   // a -> M1->vals;
   // ia -> M1->rowOffs

   int      mtype = -2;        /* Real symmetric matrix */
   int      nrhs = 1;          /* Number of right hand sides. */

   /* Internal solver memory pointer pt,                  */
   /* 32-bit: int pt[64]; 64-bit: long int pt[64]         */
   /* or void *pt[64] should be OK on both architectures  */ 
   void    *pt[64]; 

   /* Pardiso control parameters. */
   int      iparm[64];
   int      maxfct, mnum, phase, error, msglvl;

   /* Number of processors. */
   int      num_procs;

   /* Auxiliary variables. */
   char    *var;
   int      i;

   double   ddum;              /* Double dummy */
   int      idum;              /* Integer dummy. */

   double t0, t1;
   
/* -------------------------------------------------------------------- */
/* ..  Setup Pardiso control parameters.                                */
/* -------------------------------------------------------------------- */

   error = 0;
   //solver = 0; /* use sparse direct solver */
   // do we need this, or will init take care of it?
   for (i=0; i<64; i++) {
     pt[i] = (void*)0;
   }
   for (i=0; i<64; i++) {
     iparm[i] = 0;
   }
   iparm[0] = 1; /* Don't use solver default values */
   iparm[1] = 3; /* Fill-in reordering from OpenMP METIS */
   /* Numbers of processors; if 0, defaults to max number or MKL_NUM_THREADS */
   iparm[2] = 0;
   iparm[3] = 0; /* No iterative-direct algorithm */
   iparm[4] = 0; /* No user fill-in reducing permutation */
   iparm[5] = 0; /* Write solution into x */
   iparm[6] = 0; /* Not in use */
   iparm[7] = 0; /* Max numbers of iterative refinement steps */
   iparm[8] = 0; /* Not in use */
   iparm[9] = 13; /* Perturb the pivot elements with 1E-13 */
   iparm[10] = 1; /* Use nonsymmetric permutation and scaling MPS */
   iparm[11] = 0; /* Not in use */
   iparm[12] = 0; /* Maximum weighted matching algorithm is switched-off (default for symmetric). Try iparm[12] = 1 in case of inappropriate accuracy */
   iparm[13] = 0; /* Output: Number of perturbed pivots */
   iparm[14] = 0; /* Not in use */
   iparm[15] = 0; /* Not in use */
   iparm[16] = 0; /* Not in use */
   iparm[17] = -1; /* Output: Number of nonzeros in the factor LU */
   iparm[18] = -1; /* Output: Mflops for LU factorization */
   iparm[19] = 0; /* Output: Numbers of CG Iterations */
   iparm[20] = 1; /* use 1x1 and 2x2 pivoting

   //F77_FUNC(pardisoinit) (pt,  &mtype, &solver, iparm, &error); 

   if (error != 0) 
    {
      if (error == -10 )
	 printf("No license file found \n");
      if (error == -11 )
	 printf("License is expired \n");
      if (error == -12 )
	 printf("Wrong username or hostname \n");
      return 1; 
    }
   else
    { printf("PARDISO license check was successful ... \n");
    }

   /* Numbers of processors, value of OMP_NUM_THREADS */
   var = getenv("OMP_NUM_THREADS");
   if(var != NULL)
      sscanf( var, "%d", &num_procs );
   else {
     printf("Set environment OMP_NUM_THREADS to 1");
     exit(1);
   }
   iparm[2]  = num_procs;
   
   // other special settings
   iparm[1] = 3; // 2 for metis, 0 for AMD
   iparm[9] = 12;
   iparm[10] = 1;
   iparm[12] = 1; // setting this to 2 can cause errors sometimes
   
   maxfct = 1;		/* Maximum number of numerical factorizations.  */
   mnum   = 1;         /* Which factorization to use. */
    
   msglvl = 0;         /* Print statistical information  */
   error  = 0;         /* Initialize error flag */

/* -------------------------------------------------------------------- */
/* ..  Reordering and Symbolic Factorization.  This step also allocates */
/*     all memory that is necessary for the factorization.              */
/* -------------------------------------------------------------------- */
   phase = 11; 

   t0 = currentTimeUsec();
   PARDISO (pt, &maxfct, &mnum, &mtype, &phase,
		      &n, M1->vals, M1->rowOffs, M1->colIdxs, &idum, &nrhs,
		      iparm, &msglvl, &ddum, &ddum, &error);
   t1 = currentTimeUsec();
    
   if (error != 0) {
     printf("ERROR during symbolic factorization: %d\n", error);
     exit(1);
   }
   printf("Analyze: msec=%8.1f\n", (t1-t0)/1000.0);
   printf("Number of nonzeros in factors  = %d\n", iparm[17]);
   printf("Number of factorization MFLOPS = %d\n", iparm[18]);
   
/* -------------------------------------------------------------------- */
/* ..  Numerical factorization.                                         */
/* -------------------------------------------------------------------- */    
   phase = 22;

   t0 = currentTimeUsec();
   PARDISO (pt, &maxfct, &mnum, &mtype, &phase,
		      &n, M1->vals, M1->rowOffs, M1->colIdxs, &idum, &nrhs,
		      iparm, &msglvl, &ddum, &ddum, &error);
   t1 = currentTimeUsec();   

   if (error != 0) {
     printf("ERROR during numerical factorization: %d\n", error);
     exit(2);
   }
   printf("Factor:  msec=%8.1f\n", (t1-t0)/1000.0);

/* -------------------------------------------------------------------- */    
/* ..  Back substitution and iterative refinement.                      */
/* -------------------------------------------------------------------- */    
   phase = 33;

   iparm[7] = 1;       /* Max numbers of iterative refinement steps. */

   t0 = currentTimeUsec();
   PARDISO (pt, &maxfct, &mnum, &mtype, &phase,
		      &n, M1->vals, M1->rowOffs, M1->colIdxs, &idum, &nrhs,
		      iparm, &msglvl, b1, x1, &error);
   t1 = currentTimeUsec();   
   if (error != 0) {
     printf("ERROR during solution: %d\n", error);
     exit(3);
   }

   mulVector (r, M1, x1);
   subVector (r, r, b1, n);
   printf("Solve:   msec=%8.1f\n\n", (t1-t0)/1000.0);
   /* for (i=0; i<n; i++) { */
   /*   printf ("%g ", x1[i]); */
   /* } */
   /* printf ("\n"); */
   printf("residual=%g\n", normVector(r, n));

#if 0
   M2 = readCRSMatrix (fp, /*symmetric=*/1);
   n = M2->nrows;
   b2 = readVector (fp, n);
   x2 = (double*)malloc(n*sizeof(double));

   setVector (x2, x1, n);

   iparm[7] = 1;       /* Max numbers of iterative refinement steps. */
   iparm[3] = 102;

   printf ("maxfct=%d\n", maxfct);
   printf ("mnum=%d\n", mnum);
   printf ("mtype=%d\n", mtype);
   printf ("phase=%d\n", phase);
   printf ("idum=%d\n", idum);
   for (i=0; i<64; i++) {
     printf ("%d ", iparm[i]);
   }
   printf ("\n");

   t0 = currentTimeUsec();
   PARDISO (pt, &maxfct, &mnum, &mtype, &phase,
		      &n, M2->vals, M2->rowOffs, M2->colIdxs, &idum, &nrhs,
		      iparm, &msglvl, b2, x2, &error);
   t1 = currentTimeUsec();   
   if (error != 0) {
     printf("ERROR during solution: %d\n", error);
     exit(3);
   }
   printf ("num iterations=%d\n", iparm[19]);

   for (i=0; i<n; i++) {
     printf ("%g ", x2[i]);
   }
   printf ("\n");
   printf("Solve:   msec=%8.1f\n\n", (t1-t0)/1000.0);

   mulVector (r, M2, x2);
   subVector (r, r, b2, n);
   printf("residual=%g\n", normVector(r, n));
#endif

/* -------------------------------------------------------------------- */    
/* ..  Termination and release of memory.                               */
/* -------------------------------------------------------------------- */    
   phase = -1;                 /* Release internal memory. */
    
   PARDISO (pt, &maxfct, &mnum, &mtype, &phase,
		      &n, &ddum, M1->rowOffs, M1->colIdxs, &idum, &nrhs,
		      iparm, &msglvl, &ddum, &ddum, &error);

   return 0;
} 
Exemple #12
0
      void operator() (const TVectorType& y, TVectorType& x)
      {
        typedef typename TVectorType::value_type value_type;
        typedef typename to_real_type<value_type>::type real_type;

        // no convergence at the beginning
        m_convergence = false;
        m_iteration = 0;

        real_type n2y = getL2NormOfDistributedVector(y);
        real_type toly = m_tolerance * n2y;

        // the residual y - F(x_k)
        TVectorType residual_k(y.size());

        // temporary vectors
        TVectorType t_sy(y.size());
        TVectorType t_sx(x.size());

        // compute the initial residual r_0 = y - F(x_0), now t_sy is distributed
        // u = b - A*x;
        m_forward(x, t_sy);
        subVector(y, t_sy, residual_k);

        // L2 norm of residual
        // beta = norm(u);
        real_type beta = getL2NormOfDistributedVector(residual_k);
        m_rho_0 = beta;
        m_rho_k = m_rho_0;
        real_type normr = beta;

        // normalize the residual
        // u = u / beta
        if (beta != 0)
        {
          scale(real_type(1) / beta, residual_k, residual_k);
        }

        // v = A'*u;
        TVectorType v(x.size(), value_type(0));
        adjoint(m_forward)(residual_k, v);

        // normalize v
        real_type alpha = getL2NormOfDistributedVector(v);
        if (alpha != 0)
        {
          scale(real_type(1) / alpha, v, v);
        }

        real_type normar = alpha * beta;

        if (normar == 0)
        {
          // stop if norm of residum is 0
          // => x0 is the exact solution
          return;
        }

        if (n2y == 0) {
          // rhs vector y is zero, so x is also a zero vector
          x.assign(x.size(), value_type(0));
          return;
        }

        real_type norma = 0;
        real_type c = 1;
        real_type s = 0;
        real_type phibar = beta;
        TVectorType d(x.size(), value_type(0)); //d must be initialized to zero
        for (m_iteration = 0; m_iteration < m_max_iterations; ++m_iteration)
        {
          // u = A*v - alpha * u;
          m_forward(v, t_sy);
          subScaledVector(t_sy, alpha, residual_k, residual_k);
          // beta = norm(u)
          // normalize the residual
          beta = getL2NormOfDistributedVector(residual_k);
          // Normalize residual
          scale(real_type(1) / beta, residual_k, residual_k);
          //m_rho_k = beta;

          // norma = norm([norma alpha beta]);
          norma = std::sqrt(norma*norma + alpha*alpha + beta*beta);

          // thet = -s * alpha;
          real_type thet = -s * alpha;
          // rhot = c * alpha;
          real_type rhot = c * alpha;
          // sqrt(rhot^2 + beta^2);
          real_type rho = std::sqrt(rhot*rhot + beta*beta);
          // c = rhot / rho;
          c = rhot / rho;
          // s = -beta / rho;
          s = -beta / rho;
          // phi = c * phibar
          real_type phi = c * phibar;

          if (phi == 0)
          {
            // Stagnation @see matlab implementation
            break;
          }

          // phibar = s * phibar;
          phibar = s * phibar;

          // d = (v - thet * d) / rho;
          subScaledVector(v, thet, d, d);
          scale(real_type(1) / rho, d, d);

          // check for convergence in min{|b-A*x|}
          // absolute convergence
          if (normar / (norma*normr) <= m_tolerance)
          {
            m_convergence = true;
            break;
          }

          // check for convergence in A*x=b
          // relative convergence
          if (normr <= toly)
          {
            m_convergence = true;
            break;
          }

          // x = x + phi * d;
          addScaledVector(x, phi, d, x);
          // normr = abs(s) * normr;
          normr = std::abs(s) * normr;
          m_rho_k = normr;
          // vt = A' * u;
          adjoint(m_forward)(residual_k, t_sx);
          // v = vt - beta * v;
          subScaledVector(t_sx, beta, v, v);
          // alpha = norm(v);
          alpha = getL2NormOfDistributedVector(v);
          // v = v / alpha;
          scale(real_type(1) / alpha, v, v);
          // normar = alpha * abs(s * phi);
          normar = alpha * std::abs(s * phi);
        }
      }
int API::select(const string tableName, const vector<string> &attr, const vector<string> &op, const vector<string> &value,
	const vector<int>& type, const vector<int>& begin, int recordSize, vector<vector<char>>& returnRecord, int toDelete){
	vector<int> end;
	vector<char> temp;
	vector<vector<char>> valuex;
	vector<int> opx;
	vector<Address> returnAddr;
	if (op.size()==0){//selete * from xxx;//无条件
		if (toDelete){
			//判断有无索引
			vector<char> tName;
			sToVchar(tableName, tName);
			vector<vector<char>> returnRecord;
			vector<int> op; op.push_back(0);
			vector<vector<char>> value; value.push_back(tName);
			vector<int> begin; begin.push_back(50);
			vector<int> end; end.push_back(100);
			vector<int> type; type.push_back(53);
			Index index;
			rmanager.findAllRecord("index.catalog", op, value, begin, end, type, index.recordSize, returnRecord, 0);
			for (int i = 0; i < returnRecord.size(); i++){//若有 则清空索引文件
				string temp = "";
				for (int j = 0; j < 50; j++)
					if (returnRecord[i][j])
						temp += returnRecord[i][j];
				//bmanager.deleteFile(temp + ".index");
				//cmanager.clearFile(temp + ".index");
					
					imanager.rebuildIndex(temp);
			}
			return rmanager.deleteAll(tableName, recordSize);
		}
		else{
			//cout << "okok" << endl;
			returnAddr=rmanager.getAllRecord(tableName, recordSize, returnRecord);
			//cout << "okok" << endl;
			return returnAddr.size();
		}
	}
	else{//有条件
		for (int i = 0; i < op.size(); i++){
			if (type[i] == 1){//int
				end.push_back(begin[i] + 4);
				intToChar4(atoi(value[i].c_str()), temp);
				valuex.push_back(temp);
				opx.push_back(typeToInt(op[i]));
			}
			else if (type[i] == 2){//float
				end.push_back(begin[i] + 4);
				floatToChar4(atof(value[i].c_str()), temp);
				valuex.push_back(temp);
				opx.push_back(typeToInt(op[i]));
			}
			else{//char(n)
				end.push_back(begin[i] + type[i]-3);
				temp.clear();
				for (int k = 0; k < value[i].size(); k++)
					temp.push_back(value[i][k]);
				//sToVchar(value[i], temp);
				valuex.push_back(temp);
				opx.push_back(typeToInt(op[i]));
			}
		}
		
		//判断属性是否都有索引
		vector<int> in;
		vector<string> attrHasIndex;

		Index index;
		string indexName;
		int useIndex = 1;
		vector<char> key;
		vector<char> key1;
		sToVchar(tableName, key);
		for (int i = 0; i < attr.size(); i++){
			int flag = 0;
			for (int j = 0; j < attrHasIndex.size(); j++)//查找这个属性有无在attrHasIndex中
				if (attr[i] == attrHasIndex[j]){
					flag = 1;
					break;
				}
			if (flag)
				continue;
			
			//查找这个属性上有无index
			key.erase(key.begin() + 50, key.end());
			sToVchar(attr[i], key1);
			key.insert(key.end(), key1.begin(), key1.end());
			Address addr = rmanager.findLastRecord("index.catalog", key, index.recordSize, 50);
			if (addr.isNullAddr()){//条件中有一个属性没有索引
				//cout << attr[i] << ": no index!" << endl;
				useIndex = 0;
				break;
			}
			else{//有索引
				//cout << attr[i] << ": index!" << endl;
				temp = rmanager.getNextRecord(addr, "index.catalog", index.recordSize);
				indexName = "";
				for (int i = 0; i < 50; i++){
					if (temp[i])
						indexName += temp[i];
				}
				attrHasIndex.push_back(indexName);
				in.push_back(i);
			}

		}

		//选择select方式
		if (!useIndex||attrHasIndex.size()>1){
			clock_t start = clock();
			returnAddr = rmanager.findAllRecord(tableName, opx, valuex, begin, end, type, recordSize, returnRecord, toDelete);
			clock_t finish = clock();
			printf("Time consumed: %f second\n", (double)(finish - start) / CLOCKS_PER_SEC);
		}
		else{//符合使用index加速的条件
			if (toDelete == 0){//select
				cout << "use index" << endl;
				clock_t start = clock();
				/*for (int i = 0; i < valuex.size(); i++)
					printvc(valuex[i]);*/
				returnAddr = imanager.select(indexName, valuex, opx);
				//cout << "ok" << endl;
				returnRecord.clear();
				for (int i = 0; i < returnAddr.size(); i++){
					returnAddr[i].setAddrFile(tableName);
					returnRecord.push_back(rmanager.getRecord(returnAddr[i], recordSize));
				}
				clock_t finish = clock();
				printf("Time consumed: %f second\n", (double)(finish - start) / CLOCKS_PER_SEC);

			}
			else{//delete
				returnAddr = rmanager.findAllRecord(tableName, opx, valuex, begin, end, type, recordSize, returnRecord, toDelete);

				for (int i = 0; i < attrHasIndex.size(); i++)
					for (int j = 0; j < returnRecord.size(); j++)
						imanager.deleteKey(attrHasIndex[i], subVector(returnRecord[j], begin[in[i]], end[in[i]]));				
			}
		}

		//returnAddr = rmanager.findAllRecord(tableName, opx, valuex, begin, end, type, recordSize, returnRecord, toDelete);
			
		return returnAddr.size();
	}
}
pair<Problema*,Problema*> QuickHullP::descomponer() {
	pair<Problema*,Problema*> subProblemas;
	subProblemas.first = new QuickHullP(subVector(0));
	subProblemas.second = new QuickHullP(subVector(1));
	return subProblemas;
}