Skip to content

Commit

Permalink
do not pass go pointers to the C code
Browse files Browse the repository at this point in the history
This is a proposed solution for afbarnard#4.
  • Loading branch information
idavydov committed May 10, 2016
1 parent a585f8c commit e251ebd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
14 changes: 7 additions & 7 deletions lbfgsb.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,14 +292,14 @@ func (lbfgsb *Lbfgsb) Minimize(
upperBounds := makeCCopySlice_Float(lbfgsb.upperBounds, dim)

// Set up callbacks for function, gradient, and logging
callbackData_c := unsafe.Pointer(
&callbackData{objective: objective})
callbackData_c := uintptr(unsafe.Pointer(
&callbackData{objective: objective}))
var doLogging_c C.int // false
var logFunctionCallbackData_c unsafe.Pointer // null
var logFunctionCallbackData_c uintptr // null
if lbfgsb.logger != nil {
doLogging_c = C.int(1) // true
logFunctionCallbackData_c = unsafe.Pointer(
&logCallbackData{logger: lbfgsb.logger})
logFunctionCallbackData_c = uintptr(unsafe.Pointer(
&logCallbackData{logger: lbfgsb.logger}))
}

// Allocate arrays for return value
Expand Down Expand Up @@ -333,11 +333,11 @@ func (lbfgsb *Lbfgsb) Minimize(

// Call the actual L-BFGS-B procedure
statusCode_c := C.lbfgsb_minimize_c(
callbackData_c, dim_c,
(C.uintptr_t)(callbackData_c), dim_c,
boundsControl_c, lowerBounds_c, upperBounds_c,
approximationSize_c, fTolerance_c, gTolerance_c,
x0_c, minX_c, minF_c, minG_c, &iters_c, &evals_c,
printControl_c, doLogging_c, logFunctionCallbackData_c,
printControl_c, doLogging_c, (C.uintptr_t)(logFunctionCallbackData_c),
statusMessage_c, statusMessageLength_c,
)

Expand Down
8 changes: 4 additions & 4 deletions lbfgsb_go_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

int lbfgsb_minimize_c
(
void *callback_data,
uintptr_t callback_data,
int dim,
int *bounds_control,
double *lower_bounds,
Expand All @@ -31,7 +31,7 @@ int lbfgsb_minimize_c
int *evals,
int fortran_print_control,
int do_logging,
void *log_function_callback_data,
uintptr_t log_function_callback_data,
char *status_message,
int status_message_length
)
Expand All @@ -47,7 +47,7 @@ int lbfgsb_minimize_c
(
go_objective_function_callback,
go_objective_gradient_callback,
callback_data,
(void *)callback_data,
dim,
bounds_control,
lower_bounds,
Expand All @@ -63,7 +63,7 @@ int lbfgsb_minimize_c
evals,
fortran_print_control,
log_function_pointer,
log_function_callback_data,
(void *)log_function_callback_data,
status_message,
status_message_length
);
Expand Down
6 changes: 4 additions & 2 deletions lbfgsb_go_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
#ifndef __LBFGSB_GO_INTERFACE_H__
#define __LBFGSB_GO_INTERFACE_H__

#include <stdint.h>

int lbfgsb_minimize_c
(
void *callback_data,
uintptr_t callback_data,
int dim,
int *bounds_control,
double *lower_bounds,
Expand All @@ -25,7 +27,7 @@ int lbfgsb_minimize_c
int *evals,
int fortran_print_control,
int do_logging,
void *log_function_callback_data,
uintptr_t log_function_callback_data,
char *status_message,
int status_message_length
);
Expand Down

0 comments on commit e251ebd

Please sign in to comment.