File: example_scal_multiple_strided_batch.cpp

package info (click to toggle)
rocblas 6.4.4-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,082,776 kB
  • sloc: cpp: 244,923; f90: 50,012; python: 50,003; sh: 24,630; asm: 8,917; makefile: 150; ansic: 107; xml: 36; awk: 14
file content (411 lines) | stat: -rw-r--r-- 14,889 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
/* ************************************************************************
 * Copyright (C) 2016-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 cop-
 * ies 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 IM-
 * PLIED, 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 CONNE-
 * CTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * ************************************************************************ */

#include "client_utility.hpp"
#include "host_alloc.hpp"
#include "rocblas_vector.hpp"

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <hip/hip_runtime.h>
#include <iostream>
#include <limits>
#include <rocblas/rocblas.h>
#include <string>
#include <vector>

#ifndef CHECK_HIP_ERROR
#define CHECK_HIP_ERROR(error)                    \
    if(error != hipSuccess)                       \
    {                                             \
        fprintf(stderr,                           \
                "Hip error: '%s'(%d) at %s:%d\n", \
                hipGetErrorString(error),         \
                error,                            \
                __FILE__,                         \
                __LINE__);                        \
        exit(EXIT_FAILURE);                       \
    }
#endif

#ifndef CHECK_ROCBLAS_ERROR
#define CHECK_ROCBLAS_ERROR(error)                              \
    if(error != rocblas_status_success)                         \
    {                                                           \
        fprintf(stderr, "rocBLAS error: ");                     \
        if(error == rocblas_status_invalid_handle)              \
            fprintf(stderr, "rocblas_status_invalid_handle");   \
        if(error == rocblas_status_not_implemented)             \
            fprintf(stderr, " rocblas_status_not_implemented"); \
        if(error == rocblas_status_invalid_pointer)             \
            fprintf(stderr, "rocblas_status_invalid_pointer");  \
        if(error == rocblas_status_invalid_size)                \
            fprintf(stderr, "rocblas_status_invalid_size");     \
        if(error == rocblas_status_memory_error)                \
            fprintf(stderr, "rocblas_status_memory_error");     \
        if(error == rocblas_status_internal_error)              \
            fprintf(stderr, "rocblas_status_internal_error");   \
        fprintf(stderr, "\n");                                  \
        exit(EXIT_FAILURE);                                     \
    }
#endif

// default sizes
#define DIM1 127
#define BATCH_COUNT 10
#define ALPHA 2

void print_strided_batched_vector(
    const char* name, float* x, rocblas_int n1, rocblas_int n2, rocblas_int s1, rocblas_int s2)
{
    // n1, n2 are vector dimensions, sometimes called n, batch_count
    // s1, s1 are vector strides, sometimes called incx, stride_x
    printf("---------- %s ----------\n", name);
    int max_size = 4;

    for(int i2 = 0; i2 < n2 && i2 < max_size; i2++)
    {
        for(int i1 = 0; i1 < n1 && i1 < max_size; i1++)
        {
            printf("%8.1f ", x[(i1 * s1) + (i2 * s2)]);
        }
        printf("\n");
    }
}

void print_multiple_strided_batched_vector(const char* name,
                                           float*      x,
                                           rocblas_int n1,
                                           rocblas_int n2,
                                           rocblas_int n3,
                                           rocblas_int s1,
                                           rocblas_int s2,
                                           rocblas_int s3)
{
    // n1, n2 are vector dimensions, sometimes called n, batch_count
    // s1, s1 are vector strides, sometimes called incx, stride_x
    printf("---------- %s ----------\n", name);
    int max_size = 4;

    for(int i3 = 0; i3 < n3 && i3 < max_size; i3++)
    {
        for(int i2 = 0; i2 < n2 && i2 < max_size; i2++)
        {
            for(int i1 = 0; i1 < n1 && i1 < max_size; i1++)
            {
                printf("%8.1f ", x[(i1 * s1) + (i2 * s2) + (i3 * s3)]);
            }
            printf("\n");
        }
        printf("---------------------------------------\n");
    }
}

// cppcheck-suppress constParameter
static void show_usage(char* argv[])
{
    std::cerr
        << "Usage: " << argv[0] << " <options>\n"
        << "options:\n"
        << "\t-h, --help\t\t\t\tShow this help message\n"
        << "\t-v, --verbose\t\t\t\tverbose output\n"
        << "\t-n \t\t\tn\t\tSCAL_STRIDED_BATCHED argument n\n"
        << "\t--incx \t\t\tincx \t\tSCAL_STRIDED_BATCHED argument lda\n"
        << "\t--stride_x \t\tstride_x \tSCAL_STRIDED_BATCHED argument stride_x\n"
        << "\t--batch_count \t\tbatch_count \tSCAL_STRIDED_BATCHED argument batch count\n"
        << "\t--multiple_count \tmultiple_count \tSCAL_STRIDED_BATCHED argument multiple count\n"
        << "\t--alpha \t\talpha \t\tSCAL_STRIDED_BATCHED argument alpha\n"
        << "\t--header \t\theader \t\tprint header for output\n"
        << std::endl;
}

