Esempio n. 1
0
int main(void) {
    
    i=EEPROM_read(Address); // de vazut adresele cum sunt puse!
    
    //timer_1(1000); // set value in ms
    
    DDRD = 0xf8;
    DDRB = 0xff;
    
    // set external interrupt on digital PIN 2
    cli();
    
    PORTD |= (1 << PORTD2);    // turn On the Pull-up
    
    EICRA |= (1 << ISC00);    //
    EICRA |= (1 << ISC01);    // set INT0 to trigger on rising edge
    
    EIMSK |= (1 << INT0);     // Turns on INT0 - external interrupt mask register
    
    sei();
    
    clear(2);
    clear(1);
    
    while(1){
        
            if(i<100 && i>=1){
    
                nr_2=i%10;
                nr_1=i/10;
                
                if(nr_1>=1){
                    if(nr_1==0){
                        zero(1);
                    }
                    if(nr_1==1){
                        one(1);
                    }
                    if(nr_1==2){
                        two(1);
                    }
                    if(nr_1==3){
                        three(1);
                    }
                    if(nr_1==4){
                        four(1);
                    }
                    if(nr_1==5){
                        five(1);
                    }
                    if(nr_1==6){
                        six(1);
                    }
                    if(nr_1==7){
                        seven(1);
                    }
                    if(nr_1==8){
                        eight(1);
                    }
                    if(nr_1==9){
                        nine(1);
                    }
                }
                
                if(nr_2==0){
                    zero(2);
                }
                if(nr_2==1){
                    one(2);
                }
                if(nr_2==2){
                    two(2);
                }
                if(nr_2==3){
                    three(2);
                }
                if(nr_2==4){
                    four(2);
                }
                if(nr_2==5){
                    five(2);
                }
                if(nr_2==6){
                    six(2);
                }
                if(nr_2==7){
                    seven(2);
                }
                if(nr_2==8){
                    eight(2);
                }
                if(nr_2==9){
                    nine(2);
                }
                
            }
            else{
                i=0;
                clear(2);
                clear(1);
                
            }
        
            EEPROM_write(Address, i);
        
    }
    
	return 0;     
}
 void checkRegret(void) {
   ExpectationQuad<Real>::checkRegret();
   // Check v'(eps)
   Real x = eps_, two(2), p1(0.1), zero(0), one(1);
   Real vx(0), vy(0);
   Real dv = regret(x,1);
   Real t(1), diff(0), err(0);
   std::cout << std::right << std::setw(20) << "CHECK REGRET: v'(eps) is correct? \n";
   std::cout << std::right << std::setw(20) << "t"
                           << std::setw(20) << "v'(x)"
                           << std::setw(20) << "(v(x+t)-v(x-t))/2t"
                           << std::setw(20) << "Error"
                           << "\n";
   for (int i = 0; i < 13; i++) {
     vy = regret(x+t,0);
     vx = regret(x-t,0);
     diff = (vy-vx)/(two*t);
     err = std::abs(diff-dv);
     std::cout << std::scientific << std::setprecision(11) << std::right
               << std::setw(20) << t
               << std::setw(20) << dv
               << std::setw(20) << diff
               << std::setw(20) << err
               << "\n";
     t *= p1;
   }
   std::cout << "\n";
   // check v''(eps) 
   vx = zero;
   vy = zero;
   dv = regret(x,2);
   t = one;
   diff = zero;
   err = zero;
   std::cout << std::right << std::setw(20) << "CHECK REGRET: v''(eps) is correct? \n";
   std::cout << std::right << std::setw(20) << "t"
                           << std::setw(20) << "v''(x)"
                           << std::setw(20) << "(v'(x+t)-v'(x-t))/2t"
                           << std::setw(20) << "Error"
                           << "\n";
   for (int i = 0; i < 13; i++) {
     vy = regret(x+t,1);
     vx = regret(x-t,1);
     diff = (vy-vx)/(two*t);
     err = std::abs(diff-dv);
     std::cout << std::scientific << std::setprecision(11) << std::right
               << std::setw(20) << t
               << std::setw(20) << dv
               << std::setw(20) << diff
               << std::setw(20) << err
               << "\n";
     t *= p1;
   }
   std::cout << "\n"; 
   // Check v'(0)
   x = zero;
   vx = zero;
   vy = zero;
   dv = regret(x,1);
   t = one;
   diff = zero;
   err = zero;
   std::cout << std::right << std::setw(20) << "CHECK REGRET: v'(0) is correct? \n";
   std::cout << std::right << std::setw(20) << "t"
                           << std::setw(20) << "v'(x)"
                           << std::setw(20) << "(v(x+t)-v(x-t))/2t"
                           << std::setw(20) << "Error"
                           << "\n";
   for (int i = 0; i < 13; i++) {
     vy = regret(x+t,0);
     vx = regret(x-t,0);
     diff = (vy-vx)/(two*t);
     err = std::abs(diff-dv);
     std::cout << std::scientific << std::setprecision(11) << std::right
               << std::setw(20) << t
               << std::setw(20) << dv
               << std::setw(20) << diff
               << std::setw(20) << err
               << "\n";
     t *= p1;
   }
   std::cout << "\n";
   // check v''(eps) 
   vx = zero;
   vy = zero;
   dv = regret(x,2);
   t = one;
   diff = zero;
   err = zero;
   std::cout << std::right << std::setw(20) << "CHECK REGRET: v''(0) is correct? \n";
   std::cout << std::right << std::setw(20) << "t"
                           << std::setw(20) << "v''(x)"
                           << std::setw(20) << "(v'(x+t)-v'(x-t))/2t"
                           << std::setw(20) << "Error"
                           << "\n";
   for (int i = 0; i < 13; i++) {
     vy = regret(x+t,1);
     vx = regret(x-t,1);
     diff = (vy-vx)/(two*t);
     err = std::abs(diff-dv);
     std::cout << std::scientific << std::setprecision(11) << std::right
               << std::setw(20) << t
               << std::setw(20) << dv
               << std::setw(20) << diff
               << std::setw(20) << err
               << "\n";
     t *= p1;
   }
   std::cout << "\n"; 
   // Check v'(0)
   x = -eps_;
   vx = zero;
   vy = zero;
   dv = regret(x,1);
   t = one;
   diff = zero;
   err = zero;
   std::cout << std::right << std::setw(20) << "CHECK REGRET: v'(-eps) is correct? \n";
   std::cout << std::right << std::setw(20) << "t"
                           << std::setw(20) << "v'(x)"
                           << std::setw(20) << "(v(x+t)-v(x-t))/2t"
                           << std::setw(20) << "Error"
                           << "\n";
   for (int i = 0; i < 13; i++) {
     vy = regret(x+t,0);
     vx = regret(x-t,0);
     diff = (vy-vx)/(two*t);
     err = std::abs(diff-dv);
     std::cout << std::scientific << std::setprecision(11) << std::right
               << std::setw(20) << t
               << std::setw(20) << dv
               << std::setw(20) << diff
               << std::setw(20) << err
               << "\n";
     t *= p1;
   }
   std::cout << "\n";
   // check v''(eps) 
   vx = zero;
   vy = zero;
   dv = regret(x,2);
   t = one;
   diff = zero;
   err = zero;
   std::cout << std::right << std::setw(20) << "CHECK REGRET: v''(-eps) is correct? \n";
   std::cout << std::right << std::setw(20) << "t"
                           << std::setw(20) << "v''(x)"
                           << std::setw(20) << "(v'(x+t)-v'(x-t))/2t"
                           << std::setw(20) << "Error"
                           << "\n";
   for (int i = 0; i < 13; i++) {
     vy = regret(x+t,1);
     vx = regret(x-t,1);
     diff = (vy-vx)/(two*t);
     err = std::abs(diff-dv);
     std::cout << std::scientific << std::setprecision(11) << std::right
               << std::setw(20) << t
               << std::setw(20) << dv
               << std::setw(20) << diff
               << std::setw(20) << err
               << "\n";
     t *= p1;
   }
   std::cout << "\n"; 
 }
Esempio n. 3
0
    Element operator()(const mult<Element>&, const Element& v) const
    { 
	return one(v) / v ; 
    } 
Esempio n. 4
0
File: node.cpp Progetto: KDE/abakus
Abakus::Number BuiltinFunction::derivative() const
{
    Abakus::Number du = operand()->derivative();
    Abakus::Number value = operand()->value();
    Abakus::Number one(1), zero(0);

    if(du == zero)
        return du;

    // In case these functions get added later, these derivatives may
    // be useful:
    // d/dx(asinh u) = (du/dx * 1 / sqrt(x^2 + 1))
    // d/dx(acosh u) = (du/dx * 1 / sqrt(x^2 - 1))
    // d/dx(atanh u) = (du/dx * 1 / (1 - x^2))

    // This is very unfortunate duplication.
    if(name() == "sin")
        return value.cos() * du;
    else if(name() == "cos")
        return -value.sin() * du;
    else if(name() == "tan") {
        Abakus::Number cosResult;

        cosResult = value.cos();
        cosResult = cosResult * cosResult;
        return one / cosResult;
    }
    else if(name() == "asinh") {
        value = value * value + one;
        return du / value.sqrt();
    }
    else if(name() == "acosh") {
        value = value * value - one;
        return du / value.sqrt();
    }
    else if(name() == "atanh") {
        value = one - value * value;
        return du / value;
    }
    else if(name() == "sinh") {
        return du * value.cosh();
    }
    else if(name() == "cosh") {
        return du * value.sinh(); // Yes the sign is correct.
    }
    else if(name() == "tanh") {
        Abakus::Number tanh = value.tanh();

        return du * (one - tanh * tanh);
    }
    else if(name() == "atan") {
        return one * du / (one + value * value);
    }
    else if(name() == "acos") {
        // Same as asin but with inverted sign.
        return -(one / (value * value - one).sqrt() * du);
    }
    else if(name() == "asin") {
        return one / (value * value - one).sqrt() * du;
    }
    else if(name() == "ln") {
        return du / value;
    }
    else if(name() == "exp") {
        return du * value.exp();
    }
    else if(name() == "log") {
        return du / value / Abakus::Number(10).ln();
    }
    else if(name() == "sqrt") {
        Abakus::Number half("0.5");
        return half * value.pow(-half) * du;
    }
    else if(name() == "abs") {
        return (value / value.abs()) * du;
    }

    // Approximate it.

    Abakus::Number epsilon("1e-15");
    Abakus::Number fxh = evaluateFunction(function(), value + epsilon);
    Abakus::Number fx = evaluateFunction(function(), value);
    return (fxh - fx) / epsilon;
}
/********************************************************
 *   Internal Envelope Building Ops (the heave lifting)
 ********************************************************/
