示例#1
0
int dataExchange(unit_t tmin, unit_t tmax, pvector_t *v, pvector_t tmp) {
	index_t i, x, pos = 0;
	unit_t t;
	pvector_t data;

	if (initVector(&data, ELEMENTS_PER_PROCESS << 1) == NULL)
		return ERRORCODE_CANT_MALLOC;

	for(i = 0; i < PROCESS_NUMBER; i++) {
		if (ID == i) copyVector(*v, tmp, (*v)->length);

		if (MPI_Bcast(tmp->vector, listLength(i), MPI_UNIT, i, MPI_COMM_WORLD) != MPI_SUCCESS)
			return ERRORCODE_MPI_ERROR;

		for(x = 0; x < listLength(i); x++) {
			t = getFromVector(tmp, x);
			if (t <= tmax) {
				if (t > tmin)
					setInVector(data, pos++, t);
			} else
				x+= ELEMENTS_NUMBER;
		}
	}

	freeVector(v);
	if (initVector(v, pos) == NULL)
		return ERRORCODE_CANT_MALLOC;
	copyVector(data, *v, pos);
	freeVector(&data);
	return ERRORCODE_NOERRORS;
}
示例#2
0
Vector adams()
{
  size_t numof_xs = get_numof_xs(h);
  Vector runge_ys = runge();
  Vector adams_ys;
  initVector(&adams_ys, numof_xs);
  for (size_t i = 0; i < adams_acc; ++i)
    append(&adams_ys, runge_ys.values[i]);
  disposeVector(&runge_ys);

  Vector etas;
  initVector(&etas, numof_xs);
  for (size_t i = 0; i < adams_acc; ++i)
    append(&etas, eta(i, adams_ys.values[i]));
  FiniteDiffTable table = createFiniteDiffTable(&etas);

  for (size_t k = 5; k < numof_xs; ++k) {
    double yk = adams_next_yk(adams_ys.values[k - 1], &table);
    append(&adams_ys, yk);
    append(&etas, eta(k, yk));
    disposeFiniteDiffTable(&table);
    table = createFiniteDiffTable(&etas);
  }

  printTable(&table, &adams_ys, stdout);
  disposeVector(&etas);
  disposeFiniteDiffTable(&table);
  return adams_ys;
}
示例#3
0
void boidInitialization(Boid *boid, int id,double width, double depth, double high,Vector * startingV,Vector * startingA)
{
	double mass, force;
	Vector position, vVelocity, vAcceleration;

	//Vector offsetAcceleration;
	//double accelerationOffset, velocityOffset;

	////vettore accelerazione iniziale (valore base uguale x tutti + leggero offset)
	//initVector(&vAcceleration);
	//accelerationOffset = simParameters.maxAcceleration/100;
	//randomVector(&offsetAcceleration, accelerationOffset, accelerationOffset, accelerationOffset);
	//addVector(&offsetAcceleration,startingA,&vAcceleration);


	////vettore velocità iniziale (valore base uguale x tutti + leggero offset)
	//initVector(&vVelocity);
	//velocityOffset = simParameters.maxVelocity/100;
	//randomVector(&offsetVelocity, velocityOffset, velocityOffset, velocityOffset);
	//addVector(&offsetVelocity,startingV,&vVelocity);

	mass=1;
	force=0.1;

	initVector(&vAcceleration);
	initVector(&vVelocity);

    // vettore posizione
    randomVector(&position, width, depth, high);

    initBoid(&position, &vVelocity, &vAcceleration, simParameters.maxVelocity, simParameters.maxAcceleration, force, mass, id, boid);
}
示例#4
0
// Makes a new frame that points to its parent
// uses a Frame* so that the first one can point to NULL and it is prettier
Frame* makeFrame(Frame *parent) {
    // Create a frame
    // point new frame's parent pointer to parent
    Frame *newEnv = (Frame *)malloc(sizeof(Frame));
    newEnv->parent = parent;
    initVector(&(newEnv->names), 2*sizeof(void**));
    initVector(&(newEnv->values), 2*sizeof(void**));
    return newEnv;
}
/**
 * @fn executed by the slaves processes
 *
 * @param inMyRank the slave Id
 * @param inLabel the master-slaves shared label to communicate
 *
 * @brief The slave :
 * -Receives the vector
 * -Then, it's waiting for matrix's rows
 *  -if is not the END label then
 *  	-computes the scalar product
 *  	-sends the results to the master
 *  -else
 *  	-Game Over
 *
 */