static int parse_arguments(int    argc,
                           char*  argv[],
                           int&   n,
                           int&   incx,
                           int&   stride_x,
                           int&   batch_count,
                           int&   multiple_count,
                           float& alpha,
                           bool&  header,
                           bool&  verbose)
{
    if(argc >= 2)
    {
        for(int i = 1; i < argc; ++i)
        {
            std::string arg = argv[i];

            if((arg.at(0) == '-') || ((arg.at(0) == '-') && (arg.at(1) == '-')))
            {
                if((arg == "-h") || (arg == "--help"))
                {
                    return EXIT_FAILURE;
                }
                if((arg == "-v") || (arg == "--verbose"))
                {
                    verbose = true;
                }
                else if(arg == "--header")
                {
                    header = true;
                }
                else if((arg == "-n") && (i + 1 < argc))
                {
                    n = atoi(argv[++i]);
                }
                else if((arg == "--batch_count") && (i + 1 < argc))
                {
                    batch_count = atoi(argv[++i]);
                }
                else if((arg == "--multiple_count") && (i + 1 < argc))
                {
                    multiple_count = atoi(argv[++i]);
                }
                else if((arg == "--incx") && (i + 1 < argc))
                {
                    incx = atoi(argv[++i]);
                }
                else if((arg == "--stride_x") && (i + 1 < argc))
                {
                    stride_x = atoi(argv[++i]);
                }
                else if((arg == "--alpha") && (i + 1 < argc))
                {
                    alpha = atof(argv[++i]);
                }
                else
                {
                    std::cerr << "error with " << arg << std::endl;
                    std::cerr << "do not recognize option" << std::endl << std::endl;
                    return EXIT_FAILURE;
                }
            }
            else
            {
                std::cerr << "error with " << arg << std::endl;
                std::cerr << "option must start with - or --" << std::endl << std::endl;
                return EXIT_FAILURE;
            }
        }
    }
    return EXIT_SUCCESS;
}

bool bad_argument(rocblas_int n, rocblas_int incx, rocblas_int stride_x, rocblas_int batch_count)
{
    bool argument_error = false;
    if(stride_x < 0)
    {
        argument_error = true;
        std::cerr << "ERROR: bad argument stride_x < 0" << std::endl;
    }
    if(n < 0)
    {
        argument_error = true;
        std::cerr << "ERROR: bad argument n = " << n << " < "
                  << "0" << std::endl;
    }
    if(incx < 0)
    {
        argument_error = true;
        std::cerr << "ERROR: bad argument incx = " << incx << " < "
                  << "0" << std::endl;
    }
    if(batch_count < 1)
    {
        argument_error = true;
        std::cerr << "ERROR: bad argument batch_count = " << batch_count << " < 1" << std::endl;
    }

    return argument_error;
}

void initialize_x(float* hx, float* hx_gold, int size_x)
{
    srand(1);
    for(int i = 0; i < size_x; i++)
    {
        hx[i] = hx_gold[i] = rand() % 3;
    }
}

