Wednesday, 29 December 2010

Printing Large HTML tables on multiple pages

This tutorial is to have header and footer of a large HTML table on every page when printing:

Try this:

1. Create a HTML document with the following code:



2. Note the use of thead, tbody and tfoot.

3. I have tested it on Firefox, IE8.

Thanks...

Friday, 3 December 2010

Setting Up an MPI Cluster on Ubuntu...

Setting up a MPI cluster on Ubuntu involves the following steps:

1. Install OpenMPI on all machines.
$sudo apt-get install libopenmpi-dev openmpi-bin openmpi-doc

2. Installing openSSH on all machines.
$sudo apt-get install openssh-server openssh-client

3. Select one Master machine say M and others will be slaves say S.

4. Now we need to make sure M can logon to all S machines with out a password.

Create a user with same name on all machines. Say 'fawad'.

Login on M using 'fawad' and generate public/private key pair.

$ssh-keygen -t dsa

This command will generate id_dsa and id_dsa.pub files in ~/.ssh folder. id_dsa is the private key and id_dsa.pub is the public key. Now we need to keep the private key and copy the public key on to all the slaves machines.

5. Use scp to copy id_dsa.pub in the home directory of fawad on all S machines.

6. On the S machines do:

Login as fawad.
$cd ~/.ssh
$ cat id_dsa.pub >> authorized_keys

7. Now the M machine will be able to logon to all machines without password.

8. On the M machines.
Write a MPI code and save it. In my case i used the following code and saved it in hello_nodes.c

--------------hello_nodes.c--------------------

/*

A simle example program using MPI - Hello World

The program consists of one receiver process and N-1 sender
processes. The sender processes send a message consisting
of their hostname to the receiver. The receiver process prints
out the values it receives in the messages.

Compile the program with 'mpicc hello_nodes.c -o hello_nodes'
To run the program on four processors do 'mpiexec -n 4 ./hello'

*/

#include
#include
#include

int main(int argc, char *argv[]) {
const int tag = 42; /* Message tag */
int id, ntasks, source_id, err, i;
MPI_Status status;
char msg[80]; /* Message array */

err = MPI_Init(&argc, &argv); /* Initialize MPI */
if (err != MPI_SUCCESS) {
printf("MPI_init failed!\n");
exit(1);
}

err = MPI_Comm_size(MPI_COMM_WORLD, &ntasks); /* Get nr of tasks */
if (err != MPI_SUCCESS) {
printf("MPI_Comm_size failed!\n");
exit(1);
}

err = MPI_Comm_rank(MPI_COMM_WORLD, &id); /* Get id of this process */
if (err != MPI_SUCCESS) {
printf("MPI_Comm_rank failed!\n");
exit(1);
}

/* Check that we run on at least two processors */
if (ntasks < 2) {
printf("You have to use at least 2 processors to run this program\n");
MPI_Finalize(); /* Quit if there is only one processor */
exit(0);
}

/* Process 0 (the receiver) does this */
if (id == 0) {
int length;
MPI_Get_processor_name(msg, &length); /* Get name of this processor */
printf("Hello World from process %d running on %s\n", id, msg);
for (i=1; i err = MPI_Recv(msg, 80, MPI_CHAR, MPI_ANY_SOURCE, tag, MPI_COMM_WORLD, \
&status); /* Receive a message from any sender */
if (err != MPI_SUCCESS) {
printf("Error in MPI_Recv!\n");
exit(1);
}
source_id = status.MPI_SOURCE; /* Get id of sender */
printf("Hello World from process %d running on %s\n", source_id, msg);
}
}

/* Processes 1 to N-1 (the senders) do this */
else {
int length;
MPI_Get_processor_name(msg, &length); /* Get name of this processor */
err = MPI_Send(msg, length+1, MPI_CHAR, 0, tag, MPI_COMM_WORLD);
if (err != MPI_SUCCESS) {
printf("Process %i: Error in MPI_Send!\n", id);
exit(1);
}
}

err = MPI_Finalize(); /* Terminate MPI */
if (err != MPI_SUCCESS) {
printf("Error in MPI_Finalize!\n");
exit(1);
}
if (id==0) printf("Ready\n");
exit(0);
}


-------------------------------------------

9. Compile the code.
$ mpicc hello_nodes.c -o hello_nodes

