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

STEllAR-GROUP / hpx / #882

31 Aug 2023 07:44PM UTC coverage: 41.798% (-44.7%) from 86.546%
#882

push

19442 of 46514 relevant lines covered (41.8%)

126375.38 hits per line

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

0.0
/examples/transpose/transpose_serial.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/chrono.hpp>
8
#include <hpx/init.hpp>
9

10
#include <algorithm>
11
#include <cstdint>
12
#include <exception>
13
#include <iostream>
14
#include <string>
15
#include <vector>
16

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

20
bool verbose = false;
21

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

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

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

34
    verbose = vm.count("verbose") ? true : false;
×
35

36
    std::uint64_t bytes =
×
37
        static_cast<std::uint64_t>(2 * sizeof(double) * order * order);
×
38

39
    std::vector<double> A(order * order);
×
40
    std::vector<double> B(order * order);
×
41

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

50
    // Fill the original matrix, set transpose to known garbage value.
51
    for (std::uint64_t i = 0; i < order; ++i)
×
52
    {
53
        for (std::uint64_t j = 0; j < order; ++j)
×
54
        {
55
            A[i * order + j] = COL_SHIFT * static_cast<double>(j) +
×
56
                ROW_SHIFT * static_cast<double>(i);
×
57
            B[i * order + j] = -1.0;
58
        }
59
    }
60

61
    double errsq = 0.0;
62
    double avgtime = 0.0;
×
63
    double maxtime = 0.0;
×
64
    double mintime =
65
        366.0 * 24.0 * 3600.0;    // set the minimum time to a large value;
66
                                  // one leap year should be enough
×
67
    for (std::uint64_t iter = 0; iter < iterations; ++iter)
68
    {
69
        hpx::chrono::high_resolution_timer t;
70

×
71
        if (tile_size < order)
72
        {
×
73
            for (std::uint64_t i = 0; i < order; i += tile_size)
74
            {
×
75
                for (std::uint64_t j = 0; j < order; j += tile_size)
76
                {
×
77
                    std::uint64_t i_max = (std::min) (order, i + tile_size);
×
78
                    for (std::uint64_t it = i; it < i_max; ++it)
79
                    {
×
80
                        std::uint64_t j_max = (std::min) (order, j + tile_size);
×
81
                        for (std::uint64_t jt = j; jt < j_max; ++jt)
82
                        {
×
83
                            B[it + order * jt] = A[jt + order * it];
84
                        }
85
                    }
86
                }
87
            }
88
        }
89
        else
90
        {
×
91
            for (std::uint64_t i = 0; i < order; ++i)
92
            {
×
93
                for (std::uint64_t j = 0; j < order; ++j)
94
                {
×
95
                    B[i + order * j] = A[j + order * i];
96
                }
97
            }
98
        }
99

×
100
        double elapsed = t.elapsed();
101

×
102
        if (iter > 0 || iterations == 1)    // Skip the first iteration
103
        {
×
104
            avgtime = avgtime + elapsed;
×
105
            maxtime = (std::max) (maxtime, elapsed);
×
106
            mintime = (std::min) (mintime, elapsed);
107
        }
108

×
109
        errsq += test_results(order, B);
110
    }    // end of iter loop
111

112
    // Analyze and output results
113

114
    double epsilon = 1.e-8;
×
115
    if (errsq < epsilon)
116
    {
×
117
        std::cout << "Solution validates\n";
×
118
        avgtime = avgtime /
×
119
            static_cast<double>(
×
120
                (std::max) (iterations - 1, static_cast<std::uint64_t>(1)));
×
121
        std::cout << "Rate (MB/s): "
122
                  << 1.e-6 * static_cast<double>(bytes) / mintime << ", "
123
                  << "Avg time (s): " << avgtime << ", "
×
124
                  << "Min time (s): " << mintime << ", "
125
                  << "Max time (s): " << maxtime << "\n";
×
126

×
127
        if (verbose)
128
            std::cout << "Squared errors: " << errsq << "\n";
129
    }
130
    else
131
    {
×
132
        std::cout << "ERROR: Aggregate squared error " << errsq
×
133
                  << " exceeds threshold " << epsilon << "\n";
134
        std::terminate();
135
    }
×
136

137
    return hpx::local::finalize();
138
}
×
139

140
int main(int argc, char* argv[])
141
{
142
    using namespace hpx::program_options;
×
143

144
    options_description desc_commandline;
×
145
    // clang-format off
×
146
    desc_commandline.add_options()
147
        ("matrix_size", value<std::uint64_t>()->default_value(1024),
×
148
         "Matrix Size")
149
        ("iterations", value<std::uint64_t>()->default_value(10),
×
150
         "# iterations")
151
        ("tile_size", value<std::uint64_t>(),
152
         "Number of tiles to divide the individual matrix blocks for improved "
×
153
         "cache and TLB performance")
154
        ( "verbose", "Verbose output")
155
    ;
156
    // clang-format on
157

158
    // Initialize and run HPX, this example is serial and therefore only needs
×
159
    // one thread, We just use hpx::init to parse our command line arguments
160
    std::vector<std::string> const cfg = {"hpx.os_threads!=1"};
×
161

×
162
    hpx::local::init_params init_args;
×
163
    init_args.desc_cmdline = desc_commandline;
164
    init_args.cfg = cfg;
×
165

×
166
    return hpx::local::init(hpx_main, argc, argv, init_args);
167
}
×
168

169
double test_results(std::uint64_t order, std::vector<double> const& trans)
170
{
171
    double errsq = 0.0;
×
172

173
    for (std::uint64_t i = 0; i < order; ++i)
×
174
    {
175
        for (std::uint64_t j = 0; j < order; ++j)
176
        {
×
177
            double diff = trans[i * order + j] -
×
178
                (COL_SHIFT * static_cast<double>(i) +
179
                    ROW_SHIFT * static_cast<double>(j));
180
            errsq += diff * diff;
181
        }
×
182
    }
×
183

184
    if (verbose)
×
185
        std::cout << " Squared sum of differences: " << errsq << "\n";
186

187
    return errsq;
188
}
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

© 2025 Coveralls, Inc