diff options
author | Benoit Jacob <benoitjacob@google.com> | 2015-06-25 15:50:59 -0400 |
---|---|---|
committer | Benoit Jacob <benoitjacob@google.com> | 2015-06-25 15:53:04 -0400 |
commit | 75c4ec0ba4dd86e4f763a54e01002ff29f1d57ae (patch) | |
tree | c8e35a06c7d959e6ad0a90b4929305055919e3f8 /eight_bit_int_gemm | |
download | gemmlowp-75c4ec0ba4dd86e4f763a54e01002ff29f1d57ae.tar.gz |
initial import
Diffstat (limited to 'eight_bit_int_gemm')
-rw-r--r-- | eight_bit_int_gemm/eight_bit_int_gemm.cc | 89 | ||||
-rw-r--r-- | eight_bit_int_gemm/eight_bit_int_gemm.h | 66 |
2 files changed, 155 insertions, 0 deletions
diff --git a/eight_bit_int_gemm/eight_bit_int_gemm.cc b/eight_bit_int_gemm/eight_bit_int_gemm.cc new file mode 100644 index 0000000..4c52dee --- /dev/null +++ b/eight_bit_int_gemm/eight_bit_int_gemm.cc @@ -0,0 +1,89 @@ +// Copyright 2014 Google Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "eight_bit_int_gemm/eight_bit_int_gemm.h" + +// gemmlowp symbols should have hidden visibility. +// currently this is ensured in the build system by +// passing -finlines-visibility-hidden. TODO: it would be +// safer to hardcode it here with some #pragma's. +#include "public/gemmlowp.h" + +namespace gemmlowp { + +namespace eight_bit_int_gemm { + +namespace { + +// To be used as template parameter for GlobalLock. +// GlobalLock<EightBitIntGemmLockId> is the global lock +// on EightBitIntGemm entry points, protecting +// EightBitIntGemm's global state. +struct EightBitIntGemmLockId; + +// Global state: consists of one global GemmContext instance. +GemmContext* global_context; + +GemmContext* GetOrCreateGlobalContext() { + if (!global_context) { + global_context = new GemmContext; + } + return global_context; +} + +void DestroyGlobalContext() { + delete global_context; + global_context = nullptr; +} + +} // end anonymous namespace + +// Public interface entry points + +void EightBitIntGemm(int m, int n, int k, const std::uint8_t* a, + std::int32_t a_offset, int lda, const std::uint8_t* b, + std::int32_t b_offset, int ldb, std::uint8_t* c, + std::int32_t c_offset, std::int32_t c_mult_int, + std::int32_t c_shift, int ldc) { + AutoGlobalLock<EightBitIntGemmLockId> lock; + GemmContext* context = GetOrCreateGlobalContext(); + + MatrixMap<const std::uint8_t, MapOrder::RowMajor> lhs(b, n, k, ldb); + MatrixMap<const std::uint8_t, MapOrder::ColMajor> rhs(a, k, m, lda); + MatrixMap<std::uint8_t, MapOrder::ColMajor> result(c, n, m, ldc); + + const int lhs_offset = b_offset; + const int rhs_offset = a_offset; + const int result_offset = c_offset; + const int result_mult_int = c_mult_int; + const int result_shift = c_shift; + + Gemm(context, lhs, rhs, &result, lhs_offset, rhs_offset, result_offset, + result_mult_int, result_shift); +} + +void SetMaxNumThreads(int n) { + AutoGlobalLock<EightBitIntGemmLockId> lock; + GemmContext* context = GetOrCreateGlobalContext(); + context->set_max_num_threads(n); +} + +void FreePersistentResources() { + AutoGlobalLock<EightBitIntGemmLockId> lock; + DestroyGlobalContext(); +} + +} // namespace eight_bit_int_gemm + +} // namespace gemmlowp diff --git a/eight_bit_int_gemm/eight_bit_int_gemm.h b/eight_bit_int_gemm/eight_bit_int_gemm.h new file mode 100644 index 0000000..1d2b1cb --- /dev/null +++ b/eight_bit_int_gemm/eight_bit_int_gemm.h @@ -0,0 +1,66 @@ +// Copyright 2014 Google Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// eight_bit_int_gemm.h: exposes the standard EightBitIntGemm interface. + +#ifndef GEMMLOWP_EIGHT_BIT_INT_GEMM_EIGHT_BIT_INT_GEMM_H_ +#define GEMMLOWP_EIGHT_BIT_INT_GEMM_EIGHT_BIT_INT_GEMM_H_ + +#include <cstdint> + +namespace gemmlowp { + +namespace eight_bit_int_gemm { + +// Concurrency / reentrancy notice +// =============================== +// +// This eight_bit_int_gemm has global singleton persistent state. +// A global lock ensures serialization of calls, so this library +// is fully reentrant but only one calling thread gets to actually run +// at a time, while other calling threads would wait. So it is safe +// albeit potentially slow to call the functions exposed here on +// multiple threads concurrently. +// +// Users who prefer a state-less, singleton-less interface, +// should use the main gemmlowp interface (public/gemmlowp.h) instead. + +// The main entry point to compute a Gemm. This is the standard +// EightBitIntGemm interface. +void EightBitIntGemm(int m, int n, int k, const std::uint8_t *a, + std::int32_t a_offset, int lda, const std::uint8_t *b, + std::int32_t b_offset, int ldb, std::uint8_t *c, + std::int32_t c_offset, std::int32_t c_mult_int, + std::int32_t c_shift, int ldc); + +// Frees any persistent resources +// (threads, thread pools, allocators, buffers, ...) +// that gemmlowp might hold. This is called automatically +// on thread exit, but one may also call it earlier, at any time. +void FreePersistentResources(); + +// Allows specifying the number of hardware threads, as a hint as to +// how many worker threads to use for sufficiently large Gemm's. +// We will never use more threads than that, but may use fewer, +// for instance on Gemm's that are too small to benefit from all +// available threads. The value 0 lets the implementation query +// the system to determine the number of hardware threads. +// Default value: 0. +void SetMaxNumThreads(int n); + +} // namespace eight_bit_int_gemm + +} // namespace gemmlowp + +#endif // GEMMLOWP_EIGHT_BIT_INT_GEMM_EIGHT_BIT_INT_GEMM_H_ |