10. Run the code on the same machine.
fawad@fawad-virtual-machine:~$ mpirun -np 4 hello_nodes
Hello World from process 0 running on fawad-virtual-machine
Hello World from process 2 running on fawad-virtual-machine
Hello World from process 1 running on fawad-virtual-machine
Hello World from process 3 running on fawad-virtual-machine
Ready

11. Create a file called hostfile, which contains the slave computers in the MPI cluster:

fawad@fawad-virtual-machine:~$ cat hostfile
172.16.92.130
172.16.92.129

12. Copy the source code hello_mpi to all slave machines using scp.

13. Then run the following command to run the job on all slave machines:

fawad@fawad-virtual-machine:~$ mpirun --hostfile hostfile -np 10 hello_nodes
Hello World from process 0 running on ubuntu
Hello World from process 6 running on ubuntu
Hello World from process 4 running on ubuntu
Hello World from process 8 running on ubuntu
Hello World from process 3 running on fawad-virtual-machine
Hello World from process 2 running on ubuntu
Hello World from process 9 running on fawad-virtual-machine
Hello World from process 5 running on fawad-virtual-machine
Hello World from process 7 running on fawad-virtual-machine
Hello World from process 1 running on fawad-virtual-machine
Ready


DONE :).

Now the next step is that we need to try creating a NFS so that we do not have to copy the binary file all the time.
Best MPI Books i would recommend:

Wednesday, 1 December 2010

Finding and killing an Oracle session...

Finding a session:
------------------
SELECT USERNAME, OSUSER, MACHINE, SID, SERIAL#, STATUS, SECONDS_IN_WAIT
from v$session
WHERE
TYPE != 'BACKGROUND'
AND username = 'STAN4'
order by seconds_in_wait desc

Killing a session:
------------------
ALTER SYSTEM KILL SESSION 'sid,serial#' IMMEDIATE;

Tuesday, 30 November 2010

Java Regex...

Parsing Using Java Regex:

import java.util.regex.*;

public class SampleRegex
{
public static void main(String[] params)
{
Pattern pattern = Pattern.compile("(.*):(.*)");
Matcher matcher = pattern.matcher(params[0]);
if(matcher.matches())
{
System.out.print("Key:");
System.out.println(matcher.group(1));
System.out.print("Value:");
System.out.println(matcher.group(2));
}
else
System.out.print("No match");
}
}

Further reading links:
1. http://www.developer.com/article.php/1460561
2. http://download.oracle.com/javase/tutorial/essential/regex/

Friday, 26 November 2010

do While Loop PL/SQL Oracle...

There is nothing like do while loop in PL/SQL. But you can do the following:

SQL> declare
2 n_num number := 1;
3 begin
4 loop
5 dbms_output.put(n_num||', ');
6 n_num := n_num + 1;
7 exit when n_num > 5;
8 end loop;
9 dbms_output.put_line('Final: '||n_num);
10 end;
11 /

For details visit: http://www.dba-oracle.com/concepts/pl_sql_repeat_until_loop.htm

Matching Date in Sed: A Filename comparison example....

$ echo "01_hello_world_filename_22-07-2010.csv" | sed 's/^\([0-9]\{2\}\)\(_hello_world_filename_\)\(0[1-9]\|[12][0-9]\|3[01]\)-\(0[1-9]\|1[012]\)-\(19\|20\)[0-9]\{2\}.Csv$/Matched/ig'

Output: Matched

Wednesday, 24 November 2010

SED : Tutorial 2

Deleting Everything after first H
echo "Fawad 123 Hello How are you" | sed s/H.*//
Output: Fawad 123

Double Substitution
$ echo "Fawad is a friend of Nazir" | sed 's/Fawad/Midhat/g;s/Nazir/Fawad/g'
Output: Midhat is a friend of Fawad

Matching Exact Patterns using [ and ].
$ echo "Aaminah is a name" | sed s/[aA][aA]/BB/g
Output: BBminah is a name

REF: http://www.grymoire.com/Unix/Regular.html

Regular Expression Matches
[] The characters "[]"
[0] The character "0"
[0-9] Any number
[^0-9] Any character other than a number
[-0-9] Any number or a "-"
[0-9-] Any number or a "-"
[^-0-9] Any character except a number or a "-"
[]0-9] Any number or a "]"
[0-9]] Any number followed by a "]"
[0-9-z] Any number,
or any character between "9" and "z".
[0-9\-a\]] Any number, or
a "-", a "a", or a "]"