void Discrete_upper_envelope::build_ric_with_slope_unique_lines_sorted_by_slope(
        std::list< const Line* >& sorted_lines,
        Rand_order_mem& rand_order,
        Building_envelope_mem& building_envelope01,
        Building_envelope_mem& building_envelope02) {

    // typedefs
    typedef Rand_order_mem::iterator Rand_order_iter;
    typedef Building_envelope::iterator Building_env_iter;

    // checks
    rAssert(rand_order.size() == sorted_lines.size());

    // compute the random insertion order
    int bin_begin = 1 << DDAD_util::log2(sorted_lines.size());
    int bin_size = sorted_lines.size()-bin_begin;
    while(bin_begin >= 1) {
        Shuffle::standard_sequential_sample_with_removal(
                rand_order.begin()+bin_begin, sorted_lines, bin_size);
        bin_size = bin_begin = bin_begin >> 1;
    }
    rAssert(sorted_lines.size() == 1);
    Shuffle::standard_sequential_sample_with_removal(
            rand_order.begin(), sorted_lines, 1);
    rLog(up_env_ric, "Random insertion order:\n%s",
            vect_to_cstring(rand_order.begin(), rand_order.end()));

    // init the envelopes for building between
    // the largest a building envelope could be is all
    // lines plus the end sentinal line.
    Line neg_inf(INT_MIN, INT_MIN);
    Line pos_inf(INT_MAX, INT_MAX);

    building_envelope01.resize(0);
    building_envelope02.resize(0);
    Building_envelope *build_env = &building_envelope01;
    Building_envelope *next_env = &building_envelope02;
    build_env->push_back(Building_envelope_cell(&neg_inf, one()-1));
    build_env->push_back(Building_envelope_cell(rand_order.begin()->line, one()));
    build_env->push_back(Building_envelope_cell(&pos_inf, U()+1));


    // init vars for adding
    int delta = 1, num_lines = (int)rand_order.size();
    int end_offset = 0, begin_offset = std::min(end_offset+delta, num_lines-1);
    Building_env_iter end, begin;

    // add the lines in increasing sizes of powers of 2
    while (end_offset < num_lines-1) {

        begin = rand_order.begin() + begin_offset;
        end = rand_order.begin() + end_offset;

        // log the current envelope and the lines processed in this round
        rLog(up_env_ric, "Building envelope:\n%s",
                vect_to_cstring(build_env->begin(), build_env->end()));
        rLog(up_env_ric, "Process:\n%s", vect_to_cstring(end+1, begin+1));

        // for each line l identify the left most cell intersected by l
        // (if such a cell exists)
        // the order of the slopes of the upper envelope
        // must be ordered by slope so we can treat this as a
        // merge operation
        Building_env_iter c_j = build_env->begin()+build_env->size()-1;
        for (Rand_order_iter l = begin ; l != end ; --l) {

            // log the line that we are processing this iteration
            rLog(up_env_ric, "Finding intersections for %s",
                    thing_to_cstring(*(l->line)));

            // advance along the upper envelope until
            // the slope of l is between the slope of two lines of the
            // upper envelope
            while ( c_j != build_env->begin()
                    && line_slope_lt(*(l->line), *(c_j->line)) ) { --c_j; }

            // this assertion verifys that we found the correct possible
            // insertion point for l
            rAssert( c_j != build_env->end() && (c_j+1) != build_env->end() );
            rAssert( line_slope_lt(*(c_j)->line,*(l->line)));
            rAssert( line_slope_lt(*(l->line), *(c_j+1)->line) );
            rLog(up_env_ric, "First cell with smaller slope\n%s",
                    thing_to_cstring(*c_j));

            // next we walk backwards thought the cells,
            // to find the left most cell intersected by l->line
            // left_most will store the current left most
            // cell intersected by l->line.  If
            // at the end left_most is still build_env->end()
            // then l->line did not intersect any cells
            Building_env_iter left_most = (c_j+2 != build_env->end()
                    && line_above_or_on(*(l->line),*(c_j+1)->line, (c_j+1)->left)) ?
                (c_j+1) : build_env->end();

            for (Building_env_iter inter = c_j; inter != build_env->begin()
                    && line_above(*(l->line), *(inter->line), (inter+1)->left) ;
                    --inter) {
                left_most = inter;
                rLog(up_env_ric, "Intersection with \n %s", thing_to_cstring(*inter));
            }
            rAssert(left_most != build_env->begin());

            // if the cell intersected any cells
            // set the intersection flag for the left most intersection cell
            if (left_most != build_env->end()) {
                l->ptr = left_most->ptr;
                left_most->ptr = &(*l);
            }
            rLog(up_env_ric, "Intersection test ends at \n %s",
                    (left_most == build_env->end()) ?
                    "NO_INTERS" : thing_to_cstring(*left_most));
        }

        // log the current envelope after all intersections found
        rLog(up_env_ric, "Building envelope after intersections:\n%s",
                vect_to_cstring(build_env->begin(), build_env->end()));

        // set up the next upper envelope
        next_env->push_back(build_env->front());
        Building_envelope_cell* left_list = &(*(build_env->begin()));
        Building_envelope_cell* left_list_tail = &(*(build_env->begin()));
        Building_env_iter inf_cell = build_env->begin()+(build_env->size()-1);

        // compute the upper envelope in each cell
        for (Building_env_iter ci = build_env->begin()+1 ; ci != inf_cell ; ++ci) {

            // log the cell we are updating
            rLog(up_env_ric, "Cell to update\n%s", thing_to_cstring(*ci));

            // construct and filter the three sorted list of lines
            Three_sorted_lists_of_lines L(*ci, left_list, left_list_tail);
            rLog(up_env_ric, "Init three sorted lists:\n%s", thing_to_cstring(L));
            L.filter_below();

            // compute the discrete upper envelope of the intersecting lines
            due_w_bin_search_of_sorted_and_filtered_lines(
                    *next_env, L, ci->left, (ci+1)->left-1);

            // log the envelope
            rLog(up_env_ric, "Next Envelope\n%s",
                    vect_to_cstring(next_env->begin(), next_env->end()));

            // update the left list by filtering all lines that are before
            // the filter in the all sorted list
            L.filter_all_sorted(*(next_env->back().line));
            left_list = L.all_sorted_head.ptr;
            left_list_tail = L.all_sorted_tail;
            L.all_sorted_head.ptr = NULL;
        }

        // copy the infinite cell to the end
        next_env->push_back(build_env->back());

        // log the current envelope after all intersections found
        rLog(up_env_ric, "Next Envelope:\n%s",
                vect_to_cstring(next_env->begin(), next_env->end()));

        // update for the next round
        std::swap(next_env, build_env);
        next_env->resize(0);

        delta *= 2;
        end_offset = begin_offset;
        begin_offset = std::min(end_offset+delta, num_lines-1);
    }


    // copy build envelope over to envelope
    building_envelope_to_envelope(envelope_, *build_env);
}
 Real regret(Real x, int deriv = 0) {
   Real zero(0), one(1);
   Real X = ((deriv==0) ? x : ((deriv==1) ? one : zero));
   Real reg = error(x,deriv) + X;
   return reg;
 }
Esempio n. 7
0
// PR c++/54020
// { dg-do compile { target c++11 } }

// Preliminaries.
extern int nonconst_func(int);
constexpr int identity(int x) { return x; }
constexpr int zero() { return identity(0); }
constexpr int one() { return identity(1); }

// Correctly accepted.
constexpr int three = one() ? 3 : nonconst_func(0);

// Incorrectly accepted.  See [dcl.constexpr] #5:
//   For a constexpr function, if no function argument values exist
//   such that the function invocation sub-stitution would produce a
//   constant expression (5.19), the program is ill-formed; no diagnostic
//   required.
constexpr int bogus() { return zero () ? 3 : nonconst_func(0); } // { dg-error "nonconst_func" }

// Correctly rejected (not sure why).
constexpr int correct_error() { return nonconst_func(0); } // { dg-error "nonconst_func" }

// Correctly rejected.
constexpr int z = bogus();	// { dg-error "" }

// This is also correctly rejected.
constexpr int correct_failure() { return 0 ? 3 : nonconst_func(0); } // { dg-error "nonconst_func" }
Esempio n. 8
0
 forceinline ModEvent
 BoolVarImp::nq(Space& home, double n) {
   if ((n < 0) || (n > 1)) return ME_INT_NONE;
   return (n == 0) ? one(home): zero(home);
 }
Esempio n. 9
0
one foo(const A&) {return one();}
Esempio n. 10
0
 forceinline ModEvent
 BoolVarImp::gq(Space& home, double n) {
   if (n <= 0) return ME_INT_NONE;
   if (n > 1)  return ME_INT_FAILED;
   return one(home);
 }
Esempio n. 11
0
 forceinline ModEvent
 BoolVarImp::eq(Space& home, double n) {
   if ((n < 0) || (n > 1)) return ME_INT_FAILED;
   return (n == 0) ? zero(home): one(home);
 }
Esempio n. 12
0
 forceinline ModEvent
 BoolVarImp::one(Space& home) {
   if (one())  return ME_BOOL_NONE;
   if (zero()) return ME_BOOL_FAILED;
   return one_none(home);
 }
