File: testing_check_spmat_dispatch.hpp

package info (click to toggle)
rocsparse 6.4.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,428 kB
  • sloc: cpp: 234,069; f90: 9,307; sh: 2,262; python: 1,939; makefile: 1,585; ansic: 440; xml: 26
file content (340 lines) | stat: -rw-r--r-- 14,155 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/* ************************************************************************
 * Copyright (C) 2020-2023 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.
 *
 * ************************************************************************ */

#pragma once

#include "auto_testing_bad_arg.hpp"

template <rocsparse_format FORMAT, typename I, typename J, typename T>
struct testing_check_spmat_dispatch_traits;

//
// TRAITS FOR CSR FORMAT.
//
template <typename I, typename J, typename T>
struct testing_check_spmat_dispatch_traits<rocsparse_format_csr, I, J, T>
{
    using host_sparse_matrix   = host_csr_matrix<T, I, J>;
    using device_sparse_matrix = device_csr_matrix<T, I, J>;

    static void sparse_initialization(rocsparse_matrix_factory<T, I, J>& matrix_factory,
                                      host_sparse_matrix&                hA,
                                      J                                  m,
                                      J                                  n,
                                      rocsparse_index_base               base,
                                      J                                  block_dim)
    {
        matrix_factory.init_csr(hA, m, n, base);
    }

    static void display_info(const Arguments& arg, host_sparse_matrix& hA, double gpu_time_used)
    {
        double gbyte_count = check_matrix_csr_gbyte_count<T>(hA.m, hA.nnz);
        double gpu_gbyte   = get_gpu_gbyte(gpu_time_used, gbyte_count);

        display_timing_info(display_key_t::M,
                            hA.m,
                            display_key_t::N,
                            hA.n,
                            display_key_t::nnz,
                            hA.nnz,
                            display_key_t::bandwidth,
                            gpu_gbyte,
                            display_key_t::time_ms,
                            get_gpu_time_msec(gpu_time_used));
    }
};

//
// TRAITS FOR CSC FORMAT.
//
template <typename I, typename J, typename T>
struct testing_check_spmat_dispatch_traits<rocsparse_format_csc, I, J, T>
{
    using host_sparse_matrix   = host_csc_matrix<T, I, J>;
    using device_sparse_matrix = device_csc_matrix<T, I, J>;

    static void sparse_initialization(rocsparse_matrix_factory<T, I, J>& matrix_factory,
                                      host_sparse_matrix&                hA,
                                      J                                  m,
                                      J                                  n,
                                      rocsparse_index_base               base,
                                      J                                  block_dim)
    {
        matrix_factory.init_csc(hA, m, n, base);
    }

    static void display_info(const Arguments& arg, host_sparse_matrix& hA, double gpu_time_used)
    {
        double gbyte_count = check_matrix_csc_gbyte_count<T>(hA.n, hA.nnz);
        double gpu_gbyte   = get_gpu_gbyte(gpu_time_used, gbyte_count);

        display_timing_info(display_key_t::M,
                            hA.m,
                            display_key_t::N,
                            hA.n,
                            display_key_t::nnz,
                            hA.nnz,
                            display_key_t::bandwidth,
                            gpu_gbyte,
                            display_key_t::time_ms,
                            get_gpu_time_msec(gpu_time_used));
    }
};

//
// TRAITS FOR COO FORMAT.
//
template <typename I, typename T>
struct testing_check_spmat_dispatch_traits<rocsparse_format_coo, I, I, T>
{
    using host_sparse_matrix   = host_coo_matrix<T, I>;
    using device_sparse_matrix = device_coo_matrix<T, I>;

    static void sparse_initialization(rocsparse_matrix_factory<T, I, I>& matrix_factory,
                                      host_sparse_matrix&                hA,
                                      I                                  m,
                                      I                                  n,
                                      rocsparse_index_base               base,
                                      I                                  block_dim)
    {
        matrix_factory.init_coo(hA, m, n, base);
    }

