File: testing_extract.cpp

package info (click to toggle)
rocsparse 6.4.3-3
  • links: PTS, VCS
  • area: main
  • in suites: 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 (428 lines) | stat: -rw-r--r-- 14,188 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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*! \file */
/* ************************************************************************
 * Copyright (C) 2024 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.
 *
 * ************************************************************************ */
#include "auto_testing_bad_arg.hpp"
#include "testing.hpp"

//
// Host extract function.
//
template <rocsparse_direction DIRECTION, typename T, typename I, typename J>
static void test_extract_compute_csx(const host_csx_matrix<DIRECTION, T, I, J>& S_,
                                     host_csx_matrix<DIRECTION, T, I, J>&       T_,
                                     rocsparse_fill_mode                        T_fill_mode_,
                                     rocsparse_diag_type                        T_diag_)
{
    const int64_t size = (DIRECTION == rocsparse_direction_row) ? S_.m : S_.n;

    auto predicate = [T_fill_mode_, T_diag_](J i, J j) {
        return (DIRECTION == rocsparse_direction_row)
                   ? ((T_fill_mode_ == rocsparse_fill_mode_lower)
                          ? ((T_diag_ == rocsparse_diag_type_unit) ? (i > j) : (i >= j))
                          : ((T_diag_ == rocsparse_diag_type_unit) ? (i < j) : (i <= j)))
                   : ((T_fill_mode_ == rocsparse_fill_mode_lower)
                          ? ((T_diag_ == rocsparse_diag_type_unit) ? (i < j) : (i <= j))
                          : ((T_diag_ == rocsparse_diag_type_unit) ? (i > j) : (i >= j)));
    };

    for(J i = 0; i <= size; ++i)
    {
        T_.ptr[i] = 0;
    }

    for(J i = 0; i < size; ++i)
    {
        for(I k = S_.ptr[i] - S_.base; k < S_.ptr[i + 1] - S_.base; ++k)
        {
            const J j = S_.ind[k] - S_.base;
            if(predicate(i, j))
            {
                T_.ptr[i + 1] += 1;
            }
        }
    }

    for(J i = 2; i <= size; ++i)
    {
        T_.ptr[i] += T_.ptr[i - 1];
    }

    const I nnz = T_.ptr[size];
    T_.define(T_.m, T_.n, nnz, T_.base);

    for(J i = 0; i < size; ++i)
    {
        for(I k = S_.ptr[i] - S_.base; k < S_.ptr[i + 1] - S_.base; ++k)
        {
            const J j = S_.ind[k] - S_.base;
            if(predicate(i, j))
            {
                T_.ind[T_.ptr[i]] = j + T_.base;
                T_.val[T_.ptr[i]] = S_.val[k];
                T_.ptr[i] += 1;
            }
        }
    }

    for(J i = size; i > 0; --i)
    {
        T_.ptr[i] = T_.ptr[i - 1];
    }
    T_.ptr[0] = 0;

    for(J i = 0; i <= size; ++i)
    {
        T_.ptr[i] += T_.base;
    }
}

//
// Testing bad arguments.
//
template <typename I, typename J, typename T>
void testing_extract_bad_arg(const Arguments& arg)
{
    rocsparse_local_handle  local_handle;
    rocsparse_spmat_descr   source            = (rocsparse_spmat_descr)0x4;
    rocsparse_spmat_descr   target            = (rocsparse_spmat_descr)0x4;
    rocsparse_handle        handle            = local_handle;
    rocsparse_extract_alg   alg               = rocsparse_extract_alg_default;
    rocsparse_extract_stage stage             = rocsparse_extract_stage_analysis;
    size_t                  local_buffer_size = 100;
    void*                   buffer            = (void*)0x4;
    {
        rocsparse_extract_descr* descr = (rocsparse_extract_descr*)0x4;
        bad_arg_analysis(rocsparse_create_extract_descr, descr, source, target, alg);
    }

    {
        rocsparse_extract_descr descr = (rocsparse_extract_descr)0x4;

        {
            size_t* buffer_size_in_bytes = &local_buffer_size;
            bad_arg_analysis(rocsparse_extract_buffer_size,
                             handle,
                             descr,
                             source,
                             target,
                             stage,
                             buffer_size_in_bytes);
        }

        {
            size_t               buffer_size_in_bytes              = local_buffer_size;
            static constexpr int nargs_to_exclude                  = 1;
            static constexpr int args_to_exclude[nargs_to_exclude] = {5};

            select_bad_arg_analysis(rocsparse_extract,
                                    nargs_to_exclude,
                                    args_to_exclude,
                                    handle,
                                    descr,
                                    source,
                                    target,
                                    stage,
                                    buffer_size_in_bytes,
                                    buffer);
        }
    }
}

