blob: f6f93dd507f5dd1d40b2895f13dd358122de2ca1 [file] [log] [blame]
Georgios Pinitas421405b2018-10-26 19:05:32 +01001/*
2 * Copyright (c) 2018 ARM Limited.
3 *
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#include "arm_compute/core/Types.h"
25#include "arm_compute/runtime/NEON/NEFunctions.h"
26#include "arm_compute/runtime/NEON/NEScheduler.h"
27#include "utils/Utils.h"
28
29#include <cstdlib>
30
31using namespace arm_compute;
32using namespace utils;
33
34class NESGEMMExample : public Example
35{
36public:
37 bool do_setup(int argc, char **argv) override
38 {
39 NPYLoader npy0, npy1, npy2;
40 alpha = 1.0f;
41 beta = 0.0f;
42
43 std::ifstream stream;
44 if(argc > 1)
45 {
46 stream.open(argv[1], std::fstream::in);
47 }
48
49 if(argc < 3 || (argc < 4 && stream.bad()))
50 {
51 // Print help
52 std::cout << "Usage: 1) ./build/neon_sgemm input_matrix_1.npy input_matrix_2.npy [input_matrix_3.npy] [alpha = 1] [beta = 0]\n";
53 std::cout << " 2) ./build/neon_sgemm M N K [alpha = 1.0f] [beta = 0.0f]\n\n";
54 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";
55
56 src0.allocator()->init(TensorInfo(TensorShape(5U, 7U), 1, DataType::F32));
57 src1.allocator()->init(TensorInfo(TensorShape(3U, 5U), 1, DataType::F32));
58 src2.allocator()->init(TensorInfo(TensorShape(3U, 7U), 1, DataType::F32));
59 }
60 else
61 {
62 if(stream.good()) /* case file1.npy file2.npy [file3.npy] [alpha = 1.0f] [beta = 0.0f] */
63 {
64 npy0.open(argv[1]);
65 npy0.init_tensor(src0, DataType::F32);
66 npy1.open(argv[2]);
67 npy1.init_tensor(src1, DataType::F32);
68
69 if(argc > 3)
70 {
71 stream.close();
72 stream.clear();
73 stream.open(argv[3], std::fstream::in);
74 if(stream.good()) /* case with third file */
75 {
76 npy2.open(argv[3]);
77 npy2.init_tensor(src2, DataType::F32);
78
79 if(argc > 4)
80 {
81 // Convert string to float
82 alpha = strtof(argv[4], nullptr);
83
84 if(argc > 5)
85 {
86 // Convert string to float
87 beta = strtof(argv[5], nullptr);
88 }
89 }
90 }
91 else /* case without third file */
92 {
93 alpha = strtof(argv[3], nullptr);
94
95 if(argc > 4)
96 {
97 beta = strtof(argv[4], nullptr);
98 }
99 }
100 }
101 }
102 else /* case M N K [alpha = 1.0f] [beta = 0.0f] */
103 {
104 size_t M = strtol(argv[1], nullptr, 10);
105 size_t N = strtol(argv[2], nullptr, 10);
106 size_t K = strtol(argv[3], nullptr, 10);
107
108 src0.allocator()->init(TensorInfo(TensorShape(K, M), 1, DataType::F32));
109 src1.allocator()->init(TensorInfo(TensorShape(N, K), 1, DataType::F32));
110 src2.allocator()->init(TensorInfo(TensorShape(N, M), 1, DataType::F32));
111
112 if(argc > 4)
113 {
114 alpha = strtof(argv[4], nullptr);
115
116 if(argc > 5)
117 {
118 beta = strtof(argv[5], nullptr);
119 }
120 }
121 }
122 }
123
124 init_sgemm_output(dst, src0, src1, DataType::F32);
125
126 // Configure function
127 sgemm.configure(&src0, &src1, nullptr, &dst, alpha, beta);
128
129 // Allocate all the images
130 src0.allocator()->allocate();
131 src1.allocator()->allocate();
132 dst.allocator()->allocate();
133
134 // Fill the input images with either the data provided or random data
135 if(npy0.is_open())
136 {
137 npy0.fill_tensor(src0);
138 npy1.fill_tensor(src1);
139
140 output_filename = "sgemm_out.npy";
141 is_fortran = npy0.is_fortran();
142
143 if(npy2.is_open())
144 {
145 src2.allocator()->allocate();
146 npy2.fill_tensor(src2);
147 }
148 }
149 else
150 {
151 src2.allocator()->allocate();
152
153 fill_random_tensor(src0, -1.f, 1.f);
154 fill_random_tensor(src1, -1.f, 1.f);
155 fill_random_tensor(src2, -1.f, 1.f);
156 }
157
158 // Dummy run for CLTuner
159 sgemm.run();
160
161 return true;
162 }
163 void do_run() override
164 {
165 // Execute the function
166 sgemm.run();
167 }
168 void do_teardown() override
169 {
170 if(!output_filename.empty()) /* Save to .npy file */
171 {
172 save_to_npy(dst, output_filename, is_fortran);
173 }
174 }
175
176private:
177 Tensor src0{}, src1{}, src2{}, dst{};
178 NEGEMM sgemm{};
179 float alpha{}, beta{};
180 bool is_fortran{};
181 std::string output_filename{};
182};
183
184/** Main program for sgemm test
185 *
186 * @param[in] argc Number of arguments
187 * @param[in] argv Arguments ( [optional] Matrix A, [optional] Matrix B, [optional] Matrix C, [optional] alpha, [optional] beta )
188 */
189int main(int argc, char **argv)
190{
191 return utils::run_example<NESGEMMExample>(argc, argv);
192}