Skip to content

Commit 9468bf2

Browse files
authored
Merge pull request #199 from mrkn/ractor_support
Ractor support
2 parents 980d0e8 + ca2ddb7 commit 9468bf2

File tree

7 files changed

+85
-2
lines changed

7 files changed

+85
-2
lines changed

ext/numo/narray/gen/tmpl/alloc_func.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ static const rb_data_type_t <%=type_name%>_data_type = {
8787
{0, <%=type_name%>_free, <%=type_name%>_memsize,},
8888
&na_data_type,
8989
&<%=type_name%>_info,
90-
0, // flags
90+
RUBY_TYPED_FROZEN_SHAREABLE, // flags
9191
};
9292

9393
<% end %>

ext/numo/narray/gen/tmpl/init_class.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
rb_hash_aset(hCast, rb_cArray, cT);
1515
<% for x in upcast %>
1616
<%= x %><% end %>
17+
rb_obj_freeze(hCast);
1718

1819
<% @children.each do |m| %>
1920
<%= m.init_def %><% end %>

ext/numo/narray/narray.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1989,6 +1989,10 @@ na_equal(VALUE self, volatile VALUE other)
19891989
void
19901990
Init_narray()
19911991
{
1992+
#ifdef HAVE_RB_EXT_RACTOR_SAFE
1993+
rb_ext_ractor_safe(true);
1994+
#endif
1995+
19921996
mNumo = rb_define_module("Numo");
19931997

19941998
/*

ext/numo/narray/numo/narray.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,12 @@ typedef unsigned int BIT_DIGIT;
450450
#include "numo/ndloop.h"
451451
#include "numo/intern.h"
452452

453+
// for Ractor support code
454+
#ifndef HAVE_RB_EXT_RACTOR_SAFE
455+
# undef RUBY_TYPED_FROZEN_SHAREABLE
456+
# define RUBY_TYPED_FROZEN_SHAREABLE 0
457+
#endif
458+
453459
#if defined(__cplusplus)
454460
#if 0
455461
{ /* satisfy cc-mode */

numo-narray.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@ Gem::Specification.new do |spec|
3535
end
3636
spec.add_development_dependency "rake", ">= 12.3.3"
3737
spec.add_development_dependency "rake-compiler", "~> 1.1"
38-
spec.add_development_dependency "test-unit", "~> 3.0"
38+
spec.add_development_dependency "test-unit"
3939
end

test/ractor_test.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
require_relative "test_helper"
2+
3+
class NArrayRactorTest < NArrayTestBase
4+
if respond_to?(:ractor)
5+
ractor keep: true
6+
data(:dtype, TYPES, keep: true)
7+
def test_non_frozen(data)
8+
dtype = data.fetch(:dtype)
9+
ary = random_array(dtype)
10+
r = Ractor.new(ary) {|x| x }
11+
ary2 = r.take
12+
assert_equal(ary, ary2)
13+
assert_not_same(ary, ary2)
14+
end
15+
16+
def test_frozen(data)
17+
dtype = data.fetch(:dtype)
18+
ary1 = random_array(dtype)
19+
ary1.freeze
20+
r = Ractor.new(ary1) do |ary2|
21+
[ary2, ary2 * 10]
22+
end
23+
ary2, res = r.take
24+
assert_equal((dtype != Numo::RObject),
25+
ary1.equal?(ary2))
26+
assert_equal(ary1*10, res)
27+
end
28+
29+
def test_parallel(data)
30+
dtype = data.fetch(:dtype)
31+
ary1 = random_array(dtype, 100000)
32+
r1 = Ractor.new(ary1) do |ary2|
33+
ary2 * 10
34+
end
35+
r2 = Ractor.new(ary1) do |ary4|
36+
ary4 * 10
37+
end
38+
assert_equal(r1.take, r2.take)
39+
end
40+
41+
def random_array(dtype, n=1000)
42+
case dtype
43+
when Numo::DFloat, Numo::SFloat, Numo::DComplex, Numo::SComplex
44+
dtype.new(n).rand_norm
45+
else
46+
dtype.new(n).rand(10)
47+
end
48+
end
49+
end
50+
end

test/test_helper.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,25 @@
22
require "numo/narray"
33
require "test/unit"
44
require "stringio"
5+
6+
class NArrayTestBase < Test::Unit::TestCase
7+
FLOAT_TYPES = [
8+
Numo::DFloat,
9+
Numo::DComplex,
10+
]
11+
12+
TYPES = [
13+
*FLOAT_TYPES,
14+
Numo::SFloat,
15+
Numo::SComplex,
16+
Numo::Int64,
17+
Numo::Int32,
18+
Numo::Int16,
19+
Numo::Int8,
20+
Numo::UInt64,
21+
Numo::UInt32,
22+
Numo::UInt16,
23+
Numo::UInt8,
24+
Numo::RObject,
25+
]
26+
end

0 commit comments

Comments
 (0)