File: flops.hpp

package info (click to toggle)
rocsparse 5.3.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 13,540 kB
  • sloc: cpp: 157,515; f90: 9,304; sh: 1,689; python: 1,596; xml: 206; makefile: 26
file content (267 lines) | stat: -rw-r--r-- 8,326 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*! \file */
/* ************************************************************************
 * Copyright (C) 2019-2022 Advanced Micro Devices, Inc. All rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * ************************************************************************ */

/*! \file
 *  \brief flops.hpp provides floating point counts of Sparse Linear Algebra Subprograms
 *  of Level 1, 2 and 3.
 */

#pragma once
#ifndef FLOPS_HPP
#define FLOPS_HPP

#include <rocsparse.h>

/*
 * ===========================================================================
 *    level 1 SPARSE
 * ===========================================================================
 */
constexpr double axpyi_gflop_count(rocsparse_int nnz)
{
    return (2.0 * nnz) / 1e9;
}

template <typename I>
constexpr double axpby_gflop_count(I nnz)
{
    return (3.0 * nnz) / 1e9;
}

constexpr double doti_gflop_count(rocsparse_int nnz)
{
    return (2.0 * nnz) / 1e9;
}

template <typename I>
constexpr double roti_gflop_count(I nnz)
{
    return (6.0 * nnz) / 1e9;
}

/*
 * ===========================================================================
 *    level 2 SPARSE
 * ===========================================================================
 */
template <typename I, typename J>
constexpr double spmv_gflop_count(J M, I nnz, bool beta = false)
{
    return (2.0 * nnz + (beta ? M : 0)) / 1e9;
}

template <typename I, typename J>
constexpr double csrsv_gflop_count(J M, I nnz, rocsparse_diag_type diag)
{
    return (2.0 * nnz + M + (diag == rocsparse_diag_type_non_unit ? M : 0)) / 1e9;
}

template <typename I, typename J>
constexpr double spsv_gflop_count(J M, I nnz, rocsparse_diag_type diag)
{
    return csrsv_gflop_count(M, nnz, diag);
}

template <typename I>
constexpr double gemvi_gflop_count(I M, I nnz)
{
    return (M + 2.0 * nnz * M) / 1e9;
}

/*
 * ===========================================================================
 *    level 3 SPARSE
 * ===========================================================================
 */
constexpr double bsrmm_gflop_count(rocsparse_int N,
                                   rocsparse_int nnzb,
                                   rocsparse_int block_dim,
                                   rocsparse_int nnz_C,
                                   bool          beta = false)
{
    return (2.0 * nnzb * block_dim * block_dim * N + (beta ? nnz_C : 0)) / 1e9;
}

constexpr double gebsrmm_gflop_count(rocsparse_int N,
                                     rocsparse_int nnzb,
                                     rocsparse_int row_block_dim,
                                     rocsparse_int col_block_dim,
                                     rocsparse_int nnz_C,
                                     bool          beta = false)
{
    return (2.0 * nnzb * row_block_dim * col_block_dim * N + (beta ? nnz_C : 0)) / 1e9;
}

template <typename I, typename J>
constexpr double csrmm_gflop_count(J N, I nnz_A, I nnz_C, bool beta = false)
{
    // Multiplication by 2 comes from 1 addition and 1 multiplication in product. Multiplication
    // by alpha and beta not counted.
    return (2.0 * nnz_A * N + (beta ? nnz_C : 0)) / 1e9;
}

template <typename I, typename J>
constexpr double spmm_gflop_count(J N, I nnz_A, I nnz_C, bool beta = false)
{
    return csrmm_gflop_count(N, nnz_A, nnz_C, beta);
}

template <rocsparse_format FORMAT>
struct rocsparse_gflop_count
{
    template <typename T, typename I, typename J>
    static constexpr double sddmm(J M, J N, I nnz, J K, bool beta = false);
};

