blob: 805aec1cf338fdedb79ddc3067b6341d171c5bd7 [file] [log] [blame]
Anthony Barbier8140e1e2017-12-14 23:48:46 +00001/*
Anthony Barbier06ea0482018-02-22 15:45:35 +00002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbier8140e1e2017-12-14 23:48:46 +00003 *
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"
29#include "arm_compute/runtime/CL/CLFunctions.h"
30#include "arm_compute/runtime/CL/CLScheduler.h"
31#include "arm_compute/runtime/CL/CLTuner.h"
32#include "utils/Utils.h"
33
34#include <cstdlib>
35
36using namespace arm_compute;
37using namespace utils;
38
Anthony Barbierf45d5a92018-01-24 16:23:15 +000039class CLSGEMMExample : public Example
Anthony Barbier8140e1e2017-12-14 23:48:46 +000040{
Anthony Barbierf45d5a92018-01-24 16:23:15 +000041public:
Jenkins52ba29e2018-08-29 15:32:11 +000042 bool do_setup(int argc, char **argv) override
Anthony Barbier8140e1e2017-12-14 23:48:46 +000043 {
Anthony Barbierf45d5a92018-01-24 16:23:15 +000044 NPYLoader npy0, npy1, npy2;
45 alpha = 1.0f;
46 beta = 0.0f;
Anthony Barbier8140e1e2017-12-14 23:48:46 +000047
Anthony Barbierf45d5a92018-01-24 16:23:15 +000048 CLScheduler::get().default_init(&tuner);
Anthony Barbier8140e1e2017-12-14 23:48:46 +000049
Anthony Barbierf45d5a92018-01-24 16:23:15 +000050 std::ifstream stream;
51 if(argc > 1)
Anthony Barbier8140e1e2017-12-14 23:48:46 +000052 {
Anthony Barbierf45d5a92018-01-24 16:23:15 +000053 stream.open(argv[1], std::fstream::in);
54 }
Anthony Barbier8140e1e2017-12-14 23:48:46 +000055
Anthony Barbierf45d5a92018-01-24 16:23:15 +000056 if(argc < 3 || (argc < 4 && stream.bad()))
57 {
58 // Print help
59 std::cout << "Usage: 1) ./build/cl_sgemm input_matrix_1.npy input_matrix_2.npy [input_matrix_3.npy] [alpha = 1] [beta = 0]\n";
60 std::cout << " 2) ./build/cl_sgemm M N K [alpha = 1.0f] [beta = 0.0f]\n\n";
61 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";
62
63 src0.allocator()->init(TensorInfo(TensorShape(5U, 7U), 1, DataType::F32));
64 src1.allocator()->init(TensorInfo(TensorShape(3U, 5U), 1, DataType::F32));
65 src2.allocator()->init(TensorInfo(TensorShape(3U, 7U), 1, DataType::F32));
66 }
67 else
68 {
69 if(stream.good()) /* case file1.npy file2.npy [file3.npy] [alpha = 1.0f] [beta = 0.0f] */
Anthony Barbier8140e1e2017-12-14 23:48:46 +000070 {
Anthony Barbierf45d5a92018-01-24 16:23:15 +000071 npy0.open(argv[1]);
72 npy0.init_tensor(src0, DataType::F32);
73 npy1.open(argv[2]);
74 npy1.init_tensor(src1, DataType::F32);
75
76 if(argc > 3)
Anthony Barbier8140e1e2017-12-14 23:48:46 +000077 {
Anthony Barbierf45d5a92018-01-24 16:23:15 +000078 stream.close();
79 stream.clear();
80 stream.open(argv[3], std::fstream::in);
81 if(stream.good()) /* case with third file */
Anthony Barbier8140e1e2017-12-14 23:48:46 +000082 {
Anthony Barbierf45d5a92018-01-24 16:23:15 +000083 npy2.open(argv[3]);
84 npy2.init_tensor(src2, DataType::F32);
Anthony Barbier8140e1e2017-12-14 23:48:46 +000085
Anthony Barbierf45d5a92018-01-24 16:23:15 +000086 if(argc > 4)
Anthony Barbier8140e1e2017-12-14 23:48:46 +000087 {
88 // Convert string to float
Anthony Barbierf45d5a92018-01-24 16:23:15 +000089 alpha = strtof(argv[4], nullptr);
90
91 if(argc > 5)
92 {
93 // Convert string to float
94 beta = strtof(argv[5], nullptr);
95 }
96 }
97 }
98 else /* case without third file */
99 {
100 alpha = strtof(argv[3], nullptr);
101
102 if(argc > 4)
103 {
104 beta = strtof(argv[4], nullptr);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000105 }
106 }
107 }
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000108 }
109 else /* case M N K [alpha = 1.0f] [beta = 0.0f] */
110 {
111 size_t M = strtol(argv[1], nullptr, 10);
112 size_t N = strtol(argv[2], nullptr, 10);
113 size_t K = strtol(argv[3], nullptr, 10);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000114
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000115 src0.allocator()->init(TensorInfo(TensorShape(K, M), 1, DataType::F32));
116 src1.allocator()->init(TensorInfo(TensorShape(N, K), 1, DataType::F32));
117 src2.allocator()->init(TensorInfo(TensorShape(N, M), 1, DataType::F32));
118
119 if(argc > 4)
120 {
121 alpha = strtof(argv[4], nullptr);
122
123 if(argc > 5)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000124 {
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000125 beta = strtof(argv[5], nullptr);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000126 }
127 }
128 }
129 }
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000130
131 init_sgemm_output(dst, src0, src1, DataType::F32);
132
133 // Configure function
134 sgemm.configure(&src0, &src1, (src2.info()->total_size() > 0) ? &src2 : nullptr, &dst, alpha, beta);
135
136 // Allocate all the images
137 src0.allocator()->allocate();
138 src1.allocator()->allocate();
139 dst.allocator()->allocate();
140
141 // Fill the input images with either the data provided or random data
142 if(npy0.is_open())
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000143 {
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000144 npy0.fill_tensor(src0);
145 npy1.fill_tensor(src1);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000146
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000147 output_filename = "sgemm_out.npy";
148 is_fortran = npy0.is_fortran();
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000149
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000150 if(npy2.is_open())
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000151 {
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000152 src2.allocator()->allocate();
153 npy2.fill_tensor(src2);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000154 }
155 }
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000156 else
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000157 {
158 src2.allocator()->allocate();
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000159
160 fill_random_tensor(src0, -1.f, 1.f);
161 fill_random_tensor(src1, -1.f, 1.f);
162 fill_random_tensor(src2, -1.f, 1.f);
163 }
164
165 // Dummy run for CLTuner
166 sgemm.run();
Jenkins52ba29e2018-08-29 15:32:11 +0000167
168 return true;
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000169 }
170 void do_run() override
171 {
172 // Execute the function
173 sgemm.run();
174
175 // Make sure all the OpenCL jobs are done executing:
176 CLScheduler::get().sync();
177 }
178 void do_teardown() override
179 {
Anthony Barbier06ea0482018-02-22 15:45:35 +0000180 if(!output_filename.empty()) /* Save to .npy file */
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000181 {
182 save_to_npy(dst, output_filename, is_fortran);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000183 }
184 }
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000185
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000186private:
187 CLTensor src0{}, src1{}, src2{}, dst{};
188 CLGEMM sgemm{};
189 CLTuner tuner{};
190 float alpha{}, beta{};
191 bool is_fortran{};
192 std::string output_filename{};
193};
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000194
195/** Main program for sgemm test
196 *
197 * @param[in] argc Number of arguments
198 * @param[in] argv Arguments ( [optional] Matrix A, [optional] Matrix B, [optional] Matrix C, [optional] alpha, [optional] beta )
199 */
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000200int main(int argc, char **argv)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000201{
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000202 return utils::run_example<CLSGEMMExample>(argc, argv);
Jenkinsb3a371b2018-05-23 11:36:53 +0100203}