    static void display_info(const Arguments& arg, host_sparse_matrix& hA, double gpu_time_used)
    {
        double gbyte_count = check_matrix_coo_gbyte_count<T>(hA.nnz);
        double gpu_gbyte   = get_gpu_gbyte(gpu_time_used, gbyte_count);

        display_timing_info(display_key_t::M,
                            hA.m,
                            display_key_t::N,
                            hA.n,
                            display_key_t::nnz,
                            hA.nnz,
                            display_key_t::bandwidth,
                            gpu_gbyte,
                            display_key_t::time_ms,
                            get_gpu_time_msec(gpu_time_used));
    }
};

//
// TRAITS FOR ELL FORMAT.
//
template <typename I, typename T>
struct testing_check_spmat_dispatch_traits<rocsparse_format_ell, I, I, T>
{
    using host_sparse_matrix   = host_ell_matrix<T, I>;
    using device_sparse_matrix = device_ell_matrix<T, I>;

    static void sparse_initialization(rocsparse_matrix_factory<T, I, I>& matrix_factory,
                                      host_sparse_matrix&                hA,
                                      I                                  m,
                                      I                                  n,
                                      rocsparse_index_base               base,
                                      I                                  block_dim)
    {
        matrix_factory.init_ell(hA, m, n, base);
    }

    static void display_info(const Arguments& arg, host_sparse_matrix& hA, double gpu_time_used)
    {
        double gbyte_count = check_matrix_ell_gbyte_count<T>(hA.nnz);
        double gpu_gbyte   = get_gpu_gbyte(gpu_time_used, gbyte_count);

        display_timing_info(display_key_t::M,
                            hA.m,
                            display_key_t::N,
                            hA.n,
                            display_key_t::nnz,
                            hA.nnz,
                            display_key_t::bandwidth,
                            gpu_gbyte,
                            display_key_t::time_ms,
                            get_gpu_time_msec(gpu_time_used));
    }
};

//
// TRAITS FOR BSR FORMAT.
//
template <typename I, typename J, typename T>
struct testing_check_spmat_dispatch_traits<rocsparse_format_bsr, I, J, T>
{
    using host_sparse_matrix   = host_gebsr_matrix<T, I, J>;
    using device_sparse_matrix = device_gebsr_matrix<T, I, J>;

    static void sparse_initialization(rocsparse_matrix_factory<T, I, J>& matrix_factory,
                                      host_sparse_matrix&                hA,
                                      J                                  m,
                                      J                                  n,
                                      rocsparse_index_base               base,
                                      J                                  block_dim)
    {
        matrix_factory.init_gebsr(hA, m, n, block_dim, block_dim, base);
    }

    static void display_info(const Arguments& arg, host_sparse_matrix& hA, double gpu_time_used)
    {
        double gbyte_count
            = check_matrix_gebsr_gbyte_count<T>(hA.mb, hA.nnzb, hA.row_block_dim, hA.col_block_dim);
        double gpu_gbyte = get_gpu_gbyte(gpu_time_used, gbyte_count);

        display_timing_info(display_key_t::M,
                            hA.mb * hA.row_block_dim,
                            display_key_t::N,
                            hA.nb * hA.row_block_dim,
                            display_key_t::Mb,
                            hA.mb,
                            display_key_t::Nb,
                            hA.nb,
                            display_key_t::bdim,
                            hA.row_block_dim,
                            display_key_t::nnzb,
                            hA.nnzb,
                            display_key_t::bandwidth,
                            gpu_gbyte,
                            display_key_t::time_ms,
                            get_gpu_time_msec(gpu_time_used));
    }
};

template <rocsparse_format FORMAT, typename I, typename J, typename T>
struct testing_check_spmat_dispatch
{
private:
    using traits = testing_check_spmat_dispatch_traits<FORMAT, I, J, T>;

