December 3, 2012
A workshop on OpenStack was conducted on Saturday, December 1, 2012 at the Symbiosis Institute of Computer Studies and Research (SICSR), Pune, India. Both theory and lab sessions were organized for the students. I started the proceedings using Perry Myers’s presentation on “Introduction and Overview of OpenStack for IaaS (Infrastructure as a Service) Clouds” for the Essex release. The various building blocks of OpenStack with their functionality was explained. An overall big picture of the architecture was presented to them with illustrations.
The OpenStack Lab Guide was then given to the participants to setup their own OpenStack private cloud. Some of them had brought their own laptops, while others used the Fedora machines in the labs. We started by setting up the Keystone service, and adding users for authentication. The Glance service was then installed, and configured. A Fedora 17 and cirros image were then imported into Glance. The Nova service was then setup, and a SSH keypair was created for testing.
The Horizon dashboard user interface was used to start a virtual machine instance. Using ssh and the created keypair, we were able to login to the virtual machine and use it. curl was used to test the different REST API on the running stack. I also showed them simple Python examples to demonstrate the OpenStack APIs. As a final presentation for the day, I gave an introduction on libvirtd, and KVM.
Thanks to Manoj Aswani for working with me in organizing this workshop. Thanks also to Perry Myers and Mark McLoughlin for permission to use their presentation.
December 1, 2012
Arrows provide a generalization for monads, which was introduced by John Hughes. The arrows package provides classes that extend the Arrows class. It is now available in Fedora. Install it using:
$ sudo yum install ghc-arrows-devel
Consider the identity function defined using the arrows notation:
{-# LANGUAGE Arrows #-}
import Control.Arrow (returnA)
idA :: a -> a
idA = proc a -> returnA -< a
The idA function returns the given input as shown below:
*Main> idA 6
6
*Main> idA True
True
*Main> idA "Eureka!"
"Eureka!"
A mulTwo function that multiplies an integer by two can be written as:
{-# LANGUAGE Arrows #-}
import Control.Arrow (returnA)
mulTwo :: Int -> Int
mulTwo = proc a -> returnA -< (a * 2)
Testing mulTwo with ghci:
*Main> mulTwo 4
8
*Main> mulTwo 5
10
We can also use the do notation with arrows. For example:
{-# LANGUAGE Arrows #-}
import Control.Arrow (returnA)
mulFour :: Int -> Int
mulFour = proc a -> do b <- mulTwo -< a
mulTwo -< b
Loading mulFour in ghci:
*Main> mulFour 2
8
*Main> mulFour 3
12
Arrows also supports the use of conditional statements. An example when used with the if … then … else construct is as follows:
{-# LANGUAGE Arrows #-}
import Control.Arrow (returnA)
condMul :: Int -> Int
condMul = proc a ->
if a < 5
then returnA -< (a * 2)
else returnA -< (a * 3)
The condMul function multiplies the input integer by two if it is less than five, and with three when greater than five:
*Main> condMul 2
4
*Main> condMul 6
18
November 10, 2012
SuperLU is a library for solving sparse linear system of equations AX = B. It is now available in Fedora. Install it using:
$ sudo yum install SuperLU-devel
The superlu.c 5x5 example in the sources demonstrates the use of the SuperLU library:
#include "slu_ddefs.h"
main(int argc, char *argv[])
{
SuperMatrix A, L, U, B;
double *a, *rhs;
double s, u, p, e, r, l;
int *asub, *xa;
int *perm_r; /* row permutations from partial pivoting */
int *perm_c; /* column permutation vector */
int nrhs, info, i, m, n, nnz, permc_spec;
superlu_options_t options;
SuperLUStat_t stat;
/* Initialize matrix A. */
m = n = 5;
nnz = 12;
if ( !(a = doubleMalloc(nnz)) ) ABORT("Malloc fails for a[].");
if ( !(asub = intMalloc(nnz)) ) ABORT("Malloc fails for asub[].");
if ( !(xa = intMalloc(n+1)) ) ABORT("Malloc fails for xa[].");
s = 19.0; u = 21.0; p = 16.0; e = 5.0; r = 18.0; l = 12.0;
a[0] = s; a[1] = l; a[2] = l; a[3] = u; a[4] = l; a[5] = l;
a[6] = u; a[7] = p; a[8] = u; a[9] = e; a[10]= u; a[11]= r;
asub[0] = 0; asub[1] = 1; asub[2] = 4; asub[3] = 1;
asub[4] = 2; asub[5] = 4; asub[6] = 0; asub[7] = 2;
asub[8] = 0; asub[9] = 3; asub[10]= 3; asub[11]= 4;
xa[0] = 0; xa[1] = 3; xa[2] = 6; xa[3] = 8; xa[4] = 10; xa[5] = 12;
/* Create matrix A in the format expected by SuperLU. */
dCreate_CompCol_Matrix(&A, m, n, nnz, a, asub, xa, SLU_NC, SLU_D, SLU_GE);
/* Create right-hand side matrix B. */
nrhs = 1;
if ( !(rhs = doubleMalloc(m * nrhs)) ) ABORT("Malloc fails for rhs[].");
for (i = 0; i < m; ++i) rhs[i] = 1.0;
dCreate_Dense_Matrix(&B, m, nrhs, rhs, m, SLU_DN, SLU_D, SLU_GE);
if ( !(perm_r = intMalloc(m)) ) ABORT("Malloc fails for perm_r[].");
if ( !(perm_c = intMalloc(n)) ) ABORT("Malloc fails for perm_c[].");
/* Set the default input options. */
set_default_options(&options);
options.ColPerm = NATURAL;
/* Initialize the statistics variables. */
StatInit(&stat);
/* Solve the linear system. */
dgssv(&options, &A, perm_c, perm_r, &L, &U, &B, &stat, &info);
dPrint_CompCol_Matrix("A", &A);
dPrint_CompCol_Matrix("U", &U);
dPrint_SuperNode_Matrix("L", &L);
print_int_vec("\nperm_r", m, perm_r);
/* De-allocate storage */
SUPERLU_FREE (rhs);
SUPERLU_FREE (perm_r);
SUPERLU_FREE (perm_c);
Destroy_CompCol_Matrix(&A);
Destroy_SuperMatrix_Store(&B);
Destroy_SuperNode_Matrix(&L);
Destroy_CompCol_Matrix(&U);
StatFree(&stat);
}
Running the program produces the following output:
$ ./superlu
CompCol matrix A:
Stype 0, Dtype 1, Mtype 0
nrow 5, ncol 5, nnz 12
nzval: 19.000000 12.000000 12.000000 21.000000 12.000000 12.000000 21.000000 16.000000 21.000000 5.000000 21.000000 18.000000
rowind: 0 1 4 1 2 4 0 2 0 3 3 4
colptr: 0 3 6 8 10 12
CompCol matrix U:
Stype 0, Dtype 1, Mtype 4
nrow 5, ncol 5, nnz 11
nzval: 21.000000 -13.263158 7.578947 21.000000
rowind: 0 1 2 0
colptr: 0 0 0 1 4 4
SuperNode matrix L:
Stype 3, Dtype 1, Mtype 1
nrow 5, ncol 5, nnz 11, nsuper 2
nzval:
0 0 1.900000e+01
1 0 6.315789e-01
4 0 6.315789e-01
1 1 2.100000e+01
2 1 5.714286e-01
4 1 5.714286e-01
1 2 -1.326316e+01
2 2 2.357895e+01
4 2 -2.410714e-01
3 3 5.000000e+00
4 3 -7.714286e-01
3 4 2.100000e+01
4 4 3.420000e+01
nzval_colptr: 0 3 6 9 11 13
rowind: 0 1 4 1 2 4 3 4
rowind_colptr: 0 3 6 6 8 8
col_to_sup: 0 1 1 2 2
sup_to_col: 0 1 3 5
perm_r
0 0
1 1
2 2
3 3
4 4
The SuperLU User’s Guide provides detailed documentation on the algorithms and usage of three libraries used to solve sparse linear systems.
October 8, 2012
A Fedora Activity Day was organized at R. V. College of Engineering, Bengaluru on Sunday, October 7, 2012. The participants were students from the Master of Computer Applications (MCA) department.
I started the day’s proceedings on best communication practices, project guidelines that need to be followed, and introduced the audience to the various Fedora sub-projects that they can get started with. The next session was on version control systems, and their importance with an introduction to Git. We had a hands-on session where the participants practised simple Git commands.
Praveen Kumar (HP) then spoke about Bugzilla, and how one can file bugs for Fedora. He also explained how to write meaningful bug reports.
Post-lunch, I introduced them to packaging concepts and terminology. Using the RPM packaging presentation, we taught them how to build, and test RPMs. Praveen Kumar explained the Fedora package review process, and the various tools and infrastructure that we use for the same.
The final session of the day was from Charles Rose (Dell) who introduced the participants to virtualization with KVM on Fedora. He also addressed the basic concepts involved in virtualization with numerous examples. The department lab is planning to move all their backend services to Virtual Machines.
Thanks to Prof. Renuka Prasad for working with us in organizing this workshop.
More photos are available in my /gallery.
October 2, 2012
blaze-textual is a library for rendering Haskell data types to bytestrings. It is now available in Fedora. Install it using:
$ sudo yum install ghc-blaze-textual-devel
You can import the Text module (for example) using:
ghci> :m + Blaze.Text
A simple example of using digit, integral, and float functions is shown below:
import qualified Data.ByteString.Lazy as L
import Blaze.ByteString.Builder
import Blaze.Text
import Blaze.Text.Int
main = do
let a = toLazyByteString $ digit 3
b = toLazyByteString $ integral 3
c = toLazyByteString $ float 3
L.putStrLn a
L.putStrLn b
L.putStrLn c
You can compile it using:
$ ghc --make test.hs
[1 of 1] Compiling Main ( test.hs, test.o )
Linking test ...
Running test gives:
$ ./test
3
3
3.0
September 30, 2012
HSH package allows you to use shell commands and expressions with Haskell programs. It is now available in Fedora. Install it using:
$ sudo yum install ghc-HSH-devel
You can import the HSH module using:
ghci> :m + HSH
The runIO function can execute a shell command:
Prelude HSH> runIO "date"
Sun Sep 30 21:29:50 IST 2012
You can use pipes with multiple commands by separating them with “-|-”:
Prelude HSH> runIO $ "date" -|- "wc"
1 6 29
The runSL function takes a command as an argument and returns the first line of the output:
Prelude HSH> runSL "cal"
" September 2012"
The setenv function can set an environment variable:
Prelude HSH> runIO "echo $TERM"
xterm
Prelude HSH> runIO $ setenv [("TERM", "gnome-terminal")] $ "echo $TERM"
gnome-terminal
The HSH package also provides shell equivalent commands. Few examples are shown below:
Prelude HSH> basename "/tmp/tmp/doc"
"doc"
Prelude HSH> dirname "/tmp/tmp/doc"
"/tmp/tmp"
Prelude HSH> pwd
"/tmp"
Prelude HSH> cut 2 ' ' "alpha bravo charlie delta echo"
"charlie"
The wcL and wcW functions count the lines and words respectively:
Prelude HSH> wcL ["hello", "world"]
["2"]
Prelude HSH> wcW ["first", "second", "third fourth"]
["4"]
September 23, 2012
MonadRandom package is a random number generation monad. It is now available in Fedora. Install it using:
$ sudo yum install ghc-MonadRandom-devel
The example of simulating a die is shown below:
import Control.Monad.Random
die :: (RandomGen g) => Rand g Int
die = getRandomR (1,6)
dice :: (RandomGen g) => Int -> Rand g [Int]
dice n = sequence (replicate n die)
main = do
values <- evalRandIO (dice 1)
putStrLn (show values)
Compile it using:
$ ghc --make die.hs
[1 of 1] Compiling Main ( die.hs, die.o )
Linking die ...
You can run it using:
$ ./die
[3]
$ ./die
[5]
$ ./die
[1]
$ ./die
[1]
$ ./die
[6]
The fromList function produces a random value from a weighted list:
Prelude> :m + Control.Monad.Random
Prelude Control.Monad.Random> fromList [(1,2), (3,4), (5,6)]
5
Prelude Control.Monad.Random> fromList [(1,2), (3,4), (5,6)]
1
Prelude Control.Monad.Random> fromList [(1,2), (3,4), (5,6)]
5
Prelude Control.Monad.Random> fromList [(1,2), (3,4), (5,6)]
3
Prelude Control.Monad.Random> fromList [(1,2), (3,4), (5,6)]
1
September 21, 2012
show package provides ShowQ, ShowFun and SimpleReflect modules. It is now available in Fedora. Install it using:
$ sudo yum install ghc-show-devel
To import the ShowQ module (for example), you can use:
Prelude> :m + ShowQ
The ShowFun module provides Typeable instances for IO expressions. The ShowQ module includes SmallCheck and QuickCheck support. Example uses of mysmallcheck, myquickcheck and tests functions in ShowQ are shown below:
Prelude ShowQ> mysmallcheck (\s -> length ("Hello") == 5)
Depth 0:
Completed 1 test(s) without failure.
Depth 1:
Completed 1 test(s) without failure.
Depth 2:
Completed 1 test(s) without failure.
Depth 3:
Completed 1 test(s) without failure.
Depth 4:
Completed 1 test(s) without failure.
Depth 5:
Completed 1 test(s) without failure.
Depth 6:
Completed 1 test(s) without failure.
()
Prelude ShowQ> myquickcheck (\s -> length ("Hello") == 5)
"+++ OK, passed 100 tests.
OK, passed 100 tests."
Prelude ShowQ> tests (\s -> length("Hello") == 5) 5 [["a"], ["b"]]
+++ OK, passed 100 tests.
"OK, passed 100 tests.1%b.\n1%a.\n"
The SimpleReflect module expands functions literally, and provides simple reflection of Haskell expressions containing variables. Few examples:
Prelude SimpleReflect> foldr f x [1..5]
f 1 (f 2 (f 3 (f 4 (f 5 x))))
Prelude SimpleReflect> sum [1..5] :: Expr
0 + 1 + 2 + 3 + 4 + 5
Prelude SimpleReflect> sum $ map (*x) [1..5]
0 + 1 * x + 2 * x + 3 * x + 4 * x + 5 * x
September 20, 2012
IOSpec package provides several modules that give a pure specification of functions in the IO monad. It is now available in Fedora. Install it using:
$ sudo yum install ghc-IOSpec-devel
To import a specific Fork module (for example), you can use:
Prelude> :m + Test.IOSpec.Fork
Test.IOSpec.Teletype provides a pure specification of the getChar and putChar functions. A simple example of echo is shown below:
{-# LANGUAGE NPlusKPatterns #-}
import Prelude hiding (getChar, putChar)
import qualified Prelude (putStrLn)
import qualified Data.Stream as Stream
import Test.IOSpec hiding (putStrLn)
import Test.QuickCheck
import Data.Char (ord)
echo :: IOSpec Teletype ()
echo = getChar >>= putChar >> echo
copy :: Effect ()
copy = ReadChar (\x -> Print x copy)
takeOutput :: Int -> Effect () -> String
takeOutput 0 _ = ""
takeOutput (n + 1) (Print c xs) = c : takeOutput n xs
takeOutput _ _ = error "Echo.takeOutput"
withInput :: Stream.Stream Char -> Effect a -> Effect a
withInput stdin (Done x) = Done x
withInput stdin (Print c e) = Print c (withInput stdin e)
withInput stdin (ReadChar f) = withInput (Stream.tail stdin)
(f (Stream.head stdin))
echoProp :: Stream.Stream Char -> Property
echoProp input =
forAll (choose (1,10000)) $ \n ->
takeOutput n (withInput input (evalIOSpec echo singleThreaded))
== takeOutput n (withInput input copy)
main = do
Prelude.putStrLn "Testing echo..."
quickCheck echoProp
You can compile it using:
$ ghc --make Echo.hs
[1 of 1] Compiling Main ( Echo.hs, Echo.o )
Linking Echo ...
Test it using:
$ ./Echo
Testing echo...
+++ OK, passed 100 tests.
The Test.IOSpec.IORef provides a pure specification of mutable variables. An example is shown below:
import Test.IOSpec
import Test.QuickCheck
readOnce :: Int -> IOSpec IORefS Int
readOnce x = do ref <- newIORef x
readIORef ref
readTwice :: Int -> IOSpec IORefS Int
readTwice x = do ref <- newIORef x
readIORef ref
readIORef ref
readIORefProp :: Int -> Bool
readIORefProp x =
let once = evalIOSpec (readOnce x) singleThreaded
twice = evalIOSpec (readTwice x) singleThreaded
in once == twice
main = quickCheck readIORefProp
You can compile it using:
$ ghc --make Refs.hs
[1 of 1] Compiling Main ( Refs.hs, Refs.o )
Linking Refs ...
Test it using:
$ ./Refs
+++ OK, passed 100 tests.
September 19, 2012
data-inttrie package is a simple lazy, infinite trie for integers. It is now available in Fedora. Install it using:
$ sudo yum install ghc-data-inttrie-devel
To import the data-inttrie module, use:
Prelude> :m + Data.IntTrie
The Bits class in Data.Bits module defines bitwise operations on integral types, and is defined using:
class Num a => Bits a where
The complement function, for example, reverses the bits in the argument:
Prelude> :m + Data.bits
Prelude Data.Bits> complement 3
-4
The apply function in Data.IntTrie applies a trie to an argument:
apply :: (Ord b, Bits b) => IntTrie a -> b -> a
For example:
Prelude Data.IntTrie> let bits f = apply (fmap f identity)
Prelude Data.IntTrie> bits (*3) 4 :: Int
12