• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

STEllAR-GROUP / hpx / #847

07 Dec 2022 07:15PM UTC coverage: 85.835% (-0.6%) from 86.482%
#847

push

StellarBot
Merge #6095

6095: Replacing facilities from Boost.Range r=hkaiser a=hkaiser

Working towards #3440 


Co-authored-by: Hartmut Kaiser <hartmut.kaiser@gmail.com>

43 of 43 new or added lines in 21 files covered. (100.0%)

171460 of 199755 relevant lines covered (85.84%)

1820288.13 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.58
/examples/transpose/transpose_smp.cpp
1
//  Copyright (c) 2014 Thomas Heller
2
//
3
//  SPDX-License-Identifier: BSL-1.0
4
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
5
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6

7
#include <hpx/local/algorithm.hpp>
8
#include <hpx/local/init.hpp>
9
#include <hpx/local/numeric.hpp>
10
#include <hpx/modules/iterator_support.hpp>
11

12
#include <algorithm>
13
#include <cstdint>
14
#include <exception>
15
#include <iostream>
16
#include <vector>
17

18
#define COL_SHIFT 1000.00    // Constant to shift column index
19
#define ROW_SHIFT 0.001      // Constant to shift row index
20

21
bool verbose = false;
22

23
double test_results(std::uint64_t order, std::vector<double> const& trans);
24

25
///////////////////////////////////////////////////////////////////////////////
26
int hpx_main(hpx::program_options::variables_map& vm)
1✔
27
{
28
    std::uint64_t order = vm["matrix_size"].as<std::uint64_t>();
1✔
29
    std::uint64_t iterations = vm["iterations"].as<std::uint64_t>();
1✔
30
    std::uint64_t tile_size = order;
1✔
31

32
    if (vm.count("tile_size"))
1✔
33
        tile_size = vm["tile_size"].as<std::uint64_t>();
×
34

35
    verbose = vm.count("verbose") ? true : false;
1✔
36

37
    std::uint64_t bytes =
1✔
38
        static_cast<std::uint64_t>(2.0 * sizeof(double) * order * order);
1✔
39

40
    std::vector<double> A(order * order);
1✔
41
    std::vector<double> B(order * order);
1✔
42

43
    std::cout << "Serial Matrix transpose: B = A^T\n"
1✔
44
              << "Matrix order          = " << order << "\n";
1✔
45
    if (tile_size < order)
1✔
46
        std::cout << "Tile size             = " << tile_size << "\n";
×
47
    else
48
        std::cout << "Untiled\n";
1✔
49
    std::cout << "Number of iterations  = " << iterations << "\n";
1✔
50

51
    using hpx::execution::par;
52
    using hpx::experimental::for_loop_strided;
53
    using hpx::ranges::for_each;
54

55
    const std::uint64_t start = 0;
1✔
56

57
    // Fill the original matrix, set transpose to known garbage value.
58
    auto range = hpx::util::counting_shape(order);
1✔
59
    // parallel for
60
    for_each(par, range, [&](std::uint64_t i) {
1,022✔
61
        for (std::uint64_t j = 0; j < order; ++j)
361,478✔
62
        {
63
            A[i * order + j] = COL_SHIFT * j + ROW_SHIFT * i;
360,457✔
64
            B[i * order + j] = -1.0;
360,457✔
65
        }
360,457✔
66
    });
1,018✔
67

68
    double errsq = 0.0;
1✔
69
    double avgtime = 0.0;
1✔
70
    double maxtime = 0.0;
1✔
71
    double mintime =
1✔
72
        366.0 * 24.0 * 3600.0;    // set the minimum time to a large value;
73
                                  // one leap year should be enough
74
    for (std::uint64_t iter = 0; iter < iterations; ++iter)
11✔
75
    {
76
        hpx::chrono::high_resolution_timer t;
10✔
77
        if (tile_size < order)
10✔
78
        {
79
            // parallel for
80
            for_loop_strided(
×
81
                par, start, order + tile_size, tile_size, [&](std::uint64_t i) {
×
82
                    for (std::uint64_t j = 0; j < order; j += tile_size)
×
83
                    {
84
                        std::uint64_t i_max = (std::min)(order, i + tile_size);
×
85
                        std::uint64_t j_max = (std::min)(order, j + tile_size);
×
86

87
                        for (std::uint64_t it = i; it < i_max; ++it)
×
88
                        {
89
                            for (std::uint64_t jt = j; jt < j_max; ++jt)
×
90
                            {
91
                                B[it + order * jt] = A[jt + order * it];
×
92
                            }
×
93
                        }
×
94
                    }
×
95
                });
×
96
        }
×
97
        else
98
        {
99
            // parallel for
100
            auto range = hpx::util::counting_shape(order);
10✔
101
            for_each(par, range, [&](std::uint64_t i) {
10,235✔
102
                for (std::uint64_t j = 0; j < order; ++j)
3,230,622✔
103
                {
104
                    B[i + order * j] = A[j + order * i];
3,220,397✔
105
                }
3,220,397✔
106
            });
10,209✔
107
        }
108

109
        double elapsed = t.elapsed();
10✔
110

111
        if (iter > 0 || iterations == 1)    // Skip the first iteration
10✔
112
        {
113
            avgtime = avgtime + elapsed;
9✔
114
            maxtime = (std::max)(maxtime, elapsed);
9✔
115
            mintime = (std::min)(mintime, elapsed);
9✔
116
        }
9✔
117

118
        errsq += test_results(order, B);
10✔
119
    }    // end of iter loop
10✔
120

121
    // Analyze and output results
122

123
    double epsilon = 1.e-8;
1✔
124
    if (errsq < epsilon)
1✔
125
    {
126
        std::cout << "Solution validates\n";
1✔
127
        avgtime = avgtime /
1✔
128
            static_cast<double>(
129
                (std::max)(iterations - 1, static_cast<std::uint64_t>(1)));
1✔
130
        std::cout << "Rate (MB/s): " << 1.e-6 * bytes / mintime << ", "
1✔
131
                  << "Avg time (s): " << avgtime << ", "
1✔
132
                  << "Min time (s): " << mintime << ", "
1✔
133
                  << "Max time (s): " << maxtime << "\n";
1✔
134

135
        if (verbose)
1✔
136
            std::cout << "Squared errors: " << errsq << "\n";
×
137
    }
1✔
138
    else
139
    {
140
        std::cout << "ERROR: Aggregate squared error " << errsq
×
141
                  << " exceeds threshold " << epsilon << "\n";
×
142
        std::terminate();
×
143
    }
144

145
    return hpx::local::finalize();
1✔
146
}
1✔
147