template <rocsparse_direction DIRECTION, typename T, typename I, typename J>
static void testing_rocsparse_spmat_extract(rocsparse_handle                       handle,
                                            rocsparse_const_spmat_descr            A,
                                            device_csx_matrix<DIRECTION, T, I, J>& device_B,
                                            rocsparse_spmat_descr                  B,
                                            rocsparse_extract_alg                  alg)
{

    hipStream_t stream;
    CHECK_ROCSPARSE_ERROR(rocsparse_get_stream(handle, &stream));

    rocsparse_extract_descr extract_descr;

    //
    // Create descriptor.
    //
    CHECK_ROCSPARSE_ERROR(rocsparse_create_extract_descr(&extract_descr, A, B, alg));
    //
    // Analysis: get buffer size.
    //
    size_t buffer_size = 0;
    void*  buffer      = nullptr;
    CHECK_ROCSPARSE_ERROR(rocsparse_extract_buffer_size(
        handle, extract_descr, A, B, rocsparse_extract_stage_analysis, &buffer_size));

    CHECK_HIP_ERROR(rocsparse_hipMalloc(&buffer, buffer_size));

    //
    // Analysis: execute.
    //
    CHECK_ROCSPARSE_ERROR(rocsparse_extract(
        handle, extract_descr, A, B, rocsparse_extract_stage_analysis, buffer_size, buffer));

    int64_t nnz;
    CHECK_ROCSPARSE_ERROR(rocsparse_extract_nnz(handle, extract_descr, &nnz));

    //
    // Synchronize to get nnz.
    //
    CHECK_HIP_ERROR(hipStreamSynchronize(stream));

    CHECK_HIP_ERROR(rocsparse_hipFree(buffer));
    buffer      = nullptr;
    buffer_size = 0;

    //
    // Realloc.
    //
    device_B.define(device_B.m, device_B.n, nnz, device_B.base);

    rocsparse_format B_format;
    CHECK_ROCSPARSE_ERROR(rocsparse_spmat_get_format(B, &B_format));
    CHECK_ROCSPARSE_ERROR(rocsparse_spmat_set_nnz(B, nnz));
    switch(B_format)
    {
    case rocsparse_format_csr:
    {
        CHECK_ROCSPARSE_ERROR(
            rocsparse_csr_set_pointers(B, device_B.ptr, device_B.ind, device_B.val));
        break;
    }

    case rocsparse_format_csc:
    {
        CHECK_ROCSPARSE_ERROR(
            rocsparse_csc_set_pointers(B, device_B.ptr, device_B.ind, device_B.val));
        break;
    }

    case rocsparse_format_bell:
    case rocsparse_format_ell:
    case rocsparse_format_bsr:
    case rocsparse_format_coo:
    case rocsparse_format_coo_aos:
    {
        CHECK_ROCSPARSE_ERROR(rocsparse_status_not_implemented);
        break;
    }
    }

    //
    // Compute: get buffer size.
    //
    CHECK_ROCSPARSE_ERROR(rocsparse_extract_buffer_size(
        handle, extract_descr, A, B, rocsparse_extract_stage_compute, &buffer_size));
    CHECK_HIP_ERROR(rocsparse_hipMalloc(&buffer, buffer_size));

    //
    // Compute: execute.
    //
    CHECK_ROCSPARSE_ERROR(rocsparse_extract(
        handle, extract_descr, A, B, rocsparse_extract_stage_compute, buffer_size, buffer));

    //
    // Synchronize.
    //
    CHECK_HIP_ERROR(hipStreamSynchronize(stream));

    CHECK_HIP_ERROR(rocsparse_hipFree(buffer));
    //
    // Destroy algorithm descriptor.
    //
    CHECK_ROCSPARSE_ERROR(rocsparse_destroy_extract_descr(extract_descr));
}

template <typename I, typename J, typename T>
static void init_csx(const Arguments&                   arg,
                     host_csr_matrix<T, I, J>&          A,
                     rocsparse_matrix_factory<T, I, J>& matrix_factory)
{
    J m = arg.M;
    J n = arg.N;
    matrix_factory.init_csr(A, m, n, arg.baseA);
}

template <typename I, typename J, typename T>
static void init_csx(const Arguments&                   arg,
                     host_csc_matrix<T, I, J>&          A,
                     rocsparse_matrix_factory<T, I, J>& matrix_factory)
{
    J m = arg.M;
    J n = arg.N;
    matrix_factory.init_csc(A, m, n, arg.baseA);
}

