static int gcvec_add( lua_State* L ) { vec_t* v1 = checkvec( L, 1 ); vec_t* v2 = checkvec( L, 2 ); new_vec( L, v1->x + v2->x, v1->y + v2->y, v1->z + v2->z, v1->w + v2->w ); return 1; }
static int gcvec_sub( lua_State* L ) { vec_t* v1 = checkvec( L, 1 ); vec_t* v2 = checkvec( L, 2 ); new_vec( L, v1->x - v2->x, v1->y - v2->y, v1->z - v2->z, v1->w - v2->w ); return 1; }
static int gcvec_div( lua_State* L ) { vec_t* v1 = checkvec( L, 1 ); float s = (float)luaL_checknumber( L, 2 ); luaL_argcheck( L, s != 0.0f, 2, "division by zero" ); new_vec( L, v1->x / s, v1->y / s, v1->z / s, v1->w / s ); return 1; }
static int gcvec_get_item( lua_State* L ) { vec_t* v = checkvec( L, 1 ); int i = luaL_checkint( L, 2 ); luaL_argcheck( L, i >= 1 && i <= LUA_VEC_SIZE, 2, "index out of range" ); lua_pushnumber( L, (&v->x)[i-1] ); return 1; }
static int gcvec_mul( lua_State* L ) { vec_t* v1 = checkvec( L, 1 ); if( lua_isuserdata(L, 2) ) { // vector * vector vec_t* v2 = checkvec( L, 2 ); new_vec( L, v1->x * v2->x, v1->y * v2->y, v1->z * v2->z, v1->w * v2->w ); } else { // vector * scalar float s = (float)luaL_checknumber( L, 2 ); new_vec( L, v1->x * s, v1->y * s, v1->z * s, v1->w * s ); } return 1; }
static int gcvec_set_item( lua_State* L ) { vec_t* v = checkvec( L, 1 ); int i = luaL_checkint( L, 2 ); float val = (float)luaL_checknumber( L, 3 ); luaL_argcheck( L, i >= 1 && i <= LUA_VEC_SIZE, 2, "index out of range" ); (&v->x)[i-1] = val; return 0; }
static void star_calc(liqcell *sheepdog_flow,STAR *stars,vgraph *graph,liqsketch *sketch, int drawwidth,int drawheight) { // pseudostarcount is the count of actual stars, between 0..starscount, based on the size of the box int pseudostarcount = starcount * drawwidth / 800; if(pseudostarcount>starcount)pseudostarcount=starcount; //liqapp_log("stardraw %i,%i :: %i",drawwidth,drawheight,pseudostarcount); int aax=0; int aay=0; int aaz=0; accel_read(&aax,&aay,&aaz); #define ff 0.08 float fax=ff * (float)aax; float fay=ff * (float)aay; float faz=ff * (float)aaz; // 10000 == vigerous shake // 0 == still float r= 0.01 * ((float)accel_fat); //liqapp_log("af %5i %3.2f",accel_fat,r); int size = 4 + (accel_fat/(1000/8)); if(size>24)size=24; liqcell *backplane = liqcell_child_lookup(sheepdog_flow,"backplane"); liqimage *img = liqcell_getimage(backplane); // grab the background //liqapp_log("...creating cliprect"); liqcliprect *cr = (img?liqcliprect_newfromimage(img):NULL); if(cr) { vgraph_drawimage(graph,0,0,800,480,img); } //liqapp_log("%-4i %-4i %-4i",aax,aay,aaz); //liqapp_log("%-4.2f %-4.2f %-4.2f",fax,fay,faz); //liqapp_log("stars calc"); int a; STAR *sa; for(a=0;a<pseudostarcount;a++) { sa=&stars[a]; //sa->a.x *= 0.3; //sa->a.y *= 0.3; sa->a.x = -fax; // start with gravity sa->a.y = -fay; //sa->a.x = 0; // start with no acceleration //sa->a.y = 0; //sa->a.z = 0; if(cr) { // need to get a sample of points from the ground below // this is achieved with: inline void liqcliprect_drawpgetcolor( liqcliprect *self,int x, int y, unsigned char *grey,unsigned char *u,unsigned char *v); unsigned char gcy[9]={128}; unsigned char gcu=128; unsigned char gcv=128; int gx=sa->p.x; int gy=sa->p.y; if(gy>0) { if(gx>0) liqcliprect_drawpgetcolor( cr, gx-1, gy-1, &gcy[0], &gcu, &gcv ); liqcliprect_drawpgetcolor( cr, gx , gy-1, &gcy[1], &gcu, &gcv ); if(gx<799)liqcliprect_drawpgetcolor( cr, gx+1, gy-1, &gcy[2], &gcu, &gcv ); } { if(gx>0) liqcliprect_drawpgetcolor( cr, gx-1, gy , &gcy[3], &gcu, &gcv ); liqcliprect_drawpgetcolor( cr, gx , gy , &gcy[4], &gcu, &gcv ); if(gx<799)liqcliprect_drawpgetcolor( cr, gx+1, gy , &gcy[5], &gcu, &gcv ); } if(gy<479) { if(gx>0) liqcliprect_drawpgetcolor( cr, gx-1, gy+1, &gcy[6], &gcu, &gcv ); liqcliprect_drawpgetcolor( cr, gx , gy+1, &gcy[7], &gcu, &gcv ); if(gx<799)liqcliprect_drawpgetcolor( cr, gx+1, gy+1, &gcy[8], &gcu, &gcv ); } // now we look for gradients // relative to 4 //the size of the gradient represents a vector in that other direction float gax=0; float gay=0; void checkvec(int idx,int ox,int oy) { int vidx = gcy[idx]; int v4 = gcy[4]; gax += (float)ox * (float)(vidx-v4) ; gay += (float)oy * (float)(vidx-v4) ; } #define rx 1 #define ry 1 checkvec(0, -rx,-ry); checkvec(1, 0, -ry); checkvec(2, rx,-ry); checkvec(3, -rx,0); //checkvec(0, 0, 0); checkvec(5, rx,0); checkvec(6, -rx,ry); checkvec(7, 0, ry); checkvec(8, rx,ry); liqapp_log("g? %i %3.3f,%3.3f %i",a,gax,gay,gcy[4]); sa->a.x += gax; // start with no acceleration sa->a.y += gay; } }
int main(int argc, char *argv[]) { Teuchos::GlobalMPISession mpiSession(&argc, &argv); // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided. int iprint = argc - 1; Teuchos::RCP<std::ostream> outStream; Teuchos::oblackholestream bhs; // outputs nothing if (iprint > 0) outStream = Teuchos::rcp(&std::cout, false); else outStream = Teuchos::rcp(&bhs, false); int errorFlag = 0; double errtol = ROL::ROL_THRESHOLD; // *** Test body. try { int dim = 100; Teuchos::RCP<std::vector<ElementT> > x_rcp = Teuchos::rcp( new std::vector<ElementT> (dim, 0.0) ); Teuchos::RCP<std::vector<ElementT> > y_rcp = Teuchos::rcp( new std::vector<ElementT> (dim, 0.0) ); Teuchos::RCP<std::vector<ElementT> > z_rcp = Teuchos::rcp( new std::vector<ElementT> (dim, 0.0) ); ROL::StdVector<RealT, ElementT> x(x_rcp); ROL::StdVector<RealT, ElementT> y(y_rcp); ROL::StdVector<RealT, ElementT> z(z_rcp); RealT left = -1e0, right = 1e0; // set x,y,z for (int i=0; i<dim; i++) { (*x_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left; (*y_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left; (*z_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left; } // Standard tests. std::vector<RealT> consistency = x.checkVector(y, z, true, *outStream); ROL::StdVector<RealT, ElementT> checkvec(Teuchos::rcp(&consistency, false)); if (checkvec.norm() > std::sqrt(ROL::ROL_EPSILON)) { errorFlag++; } // Basis tests. // set x to first basis vector Teuchos::RCP<ROL::Vector<RealT> > zp = x.clone(); zp = x.basis(0); RealT znorm = zp->norm(); *outStream << "Norm of ROL::Vector z (first basis vector): " << znorm << "\n"; if ( std::abs(znorm-1.0) > errtol ) { *outStream << "---> POSSIBLE ERROR ABOVE!\n"; errorFlag++; }; // set x to middle basis vector zp = x.basis(dim/2); znorm = zp->norm(); *outStream << "\nNorm of ROL::Vector z ('middle' basis vector): " << znorm << "\n"; if ( std::abs(znorm-1.0) > errtol ) { *outStream << "---> POSSIBLE ERROR ABOVE!\n"; errorFlag++; }; // set x to last basis vector zp = x.basis(dim-1); znorm = zp->norm(); *outStream << "\nNorm of ROL::Vector z (last basis vector): " << znorm << "\n"; if ( std::abs(znorm-1.0) > errtol ) { *outStream << "---> POSSIBLE ERROR ABOVE!\n"; errorFlag++; }; // Repeat the checkVector tests with a zero vector. x.scale(0.0); consistency = x.checkVector(x, x, true, *outStream); if (checkvec.norm() > 0.0) { errorFlag++; } } catch (std::logic_error err) { *outStream << err.what() << "\n"; errorFlag = -1000; }; // end try if (errorFlag != 0) std::cout << "End Result: TEST FAILED\n"; else std::cout << "End Result: TEST PASSED\n"; return 0; }
/* Benchmark certain kernel operations */ void time_kernels ( struct vtx_data **A, /* matrix/graph being analyzed */ int n, /* number of rows/columns in matrix */ double *vwsqrt /* square roots of vertex weights */ ) { extern int DEBUG_PERTURB; /* debug flag for matrix perturbation */ extern int PERTURB; /* randomly perturb to break symmetry? */ extern int NPERTURB; /* number of edges to perturb */ extern int DEBUG_TRACE; /* trace main execution path */ extern double PERTURB_MAX; /* maximum size of perturbation */ int i, beg, end; double *dvec1, *dvec2, *dvec3; float *svec1, *svec2, *svec3, *vwsqrt_float; double norm_dvec, norm_svec; double dot_dvec, dot_svec; double time, time_dvec, time_svec; double diff; double factor, fac; float factor_float, fac_float; int loops; double min_time, target_time; double *mkvec(); float *mkvec_float(); void frvec(), frvec_float(); void vecran(); double ch_norm(), dot(); double norm_float(), dot_float(); double seconds(); void scadd(), scadd_float(), update(), update_float(); void splarax(), splarax_float(); void perturb_init(), perturb_clear(); if (DEBUG_TRACE > 0) { printf("<Entering time_kernels>\n"); } beg = 1; end = n; dvec1 = mkvec(beg, end); dvec2 = mkvec(beg, end); dvec3 = mkvec(beg - 1, end); svec1 = mkvec_float(beg, end); svec2 = mkvec_float(beg, end); svec3 = mkvec_float(beg - 1, end); if (vwsqrt == NULL) { vwsqrt_float = NULL; } else { vwsqrt_float = mkvec_float(beg - 1, end); for (i = beg - 1; i <= end; i++) { vwsqrt_float[i] = vwsqrt[i]; } } vecran(dvec1, beg, end); vecran(dvec2, beg, end); vecran(dvec3, beg, end); for (i = beg; i <= end; i++) { svec1[i] = dvec1[i]; svec2[i] = dvec2[i]; svec3[i] = dvec3[i]; } /* Set number of loops so that ch_norm() takes about one second. This should insulate against inaccurate timings on faster machines. */ loops = 1; time_dvec = 0; min_time = 0.5; target_time = 1.0; while (time_dvec < min_time) { time = seconds(); for (i = loops; i; i--) { norm_dvec = ch_norm(dvec1, beg, end); } time_dvec = seconds() - time; if (time_dvec < min_time) { loops = 10 * loops; } } loops = (target_time / time_dvec) * loops; if (loops < 1) loops = 1; printf(" Kernel benchmarking\n"); printf("Time (in seconds) for %d loops of each operation:\n\n", loops); printf("Routine Double Float Discrepancy Description\n"); printf("------- ------ ----- ----------- -----------\n"); /* Norm operation */ time = seconds(); for (i = loops; i; i--) { norm_dvec = ch_norm(dvec1, beg, end); } time_dvec = seconds() - time; time = seconds(); for (i = loops; i; i--) { norm_svec = norm_float(svec1, beg, end); } time_svec = seconds() - time; diff = norm_dvec - norm_svec; printf("norm %6.2f %6.2f %14.5e", time_dvec, time_svec, diff); printf(" 2 norm\n"); /* Dot operation */ time = seconds(); for (i = loops; i; i--) { dot_dvec = dot(dvec1, beg, end, dvec2); } time_dvec = seconds() - time; time = seconds(); for (i = loops; i; i--) { dot_svec = dot_float(svec1, beg, end, svec2); } time_svec = seconds() - time; diff = dot_dvec - dot_svec; printf("dot %6.2f %6.2f %14.5e", time_dvec, time_svec, diff); printf(" scalar product\n"); /* Scadd operation */ factor = 1.01; factor_float = factor; fac = factor; time = seconds(); for (i = loops; i; i--) { scadd(dvec1, beg, end, fac, dvec2); fac = -fac; /* to keep things in scale */ } time_dvec = seconds() - time; fac_float = factor_float; time = seconds(); for (i = loops; i; i--) { scadd_float(svec1, beg, end, fac_float, svec2); fac_float = -fac_float; /* to keep things in scale */ } time_svec = seconds() - time; diff = checkvec(dvec1, beg, end, svec1); printf("scadd %6.2f %6.2f %14.5e", time_dvec, time_svec, diff); printf(" vec1 <- vec1 + alpha*vec2\n"); /* Update operation */ time = seconds(); for (i = loops; i; i--) { update(dvec1, beg, end, dvec2, factor, dvec3); } time_dvec = seconds() - time; time = seconds(); for (i = loops; i; i--) { update_float(svec1, beg, end, svec2, factor_float, svec3); } time_svec = seconds() - time; diff = checkvec(dvec1, beg, end, svec1); printf("update %6.2f %6.2f %14.2g", time_dvec, time_svec, diff); printf(" vec1 <- vec2 + alpha*vec3\n"); /* splarax operation */ if (PERTURB) { if (NPERTURB > 0 && PERTURB_MAX > 0.0) { perturb_init(n); if (DEBUG_PERTURB > 0) { printf("Matrix being perturbed with scale %e\n", PERTURB_MAX); } } else if (DEBUG_PERTURB > 0) { printf("Matrix not being perturbed\n"); } } time = seconds(); for (i = loops; i; i--) { splarax(dvec1, A, n, dvec2, vwsqrt, dvec3); } time_dvec = seconds() - time; time = seconds(); for (i = loops; i; i--) { splarax_float(svec1, A, n, svec2, vwsqrt_float, svec3); } time_svec = seconds() - time; diff = checkvec(dvec1, beg, end, svec1); printf("splarax %6.2f %6.2f %14.5e", time_dvec, time_svec, diff); printf(" sparse matrix vector multiply\n"); if (PERTURB && NPERTURB > 0 && PERTURB_MAX > 0.0) { perturb_clear(); } printf("\n"); /* Free memory */ frvec(dvec1, 1); frvec(dvec2, 1); frvec(dvec3, 0); frvec_float(svec1, 1); frvec_float(svec2, 1); frvec_float(svec3, 0); if (vwsqrt_float != NULL) { frvec_float(vwsqrt_float, beg - 1); } }
static int gcvec_normalize (lua_State *L) { const vec_t* v = checkvec(L, 1); float s = 1.0f / sqrt(v->x*v->x + v->y*v->y + v->z*v->z + v->w*v->w); new_vec(L, v->x*s, v->y*s, v->z*s, v->w*s); return 1; }
static int gcvec_length (lua_State *L) { const vec_t* v = checkvec(L, 1); lua_pushnumber(L, sqrt(v->x*v->x + v->y*v->y + v->z*v->z + v->w*v->w)); return 1; }
static int gcvec_cross (lua_State *L) { const vec_t* v1 = checkvec(L, 1); const vec_t* v2 = checkvec(L, 2); new_vec(L, v1->y * v2->z - v1->z * v2->y, v1->z * v2->x - v1->x * v2->z, v1->x * v2->y - v1->y * v2->x, 0.0f); return 1; }
static int gcvec_dot (lua_State *L) { const vec_t* v1 = checkvec(L, 1); const vec_t* v2 = checkvec(L, 2); lua_pushnumber(L, v1->x*v2->x + v1->y*v2->y + v1->z*v2->z + v1->w*v2->w); return 1; }
static int gcvec_tostring( lua_State* L ) { vec_t* v = checkvec(L, 1); lua_pushfstring( L, "vec(%f, %f, %f, %f)", v->x, v->y, v->z, v->w ); return 1; }
static int gcvec_negate( lua_State* L ) { vec_t* v = checkvec( L, 1 ); new_vec( L, -v->x, -v->y, -v->z, -v->w ); return 1; }
int main(int argc, char *argv[]) { Teuchos::GlobalMPISession mpiSession(&argc, &argv,0); Platform &platform = Tpetra::DefaultPlatform::getDefaultPlatform(); Teuchos::RCP<const Teuchos::Comm<int> > comm = platform.getComm(); int iprint = argc - 1; Teuchos::oblackholestream bhs; // outputs nothing std::ostream& outStream = (iprint > 0) ? std::cout : bhs; int errorFlag = 0; RealT errtol = ROL::ROL_THRESHOLD<RealT>(); try { // Dimension of the optimization vector int dim = 10; Teuchos::RCP<Map> map = Teuchos::rcp( new Map(dim,0,comm) ); // Create Tpetra::MultiVectors (single vectors) MVP x_rcp = Teuchos::rcp( new MV(map,1,true) ); MVP y_rcp = Teuchos::rcp( new MV(map,1,true) ); VP W_rcp = Teuchos::rcp( new V(map,true) ); // Random elements //x_rcp->randomize(); //y_rcp->randomize(); x_rcp->putScalar(1.0); y_rcp->putScalar(1.0); // Set all values to 2 W_rcp->putScalar(2.0); // Create ROL vectors ROL::PrimalScaledTpetraMultiVector<RealT,LO,GO,Node> x(x_rcp,W_rcp); ROL::DualScaledTpetraMultiVector<RealT,LO,GO,Node> y(y_rcp,W_rcp); // const ROL::Vector<RealT> &g = x.dual(); // const ROL::Vector<RealT> &h = x.dual(); // RealT hnorm = h.norm(); // RealT gnorm = g.norm(); RealT xy = x.dot(y.dual()); RealT yx = y.dot(x.dual()); outStream << "\nAbsolute error between x.dot(y.dual()) and y.dot(x.dual()): " << std::abs(xy-yx) << "\n"; outStream << "x.dot(y.dual()): " << xy << "\n"; outStream << "y.dot(x.dual()): " << yx << "\n"; if ( std::abs(xy-yx) > errtol ) { outStream << "---> POSSIBLE ERROR ABOVE!\n"; errorFlag++; } RealT xx = std::sqrt(x.dot(x)), xnorm = x.norm(); RealT yy = std::sqrt(y.dot(y)), ynorm = y.norm(); outStream << "\nAbsolute error between sqrt(x.dot(x)) and x.norm(): " << std::abs(xx-xnorm) << "\n"; outStream << "sqrt(x.dot(x)): " << xx << "\n"; outStream << "x.norm(): " << xnorm << "\n"; if ( std::abs(xx-xnorm) > errtol ) { outStream << "---> POSSIBLE ERROR ABOVE!\n"; errorFlag++; } outStream << "\nAbsolute error between sqrt(y.dot(y)) and y.norm(): " << std::abs(yy-ynorm) << "\n"; outStream << "sqrt(y.dot(y)): " << yy << "\n"; outStream << "y.norm(): " << ynorm << "\n"; if ( std::abs(yy-ynorm) > errtol ) { outStream << "---> POSSIBLE ERROR ABOVE!\n"; errorFlag++; } // clone z from x, deep copy x into z, norm of z Teuchos::RCP<ROL::Vector<RealT> > z = x.clone(); z->set(x); RealT znorm = z->norm(); outStream << "\nNorm of ROL::Vector z (clone of x): " << znorm << "\n"; if ( std::abs(xnorm - znorm) > errtol ) { outStream << "---> POSSIBLE ERROR ABOVE!\n"; errorFlag++; } Teuchos::RCP<ROL::Vector<RealT> > w = y.clone(); w = y.clone(); w->set(y); RealT wnorm = w->norm(); outStream << "\nNorm of ROL::Vector w (clone of y): " << wnorm << "\n"; if ( std::abs(ynorm - wnorm) > errtol ) { outStream << "---> POSSIBLE ERROR ABOVE!\n"; errorFlag++; } // Standard tests. // Create Tpetra::MultiVectors (single vectors) MVP x1_rcp = Teuchos::rcp( new MV(map,1,true) ); MVP y1_rcp = Teuchos::rcp( new MV(map,1,true) ); MVP z1_rcp = Teuchos::rcp( new MV(map,1,true) ); ROL::PrimalScaledTpetraMultiVector<RealT,LO,GO,Node> x1(x1_rcp,W_rcp); ROL::PrimalScaledTpetraMultiVector<RealT,LO,GO,Node> y1(y1_rcp,W_rcp); ROL::PrimalScaledTpetraMultiVector<RealT,LO,GO,Node> z1(z1_rcp,W_rcp); x1_rcp->randomize(); y1_rcp->randomize(); z1_rcp->randomize(); std::vector<RealT> consistency = x1.checkVector(y1, z1, true, outStream); ROL::StdVector<RealT> checkvec(Teuchos::rcp(&consistency, false)); if (checkvec.norm() > std::sqrt(errtol)) { errorFlag++; } } catch (std::logic_error err) { outStream << err.what() << "\n"; errorFlag = -1000; }; // end try if (errorFlag != 0) std::cout << "End Result: TEST FAILED\n"; else std::cout << "End Result: TEST PASSED\n"; return 0; }