File: example_gemv_graph_capture.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 (182 lines) | stat: -rw-r--r-- 6,493 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
/* ************************************************************************
 * 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 <hip/hip_runtime.h>
#include <rocblas/rocblas.h>

const int NUM_ITER = 1000;

double maxRelativeError(std::vector<double>& A, std::vector<double>& reference)
{
    double maxRelativeError = double(std::numeric_limits<double>::min());

    size_t n = A.size();
    for(size_t i = 0; i < n; ++i)
    {
        double gold          = double(reference[i]);
        double relativeError = gold != 0 ? (gold - double(A[i])) / (gold) : double(A[i]);
        relativeError        = relativeError > 0 ? relativeError : -relativeError;
        maxRelativeError     = relativeError < maxRelativeError ? maxRelativeError : relativeError;
    }
    return maxRelativeError;
}

int main()
{
    rocblas_status rstatus = rocblas_status_success;
    rocblas_int    M       = 8192;
    rocblas_int    N       = 3;
    rocblas_int    incx    = 1;
    rocblas_int    incy    = 1;
    typedef double dataType;

    dataType h_alpha = 1.0;
    dataType h_beta  = 0.0;

    const rocblas_operation trans_A = rocblas_operation_transpose;

    size_t size_x, dim_x, abs_incx;
    size_t size_y, dim_y, abs_incy;

    if(trans_A == rocblas_operation_none)
    {
        dim_x = N;
        dim_y = M;
    }
    else // transpose
    {
        dim_x = M;
        dim_y = N;
    }

    rocblas_int lda    = M;
    size_t      size_A = lda * size_t(dim_x);

    abs_incx = incx >= 0 ? incx : -incx;
    abs_incy = incy >= 0 ? incy : -incy;
    size_x   = dim_x * abs_incx;
    size_y   = dim_y * abs_incy;

    std::vector<dataType> hA(size_A);
    std::vector<dataType> hx(size_x);
    std::vector<dataType> hy(size_y, 0);

    // initial data on host
    srand(1);
    for(int i = 0; i < size_A; ++i)
    {
        hA[i] = rand() % 17;
    }
    for(int i = 0; i < size_x; ++i)
    {
        hx[i] = rand() % 17;
    }

    std::vector<dataType> hy_gold = hy;

    // using rocblas API
    rocblas_handle handle;
    CHECK_ROCBLAS_ERROR(rocblas_create_handle(&handle));

    hipStream_t stream_1, stream_2;
    CHECK_HIP_ERROR(hipStreamCreate(&stream_1));
    CHECK_HIP_ERROR(hipStreamCreate(&stream_2));

    bool           graph_captured = false;
    hipGraph_t     graph;
    hipGraphExec_t graph_exec;

    // allocate memory on device
    dataType *dA, *dX, *dY;
    CHECK_HIP_ERROR(hipMalloc(&dA, size_A * sizeof(dataType)));
    CHECK_HIP_ERROR(hipMalloc(&dX, size_x * sizeof(dataType)));
    CHECK_HIP_ERROR(hipMalloc(&dY, size_y * sizeof(dataType)));

    // copy data from CPU to device
    CHECK_HIP_ERROR(hipMemcpy(dA, hA.data(), sizeof(dataType) * size_A, hipMemcpyHostToDevice));
    CHECK_HIP_ERROR(hipMemcpy(dX, hx.data(), sizeof(dataType) * size_x, hipMemcpyHostToDevice));
    CHECK_HIP_ERROR(hipMemcpy(dY, hy.data(), sizeof(dataType) * size_y, hipMemcpyHostToDevice));

    // enable passing alpha and beta parameters from pointer to host memory
    CHECK_ROCBLAS_ERROR(rocblas_set_pointer_mode(handle, rocblas_pointer_mode_host));

    CHECK_ROCBLAS_ERROR(rocblas_set_stream(handle, stream_1));

    for(auto i = 0; i < NUM_ITER; i++)
    {
        if(!graph_captured)
        {
            CHECK_HIP_ERROR(hipStreamBeginCapture(stream_1, hipStreamCaptureModeGlobal));
            // asynchronous calculation on device, returns before finished calculations
            CHECK_ROCBLAS_ERROR(rocblas_dgemv(
                handle, trans_A, M, N, &h_alpha, dA, lda, dX, incx, &h_beta, dY, incy));
            CHECK_HIP_ERROR(hipStreamEndCapture(stream_1, &graph));
            CHECK_HIP_ERROR(hipGraphInstantiate(&graph_exec, graph, NULL, NULL, 0));
            CHECK_HIP_ERROR(hipGraphDestroy(graph));
            graph_captured = true;
        }
        CHECK_HIP_ERROR(
            hipGraphLaunch(graph_exec, stream_2)); //Launching the graph in a different stream.
    }

    // fetch device memory results, automatically blocked until results ready
    CHECK_HIP_ERROR(hipMemcpy(hy.data(), dY, sizeof(dataType) * size_y, hipMemcpyDeviceToHost));

    rocblas_cout << "M, N, lda = " << M << ", " << N << ", " << lda << std::endl;

    // calculate expected result using CPU
    for(size_t j = 0; j < size_y; j++)
    {
        for(size_t i = j * M, k = 0; k < M; k++, i++)
        {
            hy_gold[j] += h_alpha * hA[i] * hx[k] + h_beta * hy_gold[j];
        }
    }

    auto     maxRelativeErr = maxRelativeError(hy, hy_gold);
    dataType eps            = std::numeric_limits<dataType>::epsilon();
    dataType tolerance      = 10;
    if(maxRelativeErr > eps * tolerance)
    {
        std::cout << "FAIL";
    }
    else
    {
        std::cout << "PASS";
    }
    std::cout << ": max. relative err. = " << maxRelativeErr << std::endl;

    CHECK_HIP_ERROR(hipGraphExecDestroy(graph_exec));

    /* CAUTION: Do not destroy streams before destroying the rocblas_handle.
       rocblas_handle requires stream parameter to free allocated device memory.*/
    CHECK_ROCBLAS_ERROR(rocblas_destroy_handle(handle));

    CHECK_HIP_ERROR(hipStreamDestroy(stream_1));
    CHECK_HIP_ERROR(hipStreamDestroy(stream_2));

    CHECK_HIP_ERROR(hipFree(dA));
    CHECK_HIP_ERROR(hipFree(dX));
    CHECK_HIP_ERROR(hipFree(dY));

    return EXIT_SUCCESS;
}