Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
superchromix committed Aug 9, 2017
0 parents commit 32ac063
Show file tree
Hide file tree
Showing 120 changed files with 13,531 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Python
**/.idea
__pycache__

# docs
/docs/_build

159 changes: 159 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
# Levenberg Marquardt curve fitting in CUDA
# https://github.com/gpufit/Gpufit
# see also CMake configuration in /docs/installation.rst

# CMake

cmake_minimum_required( VERSION 3.7 )
set_property( GLOBAL PROPERTY USE_FOLDERS ON )

if( NOT PROJECT_NAME )
project( Gpufit VERSION 1.0.0 )
include( CTest )
endif()

if( MSVC ) # link runtime statically
foreach( type ${CMAKE_CONFIGURATION_TYPES} ${CMAKE_BUILD_TYPE} )
string( TOUPPER ${type} TYPE )
foreach( flags CMAKE_C_FLAGS_${TYPE} CMAKE_CXX_FLAGS_${TYPE} )
get_property( help CACHE ${flags} PROPERTY HELPSTRING )
string( REPLACE "/MD" "/MT" ${flags} "${${flags}}" )
set( ${flags} "${${flags}}" CACHE STRING "${help}" FORCE )
endforeach()
endforeach()
endif()

function( add_launcher target executable arguments working_directory )
if( MSVC12 OR MSVC14 )
file( WRITE ${CMAKE_CURRENT_BINARY_DIR}/${target}.vcxproj.user
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<Project ToolsVersion=\"14.0\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">\n"
" <PropertyGroup>\n"
" <LocalDebuggerCommand>${executable}</LocalDebuggerCommand>\n"
" <LocalDebuggerCommandArguments>${arguments}</LocalDebuggerCommandArguments>\n"
" <LocalDebuggerWorkingDirectory>${working_directory}</LocalDebuggerWorkingDirectory>\n"
" </PropertyGroup>\n"
"</Project>\n"
)
endif()
endfunction()

# Boost

find_package( Boost 1.58.0 )
if( Boost_FOUND )
function( add_boost_test modules name )
string( REPLACE ";" "_" prefix "${modules}" )
set( target ${prefix}_Test_${name} )
add_executable( ${target} ${name}.cpp
${PROJECT_SOURCE_DIR}/Tests/utils.h
${PROJECT_SOURCE_DIR}/Tests/utils.cpp
)
target_include_directories( ${target} PRIVATE ${PROJECT_SOURCE_DIR} )
target_link_libraries( ${target} ${modules} Boost::boost )
set_property( TARGET ${target}
PROPERTY RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}" )
set_property( TARGET ${target} PROPERTY FOLDER Tests )

add_test( NAME ${target}
COMMAND ${target} --build_info --log_level=all --report_level=detailed )
endfunction()
else()
set( BUILD_TESTING OFF )
message( WARNING "Boost NOT found - skipping tests! (set BOOST_ROOT manually)" )
endif()

# MATLAB

find_package( Matlab )
if( Matlab_FOUND )
find_program( Matlab_EXECUTABLE matlab
PATHS "${Matlab_ROOT_DIR}/bin" PATH_SUFFIXES win32 win64 NO_DEFAULT_PATH )
function( add_matlab_launcher target )
set( paths "${CMAKE_BINARY_DIR}/$(Configuration)" ${ARGN} )
list( GET paths -1 working_directory )
string( REPLACE ";" "','" paths "${paths}" )
set( arguments "-r addpath('${paths}');addpath(genpath(pwd))" )
add_launcher( ${target} "${Matlab_EXECUTABLE}" "${arguments}" "${working_directory}" )
endfunction()
endif()

# Python

find_package( PythonInterp )
if( PYTHONINTERP_FOUND )
function( add_python_launcher target )
set( paths "${CMAKE_BINARY_DIR}/$(Configuration)" ${ARGN} )
list( GET paths -1 working_directory )
string( REPLACE ";" "')\nsys.path.append('" paths "${paths}" )
set( arguments "-i -c \"import sys\nsys.path.append('${paths}')\"" )
add_launcher( ${target} "${PYTHON_EXECUTABLE}" "${arguments}" "${working_directory}" )
endfunction()
endif()

# Cpufit

add_subdirectory( Cpufit )

# Gpufit

add_subdirectory( Gpufit )

# Examples using Gpufit and Cpufit

add_subdirectory( examples )

# Launcher
#
# Uses the following variables:
#
# Matlab_WORKING_DIRECTORY (Default: user home directory)
# -- Working directory for MATLAB applications using Cpufit and Gpufit.
# Python_WORKING_DIRECTORY (Default: user home directory)
# -- Working directory for Python applications using Gpufit.

if( WIN32 )
file( TO_CMAKE_PATH "$ENV{HOMEPATH}" home )
else()
file( TO_CMAKE_PATH "$ENV{HOME}" home )
endif()

if( Matlab_FOUND )
set( Matlab_WORKING_DIRECTORY "${home}" CACHE PATH "MATLAB working directory" )
if( Matlab_WORKING_DIRECTORY )
add_custom_target( RUN_MATLAB )
set_property( TARGET RUN_MATLAB PROPERTY FOLDER CMakePredefinedTargets )
add_dependencies( RUN_MATLAB CpufitMex GpufitMex )
add_matlab_launcher( RUN_MATLAB
"${CMAKE_SOURCE_DIR}/Cpufit/matlab"
"${CMAKE_SOURCE_DIR}/Gpufit/matlab"
"${Matlab_WORKING_DIRECTORY}"
)
endif()
endif()

