Chef Blogs

Hello, developers!

Thomas Petchel | Posted on | community | customers | learnchef

I’m pleased to join Chef this week as technical content lead for learnchef.com. I just wanted to take a moment to introduce myself and talk a bit about my role here and how I can help you.

My background mainly centered around client-side systems programming using C++ and .NET on Windows. As a developer, I was attracted to the idea of modeling IT infrastructure as code, and couldn’t pass up the opportunity to learn more about configuration management, continuous delivery, devops, and the cloud.

I’ll be working to improve and extend the content on learnchef.com to help new users get awesome quickly, and also help existing Chef users grow their awesomeness. We’re already working through a bunch of items from our backlog, and I look forward to hearing what additional content you’d like to see from us.

I wanted to get my feet wet with Ruby before diving more into Chef, so my first order of business here was to write my first Ruby program and run it on my Mac and on Linux. In my previous role, I worked a lot with concurrent and parallel programming. I also like cheap thrills. So here’s my quick attempt at approximating the value of π using the Monte Carlo method. Because the problem is embarrassingly parallel, it was straight-forward to distribute the work across processors (using the parallel gem) and join the results (similar to your basic map-reduce job.)

[ruby]
include Math
require ‘parallel’

# Determines if the given point lies within the unit circle
def in_unit_circle(x, y)
return Math.sqrt(x ** 2 + y ** 2) < 1.0
end

# Counts the number of random points that lie within the unit circle
def count_points(iterations)
count = 0
for n in 1..iterations
x = rand
y = rand
count += 1 if in_unit_circle(x, y)
end
4 * count
end

# Produce same result each run
srand(42)

# The total number of iterations
iterations = 10000000

# Remember the total number of logical processors
procs = Parallel.processor_count

# Map: run the simulation on each processor in parallel
counts = Parallel.map_with_index([1..procs], :in_threads=>procs) do |index|
count_points(iterations/procs)
end

# Reduce: accumulate the results
total_count = counts.reduce(0, :+)

# In case not all processors were used, run the remaining iterations serially
actual_iterations = counts.size * (iterations/procs)
total_count += count_points(iterations – actual_iterations) if (actual_iterations < iterations)

# The result is the total number of points that lie in the unit circle divided
# by the total number of iterations
pi = total_count.to_f / iterations
puts "pi = ~#{pi}"
[/ruby]

After running the program, I learned that the distribution of Ruby on my MacOS and Linux systems didn’t support threads – so no benefit from parallel processing. Perhaps I’ll return to that later, but for now, here’s my simplified program that runs serially:

[ruby]
include Math

# Determines if the given point lies within the unit circle
def in_unit_circle(x, y)
Math.sqrt(x ** 2 + y ** 2) < 1.0
end

# Counts the number of random points that lie within the unit circle
def count_points(iterations)
count = 0
for n in 1..iterations
x = rand
y = rand
count += 1 if in_unit_circle(x, y)
end
4 * count
end

# Produce same result each run
srand(42)

# The total number of iterations
iterations = 10000000

# Run the simulation
total_count = count_points(iterations)

# The result is the total number of points that lie in the unit circle divided
# by the total number of iterations
pi = total_count.to_f / iterations
puts "pi = ~#{pi}"
[/ruby]

This program tells me that π equals 3.1415988 – not bad for a quick test. The result should become more accurate as you increase the iterations.

If you’re just starting out with Chef but haven’t yet used Ruby, I encourage you to try your hand at one of your favorite problems. You’ll quickly get a feel for the language and its unique idioms.

I look forward to meeting many of you at this year’s #ChefConf later this month! I’d love to learn more about your background and also your requirements so that we can make learnchef.com as useful as possible.

If you can’t make it to #ChefConf but still want to talk content, drop me a line at tpetchel@getchef.com or on Twitter @tpetchel.

Happy coding!