Project

General

Profile

Actions

Bug #20489

open

Ractor behavior strange in ruby master

Added by nekoyama32767 (Jinsong Yu) 2 months ago. Updated about 2 months ago.

Status:
Open
Assignee:
-
Target version:
-
ruby -v:
ruby 3.4.0dev (2024-05-14T01:58:31Z master 9d01f657b3) [x86_64-linux]
[ruby-dev:52086]

Description

This is a tarai program
Run./ruby tarai_ractor.rb 2 8 is to use 2 thread to run 8 times tarai function total, that means 4 times tarai for each ractor(thread).

GC.disable

def split_len(len, split)
    ret = []
    mod = len % split
    head = 0
    tail = 0
    split.times do |i|
        if head >= len
            break
        end
        k = 0
        if i < mod then k = 1 end
        tail = tail + (len/split) + k
        ret.append(head...tail)
        head = tail
    end
    return ret
end

def ary_split(ary, split)
    return split_len(ary.length,split)
end
def item_check(item)
    if item[0] != nil
        1 + item_check(item[0]) + item_check(item[1])
    else
        1
    end
end


def tarai(x, y, z) =
    x <= y ? y : tarai(tarai(x-1, y, z),
                       tarai(y-1, z, x),
                       tarai(z-1, x, y))
  
times = ARGV[0].to_i
split = ARGV[1].to_i

p split_len(times, split)
split_len(times, split).each.map do |sp|
    Ractor.new (sp) {
        s = _1
        s.each do 
         tarai(13, 7, 0) 
        end
    }
end.each(&:take)

The problem is in ruby 3.1.2 and ruby 3.3
./ruby tarai_ractor.rb 1 1 has simiular execute time with ./ruby tarai_ractor.rb 8 8 because each thread only run 1 time of tarai function, like follow:

ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-linux]:

time ruby exp_ractor_tarai.rb 1 1
[0...1]
<internal:ractor>:267: warning: Ractor is experimental, and the behavior may change in future versions of Ruby! Also there are many implementation issues.

real    0m1.442s
user    0m1.429s
sys     0m0.014s

time ruby exp_ractor_tarai.rb 8 8
[0...1, 1...2, 2...3, 3...4, 4...5, 5...6, 6...7, 7...8]
<internal:ractor>:267: warning: Ractor is experimental, and the behavior may change in future versions of Ruby! Also there are many implementation issues.

real    0m1.857s
user    0m13.817s
sys     0m0.041s

But in ruby master(ruby 3.4.0dev)
ruby 3.4.0dev (2024-05-14T01:58:31Z master 9d01f657b3) [x86_64-linux]
1 ractor 1 tarai:

time ../ruby exp_ractor_tarai.rb 1 1

`RubyGems' were not loaded.
`error_highlight' was not loaded.
`did_you_mean' was not loaded.
`syntax_suggest' was not loaded.
[0...1]
exp_ractor_tarai.rb:47: warning: Ractor is experimental, and the behavior may change in future versions of Ruby! Also there are many implementation issues.

real    0m1.671s
user    0m1.666s
sys     0m0.005s

8 ractor 8 tarai:

time ../ruby exp_ractor_tarai.rb 8 8

`RubyGems' were not loaded.
`error_highlight' was not loaded.
`did_you_mean' was not loaded.
`syntax_suggest' was not loaded.
[0...1, 1...2, 2...3, 3...4, 4...5, 5...6, 6...7, 7...8]
exp_ractor_tarai.rb:47: warning: Ractor is experimental, and the behavior may change in future versions of Ruby! Also there are many implementation issues.

real    0m18.408s
user    1m58.659s
sys     0m0.021s

And in ruby 3.4.0dev when run time ../ruby exp_ractor_tarai.rb 16 16 16 thread should be used in system monitoring while only 8 threads are used.
Ruby 3.3 and Ruby 3.1.2 do not have this problem.


Files

thead16_16.png (168 KB) thead16_16.png nekoyama32767 (Jinsong Yu), 05/14/2024 08:59 AM
thread16_8.png (165 KB) thread16_8.png nekoyama32767 (Jinsong Yu), 05/14/2024 09:00 AM

Updated by ko1 (Koichi Sasada) about 2 months ago

Thank you. I'll investigate it.

Actions

Also available in: Atom PDF

Like0
Like0