Tuesday, December 11, 2012

more #phi, this time PERL on PHI

You can't do this on GPGPU... We used this prime number gen as a simple example:
#!/usr/bin/perl -w
# prime-pthread, courtesy of Tom Christiansen
    
use strict;  
use threads;
use Thread::Queue;
    
my $stream = new Thread::Queue;
my $kid    = new threads(\&check_num, $stream, 2);
    
for my $i ( 3 .. 50 ) {
    $stream->enqueue($i);
} 
    
$stream->enqueue(undef);
$kid->join;
    
sub check_num {
     my ($upstream, $cur_prime) = @_;
     my $kid;      
     my $downstream = new Thread::Queue;
         while (my $num = $upstream->dequeue) {
             next unless $num % $cur_prime;
             if ($kid) {
               $downstream->enqueue($num);
                      } else {
                print "Found prime $num\n";
                    $kid = new threads(\&check_num, $downstream, $num);
             }
     } 
     $downstream->enqueue(undef) if $kid;
     $kid->join           if $kid;
}

How to port to a GPGPU? Well ya gonna need a version of this (more later)
[root@mic01-mic0 test]# ../bin/perl -v | grep mic
This is perl 5, version 16, subversion 2 (v5.16.2) built for mic-linux

Here we are running native threaded perl right on the MIC...
[root@mic01-mic0 test]# ../bin/perl ./thread.pl
Found prime 3
Found prime 5
Found prime 7
Found prime 11
Found prime 13
Found prime 17
Found prime 19
Found prime 23
Found prime 29
Found prime 31
Found prime 37
Found prime 41
Found prime 43
Found prime 47

Done.

Simple eh?


Read on for a quick how to for native Xeon Phi Mic build to make a version of perl, you need cross tools and some symlinks, but that's it to run any native perl goodness... there are better ways to do this, but I was in a hurry...

Your new "compiler" will look like this:
[jcuff@mic0 perl-5.16.0]$ cat /usr/bin/mic-linux-gcc
#!/bin/bash

icc -mmic "$@"

Make some symlinks
ln -s /usr/bin/readelf /usr/bin/mic-linux-readelf
ln -s /usr/bin/objdump /usr/bin/mic-linux-objdump
ln -s /usr/bin/ranlib /usr/bin/mic-linux-ranlib

ok now finally set up and build...
source /opt/intel/bin/iccvars.sh intel64

./configure --prefix=/n/sw/mic -Dcc=mic-linux-gcc -Dusethreads --target=mic-linux --host-cc=icc

Again - I'm not going to talk about benchmarks... but if a PCI card can run perl in multiple threads it sure makes life easier for many, especially our users... Special thanks to the folks who put this page together for the help getting started.



[any opinions here are all mine, and have absolutely nothing to do with my employer]
(c) 2011 James Cuff