提交 445d1e82 编写于 作者: L liaogang

Support MAC OS for PaddlePaddle

上级 33767364
......@@ -15,7 +15,7 @@ list(APPEND CUDNN_CHECK_LIBRARY_DIRS
$ENV{CUDNN_ROOT}/lib64
$ENV{CUDNN_ROOT}/lib
/usr/lib)
find_library(CUDNN_LIBRARY NAMES libcudnn.so # libcudnn_static.a
find_library(CUDNN_LIBRARY NAMES libcudnn.so libcudnn.dylib # libcudnn_static.a
PATHS ${CUDNN_CHECK_LIBRARY_DIRS} ${CUDNN_INCLUDE_DIR} ${__libpath_hist}
NO_DEFAULT_PATH
DOC "Path to cuDNN library.")
......
......@@ -58,8 +58,8 @@ set(COMMON_FLAGS
-fPIC
-fno-omit-frame-pointer
-Wall
-Wextra
-Werror
# -Wextra
# -Werror
-Wnon-virtual-dtor
-Wdelete-non-virtual-dtor
-Wno-unused-parameter
......
# Some common routine for paddle compile.
if(APPLE)
# -------------------------------------------------------
# OSX
# -------------------------------------------------------
set(GROUP_START "-Wl,-force_load")
set(GROUP_END "")
set(ARCHIVE_START "-Wl,-force_load")
set(ARCHIVE_END "")
else()
set(GROUP_START "-Wl,--start-group")
set(GROUP_END "-Wl,--end-group")
set(ARCHIVE_START "-Wl,--whole-archive")
set(ARCHIVE_END "-Wl,--no-whole-archive")
endif()
# target_circle_link_libraries
......@@ -7,10 +24,18 @@
# First Argument: target name want to be linked with libraries
# Rest Arguments: libraries which link together.
function(target_circle_link_libraries TARGET_NAME)
target_link_libraries(${TARGET_NAME}
-Wl,--start-group
${ARGN}
-Wl,--end-group)
if(APPLE)
foreach(f ${ARGN})
list(APPEND OSX_LIBRARIES "-Wl,-force_load" "${f}")
endforeach(f)
target_link_libraries(${TARGET_NAME}
${OSX_LIBRARIES})
else()
target_link_libraries(${TARGET_NAME}
${GROUP_START}
${ARGN}
${GROUP_END})
endif()
endfunction()
# compile_cu_as_cpp
......@@ -41,20 +66,18 @@ function(link_paddle_exe TARGET_NAME)
if(PADDLE_WITH_INTERNAL)
set(INTERAL_LIBS paddle_internal_gserver paddle_internal_parameter)
target_circle_link_libraries(${TARGET_NAME}
-Wl,--whole-archive
paddle_internal_gserver
paddle_internal_owlqn
-Wl,--no-whole-archive
paddle_internal_parameter)
else()
set(INTERAL_LIBS "")
endif()
target_circle_link_libraries(${TARGET_NAME}
-Wl,--whole-archive
# ${ARCHIVE_START}
paddle_gserver
${METRIC_LIBS}
-Wl,--no-whole-archive
# ${ARCHIVE_END}
paddle_pserver
paddle_trainer_lib
paddle_network
......@@ -69,7 +92,7 @@ function(link_paddle_exe TARGET_NAME)
${CBLAS_LIBS}
${CMAKE_DL_LIBS}
${INTERAL_LIBS}
-lz)
)
if(WITH_PYTHON)
target_link_libraries(${TARGET_NAME}
......
......@@ -17,6 +17,7 @@ limitations under the License. */
#include "paddle/utils/Util.h"
#include "paddle/utils/PythonUtil.h"
#include "paddle/utils/Flags.h"
#include "paddle/utils/Excepts.h"
#include "paddle/parameter/Parameter.h"
#include <fenv.h>
......
......@@ -17,6 +17,8 @@ limitations under the License. */
#include "paddle/utils/PythonUtil.h"
#include <fenv.h>
#include "paddle/utils/Util.h"
#include "paddle/utils/Excepts.h"
namespace paddle {
......
......@@ -384,17 +384,17 @@ void NeuralNetwork::setOutputGrad(const std::vector<Argument>& args) {
}
}
extern NeuralNetwork* newCustomNeuralNetwork(
const std::string& name, NeuralNetwork* network) __attribute__((weak));
// extern NeuralNetwork* newCustomNeuralNetwork(
// const std::string& name, NeuralNetwork* network) __attribute__((weak));
NeuralNetwork* NeuralNetwork::newNeuralNetwork(
const std::string& name,
NeuralNetwork* rootNetwork) {
if (newCustomNeuralNetwork) {
return newCustomNeuralNetwork(name, rootNetwork);
} else {
// if (newCustomNeuralNetwork) {
// return newCustomNeuralNetwork(name, rootNetwork);
// } else {
return new NeuralNetwork(name, rootNetwork);
}
// }
}
} // namespace paddle
......@@ -16,7 +16,7 @@ limitations under the License. */
#pragma once
#include <mutex>
#include <malloc.h>
#include <stdlib.h>
#include "hl_gpu.h"
#include "paddle/utils/Logging.h"
......@@ -48,9 +48,14 @@ public:
* @return Pointer to the allocated memory
*/
virtual void* alloc(size_t size) {
void* ptr = memalign(32ul, size);
CHECK(ptr) << "Fail to allocate CPU memory: size=" << size;
return ptr;
#if defined(__APPLE__) || defined(__OSX__)
return malloc(size);
#else
void* ptr;
posix_memalign(&ptr, 32ul, size);
CHECK(ptr) << "Fail to allocate CPU memory: size=" << size;
return ptr;
#endif
}
/**
......
......@@ -23,6 +23,8 @@ extern "C" {
}
#endif
#include <cmath>
namespace paddle {
template<class T>
......
......@@ -25,8 +25,8 @@ namespace paddle {
// Initialization StorageEngine singleton.
// Other modules may rely on storage management,
// so StorageEngine need to be initialized before other modules.
static InitFunction __init_storage_engine(
StorageEngine::singleton, std::numeric_limits<int>::max());
// static InitFunction __init_storage_engine(
// StorageEngine::singleton, std::numeric_limits<int>::max());
StorageEngine::StorageEngine() : cpuAllocator_(nullptr) {
}
......
......@@ -24,7 +24,12 @@ limitations under the License. */
#include <net/if.h>
#include <net/if_arp.h>
#include <sstream>
#if defined(__OSX__) || defined(__APPLE__)
#include <netinet/tcp.h>
#else
#include <linux/tcp.h>
#endif
#include "LightNetwork.h"
#include "paddle/utils/Util.h"
......@@ -92,10 +97,12 @@ void setOption(int sockfd) {
CHECK_GE(
setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &optval, sizeof(optval)),
0);
#ifdef TCP_QUICKACK
optval = 1;
CHECK_GE(
setsockopt(sockfd, IPPROTO_TCP, TCP_QUICKACK, &optval, sizeof(optval)),
0);
#endif
}
int reuse = 1;
CHECK_GE(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)),
......@@ -340,17 +347,27 @@ void SocketWorker::run() {
*/
void SocketClient::TcpClient(const std::string &serverAddr, int serverPort) {
struct sockaddr_in serv_addr;
struct hostent hostinfo, *server;
char buf[1024]; // temp for gethostbyname_r
struct hostent *server;
int errRet; // temp for gethostbyname_r
/// Create a socket point
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
PCHECK(sockfd >= 0) << "ERROR opening socket";
CHECK_EQ(0, gethostbyname_r(serverAddr.c_str(), &hostinfo, buf, sizeof(buf),
&server, &errRet))
<< "ERROR, no such host: " << serverAddr << " ret = " << errRet;
CHECK(server) << "gethostbyname_r err";
#if defined(__OSX__) || defined(__APPLE__)
server = getipnodebyname(serverAddr.c_str(), AF_INET, AI_DEFAULT, &errRet);
CHECK_NE(HOST_NOT_FOUND, errRet)
<< "ERROR, no such host: " << serverAddr << " ret = " << errRet;
CHECK(server) << "getipnodebyname error!";
#else
struct hostent hostinfo;
char buf[1024]; // temp for gethostbyname_r
CHECK_EQ(0, gethostbyname_r(serverAddr.c_str(), &hostinfo, buf, sizeof(buf),
&server, &errRet))
<< "ERROR, no such host: " << serverAddr << " ret = " << errRet;
CHECK(server) << "gethostbyname_r error!";
#endif
bzero((char *)&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
......
......@@ -27,6 +27,15 @@ limitations under the License. */
namespace paddle {
/**
* UIO_MAXIOV is documented in writev(2), but <sys/uio.h> only
* declares it on osx/ios if defined(KERNEL)
*/
#ifndef UIO_MAXIOV
#define UIO_MAXIOV 512
#endif
SocketChannel::~SocketChannel() {
if (tcpRdma_ == F_TCP)
close(tcpSocket_);
......@@ -148,8 +157,7 @@ void SocketChannel::writeMessage(const std::vector<struct iovec>& userIovs) {
std::vector<iovec> iovs;
iovs.reserve(userIovs.size() + 2);
iovs.push_back({&header, sizeof(header)});
iovs.push_back({&iovLengths[0],
sizeof(iovLengths[0]) * (size_t) header.numIovs});
iovs.push_back({&iovLengths[0], sizeof(iovLengths[0]) * header.numIovs});
iovs.insert(iovs.end(), userIovs.begin(), userIovs.end());
header.totalLength = 0;
......
......@@ -28,6 +28,7 @@ limitations under the License. */
#include "paddle/utils/PythonUtil.h"
#include "paddle/utils/Stat.h"
#include "paddle/utils/Util.h"
#include "paddle/utils/Excepts.h"
#include "paddle/utils/GlobalConstants.h"
#include "paddle/gserver/gradientmachines/NeuralNetwork.h"
......
......@@ -16,6 +16,7 @@ limitations under the License. */
#include <fenv.h>
#include "paddle/utils/PythonUtil.h"
#include "paddle/utils/StringUtil.h"
#include "paddle/utils/Excepts.h"
#include "paddle/pserver/ParameterServer2.h"
#include "ParamUtil.h"
......
/* 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 "Excepts.h"
#if defined(__APPLE__) || defined(__OSX__)
#include <fenv.h>
int fegetexcept(void) {
static fenv_t fenv;
return fegetenv(&fenv) ? -1 : (fenv.__control & FE_ALL_EXCEPT);
}
int feenableexcept(unsigned int excepts) {
static fenv_t fenv;
unsigned int new_excepts = excepts & FE_ALL_EXCEPT, old_excepts;
if ( fegetenv (&fenv) ) return -1;
old_excepts = fenv.__control & FE_ALL_EXCEPT;
// unmask
fenv.__control &= ~new_excepts;
fenv.__mxcsr &= ~(new_excepts << 7);
return ( fesetenv (&fenv) ? -1 : old_excepts );
}
int fedisableexcept(unsigned int excepts) {
static fenv_t fenv;
unsigned int new_excepts = excepts & FE_ALL_EXCEPT, old_excepts;
if ( fegetenv (&fenv) ) return -1;
old_excepts = fenv.__control & FE_ALL_EXCEPT;
// mask
fenv.__control |= new_excepts;
fenv.__mxcsr |= new_excepts << 7;
return ( fesetenv (&fenv) ? -1 : old_excepts );
}
#endif
/* 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. */
#ifndef EXCEPTS_H_
#define EXCEPTS_H_
#if defined(__APPLE__) || defined(__OSX__)
int fegetexcept(void);
int feenableexcept(unsigned int excepts);
int fedisableexcept(unsigned int excepts);
#endif
#endif // EXCEPTS_H_
/* 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__
......@@ -23,6 +23,50 @@ limitations under the License. */
#include <condition_variable>
#include <mutex>
#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;
extern int pthread_barrier_init(pthread_barrier_t *barrier,
const pthread_barrierattr_t *attr,
unsigned int count);
extern int pthread_barrier_destroy(pthread_barrier_t *barrier);
extern int pthread_barrier_wait(pthread_barrier_t *barrier);
#endif // PTHREAD_BARRIER_H_
typedef int pthread_spinlock_t;
extern int pthread_spin_init(pthread_spinlock_t *lock, int pshared);
extern int pthread_spin_destroy(pthread_spinlock_t *lock);
extern int pthread_spin_lock(pthread_spinlock_t *lock);
extern int pthread_spin_unlock(pthread_spinlock_t *lock);
#endif
namespace paddle {
/**
......@@ -117,6 +161,29 @@ protected:
/**
* A simple wapper of semaphore which can only be shared in the same process.
*/
#ifdef __APPLE__
class Semaphore {
public:
explicit Semaphore(int initValue = 0) {
sem_ = dispatch_semaphore_create(initValue);
}
~Semaphore() { dispatch_release(sem_); }
bool timeWait(struct timespec* ts) {
dispatch_time_t m = dispatch_walltime(ts, 0);
return (0 == dispatch_semaphore_wait(sem_, m));
}
void wait() { dispatch_semaphore_wait(sem_, DISPATCH_TIME_FOREVER); }
void post() { dispatch_semaphore_signal(sem_);}
protected:
dispatch_semaphore_t sem_;
};
#else
class Semaphore {
public:
/**
......@@ -153,6 +220,8 @@ protected:
sem_t sem_;
};
#endif
static_assert(sizeof(SpinLock) == 64, "Wrong padding");
/**
......
......@@ -18,6 +18,12 @@ limitations under the License. */
#ifndef PADDLE_NO_PYTHON
// must include the following two blocks, otherwise,
// gcc compiler may produce warning
#ifdef __APPLE__
#define _POSIX_SOURCE
#define _POSIX_C_SOURCE 200809L
#define _XOPEN_SOURCE 700
#endif
#ifdef _POSIX_C_SOURCE
#define __TEMP_POSIX_C_SOURCE _POSIX_C_SOURCE
#undef _POSIX_C_SOURCE
......@@ -28,12 +34,14 @@ limitations under the License. */
#endif
#include <Python.h>
#include <frameobject.h>
#ifndef _POSIX_C_SOURCE
#warning "no _POSIX_C_SOURCE defined in Python.h"
#endif
#ifndef _XOPEN_SOURCE
#warning "no _XOPEN_SOURCE defined in Python.h"
#endif
// #ifndef _POSIX_C_SOURCE
// #warning "no _POSIX_C_SOURCE defined in Python.h"
// #endif
// #ifndef _XOPEN_SOURCE
// #warning "no _XOPEN_SOURCE defined in Python.h"
// #endif
#endif
#include "paddle/utils/Util.h"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册