Esempio n. 13
0
//==================================================================
void SimplePrimitiveBase::Dice(
					WorkGrid &g,
					bool doColorCoded ) const
{
	//Float3_	*pP = g.mpPointsCS;

	// NOTE: all spatial and directional values are in "current" (camera) space
	Float3_	*pP		= (Float3_	*)g.mSymbolIs.FindSymbolIData( "P"	);
	Float_	*pOODu	= (Float_	*)g.mSymbolIs.FindSymbolIData( "_oodu" );
	Float_	*pOODv	= (Float_	*)g.mSymbolIs.FindSymbolIData( "_oodv" );
	Float_	*pu		= (Float_	*)g.mSymbolIs.FindSymbolIData( "u" );
	Float_	*pv		= (Float_	*)g.mSymbolIs.FindSymbolIData( "v" );
	Float3_	*pI		= (Float3_	*)g.mSymbolIs.FindSymbolIData( "I"	);
	Float3_	*pN		= (Float3_	*)g.mSymbolIs.FindSymbolIData( "N"	);
	Float3_	*pNg	= (Float3_	*)g.mSymbolIs.FindSymbolIData( "Ng"	);
	SlColor	*pOs	= (SlColor	*)g.mSymbolIs.FindSymbolIData( "Os"	);
	SlColor	*pCs	= (SlColor	*)g.mSymbolIs.FindSymbolIData( "Cs"	);

	DASSERT( pP == g.mpPointsCS );

	float	du = 1.0f / (g.mXDim-1);
	float	dv = 1.0f / (g.mYDim-1);

	Matrix44 mtxLocalCameraNorm = g.mMtxLocalCamera.GetAs33();

	if ( mpAttribs->mOrientationFlipped )
		mtxLocalCameraNorm = mtxLocalCameraNorm * Matrix44::Scale( -1, -1, -1 );

	g.mMtxLocalCameraNorm	= mtxLocalCameraNorm;

	Float3	camPosCS = g.mMtxWorldCamera.GetTranslation();

	SlColor	useColor;
	SlColor	useOpa;

	if ( doColorCoded )
	{
		const static float c0 = 0.0f;
		const static float c1 = 0.33f;
		const static float c2 = 0.66f;
		const static float c3 = 1.0f;
		static Color palette[16] =
		{
			Color( c0, c0, 1-c0 ),
			Color( c1, c0, 1-c1 ),
			Color( c2, c0, 1-c2 ),
			Color( c3, c0, 1-c3 ),
			Color( c0, c0, 1-c0 ),
			Color( c1, c0, 1-c1 ),
			Color( c2, c0, 1-c2 ),
			Color( c3, c0, 1-c3 ),

			Color( c0, c1, 1-c0 ),
			Color( c1, c1, 1-c1 ),
			Color( c2, c2, 1-c2 ),
			Color( c3, c2, 1-c3 ),
			Color( c0, c3, 1-c0 ),
			Color( c1, c3, 1-c1 ),
			Color( c2, c3, 1-c2 ),
			Color( c3, c3, 1-c3 ),
		};
/*
		const static float l = 0.3f;
		const static float m = 0.6f;
		const static float h = 1.0f;
		static Color palette[8] =
		{
			Color( l, l, l ),
			Color( l, l, h ),
			Color( l, h, l ),
			Color( l, h, h ),
			Color( h, l, l ),
			Color( h, l, h ),
			Color( h, h, l ),
			Color( h, h, h ),
		};
*/
		static int cnt;

		useColor = palette[ cnt++ & 15 ];
		useOpa	 = SlColor( 1, 1, 1 );
	}
	else
	{
		useColor = mpAttribs->mColor;
		useOpa	 = mpAttribs->mOpacity;
	}

	// build the UVs
	Float2_	locUV[ MP_GRID_MAX_SIZE_SIMD_BLKS ];
	Float2_	locDUDV[ MP_GRID_MAX_SIZE_SIMD_BLKS ];

	fillUVsArray( locUV, locDUDV, du, dv, g.mXDim, g.mYDim );

	//DASSERT( sampleIdx == g.mPointsN );

	size_t	blocksN = DMT_SIMD_BLOCKS( g.mPointsN );

	Float_	one( 1.0f );

	for (size_t blkIdx=0; blkIdx < blocksN; ++blkIdx)
	{
#if 1
		Float3_	dPdu;
		Float3_	dPdv;
		Float3_	posLS;

		Eval_dPdu_dPdv( locUV[blkIdx], posLS, &dPdu, &dPdv );

		if ( posLS[0][0] == 0 && posLS[0][1] == 0 && posLS[0][2] == 0 )
			continue;

		Float3_	norLS = dPdu.GetCross( dPdv ).GetNormalized();
		Float3_	posCS = V3__V3W1_Mul_M44<Float_>( posLS, g.mMtxLocalCamera );
		Float3_	norCS = V3__V3W1_Mul_M44<Float_>( norLS, mtxLocalCameraNorm ).GetNormalized();

		pP[blkIdx]		= posCS;

		pI[blkIdx]		= (posCS - -camPosCS);//.GetNormalized();
		pOODu[blkIdx]	= one / locDUDV[blkIdx][0];
		pOODv[blkIdx]	= one / locDUDV[blkIdx][1];

		pu[blkIdx]		= locUV[blkIdx][0];
		pv[blkIdx]		= locUV[blkIdx][1];

		pN[blkIdx]		= norCS;
		pNg[blkIdx]		= norCS;
		pOs[blkIdx]		= useOpa;
		pCs[blkIdx]		= useColor;

#else
		Float2_	&tmpvblk = locUV[ blkIdx ];

		for (size_t	itmIdx=0; itmIdx < DMT_SIMD_FLEN; ++itmIdx)
		{
			Float3	dPdu;
			Float3	dPdv;
			Float3	posLS;
			Eval_dPdu_dPdv(	locUV[ blkIdx ][0][ itmIdx ],
							locUV[ blkIdx ][1][ itmIdx ],
							posLS,
							&dPdu,
							&dPdv );

			Float3	norLS = dPdu.GetCross( dPdv ).GetNormalized();
			Float3	posWS = V3__V3W1_Mul_M44<float>( posLS, g.mMtxLocalWorld );
			Float3	posCS = V3__V3W1_Mul_M44<float>( posLS, g.mMtxLocalCamera );
			Float3	norCS = V3__V3W1_Mul_M44<float>( norLS, mtxLocalCameraNorm ).GetNormalized();

			// store in blocked SOA format

			pP[blkIdx][0][itmIdx] = posWS[0];
			pP[blkIdx][1][itmIdx] = posWS[1];
			pP[blkIdx][2][itmIdx] = posWS[2];

			Float3	tmpI = (posCS - -camPosCS).GetNormalized();
			pI[blkIdx][0][itmIdx] = tmpI[0];
			pI[blkIdx][1][itmIdx] = tmpI[1];
			pI[blkIdx][2][itmIdx] = tmpI[2];

			pN[blkIdx][0][itmIdx] = norCS[0];
			pN[blkIdx][1][itmIdx] = norCS[1];
			pN[blkIdx][2][itmIdx] = norCS[2];

			pNg[blkIdx][0][itmIdx] = norCS[0];
			pNg[blkIdx][1][itmIdx] = norCS[1];
			pNg[blkIdx][2][itmIdx] = norCS[2];
		}

		pOs[blkIdx]	= useOpa;
		pCs[blkIdx]	= useColor;
#endif
	}
}
Esempio n. 14
0
static void tst3() {
    enable_trace("nlsat_interval");
    unsynch_mpq_manager         qm;
    anum_manager                am(qm);
    small_object_allocator      allocator;
    nlsat::interval_set_manager ism(am, allocator);

    scoped_anum               sqrt2(am), m_sqrt2(am), two(am), m_two(am), three(am), one(am), zero(am);
    am.set(two, 2);
    am.set(m_two, -2);
    am.set(one, 1);
    am.root(two, 2, sqrt2);
    am.set(m_sqrt2, sqrt2);
    am.neg(m_sqrt2);
    am.set(three, 3);
    
    nlsat::literal p1(1, false);
    nlsat::literal p2(2, false);
    nlsat::literal p3(3, false);
    nlsat::literal p4(4, false);
    nlsat::literal np2(2, true);
    
    nlsat::interval_set_ref s1(ism), s2(ism), s3(ism), s4(ism);
    s1 = ism.mk_empty();
    std::cout << "s1: " << s1 << "\n";
    s2 = ism.mk(true, true, zero, false, false, sqrt2, np2);
    std::cout << "s2: " << s2 << "\n";
    s3 = ism.mk(false, false, zero, false, false, two, p1);
    std::cout << "s3: " << s3 << "\n";
    s4 = ism.mk_union(s2, s3);
    std::cout << "s4: " << s4 << "\n";

    // Case
    //  s1:   [ ... ]
    //  s2:   [ ... ]
    s1 = ism.mk(false, false, zero, false, false, two, p1);
    s2 = ism.mk(false, false, zero, false, false, two, p2);
    tst_interval(s1, s2, 1);

    // Case 
    // s1:   [ ... ]
    // s2: [ ... ]
    s1 = ism.mk(false, false, zero, false, false, two, p1);
    s2 = ism.mk(false, false, m_sqrt2, false, false, one, p2);
    s3 = ism.mk_union(s1, s2);
    tst_interval(s1, s2, 2);

    // Case 
    // s1:   [ ... ]
    // s2:      [ ... ]
    s1 = ism.mk(false, false, m_sqrt2, false, false, one, p1);
    s2 = ism.mk(false, false, zero, false, false, two, p2);
    tst_interval(s1, s2, 2);

    // Case 
    // s1:   [ ... ]
    // s2:            [ ... ]
    s1 = ism.mk(false, false, m_sqrt2, false, false, one, p1);
    s2 = ism.mk(false, false, two, false, false, three, p2);
    tst_interval(s1, s2, 2);

    // Case 
    // s1:   [    ...    ]
    // s2:      [ ... ]
    s1 = ism.mk(false, false, m_sqrt2, false, false, three, p1);
    s2 = ism.mk(false, false, zero, false, false, two, p2);
    tst_interval(s1, s2, 1);

    // Case 
    // s1:   [    ...      ]
    // s2:      [ ... ] [  ...  ]
    s1 = ism.mk(false, false, m_two, false, false, two, p1);
    s2 = ism.mk(false, false, m_sqrt2, false, false, zero, p2);
    s3 = ism.mk(false, false, one, false, false, three, p2);
    s2 = ism.mk_union(s2, s3);
    tst_interval(s1, s2, 2);

    // Case
    // s1:  [ ... ]
    // s2:        [ ... ]
    s1 = ism.mk(false, false, m_two, false, false, two, p1);
    s2 = ism.mk(false, false, two, false, false, three, p2);
    tst_interval(s1, s2, 2);
    s2 = ism.mk(true, false, two, false, false, three, p2);
    tst_interval(s1, s2, 2);
    s2 = ism.mk(true, false, two, false, false, three, p1);
    tst_interval(s1, s2, 1);
    s1 = ism.mk(false, false, m_two, true, false, two, p1);
    tst_interval(s1, s2, 2);
    s1 = ism.mk(false, false, two, false, false, two, p1);
    s2 = ism.mk(false, false, two, false, false, three, p2);
    tst_interval(s1, s2, 1);

    // Case
    // s1:  [ ... ]    [ ...  ]
    // s2: [ .. ]   [ ... ] [ ... ]
    s1 = ism.mk(false, false, m_two, false, false, zero, p1);
    s3 = ism.mk(false, false, one, false, false,   three, p1);
    s1 = ism.mk_union(s1, s3);
    s2 = ism.mk(true, true, zero,  false, false, m_sqrt2, p2);
    tst_interval(s1, s2, 3);
    s3 = ism.mk(false, false, one, false, false, sqrt2, p2);
    s2 = ism.mk_union(s2, s3);
    s3 = ism.mk(false, false, two, true, true, zero, p2);
    s2 = ism.mk_union(s2, s3);
    tst_interval(s1, s2, 4);

    // Case
    s1 = ism.mk(true, true, zero, false, false, one, p1);
    s2 = ism.mk(true, false, one, true, true, zero, p2);
    tst_interval(s1, s2, 2);
    s2 = ism.mk(true, false, one, false, false, two, p2);
    s3 = ism.mk(false, false, two, true, true, zero, p1);
    s2 = ism.mk_union(s2, s3);
    tst_interval(s1, s2, 3);
}
Esempio n. 15
0
  void run( Vector<Real> &s, Real &snorm, Real &del, int &iflag, int &iter, const Vector<Real> &x,
            const Vector<Real> &grad, const Real &gnorm, ProjectedObjective<Real> &pObj ) { 
    Real tol = std::sqrt(ROL_EPSILON<Real>()), zero(0), one(1), two(2), half(0.5);
    const Real gtol = std::min(tol1_,tol2_*gnorm);

    // Gradient Vector
    g_->set(grad);
    Real normg = gnorm;
    if ( pObj.isConActivated() ) {
      primalVector_->set(grad.dual());
      pObj.pruneActive(*primalVector_,grad.dual(),x);
      g_->set(primalVector_->dual());
      normg = g_->norm();
    }

    // Old and New Step Vectors
    s.zero(); s_->zero();
    snorm = zero;
    Real snorm2(0), s1norm2(0);

    // Preconditioned Gradient Vector
    //pObj.precond(*v,*g,x,tol);
    pObj.reducedPrecond(*v_,*g_,x,grad.dual(),x,tol);

    // Basis Vector
    p_->set(*v_);
    p_->scale(-one);
    Real pnorm2 = v_->dot(g_->dual());

    iter = 0; iflag = 0;
    Real kappa(0), beta(0), sigma(0), alpha(0), tmp(0), sMp(0);
    Real gv = v_->dot(g_->dual());
    pRed_ = zero;

    for (iter = 0; iter < maxit_; iter++) {
      //pObj.hessVec(*Hp,*p,x,tol);
      pObj.reducedHessVec(*Hp_,*p_,x,grad.dual(),x,tol);

      kappa = p_->dot(Hp_->dual());
      if (kappa <= zero) {
        sigma = (-sMp+sqrt(sMp*sMp+pnorm2*(del*del-snorm2)))/pnorm2;
        s.axpy(sigma,*p_);
        iflag = 2; 
        break;
      }

      alpha = gv/kappa;
      s_->set(s); 
      s_->axpy(alpha,*p_);
      s1norm2 = snorm2 + two*alpha*sMp + alpha*alpha*pnorm2;

      if (s1norm2 >= del*del) {
        sigma = (-sMp+sqrt(sMp*sMp+pnorm2*(del*del-snorm2)))/pnorm2;
        s.axpy(sigma,*p_);
        iflag = 3; 
        break;
      }

      pRed_ += half*alpha*gv;

      s.set(*s_);
      snorm2 = s1norm2;  

      g_->axpy(alpha,*Hp_);
      normg = g_->norm();
      if (normg < gtol) {
        break;
      }

      //pObj.precond(*v,*g,x,tol);
      pObj.reducedPrecond(*v_,*g_,x,grad.dual(),x,tol);
      tmp   = gv; 
      gv    = v_->dot(g_->dual());
      beta  = gv/tmp;    

      p_->scale(beta);
      p_->axpy(-one,*v_);
      sMp    = beta*(sMp+alpha*pnorm2);
      pnorm2 = gv + beta*beta*pnorm2; 
    }
    if (iflag > 0) {
      pRed_ += sigma*(gv-half*sigma*kappa);
    }

    if (iter == maxit_) {
      iflag = 1;
    }
    if (iflag != 1) { 
      iter++;
    }

    snorm = s.norm();
    TrustRegion<Real>::setPredictedReduction(pRed_);
  }
