2

I am creating a ruby c extension gem which uses openmp.

What I am attempting to do is quite simple, just create a parallel for that will initialize a ruby array on each iteration.

so, my gem files are:

Visualize_helper.c

which defines the iterate_over_trajectories_parallel method as a simple parallel for initializing an ruby Array.

#include <ruby.h>
#include <stdlib.h>
#include <omp.h>

static VALUE iterate_over_trajectories_parallel() {

  int i;

  #pragma omp parallel for private(i)
  for (i = 0; i < 500; i++ ){

    VALUE aggr = rb_ary_new();
  }

  return Qnil;
}

void Init_visualize_helper(void) {

  // Register the VisualizeHelper module
  VALUE mVisualizeHelper = rb_define_module("VisualizeHelper");

  // Register the method that iterates on each trajectory using openmp
  rb_define_singleton_method(mVisualizeHelper, "iterate_over_trajectories_parallel",                                  iterate_over_trajectories_parallel, 0);
}  

extconf.rb

require "mkmf"
extension_name = "visualize_helper"
dir_config("visualize_helper")
$CXXFLAGS += ' -fopenmp '
$CFLAGS += ' -fopenmp '
have_library('gomp', "main")
create_makefile "visualize_helper/visualize_helper"

The problem is that depending on the iteration size everything run alright (iteration size < ~100) or a crash report each time with an different error. Some of the errors that appears:

error 1

-- Crash Report log information --------------------------------------------
See Crash Report log file under the one of following:
 * ~/Library/Logs/CrashReporter
 * /Library/Logs/CrashReporter
 * ~/Library/Logs/DiagnosticReports
 * /Library/Logs/DiagnosticReports
for more details.

-- Control frame information -----------------------------------------------
ruby(19787,0x700000088000) malloc: *** error for object 0x7f80c37dd310: double free
*** set a breakpoint in malloc_error_break to debug
c:0031 p:---- s:0121 e:000120 CFUNC  :iterate_over_trajectories_parallel
c:0030 p:0013 s:0118 e:000117 BLOCK  /Users/rapha/projetos/visualize_helper/spec/visualize_helper/visualize_helper_spec.rb:116 [FINISH]
c:0029 p:---- s:0116 e:000115 CFUNC  :instance_exec
c:0028 p:0021 s:0112 /Users/rapha/.rvm/rubies/ruby-2.1.5/bin/ruby -I/Users/rapha/.rvm/gems/ruby-2.1.5/gems/rspec-core-3.4.4/lib:/Users/rapha/.rvm/gems/ruby-2.1.5/gems/rspec-support-3.4.1/lib /Users/rapha/.rvm/gems/ruby-2.1.5/gems/rspec-core-3.4.4/exe/rspec --pattern spec/\*\*\{,/\*/\*\*\}/\*_spec.rb failed

Error 2

[BUG] object allocation during garbage collection phase
ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-darwin14.0]

-- Crash Report log information --------------------------------------------
  See Crash Report log file under the one of following:
 * ~/Library/Logs/CrashReporter
 * /Library/Logs/CrashReporter2
 * ~/Library/Logs/DiagnosticReports
 * /Library/Logs/DiagnosticReports
 for more details.

I believe that the source of the problem is in fact the error 2: object allocation during garbage collection phase

But I dont know how to fix it, any ideas on that?

thank you for your attention.

Raphael Ottoni
  • 506
  • 3
  • 14
  • 1
    I suspect that OpenMP faces the same restrictions on threading regarding the global interpreter lock that other Ruby extension code does. I don’t think you’ll be able to do what you’re trying here. – matt Apr 15 '16 at 18:05
  • 1
    Can you do this? I'm not sure the Ruby runtime will be happy with it. You may have concurrent access to the Ruby core which can cause huge problems. – tadman Apr 15 '16 at 18:05
  • Do you guys believe that the best solution here is to re-write the parallel for to use only C structures? Yes, in this example it would be trivial, but IRL the "iterate_over_trajectories_parallel" is quite complex =/ I wonder if some kind of rb_protect method will help on this – Raphael Ottoni Apr 15 '16 at 18:09

0 Answers0