Matching a specific number of sets with \{ and \}
Regular Expression Matches
_
* Any line with an asterisk
\* Any line with an asterisk
\\ Any line with a backslash
^* Any line starting with an asterisk
^A* Any line
^A\* Any line starting with an "A*"
^AA* Any line if it starts with one "A"
^AA*B Any line with one or more "A"'s followed by a "B"
^A\{4,8\}B Any line starting with 4, 5, 6, 7 or 8 "A"'s
followed by a "B"
^A\{4,\}B Any line starting with 4 or more "A"'s
followed by a "B"
^A\{4\}B Any line starting with "AAAAB"
\{4,8\} Any line with "{4,8}"
A{4,8} Any line with "A{4,8}"

Matching words with \< and \>

Searching for a word isn't quite as simple as it at first appears. The string "the" will match the word "other". You can put spaces before and after the letters and use this regular expression: " the ". However, this does not match words at the beginning or end of the line. And it does not match the case where there is a punctuation mark after the word.
There is an easy solution. The characters "\<" and "\>" are similar to the "^" and "$" anchors, as they don't occupy a position of a character. They do "anchor" the expression between to only match if it is on a word boundary. The pattern to search for the word "the" would be "\<[tT]he\>". The character before the "t" must be either a new line character, or anything except a letter, number, or underscore. The character after the "e" must also be a character other than a number, letter, or underscore or it could be the end of line character.

Backreferences - Remembering patterns with \(, \) and \1

Another pattern that requires a special mechanism is searching for repeated words. The expression "[a-z][a-z]" will match any two lower case letters. If you wanted to search for lines that had two adjoining identical letters, the above pattern wouldn't help. You need a way of remembering what you found, and seeing if the same pattern occurred again. You can mark part of a pattern using "\(" and "\)". You can recall the remembered pattern with "\" followed by a single digit. Therefore, to search for two identical letters, use "\([a-z]\)\1". You can have 9 different remembered patterns. Each occurrence of "\(" starts a new pattern. The regular expression that would match a 5 letter palindrome, (e.g. "radar"), would be
\([a-z]\)\([a-z]\)[a-z]\2\1

Examples:
1. Remember a pattern
$ echo "Aaminah Aaminah is a name" | sed 's/\(A[a-z]*\) \1/\1/g'
Output: Aaminah is a name

2. Eliminate the repeating characters.
$ echo "mmmmy nnname iis ffawad." | sed 's/\([a-z]\)\{1,3\}\1/\1/g'
Output: my name is fawad.

3. Keep first word and delete the rest.

$ echo "mmmmy nnname iis ffawad." | sed 's/\([a-z]*\).*/\1/'
Output: mmmmy

3. Printing the Last name & replacing the first name with last.
$ echo "Mr. Fawad Nazir" | sed 's/\([Ff]awad\) \([Nn]azir\)/\2 \1/'
Output: Mr. Nazir Fawad
$ echo "Mr. Fawad Nazir" | sed 's/\([Ff]awad\) \([Nn]azir\)/\2/'
Output: Mr. Nazir

3. Subtituting exact match words:
$ echo "ad Fawad ad Nazad ad" | sed 's/ ad/ xx /g'
Output: ad Fawad xx Nazad xx
$ echo "ad Fawad ad Nazad ad" | sed 's/ ad / xx /g'
Output: ad Fawad xx Nazad ad
$ echo "ad Fawad ad Nazad ad" | sed 's/ad/ xx /g'
Output: xx Faw xx xx Naz xx xx
$ echo "ad Fawad ad Nazad ad" | sed 's// xx /g'
Output: ad Fawad ad Nazad ad
$ echo "ad Fawad ad Nazad ad" | sed 's/\/ xx /g'
Output: xx Fawad xx Nazad xx
$ echo "ad Fawad ad Nazad ad" | sed 's/\/xx/g'
Output: xx Fawad xx Nazad xx


END of Tutorial 2.

Executive Summary: Anthropic's Claude Mythos Model

  What Happened Anthropic announced  Claude Mythos Preview , a new AI model it describes as its most capable ever — and so powerful in cyber...