Esempio n. 16
0
FloatingPoint Sampler::pickFpBiased(unsigned e, unsigned s)
{
  // The biased generation of random FP values is inspired by
  // PyMPF [0].
  //
  // [0] https://github.com/florianschanda/PyMPF

  Random& rnd = Random::getRandom();

  BitVector zero(1);
  BitVector one(1, static_cast<unsigned int>(1));

  BitVector sign(1);
  BitVector exp(e);
  BitVector sig(s - 1);

  if (rnd.pickWithProb(probSpecial))
  {
    // Generate special values

    uint64_t type = rnd.pick(0, 12);
    switch (type)
    {
      // NaN
      // sign = 1, exp = 11...11, sig = 11...11
      case 0:
        sign = one;
        exp = BitVector::mkOnes(e);
        sig = BitVector::mkOnes(s - 1);
        break;

      // +/- inf
      // sign = x, exp = 11...11, sig = 00...00
      case 1:
        sign = one;
        // Intentional fall-through
      case 2: exp = BitVector::mkOnes(e); break;

      // +/- zero
      // sign = x, exp = 00...00, sig = 00...00
      case 3:
        sign = one;
        // Intentional fall-through
      case 4: break;

      // +/- max subnormal
      // sign = x, exp = 00...00, sig = 11...11
      case 5:
        sign = one;
        // Intentional fall-through
      case 6: sig = BitVector::mkOnes(s - 1); break;

      // +/- min subnormal
      // sign = x, exp = 00...00, sig = 00...01
      case 7:
        sign = one;
        // Intentional fall-through
      case 8: sig = BitVector(s - 1, static_cast<unsigned int>(1)); break;

      // +/- max normal
      // sign = x, exp = 11...10, sig = 11...11
      case 9:
        sign = one;
        // Intentional fall-through
      case 10:
        exp = BitVector::mkOnes(e) - BitVector(e, static_cast<unsigned int>(1));
        sig = BitVector::mkOnes(s - 1);
        break;

      // +/- min normal
      // sign = x, exp = 00...01, sig = 00...00
      case 11:
        sign = one;
        // Intentional fall-through
      case 12: exp = BitVector(e, static_cast<unsigned int>(1)); break;

      default: Unreachable();
    }
  }
  else
  {
    // Generate normal and subnormal values

    // 50% chance of positive/negative sign
    if (rnd.pickWithProb(0.5))
    {
      sign = one;
    }

    uint64_t pattern = rnd.pick(0, 5);
    switch (pattern)
    {
      case 0:
        // sign = x, exp = xx...x0, sig = 11...11
        exp = pickBvUniform(e - 1).concat(zero);
        sig = BitVector::mkOnes(s - 1);
        break;

      case 1:
        // sign = x, exp = xx...x0, sig = 00...00
        exp = pickBvUniform(e - 1).concat(zero);
        break;

      case 2:
        // sign = x, exp = 0x...x1, sig = 11...11
        exp = zero.concat(pickBvUniform(e - 2).concat(one));
        sig = BitVector::mkOnes(s - 1);
        break;

      case 3:
        // sign = x, exp = xx...x0, sig = xx...xx
        exp = pickBvUniform(e - 1).concat(zero);
        sig = pickBvUniform(s - 1);
        break;

      case 4:
        // sign = x, exp = 0x...x1, sig = xx...xx
        exp = zero.concat(pickBvUniform(e - 2).concat(one));
        sig = pickBvUniform(s - 1);
        break;

      case 5:
      {
        // sign = x, exp = xx...xx0xx...xx, sig = xx...xx
        uint64_t lsbSize = rnd.pick(1, e - 2);
        uint64_t msbSize = e - lsbSize - 1;
        BitVector lsb = pickBvUniform(lsbSize);
        BitVector msb = pickBvUniform(msbSize);
        exp = msb.concat(zero.concat(lsb));
        sig = pickBvUniform(s - 1);
        break;
      }

      default: Unreachable();
    }
  }

  BitVector bv = sign.concat(exp.concat(sig));
  return FloatingPoint(e, s, bv);
}
Esempio n. 17
0
SkPDFImage::SkPDFImage(SkStream* stream,
                       const SkBitmap& bitmap,
                       bool isAlpha,
                       const SkIRect& srcRect)
    : fIsAlpha(isAlpha),
      fSrcRect(srcRect) {

    if (bitmap.isImmutable()) {
        fBitmap = bitmap;
    } else {
        bitmap.deepCopyTo(&fBitmap);
        fBitmap.setImmutable();
    }

    if (stream != NULL) {
        this->setData(stream);
        fStreamValid = true;
    } else {
        fStreamValid = false;
    }

    SkColorType colorType = fBitmap.colorType();

    insertName("Type", "XObject");
    insertName("Subtype", "Image");

    bool alphaOnly = (kAlpha_8_SkColorType == colorType);

    if (!isAlpha && alphaOnly) {
        // For alpha only images, we stretch a single pixel of black for
        // the color/shape part.
        SkAutoTUnref<SkPDFInt> one(new SkPDFInt(1));
        insert("Width", one.get());
        insert("Height", one.get());
    } else {
        insertInt("Width", fSrcRect.width());
        insertInt("Height", fSrcRect.height());
    }

    if (isAlpha || alphaOnly) {
        insertName("ColorSpace", "DeviceGray");
    } else if (kIndex_8_SkColorType == colorType) {
        SkAutoLockPixels alp(fBitmap);
        insert("ColorSpace",
               make_indexed_color_space(fBitmap.getColorTable()))->unref();
    } else {
        insertName("ColorSpace", "DeviceRGB");
    }

    int bitsPerComp = 8;
    if (kARGB_4444_SkColorType == colorType) {
        bitsPerComp = 4;
    }
    insertInt("BitsPerComponent", bitsPerComp);

    if (kRGB_565_SkColorType == colorType) {
        SkASSERT(!isAlpha);
        SkAutoTUnref<SkPDFInt> zeroVal(new SkPDFInt(0));
        SkAutoTUnref<SkPDFScalar> scale5Val(
                new SkPDFScalar(8.2258f));  // 255/2^5-1
        SkAutoTUnref<SkPDFScalar> scale6Val(
                new SkPDFScalar(4.0476f));  // 255/2^6-1
        SkAutoTUnref<SkPDFArray> decodeValue(new SkPDFArray());
        decodeValue->reserve(6);
        decodeValue->append(zeroVal.get());
        decodeValue->append(scale5Val.get());
        decodeValue->append(zeroVal.get());
        decodeValue->append(scale6Val.get());
        decodeValue->append(zeroVal.get());
        decodeValue->append(scale5Val.get());
        insert("Decode", decodeValue.get());
    }
}
Esempio n. 18
0
// NB: CuDNN only implements the backward algorithm for batchnorm
// in training mode (evaluation mode batchnorm has a different algorithm),
// which is why this doesn't accept a 'training' parameter.
std::tuple<Tensor, Tensor, Tensor> cudnn_batch_norm_backward(
    const Tensor& input_t, const Tensor& grad_output_t, const Tensor& weight_t,
    // Unused: but we require them to be passed so that double backwards
    // has access
    const Tensor& running_mean, const Tensor& running_var,
    const Tensor& save_mean_t, const Tensor& save_var_t,
    double epsilon)
{
  TensorArg input{ input_t, "input", 1 },
            grad_output{ grad_output_t, "grad_output", 2 },
            weight{ weight_t, "weight", 3 },
            save_mean{ save_mean_t, "save_mean", 4 },
            save_var{ save_var_t, "save_var", 5 };
  CheckedFrom c = "cudnn_batch_norm_backward";
  setCuDNNStreamToCurrent();

  checkAllDefined(c, {input, grad_output, weight, save_mean, save_var});
  checkAllSameGPU(c, {input, grad_output, weight, save_mean, save_var});
  if (input->type().scalarType() == ScalarType::Half) {
    checkScalarType(c, weight, ScalarType::Float);
  } else {
    checkAllSameType(c, {input, weight});
  }
  checkAllSameType(c, {input, grad_output});
  checkAllSameType(c, {weight, save_mean, save_var});
  // TODO: is weight required to be contiguous?
  checkAllContiguous(c, {input, grad_output, save_mean, save_var});
  checkDimRange(c, input, 2, 6 /* exclusive */);
  checkSameSize(c, input, grad_output);
  auto num_features = input->size(1);
  for (auto t : {weight, save_mean, save_var}) {
    checkNumel(c, t, num_features);
  }

  cudnnBatchNormMode_t mode;
  if (input->dim() == 2) {
    mode = CUDNN_BATCHNORM_PER_ACTIVATION;
  } else {
#if CUDNN_VERSION >= 7003
    mode = CUDNN_BATCHNORM_SPATIAL_PERSISTENT;
#else
    mode = CUDNN_BATCHNORM_SPATIAL;
#endif
  }

  auto grad_input_t  = input->type().tensor(input->sizes());
  auto grad_weight_t = weight->type().tensor(weight->sizes());
  auto grad_bias_t   = weight->type().tensor(weight->sizes());

  auto handle = getCudnnHandle();
  auto dataType = getCudnnDataType(*input);

  TensorDescriptor idesc{ *input, 4 };  // input, output, grad_output descriptor
  TensorDescriptor wdesc{ expandScale(*weight, input->dim()), 4 };  // descriptor for weight, bias, save_mean, etc.

  Constant one(dataType, 1);
  Constant zero(dataType, 0);

  CUDNN_CHECK(cudnnBatchNormalizationBackward(
    handle, mode, &one, &zero, &one, &zero,
    idesc.desc(), input->data_ptr(),
    idesc.desc(), grad_output->data_ptr(),
    idesc.desc(), grad_input_t.data_ptr(),
    wdesc.desc(), weight->data_ptr(),
    grad_weight_t.data_ptr(),
    grad_bias_t.data_ptr(),
    epsilon,
    save_mean->data_ptr(),
    save_var->data_ptr()));

  return std::tuple<Tensor,Tensor,Tensor>{grad_input_t, grad_weight_t, grad_bias_t};
}
Esempio n. 19
0
int main()
{
    printf("== one() ==\n");
    one(3, 4);
    one(10, 10);


    printf("== two() ==\n");
    const char* a = "20";
    two(a);
    const char* b = "100";
    two(b);

    printf("== three() ==\n");
    three();


    printf("== four() ==\n");
    four(0.5);
    four(1.5);


    printf("== five() ==\n");
    const int num1 = 3;
    const int num2 = 3;
    five(&num1, &num2);

    const int num3 = 4;
    five(&num1, &num3);


    printf("== six() ==\n");
    float *p_six;
    int i4 = 4, i432 = 432;

    p_six = six(&i4);
    printf("%d == %f\n", i4, *p_six);
    free(p_six);

    p_six = six(&i432);
    printf("%d == %f\n", i432, *p_six);
    free(p_six);


    printf("== seven() ==\n");
    const char s = 'S';
    seven(&s);
    const char t = '_';
    seven(&t);


    printf("== eight() ==\n");
    eight();


    printf("== nine() ==\n");
    nine();


    printf("== ten() ==\n");
    int i_ten = 100;
    ten(&i_ten);
    printf("%d == 0?\n", i_ten);


    printf("== eleven() ==\n");
    eleven();


    printf("== twelve() ==\n");
    twelve();


    printf("== thirteen() ==\n");
    thirteen(10);


    printf("== fourteen() ==\n");
    fourteen("red");
    fourteen("orange");
    fourteen("blue");
    fourteen("green");


    printf("== fifteen() ==\n");
    fifteen(1);
    fifteen(2);
    fifteen(3);


    printf("== sixteen() ==\n");
    char *str = sixteen();
    printf("%s\n", str);
    free(str);


    printf("== seventeen() ==\n");
    seventeen(35);
    seventeen(20);


    printf("== eighteen() ==\n");
    eighteen(3);
    eighteen(5);


    printf("== clear_bits() ==\n");
    long int result;

    result = clear_bits(0xFF, 0x55);
    printf("%ld\n", result);

    result = clear_bits(0x00, 0xF0);
    printf("%ld\n", result);

    result = clear_bits(0xAB, 0x00);
    printf("%ld\n", result);

    result = clear_bits(0xCA, 0xFE);
    printf("%ld\n", result);

    result = clear_bits(0x14, 0x00);
    printf("%ld\n", result);

    result = clear_bits(0xBB, 0xBB);
    printf("%ld\n", result);


    return 0;
}
Esempio n. 20
0
std::tuple<Tensor, Tensor, Tensor> cudnn_batch_norm(
    const Tensor& input_t, const Tensor& weight_t,
    const Tensor& bias_t, const Tensor& running_mean_t, const Tensor& running_var_t,
    bool training, double exponential_average_factor, double epsilon)
{
  TensorArg input{ input_t, "input", 1 },
            weight{ weight_t, "weight", 2 },
            bias{ bias_t, "bias", 3 },
            running_mean{ running_mean_t, "running_mean", 4 },
            running_var{ running_var_t, "running_var", 5 };
  CheckedFrom c = "cudnn_batch_norm";
  setCuDNNStreamToCurrent();

  checkAllDefined(c, {input, weight, bias});
  if (!training) {
    checkAllDefined(c, {running_mean, running_var});
  }
  checkAllSameGPU(c, {input, weight, bias, running_mean, running_var});
  if (input->type().scalarType() == ScalarType::Half) {
    checkScalarType(c, weight, ScalarType::Float);
  } else {
    checkAllSameType(c, {input, weight});
  }
  checkAllSameType(c, {weight, bias, running_mean, running_var});
  // TODO: is weight required to be contiguous?
  checkAllContiguous(c, {input, weight, bias, running_mean, running_var});
  checkDimRange(c, input, 2, 6 /* exclusive */);
  auto num_features = input->size(1);
  for (auto t : {weight, bias, running_mean, running_var}) {
    if (t->defined()) {
      checkNumel(c, t, num_features);
    }
  }

  cudnnBatchNormMode_t mode;
  if (input->dim() == 2) {
    mode = CUDNN_BATCHNORM_PER_ACTIVATION;
  } else {
    mode = CUDNN_BATCHNORM_SPATIAL;
#if CUDNN_VERSION >= 7003
    if(training)
      mode = CUDNN_BATCHNORM_SPATIAL_PERSISTENT;
#endif
  }

  auto output_t = input->type().tensor(input->sizes());
  TensorArg output{ output_t, "output", 0 };

  auto handle = getCudnnHandle();
  auto dataType = getCudnnDataType(*input);
  TensorDescriptor idesc{ *input, 4 };  // input descriptor
  TensorDescriptor wdesc{ expandScale(*weight, input->dim()), 4 };  // descriptor for weight, bias, running_mean, etc.

  Constant one(dataType, 1);
  Constant zero(dataType, 0);
  Tensor save_mean, save_var;

  if (training) {
    int64_t num_features = input_t.size(1);
    save_mean = weight_t.type().tensor({ num_features });
    save_var = weight_t.type().tensor({ num_features });
    CUDNN_CHECK(cudnnBatchNormalizationForwardTraining(
      handle, mode, &one, &zero,
      idesc.desc(), input->data_ptr(),
      idesc.desc(), output->data_ptr(),
      wdesc.desc(),
      weight->data_ptr(),
      bias->data_ptr(),
      exponential_average_factor,
      at::maybe_data_ptr(running_mean),
      at::maybe_data_ptr(running_var),
      epsilon,
      save_mean.data_ptr(),
      save_var.data_ptr()));
  } else {
    CUDNN_CHECK(cudnnBatchNormalizationForwardInference(
      handle, mode, &one, &zero,
      idesc.desc(), input->data_ptr(),
      idesc.desc(), output->data_ptr(),
      wdesc.desc(),
      weight->data_ptr(),
      bias->data_ptr(),
      running_mean->data_ptr(),
      running_var->data_ptr(),
      epsilon));
  }

  // save_mean and save_var can be undefined
  // If this causes problems, we can initialize them to empty tensors
  // of the correct type
  return std::tuple<Tensor, Tensor, Tensor>{output_t, save_mean, save_var};
}
Esempio n. 21
0
File: Lex.cpp Progetto: zzloojit/cpp
void Preprocess::direct_def(void)
{
  Token tok;

  except(tok::SPACE);

  int t = lex.next(tok, fbuffer);  

  
  if (t != tok::IDENT)
    {
      fatal_error("macro name define need identifier");
    }
  
  string n(tok.str);
  
  if (macros_map.find(n) != macros_map.end())
    std::cout << "redefined " + n << std::endl;
  
  macro_type& vec = macros_map[n];

  t = lex.next(tok, fbuffer);
    
  if (t == tok::LPAREN)
    {
      vec.kind = tok::macfunc_type;
      
      // 
      while ((t = lex.next(tok, fbuffer)) != tok::TEOF)
      {
        if (t == tok::SPACE)
          continue;
        else if ( t == tok::IDENT)
        {
          vec.params.push_back(tok);
          do{
            t = lex.next(tok, fbuffer);
          }while (t == tok::SPACE);
          
          if (t == tok::COMMA)
            continue;
          else if (t == tok::RPAREN)
            break;
          else
           {
             fatal_error(tok.str + " may not appear in macro parameter list");
           }
        }
      }
      
      
      
      bool seen_sharp = false;
      while ((t = lex.next(tok, fbuffer)) != tok::TEOF)
        {
          if (t == tok::NEWLINE)
            break;

          if (t == tok::SHARP)
            {
              vec.expand.push_back(tok);

              do {
                t = lex.next(tok, fbuffer);
              }while (t == tok::SPACE);
              
              if (!in_vector(vec.params, tok))
                fatal_error("# is not followed by a macro parameter");
            }
          
          if (t = tok::TWOSHARP)
            {
              while (!vec.expand.empty() && (vec.expand.back().kind == tok::SPACE))
                vec.expand.pop_back();
            }
          
          vec.expand.push_back(tok);
        }

      while (!vec.expand.empty() && (vec.expand.back().kind == tok::SPACE))
        vec.expand.pop_back();

      if (!vec.expand.empty() && ((vec.expand.front().kind == tok::TWOSHARP)
                                  ||(vec.expand.back().kind == tok::TWOSHARP)))
        fatal_error("'##' cannot appear at either end of a macro expansion");
      
      reduce_adjacent_twosharp(vec.expand);
    }
  else if (t == tok::SPACE)
    {
      vec.kind == tok::macobj_type;
      
      bool allspace = true;

      while ((t = lex.next(tok, fbuffer)) != tok::TEOF)
        {
          if (t == tok::NEWLINE)
            break;
 
          if (t != tok::SPACE)
            allspace = false;
          
          vec.expand.push_back(tok);
        }
      
      string one("1");
      if (allspace)
        vec.expand.push_back(Token(tok::NUMBER, one));
    }
  else if (t == tok::NEWLINE)
  {
    string one("1");
    vec.expand.push_back(Token(tok::NUMBER, one));
  }
  else
  {
    fatal_error("parse define error");
  }
}
NOX::Abstract::Group::ReturnType
LOCA::TurningPoint::MinimallyAugmented::ModifiedConstraint::
computeConstraints()
{
  if (isValidConstraints)
    return NOX::Abstract::Group::Ok;

  std::string callingFunction = 
    "LOCA::TurningPoint::MinimallyAugmented::ModifiedConstraint::computeConstraints()";
  NOX::Abstract::Group::ReturnType status;
  NOX::Abstract::Group::ReturnType finalStatus = NOX::Abstract::Group::Ok;

  // Compute J
  status = grpPtr->computeJacobian();
  finalStatus = 
    globalData->locaErrorCheck->combineAndCheckReturnTypes(status, 
							   finalStatus,
							   callingFunction);

  // Set up bordered systems
  Teuchos::RCP<const LOCA::BorderedSolver::JacobianOperator> op =
    Teuchos::rcp(new  LOCA::BorderedSolver::JacobianOperator(grpPtr));
  borderedSolver->setMatrixBlocksMultiVecConstraint(op, 
						    a_vector, 
						    b_vector, 
						    Teuchos::null);

  // Get linear solver parameters
  Teuchos::RCP<Teuchos::ParameterList> linear_solver_params =
    parsedParams->getSublist("Linear Solver");

  // Solve for w and v
  if (isFirstSolve) {

    std::cout << "solving for base w,v..." << std::endl;

    // Create RHS
    NOX::Abstract::MultiVector::DenseMatrix one(1,1);
    if (nullVecScaling == NVS_OrderN)
      one(0,0) = dn;
    else
      one(0,0) = 1.0;

    // Compute sigma_1 and right null vector v
    status = borderedSolver->initForSolve();
    finalStatus = 
      globalData->locaErrorCheck->combineAndCheckReturnTypes(status, 
							     finalStatus,
							     callingFunction);
    status = borderedSolver->applyInverse(*linear_solver_params, 
					  NULL, 
					  &one, 
					  *v_vector, 
					  sigma1);
    finalStatus = 
      globalData->locaErrorCheck->combineAndCheckReturnTypes(status, 
							     finalStatus,
							     callingFunction);

    // Compute sigma_2 and left null vector w
    if (!isSymmetric) {
      status = borderedSolver->initForTransposeSolve();
      finalStatus = 
	globalData->locaErrorCheck->combineAndCheckReturnTypes(
							    status, 
							    finalStatus,
							    callingFunction);
      status = borderedSolver->applyInverseTranspose(*linear_solver_params, 
						     NULL, 
						     &one, 
						     *w_vector, 
						     sigma2);
      finalStatus = 
	globalData->locaErrorCheck->combineAndCheckReturnTypes(
							    status, 
							    finalStatus,
							    callingFunction);

    }
    else {
      *w_vector = *v_vector;
      sigma2.assign(sigma1);
    }

    isFirstSolve = false;
  }

  // solve for updates to w and v
  else {

    std::cout << "solving for updates..." << std::endl;

    // Compute J*v + a*sigma_1
    status = grpPtr->applyJacobianMultiVector(*v_vector, *v_residual);
    finalStatus = 
      globalData->locaErrorCheck->combineAndCheckReturnTypes(status, 
							     finalStatus,
							     callingFunction);
    v_residual->update(Teuchos::NO_TRANS, 1.0, *a_vector, sigma1, 0.0);
    
    // Compute b^T*v - n
    NOX::Abstract::MultiVector::DenseMatrix sigma1_residual(1,1);
    v_vector->multiply(1.0, *b_vector, sigma1_residual);
    if (nullVecScaling == NVS_OrderN)
      sigma1_residual(0,0) -= dn;
    else
      sigma1_residual(0,0) -= 1.0;

    if (includeNewtonTerms) {

      // Compute (Jv)_x*dx
      Teuchos::RCP<NOX::Abstract::MultiVector> Jv_x_dx = 
	deltaX->clone(NOX::ShapeCopy);
      status = grpPtr->computeDJnDxaMulti((*v_vector)[0], *deltaX, *Jv_x_dx);
      finalStatus = 
	globalData->locaErrorCheck->combineAndCheckReturnTypes(
							     status, 
							     finalStatus,
							     callingFunction);

      // Compute (Jv)_p
      Teuchos::RCP<NOX::Abstract::MultiVector> Jv_p1 = 
	deltaX->clone(2);
      std::vector<int> idx(1); idx[0] = 0;
      Teuchos::RCP<NOX::Abstract::MultiVector> Jv_p = 
	Jv_p1->subView(idx);
      status = grpPtr->computeDJnDpMulti(bifParamID, (*v_vector)[0], *Jv_p1, 
					 false);
      finalStatus = 
	globalData->locaErrorCheck->combineAndCheckReturnTypes(
							     status, 
							     finalStatus,
							     callingFunction);
      // compute v_residual += (Jv)_x*dx + (Jv)_p*dp
      v_residual->update(1.0, *Jv_x_dx, deltaP, *Jv_p, 1.0);
      
      // Compute J
      status = grpPtr->computeJacobian();
      finalStatus = 
	globalData->locaErrorCheck->combineAndCheckReturnTypes(
							     status, 
							     finalStatus,
							     callingFunction);
    }

    // Compute update to sigma_1 and right null vector v
    NOX::Abstract::MultiVector::DenseMatrix sigma1_update(1,1);
    status = borderedSolver->initForSolve();
    finalStatus = 
      globalData->locaErrorCheck->combineAndCheckReturnTypes(
							   status, 
							   finalStatus,
							   callingFunction);
    status = borderedSolver->applyInverse(*linear_solver_params, 
					  v_residual.get(), 
					  &sigma1_residual, 
					  *v_vector_update, 
					  sigma1_update);
    finalStatus = 
      globalData->locaErrorCheck->combineAndCheckReturnTypes(
							   status, 
							   finalStatus,
							   callingFunction);

    // apply updates
    v_vector->update(-1.0, *v_vector_update, 1.0);
    sigma1(0,0) -= sigma1_update(0,0);
    
    if (!isSymmetric) {
      // Compute J^T*w + b*sigma_w
      status = grpPtr->applyJacobianTransposeMultiVector(*w_vector, 
							 *w_residual);
      finalStatus = 
	globalData->locaErrorCheck->combineAndCheckReturnTypes(
							     status, 
							     finalStatus,
							     callingFunction);
      w_residual->update(Teuchos::NO_TRANS, 1.0, *b_vector, sigma2, 0.0);

      // Compute a^T*w - n
      NOX::Abstract::MultiVector::DenseMatrix sigma2_residual(1,1);
      w_vector->multiply(1.0, *a_vector, sigma2_residual);
      if (nullVecScaling == NVS_OrderN)
	sigma2_residual(0,0) -= dn;
      else
	sigma2_residual(0,0) -= 1.0;

      if (includeNewtonTerms) {

	// Compute (J^T*w)_x*dx
	Teuchos::RCP<NOX::Abstract::MultiVector> Jtw_x_dx = 
	  deltaX->clone(NOX::ShapeCopy);
	status = grpPtr->computeDwtJnDx((*w_vector)[0], (*deltaX)[0], 
					(*Jtw_x_dx)[0]);
	finalStatus = 
	  globalData->locaErrorCheck->combineAndCheckReturnTypes(
							     status, 
							     finalStatus,
							     callingFunction);

	// Compute (J^T*w)_p
	Teuchos::RCP<NOX::Abstract::MultiVector> Jtw_p1 = 
	  deltaX->clone(2);
	std::vector<int> idx(1); idx[0] = 0;
	Teuchos::RCP<NOX::Abstract::MultiVector> Jtw_p = 
	  Jtw_p1->subView(idx);
	status = grpPtr->computeDwtJDp(bifParamID, (*w_vector)[0], *Jtw_p1, 
				       false);
	finalStatus = 
	  globalData->locaErrorCheck->combineAndCheckReturnTypes(
							     status, 
							     finalStatus,
							     callingFunction);
	// compute w_residual += (J^T*w)_x*dx + (J^T*w)_p*dp
	w_residual->update(1.0, *Jtw_x_dx, deltaP, *Jtw_p, 1.0);
	
	// Compute J
	status = grpPtr->computeJacobian();
	finalStatus = 
	  globalData->locaErrorCheck->combineAndCheckReturnTypes(
							    status, 
							    finalStatus,
							    callingFunction);
      }

      // Compute update to sigma_2 and left null vector w
      NOX::Abstract::MultiVector::DenseMatrix sigma2_update(1,1);
      status = borderedSolver->initForTransposeSolve();
      finalStatus = 
	globalData->locaErrorCheck->combineAndCheckReturnTypes(
							     status, 
							     finalStatus,
							     callingFunction);
      status = borderedSolver->applyInverseTranspose(*linear_solver_params, 
						     w_residual.get(), 
						     &sigma2_residual, 
						     *w_vector_update, 
						     sigma2_update);
      finalStatus = 
	globalData->locaErrorCheck->combineAndCheckReturnTypes(
							     status, 
							     finalStatus,
							     callingFunction);

      // apply updates
      w_vector->update(-1.0, *w_vector_update, 1.0);
      sigma2(0,0) -= sigma2_update(0,0);

    }
    else {
      *w_vector = *v_vector;
      sigma2.assign(sigma1);
    }
    
  }
  
  // Compute sigma = -w^T*J*v
  status = grpPtr->applyJacobianMultiVector(*v_vector, *Jv_vector);
  finalStatus = 
    globalData->locaErrorCheck->combineAndCheckReturnTypes(status, 
							   finalStatus,
							   callingFunction);
  if (!isSymmetric) {
    status = grpPtr->applyJacobianTransposeMultiVector(*w_vector, *Jtw_vector);
    finalStatus = 
      globalData->locaErrorCheck->combineAndCheckReturnTypes(status, 
							     finalStatus,
							     callingFunction);
  }
  else
    *Jtw_vector = *Jv_vector;
  Jv_vector->multiply(-1.0, *w_vector, constraints);

  // Scale sigma
  double w_norm = (*w_vector)[0].norm();
  double v_norm = (*v_vector)[0].norm();
  double Jv_norm = (*Jv_vector)[0].norm();
  double Jtw_norm = (*Jtw_vector)[0].norm();
  if (nullVecScaling == NVS_OrderN)
    sigma_scale = dn;
  else
    sigma_scale = 1.0;
  constraints.scale(1.0/sigma_scale);

  if (globalData->locaUtils->isPrintType(NOX::Utils::OuterIteration)) {
    globalData->locaUtils->out() << "\n\t||Right null vector v|| = " 
				 << globalData->locaUtils->sciformat(v_norm);
    globalData->locaUtils->out() << "\n\t||Left null vector w|| = " 
				 << globalData->locaUtils->sciformat(w_norm);
    globalData->locaUtils->out() << "\n\t||Jv|| = " 
				 << globalData->locaUtils->sciformat(Jv_norm);
    globalData->locaUtils->out() << "\n\t||J^T*w|| = " 
				 << globalData->locaUtils->sciformat(Jtw_norm);
    globalData->locaUtils->out() << 
      "\n\tRight estimate for singularity of Jacobian (sigma1) = " << 
      globalData->locaUtils->sciformat(sigma1(0,0));
    globalData->locaUtils->out() << 
      "\n\tLeft estimate for singularity of Jacobian (sigma2) = " << 
      globalData->locaUtils->sciformat(sigma2(0,0));
    globalData->locaUtils->out() << 
      "\n\tFinal Estimate for singularity of Jacobian (sigma) = " << 
      globalData->locaUtils->sciformat(constraints(0,0)) << std::endl;
  }

  isValidConstraints = true;

  // Update a and b if requested
  if (updateVectorsEveryIteration) {
    if (globalData->locaUtils->isPrintType(NOX::Utils::OuterIteration)) {
      globalData->locaUtils->out() << 
	"\n\tUpdating null vectors for the next nonlinear iteration" << 
	std::endl;
    }
    *a_vector = *w_vector;
    *b_vector = *v_vector;

    scaleNullVectors((*a_vector)[0],(*b_vector)[0]);
  }

  return finalStatus;
}
CXXCAPI int unittestMutex(void) {
    Print printf(Platform::instance().output());
    Print errorf(Platform::instance().error());
    int errors = 0;
    int status;
    bool disabled;

    printf("%s[%d]: begin\n", __FILE__, __LINE__);

    ::staticMutex.show();

    Mutex mutex;

    printf("%s[%d]: mutex begin\n", __FILE__, __LINE__);

    mutex.show();
    if ((status = mutex.begin()) != 0) { errorf("%s[%d]: %d!=%d!\n", __FILE__, __LINE__, status, 0); ++errors; }
    mutex.show();

    printf("%s[%d]: mutex end\n", __FILE__, __LINE__);

    mutex.show();
    if ((status = mutex.end()) != 0) { errorf("%s[%d]: %d!=%d!\n", __FILE__, __LINE__, status, 0); ++errors; }
    mutex.show();

    printf("%s[%d]: mutex recursion\n", __FILE__, __LINE__);

    mutex.show();
    if ((status = mutex.begin()) != 0) { errorf("%s[%d]: %d!=%d!\n", __FILE__, __LINE__, status, 0); ++errors; }
    mutex.show();
    if ((status = mutex.attempt()) != 0) { errorf("%s[%d]: %d!=%d!\n", __FILE__, __LINE__, status, 0); ++errors; }
    mutex.show();
    if ((status = mutex.begin()) != 0) { errorf("%s[%d]: %d!=%d!\n", __FILE__, __LINE__, status, 0); ++errors; }
    mutex.show();
    if ((status = mutex.end()) != 0) { errorf("%s[%d]: %d!=%d!\n", __FILE__, __LINE__, status, 0); ++errors; }
    mutex.show();
    if ((status = mutex.end()) != 0) { errorf("%s[%d]: %d!=%d!\n", __FILE__, __LINE__, status, 0); ++errors; }
    mutex.show();
    if ((status = mutex.end()) != 0) { errorf("%s[%d]: %d!=%d!\n", __FILE__, __LINE__, status, 0); ++errors; }
    mutex.show();

    printf("%s[%d]: critical section\n", __FILE__, __LINE__);

    struct MyCriticalSection : public CriticalSection {
    	explicit MyCriticalSection(Mutex& mutexr, bool disable = true)
    		: CriticalSection(mutexr, disable)
    	{
    		++level;
    	}
    	virtual ~MyCriticalSection() {
    		--level;
    	}
    	int getStatus() { return this->status; }
    	bool getDisabled() { return this->disabled; }
    };

    {
        MyCriticalSection one(mutex);
        if ((status = one.getStatus()) != 0) { errorf("%s[%d]: %d!=%d!\n", __FILE__, __LINE__, status, 0); ++errors; }
        if (!(disabled = one.getDisabled())) { errorf("%s[%d]: %d!=%d!\n", __FILE__, __LINE__, disabled, true); ++errors; }
        if (level != 1) { errorf("%s[%d]: %d!=%d!\n", __FILE__, __LINE__, level, 1); ++errors; }
        mutex.show();
        {
            MyCriticalSection two(mutex);
            if ((status = two.getStatus()) != 0) { errorf("%s[%d]: %d!=%d!\n", __FILE__, __LINE__, status, 0); ++errors; }
            if (!(disabled = two.getDisabled())) { errorf("%s[%d]: %d!=%d!\n", __FILE__, __LINE__, disabled, true); ++errors; }
            if (level != 2) { errorf("%s[%d]: %d!=%d!\n", __FILE__, __LINE__, level, 2); ++errors; }
            mutex.show();
        }   
        if (level != 1) { errorf("%s[%d]: %d!=%d!\n", __FILE__, __LINE__, level, 1); ++errors; }
        mutex.show();
    }
    if (level != 0) { errorf("%s[%d]: %d!=%d!\n", __FILE__, __LINE__, level, 0); ++errors; }
    mutex.show();

    printf("%s[%d]: critical section enabled\n", __FILE__, __LINE__);

    {
        MyCriticalSection one(mutex, false);
        if ((status = one.getStatus()) != 0) { errorf("%s[%d]: %d!=%d!\n", __FILE__, __LINE__, status, 0); ++errors; }
        if ((disabled = one.getDisabled())) { errorf("%s[%d]: %d!=%d!\n", __FILE__, __LINE__, disabled, false); ++errors; }
        if (level != 1) { errorf("%s[%d]: %d!=%d!\n", __FILE__, __LINE__, level, 1); ++errors; }
        mutex.show();
        {
            MyCriticalSection two(mutex, false);
            if ((status = two.getStatus()) != 0) { errorf("%s[%d]: %d!=%d!\n", __FILE__, __LINE__, status, 0); ++errors; }
            if ((disabled = two.getDisabled())) { errorf("%s[%d]: %d!=%d!\n", __FILE__, __LINE__, disabled, false); ++errors; }
            if (level != 2) { errorf("%s[%d]: %d!=%d!\n", __FILE__, __LINE__, level, 2); ++errors; }
            mutex.show();
        }
        if (level != 1) { errorf("%s[%d]: %d!=%d!\n", __FILE__, __LINE__, level, 1); ++errors; }
        mutex.show();
    }
    if (level != 0) { errorf("%s[%d]: %d!=%d!\n", __FILE__, __LINE__, level, 0); ++errors; }
    mutex.show();

    printf("%s[%d]: end errors=%d\n",
        __FILE__, __LINE__, errors);

    return errors;
}
Esempio n. 24
0
static void
read_PK_char(struct font *fontp, wide_ubyte ch)
{
    int i, j;
    int n;
    int row_bit_pos;
    Boolean paint_switch;
    BMUNIT *cp;
    struct glyph *g;
    FILE *fp = fontp->file;
    long fpwidth;
    BMUNIT word = 0;
    int word_weight, bytes_wide;
    int rows_left, h_bit, count;

    g = &fontp->glyph[ch];
    PK_flag_byte = g->x2;
    PK_dyn_f = PK_flag_byte >> 4;
    paint_switch = ((PK_flag_byte & 8) != 0);
    PK_flag_byte &= 0x7;
    if (PK_flag_byte == 7)
	n = 4;
    else if (PK_flag_byte > 3)
	n = 2;
    else
	n = 1;

    if (debug & DBG_PK)
	Printf("loading pk char %d, char type %d ", ch, n);

    /*
     * now read rest of character preamble
     */
    if (n != 4)
	fpwidth = num(fp, 3);
    else {
	fpwidth = sfour(fp);
	(void)four(fp);	/* horizontal escapement */
    }
    (void)num(fp, n);	/* vertical escapement */
    {
	unsigned long w, h;

	w = num(fp, n);
	h = num(fp, n);
	if (w > 0x7fff || h > 0x7fff)
	    oops("Character %d too large in file %s", ch, fontp->fontname);
	g->bitmap.w = w;
	g->bitmap.h = h;
    }
    g->x = snum(fp, n);
    g->y = snum(fp, n);

    g->dvi_adv = fontp->dimconv * fpwidth;

    if (debug & DBG_PK) {
	if (g->bitmap.w != 0)
	    Printf(", size=%dx%d, dvi_adv=%ld", g->bitmap.w, g->bitmap.h,
		   g->dvi_adv);
	Putchar('\n');
    }

    alloc_bitmap(&g->bitmap);
    cp = (BMUNIT *) g->bitmap.bits;

    /*
     * read character data into *cp
     */
    bytes_wide = ROUNDUP((int)g->bitmap.w, BMBITS) * BMBYTES;
    PK_bitpos = -1;
    if (PK_dyn_f == 14) {	/* get raster by bits */
	bzero(g->bitmap.bits, (int)g->bitmap.h * bytes_wide);
	for (i = 0; i < (int)g->bitmap.h; i++) {	/* get all rows */
	    cp = ADD(g->bitmap.bits, i * bytes_wide);
#ifndef	WORDS_BIGENDIAN
	    row_bit_pos = -1;
#else
	    row_bit_pos = BMBITS;
#endif
	    for (j = 0; j < (int)g->bitmap.w; j++) {	/* get one row */
		if (--PK_bitpos < 0) {
		    word = one(fp);
		    PK_bitpos = 7;
		}
#ifndef	WORDS_BIGENDIAN
		if (++row_bit_pos >= BMBITS) {
		    cp++;
		    row_bit_pos = 0;
		}
#else
		if (--row_bit_pos < 0) {
		    cp++;
		    row_bit_pos = BMBITS - 1;
		}
#endif
		if (word & (1 << PK_bitpos))
		    *cp |= 1 << row_bit_pos;
	    }
	}
    }
    else {	/* get packed raster */
	rows_left = g->bitmap.h;
	h_bit = g->bitmap.w;
	PK_repeat_count = 0;
	word_weight = BMBITS;
	word = 0;
	while (rows_left > 0) {
	    count = PK_packed_num(fp);
	    while (count > 0) {
		if (count < word_weight && count < h_bit) {
#ifndef	WORDS_BIGENDIAN
		    if (paint_switch)
			word |= bit_masks[count] << (BMBITS - word_weight);
#endif
		    h_bit -= count;
		    word_weight -= count;
#ifdef	WORDS_BIGENDIAN
		    if (paint_switch)
			word |= bit_masks[count] << word_weight;
#endif
		    count = 0;
		}
		else if (count >= h_bit && h_bit <= word_weight) {
		    if (paint_switch)
			word |= bit_masks[h_bit] <<
#ifndef	WORDS_BIGENDIAN
			    (BMBITS - word_weight);
#else
			    (word_weight - h_bit);
#endif
		    *cp++ = word;
		    /* "output" row(s) */
		    for (i = PK_repeat_count * bytes_wide / BMBYTES; i > 0; --i) {
			*cp = *SUB(cp, bytes_wide);
			++cp;
		    }
		    rows_left -= PK_repeat_count + 1;
		    PK_repeat_count = 0;
		    word = 0;
		    word_weight = BMBITS;
		    count -= h_bit;
		    h_bit = g->bitmap.w;
		}
		else {
		    if (paint_switch)
#ifndef	WORDS_BIGENDIAN
			word |= bit_masks[word_weight] <<
			    (BMBITS - word_weight);
#else
			word |= bit_masks[word_weight];
#endif
		    *cp++ = word;
		    word = 0;
		    count -= word_weight;
		    h_bit -= word_weight;
		    word_weight = BMBITS;
		}
	    }
	    paint_switch = 1 - paint_switch;
	}
	if (cp != ((BMUNIT *) (g->bitmap.bits + bytes_wide * g->bitmap.h)))
	    oops("Wrong number of bits stored:  char. %d, font %s", ch,
		 fontp->fontname);
	if (rows_left != 0 || h_bit != g->bitmap.w)
	    oops("Bad pk file (%s), too many bits", fontp->fontname);
    }
}
void Discrete_upper_envelope::build_orient_with_slope_unique_lines_sorted_by_slope(
        const std::vector< const Line* >& sorted_lines,
        Building_with_orient_mem& dual_points) {

    // typedefs and preconditions
    typedef Point_2::Std_point Point;
    typedef Poly_chain_2::Vertex_list::const_iterator Vertex_iter;

    // if there is only 1 line then we are done
    if (sorted_lines.size() == 1) {
        envelope_.push_back(Envelope_cell(*sorted_lines.front(), one(), U()));
        return;
    }

    // dualize the lines
    dual_points.resize(0);
    for (std::vector< const Line* >::const_iterator iter = sorted_lines.begin() ;
            iter != sorted_lines.end() ; ++iter) {
        dual_points.push_back(Line_2::dual(**iter));
    }

    // log the points
    rLog(up_env_orient, "Dual Points:\n %s",
            vect_to_cstring(dual_points.begin(), dual_points.end()));

    // compute the lower hull of the duals points
    Convex_hull::Graham_yao ch;
    Poly_chain_2 lower_hull;
    lower_hull.reserve(dual_points.size());
    ch.build_lower_hull(lower_hull, dual_points);

    // log the lower envelope
    rLog(up_env_orient, "Lower hull\n %s", thing_to_cstring(lower_hull));

    // iterate over the lines of the upper envelope and
    // convert them to cells
    int cell_begin = one();
    for (Vertex_iter pi = lower_hull.vertices_begin() ;
            pi != lower_hull.vertices_end() && cell_begin <= U() ; ++pi) {

        // compute the end of the envelope for line corresponding to pi
        // by getting the next dual point
        // if the point is the last point then dual(pi) is the
        // remainder of the upper envelope.
        // if it is not, compute the intersection of dual(pi) and
        // dual(pip1) to determine where the cell ends
        int cell_end;
        Vertex_iter pip1 = DDAD_util::next(pi);
        const Line* l_pi = (const Line*)pi->data();
        if (pip1 == lower_hull.vertices_end()) { cell_end = U(); }
        else {
            int x_coord;
            const Line* l_pip1 = (const Line*)pip1->data();
            Line_2::vertical_grid_line_left_or_thourgh_intersection(
                    x_coord, *l_pi, *l_pip1);
            cell_end = std::min(U(), x_coord);
        }

        if (cell_end >= cell_begin) {
            envelope_.push_back(Envelope_cell(*l_pi, cell_begin, cell_end));
            cell_begin = cell_end+1;
        }
    }

    // log the sorted list
    rLog(up_env_orient, "Output of build_orient:\n %s",
           vect_to_cstring(envelope_.begin(), envelope_.end()));
}
Esempio n. 26
0
void
read_PK_index(struct font *fontp, wide_bool hushcs)
{
    int hppp, vppp;
    long checksum;

    fontp->read_char = read_PK_char;
    if (debug & DBG_PK)
	Printf("Reading PK pixel file %s\n", fontp->filename);

    Fseek(fontp->file, (long)one(fontp->file), 1);	/* skip comment */

    (void)four(fontp->file);	/* skip design size */
    checksum = four(fontp->file);
    if (checksum != fontp->checksum && checksum != 0 && fontp->checksum != 0
	&& !hushcs)
	    Fprintf(stderr,
		    "Checksum mismatch (dvi = %lu, pk = %lu) in font file %s\n",
		    fontp->checksum, checksum, fontp->filename);
    hppp = sfour(fontp->file);
    vppp = sfour(fontp->file);
    if (hppp != vppp && (debug & DBG_PK))
	Printf("Font has non-square aspect ratio %d:%d\n", vppp, hppp);
    /*
     * Prepare glyph array.
     */
    fontp->glyph = xmalloc(256 * sizeof(struct glyph));
    bzero((char *)fontp->glyph, 256 * sizeof(struct glyph));
    /*
     * Read glyph directory (really a whole pass over the file).
     */
    for (;;) {
	int bytes_left, flag_low_bits;
	unsigned int ch;

	PK_skip_specials(fontp);
	if (PK_flag_byte == PK_POST)
	    break;
	flag_low_bits = PK_flag_byte & 0x7;
	if (flag_low_bits == 7) {
	    bytes_left = four(fontp->file);
	    ch = four(fontp->file);
	}
	else if (flag_low_bits > 3) {
	    bytes_left = ((flag_low_bits - 4) << 16) + two(fontp->file);
	    ch = one(fontp->file);
	}
	else {
	    bytes_left = (flag_low_bits << 8) + one(fontp->file);
	    ch = one(fontp->file);
	}
	fontp->glyph[ch].addr = ftell(fontp->file);
	fontp->glyph[ch].x2 = PK_flag_byte;
#ifdef linux
#ifndef SHORTSEEK
#define SHORTSEEK 2048
#endif
	/* A bug in Linux libc (as of 18oct94) makes a short read faster
	   than a short forward seek. Totally non-intuitive.  */
	if (bytes_left > 0 && bytes_left < SHORTSEEK) {
	    char *dummy = xmalloc(bytes_left);
	    Fread(dummy, 1, bytes_left, fontp->file);
	    free(dummy);
	}
	else
	    /* seek backward, or long forward */
#endif /* linux */
	    Fseek(fontp->file, (long)bytes_left, 1);
	if (debug & DBG_PK)
	    Printf("Scanning pk char %u, at %ld.\n", ch, fontp->glyph[ch].addr);
    }
}
Esempio n. 27
0
int main(int argc, char *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

  /*** Initialize communicator. ***/
  Teuchos::GlobalMPISession mpiSession (&argc, &argv, &bhs);
  Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::DefaultPlatform::getDefaultPlatform().getComm();
  const int myRank = comm->getRank();
  if ((iprint > 0) && (myRank == 0)) {
    outStream = Teuchos::rcp(&std::cout, false);
  }
  else {
    outStream = Teuchos::rcp(&bhs, false);
  }

  int errorFlag  = 0;

  // *** Example body.
  try {

    /*** Read in XML input ***/
    std::string filename = "input.xml";
    Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp( new Teuchos::ParameterList() );
    Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() );

    /*** Initialize main data structure. ***/
    Teuchos::RCP<ElasticitySIMPOperators<RealT> > data = Teuchos::rcp(new ElasticitySIMPOperators<RealT>(comm, parlist, outStream));
    /*** Build vectors and dress them up as ROL vectors. ***/
    Teuchos::RCP<const Tpetra::Map<> > vecmap_u = data->getDomainMapA();
    Teuchos::RCP<const Tpetra::Map<> > vecmap_z = data->getCellMap();
    Teuchos::RCP<Tpetra::MultiVector<> > u_rcp = Teuchos::rcp(new Tpetra::MultiVector<>(vecmap_u, 1, true));
    Teuchos::RCP<Tpetra::MultiVector<> > z_rcp = Teuchos::rcp(new Tpetra::MultiVector<>(vecmap_z, 1, true));
    Teuchos::RCP<Tpetra::MultiVector<> > du_rcp = Teuchos::rcp(new Tpetra::MultiVector<>(vecmap_u, 1, true));
    Teuchos::RCP<Tpetra::MultiVector<> > dw_rcp = Teuchos::rcp(new Tpetra::MultiVector<>(vecmap_u, 1, true));
    Teuchos::RCP<Tpetra::MultiVector<> > dz_rcp = Teuchos::rcp(new Tpetra::MultiVector<>(vecmap_z, 1, true));
    Teuchos::RCP<Tpetra::MultiVector<> > dz2_rcp = Teuchos::rcp(new Tpetra::MultiVector<>(vecmap_z, 1, true));
    Teuchos::RCP<std::vector<RealT> >    vscale_rcp = Teuchos::rcp(new std::vector<RealT>(1, 0));
    Teuchos::RCP<std::vector<RealT> >    vc_rcp = Teuchos::rcp(new std::vector<RealT>(1, 0));
    Teuchos::RCP<std::vector<RealT> >    vc_lam_rcp = Teuchos::rcp(new std::vector<RealT>(1, 0));
    // Set all values to 1 in u, z.
    u_rcp->putScalar(1.0);
    // Set z to gray solution.
    RealT volFrac = parlist->sublist("ElasticityTopoOpt").get("Volume Fraction", 0.5), one(1), two(2);
    z_rcp->putScalar(volFrac);
    (*vscale_rcp)[0] = one/std::pow(static_cast<RealT>(z_rcp->getGlobalLength())*(one-volFrac),two);

   //test     
   /*data->updateMaterialDensity (z_rcp);
    Teuchos::RCP<Tpetra::MultiVector<RealT> > rhs = Teuchos::rcp(new Tpetra::MultiVector<> (data->getVecF()->getMap(), 1, true));
    data->ApplyMatAToVec(rhs, u_rcp);
    data->outputTpetraVector(rhs, "KU0.txt");
    data->ApplyInverseJacobian1ToVec(u_rcp, rhs, false);
    data->outputTpetraVector(u_rcp, "KKU0.txt");
    
    data->ApplyJacobian1ToVec(rhs, u_rcp);
    data->outputTpetraVector(rhs, "KU1.txt");
    data->ApplyInverseJacobian1ToVec(u_rcp, rhs, false);
    data->outputTpetraVector(u_rcp, "KKU1.txt");
  */
    //u_rcp->putScalar(1.0);
    //z_rcp->putScalar(1.0);
    // Randomize d vectors.
    du_rcp->randomize();
    dw_rcp->randomize();
    dz_rcp->randomize();
    dz2_rcp->randomize();
    // Create ROL::TpetraMultiVectors.
    Teuchos::RCP<ROL::Vector<RealT> > up = Teuchos::rcp(new ROL::TpetraMultiVector<RealT>(u_rcp));
    Teuchos::RCP<ROL::Vector<RealT> > zp = Teuchos::rcp(new ROL::TpetraMultiVector<RealT>(z_rcp));
    Teuchos::RCP<ROL::Vector<RealT> > dup = Teuchos::rcp(new ROL::TpetraMultiVector<RealT>(du_rcp));
    Teuchos::RCP<ROL::Vector<RealT> > dwp = Teuchos::rcp(new ROL::TpetraMultiVector<RealT>(dw_rcp));
    Teuchos::RCP<ROL::Vector<RealT> > dzp = Teuchos::rcp(new ROL::TpetraMultiVector<RealT>(dz_rcp));
    Teuchos::RCP<ROL::Vector<RealT> > dz2p = Teuchos::rcp(new ROL::TpetraMultiVector<RealT>(dz2_rcp));
    Teuchos::RCP<ROL::Vector<RealT> > vcp
      = Teuchos::rcp(new ROL::PrimalScaledStdVector<RealT>(vc_rcp,vscale_rcp));
    Teuchos::RCP<ROL::Vector<RealT> > vc_lamp
      = Teuchos::rcp(new ROL::DualScaledStdVector<RealT>(vc_lam_rcp,vscale_rcp));
    // Create ROL SimOpt vectors.
    ROL::Vector_SimOpt<RealT> x(up,zp);
    ROL::Vector_SimOpt<RealT> d(dup,dzp);
    ROL::Vector_SimOpt<RealT> d2(dwp,dz2p);

    /*** Build objective function, constraint and reduced objective function. ***/
    Teuchos::RCP<ROL::Objective_SimOpt<RealT> > obj
       = Teuchos::rcp(new Objective_PDEOPT_ElasticitySIMP<RealT>(data, parlist));
    Teuchos::RCP<ROL::EqualityConstraint_SimOpt<RealT> > con
       = Teuchos::rcp(new EqualityConstraint_PDEOPT_ElasticitySIMP<RealT>(data, parlist));
    Teuchos::RCP<ROL::Reduced_Objective_SimOpt<RealT> > objReduced
       = Teuchos::rcp(new ROL::Reduced_Objective_SimOpt<RealT>(obj, con, up, dwp));
    Teuchos::RCP<ROL::EqualityConstraint<RealT> > volcon
       = Teuchos::rcp(new EqualityConstraint_PDEOPT_ElasticitySIMP_Volume<RealT>(data, parlist));

    /*** Build bound constraint ***/
    Teuchos::RCP<Tpetra::MultiVector<> > lo_rcp = Teuchos::rcp(new Tpetra::MultiVector<>(vecmap_z, 1, true));
    Teuchos::RCP<Tpetra::MultiVector<> > hi_rcp = Teuchos::rcp(new Tpetra::MultiVector<>(vecmap_z, 1, true));
    lo_rcp->putScalar(0.0); hi_rcp->putScalar(1.0);
    Teuchos::RCP<ROL::Vector<RealT> > lop = Teuchos::rcp(new ROL::TpetraMultiVector<RealT>(lo_rcp));
    Teuchos::RCP<ROL::Vector<RealT> > hip = Teuchos::rcp(new ROL::TpetraMultiVector<RealT>(hi_rcp));
    Teuchos::RCP<ROL::BoundConstraint<RealT> > bnd = Teuchos::rcp(new ROL::BoundConstraint<RealT>(lop,hip));

    /*** Check functional interface. ***/
    *outStream << "Checking Objective:" << "\n";
    obj->checkGradient(x,d,true,*outStream);
    obj->checkHessVec(x,d,true,*outStream);
    obj->checkHessSym(x,d,d2,true,*outStream);
    *outStream << "Checking Constraint:" << "\n";
    con->checkAdjointConsistencyJacobian(*dup,d,x,true,*outStream);
    con->checkAdjointConsistencyJacobian_1(*dwp, *dup, *up, *zp, true, *outStream);
    con->checkAdjointConsistencyJacobian_2(*dwp, *dzp, *up, *zp, true, *outStream);
    con->checkInverseJacobian_1(*up,*up,*up,*zp,true,*outStream);
    con->checkInverseAdjointJacobian_1(*up,*up,*up,*zp,true,*outStream);
    con->checkApplyJacobian(x,d,*up,true,*outStream);
    con->checkApplyAdjointHessian(x,*dup,d,x,true,*outStream);
    *outStream << "Checking Reduced Objective:" << "\n";
    objReduced->checkGradient(*zp,*dzp,true,*outStream);
    objReduced->checkHessVec(*zp,*dzp,true,*outStream);
    *outStream << "Checking Volume Constraint:" << "\n";
    volcon->checkAdjointConsistencyJacobian(*vcp,*dzp,*zp,true,*outStream);
    volcon->checkApplyJacobian(*zp,*dzp,*vcp,true,*outStream);
    volcon->checkApplyAdjointHessian(*zp,*vcp,*dzp,*zp,true,*outStream);

    /*** Run optimization ***/
    ROL::AugmentedLagrangian<RealT> augLag(objReduced,volcon,*vc_lamp,1.0,*zp,*vcp,*parlist);
    ROL::Algorithm<RealT> algo("Augmented Lagrangian",*parlist,false);
    algo.run(*zp,*vc_lamp,augLag,*volcon,*bnd,true,*outStream);
    //ROL::MoreauYosidaPenalty<RealT> MYpen(objReduced,bnd,*zp,1.0);
    //ROL::Algorithm<RealT> algo("Moreau-Yosida Penalty",*parlist,false);
    //algo.run(*zp,*vc_lamp,MYpen,*volcon,*bnd,true,*outStream);

    data->outputTpetraVector(z_rcp, "density.txt");
    data->outputTpetraVector(u_rcp, "state.txt");
  }
  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;
}
 LogExponentialQuadrangle(const Real coeff = 1) : ExpectationQuad<Real>() {
     Real zero(0), one(1);
     coeff_ = ((coeff > zero) ? coeff : one);
 }
