Skip to content
This repository has been archived by the owner on Jun 13, 2024. It is now read-only.

Commit

Permalink
Merge pull request #23 from lunzueta/master
Browse files Browse the repository at this point in the history
matcaffe VS project and scripts added.
  • Loading branch information
pavlejosipovic committed Mar 2, 2016
2 parents b31b214 + bc07e06 commit 6cb3194
Show file tree
Hide file tree
Showing 11 changed files with 319 additions and 112 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ After you have build solution with Python support, in order to use it you have t
1) set PythonPath environment variable to point to <caffe_root>\Build\x64\Release\pycaffe
or
2) cp –r <caffe_root>\Build\x64\Release\pycaffe\caffe $PYTHON_DIR\lib\site-packages

### Matlab
To build Caffe Matlab wrapper set `MatlabSupport` to `true` and `MatlabDir` to the root of your Matlab installation in `.\windows\CommonSettings.props`.

#### Remark
After you have build solution with Matlab support, in order to use it you have to set the generated matcaffe folder in the Matlab search path.

### Build
Now, you should be able to build `.\windows\Caffe.sln`

Expand Down
220 changes: 110 additions & 110 deletions cmake/Modules/FindMKL.cmake
Original file line number Diff line number Diff line change
@@ -1,110 +1,110 @@
# Find the MKL libraries
#
# Options:
#
# MKL_USE_SINGLE_DYNAMIC_LIBRARY : use single dynamic library interface
# MKL_USE_STATIC_LIBS : use static libraries
# MKL_MULTI_THREADED : use multi-threading
#
# This module defines the following variables:
#
# MKL_FOUND : True mkl is found
# MKL_INCLUDE_DIR : unclude directory
# MKL_LIBRARIES : the libraries to link against.


# ---[ Options
caffe_option(MKL_USE_SINGLE_DYNAMIC_LIBRARY "Use single dynamic library interface" ON)
caffe_option(MKL_USE_STATIC_LIBS "Use static libraries" OFF IF NOT MKL_USE_SINGLE_DYNAMIC_LIBRARY)
caffe_option(MKL_MULTI_THREADED "Use multi-threading" ON IF NOT MKL_USE_SINGLE_DYNAMIC_LIBRARY)

# ---[ Root folders
set(INTEL_ROOT "/opt/intel" CACHE PATH "Folder contains intel libs")
find_path(MKL_ROOT include/mkl.h PATHS $ENV{MKL_ROOT} ${INTEL_ROOT}/mkl
DOC "Folder contains MKL")

# ---[ Find include dir
find_path(MKL_INCLUDE_DIR mkl.h PATHS ${MKL_ROOT} PATH_SUFFIXES include)
set(__looked_for MKL_INCLUDE_DIR)

# ---[ Find libraries
if(CMAKE_SIZEOF_VOID_P EQUAL 4)
set(__path_suffixes lib lib/ia32)
else()
set(__path_suffixes lib lib/intel64)
endif()

set(__mkl_libs "")
if(MKL_USE_SINGLE_DYNAMIC_LIBRARY)
list(APPEND __mkl_libs rt)
else()
if(CMAKE_SIZEOF_VOID_P EQUAL 4)
if(WIN32)
list(APPEND __mkl_libs intel_c)
else()
list(APPEND __mkl_libs intel gf)
endif()
else()
list(APPEND __mkl_libs intel_lp64 gf_lp64)
endif()

if(MKL_MULTI_THREADED)
list(APPEND __mkl_libs intel_thread)
else()
list(APPEND __mkl_libs sequential)
endif()

list(APPEND __mkl_libs core cdft_core)
endif()


foreach (__lib ${__mkl_libs})
set(__mkl_lib "mkl_${__lib}")
string(TOUPPER ${__mkl_lib} __mkl_lib_upper)

if(MKL_USE_STATIC_LIBS)
set(__mkl_lib "lib${__mkl_lib}.a")
endif()

find_library(${__mkl_lib_upper}_LIBRARY
NAMES ${__mkl_lib}
PATHS ${MKL_ROOT} "${MKL_INCLUDE_DIR}/.."
PATH_SUFFIXES ${__path_suffixes}
DOC "The path to Intel(R) MKL ${__mkl_lib} library")
mark_as_advanced(${__mkl_lib_upper}_LIBRARY)