void slave(int inMyRank, int inLabel) {

	int i;
	int product;
	int vectorSize;
	MPI_Status status;
	int * vector;
	int * row;

	//Beginning
	printf("Slave#%d, I'm waiting for the vector\n", inMyRank);

	/* -- Receive the vector -- */
	//Receive the vector and its size
	MPI_Bcast(&vectorSize, 1, MPI_INT, 0, MPI_COMM_WORLD);
	vector = initVector(vectorSize);
	row = initVector(vectorSize);

	MPI_Bcast(vector, vectorSize, MPI_INT, 0, MPI_COMM_WORLD);
	printf("Slave#%d, I received the vector : \n", inMyRank);
	printVector(vector, vectorSize);

	/* -- waiting for rows -- */
	//Receive the first row
	MPI_Recv(row, vectorSize, MPI_INT, 0, inLabel, MPI_COMM_WORLD, &status);
	printf("Slave#%d, I received the row : \n", inMyRank);
	printVector(row, vectorSize);

	//if is not the END label
	while (status.MPI_TAG != END) {

		//compute the scalar product
		product = scalarProduct(vector, row, vectorSize);
		printf("Slave#%d, The scalar product is : %d\n", inMyRank, product);

		//Send the computed result
		MPI_Send(&product, 1, MPI_INT, 0, inLabel, MPI_COMM_WORLD);

		//Get the next row
		printf("Slave#%d, I'm waiting for a new line\n", inMyRank);
		MPI_Recv(row, vectorSize, MPI_INT, 0, MPI_ANY_TAG, MPI_COMM_WORLD,
				&status);
		printf("Slave#%d, I received the row : \n", inMyRank);
		printVector(row, vectorSize);

	}

	//Game  Over
	printf("Slave#%d, I'm done. Bye\n", inMyRank);

	//Cleaning
	free(vector);
	free(row);

}
ball_t createBall(int xPos, int yPos, int xVel, int yVel, unsigned char radius, unsigned char color) {
	ball_t thisBall;

	thisBall.position = initVector(xPos, yPos);
	thisBall.velocity = initVector(xVel, yVel);

	thisBall.radius = radius;
	thisBall.color = color;

	return thisBall;
}
示例#7
0
文件: Btree.c 项目: ChangerMyGit/DSA
BTNode * newBTNode(BTNode * parent , int order ,int keys_Length){
	BTNode * node = (BTNode *)malloc(sizeof(BTNode));
	node->parent = parent;
	// 至多M个孩子
	node->child = initVector(order * sizeof(BTNode *));
	// 至多M-1个关键码
	node->keys = initVector(keys_Length);
	setAllValue(node->child,NULL,order);
	setAllValue(node->keys,NULL,order-1);
	return node;
}
示例#8
0
double BoostedTrees::train(DataSet& train_data, DataSet& test_data){
    double ce, rce, rmse, tce, trce, trmse;
    learnBiasConstant(train_data);
    clock_t clock_begin = clock();
    fprintf(stderr, "training data initialized  ... ok \n");
     //fprintf(stderr, "ok -2\n");
    vector<double> train_preds;
    vector<double> test_preds;
    initVector(train_preds, train_data.size);
    initVector(test_preds, test_data.size);
    eval(train_data, train_preds);
    eval(test_data, test_preds);
    clock_t clock_end = clock();
    train_data.calRCE(train_preds, ce, rce, rmse);
    test_data.calRCE(test_preds, tce, trce, trmse);
    fprintf(stderr, "%d\t%.1f\t%f\t%f\t%f\t%f\t%f\t%f\n", 0,
              double(clock_end - clock_begin)/CLOCKS_PER_SEC,(float)rmse,
              (float)ce, (float) rce, (float)trmse, (float) tce, (float) trce);
    // eval_bdt(bdt, train, args);
     //  fprintf(stderr, "ok -1\n");
    fprintf(stderr, "training ... \n");
    fprintf(stderr, "round\ttime(s)\ttrain_rmse\ttrain_ce\ttrain_rce\ttest_rmse\ttest_ce \ttest_rce\n");


    for (int iter = 0; iter < args.rounds; iter++)  {
        // single regression tree
        DTNode* t = new DTNode(train_data.rawData, args, args.depth, 1, args.kfeatures, false);
        trees.push_back(t);
        //fprintf(stderr, "ok 1\n");
        // get classification on training data

        eval(train_data, train_preds);
        eval(test_data, test_preds);
        train_data.calRCE(train_preds, ce, rce, rmse);
        test_data.calRCE(test_preds, tce, trce, trmse);
        //fprintf(stderr, "ok 4\n");

        clock_end = clock();
        fprintf(stderr, "%d\t%.1f\t%f\t%f\t%f\t%f\t%f\t%f\n", iter,
                double(clock_end - clock_begin)/CLOCKS_PER_SEC,(float)rmse,
                (float)ce, (float) rce, (float)trmse, (float) tce, (float) trce);
        //fprintf(stderr, "ok 5\n");
        //train.update_target(train_preds);

        updateTarget(train_data, train_preds);

        if (args.print_features){
            cout << endl;
            t->printFeature();
            cout << endl;
        }
    }

}
示例#9
0
void cudaBoidInitialization(Boid *boid, int id,double width, double depth, double high,Vector * startingV,Vector * startingA)
{
    double mass, force;
    Vector position, vVelocity, vAcceleration;
    mass=1;
    force=0.1;

    initVector(&vAcceleration);
    initVector(&vVelocity);

    // vettore posizione
    randomVector(&position, width, depth, high);

    initBoid(&position, &vVelocity, &vAcceleration, simParameters.maxVelocity, simParameters.maxAcceleration, force, mass, id, boid);
}
int SZTools::IntRand(int from, int to){
	if( from >= to ) return -1;
	initVector();
	int rec = (rand()%(to-from)+from );
	recInt.push_back(rec);
	return rec;
}
double SZTools::DoubleRand(double to){
	if( to <= 0 ) return -1;
	initVector();
	double rec = (rand()%prec[precision])*1.0/prec[precision]*to;
	recDouble.push_back(rec);
	return rec;
}
示例#12
0
// allows us to evaluate user defined functions
Value evalApply(Value expr, Frame *env) {
    // evaluate function
    Value f = eval(expr.consValue->car, env);
    if( (f.type != closureType) && (f.type != primitiveType) ){
        return evaluationError("first argument of a function applcation did not evaluate to a proceedure");
    }

    // evaluate arguments
    Vector args;
    initVector(&args,8);
    int i = 1;
    Value curr = getNth(expr.consValue, i); 
    while(curr.type != openType){
        Value *storedVal = (Value *)malloc(sizeof(Value));
        (*storedVal) = valdup(eval(curr,env));
        if((*storedVal).type == openType){ //This is an error
            void *pointerToAVal = NULL;
            for(int j = 0; j < i-2; j++){
                getVectorItem(&args, j, &pointerToAVal);
                freeValue(*((Value *)pointerToAVal));
                free((Value *)pointerToAVal);
            }
            free(storedVal);
            return evaluationError(NULL);
        }
        insertVectorItem(&(args), i-1, (void *)storedVal);
        i++;
        curr = getNth(expr.consValue, i);
    }
    
    // apply the function
    Value ret = apply(f, args);
    cleanupVector(&args);
    return ret;
}
MyIoVector2D::MyIoVector2D(QWidget *target_ui, std::vector<QString> *target_data_master, std::vector<std::vector<QString>> *target_data_slave,
	QString *name_, uint8_t flags)
	: MyIo(target_ui, name_, flags),  data_master(target_data_master), data_slave(target_data_slave)
{
	numberCols = 1;
	initVector();
}
double SZTools::DoubleRand(double from, double to){
	if( from >= to ) return -1;
	initVector();
	double rec = (rand()%prec[precision])*1.0/prec[precision]*(to-from)+from;
	recDouble.push_back(rec);
	return rec;
}
int SZTools::IntRand(int to){
	if( to <= 0 ) return -1;
	initVector();
	int rec = (rand()%to);
	recInt.push_back(rec);
	return rec;
}
示例#16
0
vector_t getPoint(const points_t points,
                  const index_t index)
{
  if ((index>=0)&&(index<getNumPoints(points))) {
    return points.v[index];
  }
  return initVector(0.0f,0.0f,0.0f);
}
void SZTools::IntArrayRand( int* a, int len, int to){
	if( to <= 0 ) return;
	initVector();
	for(int i = 0; i < len; i++){
		int rec = (rand()%to);
		recInt.push_back(rec);
		*(a+i) = rec;
	}
}
void SZTools::IntArrayRand( int* a, int len, int from, int to){
	if( from >= to ) return;
	initVector();
	for(int i = 0; i < len; i++){
		int rec = (rand()%(to-from)+from );
		recInt.push_back(rec);
		*(a+i) = rec;
	}
}
示例#19
0
void loadParticles(ParticlesParameters *pariclesList, int nParticles)
{
	int i;
	Vector nullVector;
	initVector(&nullVector);
	boidSet=(Boid *)malloc(sizeof(Boid)*nParticles);
	for (i=0;i<nParticles;i++)
		initBoid(&(pariclesList[i].position),&(pariclesList[i].velocity),&nullVector,pariclesList[i].maxSpeed,pariclesList[i].maxAcceleration,pariclesList[i].maxForce,pariclesList[i].mass,i+1,&boidSet[i]);
}
void SZTools::DoubleArrayRand( double* a, int len, double from, double to){
	if( from >= to ) return;
	initVector();
	for(int i = 0; i < len; i++){
		double rec = (rand()%prec[precision])*1.0/prec[precision]*to;
		recDouble.push_back(rec);
		*(a+i) = rec;
	}
}
paddle_t createPaddle(int xPos, int yPos, int width, int height) {
	paddle_t thisPaddle;

	thisPaddle.position = initVector(xPos, yPos);
	thisPaddle.width = width;
	thisPaddle.height = height;

	return thisPaddle;
}
示例#22
0
Vector calcValues(Polynomial const *polynomial, Vector const *points)
{
    Vector values;
    initVector(&values, points->size);
    values.size = values.capacity;
    for (size_t i = 0; i < values.size; ++i) {
        values.values[i] = calcValue(polynomial, points->values[i]);
    }
    return values;
}
示例#23
0
void appendPoints(points_t *points,
                  const float *x, const float *y,
                  const float *z, const size_t num_points)
{
  const size_t n=getNumPoints(*points);
  size_t i,j;
  reallocatePoints(points,getNumPoints(*points)+num_points);
  for (i=n,j=0; i<getNumPoints(*points); i++, j++) {
    points->v[i] = initVector(x[j],y[j],z[j]);
  }
}
示例#24
0
Vector print_euler_errors(Vector const *euler_ys, Vector const *adams_ys)
{
  Vector euler_errors;
  initVector(&euler_errors, euler_ys->size);
  for (size_t i = 0; i < euler_ys->size; ++i)
    append(&euler_errors, fabs(euler_ys->values[i] - adams_ys->values[i]));
  printf("Euler. Error\n");
  printVector(&euler_errors, stdout);
  printf("\n\n");
  return euler_errors;
}
示例#25
0
vector_t averageListedPoints(const points_t points, const list_t list)
{
  index_t i;
  vector_t m = initVector(0.0f, 0.0f, 0.0f);
  for (i=0; i<getLength(list); i++) {
    entry_t e=getEntry(list,i);
    m=addVectors(getPoint(points,entry_getIndex(&e)),m);
  }
  m=scaleVector(m,1.0f/((float)getLength(list)));
  return m;
}
示例#26
0
int main(int argc, char* argv[]){
    for(int i=0;i<100;i++){
        vectors.push_back(Physics::getRandomVector(Vector(1,1,1),30));
    }
    atexit(onExit);
    
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(800, 600);
    
    main_window = glutCreateWindow("Graphics Final");
    glutDisplayFunc(myGlutDisplay);
    glutReshapeFunc(myGlutReshape);
    glutKeyboardFunc(myKeyboardFunc);
    glutSpecialFunc(myKeyboardSpecFunc);

    
    glClearColor (0.48, 0.48, 0.48, 0.0);
    glShadeModel (GL_SMOOTH);
    
    GLfloat light_pos0[] = {0.0f, 0.0f, 1.0f, 0.0f};
    GLfloat diffuse[] = {0.5f, 0.5f, 0.5f, 0.0f};
    GLfloat ambient[] = {0.05f, 0.05f, 0.05f, 0.0f};
    
    glLightfv (GL_LIGHT0, GL_AMBIENT, ambient);
    glLightfv (GL_LIGHT0, GL_DIFFUSE, diffuse);
    glLightfv (GL_LIGHT0, GL_POSITION, light_pos0);
    
    glColorMaterial (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
    glEnable (GL_COLOR_MATERIAL);
    
    glEnable(GL_LIGHTING);
    glEnable (GL_LIGHT0);
    glEnable (GL_DEPTH_TEST);

    glPolygonOffset(1, 1);
    
    srand (time(NULL));
    ///////SEED FIREWORKS///////
    resetFireWork();
    /////////////////////////////
    
    for(int i=0; i<NUM_OF_PARTICALS; i++){
        particalsVec.push_back(new ParticalRound());
        initVector(i);
    }
    
    
    glutMainLoop();
    
    return EXIT_SUCCESS;
    
}
示例#27
0
int main (int argc, char *argv[])
{
    Vector *v = initVector();
    readCities(v);
    readAirports(v);
    cleanCities(v);
    run(v);
    deallocVector(v);
    return 0;

}
示例#28
0
void loadLeader(Vector *leaderPosition, int nFrame)
{
	int i;
	Vector nullVector;
	initVector(&nullVector);
	leader=(Boid *)malloc(sizeof(Boid)*nFrame);
	// the leader only need the position ,so the others variables will be not setup
	for (i=0;i<nFrame;i++)
		initBoid(&(leader[i].currentPosition),&nullVector,&nullVector,0,0,0,0,-1,&leader[i]);

}
示例#29
0
Vector runge()
{
  size_t numof_xs = get_numof_xs(h);
  Vector ys;
  initVector(&ys, numof_xs);
  ys.values[0] = y0;
  for (size_t i = 1; i < numof_xs; ++i) {
    ys.values[i] = runge_next_yk(get_x(i - 1, h), ys.values[i - 1]);
  }
  ys.size = numof_xs;
  return ys;
}
示例#30
0
Vector euler(double h_value)
{
  size_t numof_xs = get_numof_xs(h_value);
  Vector ys;
  initVector(&ys, numof_xs);
  ys.values[0] = y0;
  for (size_t i = 1; i < numof_xs; ++i) {
    ys.values[i] = ys.values[i - 1] + h_value * f(get_x(i - 1, h_value), ys.values[i - 1]);
  }
  ys.size = numof_xs;
  return ys;
}