148
int main(int argc, char* argv[])
1✔
149
{
150
    using namespace hpx::program_options;
151

152
    options_description desc_commandline;
1✔
153
    // clang-format off
154
    desc_commandline.add_options()
4✔
155
        ("matrix_size", value<std::uint64_t>()->default_value(1024),
1✔
156
         "Matrix Size")
157
        ("iterations", value<std::uint64_t>()->default_value(10),
1✔
158
         "# iterations")
159
        ("tile_size", value<std::uint64_t>(),
1✔
160
         "Number of tiles to divide the individual matrix blocks for improved "
161
         "cache and TLB performance")
162
        ( "verbose", "Verbose output")
163
    ;
164
    // clang-format on
165

166
    hpx::local::init_params init_args;
1✔
167
    init_args.desc_cmdline = desc_commandline;
1✔
168

169
    return hpx::local::init(hpx_main, argc, argv, init_args);
1✔
170
}
1✔
171

172
double test_results(std::uint64_t order, std::vector<double> const& trans)
10✔
173
{
174
    using hpx::transform_reduce;
175
    using hpx::execution::par;
176

177
    const std::uint64_t start = 0;
10✔
178

179
    auto range = hpx::util::counting_shape(start, order);
10✔
180
    // parallel reduce
181
    double errsq = transform_reduce(
10✔
182
        par, std::begin(range), std::end(range), 0.0,
10✔
183
        [](double lhs, double rhs) { return lhs + rhs; },
10,204✔
184
        [&](std::uint64_t i) -> double {
10,229✔
185
            double errsq = 0.0;
10,219✔
186
            for (std::uint64_t j = 0; j < order; ++j)
3,395,940✔
187
            {
188
                double diff =
3,385,721✔
189
                    trans[i * order + j] - (COL_SHIFT * i + ROW_SHIFT * j);
3,385,721✔
190
                errsq += diff * diff;
3,385,721✔
191
            }
3,385,721✔
192
            return errsq;
10,192✔
193
        });
194

195
    if (verbose)
10✔
196
        std::cout << " Squared sum of differences: " << errsq << "\n";
×
197

198
    return errsq;
10✔
199
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc