Skip to content

Commit

Permalink
Merge pull request apache#10 from dato-code/windows_compile
Browse files Browse the repository at this point in the history
Windows compile
  • Loading branch information
Jay Gu committed Jan 27, 2016
2 parents 51b7ee6 + c9d12f6 commit 8270509
Show file tree
Hide file tree
Showing 20 changed files with 145 additions and 46 deletions.
4 changes: 2 additions & 2 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[submodule "mshadow"]
path = mshadow
url = /~https://github.com/dmlc/mshadow.git
url = /~https://github.com/dato-code/mshadow.git
[submodule "dmlc-core"]
path = dmlc-core
url = /~https://github.com/dmlc/dmlc-core.git
url = /~https://github.com/dato-code/dmlc-core.git
[submodule "ps-lite"]
path = ps-lite
url = /~https://github.com/dmlc/ps-lite
32 changes: 18 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,10 @@ ifndef DMLC_CORE
DMLC_CORE = dmlc-core
endif

ifneq ($(USE_OPENMP), 1)
export NO_OPENMP = 1
endif

# use customized config file
include $(config)
include mshadow/make/mshadow.mk
include $(DMLC_CORE)/make/dmlc.mk
unexport NO_OPENMP

# all the possible warning tread
WARNFLAGS= -Wall
Expand All @@ -32,7 +27,16 @@ ifeq ($(DEBUG), 1)
else
CFLAGS += -O3
endif
CFLAGS += -I./mshadow/ -I./dmlc-core/include -fPIC -Iinclude $(MSHADOW_CFLAGS)

ifneq ($(WIN32), 1)
CFLAGS += -fPIC
SHARED_LIB_EXT = so
STATIC_LIB_EXT = a
else
SHARED_LIB_EXT = dll
STATIC_LIB_EXT = lib
endif
CFLAGS += -I./mshadow/ -I./dmlc-core/include -Iinclude $(MSHADOW_CFLAGS)
LDFLAGS = -pthread $(MSHADOW_LDFLAGS) $(DMLC_LDFLAGS)
ifeq ($(DEBUG), 1)
NVCCFLAGS = -g -G -O0 -ccbin $(CXX) $(MSHADOW_NVCCFLAGS)
Expand Down Expand Up @@ -96,7 +100,7 @@ include $(MXNET_PLUGINS)

.PHONY: clean all test lint doc clean_all rcpplint rcppexport roxygen

all: lib/libmxnet.a lib/libmxnet.so $(BIN)
all: lib/libmxnet.$(STATIC_LIB_EXT) lib/libmxnet.$(SHARED_LIB_EXT) $(BIN)

SRC = $(wildcard src/*.cc src/*/*.cc)
OBJ = $(patsubst %.cc, build/%.o, $(SRC))
Expand Down Expand Up @@ -177,19 +181,19 @@ $(EXTRA_OPERATORS)/build/%_gpu.o: $(EXTRA_OPERATORS)/%.cu
$(NVCC) $(NVCCFLAGS) -Xcompiler "$(CFLAGS) -Isrc/operator" -M -MT $(EXTRA_OPERATORS)/build/$*_gpu.o $< >$(EXTRA_OPERATORS)/build/$*_gpu.d
$(NVCC) -c -o $@ $(NVCCFLAGS) -Xcompiler "$(CFLAGS) -Isrc/operator" $<

# NOTE: to statically link libmxnet.a we need the option
# --Wl,--whole-archive -lmxnet --Wl,--no-whole-archive
lib/libmxnet.a: $(ALL_DEP)
@mkdir -p $(@D)
ar crv $@ $(filter %.o, $?)

copy_cuda_deps:
ifdef CUDA_DEP
@mkdir -p lib
cp $(CUDA_DEP) lib
endif

lib/libmxnet.so: $(ALL_DEP) copy_cuda_deps
# NOTE: to statically link libmxnet.a we need the option
# --Wl,--whole-archive -lmxnet --Wl,--no-whole-archive
lib/libmxnet.$(STATIC_LIB_EXT): $(ALL_DEP)
@mkdir -p $(@D)
ar crv $@ $(filter %.o, $?)