    using host_sparse_matrix   = typename traits::host_sparse_matrix;
    using device_sparse_matrix = typename traits::device_sparse_matrix;

public:
    static void testing_check_spmat(const Arguments& arg)
    {
        J                      m           = arg.M;
        J                      n           = arg.N;
        rocsparse_index_base   base        = arg.baseA;
        J                      block_dim   = arg.block_dim;
        rocsparse_matrix_type  matrix_type = arg.matrix_type;
        rocsparse_fill_mode    uplo        = arg.uplo;
        rocsparse_storage_mode storage     = arg.storage;

        if(block_dim > 1)
        {
            m = (m + block_dim - 1) / block_dim;
            n = (n + block_dim - 1) / block_dim;
        }

        // Create rocsparse handle
        rocsparse_local_handle handle;

        rocsparse_matrix_factory<T, I, J> matrix_factory(arg);

        // Allocate host memory for CSR matrix
        host_sparse_matrix hA;

        // Generate (or load from file) CSR matrix
        traits::sparse_initialization(matrix_factory, hA, m, n, base, block_dim);

        // CSR matrix on device
        device_sparse_matrix dA(hA);

        // Create descriptor
        rocsparse_local_spmat A(dA);

        // Set Attributes
        EXPECT_ROCSPARSE_STATUS(
            rocsparse_spmat_set_attribute(
                A, rocsparse_spmat_matrix_type, &matrix_type, sizeof(matrix_type)),
            rocsparse_status_success);

        EXPECT_ROCSPARSE_STATUS(
            rocsparse_spmat_set_attribute(A, rocsparse_spmat_fill_mode, &uplo, sizeof(uplo)),
            rocsparse_status_success);

        EXPECT_ROCSPARSE_STATUS(rocsparse_spmat_set_attribute(
                                    A, rocsparse_spmat_storage_mode, &storage, sizeof(storage)),
                                rocsparse_status_success);

        // Allocate buffer
        size_t                buffer_size;
        rocsparse_data_status data_status;
        CHECK_ROCSPARSE_ERROR(rocsparse_check_spmat(handle,
                                                    A,
                                                    &data_status,
                                                    rocsparse_check_spmat_stage_buffer_size,
                                                    &buffer_size,
                                                    nullptr));

        void* dbuffer;
        CHECK_HIP_ERROR(rocsparse_hipMalloc(&dbuffer, buffer_size));

        if(arg.unit_check)
        {
            CHECK_ROCSPARSE_ERROR(rocsparse_check_spmat(handle,
                                                        A,
                                                        &data_status,
                                                        rocsparse_check_spmat_stage_compute,
                                                        &buffer_size,
                                                        dbuffer));
            CHECK_ROCSPARSE_DATA_ERROR(data_status);
        }

        if(arg.timing)
        {
            int number_cold_calls = 2;
            int number_hot_calls  = arg.iters;

            // Warm up
            for(int iter = 0; iter < number_cold_calls; ++iter)
            {
                CHECK_ROCSPARSE_ERROR(rocsparse_check_spmat(handle,
                                                            A,
                                                            &data_status,
                                                            rocsparse_check_spmat_stage_compute,
                                                            nullptr,
                                                            dbuffer));
            }

            double gpu_time_used = get_time_us();

            // Performance run
            for(int iter = 0; iter < number_hot_calls; ++iter)
            {
                CHECK_ROCSPARSE_ERROR(rocsparse_check_spmat(handle,
                                                            A,
                                                            &data_status,
                                                            rocsparse_check_spmat_stage_compute,
                                                            nullptr,
                                                            dbuffer));
            }

            gpu_time_used = (get_time_us() - gpu_time_used) / number_hot_calls;

            traits::display_info(arg, hA, gpu_time_used);
        }

        CHECK_HIP_ERROR(rocsparse_hipFree(dbuffer));
    }
};