Esempio n. 29
0
	void DrawQuads()
	{
		if(g_DrawVertices->size() < 4 * 3)
			return;

		//Create verts to draw triangle
		std::vector<float> vertices;
		for(size_t i = 0; i < g_DrawVertices->size(); i += 12)
		{
			Vector3Df one((*g_DrawVertices)[i], (*g_DrawVertices)[i + 1], (*g_DrawVertices)[i + 2]);
			Vector3Df two((*g_DrawVertices)[i + 3], (*g_DrawVertices)[i + 4], (*g_DrawVertices)[i + 5]);
			Vector3Df three((*g_DrawVertices)[i + 6], (*g_DrawVertices)[i + 7], (*g_DrawVertices)[i + 8]);
			Vector3Df four((*g_DrawVertices)[i + 9], (*g_DrawVertices)[i + 10], (*g_DrawVertices)[i + 11]);

			AddToVector(vertices, one);
			AddToVector(vertices, two);
			AddToVector(vertices, three);
			AddToVector(vertices, one);
			AddToVector(vertices, three);
			AddToVector(vertices, four);
		}

		//Create verts to draw triangle
		std::vector<float> normals;
		for(size_t i = 0; i < g_DrawNormals->size(); i += 12)
		{
			Vector3Df one((*g_DrawNormals)[i], (*g_DrawNormals)[i + 1], (*g_DrawNormals)[i + 2]);
			Vector3Df two((*g_DrawNormals)[i + 3], (*g_DrawNormals)[i + 4], (*g_DrawNormals)[i + 5]);
			Vector3Df three((*g_DrawNormals)[i + 6], (*g_DrawNormals)[i + 7], (*g_DrawNormals)[i + 8]);
			Vector3Df four((*g_DrawNormals)[i + 9], (*g_DrawNormals)[i + 10], (*g_DrawNormals)[i + 11]);

			AddToVector(normals, one);
			AddToVector(normals, two);
			AddToVector(normals, three);
			AddToVector(normals, one);
			AddToVector(normals, three);
			AddToVector(normals, four);
		}

		std::vector<float> texCoord;
		//Create tex coords to draw triangle
		if(g_DrawTextureCoords->size() >= 4 * 2) //4 verts * 2 uv
		{
			for(size_t i = 0; i < g_DrawTextureCoords->size(); i += 8)
			{
				Vector2Df one((*g_DrawTextureCoords)[i], (*g_DrawTextureCoords)[i + 1]);
				Vector2Df two((*g_DrawTextureCoords)[i + 2], (*g_DrawTextureCoords)[i + 3]);
				Vector2Df three((*g_DrawTextureCoords)[i + 4], (*g_DrawTextureCoords)[i + 5]);
				Vector2Df four((*g_DrawTextureCoords)[i + 6], (*g_DrawTextureCoords)[i + 7]);

				AddToVector(texCoord, one);
				AddToVector(texCoord, two);
				AddToVector(texCoord, three);
				AddToVector(texCoord, one);
				AddToVector(texCoord, three);
				AddToVector(texCoord, four);
			}
		}

		DrawTriangles(vertices, texCoord, normals);
	}
