// main program that runs all the tests int main(int argc, char *argv[]) { bool ok = true; const char *test[]= { "correct", "det_lu", "det_minor", "ode", "poly", "sparse_hessian", "sparse_jacobian", "speed" }; const size_t n_test = sizeof(test) / sizeof(test[0]); const size_t test_correct = 0; const size_t test_det_lu = 1; const size_t test_det_minor = 2; const size_t test_ode = 3; const size_t test_poly = 4; const size_t test_sparse_hessian = 5; const size_t test_sparse_jacobian= 6; const size_t test_speed = 7; assert( n_test == test_speed+1 ); size_t i; size_t match = n_test; int iseed = 0; bool error = argc < 3; if( ! error ) { for(i = 0; i < n_test; i++) if( strcmp(test[i], argv[1]) == 0 ) match = i; error = match == n_test; iseed = std::atoi( argv[2] ); error |= iseed < 0; global_retape = false; global_optimize = false; for(i = 3; i < size_t(argc); i++) { if( strcmp(argv[i], "retape") == 0 ) global_retape = true; else if( strcmp(argv[i], "optimize") == 0 ) global_optimize = true; else error = true; } } if( error ) { cout << "usage: ./" << AD_PACKAGE << " test seed option_list" << endl; cout << "test choices: " << endl; for(i = 0; i < n_test; i++) cout << "\t" << test[i] << endl; cout << "seed choices: "; cout << "a positive integer used as a random seed." << endl; cout << "option choices: "; cout << " \"retape\", \"optimize\"." << endl << endl; return 1; } // initialize the random number simulator CppAD::uniform_01(size_t(iseed)); // arguments needed for speed tests size_t n_size = 5; CppAD::vector<size_t> size_det_lu(n_size); CppAD::vector<size_t> size_det_minor(n_size); CppAD::vector<size_t> size_ode(n_size); CppAD::vector<size_t> size_poly(n_size); CppAD::vector<size_t> size_sparse_hessian(n_size); CppAD::vector<size_t> size_sparse_jacobian(n_size); for(i = 0; i < n_size; i++) { size_det_lu[i] = 3 * i + 1; size_det_minor[i] = i + 1; size_ode[i] = i + 1; size_poly[i] = 8 * i + 1; size_sparse_hessian[i] = 30 * (i + 1); size_sparse_jacobian[i] = 30 * (i + 1); } # ifndef NDEBUG size_t base_count = CPPAD_TRACK_COUNT(); # endif switch(match) { // run all the correctness tests case test_correct: if( available_det_lu() ) ok &= run_correct( correct_det_lu, "det_lu" ); if( available_det_minor() ) ok &= run_correct( correct_det_minor, "det_minor" ); if( available_ode() ) ok &= run_correct( correct_ode, "ode" ); if( available_poly() ) ok &= run_correct( correct_poly, "poly" ); if( available_sparse_hessian() ) ok &= run_correct( correct_sparse_hessian, "sparse_hessian" ); if( available_sparse_jacobian() ) ok &= run_correct( correct_sparse_jacobian, "sparse_jacobian" ); // summarize results assert( ok || (Run_error_count > 0) ); if( ok ) { cout << "All " << int(Run_ok_count) << " correctness tests passed." << endl; } else { cout << int(Run_error_count) << " correctness tests failed." << endl; } break; // --------------------------------------------------------- // run all the speed tests case test_speed: if( available_det_lu() ) Run_speed( speed_det_lu, size_det_lu, "det_lu" ); if( available_det_minor() ) Run_speed( speed_det_minor, size_det_minor, "det_minor" ); if( available_ode() ) Run_speed( speed_ode, size_ode, "ode" ); if( available_poly() ) Run_speed( speed_poly, size_poly, "poly" ); if( available_sparse_hessian() ) Run_speed( speed_sparse_hessian, size_sparse_hessian, "sparse_hessian" ); if( available_sparse_jacobian() ) Run_speed( speed_sparse_jacobian, size_sparse_jacobian, "sparse_jacobian" ); ok = true; break; // --------------------------------------------------------- case test_det_lu: if( ! available_det_lu() ) { cout << AD_PACKAGE << ": test " << argv[1] << " not available" << endl; exit(1); } ok &= run_correct(correct_det_lu, "det_lu"); Run_speed(speed_det_lu, size_det_lu, "det_lu"); break; // --------------------------------------------------------- case test_det_minor: if( ! available_det_minor() ) { cout << AD_PACKAGE << ": test " << argv[1] << " not available" << endl; exit(1); } ok &= run_correct(correct_det_minor, "det_minor"); Run_speed(speed_det_minor, size_det_minor, "det_minor"); break; // --------------------------------------------------------- case test_ode: if( ! available_ode() ) { cout << AD_PACKAGE << ": test " << argv[1] << " not available" << endl; exit(1); } ok &= run_correct(correct_ode, "ode"); Run_speed(speed_ode, size_ode, "ode"); break; // --------------------------------------------------------- case test_poly: if( ! available_poly() ) { cout << AD_PACKAGE << ": test " << argv[1] << " not available" << endl; exit(1); } ok &= run_correct(correct_poly, "poly"); Run_speed(speed_poly, size_poly, "poly"); break; // --------------------------------------------------------- case test_sparse_hessian: if( ! available_sparse_hessian() ) { cout << AD_PACKAGE << ": test " << argv[1] << " not available" << endl; exit(1); } ok &= run_correct(correct_sparse_hessian, "sparse_hessian"); Run_speed( speed_sparse_hessian, size_sparse_hessian, "sparse_hessian"); break; // --------------------------------------------------------- case test_sparse_jacobian: if( ! available_sparse_jacobian() ) { cout << AD_PACKAGE << ": test " << argv[1] << " not available" << endl; exit(1); } ok &= run_correct(correct_sparse_jacobian, "sparse_jacobian"); Run_speed( speed_sparse_jacobian, size_sparse_jacobian, "sparse_jacobian" ); break; // --------------------------------------------------------- default: assert(0); } # ifndef NDEBUG if( CPPAD_TRACK_COUNT() == base_count ) { Run_ok_count++; cout << "No memory leak detected" << endl; } else { ok = false; Run_error_count++; cout << "Memory leak detected" << endl; } # endif return static_cast<int>( ! ok ); }
// main program that runs all the tests int main(int argc, char *argv[]) { bool ok = true; enum test_enum { test_correct, test_speed, test_det_lu, test_det_minor, test_mat_mul, test_ode, test_poly, test_sparse_hessian, test_sparse_jacobian, test_error }; struct test_struct { const char *name; const test_enum index; }; const test_struct test_list[]= { { "correct", test_correct }, { "speed", test_speed }, { "det_lu", test_det_lu }, { "det_minor", test_det_minor }, { "mat_mul", test_mat_mul }, { "ode", test_ode }, { "poly", test_poly }, { "sparse_hessian", test_sparse_hessian }, { "sparse_jacobian", test_sparse_jacobian } }; const size_t n_test = sizeof(test_list) / sizeof(test_list[0]); size_t i; test_enum match = test_error; int iseed = 0; bool error = argc < 3; if( ! error ) { for(i = 0; i < n_test; i++) if( strcmp(test_list[i].name, argv[1]) == 0 ) match = test_list[i].index; error = match == test_error; iseed = std::atoi( argv[2] ); error |= iseed < 0; global_onetape = false; global_colpack = false; global_optimize = false; global_atomic = false; global_memory = false; global_boolsparsity = false; for(i = 3; i < size_t(argc); i++) { if( strcmp(argv[i], "onetape") == 0 ) global_onetape = true; else if( strcmp(argv[i], "colpack") == 0 ) global_colpack = true; else if( strcmp(argv[i], "optimize") == 0 ) global_optimize = true; else if( strcmp(argv[i], "atomic") == 0 ) global_atomic = true; else if( strcmp(argv[i], "memory") == 0 ) global_memory = true; else if( strcmp(argv[i], "boolsparsity") == 0 ) global_boolsparsity = true; else error = true; } } if( error ) { cout << "usage: ./speed_" << AD_PACKAGE << " test seed option_list" << endl; cout << "test choices: " << endl; for(i = 0; i < n_test; i++) cout << "\t" << test_list[i].name << endl; cout << "seed choices: "; cout << "a positive integer used as a random seed." << endl; cout << "option choices: "; cout << " \"onetape\","; cout << " \"colpack\","; cout << " \"optimize\","; cout << " \"atomic\","; cout << " \"memory\","; cout << " \"boolsparsity\"." << endl << endl; return 1; } if( global_memory ) CppAD::thread_alloc::hold_memory(true); // initialize the random number simulator CppAD::uniform_01(size_t(iseed)); // arguments needed for speed tests size_t n_size = 5; CppAD::vector<size_t> size_det_lu(n_size); CppAD::vector<size_t> size_det_minor(n_size); CppAD::vector<size_t> size_mat_mul(n_size); CppAD::vector<size_t> size_ode(n_size); CppAD::vector<size_t> size_poly(n_size); CppAD::vector<size_t> size_sparse_hessian(n_size); CppAD::vector<size_t> size_sparse_jacobian(n_size); for(i = 0; i < n_size; i++) { size_det_minor[i] = i + 1; size_det_lu[i] = 10 * i + 1; size_mat_mul[i] = 10 * i + 1; size_ode[i] = 10 * i + 1; size_poly[i] = 10 * i + 1; size_sparse_hessian[i] = 100 * (i + 1) * (i + 1); size_sparse_jacobian[i] = 100 * (i + 1) * (i + 1); } switch(match) { // run all the correctness tests case test_correct: ok &= run_correct( available_det_lu, correct_det_lu, "det_lu" ); ok &= run_correct( available_det_minor, correct_det_minor, "det_minor" ); ok &= run_correct( available_mat_mul, correct_mat_mul, "mat_mul" ); ok &= run_correct( available_ode, correct_ode, "ode" ); ok &= run_correct( available_poly, correct_poly, "poly" ); ok &= run_correct( available_sparse_hessian, correct_sparse_hessian, "sparse_hessian" ); ok &= run_correct( available_sparse_jacobian, correct_sparse_jacobian, "sparse_jacobian" ); // summarize results assert( ok || (Run_error_count > 0) ); if( ok ) { cout << "All " << int(Run_ok_count) << " correctness tests passed." << endl; } else { cout << int(Run_error_count) << " correctness tests failed." << endl; } break; // --------------------------------------------------------- // run all the speed tests case test_speed: if( available_det_lu() ) run_speed( speed_det_lu, size_det_lu, "det_lu" ); if( available_det_minor() ) run_speed( speed_det_minor, size_det_minor, "det_minor" ); if( available_mat_mul() ) run_speed( speed_mat_mul, size_mat_mul, "mat_mul" ); if( available_ode() ) run_speed( speed_ode, size_ode, "ode" ); if( available_poly() ) run_speed( speed_poly, size_poly, "poly" ); if( available_sparse_hessian() ) run_speed( speed_sparse_hessian, size_sparse_hessian, "sparse_hessian" ); if( available_sparse_jacobian() ) run_speed( speed_sparse_jacobian, size_sparse_jacobian, "sparse_jacobian" ); ok = true; break; // --------------------------------------------------------- case test_det_lu: if( ! available_det_lu() ) { not_available_message( argv[1] ); exit(1); } ok &= run_correct( available_det_lu, correct_det_lu, "det_lu") ; run_speed(speed_det_lu, size_det_lu, "det_lu"); break; // --------------------------------------------------------- case test_det_minor: if( ! available_det_minor() ) { not_available_message( argv[1] ); exit(1); } ok &= run_correct( available_det_minor, correct_det_minor, "det_minor" ); run_speed(speed_det_minor, size_det_minor, "det_minor"); break; // --------------------------------------------------------- case test_mat_mul: if( ! available_mat_mul() ) { not_available_message( argv[1] ); exit(1); } ok &= run_correct( available_mat_mul, correct_mat_mul, "mat_mul" ); run_speed(speed_mat_mul, size_mat_mul, "mat_mul"); break; // --------------------------------------------------------- case test_ode: if( ! available_ode() ) { not_available_message( argv[1] ); exit(1); } ok &= run_correct( available_ode, correct_ode, "ode" ); run_speed(speed_ode, size_ode, "ode"); break; // --------------------------------------------------------- case test_poly: if( ! available_poly() ) { not_available_message( argv[1] ); exit(1); } ok &= run_correct( available_poly, correct_poly, "poly" ); run_speed(speed_poly, size_poly, "poly"); break; // --------------------------------------------------------- case test_sparse_hessian: if( ! available_sparse_hessian() ) { not_available_message( argv[1] ); exit(1); } ok &= run_correct( available_sparse_hessian, correct_sparse_hessian, "sparse_hessian" ); run_speed( speed_sparse_hessian, size_sparse_hessian, "sparse_hessian" ); cout << AD_PACKAGE << "_sparse_hessian_sweep = "; for(i = 0; i < size_sparse_hessian.size(); i++) { if( i == 0 ) cout << "[ "; else cout << ", "; size_t n_sweep; info_sparse_hessian(size_sparse_hessian[i], n_sweep); cout << n_sweep; } cout << " ]" << endl; break; // --------------------------------------------------------- case test_sparse_jacobian: if( ! available_sparse_jacobian() ) { not_available_message( argv[1] ); exit(1); } ok &= run_correct( available_sparse_jacobian, correct_sparse_jacobian, "sparse_jacobian" ); run_speed( speed_sparse_jacobian, size_sparse_jacobian, "sparse_jacobian" ); cout << AD_PACKAGE << "_sparse_jacobian_n_sweep = "; for(i = 0; i < size_sparse_jacobian.size(); i++) { if( i == 0 ) cout << "[ "; else cout << ", "; size_t n_sweep; info_sparse_jacobian(size_sparse_jacobian[i], n_sweep); cout << n_sweep; } cout << " ]" << endl; break; // --------------------------------------------------------- default: assert(0); } # ifndef NDEBUG // return memory for vectors that are still in scope size_det_lu.clear(); size_det_minor.clear(); size_mat_mul.clear(); size_ode.clear(); size_poly.clear(); size_sparse_hessian.clear(); size_sparse_jacobian.clear(); // check for memory leak if( CppAD::thread_alloc::free_all() ) { Run_ok_count++; cout << "No memory leak detected" << endl; } else { ok = false; Run_error_count++; cout << "Memory leak detected" << endl; } # endif return static_cast<int>( ! ok ); }