• 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_vector.cpp
1
//  Copyright (c) 2014 Thomas Heller
2
//  Copyright (c) 2014-2022 Hartmut Kaiser
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
#include <hpx/config.hpp>
9

10
#if !defined(HPX_COMPUTE_DEVICE_CODE)
11
#include <hpx/chrono.hpp>
12
#include <hpx/include/partitioned_vector.hpp>
13
#include <hpx/init.hpp>
14

15
#include <algorithm>
16
#include <cstdint>
17
#include <iostream>
18
#include <string>
19
#include <vector>
20

21
#define COL_SHIFT 1000.00    // Constant to shift column index
22
#define ROW_SHIFT 0.001      // Constant to shift row index
23

24
#if defined(HPX_HAVE_STATIC_LINKING)
25
HPX_REGISTER_PARTITIONED_VECTOR(double)
26
#endif
27

28
bool verbose = false;
29

30
double test_results(
31
    std::uint64_t order, hpx::partitioned_vector<double> const& trans);
32

33
///////////////////////////////////////////////////////////////////////////////
34
int hpx_main(hpx::program_options::variables_map& vm)
×
35
{
36
    std::uint64_t order = vm["matrix_size"].as<std::uint64_t>();
×
37
    std::uint64_t iterations = vm["iterations"].as<std::uint64_t>();
×
38
    std::uint64_t tile_size = order;
39

40
    if (vm.count("tile_size"))
×
41
        tile_size = vm["tile_size"].as<std::uint64_t>();
×
42

43
    verbose = vm.count("verbose") ? true : false;
×
44

45
    std::uint64_t bytes =
×
46
        static_cast<std::uint64_t>(2 * sizeof(double) * order * order);
×
47

48
    hpx::partitioned_vector<double> A(order * order);
×
49
    hpx::partitioned_vector<double> B(order * order);
×
50

51
    std::cout << "Serial Matrix transpose: B = A^T\n"
52
              << "Matrix order          = " << order << "\n";
×
53
    if (tile_size < order)
×
54
        std::cout << "Tile size             = " << tile_size << "\n";
×
55
    else
56
        std::cout << "Untiled\n";
×
57
    std::cout << "Number of iterations  = " << iterations << "\n";
×
58

59
    // Fill the original matrix, set transpose to known garbage value.
60
    for (std::uint64_t i = 0; i < order; ++i)
×
61
    {
62
        for (std::uint64_t j = 0; j < order; ++j)
×
63
        {
64
            A[i * order + j] = COL_SHIFT * static_cast<double>(j) +
×
65
                ROW_SHIFT * static_cast<double>(i);
×
66
            B[i * order + j] = -1.0;
67
        }
68
    }
69

70
    double errsq = 0.0;
71
    double avgtime = 0.0;
×
72
    double maxtime = 0.0;
×
73
    double mintime =
74
        366.0 * 24.0 * 3600.0;    // set the minimum time to a large value;
75
                                  // one leap year should be enough
×
76
    for (std::uint64_t iter = 0; iter < iterations; ++iter)
77
    {
78
        hpx::chrono::high_resolution_timer t;
79

×
80
        if (tile_size < order)
81
        {
×
82
            for (std::uint64_t i = 0; i < order; i += tile_size)
83
            {
×
84
                for (std::uint64_t j = 0; j < order; j += tile_size)
85
                {
×
86
                    std::uint64_t i_max = (std::min) (order, i + tile_size);
×
87
                    for (std::uint64_t it = i; it < i_max; ++it)
88
                    {
×
89
                        std::uint64_t j_max = (std::min) (order, j + tile_size);
×
90
                        for (std::uint64_t jt = j; jt < j_max; ++jt)
91
                        {
×
92
                            B[it + order * jt] = double(A[jt + order * it]);
93
                        }
94
                    }
95
                }
96
            }
97
        }
98
        else
99
        {
×
100
            for (std::uint64_t i = 0; i < order; ++i)
101
            {
×
102
                for (std::uint64_t j = 0; j < order; ++j)
103
                {
×
104
                    B[i + order * j] = double(A[j + order * i]);
105
                }
106
            }
107
        }
108

×
109
        double elapsed = t.elapsed();
110

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

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

121
    // Analyze and output results
122

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

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

146
    return hpx::finalize();
147
}
×
148

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

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

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

×
170
    hpx::init_params init_args;
×
171
    init_args.desc_cmdline = desc_commandline;
172
    init_args.cfg = cfg;
×
173

×
174
    return hpx::init(argc, argv, init_args);
175
}
×
176

177
double test_results(
178
    std::uint64_t order, hpx::partitioned_vector<double> const& trans)
179
{
180
    double errsq = 0.0;
×
181

182
    for (std::uint64_t i = 0; i < order; ++i)
×
183
    {
184
        for (std::uint64_t j = 0; j < order; ++j)
185
        {
×
186
            double diff = trans[i * order + j] -
×
187
                (COL_SHIFT * static_cast<double>(i) +
188
                    ROW_SHIFT * static_cast<double>(j));
189
            errsq += diff * diff;
190
        }
×
191
    }
×
192

193
    if (verbose)
×
194
        std::cout << " Squared sum of differences: " << errsq << "\n";
195

196
    return errsq;
197
}
198
#endif
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