Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SINGA-236 memory pool (update from an old branch) #254

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ OPTION(USE_CUDA "Use Cuda libs" OFF)
OPTION(USE_CUDNN "Use Cudnn libs" ON)
OPTION(USE_OPENCV "Use opencv" OFF)
OPTION(USE_LMDB "Use LMDB libs" OFF)
OPTION(USE_PYTHON "Generate py wrappers" OFF)
OPTION(USE_PYTHON "Generate py wrappers" ON)
OPTION(USE_JAVA "Generate java wrappers" OFF)
OPTION(USE_OPENCL "Use OpenCL" OFF)
OPTION(ENABLE_DIST "enable distributed training" OFF)

INCLUDE("cmake/Dependencies.cmake")
INCLUDE("cmake/Utils.cmake")
#INCLUDE("cmake/Utils.cmake")
ADD_DEFINITIONS(-DUSE_CMAKE)
#message(STATUS "${SINGA_INCLUDE_DIR}")

Expand All @@ -64,17 +65,28 @@ SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
IF (USE_CUDA)
ADD_SUBDIRECTORY(lib/cnmem)
LIST(APPEND SINGA_LINKER_LIBS cnmem)
SET(global_cuda_objs "")
ENDIF()

# TODO(wangwei) detect the ev lib
IF (ENABLE_DIST)
LIST(APPEND SINGA_LINKER_LIBS ev)
ENDIF()

INCLUDE_DIRECTORIES("${CMAKE_BINARY_DIR}/include")
ADD_SUBDIRECTORY(src)
ADD_SUBDIRECTORY(test)
ADD_SUBDIRECTORY(examples)

IF (USE_PYTHON)
ADD_SUBDIRECTORY(python)
ENDIF()

IF (USE_JAVA)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing ")
ADD_SUBDIRECTORY(java)
ENDIF()

INSTALL(DIRECTORY include/singa DESTINATION ${CMAKE_INSTALL_PREFIX}/include)
INSTALL(FILES ${CMAKE_BINARY_DIR}/include/singa/singa_config.h DESTINATION
${CMAKE_INSTALL_PREFIX}/include/singa)
Expand Down
8 changes: 7 additions & 1 deletion cmake/Dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ FIND_PACKAGE( Protobuf REQUIRED )
INCLUDE_DIRECTORIES(SYSTEM ${PROTOBUF_INCLUDE_DIR})
MESSAGE(STATUS "proto libs " ${PROTOBUF_LIBRARIES})
LIST(APPEND SINGA_LINKER_LIBS ${PROTOBUF_LIBRARIES})
INCLUDE("cmake/Protobuf.cmake")
#INCLUDE("cmake/Protobuf.cmake")

FIND_PACKAGE(Glog)
IF(GLOG_FOUND)
Expand Down Expand Up @@ -92,3 +92,9 @@ IF(USE_PYTHON)
FIND_PACKAGE(PythonInterp 2.7 REQUIRED)
FIND_PACKAGE(SWIG 3.0 REQUIRED)
ENDIF()

IF(USE_JAVA)
FIND_PACKAGE(Java)
FIND_PACKAGE(JNI )
FIND_PACKAGE(SWIG 3.0 REQUIRED)
ENDIF()
31 changes: 0 additions & 31 deletions cmake/Protobuf.cmake

This file was deleted.

70 changes: 0 additions & 70 deletions cmake/Utils.cmake

This file was deleted.

15 changes: 11 additions & 4 deletions include/singa/core/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#define SINGA_CORE_COMMON_H_
#include <random>
#include <chrono>
#include "./singa/singa_config.h"
#include "singa/singa_config.h"
#include <atomic>
#include <memory>
#include "singa/utils/logging.h"
Expand All @@ -36,7 +36,10 @@


#ifdef USE_OPENCL
#include "singa/utils/opencl_utils.h"
#define CL_HPP_MINIMUM_OPENCL_VERSION 120
#define CL_HPP_TARGET_OPENCL_VERSION 120
#include <CL/cl2.hpp>
#include <unordered_map>
#endif // USE_OPENCL