list(APPEND __looked_for ${__mkl_lib_upper}_LIBRARY)
list(APPEND MKL_LIBRARIES ${${__mkl_lib_upper}_LIBRARY})
endforeach()


if(NOT MKL_USE_SINGLE_DYNAMIC_LIBRARY)
if (MKL_USE_STATIC_LIBS)
set(__iomp5_libs iomp5 libiomp5mt.lib)
else()
set(__iomp5_libs iomp5 libiomp5md.lib)
endif()

if(WIN32)
find_path(INTEL_INCLUDE_DIR omp.h PATHS ${INTEL_ROOT} PATH_SUFFIXES include)
list(APPEND __looked_for INTEL_INCLUDE_DIR)
endif()

find_library(MKL_RTL_LIBRARY ${__iomp5_libs}
PATHS ${INTEL_RTL_ROOT} ${INTEL_ROOT}/compiler ${MKL_ROOT}/.. ${MKL_ROOT}/../compiler
PATH_SUFFIXES ${__path_suffixes}
DOC "Path to Path to OpenMP runtime library")

list(APPEND __looked_for MKL_RTL_LIBRARY)
list(APPEND MKL_LIBRARIES ${MKL_RTL_LIBRARY})
endif()


include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(MKL DEFAULT_MSG ${__looked_for})

if(MKL_FOUND)
message(STATUS "Found MKL (include: ${MKL_INCLUDE_DIR}, lib: ${MKL_LIBRARIES}")
endif()

caffe_clear_vars(__looked_for __mkl_libs __path_suffixes __lib_suffix __iomp5_libs)
# Find the MKL libraries
#
# Options:
#
# MKL_USE_SINGLE_DYNAMIC_LIBRARY : use single dynamic library interface
# MKL_USE_STATIC_LIBS : use static libraries
# MKL_MULTI_THREADED : use multi-threading
#
# This module defines the following variables:
#
# MKL_FOUND : True mkl is found
# MKL_INCLUDE_DIR : unclude directory
# MKL_LIBRARIES : the libraries to link against.


# ---[ Options
caffe_option(MKL_USE_SINGLE_DYNAMIC_LIBRARY "Use single dynamic library interface" ON)
caffe_option(MKL_USE_STATIC_LIBS "Use static libraries" OFF IF NOT MKL_USE_SINGLE_DYNAMIC_LIBRARY)
caffe_option(MKL_MULTI_THREADED "Use multi-threading" ON IF NOT MKL_USE_SINGLE_DYNAMIC_LIBRARY)

# ---[ Root folders
set(INTEL_ROOT "/opt/intel" CACHE PATH "Folder contains intel libs")
find_path(MKL_ROOT include/mkl.h PATHS $ENV{MKL_ROOT} ${INTEL_ROOT}/mkl
DOC "Folder contains MKL")

# ---[ Find include dir
find_path(MKL_INCLUDE_DIR mkl.h PATHS ${MKL_ROOT} PATH_SUFFIXES include)
set(__looked_for MKL_INCLUDE_DIR)

# ---[ Find libraries
if(CMAKE_SIZEOF_VOID_P EQUAL 4)
set(__path_suffixes lib lib/ia32)
else()
set(__path_suffixes lib lib/intel64)
endif()

set(__mkl_libs "")
if(MKL_USE_SINGLE_DYNAMIC_LIBRARY)
list(APPEND __mkl_libs rt)
else()
if(CMAKE_SIZEOF_VOID_P EQUAL 4)
if(WIN32)
list(APPEND __mkl_libs intel_c)
else()
list(APPEND __mkl_libs intel gf)
endif()
else()
list(APPEND __mkl_libs intel_lp64 gf_lp64)
endif()

if(MKL_MULTI_THREADED)
list(APPEND __mkl_libs intel_thread)
else()
list(APPEND __mkl_libs sequential)
endif()

list(APPEND __mkl_libs core cdft_core)
endif()


foreach (__lib ${__mkl_libs})
set(__mkl_lib "mkl_${__lib}")
string(TOUPPER ${__mkl_lib} __mkl_lib_upper)

if(MKL_USE_STATIC_LIBS)
set(__mkl_lib "lib${__mkl_lib}.a")
endif()

