aboutsummaryrefslogtreecommitdiff
path: root/examples/cl_sgemm.cpp
blob: 8808f7ebf52924b71531f2a181720a28e643a509 (plain)
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
/*
 * Copyright (c) 2017 ARM Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * 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.
 */
#ifndef ARM_COMPUTE_CL /* Needed by Utils.cpp to handle OpenCL exceptions properly */
#error "This example needs to be built with -DARM_COMPUTE_CL"
#endif /* ARM_COMPUTE_CL */

#include "arm_compute/core/Types.h"
#include "arm_compute/runtime/CL/CLFunctions.h"
#include "arm_compute/runtime/CL/CLScheduler.h"
#include "arm_compute/runtime/CL/CLTuner.h"
#include "utils/Utils.h"

using namespace arm_compute;
using namespace utils;

void main_cl_sgemm(int argc, const char **argv)
{
    NPYLoader npy0, npy1, npy2;
    CLImage   src0, src1, src2, dst;
    int       alpha = 1, beta = 0;

    CLTuner tuner;
    CLScheduler::get().default_init(&tuner);

    std::ifstream stream;
    if(argc > 1)
    {
        stream.open(argv[1], std::fstream::in);
    }

    if(argc < 3 || (argc < 4 && stream.bad()))
    {
        // Print help
        std::cout << "Usage: 1) ./build/cl_sgemm input_matrix_1.npy input_matrix_2.npy [input_matrix_3.npy] [alpha = 1] [beta = 0]\n";
        std::cout << "       2) ./build/cl_sgemm M N K [alpha = 1] [beta = 0]\n\n";
        std::cout << "Too few or no input_matrices provided, creating random 5x7, 3x5 and 3x7 matrices\n\n";

        src0.allocator()->init(TensorInfo(5, 7, Format::F32));
        src1.allocator()->init(TensorInfo(3, 5, Format::F32));
        src2.allocator()->init(TensorInfo(3, 7, Format::F32));
    }
    else
    {
        if(stream.good()) /* case file1.npy file2.npy [file3.npy] [alpha = 1] [beta = 0] */
        {
            npy0.open(argv[1]);
            npy0.init_tensor(src0, Format::F32);
            npy1.open(argv[2]);
            npy1.init_tensor(src1, Format::F32);

            if(argc > 3)
            {
                stream.close();
                stream.clear();
                stream.open(argv[3], std::fstream::in);
                if(stream.good()) /* case with third file */
                {
                    npy2.open(argv[3]);
                    npy2.init_tensor(src2, Format::F32);

                    if(argc > 4)
                    {
                        alpha = strtol(argv[4], nullptr, 10);

                        if(argc > 5)
                        {
                            beta = strtol(argv[5], nullptr, 10);
                        }
                    }
                }
                else /* case without third file */
                {
                    alpha = strtol(argv[3], nullptr, 10);

                    if(argc > 4)
                    {
                        beta = strtol(argv[4], nullptr, 10);
                    }
                }
            }
        }
        else /* case M N K [alpha = 1] [beta = 0] */
        {
            size_t M = strtol(argv[1], nullptr, 10);
            size_t N = strtol(argv[2], nullptr, 10);
            size_t K = strtol(argv[3], nullptr, 10);

            src0.allocator()->init(TensorInfo(K, M, Format::F32));
            src1.allocator()->init(TensorInfo(N, K, Format::F32));
            src2.allocator()->init(TensorInfo(N, M, Format::F32));

            if(argc > 4)
            {
                alpha = strtol(argv[4], nullptr, 10);

                if(argc > 5)
                {
                    beta = strtol(argv[5], nullptr, 10);
                }
            }
        }
    }

    init_sgemm_output(dst, src0, src1, Format::F32);

    // Configure function
    CLGEMM sgemm;
    sgemm.configure(&src0, &src1, (src2.info()->total_size() > 0) ? &src2 : nullptr, &dst, alpha, beta);

    // Allocate all the images
    src0.allocator()->allocate();
    src1.allocator()->allocate();
    dst.allocator()->allocate();

    // Fill the input images with either the data provided or random data
    if(npy0.is_open())
    {
        npy0.fill_tensor(src0);
        npy1.fill_tensor(src1);

        if(npy2.is_open())
        {
            src2.allocator()->allocate();
            npy2.fill_tensor(src2);
        }
    }
    else
    {
        src2.allocator()->allocate();

        fill_random_tensor(src0, -1.f, 1.f);
        fill_random_tensor(src1, -1.f, 1.f);
        fill_random_tensor(src2, -1.f, 1.f);
    }

    // Dummy run for CLTuner
    sgemm.run();

    auto start = std::chrono::high_resolution_clock::now();

    // Execute the function
    sgemm.run();

    // Make sure all the OpenCL jobs are done executing:
    CLScheduler::get().sync();

    auto stop = std::chrono::high_resolution_clock::now();

    if(!npy0.is_open()) /* If the inputs were not files, print the results */
    {
        std::cout << "\nMatrix 1:" << std::endl;
        src0.map(true);
        src0.print(std::cout, IOFormatInfo());
        src0.unmap();

        std::cout << "Matrix 2:" << std::endl;
        src1.map(true);
        src1.print(std::cout, IOFormatInfo());
        src1.unmap();

        std::cout << "Matrix 3:" << std::endl;
        src2.map(true);
        src2.print(std::cout, IOFormatInfo());
        src2.unmap();

        std::cout << "Alpha:" << alpha << "\n\n";
        std::cout << "Beta:" << beta << "\n\n";

        std::cout << "Output Matrix:" << std::endl;
        dst.map(true);
        dst.print(std::cout, IOFormatInfo());
        dst.unmap();
    }
    else /* Save to .npy file */
    {
        save_to_npy(dst, "sgemm_out.npy", npy0.is_fortran());
    }

    auto delta = std::chrono::duration_cast<std::chrono::microseconds>(stop - start);
    std::cout << "Time elapsed: " << delta.count() << "us." << std::endl;
}

/** Main program for sgemm test
 *
 * @param[in] argc Number of arguments
 * @param[in] argv Arguments ( [optional] Matrix A, [optional] Matrix B, [optional] Matrix C, [optional] alpha, [optional] beta )
 */
int main(int argc, const char **argv)
{
    return utils::run_example(argc, argv, main_cl_sgemm);
}