23 February 2013

Installing apache 2.4.x and php 5.3.x from source on Centos

I used apache 2.4.3 and php 5.3.22 for this guide.
Before installing anything from the source you need to remove old libs/packages from the Centos. You can remove PHP by typing:
yum remove php
run following command to remove other php-libs:
yum remove php-cli php-gd php-mysql php-mbstring
There is no need to uninstall the existing apache http server, however you need to stop the process for new installation of apache httpd to work.
  • Create a directory where you will download the tars, e.g. /usr/tmp/
  • cd to this directory
  • Download the src of apache web server, apr, apr-util and php by using following commands:
wget http://apache.techartifact.com/mirror//httpd/httpd-2.4.3.tar.gz
wget http://apache.techartifact.com/mirror//apr/apr-1.4.6.tar.gz
wget http://apache.techartifact.com/mirror//apr/apr-util-1.5.1.tar.gz
wget http://www.php.net/get/php-5.3.22.tar.gz/from/us1.php.net/mirror
You also need to install gcc and openssl-devel (for ssl support).
yum install gcc
yum install openssl-devel
Installing Apache HTTP Server:
Extract all the 4 archives downloaded in the /usr/tmp/ dir using following command:
tar xvfz httpd-2.4.3.tar.gz
tar xvfz apr-1.4.6.tar.gz
tar xvfz apr-util-1.5.1.tar.gz
tar xvfz php-5.3.22.tar.gz
cd to the httpd-2.4.3 and run the following command (if you want to install Apache with ssl support add --enable-ssl option):
./configure --enable-so --with-apr=/usr/tmp/apr-1.4.6 --with-apr-util=/usr/tmp/apr-util-1.5.1
Run following commands to install the Apache:
make install
Start the Apache server using the command:
/usr/local/apache2/bin/apachectl start
Installing PHP:
cd to php-5.3.22 directory and run the following command to see the configuration options available with PHP installation:
./configure --help
Select the php modules you want to install, also add the --with-apxs2 option to configure the php with Apache server we installed, following command contains common php modules:
./configure  --with-apxs2=/usr/local/apache2/bin/apxs --disable-debug --enable-inline-optimization --enable-mbregex --enable-gd-native-ttf --enable-mbstring --enable-zend-multibyte --enable-sockets --enable-sysvsem --enable-sysvshm --enable-zip --with-gettext --with-mcrypt --with-mhash --with-openssl --with-pcre-regex --with-pear --with-zlib --with-xsl 
Compile and install the PHP:
make install
Restart the Apache server and you are done. I have tested this on Centos 5.x but it should work on Centos 6.x. Following are the important paths:
  • Default web directory, where your all web stuff go: /usr/local/apache2/htdocs
  • http.conf path:/usr/local/apache2/conf/httpd.conf
  • php.ini path:/usr/local/lib/php.ini

PITA 6: PHP Warning: Unknown: open(/var/lib/php/session/...., O_RDWR) failed: Permission denied (13) ...

Recently I compiled php 5.3 on my Centos 5.x machine, everything worked fine until I noticed that the HTTP session was not getting created.
I checked the apache error logs and found this php warning getting repeated:
 PHP Warning:  Unknown: open(/var/lib/php/session/...., O_RDWR) failed: Permission denied (13) in Unknown on line 0
I tried to look for the solution on the net, couldn't find a proper solution except someone saying that apache sever should have write access to the session directory (/var/lib/php/session in my case). I did: 
chmod 777 /var/lib/php/session
and restarted the http server, no warning this time in the error logs, everything started working as expected.

My ls -l looks like following:
drwxrwxrwx 2 root apache 270336 Feb 23 14:27 session

10 February 2013

A note on Matrix Multiplication in Java

I needed to calculate the B AxAT for some task. Here A is a matrix of some size. Initially I considered a few open-source libs e.g. Colt, Parallel Colt and JAMA, I found JAMA to be a bit easier to use and also it had better documentation than the other two. The size of my matrix A was around 1700x2700. It used to take around 65 secs for getting matrix with JAMA. It was fine with me util recently, my matrix size grew to 7000x14000. First let me make it clear that this matrix A is sparse and does contain values between [0,2]. I tried JAMA on this new matrix and tried to calculate the matrix B, first it threw Out of Memory error, when I increased the heap memory to 4GB (max I could allocate!) it took some time in calculation and at the end again threw Out of Memory error.

I tried to run the program on a higher memory machine and allocated 10GB to heap. Here is the code for JAMA for matrix multiplication:
double[][] matrix // your pre-populated matrix
Matrix aMatrix = new Matrix(matrix);
Matrix bMatrix = aMatrix.transpose();
Matrix finalMatrix= aMatrix.times(bMatrix);
double[][] finalArray = finalMatrix.getArray(); // this step is not necessary
On this higher configuration machine the JAMA ran around 3 Hours before I killed it. I started looking for other libs and other ways to calculated this matrix in as less as possible amount of time. For me a time between 15-30 minutes was acceptable. While googling I got this link: http://stackoverflow.com/questions/529457/performance-of-java-matrix-math-libraries, I found this benchmark page: http://code.google.com/p/java-matrix-benchmark/ and decided to give EJML a try, basic code for multiplication is:
double[][] matrix // your pre-populated matrix
SimpleMatrix aMatrix = new SimpleMatrix(matrix);
SimpleMatrix cMatrix = aMatrix.mult(aMatrix.transpose());
SimpleMatrix cMatrix =  aMatrix.mult(bMatrix)
EJML calculated the smaller matrix multiplication (1700x2700) in 6-7 Secs, which was way faster compared to JAMA which took more than 60 Secs for the same calculation. So I went ahead and ran it on the bigger matrix and left it running on my home machine which has 12GB RAM and an Intel i7 Proc. When I returned from my work after 9 hours the program was still running. But after half an hour I saw the output. It took 9 hours and 30 minutes for EJML to finish this calculation and a total of 5.5 GB of RAM during this operation.

Meanwhile I was also looking at JBLAS, but due to its dependency on some native libs, I was not able to run it on production as well as on my home machine which runs Windows 8. I had to install cygwin to make it work on Win 8. I ran it against the small matrix and output appeared on the screen in no time. Total time taken for the 1700x2700 matrix was 0.6 Secs, it was exciting to see the output so quickly. Following is the code for matrix multiplication using JBLAS:
double[][] matrix // your pre-populated matrix
DoubleMatrix aDoubleMatrix = new DoubleMatrix(matrix);
DoubleMatrix bDoubleMatrix = aDoubleMatrix.transpose();
DoubleMatrix fDoubleMatrix = aDoubleMatrix.mmul(bDoubleMatrix);
I ran the program on the big matrix, and got the results immediately, in 6 Secs. It uses the native libs BLAS and LAPACK, which make it so much faster than other purely java based libs. Also it uses all the cores available on your machine. Support from the author of the library is also great, he went some extra miles to resolve the issue I was facing, when I used the lib on a Centos 6 box (due to some older GLIB installation).