Skip to content

Commit c17a4f7

Browse files
author
StellarBot
committed
Updating Sphinx docs - branch (singlehtml)
1 parent 7b4f575 commit c17a4f7

File tree

178 files changed

+133290
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

178 files changed

+133290
-0
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
// Copyright (c) 2014 Hartmut Kaiser
2+
// Copyright (c) 2014 Patricia Grubel
3+
//
4+
// SPDX-License-Identifier: BSL-1.0
5+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
6+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7+
8+
// This is the first in a series of examples demonstrating the development of a
9+
// fully distributed solver for a simple 1D heat distribution problem.
10+
//
11+
// This example provides a serial base line implementation. No parallelization
12+
// is performed.
13+
14+
#include <hpx/chrono.hpp>
15+
#include <hpx/init.hpp>
16+
17+
#include <cstddef>
18+
#include <cstdint>
19+
#include <iostream>
20+
#include <vector>
21+
22+
#include "print_time_results.hpp"
23+
24+
///////////////////////////////////////////////////////////////////////////////
25+
// Command-line variables
26+
bool header = true; // print csv heading
27+
double k = 0.5; // heat transfer coefficient
28+
double dt = 1.; // time step
29+
double dx = 1.; // grid spacing
30+
31+
///////////////////////////////////////////////////////////////////////////////
32+
//[stepper_1
33+
struct stepper
34+
{
35+
// Our partition type
36+
typedef double partition;
37+
38+
// Our data for one time step
39+
typedef std::vector<partition> space;
40+
41+
// Our operator
42+
static double heat(double left, double middle, double right)
43+
{
44+
return middle + (k * dt / (dx * dx)) * (left - 2 * middle + right);
45+
}
46+
47+
// do all the work on 'nx' data points for 'nt' time steps
48+
space do_work(std::size_t nx, std::size_t nt)
49+
{
50+
// U[t][i] is the state of position i at time t.
51+
std::vector<space> U(2);
52+
for (space& s : U)
53+
s.resize(nx);
54+
55+
// Initial conditions: f(0, i) = i
56+
for (std::size_t i = 0; i != nx; ++i)
57+
U[0][i] = double(i);
58+
59+
// Actual time step loop
60+
for (std::size_t t = 0; t != nt; ++t)
61+
{
62+
space const& current = U[t % 2];
63+
space& next = U[(t + 1) % 2];
64+
65+
next[0] = heat(current[nx - 1], current[0], current[1]);
66+
67+
for (std::size_t i = 1; i != nx - 1; ++i)
68+
next[i] = heat(current[i - 1], current[i], current[i + 1]);
69+
70+
next[nx - 1] = heat(current[nx - 2], current[nx - 1], current[0]);
71+
}
72+
73+
// Return the solution at time-step 'nt'.
74+
return U[nt % 2];
75+
}
76+
};
77+
//]
78+
///////////////////////////////////////////////////////////////////////////////
79+
int hpx_main(hpx::program_options::variables_map& vm)
80+
{
81+
std::uint64_t nx =
82+
vm["nx"].as<std::uint64_t>(); // Number of grid points.
83+
std::uint64_t nt = vm["nt"].as<std::uint64_t>(); // Number of steps.
84+
85+
if (vm.count("no-header"))
86+
header = false;
87+
88+
// Create the stepper object
89+
stepper step;
90+
91+
// Measure execution time.
92+
std::uint64_t t = hpx::chrono::high_resolution_clock::now();
93+
94+
// Execute nt time steps on nx grid points.
95+
stepper::space solution = step.do_work(nx, nt);
96+
97+
// Print the final solution
98+
if (vm.count("results"))
99+
{
100+
for (std::size_t i = 0; i != nx; ++i)
101+
std::cout << "U[" << i << "] = " << solution[i] << std::endl;
102+
}
103+
104+
std::uint64_t elapsed = hpx::chrono::high_resolution_clock::now() - t;
105+
106+
std::uint64_t const os_thread_count = hpx::get_os_thread_count();
107+
print_time_results(os_thread_count, elapsed, nx, nt, header);
108+
109+
return hpx::local::finalize();
110+
}
111+
112+
int main(int argc, char* argv[])
113+
{
114+
namespace po = hpx::program_options;
115+
116+
// clang-format off
117+
po::options_description desc_commandline;
118+
desc_commandline.add_options()
119+
("results", "print generated results (default: false)")
120+
("nx", po::value<std::uint64_t>()->default_value(100),
121+
"Local x dimension")
122+
("nt", po::value<std::uint64_t>()->default_value(45),
123+
"Number of time steps")
124+
("k", po::value<double>(&k)->default_value(0.5),
125+
"Heat transfer coefficient (default: 0.5)")
126+
("dt", po::value<double>(&dt)->default_value(1.0),
127+
"Timestep unit (default: 1.0[s])")
128+
("dx", po::value<double>(&dx)->default_value(1.0),
129+
"Local x dimension")
130+
( "no-header", "do not print out the csv header row")
131+
;
132+
// clang-format on
133+
134+
// Initialize and run HPX
135+
hpx::local::init_params init_args;
136+
init_args.desc_cmdline = desc_commandline;
137+
138+
return hpx::local::init(hpx_main, argc, argv, init_args);
139+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/////////////////////////// Interest Calculator ///////////////////////////////
2+
// Copyright (c) 2012 Adrian Serio
3+
//
4+
// SPDX-License-Identifier: BSL-1.0
5+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
6+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7+
//
8+
// Purpose: Calculates compound interest while using dataflow objects.
9+
//
10+
// In this example, you supply the program with the principal [$], the interest
11+
// rate [%], the length of the compound period [months], and the length of time
12+
// the money is invested [months]. The program will calculate the new total
13+
// amount of money you have and the amount of interest made. For example if
14+
// you have $100, an interest rate of 5%, a compound period of 6 months and
15+
// you leave your money in that account for 36 months you will end up with
16+
// $134.01 and will have made $34.01 in interest.
17+
///////////////////////////////////////////////////////////////////////////////
18+
19+
#include <hpx/hpx.hpp>
20+
#include <hpx/hpx_init.hpp>
21+
22+
#include <hpx/async_local/dataflow.hpp>
23+
#include <hpx/modules/actions_base.hpp>
24+
25+
#include <iostream>
26+
27+
using hpx::program_options::options_description;
28+
using hpx::program_options::value;
29+
using hpx::program_options::variables_map;
30+
31+
///////////////////////////////////////////////////////////////////////////////
32+
//[interest_calc_add_action
33+
// Calculate interest for one period
34+
double calc(double principal, double rate)
35+
{
36+
return principal * rate;
37+
}
38+
39+
///////////////////////////////////////////////////////////////////////////////
40+
// Add the amount made to the principal
41+
double add(double principal, double interest)
42+
{
43+
return principal + interest;
44+
}
45+
//]
46+
47+
///////////////////////////////////////////////////////////////////////////////
48+
//[interest_hpx_main
49+
int hpx_main(variables_map& vm)
50+
{
51+
{
52+
using hpx::dataflow;
53+
using hpx::make_ready_future;
54+
using hpx::shared_future;
55+
using hpx::unwrapping;
56+
hpx::id_type here = hpx::find_here();
57+
58+
double init_principal =
59+
vm["principal"].as<double>(); //Initial principal
60+
double init_rate = vm["rate"].as<double>(); //Interest rate
61+
int cp = vm["cp"].as<int>(); //Length of a compound period
62+
int t = vm["time"].as<int>(); //Length of time money is invested
63+
64+
init_rate /= 100; //Rate is a % and must be converted
65+
t /= cp; //Determine how many times to iterate interest calculation:
66+
//How many full compound periods can fit in the time invested
67+
68+
// In non-dataflow terms the implemented algorithm would look like:
69+
//
70+
// int t = 5; // number of time periods to use
71+
// double principal = init_principal;
72+
// double rate = init_rate;
73+
//
74+
// for (int i = 0; i < t; ++i)
75+
// {
76+
// double interest = calc(principal, rate);
77+
// principal = add(principal, interest);
78+
// }
79+
//
80+
// Please note the similarity with the code below!
81+
82+
shared_future<double> principal = make_ready_future(init_principal);
83+
shared_future<double> rate = make_ready_future(init_rate);
84+
85+
for (int i = 0; i < t; ++i)
86+
{
87+
shared_future<double> interest =
88+
dataflow(unwrapping(calc), principal, rate);
89+
principal = dataflow(unwrapping(add), principal, interest);
90+
}
91+
92+
// wait for the dataflow execution graph to be finished calculating our
93+
// overall interest
94+
double result = principal.get();
95+
96+
std::cout << "Final amount: " << result << std::endl;
97+
std::cout << "Amount made: " << result - init_principal << std::endl;
98+
}
99+
100+
return hpx::finalize();
101+
}
102+
//]
103+
104+
///////////////////////////////////////////////////////////////////////////////
105+
//[interest_main
106+
int main(int argc, char** argv)
107+
{
108+
options_description cmdline("Usage: " HPX_APPLICATION_STRING " [options]");
109+
110+
cmdline.add_options()("principal", value<double>()->default_value(1000),
111+
"The principal [$]")("rate", value<double>()->default_value(7),
112+
"The interest rate [%]")("cp", value<int>()->default_value(12),
113+
"The compound period [months]")("time",
114+
value<int>()->default_value(12 * 30),
115+
"The time money is invested [months]");
116+
117+
hpx::init_params init_args;
118+
init_args.desc_cmdline = cmdline;
119+
120+
return hpx::init(argc, argv, init_args);
121+
}
122+
//]

0 commit comments

Comments
 (0)