Skip to content

Commit 32ac063

Browse files
committed
Initial commit
0 parents  commit 32ac063

File tree

120 files changed

+13531
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

120 files changed

+13531
-0
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Python
2+
**/.idea
3+
__pycache__
4+
5+
# docs
6+
/docs/_build
7+

CMakeLists.txt

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
# Levenberg Marquardt curve fitting in CUDA
2+
# https://github.com/gpufit/Gpufit
3+
# see also CMake configuration in /docs/installation.rst
4+
5+
# CMake
6+
7+
cmake_minimum_required( VERSION 3.7 )
8+
set_property( GLOBAL PROPERTY USE_FOLDERS ON )
9+
10+
if( NOT PROJECT_NAME )
11+
project( Gpufit VERSION 1.0.0 )
12+
include( CTest )
13+
endif()
14+
15+
if( MSVC ) # link runtime statically
16+
foreach( type ${CMAKE_CONFIGURATION_TYPES} ${CMAKE_BUILD_TYPE} )
17+
string( TOUPPER ${type} TYPE )
18+
foreach( flags CMAKE_C_FLAGS_${TYPE} CMAKE_CXX_FLAGS_${TYPE} )
19+
get_property( help CACHE ${flags} PROPERTY HELPSTRING )
20+
string( REPLACE "/MD" "/MT" ${flags} "${${flags}}" )
21+
set( ${flags} "${${flags}}" CACHE STRING "${help}" FORCE )
22+
endforeach()
23+
endforeach()
24+
endif()
25+
26+
function( add_launcher target executable arguments working_directory )
27+
if( MSVC12 OR MSVC14 )
28+
file( WRITE ${CMAKE_CURRENT_BINARY_DIR}/${target}.vcxproj.user
29+
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
30+
"<Project ToolsVersion=\"14.0\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">\n"
31+
" <PropertyGroup>\n"
32+
" <LocalDebuggerCommand>${executable}</LocalDebuggerCommand>\n"
33+
" <LocalDebuggerCommandArguments>${arguments}</LocalDebuggerCommandArguments>\n"
34+
" <LocalDebuggerWorkingDirectory>${working_directory}</LocalDebuggerWorkingDirectory>\n"
35+
" </PropertyGroup>\n"
36+
"</Project>\n"
37+
)
38+
endif()
39+
endfunction()
40+
41+
# Boost
42+
43+
find_package( Boost 1.58.0 )
44+
if( Boost_FOUND )
45+
function( add_boost_test modules name )
46+
string( REPLACE ";" "_" prefix "${modules}" )
47+
set( target ${prefix}_Test_${name} )
48+
add_executable( ${target} ${name}.cpp
49+
${PROJECT_SOURCE_DIR}/Tests/utils.h
50+
${PROJECT_SOURCE_DIR}/Tests/utils.cpp
51+
)
52+
target_include_directories( ${target} PRIVATE ${PROJECT_SOURCE_DIR} )
53+
target_link_libraries( ${target} ${modules} Boost::boost )
54+
set_property( TARGET ${target}
55+
PROPERTY RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}" )
56+
set_property( TARGET ${target} PROPERTY FOLDER Tests )
57+
58+
add_test( NAME ${target}
59+
COMMAND ${target} --build_info --log_level=all --report_level=detailed )
60+
endfunction()
61+
else()
62+
set( BUILD_TESTING OFF )
63+
message( WARNING "Boost NOT found - skipping tests! (set BOOST_ROOT manually)" )
64+
endif()
65+
66+
# MATLAB
67+
68+
find_package( Matlab )
69+
if( Matlab_FOUND )
70+
find_program( Matlab_EXECUTABLE matlab
71+
PATHS "${Matlab_ROOT_DIR}/bin" PATH_SUFFIXES win32 win64 NO_DEFAULT_PATH )
72+
function( add_matlab_launcher target )
73+
set( paths "${CMAKE_BINARY_DIR}/$(Configuration)" ${ARGN} )
74+
list( GET paths -1 working_directory )
75+
string( REPLACE ";" "','" paths "${paths}" )
76+
set( arguments "-r addpath('${paths}');addpath(genpath(pwd))" )
77+
add_launcher( ${target} "${Matlab_EXECUTABLE}" "${arguments}" "${working_directory}" )
78+
endfunction()
79+
endif()
80+
81+
# Python
82+
83+
find_package( PythonInterp )
84+
if( PYTHONINTERP_FOUND )
85+
function( add_python_launcher target )
86+
set( paths "${CMAKE_BINARY_DIR}/$(Configuration)" ${ARGN} )
87+
list( GET paths -1 working_directory )
88+
string( REPLACE ";" "')\nsys.path.append('" paths "${paths}" )
89+
set( arguments "-i -c \"import sys\nsys.path.append('${paths}')\"" )
90+
add_launcher( ${target} "${PYTHON_EXECUTABLE}" "${arguments}" "${working_directory}" )
91+
endfunction()
92+
endif()
93+
94+
# Cpufit
95+
96+
add_subdirectory( Cpufit )
97+
98+
# Gpufit
99+
100+
add_subdirectory( Gpufit )
101+
102+
# Examples using Gpufit and Cpufit
103+
104+
add_subdirectory( examples )
105+
106+
# Launcher
107+
#
108+
# Uses the following variables:
109+
#
110+
# Matlab_WORKING_DIRECTORY (Default: user home directory)
111+
# -- Working directory for MATLAB applications using Cpufit and Gpufit.
112+
# Python_WORKING_DIRECTORY (Default: user home directory)
113+
# -- Working directory for Python applications using Gpufit.
114+
115+
if( WIN32 )
116+
file( TO_CMAKE_PATH "$ENV{HOMEPATH}" home )
117+
else()
118+
file( TO_CMAKE_PATH "$ENV{HOME}" home )
119+
endif()
120+
121+
if( Matlab_FOUND )
122+
set( Matlab_WORKING_DIRECTORY "${home}" CACHE PATH "MATLAB working directory" )
123+
if( Matlab_WORKING_DIRECTORY )
124+
add_custom_target( RUN_MATLAB )
125+
set_property( TARGET RUN_MATLAB PROPERTY FOLDER CMakePredefinedTargets )
126+
add_dependencies( RUN_MATLAB CpufitMex GpufitMex )
127+
add_matlab_launcher( RUN_MATLAB
128+
"${CMAKE_SOURCE_DIR}/Cpufit/matlab"
129+
"${CMAKE_SOURCE_DIR}/Gpufit/matlab"
130+
"${Matlab_WORKING_DIRECTORY}"
131+
)
132+
endif()
133+
endif()
134+
135+
if( PYTHONINTERP_FOUND )
136+
set( Python_WORKING_DIRECTORY "${home}" CACHE PATH "Python working directory" )
137+
if( Python_WORKING_DIRECTORY )
138+
add_custom_target( RUN_PYTHON )
139+
set_property( TARGET RUN_PYTHON PROPERTY FOLDER CMakePredefinedTargets )
140+
add_dependencies( RUN_PYTHON Gpufit )
141+
add_python_launcher( RUN_PYTHON
142+
"${CMAKE_SOURCE_DIR}/Gpufit/python"
143+
"${Python_WORKING_DIRECTORY}"
144+
)
145+
endif()
146+
endif()
147+
148+
# Tests
149+
150+
if( BUILD_TESTING )
151+
add_subdirectory( tests )
152+
endif()
153+
154+
# Package
155+
156+
#set( CPACK_PACKAGE_VERSION ${PROJECT_VERSION} )
157+
#set( CPACK_GENERATOR ZIP )
158+
159+
#include( CPack )