find_library(${__mkl_lib_upper}_LIBRARY
NAMES ${__mkl_lib}
PATHS ${MKL_ROOT} "${MKL_INCLUDE_DIR}/.."
PATH_SUFFIXES ${__path_suffixes}
DOC "The path to Intel(R) MKL ${__mkl_lib} library")
mark_as_advanced(${__mkl_lib_upper}_LIBRARY)

list(APPEND __looked_for ${__mkl_lib_upper}_LIBRARY)
list(APPEND MKL_LIBRARIES ${${__mkl_lib_upper}_LIBRARY})
endforeach()


if(NOT MKL_USE_SINGLE_DYNAMIC_LIBRARY)
if (MKL_USE_STATIC_LIBS)
set(__iomp5_libs iomp5 libiomp5mt.lib)
else()
set(__iomp5_libs iomp5 libiomp5md.lib)
endif()

if(WIN32)
find_path(INTEL_INCLUDE_DIR omp.h PATHS ${INTEL_ROOT} PATH_SUFFIXES include)
list(APPEND __looked_for INTEL_INCLUDE_DIR)
endif()

find_library(MKL_RTL_LIBRARY ${__iomp5_libs}
PATHS ${INTEL_RTL_ROOT} ${INTEL_ROOT}/compiler ${MKL_ROOT}/.. ${MKL_ROOT}/../compiler
PATH_SUFFIXES ${__path_suffixes}
DOC "Path to Path to OpenMP runtime library")

list(APPEND __looked_for MKL_RTL_LIBRARY)
list(APPEND MKL_LIBRARIES ${MKL_RTL_LIBRARY})
endif()


include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(MKL DEFAULT_MSG ${__looked_for})

if(MKL_FOUND)
message(STATUS "Found MKL (include: ${MKL_INCLUDE_DIR}, lib: ${MKL_LIBRARIES}")
endif()

caffe_clear_vars(__looked_for __mkl_libs __path_suffixes __lib_suffix __iomp5_libs)
2 changes: 1 addition & 1 deletion matlab/+caffe/+test/test_solver.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

