# Managing computing jobs

Here's a program for calculating pi

#!/usr/bin/env python
"""
a little implementation of our pi calculator
using montecarlo integration

Arguments:
-n #    means use this many points
-e #    means use 2**# many points
-t ""   adds a tag to the output
"""

from numpy import floor, pi
from numpy.random import rand
import sys

e = 4
n = 2L**e
tag = ''
for ai, a in enumerate(sys.argv):
if '-e' == a:
e = int(sys.argv[ai+1])
n = 2L**e
elif '-n' == a:
n = int(sys.argv[ai+1])
elif '-t' == a:
tag = sys.argv[ai+1]

x = rand(n)
y = rand(n)
m = sum(1-floor(x*x+y*y))
pi_est = 4.*(m/n)
if len(tag)>0:
print tag, n, m, pi_est, abs(pi-pi_est)
else:
print n, m, pi_est, abs(pi-pi_est)

Save this code to a new file called montecarlo.py

## Using multicores for highly parallel tasks

• echo - just prints out the value of it's arguments

$echo "Hello world"$ x="Mars"
$echo "Hello$x"
• python from the command line

$echo 'for i in range(4): print i' | python - • piping output to a file $ echo 'for i in range(4): print i' | python - > myfile
• xargs

$cat myfile | xargs -n 1 -I {} echo "hello {} times" • combining things $ echo 'for i in range(4): print i' | python - > tagfile
\$ cat tagfile | xargs -n 1 -P 4 -I {} ./montecarlo.py -t "{}" -e 23

## SSH

Breaking news, June 2, 2015: Microsoft's power shell has just decided to support OpenSSH !!! So ssh skills became more valuable.

O'Reilly's snail book on ssh. (a take-off on the classic dragon book)

Penn state has a Linux cluster that students can use to experiment with remote computing. See Linux cluster docs This is a baby version of the services supplied by our Institute for cyberscience. Here are the two basic commands.

• ssh = "secure shell" is used to open a shell on a remote machine. The standard command-line format to open a new connection is "ssh username@hostname" For example...

ssh tcr2@lxcluster.tlt.psu.edu
• scp - "secure copy" is used to copy files between two machines. The standard format is "scp user@fromhost:/path/of/old/file user@tohost:/path/to/new/file" For example...

scp montecarlo.py tcr2@lxcluster.tlt.psu.edu:~/

• Custom configurations can be added to ssh by editing the file "~/.ssh/config"

Unfortunately, one of the pains in using the linux cluster is that you can not make scripts executable, so to run your code on the remote machine, you'll have to use the command "python montecarlo.py ..."

### Public/Private keys

Here is a good write-up on using ssh with public keys instead of passwords. (Remember, you can find out how a command works by typing "man" and the command name)

• Create your public and private keys using "ssh-keygen -t rsa -b 1024" on your host computer.

• Upload your public key to reluga.org using "ssh-copy-id" or "scp" (the public key needs to go in "~/.ssh/authorized_keys")