Skip to content

Instantly share code, notes, and snippets.

@jasonmp85
Created February 5, 2014 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonmp85/8832488 to your computer and use it in GitHub Desktop.
Save jasonmp85/8832488 to your computer and use it in GitHub Desktop.
Parallelism Test
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
static const int ITERATIONS = 2134113216;
static double do_work(int worker_num, int concurrency)
{
int chunk_size = ITERATIONS / concurrency;
int start_num = chunk_size * worker_num;
int i;
double sum = 0.0;
for (i = start_num; i < start_num + chunk_size; i++)
{
sum += sqrt((double) i);
}
return sum;
}
int main (int argc, char const *argv[])
{
int worker_num = 0;
int concurrency = 0;
double result = 0.0;
if (argc == 3)
{
worker_num = atoi(argv[1]);
concurrency = atoi(argv[2]);
result = (worker_num < concurrency) ? do_work(worker_num, concurrency) : 0.0;
printf("%0.3f\n", result);
return 0;
}
else
{
(void) fputs("Pass exactly two arguments\n", stderr);
return -1;
}
}
#!/usr/bin/env ruby
require 'rubygems'
require 'parallel'
def seconds_elapsed_during(&blk)
before = Time.now
blk.call
after = Time.now
return after - before
end
proc_count = 32 # change to expected maximum
warn "Detected #{proc_count} processors..."
baseline = nil
speedups = 1.upto(proc_count).map do |concurrency|
warn "Testing #{concurrency} active workers..."
observed = seconds_elapsed_during do
`seq 0 #{concurrency - 1} | xargs -L 1 -P #{concurrency} -I% ./a.out % #{concurrency}`
end
baseline ||= observed
speedup = (baseline / observed) - 1
warn ("%0.3f elapsed (%d%% speedup)" % [observed, speedup * 100])
speedup
end
likely_count = speedups.each_with_index.find { |sp, i| i != sp.round }.last
puts likely_count
@jasonmp85
Copy link
Author

To use:

  • Compile the C program — gcc do_work.c -lm
  • Change 32 to the expected maximum possible processor count
  • Run ./parallel_test.rb

@jasonmp85
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment