Skip to content

Commit

Permalink
Updating Sphinx docs - branch (singlehtml)
Browse files Browse the repository at this point in the history
  • Loading branch information
StellarBot committed Nov 13, 2024
1 parent ef9d6f1 commit 0cb9b0d
Show file tree
Hide file tree
Showing 178 changed files with 133,143 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
// Copyright (c) 2014 Hartmut Kaiser
// Copyright (c) 2014 Patricia Grubel
//
// SPDX-License-Identifier: BSL-1.0
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

// This is the first in a series of examples demonstrating the development of a
// fully distributed solver for a simple 1D heat distribution problem.
//
// This example provides a serial base line implementation. No parallelization
// is performed.

#include <hpx/chrono.hpp>
#include <hpx/init.hpp>

#include <cstddef>
#include <cstdint>
#include <iostream>
#include <vector>

#include "print_time_results.hpp"

///////////////////////////////////////////////////////////////////////////////
// Command-line variables
bool header = true; // print csv heading
double k = 0.5; // heat transfer coefficient
double dt = 1.; // time step
double dx = 1.; // grid spacing

///////////////////////////////////////////////////////////////////////////////
//[stepper_1
struct stepper
{
// Our partition type
typedef double partition;

// Our data for one time step
typedef std::vector<partition> space;

// Our operator
static double heat(double left, double middle, double right)
{
return middle + (k * dt / (dx * dx)) * (left - 2 * middle + right);
}

// do all the work on 'nx' data points for 'nt' time steps
space do_work(std::size_t nx, std::size_t nt)
{
// U[t][i] is the state of position i at time t.
std::vector<space> U(2);
for (space& s : U)
s.resize(nx);

// Initial conditions: f(0, i) = i
for (std::size_t i = 0; i != nx; ++i)
U[0][i] = double(i);

// Actual time step loop
for (std::size_t t = 0; t != nt; ++t)
{
space const& current = U[t % 2];
space& next = U[(t + 1) % 2];

next[0] = heat(current[nx - 1], current[0], current[1]);

for (std::size_t i = 1; i != nx - 1; ++i)
next[i] = heat(current[i - 1], current[i], current[i + 1]);

next[nx - 1] = heat(current[nx - 2], current[nx - 1], current[0]);
}

// Return the solution at time-step 'nt'.
return U[nt % 2];
}
};
//]
///////////////////////////////////////////////////////////////////////////////
int hpx_main(hpx::program_options::variables_map& vm)
{
std::uint64_t nx =
vm["nx"].as<std::uint64_t>(); // Number of grid points.
std::uint64_t nt = vm["nt"].as<std::uint64_t>(); // Number of steps.

if (vm.count("no-header"))
header = false;

// Create the stepper object
stepper step;

// Measure execution time.
std::uint64_t t = hpx::chrono::high_resolution_clock::now();

// Execute nt time steps on nx grid points.
stepper::space solution = step.do_work(nx, nt);

// Print the final solution
if (vm.count("results"))
{
for (std::size_t i = 0; i != nx; ++i)
std::cout << "U[" << i << "] = " << solution[i] << std::endl;
}

std::uint64_t elapsed = hpx::chrono::high_resolution_clock::now() - t;

std::uint64_t const os_thread_count = hpx::get_os_thread_count();
print_time_results(os_thread_count, elapsed, nx, nt, header);

return hpx::local::finalize();
}

