/go-glpk-bench-example

This repository contains working example, how to avoid runtime errors while benchmarking functions with the use of https://github.com/lukpank/go-glpk lib.

Primary LanguageGoMIT LicenseMIT

go-glpk-bench-example

This repository contains working example, how to avoid runtime errors while benchmarking functions with the use of https://github.com/lukpank/go-glpk lib.

Prerequisites

  • Installed GLPK
  • Go v1.13 (tested only with this version)

Problem

When running a benchmark for func with the usage of go-glpk, it's failing with the runtime error in cgo:

glp_free: memory allocation error
Error detected in file env/alloc.c at line 72
SIGABRT: abort
PC=0x7fff7ac4c2c6 m=7 sigcode=0

goroutine 0 [idle]:
runtime: unknown pc 0x7fff7ac4c2c6

...

goroutine 7 [syscall]:
runtime.cgocall(0x4100d10, 0xc00005bcd0, 0xc00005bd18)
        /usr/local/go/src/runtime/cgocall.go:128 +0x5b fp=0xc00005bca0 sp=0xc00005bc68 pc=0x400538b
github.com/lukpank/go-glpk/glpk._Cfunc_glp_delete_prob(0x76000d0)

...

Example code is placed in failing directory. To reproduce this error just run the following command:

make bench-fail

Solution

In this issue the author suggested to add this function to lock the system thread for goroutine, that uses go-glpk lib:

func init() {
	runtime.LockOSThread()
}

But this doesn't help.

The final solution is to add the following code right before using go-glpk:

// Some code...
runtime.LockOSThread()
defer runtime.UnlockOSThread()

lp := glpk.New()
defer lp.Delete()
// ...

The working code is in the success directory. To run working benchmark execute the following:

make bench-success