int main(int argc, char* argv[])
{
    // invalid int and float for rocblas_sgemm_strided_batched int and float arguments
    rocblas_int invalid_int   = std::numeric_limits<rocblas_int>::min() + 1;
    float       invalid_float = std::numeric_limits<float>::quiet_NaN();

    // initialize to invalid value to detect if values not specified on command line
    rocblas_int n = invalid_int, incx = invalid_int, stride_x = invalid_int;

    rocblas_int batch_count    = invalid_int;
    rocblas_int multiple_count = 2;

    float alpha = invalid_float;

    bool verbose = false;
    bool header  = false;

    if(parse_arguments(
           argc, argv, n, incx, stride_x, batch_count, multiple_count, alpha, header, verbose))
    {
        show_usage(argv);
        return EXIT_FAILURE;
    }

    // when arguments not specified, set to default values
    if(n == invalid_int)
        n = DIM1;
    if(incx == invalid_int)
        incx = 1;
    if(stride_x == invalid_int)
        stride_x = n * incx;
    if(alpha != alpha)
        alpha = ALPHA; // check for alpha == invalid_float == NaN
    if(batch_count == invalid_int)
        batch_count = BATCH_COUNT;

    if(bad_argument(n, incx, stride_x, batch_count))
    {
        show_usage(argv);
        return EXIT_FAILURE;
    }

    host_strided_batch_vector<float>   hx(n, incx, stride_x, batch_count);
    host_strided_batch_vector<float>   hx_gold(n, incx, stride_x, batch_count);
    device_strided_batch_vector<float> dx(n, incx, stride_x, batch_count);
    CHECK_HIP_ERROR(hx.memcheck());
    CHECK_HIP_ERROR(hx_gold.memcheck());
    CHECK_HIP_ERROR(dx.memcheck());

    int size_x = hx.nmemb();
    initialize_x(hx.data(), hx_gold.data(), size_x);

    if(verbose)
    {
        print_strided_batched_vector("hx initial", hx, n, batch_count, incx, stride_x);
    }

    // copy data from CPU to device
    CHECK_HIP_ERROR(dx.transfer_from(hx));

    rocblas_handle handle;
    CHECK_ROCBLAS_ERROR(rocblas_create_handle(&handle));

    CHECK_ROCBLAS_ERROR(
        rocblas_sscal_strided_batched(handle, n, &alpha, dx, incx, stride_x, batch_count));

    // copy output from device to CPU
    CHECK_HIP_ERROR(hx.transfer_from(dx));

    // calculate golden or correct result
    int s1 = incx, s2 = stride_x;
    for(int i2 = 0; i2 < batch_count; i2++)
    {
        for(int i1 = 0; i1 < n; i1++)
        {
            (hx_gold[i2])[i1 * incx] *= alpha;
        }
    }

    if(verbose)
    {
        print_strided_batched_vector("hx calculated", hx, n, batch_count, incx, stride_x);
        print_strided_batched_vector("hx_gold calculated", hx_gold, n, batch_count, incx, stride_x);
    }

    float max_relative_error = std::numeric_limits<float>::min();
    for(int i = 0; i < size_x; i++)
    {
        float relative_error = (hx_gold.data())[i] == 0
                                   ? (hx.data())[i]
                                   : ((hx_gold.data())[i] - (hx.data())[i]) / (hx_gold.data())[i];
        relative_error       = relative_error >= 0 ? relative_error : -relative_error;
        max_relative_error
            = relative_error < max_relative_error ? max_relative_error : relative_error;
    }
    float eps       = std::numeric_limits<float>::epsilon();
    float tolerance = 10;

    if(header)
    {
        std::cout << "N,incx,stride_x,batch_count,multiple_count,alpha" << std::endl;
    }
    std::cout << n << ", " << incx << ", " << stride_x << ", " << batch_count << ", "
              << multiple_count << ", " << alpha;

    if(max_relative_error != max_relative_error || max_relative_error > eps * tolerance)
    {
        std::cout << " : FAIL, " << max_relative_error << std::endl;
    }
    else
    {
        std::cout << " : PASS, " << max_relative_error << std::endl;
    }

    host_multiple_strided_batch_vector<float> hx_multiple(
        n, incx, stride_x, batch_count, multiple_count);
    device_multiple_strided_batch_vector<float> dx_multiple(
        n, incx, stride_x, batch_count, multiple_count);

    CHECK_HIP_ERROR(dx_multiple.memcheck());

    CHECK_HIP_ERROR(dx_multiple.broadcast_one_strided_batch_vector_from(hx));

    CHECK_HIP_ERROR(hx_multiple.transfer_from(dx_multiple));
    if(verbose)
    {
        print_multiple_strided_batched_vector("hx_multiple before scal",
                                              hx_multiple,
                                              n,
                                              batch_count,
                                              multiple_count,
                                              incx,
                                              stride_x,
                                              hx_multiple.multiple_stride());
    }

    for(int i = 0; i < multiple_count; i++)
    {
        CHECK_ROCBLAS_ERROR(rocblas_sscal_strided_batched(
            handle, n, &alpha, dx_multiple[i], incx, stride_x, batch_count));
    }

    CHECK_HIP_ERROR(hx_multiple.transfer_from(dx_multiple));

    if(verbose)
    {
        print_multiple_strided_batched_vector("hx_multiple after scal",
                                              hx_multiple,
                                              n,
                                              batch_count,
                                              multiple_count,
                                              incx,
                                              stride_x,
                                              hx_multiple.multiple_stride());
    }

    CHECK_ROCBLAS_ERROR(rocblas_destroy_handle(handle));
    return EXIT_SUCCESS;
}