commit
7dec631deb
@ -1,2 +1,3 @@
|
||||
*.DS_Store
|
||||
build/
|
||||
*.user
|
||||
|
@ -1,106 +0,0 @@
|
||||
/* 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. */
|
||||
|
||||
#ifdef __APPLE__
|
||||
#include <dispatch/dispatch.h>
|
||||
#endif
|
||||
|
||||
#ifdef __APPLE__
|
||||
#ifndef PTHREAD_BARRIER_H_
|
||||
#define PTHREAD_BARRIER_H_
|
||||
|
||||
#include <pthread.h>
|
||||
#include <errno.h>
|
||||
|
||||
typedef int pthread_barrierattr_t;
|
||||
typedef struct {
|
||||
pthread_mutex_t mutex;
|
||||
pthread_cond_t cond;
|
||||
int count;
|
||||
int tripCount;
|
||||
} pthread_barrier_t;
|
||||
|
||||
int pthread_barrier_init(pthread_barrier_t *barrier,
|
||||
const pthread_barrierattr_t *attr, unsigned int count) {
|
||||
if (count == 0) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
if (pthread_mutex_init(&barrier->mutex, 0) < 0) {
|
||||
return -1;
|
||||
}
|
||||
if (pthread_cond_init(&barrier->cond, 0) < 0) {
|
||||
pthread_mutex_destroy(&barrier->mutex);
|
||||
return -1;
|
||||
}
|
||||
barrier->tripCount = count;
|
||||
barrier->count = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pthread_barrier_destroy(pthread_barrier_t *barrier) {
|
||||
pthread_cond_destroy(&barrier->cond);
|
||||
pthread_mutex_destroy(&barrier->mutex);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pthread_barrier_wait(pthread_barrier_t *barrier) {
|
||||
pthread_mutex_lock(&barrier->mutex);
|
||||
++(barrier->count);
|
||||
if (barrier->count >= barrier->tripCount) {
|
||||
barrier->count = 0;
|
||||
pthread_cond_broadcast(&barrier->cond);
|
||||
pthread_mutex_unlock(&barrier->mutex);
|
||||
return 1;
|
||||
} else {
|
||||
pthread_cond_wait(&barrier->cond, &(barrier->mutex));
|
||||
pthread_mutex_unlock(&barrier->mutex);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
#endif // PTHREAD_BARRIER_H_
|
||||
|
||||
typedef int pthread_spinlock_t;
|
||||
|
||||
int pthread_spin_init(pthread_spinlock_t *lock, int pshared) {
|
||||
__asm__ __volatile__("" ::: "memory");
|
||||
*lock = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pthread_spin_destroy(pthread_spinlock_t *lock) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pthread_spin_lock(pthread_spinlock_t *lock) {
|
||||
while (1) {
|
||||
int i;
|
||||
for (i=0; i < 10000; i++) {
|
||||
if (__sync_bool_compare_and_swap(lock, 0, 1)) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
sched_yield();
|
||||
}
|
||||
}
|
||||
|
||||
int pthread_spin_unlock(pthread_spinlock_t *lock) {
|
||||
__asm__ __volatile__("" ::: "memory");
|
||||
*lock = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif // __APPLE__
|
@ -0,0 +1,85 @@
|
||||
/* 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 "paddle/utils/Locks.h"
|
||||
#include <semaphore.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace paddle {
|
||||
class SemaphorePrivate {
|
||||
public:
|
||||
sem_t sem;
|
||||
};
|
||||
|
||||
Semaphore::Semaphore(int initValue): m(new SemaphorePrivate()) {
|
||||
sem_init(&m->sem, 0, initValue);
|
||||
}
|
||||
|
||||
Semaphore::~Semaphore() {
|
||||
sem_destroy(&m->sem);
|
||||
}
|
||||
|
||||
bool Semaphore::timeWait(struct timespec* ts) {
|
||||
return (0 == sem_timedwait(&m->sem, ts));
|
||||
}
|
||||
|
||||
void Semaphore::wait() {
|
||||
sem_wait(&m->sem);
|
||||
}
|
||||
|
||||
void Semaphore::post() {
|
||||
sem_post(&m->sem);
|
||||
}
|
||||
|
||||
|
||||
class SpinLockPrivate {
|
||||
public:
|
||||
inline SpinLockPrivate() { pthread_spin_init(&lock_, 0); }
|
||||
inline ~SpinLockPrivate() { pthread_spin_destroy(&lock_); }
|
||||
pthread_spinlock_t lock_;
|
||||
char padding_[64 - sizeof(pthread_spinlock_t)];
|
||||
};
|
||||
|
||||
SpinLock::SpinLock():m(new SpinLockPrivate()) {}
|
||||
|
||||
|
||||
SpinLock::~SpinLock() { delete m; }
|
||||
|
||||
void SpinLock::lock() {
|
||||
pthread_spin_lock(&m->lock_);
|
||||
}
|
||||
|
||||
void SpinLock::unlock() {
|
||||
pthread_spin_unlock(&m->lock_);
|
||||
}
|
||||
|
||||
class ThreadBarrierPrivate {
|
||||
public:
|
||||
pthread_barrier_t barrier_;
|
||||
};
|
||||
|
||||
ThreadBarrier::ThreadBarrier(int count): m(new ThreadBarrierPrivate()) {
|
||||
pthread_barrier_init(&m->barrier_, nullptr, count);
|
||||
}
|
||||
|
||||
ThreadBarrier::~ThreadBarrier() {
|
||||
pthread_barrier_destroy(&m->barrier_);
|
||||
delete m;
|
||||
}
|
||||
|
||||
void ThreadBarrier::wait() {
|
||||
pthread_barrier_wait(&m->barrier_);
|
||||
}
|
||||
|
||||
} // namespace paddle
|
@ -0,0 +1,112 @@
|
||||
/* 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 "paddle/utils/Locks.h"
|
||||
#include "paddle/utils/Logging.h"
|
||||
#include <dispatch/dispatch.h>
|
||||
#include <libkern/OSAtomic.h>
|
||||
namespace paddle {
|
||||
class SemaphorePrivate {
|
||||
public:
|
||||
~SemaphorePrivate() {
|
||||
dispatch_release(sem);
|
||||
}
|
||||
|
||||
dispatch_semaphore_t sem;
|
||||
};
|
||||
|
||||
Semaphore::Semaphore(int initValue): m(new SemaphorePrivate()) {
|
||||
m->sem = dispatch_semaphore_create(initValue);
|
||||
}
|
||||
|
||||
Semaphore::~Semaphore() {
|
||||
delete m;
|
||||
}
|
||||
|
||||
bool Semaphore::timeWait(timespec *ts) {
|
||||
dispatch_time_t tm = dispatch_walltime(ts, 0);
|
||||
return (0 == dispatch_semaphore_wait(m->sem, tm));
|
||||
}
|
||||
|
||||
void Semaphore::wait() {
|
||||
dispatch_semaphore_wait(m->sem, DISPATCH_TIME_FOREVER);
|
||||
}
|
||||
|
||||
void Semaphore::post() {
|
||||
dispatch_semaphore_signal(m->sem);
|
||||
}
|
||||
|
||||
class SpinLockPrivate {
|
||||
public:
|
||||
SpinLockPrivate(): lock_(0) {}
|
||||
|
||||
OSSpinLock lock_;
|
||||
char padding_[64 - sizeof(OSSpinLock)]; // Padding to cache line size
|
||||
};
|
||||
|
||||
SpinLock::SpinLock(): m(new SpinLockPrivate()) {}
|
||||
SpinLock::~SpinLock() { delete m; }
|
||||
|
||||
void SpinLock::lock() {
|
||||
OSSpinLockLock(&m->lock_);
|
||||
}
|
||||
|
||||
void SpinLock::unlock() {
|
||||
OSSpinLockUnlock(&m->lock_);
|
||||
}
|
||||
|
||||
|
||||
class ThreadBarrierPrivate {
|
||||
public:
|
||||
pthread_mutex_t mutex;
|
||||
pthread_cond_t cond;
|
||||
int count;
|
||||
int tripCount;
|
||||
|
||||
inline explicit ThreadBarrierPrivate(int cnt):count(0), tripCount(cnt) {
|
||||
CHECK_NE(cnt, 0);
|
||||
CHECK_GE(pthread_mutex_init(&mutex, 0), 0);
|
||||
CHECK_GE(pthread_cond_init(&cond, 0), 0);
|
||||
}
|
||||
|
||||
inline ~ThreadBarrierPrivate() {
|
||||
pthread_cond_destroy(&cond);
|
||||
pthread_mutex_destroy(&mutex);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief wait
|
||||
* @return true if the last wait
|
||||
*/
|
||||
inline bool wait() {
|
||||
pthread_mutex_lock(&mutex);
|
||||
++count;
|
||||
if (count > tripCount) {
|
||||
count = 0;
|
||||
pthread_cond_broadcast(&cond);
|
||||
pthread_mutex_unlock(&mutex);
|
||||
return true;
|
||||
} else {
|
||||
pthread_cond_wait(&cond, &mutex);
|
||||
pthread_mutex_unlock(&mutex);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
ThreadBarrier::ThreadBarrier(int count): m(new ThreadBarrierPrivate(count)) {}
|
||||
ThreadBarrier::~ThreadBarrier() { delete m; }
|
||||
void ThreadBarrier::wait() { m->wait(); }
|
||||
|
||||
} // namespace paddle
|
@ -0,0 +1,95 @@
|
||||
/* 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 <chrono>
|
||||
|
||||
#include "paddle/utils/CustomStackTrace.h"
|
||||
#include "paddle/utils/CommandLineParser.h"
|
||||
#include "paddle/utils/Util.h"
|
||||
#include "paddle/utils/Locks.h"
|
||||
|
||||
P_DEFINE_int32(test_thread_num, 10, "testing thread number");
|
||||
|
||||
void testNormalImpl(const std::function<void(
|
||||
paddle::CustomStackTrace<std::string>&,
|
||||
size_t, size_t,
|
||||
paddle::ThreadBarrier&,
|
||||
paddle::ThreadBarrier&)>& callback) {
|
||||
paddle::CustomStackTrace<std::string> tracer;
|
||||
paddle::ThreadBarrier doneBarrier(FLAGS_test_thread_num + 1);
|
||||
paddle::ThreadBarrier startBarrier(FLAGS_test_thread_num + 1);
|
||||
constexpr size_t countDown = 10;
|
||||
constexpr size_t layerSize = 1000;
|
||||
std::vector<std::unique_ptr<std::thread>> threads;
|
||||
threads.reserve(FLAGS_test_thread_num);
|
||||
|
||||
for (int32_t i=0; i < FLAGS_test_thread_num; ++i) {
|
||||
threads.emplace_back(new std::thread([&tracer, &countDown, &layerSize,
|
||||
&startBarrier, &doneBarrier,
|
||||
&callback]{
|
||||
callback(tracer, countDown, layerSize, startBarrier, doneBarrier);
|
||||
}));
|
||||
}
|
||||
size_t cntDown = countDown;
|
||||
while (cntDown-- > 0) {
|
||||
startBarrier.wait();
|
||||
doneBarrier.wait();
|
||||
ASSERT_TRUE(tracer.empty());
|
||||
}
|
||||
|
||||
for (auto& thread : threads) {
|
||||
thread->join();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
TEST(CustomStackTrace, normalTrain) {
|
||||
testNormalImpl([](paddle::CustomStackTrace<std::string>& tracer,
|
||||
size_t countDown, size_t layerSize,
|
||||
paddle::ThreadBarrier& start, paddle::ThreadBarrier& finish){
|
||||
while (countDown-- > 0) {
|
||||
start.wait();
|
||||
for (size_t i=0; i < layerSize; ++i) {
|
||||
tracer.push("layer_" + std::to_string(i));
|
||||
}
|
||||
tracer.pop("");
|
||||
for (size_t i=0; i < layerSize; ++i) {
|
||||
tracer.pop("layer_" + std::to_string(layerSize - 1 - i));
|
||||
}
|
||||
finish.wait();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
TEST(CustomStackTrace, normalTest) {
|
||||
testNormalImpl([] (paddle::CustomStackTrace<std::string>& tracer,
|
||||
size_t countDown, size_t layerSize,
|
||||
paddle::ThreadBarrier& start, paddle::ThreadBarrier& finish){
|
||||
while (countDown-- > 0) {
|
||||
start.wait();
|
||||
for (size_t i=0; i < layerSize; ++i) {
|
||||
tracer.push("layer_" + std::to_string(i));
|
||||
}
|
||||
tracer.clear(); // in forward test, tracer will clear after forward.
|
||||
finish.wait();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
testing::InitGoogleTest(&argc, argv);
|
||||
paddle::initMain(argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
/* 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 "paddle/utils/Util.h"
|
||||
#include "paddle/utils/CustomStackTrace.h"
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
paddle::initMain(argc, argv);
|
||||
|
||||
for (size_t i=0; i < 1000; ++i) {
|
||||
paddle::gLayerStackTrace.push("layer_" + std::to_string(i));
|
||||
if (i == 998) {
|
||||
throw "Unhandle exception";
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
#!/bin/bash
|
||||
echo "Test Custom Stack Trace print correct result when fail"
|
||||
./test_CustomStackTracePrint >customStackTraceLog 2>&1
|
||||
if [ $? -eq 0 ]; then
|
||||
exit 1
|
||||
else
|
||||
set -e
|
||||
TEXT=""
|
||||
for ((i=0; i<=998; i++))
|
||||
do
|
||||
TEXT="layer_$i, "$TEXT
|
||||
done
|
||||
TEXT="Forwarding "$TEXT
|
||||
grep -q "$TEXT" customStackTraceLog
|
||||
fi
|
Loading…
Reference in new issue