template <rocsparse_direction DIRECTION, typename I, typename J, typename T>
static void testing_extract_csx_template(const Arguments& arg)
{
    rocsparse_local_handle      handle(arg);
    const rocsparse_extract_alg alg = rocsparse_extract_alg_default;

    //
    // Create matrix A.
    //
    host_csx_matrix<DIRECTION, T, I, J> host_A;
    {
        rocsparse_matrix_factory<T, I, J> matrix_factory(arg);
        init_csx(arg, host_A, matrix_factory);
    }

    device_csx_matrix<DIRECTION, T, I, J> device_A(host_A);
    rocsparse_local_spmat                 A(device_A);

    //
    // Create matrix B.
    //
    host_csx_matrix<DIRECTION, T, I, J>   host_B(host_A.m, host_A.n, 0, arg.baseB);
    device_csx_matrix<DIRECTION, T, I, J> device_B(host_B);
    rocsparse_local_spmat                 B(device_B);

    const rocsparse_fill_mode fill_mode = arg.uplo;
    const rocsparse_diag_type diag_type = arg.diag;

    //
    // Set attributes.
    //
    {

        CHECK_ROCSPARSE_ERROR(rocsparse_spmat_set_attribute(
            B, rocsparse_spmat_diag_type, &diag_type, sizeof(diag_type)));

        CHECK_ROCSPARSE_ERROR(rocsparse_spmat_set_attribute(
            B, rocsparse_spmat_fill_mode, &fill_mode, sizeof(fill_mode)));

        rocsparse_matrix_type matrix_type = rocsparse_matrix_type_triangular;
        CHECK_ROCSPARSE_ERROR(rocsparse_spmat_set_attribute(
            B, rocsparse_spmat_matrix_type, &matrix_type, sizeof(matrix_type)));
    }

    if(arg.unit_check)
    {
        //
        // Do the extraction.
        //

        testing_rocsparse_spmat_extract(handle, A, device_B, B, alg);

        //
        // Host calculation.
        //

        test_extract_compute_csx(host_A, host_B, fill_mode, diag_type);
        //
        // Compare.
        //
        host_B.unit_check(device_B);
    }

    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)
        {
            //
            // To fill
            //
        }

        double gpu_time_used = get_time_us();

        // Performance run
        for(int iter = 0; iter < number_hot_calls; ++iter)
        {
            //
            // To fill
            //
        }

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

        double gbyte_count = 0; // csr2csc_gbyte_count<T>(M, N, nnz, action);
        double gpu_gbyte   = get_gpu_gbyte(gpu_time_used, gbyte_count);

        display_timing_info(display_key_t::M,
                            host_A.m,
                            display_key_t::N,
                            host_A.n,
                            display_key_t::nnz,
                            host_A.nnz,
                            display_key_t::fill_mode,
                            rocsparse_fillmode2string(arg.uplo),
                            display_key_t::diag_type,
                            rocsparse_diagtype2string(arg.diag),
                            display_key_t::bandwidth,
                            gpu_gbyte,
                            display_key_t::time_ms,
                            get_gpu_time_msec(gpu_time_used));
    }
}

template <typename I, typename J, typename T>
void testing_extract(const Arguments& arg)
{
    switch(arg.formatA)
    {
    case rocsparse_format_csr:
    {
        testing_extract_csx_template<rocsparse_direction_row, I, J, T>(arg);
        break;
    }
    case rocsparse_format_csc:
    {
        testing_extract_csx_template<rocsparse_direction_column, I, J, T>(arg);
        break;
    }
    case rocsparse_format_ell:
    case rocsparse_format_bell:
    case rocsparse_format_bsr:
    case rocsparse_format_coo:
    case rocsparse_format_coo_aos:
    {
        std::cerr << "not implemented" << std::endl;
        break;
    }
    }
}

void testing_extract_extra(const Arguments& arg) {}

#define INSTANTIATE(ITYPE, JTYPE, TYPE)                                              \
    template void testing_extract_bad_arg<ITYPE, JTYPE, TYPE>(const Arguments& arg); \
    template void testing_extract<ITYPE, JTYPE, TYPE>(const Arguments& arg)

INSTANTIATE(int32_t, int32_t, float);
INSTANTIATE(int32_t, int32_t, double);
INSTANTIATE(int32_t, int32_t, rocsparse_float_complex);
INSTANTIATE(int32_t, int32_t, rocsparse_double_complex);
INSTANTIATE(int64_t, int32_t, float);
INSTANTIATE(int64_t, int32_t, double);
INSTANTIATE(int64_t, int32_t, rocsparse_float_complex);
INSTANTIATE(int64_t, int32_t, rocsparse_double_complex);
INSTANTIATE(int64_t, int64_t, float);
INSTANTIATE(int64_t, int64_t, double);
INSTANTIATE(int64_t, int64_t, rocsparse_float_complex);
INSTANTIATE(int64_t, int64_t, rocsparse_double_complex);