提交 9e11ca80 编写于 作者: G gangliao 提交者: Yu Yang

Use C++ 11 atomic_flag in MacOS as spin lock (#175)

* Use C++ 11 atomic_flag in MacOS as spin lock
* Add unittest for it.
上级 191fafe3
...@@ -57,7 +57,7 @@ std::vector<ParameterPtr> trainerOnePassTest(const string& configFile, ...@@ -57,7 +57,7 @@ std::vector<ParameterPtr> trainerOnePassTest(const string& configFile,
<< " sparseUpdate=" << sparseUpdate; << " sparseUpdate=" << sparseUpdate;
srand(FLAGS_seed); srand(FLAGS_seed);
*ThreadLocalRand::getSeed() = FLAGS_seed; *ThreadLocalRand::getSeed() = FLAGS_seed;
ThreadLocalRandomEngine::get().seed(FLAGS_seed);
if (useGpu) { if (useGpu) {
CHECK_LE(trainerCount, gNumDevices); CHECK_LE(trainerCount, gNumDevices);
} }
......
...@@ -15,12 +15,9 @@ limitations under the License. */ ...@@ -15,12 +15,9 @@ limitations under the License. */
#include "paddle/utils/Locks.h" #include "paddle/utils/Locks.h"
#include "paddle/utils/Logging.h" #include "paddle/utils/Logging.h"
#include <dispatch/dispatch.h> #include <dispatch/dispatch.h>
#include <atomic>
#include <libkern/OSAtomic.h> #include <libkern/OSAtomic.h>
#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_12
#include <os/lock.h>
#endif
namespace paddle { namespace paddle {
class SemaphorePrivate { class SemaphorePrivate {
...@@ -55,12 +52,7 @@ void Semaphore::post() { ...@@ -55,12 +52,7 @@ void Semaphore::post() {
class SpinLockPrivate { class SpinLockPrivate {
public: public:
#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_12 std::atomic_flag lock_ = ATOMIC_FLAG_INIT;
os_unfair_lock lock_;
#else
SpinLockPrivate(): lock_(OS_SPINLOCK_INIT) {}
OSSpinLock lock_;
#endif
char padding_[64 - sizeof(lock_)]; // Padding to cache line size char padding_[64 - sizeof(lock_)]; // Padding to cache line size
}; };
...@@ -68,19 +60,11 @@ SpinLock::SpinLock(): m(new SpinLockPrivate()) {} ...@@ -68,19 +60,11 @@ SpinLock::SpinLock(): m(new SpinLockPrivate()) {}
SpinLock::~SpinLock() { delete m; } SpinLock::~SpinLock() { delete m; }
void SpinLock::lock() { void SpinLock::lock() {
#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_12 while (m->lock_.test_and_set(std::memory_order_acquire)) {}
os_unfair_lock_lock(&m->lock_);
#else
OSSpinLockLock(&m->lock_);
#endif
} }
void SpinLock::unlock() { void SpinLock::unlock() {
#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_12 m->lock_.clear(std::memory_order_release);
os_unfair_lock_unlock(&m->lock_);
#else
OSSpinLockUnlock(&m->lock_);
#endif
} }
......
...@@ -4,6 +4,7 @@ add_simple_unittest(test_Thread) ...@@ -4,6 +4,7 @@ add_simple_unittest(test_Thread)
add_simple_unittest(test_StringUtils) add_simple_unittest(test_StringUtils)
add_simple_unittest(test_CustomStackTrace) add_simple_unittest(test_CustomStackTrace)
add_simple_unittest(test_ThreadBarrier) add_simple_unittest(test_ThreadBarrier)
add_simple_unittest(test_SpinLock)
add_executable( add_executable(
test_CustomStackTracePrint test_CustomStackTracePrint
......
/* Copyright (c) 2016 Baidu, Inc. All Rights Reserve.
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 <gtest/gtest.h>
#include <vector>
#include "paddle/utils/Logging.h"
#include "paddle/utils/CommandLineParser.h"
#include "paddle/utils/Util.h"
#include "paddle/utils/Locks.h"
P_DEFINE_int32(test_thread_num, 100, "testing thread number");
void testNormalImpl(size_t thread_num, const std::function
<void(size_t, size_t&, paddle::SpinLock&)>& callback) {
paddle::SpinLock mutex;
std::vector<std::thread> threads;
threads.reserve(thread_num);
size_t count = 0;
for (size_t i = 0; i < thread_num; ++i) {
threads.emplace_back([&thread_num, &count, &mutex, &callback]{
callback(thread_num, count, mutex);
});
}
for (auto& thread : threads) {
thread.join();
}
// Check whether all threads reach this point or not
CHECK_EQ(count, thread_num);
}
TEST(ThreadSpinLock, normalTest) {
for (auto &thread_num : {10, 30, 50 , 100 , 300, 1000}) {
testNormalImpl(thread_num, [](size_t thread_num,
size_t& count, paddle::SpinLock& mutex){
std::lock_guard<paddle::SpinLock> lock(mutex);
++count;
});
}
}
int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
paddle::initMain(argc, argv);
return RUN_ALL_TESTS();
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册