lib/libmxnet.$(SHARED_LIB_EXT): $(ALL_DEP) copy_cuda_deps
@mkdir -p $(@D)
$(CXX) $(CFLAGS) -shared -o $@ $(filter %.o %.a, $^) $(LDFLAGS)

Expand Down
6 changes: 5 additions & 1 deletion configure
Original file line number Diff line number Diff line change
Expand Up @@ -242,15 +242,19 @@ CXX=$CXX
# OS Dependent Flags
if [[ $OSTYPE == linux* ]]; then
USE_OPENMP=1
WIN32=0
USE_BLAS="blas"
SHARED_LINKER_FLAGS="-Wl,-rpath,\\\$\$ORIGIN -Wl,-rpath,${PWD}/deps/local/lib64 -Wl,-rpath,${PWD}/deps/local/lib"
elif [[ $OSTYPE == darwin* ]]; then
USE_OPENMP=0
WIN32=0
USE_BLAS="apple"
SHARED_LINKER_FLAGS="-Wl,-rpath,@loader_path -Wl,-rpath,${PWD}/deps/local/lib64 -Wl,-rpath,${PWD}/deps/local/lib"
elif [[ $OSTYPE == msys ]]; then
USE_OPENMP=0
USE_BLAS="blas"
WIN32=1
USE_BLAS="openblas"
ADD_CFLAGS="-DMXNET_EXPORTS "
SHARED_LINKER_FLAGS="-Wl,-rpath,/mingw64/bin -Wl,-rpath,${PWD}/deps/local/lib64 -Wl,-rpath,${PWD}/deps/local/lib"
fi

Expand Down
2 changes: 1 addition & 1 deletion dmlc-core
2 changes: 1 addition & 1 deletion include/mxnet/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
/*!
* \brief define dllexport for Visual Studio
*/
#ifdef _MSC_VER
#if defined(_MSC_VER) || defined(_WIN32)
#ifdef MXNET_EXPORTS
#define MXNET_API __declspec(dllexport)
#else
Expand Down
14 changes: 11 additions & 3 deletions include/mxnet/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ class MXNET_API Engine {
/*!\brief virtual destructor */
virtual ~Engine() noexcept(false) {}
/*!
* \return Engine singleton.
* \return Engine singleton. NULL if Shutdown() is called.
*
* \note Check nullptr if used in destructors.
*/
static Engine* Get();
/*!
Expand All @@ -180,9 +182,15 @@ class MXNET_API Engine {
* This function is called by another singleton X who requires
* engine to be destructed after X.
*
* \return A shared pointer to Engine singleton.
* \return A ref of the shared pointer to Engine singleton.
*/
static std::shared_ptr<Engine>& _GetSharedRef();

/**
* \brief Destroy the global engine singleton. Future
* access to engine is disabled.
*/
static std::shared_ptr<Engine> _GetSharedRef();
static void Shutdown();
/*!
* \brief Push an synchronous operation to the engine.
* \param exec_fn Execution function that executes the operation.
Expand Down
13 changes: 9 additions & 4 deletions include/mxnet/ndarray.h
Original file line number Diff line number Diff line change
Expand Up @@ -323,13 +323,18 @@ class NDArray {
}
/*! \brief destructor */
~Chunk() {
auto engine_ptr = Engine::Get();
if (static_data || delay_alloc) {
Engine::Get()->DeleteVariable([](RunContext s) {}, shandle.ctx, var);
if (engine_ptr) {
engine_ptr->DeleteVariable([](RunContext s) {}, shandle.ctx, var);
}
} else {
Storage::Handle h = this->shandle;
Engine::Get()->DeleteVariable([h](RunContext s) {
Storage::Get()->Free(h);
}, shandle.ctx, var);
if (engine_ptr) {
Engine::Get()->DeleteVariable([h](RunContext s) {
Storage::Get()->Free(h);
}, shandle.ctx, var);
}
}
}
};
Expand Down
5 changes: 5 additions & 0 deletions include/mxnet/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ class ResourceManager {
* \return Resource manager singleton.
*/
static ResourceManager *Get();

/**
* Destroy the Resource manager singleton.
*/
static void Shutdown();
};
} // namespace mxnet
#endif // MXNET_RESOURCE_H_
12 changes: 10 additions & 2 deletions make/dato-config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ export NVCC = ${NVCC}

