blob: 27af22895472886097c8c57d5c04388f2948804c [file] [log] [blame]
Giorgio Arenacf3935f2017-10-26 17:14:13 +01001/*
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +01002 * Copyright (c) 2017-2020 Arm Limited.
Giorgio Arenacf3935f2017-10-26 17:14:13 +01003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#ifndef ARM_COMPUTE_CL /* Needed by Utils.cpp to handle OpenCL exceptions properly */
25#error "This example needs to be built with -DARM_COMPUTE_CL"
26#endif /* ARM_COMPUTE_CL */
27
28#include "arm_compute/core/Types.h"
Giorgio Arenacf3935f2017-10-26 17:14:13 +010029#include "arm_compute/runtime/CL/CLScheduler.h"
30#include "arm_compute/runtime/CL/CLTuner.h"
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010031#include "arm_compute/runtime/CL/functions/CLGEMM.h"
Giorgio Arenacf3935f2017-10-26 17:14:13 +010032#include "utils/Utils.h"
33
Gian Marco0bc5a252017-12-04 13:55:08 +000034#include <cstdlib>
35
Giorgio Arenacf3935f2017-10-26 17:14:13 +010036using namespace arm_compute;
37using namespace utils;
38
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000039class CLSGEMMExample : public Example
Giorgio Arenacf3935f2017-10-26 17:14:13 +010040{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000041public:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010042 bool do_setup(int argc, char **argv) override
Giorgio Arenacf3935f2017-10-26 17:14:13 +010043 {
Michalis Spyroua4f378d2019-04-26 14:54:54 +010044 NPYLoader npy0;
45 NPYLoader npy1;
46 NPYLoader npy2;
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000047 alpha = 1.0f;
48 beta = 0.0f;
Giorgio Arenacf3935f2017-10-26 17:14:13 +010049
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000050 CLScheduler::get().default_init(&tuner);
Giorgio Arenacf3935f2017-10-26 17:14:13 +010051
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000052 std::ifstream stream;
53 if(argc > 1)
Giorgio Arenacf3935f2017-10-26 17:14:13 +010054 {
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000055 stream.open(argv[1], std::fstream::in);
56 }
Giorgio Arenacf3935f2017-10-26 17:14:13 +010057
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000058 if(argc < 3 || (argc < 4 && stream.bad()))
59 {
60 // Print help
61 std::cout << "Usage: 1) ./build/cl_sgemm input_matrix_1.npy input_matrix_2.npy [input_matrix_3.npy] [alpha = 1] [beta = 0]\n";
62 std::cout << " 2) ./build/cl_sgemm M N K [alpha = 1.0f] [beta = 0.0f]\n\n";
63 std::cout << "Too few or no input_matrices provided. Using M=7, N=3, K=5, alpha=1.0f and beta=0.0f\n\n";
64
65 src0.allocator()->init(TensorInfo(TensorShape(5U, 7U), 1, DataType::F32));
66 src1.allocator()->init(TensorInfo(TensorShape(3U, 5U), 1, DataType::F32));
67 src2.allocator()->init(TensorInfo(TensorShape(3U, 7U), 1, DataType::F32));
68 }
69 else
70 {
71 if(stream.good()) /* case file1.npy file2.npy [file3.npy] [alpha = 1.0f] [beta = 0.0f] */
Giorgio Arenacf3935f2017-10-26 17:14:13 +010072 {
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000073 npy0.open(argv[1]);
74 npy0.init_tensor(src0, DataType::F32);
75 npy1.open(argv[2]);
76 npy1.init_tensor(src1, DataType::F32);
77
78 if(argc > 3)
Giorgio Arenacf3935f2017-10-26 17:14:13 +010079 {
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000080 stream.close();
81 stream.clear();
82 stream.open(argv[3], std::fstream::in);
83 if(stream.good()) /* case with third file */
Giorgio Arenacf3935f2017-10-26 17:14:13 +010084 {
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000085 npy2.open(argv[3]);
86 npy2.init_tensor(src2, DataType::F32);
Giorgio Arenacf3935f2017-10-26 17:14:13 +010087
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000088 if(argc > 4)
Giorgio Arenacf3935f2017-10-26 17:14:13 +010089 {
Gian Marco0bc5a252017-12-04 13:55:08 +000090 // Convert string to float
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000091 alpha = strtof(argv[4], nullptr);
92
93 if(argc > 5)
94 {
95 // Convert string to float
96 beta = strtof(argv[5], nullptr);
97 }
98 }
99 }
100 else /* case without third file */
101 {
102 alpha = strtof(argv[3], nullptr);
103
104 if(argc > 4)
105 {
106 beta = strtof(argv[4], nullptr);
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100107 }
108 }
109 }
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000110 }
111 else /* case M N K [alpha = 1.0f] [beta = 0.0f] */
112 {
113 size_t M = strtol(argv[1], nullptr, 10);
114 size_t N = strtol(argv[2], nullptr, 10);
115 size_t K = strtol(argv[3], nullptr, 10);
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100116
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000117 src0.allocator()->init(TensorInfo(TensorShape(K, M), 1, DataType::F32));
118 src1.allocator()->init(TensorInfo(TensorShape(N, K), 1, DataType::F32));
119 src2.allocator()->init(TensorInfo(TensorShape(N, M), 1, DataType::F32));
120
121 if(argc > 4)
122 {
123 alpha = strtof(argv[4], nullptr);
124
125 if(argc > 5)
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100126 {
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000127 beta = strtof(argv[5], nullptr);
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100128 }
129 }
130 }
131 }
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000132
133 init_sgemm_output(dst, src0, src1, DataType::F32);
134
135 // Configure function
136 sgemm.configure(&src0, &src1, (src2.info()->total_size() > 0) ? &src2 : nullptr, &dst, alpha, beta);
137
138 // Allocate all the images
139 src0.allocator()->allocate();
140 src1.allocator()->allocate();
141 dst.allocator()->allocate();
142
143 // Fill the input images with either the data provided or random data
144 if(npy0.is_open())
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100145 {
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000146 npy0.fill_tensor(src0);
147 npy1.fill_tensor(src1);
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100148
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000149 output_filename = "sgemm_out.npy";
150 is_fortran = npy0.is_fortran();
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100151
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000152 if(npy2.is_open())
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100153 {
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000154 src2.allocator()->allocate();
155 npy2.fill_tensor(src2);
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100156 }
157 }
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000158 else
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100159 {
160 src2.allocator()->allocate();
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000161
162 fill_random_tensor(src0, -1.f, 1.f);
163 fill_random_tensor(src1, -1.f, 1.f);
164 fill_random_tensor(src2, -1.f, 1.f);
165 }
166
167 // Dummy run for CLTuner
168 sgemm.run();
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100169
170 return true;
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000171 }
172 void do_run() override
173 {
174 // Execute the function
175 sgemm.run();
176
177 // Make sure all the OpenCL jobs are done executing:
178 CLScheduler::get().sync();
179 }
180 void do_teardown() override
181 {
Gian Marco85e6f512018-02-01 16:57:48 +0000182 if(!output_filename.empty()) /* Save to .npy file */
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000183 {
184 save_to_npy(dst, output_filename, is_fortran);
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100185 }
186 }
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100187
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000188private:
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100189 CLTensor src0{};
190 CLTensor src1{};
191 CLTensor src2{};
192 CLTensor dst{};
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000193 CLGEMM sgemm{};
194 CLTuner tuner{};
195 float alpha{}, beta{};
196 bool is_fortran{};
197 std::string output_filename{};
198};
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100199
200/** Main program for sgemm test
201 *
202 * @param[in] argc Number of arguments
203 * @param[in] argv Arguments ( [optional] Matrix A, [optional] Matrix B, [optional] Matrix C, [optional] alpha, [optional] beta )
204 */
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000205int main(int argc, char **argv)
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100206{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000207 return utils::run_example<CLSGEMMExample>(argc, argv);
Gian Marcoae2af742018-02-15 12:35:44 +0000208}