Esempio n. 30
0
//----------------------------------------------------------------------------
void Smoke2D::CreateBoundaryMixedEffect (VisualEffect*& effect,
    VisualEffectInstance*& instance)
{
    PixelShader* pshader = new0 PixelShader("Wm5.BoundaryMixed2",
        1, 1, 0, 3, false);
    pshader->SetInput(0, "vertexTCoord", Shader::VT_FLOAT2,
        Shader::VS_TEXCOORD0);
    pshader->SetOutput(0, "pixelColor", Shader::VT_FLOAT4,
        Shader::VS_COLOR0);
    pshader->SetSampler(0, "MaskSampler", Shader::ST_2D);
    pshader->SetSampler(1, "OffsetSampler", Shader::ST_2D);
    pshader->SetSampler(2, "StateSampler", Shader::ST_2D);
    pshader->SetTextureUnits(msBoundaryMixedPTextureUnits);
    pshader->SetPrograms(msBoundaryMixedPPrograms);

    mIP->CreateEffect(pshader, effect, instance);

    // Create the mask and offset textures.
    Texture2D* maskMixed = new0 Texture2D(Texture::TF_A32B32G32R32F,
        mIMaxP1, mJMaxP1, 1);
    Texture2D* offsetMixed = new0 Texture2D(Texture::TF_A32B32G32R32F,
        mIMaxP1, mJMaxP1, 1);

    Float4* mask = (Float4*)maskMixed->GetData(0);
    Float4* offset = (Float4*)offsetMixed->GetData(0);
    Float4 one(1.0f, 1.0f, 1.0f, 1.0f);
    Float4 zero(0.0f, 0.0f, 0.0f, 0.0f);
    Float4 xEdgeMask(0.0f, 1.0f, 0.0f, 0.0f);
    Float4 x0EdgeOffset(+mDx, 0.0f, 0.0f, 0.0f);
    Float4 x1EdgeOffset(-mDx, 0.0f, 0.0f, 0.0f);
    Float4 yEdgeMask(1.0f, 0.0f, 0.0f, 0.0f);
    Float4 y0EdgeOffset(0.0f, +mDy, 0.0f, 0.0f);
    Float4 y1EdgeOffset(0.0f, -mDy, 0.0f, 0.0f);
    int x, y;

    // Interior.
    for (y = 1; y < mJMax; ++y)
    {
        for (x = 1; x < mIMax; ++x)
        {
            mask[Index(x,y)] = one;
            offset[Index(x,y)] = zero;
        }
    }

    // Edge-interior.
    for (x = 1; x < mIMax; ++x)
    {
        mask[Index(x,0)] = yEdgeMask;
        offset[Index(x,0)] = y0EdgeOffset;
        mask[Index(x,mJMax)] = yEdgeMask;
        offset[Index(x,mJMax)] = y1EdgeOffset;
    }
    for (y = 1; y < mJMax; ++y)
    {
        mask[Index(0,y)] = xEdgeMask;
        offset[Index(0,y)] = x0EdgeOffset;
        mask[Index(mIMax,y)] = xEdgeMask;
        offset[Index(mIMax,y)] = x1EdgeOffset;
    }

    // Corners.
    mask[Index(0,0)] = zero;
    mask[Index(mIMax,0)] = zero;
    mask[Index(0,mJMax)] = zero;
    mask[Index(mIMax,mJMax)] = zero;
    offset[Index(0,0)] = zero;
    offset[Index(mIMax,0)] = zero;
    offset[Index(0,mJMax)] = zero;
    offset[Index(mIMax,mJMax)] = zero;

    instance->SetPixelTexture(0, "MaskSampler", maskMixed);
    instance->SetPixelTexture(0, "OffsetSampler", offsetMixed);
    instance->SetPixelTexture(0, "StateSampler",
        mIP->GetTarget(2)->GetColorTexture(0));

    mRenderer->Bind(maskMixed);
    mRenderer->Bind(offsetMixed);
}