aboutsummaryrefslogtreecommitdiff
path: root/examples/cl_sgemm.cpp
diff options
context:
space:
mode:
authorMichalis Spyrou <michalis.spyrou@arm.com>2018-01-10 14:08:50 +0000
committerAnthony Barbier <anthony.barbier@arm.com>2018-11-02 16:43:10 +0000
commit2b5f0f2574551f59970bb9d710bafad2bc4bbd4a (patch)
treefd586f56b1285f0d6c52ecefc174eba0a9c8f157 /examples/cl_sgemm.cpp
parent571b18a1fca4a5ed4dd24a38cb619f4de43ba3ed (diff)
downloadComputeLibrary-2b5f0f2574551f59970bb9d710bafad2bc4bbd4a.tar.gz
COMPMID-782 Port examples to the new format
Change-Id: Ib178a97c080ff650094d02ee49e2a0aa22376dd0 Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/115717 Reviewed-by: Anthony Barbier <anthony.barbier@arm.com> Tested-by: Jenkins <bsgcomp@arm.com>
Diffstat (limited to 'examples/cl_sgemm.cpp')
-rw-r--r--examples/cl_sgemm.cpp272
1 files changed, 141 insertions, 131 deletions
diff --git a/examples/cl_sgemm.cpp b/examples/cl_sgemm.cpp
index 939870fabf..f2c63985f6 100644
--- a/examples/cl_sgemm.cpp
+++ b/examples/cl_sgemm.cpp
@@ -36,174 +36,184 @@
using namespace arm_compute;
using namespace utils;
-void main_cl_sgemm(int argc, char **argv)
+class CLSGEMMExample : public Example
{
- NPYLoader npy0, npy1, npy2;
- CLTensor src0, src1, src2, dst;
- float alpha = 1.0f, beta = 0.0f;
-
- CLTuner tuner;
- CLScheduler::get().default_init(&tuner);
-
- std::ifstream stream;
- if(argc > 1)
+public:
+ void do_setup(int argc, char **argv) override
{
- stream.open(argv[1], std::fstream::in);
- }
+ NPYLoader npy0, npy1, npy2;
+ alpha = 1.0f;
+ beta = 0.0f;
- 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.0f] [beta = 0.0f]\n\n";
- 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";
-
- src0.allocator()->init(TensorInfo(TensorShape(5U, 7U), 1, DataType::F32));
- src1.allocator()->init(TensorInfo(TensorShape(3U, 5U), 1, DataType::F32));
- src2.allocator()->init(TensorInfo(TensorShape(3U, 7U), 1, DataType::F32));
- }
- else
- {
- if(stream.good()) /* case file1.npy file2.npy [file3.npy] [alpha = 1.0f] [beta = 0.0f] */
+ CLScheduler::get().default_init(&tuner);
+
+ std::ifstream stream;
+ if(argc > 1)
{
- npy0.open(argv[1]);
- npy0.init_tensor(src0, DataType::F32);
- npy1.open(argv[2]);
- npy1.init_tensor(src1, DataType::F32);
+ stream.open(argv[1], std::fstream::in);
+ }
- if(argc > 3)
+ 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.0f] [beta = 0.0f]\n\n";
+ 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";
+
+ src0.allocator()->init(TensorInfo(TensorShape(5U, 7U), 1, DataType::F32));
+ src1.allocator()->init(TensorInfo(TensorShape(3U, 5U), 1, DataType::F32));
+ src2.allocator()->init(TensorInfo(TensorShape(3U, 7U), 1, DataType::F32));
+ }
+ else
+ {
+ if(stream.good()) /* case file1.npy file2.npy [file3.npy] [alpha = 1.0f] [beta = 0.0f] */
{
- 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, DataType::F32);
+ npy0.open(argv[1]);
+ npy0.init_tensor(src0, DataType::F32);
+ npy1.open(argv[2]);
+ npy1.init_tensor(src1, DataType::F32);
- if(argc > 4)
+ if(argc > 3)
+ {
+ stream.close();
+ stream.clear();
+ stream.open(argv[3], std::fstream::in);
+ if(stream.good()) /* case with third file */
{
- // Convert string to float
- alpha = strtof(argv[4], nullptr);
+ npy2.open(argv[3]);
+ npy2.init_tensor(src2, DataType::F32);
- if(argc > 5)
+ if(argc > 4)
{
// Convert string to float
- beta = strtof(argv[5], nullptr);
+ alpha = strtof(argv[4], nullptr);
+
+ if(argc > 5)
+ {
+ // Convert string to float
+ beta = strtof(argv[5], nullptr);
+ }
}
}
- }
- else /* case without third file */
- {
- alpha = strtof(argv[3], nullptr);
-
- if(argc > 4)
+ else /* case without third file */
{
- beta = strtof(argv[4], nullptr);
+ alpha = strtof(argv[3], nullptr);
+
+ if(argc > 4)
+ {
+ beta = strtof(argv[4], nullptr);
+ }
}
}
}
- }
- else /* case M N K [alpha = 1.0f] [beta = 0.0f] */
- {
- 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(TensorShape(K, M), 1, DataType::F32));
- src1.allocator()->init(TensorInfo(TensorShape(N, K), 1, DataType::F32));
- src2.allocator()->init(TensorInfo(TensorShape(N, M), 1, DataType::F32));
-
- if(argc > 4)
+ else /* case M N K [alpha = 1.0f] [beta = 0.0f] */
{
- alpha = strtof(argv[4], nullptr);
+ 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(TensorShape(K, M), 1, DataType::F32));
+ src1.allocator()->init(TensorInfo(TensorShape(N, K), 1, DataType::F32));
+ src2.allocator()->init(TensorInfo(TensorShape(N, M), 1, DataType::F32));
- if(argc > 5)
+ if(argc > 4)
{
- beta = strtof(argv[5], nullptr);
+ alpha = strtof(argv[4], nullptr);
+
+ if(argc > 5)
+ {
+ beta = strtof(argv[5], nullptr);
+ }
}
}
}
- }
- init_sgemm_output(dst, src0, src1, DataType::F32);
+ init_sgemm_output(dst, src0, src1, DataType::F32);
- // Configure function
- CLGEMM sgemm;
- sgemm.configure(&src0, &src1, (src2.info()->total_size() > 0) ? &src2 : nullptr, &dst, alpha, beta);
+ // Configure function
+ 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();
+ // 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);
+ // Fill the input images with either the data provided or random data
+ if(npy0.is_open())
+ {
+ npy0.fill_tensor(src0);
+ npy1.fill_tensor(src1);
+
+ output_filename = "sgemm_out.npy";
+ is_fortran = npy0.is_fortran();
- if(npy2.is_open())
+ if(npy2.is_open())
+ {
+ src2.allocator()->allocate();
+ npy2.fill_tensor(src2);
+ }
+ }
+ else
{
src2.allocator()->allocate();
- npy2.fill_tensor(src2);
+
+ fill_random_tensor(src0, -1.f, 1.f);
+ fill_random_tensor(src1, -1.f, 1.f);
+ fill_random_tensor(src2, -1.f, 1.f);
}
- }
- 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();
}
-
- // 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 */
+ void do_run() override
{
- 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();
+ // Execute the function
+ sgemm.run();
+
+ // Make sure all the OpenCL jobs are done executing:
+ CLScheduler::get().sync();
}
- else /* Save to .npy file */
+ void do_teardown() override
{
- save_to_npy(dst, "sgemm_out.npy", npy0.is_fortran());
+ if(output_filename.empty()) /* 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, output_filename, is_fortran);
+ }
}
- auto delta = std::chrono::duration_cast<std::chrono::microseconds>(stop - start);
- std::cout << "Time elapsed: " << delta.count() << "us." << std::endl;
-}
+private:
+ CLTensor src0{}, src1{}, src2{}, dst{};
+ CLGEMM sgemm{};
+ CLTuner tuner{};
+ float alpha{}, beta{};
+ bool is_fortran{};
+ std::string output_filename{};
+};
/** Main program for sgemm test
*
@@ -212,5 +222,5 @@ void main_cl_sgemm(int argc, char **argv)
*/
int main(int argc, char **argv)
{
- return utils::run_example(argc, argv, main_cl_sgemm);
+ return utils::run_example<CLSGEMMExample>(argc, argv);
}