### Install Boost Distribution - Command Line Source: https://www.boost.org/doc/libs/master/doc/html/mpi/getting_started Command to install the entire Boost distribution after it has been built. This is executed from the root of the Boost distribution directory. ```bash $cd ./b2 install ``` -------------------------------- ### Link Boost.MPI Applications - Compilation Command Source: https://www.boost.org/doc/libs/master/doc/html/mpi/getting_started Example of how to compile and link a C++ application using Boost.MPI. It includes specifying include paths, library directories, and linking against the necessary Boost.MPI and Boost.Serialization libraries. ```cpp mpic++ -I/path/to/boost/mpi my_application.cpp -Llibdir \ -lboost_mpi -lboost_serialization ``` -------------------------------- ### Compile and Run MPI Test Program (OpenMPI Example) Source: https://www.boost.org/doc/libs/master/doc/html/mpi/getting_started Command-line instructions for compiling and running the MPI test program using OpenMPI. This includes compilation with `mpiCC`, starting the LAM/MPI daemon, and executing the program with `mpirun`. ```bash mpiCC -o mpi-test mpi-test.cpp lamboot mpirun -np 2 ./mpi-test lamhalt ``` -------------------------------- ### Advanced Boost.Build MPI Configuration Source: https://www.boost.org/doc/libs/master/doc/html/mpi/getting_started An example of a more detailed 'project-config.jam' configuration for Boost.Build's MPI support. This allows specifying custom MPI compiler wrappers, compilation/link options, and MPI runner commands. ```jam using mpi : [] : [] : [] ; ``` -------------------------------- ### Show MPI Compiler Flags - Command Line Source: https://www.boost.org/doc/libs/master/doc/html/mpi/getting_started Demonstrates how to retrieve compilation and link flags for MPI wrappers using the `--show` option. This is useful for generating the correct Jam directives when manual configuration is needed, as shown in the Intel MPI example. ```bash $ mpiicc -show icc -I/softs/.../include ... -L/softs/.../lib ... -Xlinker -rpath -Xlinker /softs/.../lib .... -lmpi -ldl -lrt -lpthread $ ``` ```bash $ mpicc --showme icc -I/opt/.../include -pthread -L/opt/.../lib -lmpi -ldl -lm -lnuma -Wl,--export-dynamic -lrt -lnsl -lutil -lm -ldl $ ``` ```bash $ mpicc --showme:compile -I/opt/mpi/bullxmpi/1.2.8.3/include -pthread ``` ```bash $ mpicc --showme:link -pthread -L/opt/.../lib -lmpi -ldl -lm -lnuma -Wl,--export-dynamic -lrt -lnsl -lutil -lm -ldl $ ``` -------------------------------- ### Build Boost Distribution - Command Line Source: https://www.boost.org/doc/libs/master/doc/html/mpi/getting_started Commands to build the entire Boost distribution or specifically the Boost.MPI library and its dependencies. These commands are executed from the Boost distribution directory or the Boost.MPI build directory. ```bash $cd ./b2 ``` ```bash $cd /lib/mpi/build ../../../b2 ``` -------------------------------- ### Link Boost.MPI Python Bindings - Compilation Command Source: https://www.boost.org/doc/libs/master/doc/html/mpi/getting_started Example of how to link applications that use both C++ Boost.MPI and its Python bindings. This command adds the `boost_mpi_python` library to the link command, which is necessary for C++ type registration or using skeleton/content mechanisms within Python. ```cpp mpic++ -I/path/to/boost/mpi my_application.cpp -Llibdir \ -lboost_mpi -lboost_serialization -lboost_mpi_python-gcc ``` -------------------------------- ### Configure Boost.Build for MPI Support Source: https://www.boost.org/doc/libs/master/doc/html/mpi/getting_started A Jam file configuration snippet for Boost.Build to enable MPI support. This line should be added to the 'project-config.jam' file to inform the build system about the MPI environment. ```jam using mpi ; ``` -------------------------------- ### Ratio IO Example: Get SI Prefixes and Symbols Source: https://www.boost.org/doc/libs/master/doc/html/ratio/users_guide Demonstrates how to use ratio_string::prefix() and ratio_string::symbol() to retrieve the string representation of SI prefixes and symbols for different ratios. It includes examples for standard SI prefixes (deca, giga) and custom ratios. This example requires the boost/ratio/ratio_io.hpp header and uses iostream for output. ```cpp #include #include int main() { using namespace std; using namespace boost; cout << "ratio_string::prefix() = " << ratio_string::prefix() << '\n'; cout << "ratio_string::symbol() = " << ratio_string::symbol() << '\n'; cout << "ratio_string::prefix() = " << ratio_string::prefix() << '\n'; cout << "ratio_string::symbol() = " << ratio_string::symbol() << '\n'; cout << "ratio_string, char>::prefix() = " << ratio_string, char>::prefix() << '\n'; cout << "ratio_string, char>::symbol() = " << ratio_string, char>::symbol() << '\n'; } ``` -------------------------------- ### Run Boost.MPI Regression Tests - Command Line Source: https://www.boost.org/doc/libs/master/doc/html/mpi/getting_started Command to execute the regression tests for the Boost.MPI library. This is performed from the Boost.MPI test directory within the Boost distribution. ```bash $cd /lib/mpi/test ../../../b2 ``` -------------------------------- ### Test MPI Send and Receive in C++ Source: https://www.boost.org/doc/libs/master/doc/html/mpi/getting_started A basic C++ program demonstrating inter-processor communication using MPI's Send and Receive functions. It's used to verify the MPI implementation is working correctly. This code requires the MPI library to be installed and linked. ```cpp #include #include int main(int argc, char* argv[]) { MPI_Init(&argc, &argv); int rank; MPI_Comm_rank(MPI_COMM_WORLD, &rank); if (rank == 0) { int value = 17; int result = MPI_Send(&value, 1, MPI_INT, 1, 0, MPI_COMM_WORLD); if (result == MPI_SUCCESS) std::cout << "Rank 0 OK!" << std::endl; } else if (rank == 1) { int value; int result = MPI_Recv(&value, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); if (result == MPI_SUCCESS && value == 17) std::cout << "Rank 1 OK!" << std::endl; } MPI_Finalize(); return 0; } ``` -------------------------------- ### Getting Function Information from Pointer with Boost.Stacktrace Source: https://www.boost.org/doc/libs/master/doc/html/stacktrace/getting_started This C++ example demonstrates how to use `boost::stacktrace::frame` to get information about a function when provided with its pointer. It constructs a `frame` object from a function pointer (`old_signal_function`) and then prints the frame, which includes the function name and file/line information if available. This requires including `` and ``. ```cpp #include // ::signal #include #include // std::cerr #include // std::exit void print_signal_handler_and_exit() { typedef void(*function_t)(int); function_t old_signal_function = ::signal(SIGSEGV, SIG_DFL); boost::stacktrace::frame f(old_signal_function); std::cout << f << std::endl; std::exit(0); } ``` -------------------------------- ### Configure MPI Launch Syntax - Jam Directive Source: https://www.boost.org/doc/libs/master/doc/html/mpi/getting_started Specifies the command used to launch MPI programs when the default launch syntax cannot be detected. This Jam directive is used for running tests and ensures that MPI programs are executed with the correct launcher and arguments. ```jam using mpi : mpiicc : [] : mpiexec.hydra -n ; ``` -------------------------------- ### Configure MPI Wrapper and Link Options - Jam Directive Source: https://www.boost.org/doc/libs/master/doc/html/mpi/getting_started Provides explicit compilation and link options for Boost.MPI when the MPI wrapper is eccentric or non-existent. This Jam directive is used to manually specify library paths, include directories, and shared libraries required for certain MPI implementations, like Intel MPI. ```jam using mpi : mpiicc : /softs/intel/impi/5.0.1.035/intel64/lib /softs/intel/impi/5.0.1.035/intel64/lib/release_mt /softs/intel/impi/5.0.1.035/intel64/include mpifort mpi_mt mpigi dl rt ; ``` -------------------------------- ### Loading a Shared Library with Boost.DLL Source: https://www.boost.org/doc/libs/master/doc/html/boost_dll/getting_started Demonstrates how to load a shared library using `boost::dll::shared_library` by providing the path to the library file. Dependencies include `boost_filesystem` and `boost_system`. ```C++ boost::dll::shared_library lib("/test/boost/application/libtest_library.so"); ``` -------------------------------- ### Hello, World! - Boost.Accumulators C++ Example Source: https://www.boost.org/doc/libs/master/doc/html/accumulators/user_s_guide A C++ example demonstrating the basic usage of Boost.Accumulators to calculate the mean and 2nd moment of a sequence of double values. It includes necessary headers and shows how to define, update, and extract results from an accumulator set. ```cpp #include #include #include #include #include using namespace boost::accumulators; int main() { // Define an accumulator set for calculating the mean and the // 2nd moment ... accumulator_set > > acc; // push in some data ... acc(1.2); acc(2.3); acc(3.4); acc(4.5); // Display the results ... std::cout << "Mean: " << mean(acc) << std::endl; std::cout << "Moment: " << moment<2>(acc) << std::endl; return 0; } ``` -------------------------------- ### Boost Lambda Namespace Usage Example (C++) Source: https://www.boost.org/doc/libs/master/doc/html/lambda/getting_started This code snippet demonstrates the common convention of using namespace declarations for `std` and `boost::lambda` in Boost lambda library examples. It assumes these using declarations are in effect for brevity in documentation. ```cpp using namespace std; using namespace boost::lambda; ``` -------------------------------- ### Configure MPI Wrapper Path - Jam Directive Source: https://www.boost.org/doc/libs/master/doc/html/mpi/getting_started Specifies the path to the MPI compiler wrapper when it's not in the system's PATH or has a non-standard name. This Jam directive tells the build system how to locate and use the MPI compiler. ```jam using mpi : /opt/mpi/bullxmpi/1.2.8.3/bin/mpicc ; ``` -------------------------------- ### Boost.Random Quick Start Example Source: https://www.boost.org/doc/libs/master/doc/html/boost_random A concise example demonstrating the basic usage of Boost.Random for generating random numbers. It initializes a Mersenne Twister engine and a uniform integer distribution to simulate rolling a die. This requires the Boost.Random library. ```cpp boost::random::mt19937 rng; // produces randomness out of thin air // see pseudo-random number generators bboost::random::uniform_int_distribution<> six(1,6); // distribution that maps to 1..6 // see random number distributions int x = six(rng); // simulate rolling a die ``` -------------------------------- ### Example Usage of Proto::eval() Source: https://www.boost.org/doc/libs/master/doc/html/proto/users_guide Demonstrates a practical C++ example of how to use the `proto::eval()` function within a templated function. It shows how to define a context, evaluate an expression, and return the result. ```cpp template typename proto::result_of::eval::type MyEvaluate(Expr const &expr) { // Some user-defined context type MyContext ctx; // Evaluate an expression with the context return proto::eval(expr, ctx); } ``` -------------------------------- ### Hello World Example for Boost.Chrono Timing Source: https://www.boost.org/doc/libs/master/doc/html/chrono/users_guide A C++ program demonstrating how to use Boost.Chrono to time the execution of a code block. It measures the time taken to perform a loop of square root calculations. This example requires linking against the Boost.Chrono and Boost.System libraries. ```cpp #include #include #include int main() { boost::chrono::system_clock::time_point start = boost::chrono::system_clock::now(); for ( long i = 0; i < 10000000; ++i ) std::sqrt( 123.456L ); // burn some time boost::chrono::duration sec = boost::chrono::system_clock::now() - start; std::cout << "took " << sec.count() << " seconds\n"; return 0; } ``` -------------------------------- ### Importing Symbols from a Shared Library using Boost.DLL Source: https://www.boost.org/doc/libs/master/doc/html/boost_dll/getting_started Shows how to import functions and variables from a loaded shared library using `get` and `get_alias` member functions of `boost::dll::shared_library`. Imported symbols are valid as long as the `shared_library` instance exists. ```C++ int plugin_constant = lib.get("integer_variable"); auto function_ptr = lib.get("function_returning_int"); int& i = lib.get_alias("alias_to_int_variable"); ``` -------------------------------- ### Quickbook Section Syntax Example Source: https://www.boost.org/doc/libs/master/doc/html/quickbook/change_log Demonstrates the basic syntax for defining a section in Quickbook, including the start and end tags. This is fundamental for structuring documents. ```quickbook [section x] blah... [endsect] ``` -------------------------------- ### Hello, World! - Boost.Accumulators Output Source: https://www.boost.org/doc/libs/master/doc/html/accumulators/user_s_guide The expected output from the 'Hello, World!' C++ example using Boost.Accumulators. It displays the calculated mean and 2nd moment values after processing a set of double inputs. ```text Mean: 2.85 Moment: 9.635 ``` -------------------------------- ### Get Previous Character using boost::metaparse::get_prev_char (C++) Source: https://www.boost.org/doc/libs/master/doc/html/metaparse/reference Provides an example of how to get the last character processed by a source position using boost::metaparse::get_prev_char. This function's behavior for 'start' is unspecified. ```C++ #include #include // The following is a conceptual representation, direct usage requires appropriate context. // For example: // using namespace boost::metaparse; // static_assert(get_prev_char>::type::value == ch::value, ""); ``` -------------------------------- ### Hello World: Build and Evaluate Expression Template (C++) Source: https://www.boost.org/doc/libs/master/doc/html/proto/users_guide A basic example demonstrating how to construct an expression template and evaluate it using Boost.Proto. It includes necessary headers for core and context functionalities, and defines a simple evaluation function. ```cpp #include #include #include // This #include is only needed for compilers that use typeof emulation: #include namespace proto = boost::proto; proto::terminal< std::ostream & >::type cout_ = {std::cout}; template< typename Expr > void evaluate( Expr const & expr ) { proto::default_context ctx; proto::eval(expr, ctx); } int main() { evaluate( cout_ << "hello" << ',' << " world" ); return 0; } ``` -------------------------------- ### Install packages and configure xsltproc/boostbook (Debian/Ubuntu) Source: https://www.boost.org/doc/libs/master/doc/html/quickbook/install Installs necessary packages (xsltproc, docbook-xsl, docbook-xml) using apt-get and configures Boost Build to use the system's default locations for these tools on Debian-based systems. ```shell sudo apt-get install xsltproc docbook-xsl docbook-xml ``` ```jam using xsltproc ; using boostbook : /usr/share/xml/docbook/stylesheet/nwalsh : /usr/share/xml/docbook/schema/dtd/4.2 ; # Remove this line if you're not using doxygen using doxygen ; ``` -------------------------------- ### Configure Boost Build for BoostBook Source: https://www.boost.org/doc/libs/master/doc/html/quickbook/install Sets up Boost Build to handle BoostBook documentation compilation by specifying the paths to the XSL stylesheets and XML DTDs. This is crucial for generating BoostBook output. ```jam using boostbook : /opt/local/share/xsl/docbook-xsl/ : /opt/local/share/xml/docbook/4.2 ; ``` -------------------------------- ### Boost.MPI Python Quickstart: Hello World Source: https://www.boost.org/doc/libs/master/doc/html/mpi/python A simple 'Hello, World!' program using Boost.MPI in Python. It demonstrates importing the module and printing the process rank and total size. This code requires the boost.mpi module to be installed and accessible in the Python environment. ```python import boost.mpi as mpi print "I am process %d of %d." % (mpi.rank, mpi.size) ``` -------------------------------- ### Boost PFR get Function Template Example Source: https://www.boost.org/doc/libs/master/doc/html/doxygen/reference_section_of_pfr/core_8hpp_1a07b17ed2eaa9115346f5b14899ec53e4 An example demonstrating the usage of `boost::pfr::get` with a simple struct. It shows accessing fields by both index (e.g., `get<0>(s)`) and by type (e.g., `get(s)`), and modifying field values. ```cpp struct my_struct { int i, short s; }; my_struct s {10, 11}; assert(boost::pfr::get<0>(s) == 10); boost::pfr::get<1>(s) = 0; assert(boost::pfr::get(s) == 10); boost::pfr::get(s) = 11; ``` -------------------------------- ### C++ Example: Accumulator Setup and Usage for Tail Variate Means Source: https://www.boost.org/doc/libs/master/doc/html/accumulators/user_s_guide This example demonstrates how to set up and use accumulators for calculating both relative and absolute tail variate means in C++. It covers defining accumulator types for left and right tails, initializing them with cache sizes, and processing data with covariates. The example also includes assertions to verify the calculated relative tail variate means for specific quantile probabilities. ```cpp std::size_t c = 5; // cache size typedef double variate_type; typedef std::vector variate_set_type; typedef accumulator_set(relative)>, tag::tail > accumulator_t1; typedef accumulator_set(absolute)>, tag::tail > accumulator_t2; typedef accumulator_set(relative)>, tag::tail > accumulator_t3; typedef accumulator_set(absolute)>, tag::tail > accumulator_t4; accumulator_t1 acc1( right_tail_cache_size = c ); accumulator_t2 acc2( right_tail_cache_size = c ); accumulator_t3 acc3( left_tail_cache_size = c ); accumulator_t4 acc4( left_tail_cache_size = c ); variate_set_type cov1, cov2, cov3, cov4, cov5; double c1[] = { 10., 20., 30., 40. }; // 100 double c2[] = { 26., 4., 17., 3. }; // 50 double c3[] = { 46., 64., 40., 50. }; // 200 double c4[] = { 1., 3., 70., 6. }; // 80 double c5[] = { 2., 2., 2., 14. }; // 20 cov1.assign(c1, c1 + sizeof(c1)/sizeof(variate_type)); cov2.assign(c2, c2 + sizeof(c2)/sizeof(variate_type)); cov3.assign(c3, c3 + sizeof(c3)/sizeof(variate_type)); cov4.assign(c4, c4 + sizeof(c4)/sizeof(variate_type)); cov5.assign(c5, c5 + sizeof(c5)/sizeof(variate_type)); acc1(100., covariate1 = cov1); acc1( 50., covariate1 = cov2); acc1(200., covariate1 = cov3); acc1( 80., covariate1 = cov4); acc1( 20., covariate1 = cov5); acc2(100., covariate1 = cov1); acc2( 50., covariate1 = cov2); acc2(200., covariate1 = cov3); acc2( 80., covariate1 = cov4); acc2( 20., covariate1 = cov5); acc3(100., covariate1 = cov1); acc3( 50., covariate1 = cov2); acc3(200., covariate1 = cov3); acc3( 80., covariate1 = cov4); acc3( 20., covariate1 = cov5); acc4(100., covariate1 = cov1); acc4( 50., covariate1 = cov2); acc4(200., covariate1 = cov3); acc4( 80., covariate1 = cov4); acc4( 20., covariate1 = cov5); // check relative risk contributions BOOST_CHECK_EQUAL( *(relative_tail_variate_means(acc1, quantile_probability = 0.7).begin() ), 14./75. ); // (10 + 46) / 300 = 14/75 BOOST_CHECK_EQUAL( *(relative_tail_variate_means(acc1, quantile_probability = 0.7).begin() + 1), 7./25. ); // (20 + 64) / 300 = 7/25 BOOST_CHECK_EQUAL( *(relative_tail_variate_means(acc1, quantile_probability = 0.7).begin() + 2), 7./30. ); // (30 + 40) / 300 = 7/30 BOOST_CHECK_EQUAL( *(relative_tail_variate_means(acc1, quantile_probability = 0.7).begin() + 3), 3./10. ); // (40 + 50) / 300 = 3/10 BOOST_CHECK_EQUAL( *(relative_tail_variate_means(acc3, quantile_probability = 0.3).begin() ), 14./35. ); // (26 + 2) / 70 = 14/35 BOOST_CHECK_EQUAL( *(relative_tail_variate_means(acc3, quantile_probability = 0.3).begin() + 1), 3./35. ); // ( 4 + 2) / 70 = 3/35 BOOST_CHECK_EQUAL( *(relative_tail_variate_means(acc3, quantile_probability = 0.3).begin() + 2), 19./70. ); // (17 + 2) / 70 = 19/70 BOOST_CHECK_EQUAL( *(relative_tail_variate_means(acc3, quantile_probability = 0.3).begin() + 3), 17./70. ); // ( 3 + 14) / 70 = 17/70 // check absolute risk contributions ``` -------------------------------- ### Boost.MPI Environment Initialization Example Source: https://www.boost.org/doc/libs/master/doc/html/doxygen/classboost_1_1mpi_1_1environment A C++ example demonstrating how to initialize the MPI environment using the `boost::mpi::environment` class within the `main` function. The constructor handles MPI initialization. ```cpp int main(int argc, char* argv[]) { mpi::environment env(argc, argv); } ``` -------------------------------- ### Boost Accumulator Weighted Tail Variate Means Example Usage Source: https://www.boost.org/doc/libs/master/doc/html/accumulators/user_s_guide Demonstrates the usage of `weighted_tail_variate_means` for both relative and absolute calculations on right and left tails. It shows accumulator setup, data insertion with weights and covariates, and checking the calculated means. ```c++ std::size_t c = 5; // cache size typedef double variate_type; typedef std::vector variate_set_type; accumulator_set(relative)>, double > acc1( right_tail_cache_size = c ); accumulator_set(absolute)>, double > acc2( right_tail_cache_size = c ); accumulator_set(relative)>, double > acc3( left_tail_cache_size = c ); accumulator_set(absolute)>, double > acc4( left_tail_cache_size = c ); variate_set_type cov1, cov2, cov3, cov4, cov5; double c1[] = { 10., 20., 30., 40. }; // 100 double c2[] = { 26., 4., 17., 3. }; // 50 double c3[] = { 46., 64., 40., 50. }; // 200 double c4[] = { 1., 3., 70., 6. }; // 80 double c5[] = { 2., 2., 2., 14. }; // 20 cov1.assign(c1, c1 + sizeof(c1)/sizeof(variate_type)); cov2.assign(c2, c2 + sizeof(c2)/sizeof(variate_type)); cov3.assign(c3, c3 + sizeof(c3)/sizeof(variate_type)); cov4.assign(c4, c4 + sizeof(c4)/sizeof(variate_type)); cov5.assign(c5, c5 + sizeof(c5)/sizeof(variate_type)); acc1(100., weight = 0.8, covariate1 = cov1); acc1( 50., weight = 0.9, covariate1 = cov2); acc1(200., weight = 1.0, covariate1 = cov3); acc1( 80., weight = 1.1, covariate1 = cov4); acc1( 20., weight = 1.2, covariate1 = cov5); acc2(100., weight = 0.8, covariate1 = cov1); acc2( 50., weight = 0.9, covariate1 = cov2); acc2(200., weight = 1.0, covariate1 = cov3); acc2( 80., weight = 1.1, covariate1 = cov4); acc2( 20., weight = 1.2, covariate1 = cov5); acc3(100., weight = 0.8, covariate1 = cov1); acc3( 50., weight = 0.9, covariate1 = cov2); acc3(200., weight = 1.0, covariate1 = cov3); acc3( 80., weight = 1.1, covariate1 = cov4); acc3( 20., weight = 1.2, covariate1 = cov5); acc4(100., weight = 0.8, covariate1 = cov1); acc4( 50., weight = 0.9, covariate1 = cov2); acc4(200., weight = 1.0, covariate1 = cov3); acc4( 80., weight = 1.1, covariate1 = cov4); acc4( 20., weight = 1.2, covariate1 = cov5); // check relative risk contributions BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc1, quantile_probability = 0.7).begin() ), (0.8*10 + 1.0*46)/(0.8*100 + 1.0*200) ); BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc1, quantile_probability = 0.7).begin() + 1), (0.8*20 + 1.0*64)/(0.8*100 + 1.0*200) ); BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc1, quantile_probability = 0.7).begin() + 2), (0.8*30 + 1.0*40)/(0.8*100 + 1.0*200) ); BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc1, quantile_probability = 0.7).begin() + 3), (0.8*40 + 1.0*50)/(0.8*100 + 1.0*200) ); ``` -------------------------------- ### Example Usage of Weighted P-Square Quantile Source: https://www.boost.org/doc/libs/master/doc/html/accumulators/user_s_guide Demonstrates the initialization and usage of the accumulator_set with the weighted_p_square_quantile statistic. It includes setup for random number generation, accumulator initialization with different quantiles, processing of samples with uniform and weighted distributions, and assertions to verify the calculated quantiles. ```cpp typedef accumulator_set, double> accumulator_t; // tolerance in % double epsilon = 1; // some random number generators double mu4 = -1.0; double mu5 = -1.0; double mu6 = 1.0; double mu7 = 1.0; boost::lagged_fibonacci607 rng; boost::normal_distribution<> mean_sigma4(mu4, 1); boost::normal_distribution<> mean_sigma5(mu5, 1); boost::normal_distribution<> mean_sigma6(mu6, 1); boost::normal_distribution<> mean_sigma7(mu7, 1); boost::variate_generator > normal4(rng, mean_sigma4); boost::variate_generator > normal5(rng, mean_sigma5); boost::variate_generator > normal6(rng, mean_sigma6); boost::variate_generator > normal7(rng, mean_sigma7); accumulator_t acc0(quantile_probability = 0.001); accumulator_t acc1(quantile_probability = 0.025); accumulator_t acc2(quantile_probability = 0.975); accumulator_t acc3(quantile_probability = 0.999); accumulator_t acc4(quantile_probability = 0.001); accumulator_t acc5(quantile_probability = 0.025); accumulator_t acc6(quantile_probability = 0.975); accumulator_t acc7(quantile_probability = 0.999); for (std::size_t i=0; i<100000; ++i) { double sample = rng(); acc0(sample, weight = 1.); acc1(sample, weight = 1.); acc2(sample, weight = 1.); acc3(sample, weight = 1.); double sample4 = normal4(); double sample5 = normal5(); double sample6 = normal6(); double sample7 = normal7(); acc4(sample4, weight = std::exp(-mu4 * (sample4 - 0.5 * mu4))); acc5(sample5, weight = std::exp(-mu5 * (sample5 - 0.5 * mu5))); acc6(sample6, weight = std::exp(-mu6 * (sample6 - 0.5 * mu6))); acc7(sample7, weight = std::exp(-mu7 * (sample7 - 0.5 * mu7))); } // check for uniform distribution with weight = 1 BOOST_CHECK_CLOSE( weighted_p_square_quantile(acc0), 0.001, 15 ); BOOST_CHECK_CLOSE( weighted_p_square_quantile(acc1), 0.025, 5 ); BOOST_CHECK_CLOSE( weighted_p_square_quantile(acc2), 0.975, epsilon ); BOOST_CHECK_CLOSE( weighted_p_square_quantile(acc3), 0.999, epsilon ); // check for shifted standard normal distribution ("importance sampling") BOOST_CHECK_CLOSE( weighted_p_square_quantile(acc4), -3.090232, epsilon ); BOOST_CHECK_CLOSE( weighted_p_square_quantile(acc5), -1.959963, epsilon ); BOOST_CHECK_CLOSE( weighted_p_square_quantile(acc6), 1.959963, epsilon ); BOOST_CHECK_CLOSE( weighted_p_square_quantile(acc7), 3.090232, epsilon ); ``` -------------------------------- ### Configure quickbook (Windows) Source: https://www.boost.org/doc/libs/master/doc/html/quickbook/install Adds a pre-built quickbook executable to the Boost Build configuration for Windows. This avoids rebuilding quickbook every time documentation is generated. ```jam using quickbook : "C:/Users/example/Documents/boost/xml/bin/quickbook.exe" ; ``` -------------------------------- ### Throwing Exceptions with Stacktrace in C++ Source: https://www.boost.org/doc/libs/master/doc/html/stacktrace/getting_started This example demonstrates the usage of the `throw_with_trace` helper function. Instead of directly throwing `std::out_of_range` or `std::logic_error`, it uses `throw_with_trace` to automatically include the current call stack in the exception object. This aids in debugging by providing context. ```cpp if (i >= 4) throw_with_trace(std::out_of_range("'i' must be less than 4 in oops()")); if (i <= 0) throw_with_trace(std::logic_error("'i' must be greater than zero in oops()")); ``` -------------------------------- ### Creating Lists in Boost Documentation Source: https://www.boost.org/doc/libs/master/doc/html/quickbook/ref Provides examples for creating ordered and unordered lists using QuickBook syntax. This is useful for presenting sequential or non-sequential items clearly within documentation. ```QuickBook # one # two # three * one * two * three ``` -------------------------------- ### Enhance Assertions with Boost.Stacktrace Source: https://www.boost.org/doc/libs/master/doc/html/stacktrace/getting_started This example demonstrates how to customize assertion failure messages to include a call stack trace. It involves defining custom `boost::assertion_failed_msg` and `boost::assertion_failed` functions and ensuring `BOOST_ENABLE_ASSERT_DEBUG_HANDLER` is defined. This provides more context when assertions fail, aiding in debugging. ```cpp // BOOST_ENABLE_ASSERT_DEBUG_HANDLER is defined for the whole project #include // std::logic_error #include // std::cerr #include namespace boost { inline void assertion_failed_msg(char const* expr, char const* msg, char const* function, char const* /*file*/, long /*line*/) { std::cerr << "Expression '" << expr << "' is false in function '" << function << "': " << (msg ? msg : "<...>") << ".\n" << "Backtrace:\n" << boost::stacktrace::stacktrace() << '\n'; std::abort(); } inline void assertion_failed(char const* expr, char const* function, char const* file, long line) { ::boost::assertion_failed_msg(expr, 0 /*nullptr*/, function, file, line); } } // namespace boost ``` -------------------------------- ### Load Plugins and Handle Symbol Shadowing with Boost.DLL (C++) Source: https://www.boost.org/doc/libs/master/doc/html/boost_dll/tutorial Demonstrates loading multiple plugins using Boost.DLL and addresses symbol shadowing issues. It shows how to load plugins into an executable and collect them, highlighting the problem of duplicate symbols and the solution using the '-fvisibility=hidden' flag for POSIX systems. ```cpp #include #include #include namespace my_namespace { struct on_unload { using callback_t = std::function ; using this_type = on_unload; ~on_unload() { for (std::size_t i = 0; i < callbacks_.size(); ++i) { callback_t& function = callbacks_[i]; function(); // calling the callback } } // not thread safe static void add(const callback_t& function) { static this_type instance; instance.callbacks_.push_back(function); } private: std::vector callbacks_; on_unload() {} // prohibit construction outside of the `add` function }; // Exporting the static "add" function with name "on_unload" BOOST_DLL_ALIAS(my_namespace::on_unload::add, on_unload) } // namespace my_namespace ``` ```cpp #include #include #include using callback_t = std::function ; void print_unloaded() { std::cout << "unloaded" << std::endl; } int main(int argc, char* argv[]) { // argv[1] contains full path to our plugin library boost::dll::fs::path shared_library_path = argv[1]; // loading library and getting a function from it std::function on_unload = boost::dll::import_alias( shared_library_path, "on_unload" ); on_unload(&print_unloaded); // adding a callback std::cout << "Before library unload." << std::endl; // Releasing last reference to the library, so that it gets unloaded on_unload = {}; std::cout << "After library unload." << std::endl; } ``` ```cpp namespace dll = boost::dll; class plugins_collector { // Name => plugin using plugins_t = std::map; boost::dll::fs::path plugins_directory_; plugins_t plugins_; // loads all plugins in plugins_directory_ void load_all(); // Gets `my_plugin_api` instance using "create_plugin" or "plugin" imports, // stores plugin with its name in the `plugins_` map. void insert_plugin(dll::shared_library&& lib); public: plugins_collector(const boost::dll::fs::path& plugins_directory) : plugins_directory_(plugins_directory) { load_all(); } void print_plugins() const; std::size_t count() const; // ... }; ``` ```cpp int main(int argc, char* argv[]) { plugins_collector plugins(argv[1]); std::cout << "\n\nUnique plugins " << plugins.count() << ":\n"; plugins.print_plugins(); // ... } ``` -------------------------------- ### Measure function execution time using Boost.Chrono in C++ Source: https://www.boost.org/doc/libs/master/doc/html/chrono/users_guide This C++ example demonstrates how to measure the execution time of functions `g()` and `h()` using `boost::chrono::steady_clock`. It records the start time, calls the functions, calculates the elapsed duration in seconds, and prints the result. ```cpp void f() { boost::chrono::steady_clock::time_point start = boost::chrono::steady_clock::now(); g(); h(); duration sec = boost::chrono::steady_clock::now() - start; cout << "f() took " << sec.count() << " seconds\n"; } ``` -------------------------------- ### Configure Boost.Build with Docbook XSL and XML Source: https://www.boost.org/doc/libs/master/doc/html/quickbook/install This snippet configures Boost.Build to use a specific version of Docbook XML and Docbook XSL. It requires the paths to the unpacked Docbook XML and XSL files. This setup enables the processing of Docbook-formatted documentation. ```jam using xsltproc ; using boostbook : "/usr/local/share/xsl/docbook" : "/usr/local/share/xml/docbook/4.2" ; ``` -------------------------------- ### Implement a polling delay using Boost.Chrono in C++ Source: https://www.boost.org/doc/libs/master/doc/html/chrono/users_guide This C++ example shows a polling method to implement a delay of 5 seconds using `boost::chrono::steady_clock`. It checks the difference between the current time and a starting point against a specified delay duration in a loop. Note: this method is generally inefficient. ```cpp boost::chrono::steady_clock::time_point start= chrono::steady_clock::now(); boost::chrono::steady_clock::duration delay= chrono::seconds(5); while (boost::chrono::steady_clock::now() - start <= delay) {} ``` -------------------------------- ### Boost Proto _state Transform Example Source: https://www.boost.org/doc/libs/master/doc/html/boost/proto/_state An example demonstrating the usage of boost::proto::_state transform. It shows how to create a terminal and apply the _state transform to it, asserting the expected output. ```cpp proto::terminal::type i = {42}; char ch = proto::_state()(i, 'a'); assert( ch == 'a' ); ``` -------------------------------- ### Output from FoldToList Example - Text Source: https://www.boost.org/doc/libs/master/doc/html/proto/users_guide This text represents the output generated when the `FoldToList` transform is applied in the example. It shows the terminals being processed and outputted to `std::cout`. ```text terminal( ) terminal(3.14) terminal(1) ``` -------------------------------- ### Boost rolling_sum Header and Example Source: https://www.boost.org/doc/libs/master/doc/html/accumulators/user_s_guide Includes the header for the rolling_sum accumulator and provides an example. This accumulator calculates the sum of the last N samples within a rolling window. ```c++ #include accumulator_set > acc(tag::rolling_window::window_size = 3); BOOST_CHECK_EQUAL(0, rolling_sum(acc)); acc(1); BOOST_CHECK_EQUAL(1, rolling_sum(acc)); acc(2); BOOST_CHECK_EQUAL(3, rolling_sum(acc)); acc(3); BOOST_CHECK_EQUAL(6, rolling_sum(acc)); acc(4); BOOST_CHECK_EQUAL(9, rolling_sum(acc)); acc(5); BOOST_CHECK_EQUAL(12, rolling_sum(acc)); ``` -------------------------------- ### Boost rolling_count Header and Example Source: https://www.boost.org/doc/libs/master/doc/html/accumulators/user_s_guide Includes the header for the rolling_count accumulator and provides an example demonstrating its usage. The rolling_count tracks the number of elements currently within a rolling window. ```c++ #include accumulator_set > acc(tag::rolling_window::window_size = 3); BOOST_CHECK_EQUAL(0u, rolling_count(acc)); acc(1); BOOST_CHECK_EQUAL(1u, rolling_count(acc)); acc(1); BOOST_CHECK_EQUAL(2u, rolling_count(acc)); acc(1); BOOST_CHECK_EQUAL(3u, rolling_count(acc)); acc(1); BOOST_CHECK_EQUAL(3u, rolling_count(acc)); acc(1); BOOST_CHECK_EQUAL(3u, rolling_count(acc)); ``` -------------------------------- ### Define Command-Line Options with Boost.Options (C++) Source: https://www.boost.org/doc/libs/master/doc/html/program_options/tutorial Defines a set of command-line options for a hypothetical compiler, including help, optimization level (integer with default), include paths (vector of strings), and input files (vector of strings). It utilizes `boost::program_options` for this purpose. ```cpp int opt; po::options_description desc("Allowed options"); desc.add_options() ("help", "produce help message") ("optimization", po::value(&opt)->default_value(10), "optimization level") ("include-path,I", po::value< vector >(), "include path") ("input-file", po::value< vector >(), "input file") ; ``` -------------------------------- ### Hello World Proto Expression Evaluation Source: https://www.boost.org/doc/libs/master/doc/html/proto/users_guide This C++ code snippet demonstrates how to use Proto to build an expression template for outputting text to std::cout. It defines a Proto terminal for std::cout, an evaluate function to process the expression tree, and a main function to execute the example. The program outputs 'hello, world'. ```cpp #include #include #include using namespace boost; proto::terminal< std::ostream & >::type cout_ = { std::cout }; template< typename Expr > void evaluate( Expr const & expr ) { proto::default_context ctx; proto::eval(expr, ctx); } int main() { evaluate( cout_ << "hello" << ',' << " world" ); return 0; } ``` -------------------------------- ### Get Previous Node in Circular Slist (with start node) Source: https://www.boost.org/doc/libs/master/doc/html/doxygen/classboost_1_1intrusive_1_1circular__slist__algorithms Returns the node preceding 'this_node' in the circular list, starting the search from 'prev_init_node'. The first node checked is the one after 'prev_init_node'. Requires both nodes to be in the same list. Complexity is linear to the number of elements between the start and target nodes, and throws no exceptions. ```cpp static node_ptr get_previous_node(node_ptr prev_init_node, node_ptr this_node) noexcept; ``` -------------------------------- ### Boost date_time acst_dst_trait: Get Start Month of DST Source: https://www.boost.org/doc/libs/master/doc/html/doxygen/date_time_reference/structboost_1_1date__time_1_1acst__dst__trait Returns the month in which daylight saving time starts for a specified year. This is a utility function provided by the `acst_dst_trait` for DST calculations. ```cpp static month_type start_month(year_type); ``` -------------------------------- ### Configure xsltproc and boostbook (Windows) Source: https://www.boost.org/doc/libs/master/doc/html/quickbook/install Configures the Boost Build system to use a specific xsltproc executable and a custom location for Docbook XSL and XML on Windows. This allows BoostBook to process documentation files. ```jam using xsltproc : "C:/Users/example/Documents/boost/xml/bin/xsltproc.exe" ; using boostbook : "C:/Users/example/Documents/boost/xml/docbook-xsl" : "C:/Users/example/Documents/boost/xml/docbook-xml" ; ``` -------------------------------- ### Configure Boost.Build with a System-Wide Quickbook Binary Source: https://www.boost.org/doc/libs/master/doc/html/quickbook/install This snippet configures Boost.Build to use a pre-compiled, system-wide Quickbook executable. It requires the path to the Quickbook binary. This allows Boost.Build to find and use Quickbook without rebuilding it each time. ```jam using quickbook : "/usr/local/bin/quickbook" ; ``` -------------------------------- ### Space Parser Example Source: https://www.boost.org/doc/libs/master/doc/html/metaparse/reference Provides an example demonstrating the `space` parser's functionality. It verifies that `space` consumes a leading whitespace and returns an error if the input does not start with whitespace. Uses `BOOST_METAPARSE_STRING`, `get_remaining`, and `is_error`. ```cpp #include #include #include #include #include #include using namespace boost::metaparse; static_assert( std::is_same< BOOST_METAPARSE_STRING(" foo"), get_remaining>::type >::type::value, "it should consume the first space of the input" ); static_assert( is_error>::type::value, "it should return an error when the input does not begin with a whitespace" ); ```