1

I would like to run the Matlab engine (using the gem matlab-ruby) inside a ruby thread. The idea is to use ruby to distribute my Matlab processes onto different cores. However when I try to start a new engine from inside a thread I get a Trace/BPT trap: 5 error.

I'm new to ruby so not that strong on threads and so forth. Is what I'm trying to do possible or is it just one of those things that can't be done?

require 'matlab'

t1=Thread.new do
  engine = Matlab::Engine.new
  engine.put_variable "x", 10
  engine.put_variable "y", 22
  engine.eval "z = x * y"
  puts engine.get_variable "z"
end
t1.join
Amro
  • 123,847
  • 25
  • 243
  • 454
pez
  • 1,034
  • 2
  • 10
  • 23

1 Answers1

1

I wonder what happens if you instantiate a Matlab::Engine in the main thread, then later run your code as-is:

Matlab::Engine.new  # side effect of loading the dynamic lib
t1=Thread.new do
  engine = Matlab::Engine.new
  engine.put_variable "x", 10
  engine.put_variable "y", 22
  engine.eval "z = x * y"
  puts engine.get_variable "z"
end
t1.join

I ask because I've had trouble with Trace/BPT with different libraries (Sinatra/ActiveRecord/pg/etc.), and when running Ruby in GBD, it crashed with the following backtrace:

#0  0x00007fff89308590 in __CFInitialize ()
#1  0x00007fff5fc0d5ce in __dyld__ZN16ImageLoaderMachO11doImageInitERKN11ImageLoader11LinkContextE ()
#2  0x00007fff5fc0d607 in __dyld__ZN16ImageLoaderMachO16doInitializationERKN11ImageLoader11LinkContextE ()
#3  0x00007fff5fc0bcec in __dyld__ZN11ImageLoader23recursiveInitializationERKNS_11LinkContextEj ()
#4  0x00007fff5fc0bc9d in __dyld__ZN11ImageLoader23recursiveInitializationERKNS_11LinkContextEj ()
#5  0x00007fff5fc0bc9d in __dyld__ZN11ImageLoader23recursiveInitializationERKNS_11LinkContextEj ()
#6  0x00007fff5fc0bc9d in __dyld__ZN11ImageLoader23recursiveInitializationERKNS_11LinkContextEj ()
#7  0x00007fff5fc0bda6 in __dyld__ZN11ImageLoader15runInitializersERKNS_11LinkContextE ()
#8  0x00007fff5fc08fbb in __dyld_dlopen ()
#9  0x00007fff888a7e40 in dlopen ()
#10 0x0000000100001419 in dln_load ()
#11 0x000000010016ad19 in rb_vm_call_cfunc ()
#12 0x0000000100044d22 in rb_require_safe ()

Searching around lead me to this bug report for SBCL which suggests that dynamic libraries may only be loaded by the main thread.

Patrick
  • 5,714
  • 4
  • 31
  • 36