# whether compile with debug
DEBUG = 0
WIN32 = ${WIN32}

# the additional link flags you want to add
ADD_LDFLAGS = -L${DEPS}/lib -L${DEPS}/lib64 ${SHARED_LINKER_FLAGS}
ADD_LDFLAGS = ${ADD_LDFLAGS}
ADD_LDFLAGS += -L${DEPS}/lib -L${DEPS}/lib64 ${SHARED_LINKER_FLAGS}

# the additional compile flags you want to add
ADD_CFLAGS = -I${DEPS}/include
ADD_CFLAGS = ${ADD_CFLAGS}
ADD_CFLAGS += -I${DEPS}/include

#---------------------------------------------
# matrix computation libraries for CPU/GPU
Expand All @@ -58,6 +61,11 @@ USE_OPENCV = 0

# use openmp for parallelization
USE_OPENMP = ${USE_OPENMP}
ifneq ($(USE_OPENMP), 1)
export NO_OPENMP = 1
ADD_CFLAGS += -DDISABLE_OPENMP
endif


# choose the version of blas you want to use
# can be: mkl, blas, atlas, openblas
Expand Down
2 changes: 1 addition & 1 deletion mshadow
2 changes: 1 addition & 1 deletion ps-lite
Submodule ps-lite updated 1 files
+19 −36 include/ps/sarray.h
29 changes: 29 additions & 0 deletions scripts/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

SCRIPT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
WORKSPACE=${SCRIPT_DIR}/..

cd ${WORKSPACE}
source ${WORKSPACE}/scripts/python_env.sh

if [ -d "/usr/local/cuda-7.5" ]; then
./configure --cleanup_if_invalid --yes --cuda_path=/usr/local/cuda-7.5
HAS_GPU=1
else
./configure --cleanup_if_invalid --yes
fi

make clean_all
make -j4

if [[ $OSTYPE != msys ]]; then
nosecmd="${PYTHON_EXECUTABLE} ${NOSETEST_EXECUTABLE}"
else
nosecmd="${NOSETEST_EXECUTABLE}"
fi

