blob: 8808f7ebf52924b71531f2a181720a28e643a509 [file] [log] [blame]
Giorgio Arenacf3935f2017-10-26 17:14:13 +01001/*
2 * Copyright (c) 2017 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#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
34using namespace arm_compute;
35using namespace utils;
36
37void main_cl_sgemm(int argc, const char **argv)
38{
39 NPYLoader npy0, npy1, npy2;
40 CLImage src0, src1, src2, dst;
41 int alpha = 1, beta = 0;
42
43 CLTuner tuner;
44 CLScheduler::get().default_init(&tuner);
45
46 std::ifstream stream;
47 if(argc > 1)
48 {
49 stream.open(argv[1], std::fstream::in);
50 }
51
52 if(argc < 3 || (argc < 4 && stream.bad()))
53 {
54 // Print help
55 std::cout << "Usage: 1) ./build/cl_sgemm input_matrix_1.npy input_matrix_2.npy [input_matrix_3.npy] [alpha = 1] [beta = 0]\n";
56 std::cout << " 2) ./build/cl_sgemm M N K [alpha = 1] [beta = 0]\n\n";
57 std::cout << "Too few or no input_matrices provided, creating random 5x7, 3x5 and 3x7 matrices\n\n";
58
59 src0.allocator()->init(TensorInfo(5, 7, Format::F32));
60 src1.allocator()->init(TensorInfo(3, 5, Format::F32));
61 src2.allocator()->init(TensorInfo(3, 7, Format::F32));
62 }
63 else
64 {
65 if(stream.good()) /* case file1.npy file2.npy [file3.npy] [alpha = 1] [beta = 0] */
66 {
67 npy0.open(argv[1]);
68 npy0.init_tensor(src0, Format::F32);
69 npy1.open(argv[2]);
70 npy1.init_tensor(src1, Format::F32);
71
72 if(argc > 3)
73 {
74 stream.close();
75 stream.clear();
76 stream.open(argv[3], std::fstream::in);
77 if(stream.good()) /* case with third file */
78 {
79 npy2.open(argv[3]);
80 npy2.init_tensor(src2, Format::F32);
81
82 if(argc > 4)
83 {
84 alpha = strtol(argv[4], nullptr, 10);
85
86 if(argc > 5)
87 {
88 beta = strtol(argv[5], nullptr, 10);
89 }
90 }
91 }
92 else /* case without third file */
93 {
94 alpha = strtol(argv[3], nullptr, 10);
95
96 if(argc > 4)
97 {
98 beta = strtol(argv[4], nullptr, 10);
99 }
100 }
101 }
102 }
103 else /* case M N K [alpha = 1] [beta = 0] */
104 {
105 size_t M = strtol(argv[1], nullptr, 10);
106 size_t N = strtol(argv[2], nullptr, 10);
107 size_t K = strtol(argv[3], nullptr, 10);
108
109 src0.allocator()->init(TensorInfo(K, M, Format::F32));
110 src1.allocator()->init(TensorInfo(N, K, Format::F32));
111 src2.allocator()->init(TensorInfo(N, M, Format::F32));
112
113 if(argc > 4)
114 {
115 alpha = strtol(argv[4], nullptr, 10);
116
117 if(argc > 5)
118 {
119 beta = strtol(argv[5], nullptr, 10);
120 }
121 }
122 }
123 }
124
125 init_sgemm_output(dst, src0, src1, Format::F32);
126
127 // Configure function
128 CLGEMM sgemm;
129 sgemm.configure(&src0, &src1, (src2.info()->total_size() > 0) ? &src2 : nullptr, &dst, alpha, beta);
130
131 // Allocate all the images
132 src0.allocator()->allocate();
133 src1.allocator()->allocate();
134 dst.allocator()->allocate();
135
136 // Fill the input images with either the data provided or random data
137 if(npy0.is_open())
138 {
139 npy0.fill_tensor(src0);
140 npy1.fill_tensor(src1);
141
142 if(npy2.is_open())
143 {
144 src2.allocator()->allocate();
145 npy2.fill_tensor(src2);
146 }
147 }
148 else
149 {
150 src2.allocator()->allocate();
151
152 fill_random_tensor(src0, -1.f, 1.f);
153 fill_random_tensor(src1, -1.f, 1.f);
154 fill_random_tensor(src2, -1.f, 1.f);
155 }
156
157 // Dummy run for CLTuner
158 sgemm.run();
159
160 auto start = std::chrono::high_resolution_clock::now();
161
162 // Execute the function
163 sgemm.run();
164
165 // Make sure all the OpenCL jobs are done executing:
166 CLScheduler::get().sync();
167
168 auto stop = std::chrono::high_resolution_clock::now();
169
170 if(!npy0.is_open()) /* If the inputs were not files, print the results */
171 {
172 std::cout << "\nMatrix 1:" << std::endl;
173 src0.map(true);
174 src0.print(std::cout, IOFormatInfo());
175 src0.unmap();
176
177 std::cout << "Matrix 2:" << std::endl;
178 src1.map(true);
179 src1.print(std::cout, IOFormatInfo());
180 src1.unmap();
181
182 std::cout << "Matrix 3:" << std::endl;
183 src2.map(true);
184 src2.print(std::cout, IOFormatInfo());
185 src2.unmap();
186
187 std::cout << "Alpha:" << alpha << "\n\n";
188 std::cout << "Beta:" << beta << "\n\n";
189
190 std::cout << "Output Matrix:" << std::endl;
191 dst.map(true);
192 dst.print(std::cout, IOFormatInfo());
193 dst.unmap();
194 }
195 else /* Save to .npy file */
196 {
197 save_to_npy(dst, "sgemm_out.npy", npy0.is_fortran());
198 }
199
200 auto delta = std::chrono::duration_cast<std::chrono::microseconds>(stop - start);
201 std::cout << "Time elapsed: " << delta.count() << "us." << std::endl;
202}
203
204/** Main program for sgemm test
205 *
206 * @param[in] argc Number of arguments
207 * @param[in] argv Arguments ( [optional] Matrix A, [optional] Matrix B, [optional] Matrix C, [optional] alpha, [optional] beta )
208 */
209int main(int argc, const char **argv)
210{
211 return utils::run_example(argc, argv, main_cl_sgemm);
212}