int main(int argc, char* argv[])
{
namespace po = hpx::program_options;

// clang-format off
po::options_description desc_commandline;
desc_commandline.add_options()
("results", "print generated results (default: false)")
("nx", po::value<std::uint64_t>()->default_value(100),
"Local x dimension")
("nt", po::value<std::uint64_t>()->default_value(45),
"Number of time steps")
("k", po::value<double>(&k)->default_value(0.5),
"Heat transfer coefficient (default: 0.5)")
("dt", po::value<double>(&dt)->default_value(1.0),
"Timestep unit (default: 1.0[s])")
("dx", po::value<double>(&dx)->default_value(1.0),
"Local x dimension")
( "no-header", "do not print out the csv header row")
;
// clang-format on

// Initialize and run HPX
hpx::local::init_params init_args;
init_args.desc_cmdline = desc_commandline;

return hpx::local::init(hpx_main, argc, argv, init_args);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/////////////////////////// Interest Calculator ///////////////////////////////
// Copyright (c) 2012 Adrian Serio
//
// SPDX-License-Identifier: BSL-1.0
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Purpose: Calculates compound interest while using dataflow objects.
//
// In this example, you supply the program with the principal [$], the interest
// rate [%], the length of the compound period [months], and the length of time
// the money is invested [months]. The program will calculate the new total
// amount of money you have and the amount of interest made. For example if
// you have $100, an interest rate of 5%, a compound period of 6 months and
// you leave your money in that account for 36 months you will end up with
// $134.01 and will have made $34.01 in interest.
///////////////////////////////////////////////////////////////////////////////

#include <hpx/hpx.hpp>
#include <hpx/hpx_init.hpp>

#include <hpx/async_local/dataflow.hpp>
#include <hpx/modules/actions_base.hpp>

#include <iostream>

using hpx::program_options::options_description;
using hpx::program_options::value;
using hpx::program_options::variables_map;

///////////////////////////////////////////////////////////////////////////////
//[interest_calc_add_action
// Calculate interest for one period
double calc(double principal, double rate)
{
return principal * rate;
}

///////////////////////////////////////////////////////////////////////////////
// Add the amount made to the principal
double add(double principal, double interest)
{
return principal + interest;
}
//]

///////////////////////////////////////////////////////////////////////////////
//[interest_hpx_main
int hpx_main(variables_map& vm)
{
{
using hpx::dataflow;
using hpx::make_ready_future;
using hpx::shared_future;
using hpx::unwrapping;
hpx::id_type here = hpx::find_here();

double init_principal =
vm["principal"].as<double>(); //Initial principal
double init_rate = vm["rate"].as<double>(); //Interest rate
int cp = vm["cp"].as<int>(); //Length of a compound period
int t = vm["time"].as<int>(); //Length of time money is invested

init_rate /= 100; //Rate is a % and must be converted
t /= cp; //Determine how many times to iterate interest calculation:
//How many full compound periods can fit in the time invested

// In non-dataflow terms the implemented algorithm would look like:
//
// int t = 5; // number of time periods to use
// double principal = init_principal;
// double rate = init_rate;
//
// for (int i = 0; i < t; ++i)
// {
// double interest = calc(principal, rate);
// principal = add(principal, interest);
// }
//
// Please note the similarity with the code below!

shared_future<double> principal = make_ready_future(init_principal);
shared_future<double> rate = make_ready_future(init_rate);

for (int i = 0; i < t; ++i)
{
shared_future<double> interest =
dataflow(unwrapping(calc), principal, rate);
principal = dataflow(unwrapping(add), principal, interest);
}

// wait for the dataflow execution graph to be finished calculating our
// overall interest
double result = principal.get();

std::cout << "Final amount: " << result << std::endl;
std::cout << "Amount made: " << result - init_principal << std::endl;
}

return hpx::finalize();
}
//]

///////////////////////////////////////////////////////////////////////////////
//[interest_main
int main(int argc, char** argv)
{
options_description cmdline("Usage: " HPX_APPLICATION_STRING " [options]");

cmdline.add_options()("principal", value<double>()->default_value(1000),
"The principal [$]")("rate", value<double>()->default_value(7),
"The interest rate [%]")("cp", value<int>()->default_value(12),
"The compound period [months]")("time",
value<int>()->default_value(12 * 30),
"The time money is invested [months]");

hpx::init_params init_args;
init_args.desc_cmdline = cmdline;

return hpx::init(argc, argv, init_args);
}
//]
Loading

0 comments on commit 0cb9b0d

Please sign in to comment.