if [ ! -z "$HAS_GPU" ]; then
${nosecmd} -v --with-id ${WORKSPACE}/tests/python/unittest ${WORKSPACE}/tests/python/train ${WORKSPACE}/tests/python/gpu --with-xunit --xunit-file=alltests.nosetests.xml
else
${nosecmd} -v --with-id ${WORKSPACE}/tests/python/unittest ${WORKSPACE}/tests/python/train --with-xunit --xunit-file=alltests.nosetests.xml
fi
2 changes: 2 additions & 0 deletions src/c_api/c_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ int MXRandomSeed(int seed) {
int MXNotifyShutdown() {
API_BEGIN();
Engine::Get()->NotifyShutdown();
ResourceManager::Shutdown();
Engine::Shutdown();
API_END();
}

Expand Down
4 changes: 2 additions & 2 deletions src/common/object_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class ObjectPool {
* \brief Internal structure to hold pointers.
*/
struct LinkedList {
#if defined(_MSC_VER)
#if defined(_MSC_VER) || defined(_WIN32)
T t;
LinkedList* next{nullptr};
#else
Expand Down Expand Up @@ -168,7 +168,7 @@ void ObjectPool<T>::AllocateChunk() {
static_assert(alignof(LinkedList) % alignof(T) == 0, "ObjectPooll Invariant");
static_assert(kPageSize % alignof(LinkedList) == 0, "ObjectPooll Invariant");
void* new_chunk_ptr;
#ifdef _MSC_VER
#if defined(_MSC_VER) || defined(_WIN32)
new_chunk_ptr = _aligned_malloc(kPageSize, kPageSize);
CHECK_NE(new_chunk_ptr, NULL) << "Allocation failed";
#else
Expand Down
18 changes: 17 additions & 1 deletion src/engine/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,29 @@ inline Engine* CreateEngine() {
}
} // namespace engine

std::shared_ptr<Engine> Engine::_GetSharedRef() {

static bool engine_shutdown = false;

std::shared_ptr<Engine>& Engine::_GetSharedRef() {
if (engine_shutdown) LOG(FATAL) << "Call GetRef() after engine already shutdown" << std::endl;
static std::shared_ptr<Engine> sptr(engine::CreateEngine());
return sptr;
}

Engine* Engine::Get() {
if (engine_shutdown) {
return nullptr;
}
static Engine *inst = _GetSharedRef().get();
return inst;
}

void Engine::Shutdown() {
if (!engine_shutdown) {
Engine::Get()->WaitForAll();
_GetSharedRef().reset();
engine_shutdown = true;
}
}

} // namespace mxnet
5 changes: 3 additions & 2 deletions src/engine/threaded_engine_perdevice.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ class ThreadedEnginePerDevice : public ThreadedEngine {
// create CPU task
int cpu_priority_nthreads = dmlc::GetEnv("MXNET_CPU_PRIORITY_NTHREADS", 4);
cpu_priority_worker_.reset(new ThreadWorkerBlock<kPriorityQueue>());
auto blockptr = cpu_priority_worker_.get();
cpu_priority_worker_->pool.reset(new ThreadPool(
cpu_priority_nthreads, [this] {
this->CPUWorker(cpu_priority_worker_.get());
cpu_priority_nthreads, [this, blockptr] {
this->CPUWorker(blockptr);
}));
// GPU tasks will be created lazily
}
Expand Down
18 changes: 16 additions & 2 deletions src/resource.cc
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,22 @@ class ResourceManagerImpl : public ResourceManager {
};
} // namespace resource

static resource::ResourceManagerImpl* instance_ptr = nullptr;
static bool rm_shutdown = false;

ResourceManager* ResourceManager::Get() {
static resource::ResourceManagerImpl inst;
return &inst;
if (instance_ptr == nullptr) {
if (rm_shutdown) LOG(FATAL) << "Resource manager already shutdone" << std::endl;
instance_ptr = new resource::ResourceManagerImpl();
}
return instance_ptr;
}

void ResourceManager::Shutdown() {
if (instance_ptr != nullptr) {
delete instance_ptr;
instance_ptr = nullptr;
rm_shutdown = true;
}
}
} // namespace mxnet
4 changes: 2 additions & 2 deletions src/storage/cpu_device_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class CPUDeviceStorage {
}; // class CPUDeviceStorage

inline void* CPUDeviceStorage::Alloc(size_t size) {
#if _MSC_VER
#if defined(_MSC_VER) || defined(_WIN32)
void* ptr;
ptr = _aligned_malloc(size, alignment_);
return CHECK_NOTNULL(ptr);
Expand All @@ -51,7 +51,7 @@ inline void* CPUDeviceStorage::Alloc(size_t size) {
}

inline void CPUDeviceStorage::Free(void* ptr) {
#if _MSC_VER
#if defined(_MSC_VER) || defined(_WIN32)
_aligned_free(ptr);
#else
free(ptr);
Expand Down
11 changes: 7 additions & 4 deletions src/symbol/graph_executor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,13 @@ GraphExecutor::GetOpExecEntry(uint32_t nid) {
}

GraphExecutor::~GraphExecutor() {
Engine::Get()->WaitForAll();
// need to delete the operators before delete the NDArray they referenced.
for (OpNode& node : op_nodes_) {
node.DeleteOperator();
auto engine_ptr = Engine::Get();
if (engine_ptr) {
engine_ptr->WaitForAll();
// need to delete the operators before delete the NDArray they referenced.
for (OpNode& node : op_nodes_) {
node.DeleteOperator();
}
}
}

Expand Down
Loading

0 comments on commit 8270509

Please sign in to comment.