if( PYTHONINTERP_FOUND )
set( Python_WORKING_DIRECTORY "${home}" CACHE PATH "Python working directory" )
if( Python_WORKING_DIRECTORY )
add_custom_target( RUN_PYTHON )
set_property( TARGET RUN_PYTHON PROPERTY FOLDER CMakePredefinedTargets )
add_dependencies( RUN_PYTHON Gpufit )
add_python_launcher( RUN_PYTHON
"${CMAKE_SOURCE_DIR}/Gpufit/python"
"${Python_WORKING_DIRECTORY}"
)
endif()
endif()

# Tests

if( BUILD_TESTING )
add_subdirectory( tests )
endif()

# Package

#set( CPACK_PACKAGE_VERSION ${PROJECT_VERSION} )
#set( CPACK_GENERATOR ZIP )

#include( CPack )
29 changes: 29 additions & 0 deletions Cpufit/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

# Cpufit

set( CpuHeaders
Cpufit.h
info.h
lm_fit.h
interface.h
)

set( CpuSources
Cpufit.cpp
info.cpp
lm_fit.cpp
lm_fit_cpp.cpp
interface.cpp
Cpufit.def
)

add_library( Cpufit SHARED
${CpuHeaders}
${CpuSources}
)
set_property( TARGET Cpufit
PROPERTY RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}" )

#install( TARGETS Cpufit RUNTIME DESTINATION bin )

add_subdirectory( matlab )
4 changes: 4 additions & 0 deletions Cpufit/Cpufit.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
LIBRARY "Cpufit"
EXPORTS
cpufit @1
cpufit_get_last_error @2
1 change: 1 addition & 0 deletions Cpufit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Cpufit
76 changes: 76 additions & 0 deletions Cpufit/cpufit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include "cpufit.h"
#include "interface.h"

#include <string>

std::string last_error ;

int cpufit
(
size_t n_fits,
size_t n_points,
float * data,
float * weights,
int model_id,
float * initial_parameters,
float tolerance,
int max_n_iterations,
int * parameters_to_fit,
int estimator_id,
size_t user_info_size,
char * user_info,
float * output_parameters,
int * output_states,
float * output_chi_squares,
int * output_n_iterations
)
try
{
__int32 n_points_32 = 0;
if (n_points <= (unsigned int)(std::numeric_limits<__int32>::max()))
{
n_points_32 = __int32(n_points);
}
else
{
throw std::runtime_error("maximum number of data points per fit exceeded");
}

FitInterface fi(
data,
weights,
n_fits,
n_points_32,
tolerance,
max_n_iterations,
estimator_id,
initial_parameters,
parameters_to_fit,
user_info,
user_info_size,
output_parameters,
output_states,
output_chi_squares,
output_n_iterations);

fi.fit(model_id);

return STATUS_OK;
}
catch (std::exception & exception)
{
last_error = exception.what();

return STATUS_ERROR;
}
catch (...)
{
last_error = "Unknown Error";

return STATUS_ERROR;
}

char const * cpufit_get_last_error()
{
return last_error.c_str();
}
56 changes: 56 additions & 0 deletions Cpufit/cpufit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#ifndef CPU_FIT_H_INCLUDED
#define CPU_FIT_H_INCLUDED

// fitting model ID
#define GAUSS_1D 0
#define GAUSS_2D 1
#define GAUSS_2D_ELLIPTIC 2
#define GAUSS_2D_ROTATED 3
#define CAUCHY_2D_ELLIPTIC 4
#define LINEAR_1D 5

// estimator ID
#define LSE 0
#define MLE 1

// fit state
#define STATE_CONVERGED 0
#define STATE_MAX_ITERATION 1
#define STATE_SINGULAR_HESSIAN 2
#define STATE_NEG_CURVATURE_MLE 3

// cpufit return state
#define STATUS_OK 0
#define STATUS_ERROR -1

#ifdef __cplusplus
extern "C" {
#endif

int cpufit
(
size_t n_fits,
size_t n_points,
float * data,
float * weights,
int model_id,
float * initial_parameters,
float tolerance,
int max_n_iterations,
int * parameters_to_fit,
int estimator_id,
size_t user_info_size,
char * user_info,
float * output_parameters,
int * output_states,
float * output_chi_squares,
int * output_n_iterations
) ;

char const * cpufit_get_last_error() ;

#ifdef __cplusplus
}
#endif

#endif // CPU_FIT_H_INCLUDED
30 changes: 30 additions & 0 deletions Cpufit/info.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "info.h"

Info::Info(void) :
n_parameters_(0),
n_parameters_to_fit_(0),
max_n_iterations_(0),
n_fits_(0),
n_points_(0),
model_id_(0),
estimator_id_(0),
user_info_size_(0)
{
}

Info::~Info(void)
{
}

void Info::set_number_of_parameters_to_fit(int const * parameters_to_fit)
{
n_parameters_to_fit_ = n_parameters_;

for (int i = 0; i < n_parameters_; i++)
{
if (!parameters_to_fit[i])
{
n_parameters_to_fit_--;
}
}
}
Loading

0 comments on commit 32ac063

Please sign in to comment.