Cpufit/CMakeLists.txt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
# Cpufit
3+
4+
set( CpuHeaders
5+
Cpufit.h
6+
info.h
7+
lm_fit.h
8+
interface.h
9+
)
10+
11+
set( CpuSources
12+
Cpufit.cpp
13+
info.cpp
14+
lm_fit.cpp
15+
lm_fit_cpp.cpp
16+
interface.cpp
17+
Cpufit.def
18+
)
19+
20+
add_library( Cpufit SHARED
21+
${CpuHeaders}
22+
${CpuSources}
23+
)
24+
set_property( TARGET Cpufit
25+
PROPERTY RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}" )
26+
27+
#install( TARGETS Cpufit RUNTIME DESTINATION bin )
28+
29+
add_subdirectory( matlab )

Cpufit/Cpufit.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
LIBRARY "Cpufit"
2+
EXPORTS
3+
cpufit @1
4+
cpufit_get_last_error @2

Cpufit/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Cpufit

Cpufit/cpufit.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include "cpufit.h"
2+
#include "interface.h"
3+
4+
#include <string>
5+
6+
std::string last_error ;
7+
8+
int cpufit
9+
(
10+
size_t n_fits,
11+
size_t n_points,
12+
float * data,
13+
float * weights,
14+
int model_id,
15+
float * initial_parameters,
16+
float tolerance,
17+
int max_n_iterations,
18+
int * parameters_to_fit,
19+
int estimator_id,
20+
size_t user_info_size,
21+
char * user_info,
22+
float * output_parameters,
23+
int * output_states,
24+
float * output_chi_squares,
25+
int * output_n_iterations
26+
)
27+
try
28+
{
29+
__int32 n_points_32 = 0;
30+
if (n_points <= (unsigned int)(std::numeric_limits<__int32>::max()))
31+
{
32+
n_points_32 = __int32(n_points);
33+
}
34+
else
35+
{
36+
throw std::runtime_error("maximum number of data points per fit exceeded");
37+
}
38+
39+
FitInterface fi(
40+
data,
41+
weights,
42+
n_fits,
43+
n_points_32,
44+
tolerance,
45+
max_n_iterations,
46+
estimator_id,
47+
initial_parameters,
48+
parameters_to_fit,
49+
user_info,
50+
user_info_size,
51+
output_parameters,
52+
output_states,
53+
output_chi_squares,
54+
output_n_iterations);
55+
56+
fi.fit(model_id);
57+
58+
return STATUS_OK;
59+
}
60+
catch (std::exception & exception)
61+
{
62+
last_error = exception.what();
63+
64+
return STATUS_ERROR;
65+
}
66+
catch (...)
67+
{
68+
last_error = "Unknown Error";
69+
70+
return STATUS_ERROR;
71+
}
72+
73+
char const * cpufit_get_last_error()
74+
{
75+
return last_error.c_str();
76+
}