fid = fopen(solver_file, 'w');
fprintf(fid, [ ...
'net: "' model_file '"\n' ...
'net: "' strrep(model_file, '\', '/') '"\n' ...
'test_iter: 10 test_interval: 10 base_lr: 0.01 momentum: 0.9\n' ...
'weight_decay: 0.0005 lr_policy: "inv" gamma: 0.0001 power: 0.75\n' ...
'display: 100 max_iter: 100 snapshot_after_train: false\n' ]);
Expand Down
11 changes: 11 additions & 0 deletions matlab/+caffe/private/caffe_.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,14 @@ void mxCHECK_FILE_EXIST(const char* file) {
static vector<shared_ptr<Solver<float> > > solvers_;
static vector<shared_ptr<Net<float> > > nets_;
// init_key is generated at the beginning and everytime you call reset
#ifndef _MSC_VER // We are not using MSVC.
static double init_key = static_cast<double>(caffe_rng_rand());
#else // We are using MSVC.
// The original statement may cause MATLAB halt on Windows when cuBLAS is used.
// Using a negative number as a flag instead of calling caffe_rng_rand().
// init_key will be generated in entry function: mexFunction().
static double init_key = -1;
#endif // !_MSC_VER

/** -----------------------------------------------------------------
** data conversion functions
Expand Down Expand Up @@ -559,6 +566,10 @@ static handler_registry handlers[] = {
**/
// Usage: caffe_(api_command, arg1, arg2, ...)
void mexFunction(MEX_ARGS) {
#ifdef _MSC_VER
if (init_key == -1)
init_key = static_cast<double>(caffe_rng_rand());
#endif // _MSC_VER
mexLock(); // Avoid clearing the mex file.
mxCHECK(nrhs > 0, "Usage: caffe_(api_command, arg1, arg2, ...)");
// Handle input command
Expand Down
11 changes: 11 additions & 0 deletions windows/Caffe.sln
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "pycaffe", "pycaffe\pycaffe.
{CE6BBC46-9EFC-4029-9065-85A023866AFB} = {CE6BBC46-9EFC-4029-9065-85A023866AFB}
EndProjectSection
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "matcaffe", "matcaffe\matcaffe.vcxproj", "{7173D611-3A7A-4F07-943A-727C6862E8D5}"
ProjectSection(ProjectDependencies) = postProject
{CE6BBC46-9EFC-4029-9065-85A023866AFB} = {CE6BBC46-9EFC-4029-9065-85A023866AFB}
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "props", "props", "{632DD6E1-28DF-42F9-AD7F-1C1F2D38765C}"
ProjectSection(SolutionItems) = preProject
CommonSettings.props = CommonSettings.props
Expand All @@ -44,6 +49,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "scripts", "scripts", "{E2EF
scripts\ProtoCompile.cmd = scripts\ProtoCompile.cmd
scripts\PythonPostBuild.cmd = scripts\PythonPostBuild.cmd
scripts\PythonPreBuild.cmd = scripts\PythonPreBuild.cmd
scripts\MatlabPostBuild.cmd = scripts\MatlabPostBuild.cmd
scripts\MatlabPreBuild.cmd = scripts\MatlabPreBuild.cmd
EndProjectSection
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "caffe.managed", "caffe.managed\caffe.managed.vcxproj", "{350020CA-75B9-425E-85CA-DD8CF8CFBA9B}"
Expand Down Expand Up @@ -82,6 +89,10 @@ Global
{38B6CE09-4B1A-4E72-A547-8A3299D8DA60}.Debug|x64.Build.0 = Debug|x64
{38B6CE09-4B1A-4E72-A547-8A3299D8DA60}.Release|x64.ActiveCfg = Release|x64
{38B6CE09-4B1A-4E72-A547-8A3299D8DA60}.Release|x64.Build.0 = Release|x64
{7173D611-3A7A-4F07-943A-727C6862E8D5}.Debug|x64.ActiveCfg = Debug|x64
{7173D611-3A7A-4F07-943A-727C6862E8D5}.Debug|x64.Build.0 = Debug|x64
{7173D611-3A7A-4F07-943A-727C6862E8D5}.Release|x64.ActiveCfg = Release|x64
{7173D611-3A7A-4F07-943A-727C6862E8D5}.Release|x64.Build.0 = Release|x64
{350020CA-75B9-425E-85CA-DD8CF8CFBA9B}.Debug|x64.ActiveCfg = Debug|x64
{350020CA-75B9-425E-85CA-DD8CF8CFBA9B}.Debug|x64.Build.0 = Debug|x64
{350020CA-75B9-425E-85CA-DD8CF8CFBA9B}.Release|x64.ActiveCfg = Release|x64
Expand Down
15 changes: 14 additions & 1 deletion windows/CommonSettings.props.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
set to the root of your Python installation. If your Python installation
does not contain debug libraries, debug build will not work. -->
<PythonSupport>false</PythonSupport>
<!-- NOTE: If Matlab support is enabled, MatlabDir (below) needs to be
set to the root of your Matlab installation. -->
<MatlabSupport>false</MatlabSupport>
<CudaDependencies></CudaDependencies>

<!-- Set CUDA architecture suitable for your GPU.
Expand Down Expand Up @@ -46,6 +49,11 @@
<LibraryPath>$(PythonDir)\libs;$(LibraryPath)</LibraryPath>
<IncludePath>$(PythonDir)\include;$(IncludePath)</IncludePath>
</PropertyGroup>
<PropertyGroup Condition="'$(MatlabSupport)'=='true'">
<MatlabDir>C:\Program Files\MATLAB\R2014b</MatlabDir>
<LibraryPath>$(MatlabDir)\extern\lib\win64\microsoft;$(LibraryPath)</LibraryPath>
<IncludePath>$(MatlabDir)\extern\include;$(IncludePath)</IncludePath>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(CpuOnlyBuild)'=='true'">
<ClCompile>
<PreprocessorDefinitions>CPU_ONLY;%(PreprocessorDefinitions)</PreprocessorDefinitions>
Expand All @@ -64,6 +72,11 @@
<PreprocessorDefinitions>WITH_PYTHON_LAYER;BOOST_PYTHON_STATIC_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(MatlabSupport)'=='true'">
<ClCompile>
<PreprocessorDefinitions>MATLAB_MEX_FILE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup>
<ClCompile>
<MinimalRebuild>false</MinimalRebuild>
Expand Down Expand Up @@ -96,4 +109,4 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
</Project>
</Project>
2 changes: 2 additions & 0 deletions windows/matcaffe/matcaffe.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
LIBRARY "caffe_.mexw64"
EXPORTS mexFunction
Loading

0 comments on commit 6cb3194

Please sign in to comment.