-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ractor support #199
Ractor support #199
Conversation
Hi. I tried given branch require 'benchmark'
require 'numo/narray'
Warning[:experimental] = false
puts 'Testing Numo'
data = Ractor.make_shareable Array.new(1_000_000) { Numo::SFloat.new(10).rand(100) }
Benchmark.bm do |bm|
bm.report('no ractor') do
4.times { data.each &:mean }
end
bm.report('1 ractor') do
Ractor.new(data) do |arr|
4.times { arr.each &:mean }
nil
end.take
end
bm.report('2 ractors') do
2.times.map do
Ractor.new(data) do |arr|
2.times { arr.each &:mean }
nil
end
end.each &:take
end
bm.report('4 ractors') do
4.times.map do
Ractor.new(data) do |arr|
arr.each &:mean
nil
end
end.each &:take
end
end
puts 'Testing core Array'
data = Ractor.make_shareable Array.new(2_000_000) { Array.new(10) { Random.rand } }
Benchmark.bm do |bm|
bm.report('no ractor') do
4.times { data.each { |v| v.sum / v.size.to_f } }
end
bm.report('1 ractor') do
Ractor.new(data) do |arr|
4.times { arr.each { |v| v.sum / v.size.to_f } }
nil
end.take
end
bm.report('2 ractors') do
2.times.map do
Ractor.new(data) do |arr|
2.times { arr.each { |v| v.sum / v.size.to_f } }
nil
end
end.each &:take
end
bm.report('4 ractors') do
4.times.map do
Ractor.new(data) do |arr|
arr.each { |v| v.sum / v.size.to_f }
nil
end
end.each &:take
end
end Running on Ruby 3.1, Centos 8, it produces the following output on idling 14-core xeon e5-2680 v4.
For some reason the performance of multiple Ractors calculating Numo arrays degrades significantly |
@orlando-labs At first, you need to understand that numo-narray is not always faster than Array. Numo-narray is designed for operating large numeric arrays. So testing with 10-length arrays is very disadvantageous for numo-narray. With the following benchmark, you can see the running time chagnes in the differnt way between numo-narray and normal array. Numo-narray is slower than normal array when require 'benchmark'
require 'numo/narray'
array_count = 10000
[10, 100, 1000, 10000].each do |array_len|
data_numo = Array.new(array_count) { Numo::SFloat.new(array_len).rand(100) }
data_ary = Array.new(array_count) { Array.new(array_len) { Random.rand } }
puts
puts "# array_len = #{array_len}"
puts
Benchmark.bm do |bm|
bm.report('numo') do
4.times { data_numo.each &:mean }
end
bm.report('array') do
4.times { data_ary.each { |v| v.sum / v.size.to_f } }
end
end
end
With the following benchmark code that is similar to yours, numo-narray is faster than normal array. require 'benchmark'
require 'numo/narray'
Warning[:experimental] = false
array_len = 10000
array_count = 10000
puts 'Testing Numo'
data = Ractor.make_shareable Array.new(array_count) { Numo::SFloat.new(array_len).rand(100) }
Benchmark.bm do |bm|
bm.report('no ractor') do
4.times { data.each &:mean }
end
bm.report('1 ractor') do
Ractor.new(data) do |arr|
4.times { arr.each &:mean }
nil
end.take
end
bm.report('2 ractors') do
2.times.map do
Ractor.new(data) do |arr|
2.times { arr.each &:mean }
nil
end
end.each &:take
end
bm.report('4 ractors') do
4.times.map do
Ractor.new(data) do |arr|
arr.each &:mean
nil
end
end.each &:take
end
end
puts 'Testing core Array'
data = Ractor.make_shareable Array.new(2*array_count) { Array.new(array_len) { Random.rand } }
Benchmark.bm do |bm|
bm.report('no ractor') do
4.times { data.each { |v| v.sum / v.size.to_f } }
end
bm.report('1 ractor') do
Ractor.new(data) do |arr|
4.times { arr.each { |v| v.sum / v.size.to_f } }
nil
end.take
end
bm.report('2 ractors') do
2.times.map do
Ractor.new(data) do |arr|
2.times { arr.each { |v| v.sum / v.size.to_f } }
nil
end
end.each &:take
end
bm.report('4 ractors') do
4.times.map do
Ractor.new(data) do |arr|
arr.each { |v| v.sum / v.size.to_f }
nil
end
end.each &:take
end
end
|
Hi, @mrkn. Thanks for the response. I appreciate it. |
This is an article that ko1, a developer of Ractor, posted on his company Cookpad's blog about a year ago. Here, he says that using Ractor can be slower than not using it.
He writes that slow referencing of constants is one of the reasons why Ractor is slow.
He has written that he will fix this problem, so constant referencing may not be slow now.
numo-narray is probably one of the areas in Ruby where Ractor will be used the most in the future. I think it is very important for future for Ruby that Ractor is available in numo-narray. ping: If you don't mind, @ko1, could you take a look at this for us? |
Hi all |
I'll contact with the owner. |
Seeing similar slow down issues when using more than 2 Ractors and Numo. |
I want to let numo-narray support Ractor in this pull request.
The following changes are made:
UPCAST
constants to make them sharable in non-main RactorsNumo::RObject
I keep
Numo::RObject
non-sharable because its instances can have compound objects such asArray
andHash
.@masa16 Could you please take a look?