template <>
struct rocsparse_gflop_count<rocsparse_format_coo>
{
    template <typename I, typename J>
    static constexpr double sddmm(J M, J N, I nnz, J K, bool beta = false)
    {
        size_t innz = (size_t)nnz;
        return (innz * ((size_t(K) + (size_t(K) - 1)) + 1 + ((beta) ? 2 : 0))) / 1e9;
    }
};

template <>
struct rocsparse_gflop_count<rocsparse_format_csr>
{
    template <typename I, typename J>
    static constexpr double sddmm(J M, J N, I nnz, J K, bool beta = false)
    {
        return rocsparse_gflop_count<rocsparse_format_coo>::sddmm(M, N, nnz, K, beta);
    }
};

template <>
struct rocsparse_gflop_count<rocsparse_format_csc>
{
    template <typename I, typename J>
    static constexpr double sddmm(J M, J N, I nnz, J K, bool beta = false)
    {
        return rocsparse_gflop_count<rocsparse_format_coo>::sddmm(M, N, nnz, K, beta);
    }
};

template <>
struct rocsparse_gflop_count<rocsparse_format_coo_aos>
{
    template <typename I, typename J>
    static constexpr double sddmm(J M, J N, I nnz, J K, bool beta = false)
    {
        return rocsparse_gflop_count<rocsparse_format_coo>::sddmm(M, N, nnz, K, beta);
    }
};

template <>
struct rocsparse_gflop_count<rocsparse_format_ell>
{
    template <typename I, typename J>
    static constexpr double sddmm(J M, J N, I nnz, J K, bool beta = false)
    {
        return rocsparse_gflop_count<rocsparse_format_coo>::sddmm(M, N, nnz, K, beta);
    }
};

/*
 * ===========================================================================
 *    extra SPARSE
 * ===========================================================================
 */
template <typename T>
constexpr double csrgeam_gflop_count(
    rocsparse_int nnz_A, rocsparse_int nnz_B, rocsparse_int nnz_C, const T* alpha, const T* beta)
{
    // Flop counter
    double flops = 0.0;

    if(alpha && beta)
    {
        // Count alpha * A
        flops += static_cast<double>(nnz_A);

        // Count beta * B
        flops += static_cast<double>(nnz_B);

        // Count A + B
        flops += static_cast<double>(nnz_C);
    }
    else if(!alpha)
    {
        // Count beta * B
        flops += static_cast<double>(nnz_B);
    }
    else
    {
        // Count alpha * A
        flops += static_cast<double>(nnz_A);
    }

    return flops / 1e9;
}

template <typename T, typename I = rocsparse_int, typename J = rocsparse_int>
constexpr double csrgemm_gflop_count(J                    M,
                                     const T*             alpha,
                                     const I*             csr_row_ptr_A,
                                     const J*             csr_col_ind_A,
                                     const I*             csr_row_ptr_B,
                                     const T*             beta,
                                     const I*             csr_row_ptr_D,
                                     rocsparse_index_base baseA)
{
    // Flop counter
    double flops = 0.0;

    // Loop over rows of A
    for(J i = 0; i < M; ++i)
    {
        if(alpha)
        {
            I row_begin_A = csr_row_ptr_A[i] - baseA;
            I row_end_A   = csr_row_ptr_A[i + 1] - baseA;

            // Loop over columns of A
            for(I j = row_begin_A; j < row_end_A; ++j)
            {
                // Current column of A
                J col_A = csr_col_ind_A[j] - baseA;

                // Count flops generated by alpha * A * B
                flops += 2.0 * (csr_row_ptr_B[col_A + 1] - csr_row_ptr_B[col_A]) + 1.0;
            }
        }

        if(beta)
        {
            // Count flops generated by beta * D
            flops += (csr_row_ptr_D[i + 1] - csr_row_ptr_D[i]);
        }
    }

    return flops / 1e9;
}

#endif // FLOPS_HPP