using std::atomic;
Expand All @@ -62,6 +65,9 @@ class Block {
// Disabled as it is not used currently.
// Block(void* ptr, size_t size, size_t offset, std::shared_ptr<atomic<int>>
// ref) : data_(ptr), size_(size), offset_(offset), ref_count_(ref) {}

// TODO(wangwei) check if the set is correct and add lock if shared sturcture is allowed
void set_data(void* ptr) { data_ = ptr; }
void* mutable_data() {
initialized_ = true;
return static_cast<char*>(data_) + offset_;
Expand Down Expand Up @@ -107,8 +113,9 @@ typedef struct _Context {
#endif // USE_CUDA

#ifdef USE_OPENCL
// This stores the context ID of the OpenCL context controlled by ViennaCL.
long vcl_ctx_id;
std::shared_ptr<std::unordered_map<std::string, cl::Kernel>> kernels;
cl::CommandQueue ocl_cmdq;
cl::Context ocl_ctx;
#endif

} Context;
Expand Down
32 changes: 32 additions & 0 deletions include/singa/core/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <atomic>
#include "singa/proto/core.pb.h"
#include "singa/singa_config.h"
#include "singa/core/common.h"

#ifdef USE_CUDA
#include "cnmem.h"
Expand Down Expand Up @@ -50,6 +51,37 @@ class DeviceMemPool {
// size_t init_size_ = 0, max_size_ = 0;
};

class CppMemPool {
public:
CppMemPool();

Block* Malloc(const size_t size);
void Free(Block* ptr);

// get the free and total size of the memory pool (in terms of bytes)
std::pair<size_t, size_t> GetMemUsage(){return std::make_pair(freeSize,memPoolSize);};

~CppMemPool();

private:
// each structure define a memory uint in the memory pool
// the structure is a static double linked list
struct _Uint {
struct _Uint *pPrev, *pNext;
Block* pBlk;
};

// total size held by the memory pool (in terms of bytes)
size_t memPoolSize;
// total free size by the memory pool (in terms of bytes)
size_t freeSize;

// each pointer in this array keeps a head of the allocated memory uints of different size (power of 2)
struct _Uint **ppAllocUints;
// each pointer in this array keeps a head of the allocated memory uints of different size (power of 2)
struct _Uint **ppFreeUints;
};

#ifdef USE_CUDA
class CnMemPool : public DeviceMemPool {
public:
Expand Down
138 changes: 138 additions & 0 deletions java/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
#

# This following function is taken from
# https://github.com/Kitware/CMake/blob/master/Modules/FindProtobuf.cmake
# and modified to our compilation.
function(PROTOBUF_GENERATE_JAVA OUTPUT)
if(NOT ARGN)
message(SEND_ERROR "Error: PROTOBUF_GENERATE_JAVA() called
without any proto files")
return()
endif(NOT ARGN)

set(${OUTPUT})
foreach(FIL ${ARGN})
get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
get_filename_component(FIL_WE ${FIL} NAME_WE)
get_filename_component(PATH ${FIL} PATH)

list(APPEND ${OUTPUT} "${CMAKE_BINARY_DIR}/java/singa/proto/${FIL_WE}.java")

add_custom_command(
OUTPUT "${CMAKE_BINARY_DIR}/java/singa/proto/${FIL_WE}.java"
COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
ARGS --java_out ${CMAKE_BINARY_DIR}/java
--proto_path ${PATH} ${ABS_FIL}
DEPENDS ${ABS_FIL}
COMMENT "Running java protocol buffer compiler on ${FIL}" VERBATIM)
endforeach()

set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE)
set(${OUTPUT} ${${OUTPUT}} PARENT_SCOPE)
endfunction()

function (create_symlinks)
# Do nothing if building in-source
if (${CMAKE_CURRENT_BINARY_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
return()
endif()

foreach (path_file ${ARGN})
get_filename_component(folder ${path_file} PATH)

# Delete symlink if it exists
file(REMOVE "${CMAKE_CURRENT_BINARY_DIR}/${path_file}")

# Get OS dependent path to use in `execute_process`
file(TO_NATIVE_PATH "${CMAKE_CURRENT_BINARY_DIR}/${path_file}" link)
file(TO_NATIVE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/${path_file}" target)

if (UNIX)
set(command ln -s ${target} ${link})
else()
set(command cmd.exe /c mklink ${link} ${target})
endif()

execute_process(COMMAND ${command}
RESULT_VARIABLE result
ERROR_VARIABLE output)

if (NOT ${result} EQUAL 0)
message(FATAL_ERROR "Could not create symbolic link for: ${target} --> ${output}")
endif()

endforeach(path_file)
endfunction(create_symlinks)


# generate protobuf sources
FILE(GLOB proto_files ${CMAKE_SOURCE_DIR}/src/proto/*.proto)
PROTOBUF_GENERATE_JAVA(proto_javas ${proto_files})
MESSAGE(STATUS "proto javas: ${proto_javas}")

# generate cxx and wrap.py
if(NOT EXISTS "${CMKAE_BINARY_DIR}/java")
execute_process(
COMMAND mkdir ${CMAKE_BINARY_DIR}/java
COMMAND mkdir ${CMAKE_BINARY_DIR}/java/singa
COMMAND mkdir ${CMAKE_BINARY_DIR}/java/singa/proto
ERROR_QUIET)
endif()
execute_process(
COMMAND mkdir ${CMAKE_BINARY_DIR}/src/api
COMMAND swig -c++ -java -I${CMAKE_SOURCE_DIR}/include
-I${JAVA_INCLUDE_PATH} -I${JAVA_INCLUDE_PATH2}
-outdir ${CMAKE_BINARY_DIR}/java/singa
-package singa
-o ${CMAKE_BINARY_DIR}/src/api/singa_java_wrap.cxx
${CMAKE_SOURCE_DIR}/src/api/singa.i )

#MESSAGE(STATUS "java include: ${JAVA_INCLUDE_DIRS} ${JNI_INCLUDE_DIRS} ${JAVA_INCLUDE_PATH} ${JAVA_INCLUDE_PATH2}")
set(java_srcs "${CMAKE_BINARY_DIR}/src/api/singa_java_wrap.cxx")

#Create symlinks for all java source files Do not omit !!!RELATIVE!!!
file(GLOB_RECURSE java_source_files RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.java)
create_symlinks(${java_source_files})

IF(USE_CUDA)
# remain this custom command to avoid cuda objs can't find
ADD_CUSTOM_COMMAND(
OUTPUT ${global_cuda_objs}
COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_BINARY_DIR}/"
)
ENDIF(USE_CUDA)


ADD_LIBRARY(singa_wrap SHARED $<TARGET_OBJECTS:singa_objects> ${java_srcs} ${proto_javas} ${global_cuda_objs})
TARGET_LINK_LIBRARIES(singa_wrap ${SINGA_LINKER_LIBS} ${JNI_LIBRARIES})
TARGET_INCLUDE_DIRECTORIES(singa_wrap PRIVATE ${JAVA_INCLUDE_PATH} ${JAVA_INCLUDE_PATH2})

SET_TARGET_PROPERTIES(singa_wrap
PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/java/singa
)

IF(APPLE)
ADD_CUSTOM_TARGET(
change_suffix ALL
COMMAND ${CMAKE_COMMAND} -E rename "${CMAKE_BINARY_DIR}/java/singa/libsinga_wrap.dylib" "${CMAKE_BINARY_DIR}/java/singa/libsinga_wrap.so"
COMMENT "change .dylib to .so in mac system"
)
ADD_DEPENDENCIES(change_suffix _singa_wrap)
ENDIF(APPLE)

Loading