Changeset 3708 for trunk/test_more
 Timestamp:
 Aug 18, 2015 10:42:15 AM (4 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/test_more/checkpoint.cpp
r2859 r3708 4 4 5 5 CppAD is distributed under multiple licenses. This distribution is under 6 the terms of the 6 the terms of the 7 7 Eclipse Public License Version 1.0. 8 8 … … 36 36 return true; 37 37 } 38 39 bool test_one(void) 40 { bool ok = true; 41 using CppAD::checkpoint; 42 using CppAD::ADFun; 43 using CppAD::NearEqual; 44 size_t i, j, k, n = 4, ell = n1 , m = ell + 1; 45 double eps = 10. * std::numeric_limits<double>::epsilon(); 46 47 // checkpoint version of the function F(x) 48 ADVector ax(n), ay(ell), az(m); 49 for(j = 0; j < n; j++) 50 ax[j] = double(j); 51 checkpoint<double> f_check("f_check", f_algo, ax, ay); 52 checkpoint<double> g_check("g_check", g_algo, ay, az); 53 54 // Record a version of z = g[f(x)] without checkpointing 55 Independent(ax); 56 f_algo(ax, ay); 57 g_algo(ay, az); 58 ADFun<double> check_not(ax, az); 59 60 // Record a version of z = g[f(x)] with checkpointing 61 Independent(ax); 62 f_check(ax, ay); 63 g_check(ay, az); 64 ADFun<double> check_yes(ax, az); 65 66 // compare forward mode results for orders 0, 1, 2 67 size_t p = 2; 68 CPPAD_TESTVECTOR(double) x_p(n*(p+1)), z_not(m*(p+1)), z_yes(m*(p+1)); 69 for(j = 0; j < n; j++) 70 { for(k = 0; k <= p; k++) 71 x_p[ j * (p+1) + k ] = 1.0 / (p + 1  k); 72 } 73 z_not = check_not.Forward(p, x_p); 74 z_yes = check_yes.Forward(p, x_p); 75 for(i = 0; i < m; i++) 76 { for(k = 0; k <= p; k++) 77 { double zik_not = z_not[ i * (p+1) + k]; 78 double zik_yes = z_yes[ i * (p+1) + k]; 79 ok &= NearEqual(zik_not, zik_yes, eps, eps); 80 } 81 } 82 83 // compare reverse mode results 84 CPPAD_TESTVECTOR(double) w(m*(p+1)), dw_not(n*(p+1)), dw_yes(n*(p+1)); 85 dw_not = check_not.Reverse(p+1, w); 86 dw_yes = check_yes.Reverse(p+1, w); 87 for(j = 0; j < n; j++) 88 { for(k = 0; k <= p; k++) 89 { double dwjk_not = dw_not[ j * (p+1) + k]; 90 double dwjk_yes = dw_yes[ j * (p+1) + k]; 91 ok &= NearEqual(dwjk_not, dwjk_yes, eps, eps); 92 } 93 } 94 95 // mix sparsity so test both cases 96 f_check.option( CppAD::atomic_base<double>::bool_sparsity_enum ); 97 g_check.option( CppAD::atomic_base<double>::set_sparsity_enum ); 98 99 // compare forward mode Jacobian sparsity patterns 100 size_t q = n  1; 101 CppAD::vector< std::set<size_t> > r(n), s_not(m), s_yes(m); 102 for(j = 0; j < n; j++) 103 { if( j < q ) 104 r[j].insert(j); 105 else 106 { r[j].insert(0); 107 r[j].insert(1); 108 } 109 } 110 s_not = check_not.ForSparseJac(q, r); 111 s_yes = check_yes.ForSparseJac(q, r); 112 for(i = 0; i < m; i++) 113 ok &= s_not[i] == s_yes[i]; 114 115 // compare reverse mode Jacobian sparsity patterns 116 CppAD::vector< std::set<size_t> > s(m), r_not(m), r_yes(m); 117 for(i = 0; i < m; i++) 118 s[i].insert(i); 119 r_not = check_not.RevSparseJac(m, s); 120 r_yes = check_yes.RevSparseJac(m, s); 121 for(i = 0; i < m; i++) 122 ok &= s_not[i] == s_yes[i]; 123 124 125 // compare reverse mode Hessian sparsity patterns 126 CppAD::vector< std::set<size_t> > s_one(1), h_not(q), h_yes(q); 127 for(i = 0; i < m; i++) 128 s_one[0].insert(i); 129 h_not = check_not.RevSparseHes(q, s_one); 130 h_yes = check_yes.RevSparseHes(q, s_one); 131 for(i = 0; i < q; i++) 132 ok &= h_not[i] == h_yes[i]; 133 134 checkpoint<double>::clear(); 135 return ok; 136 } 38 137 } 39 138 40 139 bool checkpoint(void) 41 140 { bool ok = true; 42 using CppAD::checkpoint; 43 using CppAD::ADFun; 44 using CppAD::NearEqual; 45 size_t i, j, k, n = 4, ell = n1 , m = ell + 1; 46 double eps = 10. * std::numeric_limits<double>::epsilon(); 141 ok &= test_one(); 47 142 48 // checkpoint version of the function F(x)49 ADVector ax(n), ay(ell), az(m);50 for(j = 0; j < n; j++)51 ax[j] = double(j);52 checkpoint<double> f_check("f_check", f_algo, ax, ay);53 checkpoint<double> g_check("g_check", g_algo, ay, az);54 55 // Record a version of z = g[f(x)] without checkpointing56 Independent(ax);57 f_algo(ax, ay);58 g_algo(ay, az);59 ADFun<double> check_not(ax, az);60 61 // Record a version of z = g[f(x)] with checkpointing62 Independent(ax);63 f_check(ax, ay);64 g_check(ay, az);65 ADFun<double> check_yes(ax, az);66 67 // compare forward mode results for orders 0, 1, 268 size_t p = 2;69 CPPAD_TESTVECTOR(double) x_p(n*(p+1)), z_not(m*(p+1)), z_yes(m*(p+1));70 for(j = 0; j < n; j++)71 { for(k = 0; k <= p; k++)72 x_p[ j * (p+1) + k ] = 1.0 / (p + 1  k);73 }74 z_not = check_not.Forward(p, x_p);75 z_yes = check_yes.Forward(p, x_p);76 for(i = 0; i < m; i++)77 { for(k = 0; k <= p; k++)78 { double zik_not = z_not[ i * (p+1) + k];79 double zik_yes = z_yes[ i * (p+1) + k];80 ok &= NearEqual(zik_not, zik_yes, eps, eps);81 }82 }83 84 // compare reverse mode results85 CPPAD_TESTVECTOR(double) w(m*(p+1)), dw_not(n*(p+1)), dw_yes(n*(p+1));86 dw_not = check_not.Reverse(p+1, w);87 dw_yes = check_yes.Reverse(p+1, w);88 for(j = 0; j < n; j++)89 { for(k = 0; k <= p; k++)90 { double dwjk_not = dw_not[ j * (p+1) + k];91 double dwjk_yes = dw_yes[ j * (p+1) + k];92 ok &= NearEqual(dwjk_not, dwjk_yes, eps, eps);93 }94 }95 96 // mix sparsity so test both cases97 f_check.option( CppAD::atomic_base<double>::bool_sparsity_enum );98 g_check.option( CppAD::atomic_base<double>::set_sparsity_enum );99 100 // compare forward mode Jacobian sparsity patterns101 size_t q = n  1;102 CppAD::vector< std::set<size_t> > r(n), s_not(m), s_yes(m);103 for(j = 0; j < n; j++)104 { if( j < q )105 r[j].insert(j);106 else107 { r[j].insert(0);108 r[j].insert(1);109 }110 }111 s_not = check_not.ForSparseJac(q, r);112 s_yes = check_yes.ForSparseJac(q, r);113 for(i = 0; i < m; i++)114 ok &= s_not[i] == s_yes[i];115 116 // compare reverse mode Jacobian sparsity patterns117 CppAD::vector< std::set<size_t> > s(m), r_not(m), r_yes(m);118 for(i = 0; i < m; i++)119 s[i].insert(i);120 r_not = check_not.RevSparseJac(m, s);121 r_yes = check_yes.RevSparseJac(m, s);122 for(i = 0; i < m; i++)123 ok &= s_not[i] == s_yes[i];124 125 126 // compare reverse mode Hessian sparsity patterns127 CppAD::vector< std::set<size_t> > s_one(1), h_not(q), h_yes(q);128 for(i = 0; i < m; i++)129 s_one[0].insert(i);130 h_not = check_not.RevSparseHes(q, s_one);131 h_yes = check_yes.RevSparseHes(q, s_one);132 for(i = 0; i < q; i++)133 ok &= h_not[i] == h_yes[i];134 135 checkpoint<double>::clear();136 143 return ok; 137 144 }
Note: See TracChangeset
for help on using the changeset viewer.