Cpufit/cpufit.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#ifndef CPU_FIT_H_INCLUDED
2+
#define CPU_FIT_H_INCLUDED
3+
4+
// fitting model ID
5+
#define GAUSS_1D 0
6+
#define GAUSS_2D 1
7+
#define GAUSS_2D_ELLIPTIC 2
8+
#define GAUSS_2D_ROTATED 3
9+
#define CAUCHY_2D_ELLIPTIC 4
10+
#define LINEAR_1D 5
11+
12+
// estimator ID
13+
#define LSE 0
14+
#define MLE 1
15+
16+
// fit state
17+
#define STATE_CONVERGED 0
18+
#define STATE_MAX_ITERATION 1
19+
#define STATE_SINGULAR_HESSIAN 2
20+
#define STATE_NEG_CURVATURE_MLE 3
21+
22+
// cpufit return state
23+
#define STATUS_OK 0
24+
#define STATUS_ERROR -1
25+
26+
#ifdef __cplusplus
27+
extern "C" {
28+
#endif
29+
30+
int cpufit
31+
(
32+
size_t n_fits,
33+
size_t n_points,
34+
float * data,
35+
float * weights,
36+
int model_id,
37+
float * initial_parameters,
38+
float tolerance,
39+
int max_n_iterations,
40+
int * parameters_to_fit,
41+
int estimator_id,
42+
size_t user_info_size,
43+
char * user_info,
44+
float * output_parameters,
45+
int * output_states,
46+
float * output_chi_squares,
47+
int * output_n_iterations
48+
) ;
49+
50+
char const * cpufit_get_last_error() ;
51+
52+
#ifdef __cplusplus
53+
}
54+
#endif
55+
56+
#endif // CPU_FIT_H_INCLUDED

Cpufit/info.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "info.h"
2+
3+
Info::Info(void) :
4+
n_parameters_(0),
5+
n_parameters_to_fit_(0),
6+
max_n_iterations_(0),
7+
n_fits_(0),
8+
n_points_(0),
9+
model_id_(0),
10+
estimator_id_(0),
11+
user_info_size_(0)
12+
{
13+
}
14+
15+
Info::~Info(void)
16+
{
17+
}
18+
19+
void Info::set_number_of_parameters_to_fit(int const * parameters_to_fit)
20+
{
21+
n_parameters_to_fit_ = n_parameters_;
22+
23+
for (int i = 0; i < n_parameters_; i++)
24+
{
25+
if (!parameters_to_fit[i])
26+
{
27+
n_parameters_to_fit_--;
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)