File: testing_cscsort.hpp

package info (click to toggle)
hipsparse 6.4.1-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 10,804 kB
  • sloc: cpp: 106,008; f90: 7,672; sh: 560; python: 533; makefile: 38; xml: 9
file content (306 lines) | stat: -rw-r--r-- 11,854 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
/* ************************************************************************
 * Copyright (C) 2019 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
#ifndef TESTING_CSCSORT_HPP
#define TESTING_CSCSORT_HPP

#include "display.hpp"
#include "flops.hpp"
#include "gbyte.hpp"
#include "hipsparse.hpp"
#include "hipsparse_arguments.hpp"
#include "hipsparse_test_unique_ptr.hpp"
#include "unit.hpp"
#include "utility.hpp"

#include <algorithm>
#include <hipsparse.h>
#include <string>

using namespace hipsparse;
using namespace hipsparse_test;

void testing_cscsort_bad_arg(void)
{
#if(!defined(CUDART_VERSION))
    int m         = 100;
    int n         = 100;
    int nnz       = 100;
    int safe_size = 100;

    std::unique_ptr<handle_struct> unique_ptr_handle(new handle_struct);
    hipsparseHandle_t              handle = unique_ptr_handle->handle;

    std::unique_ptr<descr_struct> unique_ptr_descr(new descr_struct);
    hipsparseMatDescr_t           descr = unique_ptr_descr->descr;

    size_t buffer_size = 0;

    auto csc_col_ptr_managed
        = hipsparse_unique_ptr{device_malloc(sizeof(int) * safe_size), device_free};
    auto csc_row_ind_managed
        = hipsparse_unique_ptr{device_malloc(sizeof(int) * safe_size), device_free};
    auto perm_managed = hipsparse_unique_ptr{device_malloc(sizeof(int) * safe_size), device_free};
    auto buffer_managed
        = hipsparse_unique_ptr{device_malloc(sizeof(char) * safe_size), device_free};

    int*  csc_col_ptr = (int*)csc_col_ptr_managed.get();
    int*  csc_row_ind = (int*)csc_row_ind_managed.get();
    int*  perm        = (int*)perm_managed.get();
    void* buffer      = (void*)buffer_managed.get();

    verify_hipsparse_status_invalid_pointer(
        hipsparseXcscsort_bufferSizeExt(
            handle, m, n, nnz, (int*)nullptr, csc_row_ind, &buffer_size),
        "Error: csc_col_ptr is nullptr");
    verify_hipsparse_status_invalid_pointer(
        hipsparseXcscsort_bufferSizeExt(
            handle, m, n, nnz, csc_col_ptr, (int*)nullptr, &buffer_size),
        "Error: csc_row_ind is nullptr");
    verify_hipsparse_status_invalid_pointer(
        hipsparseXcscsort_bufferSizeExt(
            handle, m, n, nnz, csc_col_ptr, csc_row_ind, (size_t*)nullptr),
        "Error: buffer_size is nullptr");
    verify_hipsparse_status_invalid_handle(hipsparseXcscsort_bufferSizeExt(
        (hipsparseHandle_t) nullptr, m, n, nnz, csc_col_ptr, csc_row_ind, &buffer_size));

    verify_hipsparse_status_invalid_pointer(
        hipsparseXcscsort(handle, m, n, nnz, descr, (int*)nullptr, csc_row_ind, perm, buffer),
        "Error: csc_col_ptr is nullptr");
    verify_hipsparse_status_invalid_pointer(
        hipsparseXcscsort(handle, m, n, nnz, descr, csc_col_ptr, (int*)nullptr, perm, buffer),
        "Error: csc_row_ind is nullptr");
    verify_hipsparse_status_invalid_pointer(
        hipsparseXcscsort(handle, m, n, nnz, descr, csc_col_ptr, csc_row_ind, perm, (int*)nullptr),
        "Error: buffer is nullptr");
    verify_hipsparse_status_invalid_pointer(hipsparseXcscsort(handle,
                                                              m,
                                                              n,
                                                              nnz,
                                                              (hipsparseMatDescr_t) nullptr,
                                                              csc_col_ptr,
                                                              csc_row_ind,
                                                              perm,
                                                              buffer),
                                            "Error: descr is nullptr");
    verify_hipsparse_status_invalid_handle(hipsparseXcscsort(
        (hipsparseHandle_t) nullptr, m, n, nnz, descr, csc_col_ptr, csc_row_ind, perm, buffer));
#endif
}

hipsparseStatus_t testing_cscsort(Arguments argus)
{
#if(!defined(CUDART_VERSION) || CUDART_VERSION < 12000)
    int                  m        = argus.M;
    int                  n        = argus.N;
    int                  permute  = argus.permute;
    hipsparseIndexBase_t idx_base = argus.baseA;
    std::string          filename = argus.filename;

    std::unique_ptr<handle_struct> unique_ptr_handle(new handle_struct);
    hipsparseHandle_t              handle = unique_ptr_handle->handle;

    std::unique_ptr<descr_struct> unique_ptr_descr(new descr_struct);
    hipsparseMatDescr_t           descr = unique_ptr_descr->descr;

    // Set matrix index base
    CHECK_HIPSPARSE_ERROR(hipsparseSetMatIndexBase(descr, idx_base));

    if(m == 0 || n == 0)
    {
#ifdef __HIP_PLATFORM_NVIDIA__
        return HIPSPARSE_STATUS_SUCCESS;
#endif
    }

    srand(12345ULL);

    // Host structures
    std::vector<int>   hcsc_col_ptr;
    std::vector<int>   hcsc_row_ind;
    std::vector<float> hcsc_val;

    // Read or construct CSC matrix
    int nnz = 0;
    if(!generate_csr_matrix(filename, n, m, nnz, hcsc_col_ptr, hcsc_row_ind, hcsc_val, idx_base))
    {
        [&](){ GTEST_SKIP() << "Cannot open [read] " << filename; }();
        return HIPSPARSE_STATUS_SUCCESS;
    }

    // Unsort CSC columns
    std::vector<int>   hperm(nnz);
    std::vector<int>   hcsc_row_ind_unsorted(nnz);
    std::vector<float> hcsc_val_unsorted(nnz);

    hcsc_row_ind_unsorted = hcsc_row_ind;
    hcsc_val_unsorted     = hcsc_val;

    for(int i = 0; i < n; ++i)
    {
        int col_begin = hcsc_col_ptr[i] - idx_base;
        int col_end   = hcsc_col_ptr[i + 1] - idx_base;
        int col_nnz   = col_end - col_begin;

        for(int j = col_begin; j < col_end; ++j)
        {
            int rng = col_begin + rand() % col_nnz;

            int   temp_row = hcsc_row_ind_unsorted[j];
            float temp_val = hcsc_val_unsorted[j];

            hcsc_row_ind_unsorted[j] = hcsc_row_ind_unsorted[rng];
            hcsc_val_unsorted[j]     = hcsc_val_unsorted[rng];

            hcsc_row_ind_unsorted[rng] = temp_row;
            hcsc_val_unsorted[rng]     = temp_val;
        }
    }

    // Allocate memory on the device
    auto dcsc_col_ptr_managed
        = hipsparse_unique_ptr{device_malloc(sizeof(int) * (n + 1)), device_free};
    auto dcsc_row_ind_managed = hipsparse_unique_ptr{device_malloc(sizeof(int) * nnz), device_free};
    auto dcsc_val_managed = hipsparse_unique_ptr{device_malloc(sizeof(float) * nnz), device_free};
    auto dcsc_val_sorted_managed
        = hipsparse_unique_ptr{device_malloc(sizeof(float) * nnz), device_free};
    auto dperm_managed = hipsparse_unique_ptr{device_malloc(sizeof(int) * nnz), device_free};

    int*   dcsc_col_ptr    = (int*)dcsc_col_ptr_managed.get();
    int*   dcsc_row_ind    = (int*)dcsc_row_ind_managed.get();
    float* dcsc_val        = (float*)dcsc_val_managed.get();
    float* dcsc_val_sorted = (float*)dcsc_val_sorted_managed.get();

    // Set permutation vector, if asked for
#ifdef __HIP_PLATFORM_NVIDIA__
    // cusparse does not allow nullptr
    int* dperm = (int*)dperm_managed.get();
#else
    int* dperm = permute ? (int*)dperm_managed.get() : nullptr;
#endif

    // Copy data from host to device
    CHECK_HIP_ERROR(
        hipMemcpy(dcsc_col_ptr, hcsc_col_ptr.data(), sizeof(int) * (n + 1), hipMemcpyHostToDevice));
    CHECK_HIP_ERROR(hipMemcpy(
        dcsc_row_ind, hcsc_row_ind_unsorted.data(), sizeof(int) * nnz, hipMemcpyHostToDevice));
    CHECK_HIP_ERROR(
        hipMemcpy(dcsc_val, hcsc_val_unsorted.data(), sizeof(float) * nnz, hipMemcpyHostToDevice));

    // Obtain buffer size
    size_t bufferSize;
    CHECK_HIPSPARSE_ERROR(hipsparseXcscsort_bufferSizeExt(
        handle, m, n, nnz, dcsc_col_ptr, dcsc_row_ind, &bufferSize));

    // Allocate buffer on the device
    auto dbuffer_managed
        = hipsparse_unique_ptr{device_malloc(sizeof(char) * bufferSize), device_free};

    void* dbuffer = (void*)dbuffer_managed.get();

    if(permute)
    {
        // Initialize perm with identity permutation
        CHECK_HIPSPARSE_ERROR(hipsparseCreateIdentityPermutation(handle, nnz, dperm));
    }

    if(argus.unit_check)
    {
        // Sort CSC columns
        CHECK_HIPSPARSE_ERROR(hipsparseXcscsort(
            handle, m, n, nnz, descr, dcsc_col_ptr, dcsc_row_ind, dperm, dbuffer));

        if(permute)
        {
            // Sort CSC values
            CHECK_HIPSPARSE_ERROR(hipsparseSgthr(
                handle, nnz, dcsc_val, dcsc_val_sorted, dperm, HIPSPARSE_INDEX_BASE_ZERO));
        }

        // Copy output from device to host
        CHECK_HIP_ERROR(hipMemcpy(
            hcsc_row_ind_unsorted.data(), dcsc_row_ind, sizeof(int) * nnz, hipMemcpyDeviceToHost));

        if(permute)
        {
            CHECK_HIP_ERROR(hipMemcpy(hcsc_val_unsorted.data(),
                                      dcsc_val_sorted,
                                      sizeof(float) * nnz,
                                      hipMemcpyDeviceToHost));
        }

        // Unit check
        unit_check_general(1, nnz, 1, hcsc_row_ind.data(), hcsc_row_ind_unsorted.data());

        if(permute)
        {
            unit_check_general(1, nnz, 1, hcsc_val.data(), hcsc_val_unsorted.data());
        }
    }

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

        // Warm up
        for(int iter = 0; iter < number_cold_calls; ++iter)
        {
            CHECK_HIPSPARSE_ERROR(hipsparseXcscsort(
                handle, m, n, nnz, descr, dcsc_col_ptr, dcsc_row_ind, dperm, dbuffer));
        }

        double gpu_time_used = get_time_us();

        // Performance run
        for(int iter = 0; iter < number_hot_calls; ++iter)
        {
            CHECK_HIPSPARSE_ERROR(hipsparseXcscsort(
                handle, m, n, nnz, descr, dcsc_col_ptr, dcsc_row_ind, dperm, dbuffer));
        }

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

        double gbyte_count = cscsort_gbyte_count(n, nnz, permute);
        double gpu_gbyte   = get_gpu_gbyte(gpu_time_used, gbyte_count);

        display_timing_info(display_key_t::M,
                            m,
                            display_key_t::N,
                            n,
                            display_key_t::nnz,
                            nnz,
                            display_key_t::permute,
                            (permute ? "yes" : "no"),
                            display_key_t::bandwidth,
                            gpu_gbyte,
                            display_key_t::time_ms,
                            get_gpu_time_msec(gpu_time_used));
    }
#endif

    return HIPSPARSE_STATUS_SUCCESS;
}

#endif // TESTING_CSCSORT_HPP