Skip to content

Commit

Permalink
Merge pull request InsightSoftwareConsortium#5175 from bradking/impor…
Browse files Browse the repository at this point in the history
…t-liblbfgs

ENH: Import libLBFGS from upstream
  • Loading branch information
hjmjohnson authored Jan 26, 2025
2 parents acaddf4 + 2f2dcf5 commit fde014a
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "ITKOptimizersv4Export.h"
#include <memory>

#include "lbfgs.h"
#include "itk_lbfgs.h"

namespace itk
{
Expand Down
7 changes: 6 additions & 1 deletion Modules/ThirdParty/libLBFGS/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ set(ITKLIBLBFGS_THIRD_PARTY 1)
# set(ITKLIBLBFGS_NO_SRC 1)
#else()

set(ITKLIBLBFGS_INCLUDE_DIRS ${ITKLIBLBFGS_SOURCE_DIR}/include )
set(ITKLIBLBFGS_INCLUDE_DIRS ${ITKLIBLBFGS_SOURCE_DIR}/src)
set(ITKLIBLBFGS_LIBRARIES itklbfgs)

#endif()

itk_module_impl()

install(FILES ${ITKLIBLBFGS_SOURCE_DIR}/src/itk_lbfgs.h
DESTINATION ${ITKLIBLBFGS_INSTALL_INCLUDE_DIR}
COMPONENT Development
)
26 changes: 26 additions & 0 deletions Modules/ThirdParty/libLBFGS/UpdateFromUpstream.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash

set -e
set -x
shopt -s dotglob

readonly name="libLBFGS"
readonly ownership="libLBFGS Upstream <[email protected]>"
readonly subtree="Modules/ThirdParty/libLBFGS/src/itklbfgs"
readonly repo="https://github.com/chokkan/liblbfgs.git"
readonly tag="v1.10"
readonly paths="
COPYING
include/*.h
lib/*.c
lib/*.h
"

extract_source () {
git_archive
pushd "${extractdir}/${name}-reduced"
echo "* -whitespace" >> .gitattributes
popd
}

. "${BASH_SOURCE%/*}/../../../Utilities/Maintenance/update-third-party.bash"
20 changes: 1 addition & 19 deletions Modules/ThirdParty/libLBFGS/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,25 +1,7 @@
ADD_LIBRARY( itklbfgs
lbfgs.c
)

IF(ITK_LIBRARY_PROPERTIES)
SET_TARGET_PROPERTIES(itklbfgs PROPERTIES ${ITK_LIBRARY_PROPERTIES})
ENDIF(ITK_LIBRARY_PROPERTIES)

set(ITK3P_INSTALL_EXPORT_NAME "${ITKLIBLBFGS-targets}")
set(ITK3P_INSTALL_INCLUDE_DIR "${ITKLIBLBFGS_INSTALL_INCLUDE_DIR}")
set(ITK3P_INSTALL_RUNTIME_DIR "${ITKLIBLBFGS_INSTALL_RUNTIME_DIR}")
set(ITK3P_INSTALL_LIBRARY_DIR "${ITKLIBLBFGS_INSTALL_LIBRARY_DIR}")
set(ITK3P_INSTALL_ARCHIVE_DIR "${ITKLIBLBFGS_INSTALL_ARCHIVE_DIR}")

IF(UNIX)
TARGET_LINK_LIBRARIES(itklbfgs m)
ENDIF(UNIX)

INSTALL(TARGETS itklbfgs
EXPORT ${ITK3P_INSTALL_EXPORT_NAME}
RUNTIME DESTINATION ${ITK3P_INSTALL_RUNTIME_DIR} COMPONENT RuntimeLibraries
LIBRARY DESTINATION ${ITK3P_INSTALL_LIBRARY_DIR} COMPONENT RuntimeLibraries
ARCHIVE DESTINATION ${ITK3P_INSTALL_ARCHIVE_DIR} COMPONENT Development)

add_subdirectory(itklbfgs)
itk_module_target(itklbfgs NO_INSTALL)
27 changes: 27 additions & 0 deletions Modules/ThirdParty/libLBFGS/src/itk_lbfgs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed 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.txt
*
* 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.
*
*=========================================================================*/
/*=========================================================================
* Include file to wrap the internal ITK libLBFGS library header
*=========================================================================*/

#ifndef itk_lbfgs_h
#define itk_lbfgs_h

#include <itklbfgs/include/lbfgs.h>

#endif
1 change: 1 addition & 0 deletions Modules/ThirdParty/libLBFGS/src/itklbfgs/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* -whitespace
20 changes: 20 additions & 0 deletions Modules/ThirdParty/libLBFGS/src/itklbfgs/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
add_library(itklbfgs lib/lbfgs.c)
target_include_directories(itklbfgs PRIVATE include)

if(ITK_LIBRARY_PROPERTIES)
set_target_properties(itklbfgs PROPERTIES ${ITK_LIBRARY_PROPERTIES})
endif()

if(UNIX)
target_link_libraries(itklbfgs m)
endif()

install(TARGETS itklbfgs
EXPORT ${ITK3P_INSTALL_EXPORT_NAME}
RUNTIME DESTINATION ${ITK3P_INSTALL_RUNTIME_DIR} COMPONENT RuntimeLibraries
LIBRARY DESTINATION ${ITK3P_INSTALL_LIBRARY_DIR} COMPONENT RuntimeLibraries
ARCHIVE DESTINATION ${ITK3P_INSTALL_ARCHIVE_DIR} COMPONENT Development)

install(FILES
${CMAKE_CURRENT_SOURCE_DIR}/include/lbfgs.h
DESTINATION ${ITK3P_INSTALL_INCLUDE_DIR}/itklbfgs/include)
22 changes: 22 additions & 0 deletions Modules/ThirdParty/libLBFGS/src/itklbfgs/COPYING
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License

Copyright (c) 1990 Jorge Nocedal
Copyright (c) 2007-2010 Naoaki Okazaki

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@
*/

/* $Id$ */
#ifdef USE_SSE
#include <emmintrin.h>
#endif

#ifndef __LBFGS_H__
#define __LBFGS_H__
Expand Down Expand Up @@ -62,7 +59,7 @@ typedef double lbfgsfloatval_t;
#endif


/**
/**
* \addtogroup liblbfgs_api libLBFGS API
* @{
*
Expand All @@ -71,7 +68,7 @@ typedef double lbfgsfloatval_t;

/**
* Return values of lbfgs().
*
*
* Roughly speaking, a negative value indicates an error.
*/
enum {
Expand Down Expand Up @@ -368,7 +365,7 @@ typedef struct {
* function and its gradients when needed. A client program must implement
* this function to evaluate the values of the objective function and its
* gradients, given current values of variables.
*
*
* @param instance The user data sent for lbfgs() function by the client.
* @param x The current values of variables.
* @param g The gradient vector. The callback function must compute
Expand Down Expand Up @@ -505,14 +502,14 @@ void lbfgs_parameter_init(lbfgs_parameter_t *param);
* when libLBFGS is built with SSE/SSE2 optimization routines. A user does
* not have to use this function for libLBFGS built without SSE/SSE2
* optimization.
*
*
* @param n The number of variables.
*/
lbfgsfloatval_t* lbfgs_malloc(int n);

/**
* Free an array of variables.
*
*
* @param x The array of variables allocated by ::lbfgs_malloc
* function.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ licence.
#include <config.h>
#endif/*HAVE_CONFIG_H*/

//#include <stdint.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
Expand Down Expand Up @@ -132,7 +132,7 @@ typedef int (*line_search_proc)(
callback_data_t *cd,
const lbfgs_parameter_t *param
);

static int line_search_backtracking(
int n,
lbfgsfloatval_t *x,
Expand Down Expand Up @@ -1116,7 +1116,7 @@ static int line_search_morethuente(
* @param brackt The pointer to the predicate if the trial value is
* bracketed.
* @retval int Status value. Zero indicates a normal termination.
*
*
* @see
* Jorge J. More and David J. Thuente. Line search algorithm with
* guaranteed sufficient decrease. ACM Transactions on Mathematical
Expand Down Expand Up @@ -1248,7 +1248,7 @@ static int update_trial_interval(
x <- x, y <- t.
- Case b: if f(t) <= f(x) && f'(t)*f'(x) > 0,
x <- t, y <- y.
- Case c: if f(t) <= f(x) && f'(t)*f'(x) < 0,
- Case c: if f(t) <= f(x) && f'(t)*f'(x) < 0,
x <- t, y <- x.
*/
if (*fx < *ft) {
Expand Down

0 comments on commit fde014a

Please sign in to comment.