Thursday, February 26, 2009

CentOS: Add/Remove Software from DVD image

If no internet connection is available on one's server, Add/Remove Software tool (i.e. pirut) hangs! First thing I did (as a root) to make it run was to go to /etc/yum.repos.d/ and rename file CentOS-Base.repo.mv CentOS-Base.repo CentOS-Base.repo.backup As a result pirut started and showed all installed packaged. However, no packages available for installation were shown. The reason is that there are no repositories. For that reason I mounted DVD image of my CentOS 5.2 in /media/CentOS. If CentoOS folder does not exist please create it. To mount the DVD I used the following:mount -o loop /home/me/CentOS-5.2-x86_64-bin-DVD.iso /media/CentOSThen I change enabled=0 to enabled=1in file /etc/yum.repos.d/CentOS-Media.repo. And finally, I could install software from CentOS dvd.

Thursday, February 19, 2009

MATLAB: mex out of memory problem using mxCalloc

When writing mex files in ANSI C in Matlab, sometimes there is problem with memory leaks in mex files that eventually causes Out of memory error. One reason for this problem is allocating memory in mex files using e.g. malloc or calloc functions, rather than using Matlab's equivalents e.g. mxMalloc or mxCalloc. The advantage of Matlab's memory allocation functions is that, in theory, Matlab automatically frees allocated dynamic memory when the mex function finishes. Therefore, we do not loose memory. However, when malloc or calloc are used we must deallocate memory ourselves. Matlab provides mxFree function, but we do not need to use it.

Recently I had Out of memory problems with one of my mex files. It took me a while to find the source of it. It was difficult because I did not know why I'm losing memory, even though I was only using mxMalloc to allocate memory, and Matlabs automatically frees all allocated memory. Finally, I found what was the problem. The problem was that memory allocated using mxMalloc is not always freed. To be specific, the problem was with the way I return matrices from mex to Matlab. I was doing this using mxCalloc. In fact there are two ways of returning matrices to Matlab. Method 1 which is based on mxCalloc (in my case) and the second (Method 2) not using any memory allocation. The first method results in memory leaks, although mxCalloc is used, the second method does not have this problem. ANSI C code snippets with these methods://return matrix_a using Method 1 (wrong!)
plhs[0] = mxCreateDoubleMatrix(mrows,ncols, mxREAL);
mxSetPr(plhs[0],makeVectorFromMatrix(matrix_a,mrows,ncols));
or//return matrix_a using method 2 (good!)
double *out_matrix_a;
plhs[0] = mxCreateDoubleMatrix(mrows,ncols, mxREAL);
out_matrix_a = mxGetPr(plhs[0]);
makeVectorFromMatrix2( out_matrix_a, matrix_a,mrows,ncols);
where functions makeVectorFromMatrix and makeVectorFromMatrix2 are:double* makeVectorFromMatrix(double **inData, int mrows, int ncols) {
/*Makes output Vector for METHOD 1*/
int i,j,count=0;

double *out= (double*) mxMalloc((mrows+1)*(ncols+1)*sizeof(double));
for (j=0;j<ncols;j++) {
for (i=0;i<mrows;i++) {

*(out+count)= inData[i][j];
count++;

}
}
return out;
}


void makeVectorFromMatrix2(double *outData, double **inData, int mrows, int ncols) {
/*Makes output Vector for METHOD 2*/
int i,j,count=0;
for (j=0;j<ncols;j++) {
for (i=0;i<mrows;i++) {

*(outData+count)= inData[i][j];
count++;

}
}
}
Full codes of Matlab script and ANSI C mex function for testing memory leaks with these two methods of returning variables to Matlab is attached. The mex function mymexfunction.c creates two 2D arrays, and takes third array as anar argument passed from Matlab. After some processing (multiplying elements of these matrices by 2) the results are returned to Matlab. Executing attached mxCallocTest with argument 1, return matrices by mex function (mymexfunction) using Method 1 (i.e. wrong method) and with argument 2 with method 2 (good method). mxCallocTest.m runs in a long loop executing mymexfunction. When method 1 is used to return variables to matlab, it soon results in Out of Memory error. The second method does not have this problem. An example of results obtained using mxCallocTest.m with argument 1:
>> mxCallocTest(1)
10

20

MATLAB(13826,0x603d000) malloc: *** vm_allocate(size=32034816) failed (error code=3)
MATLAB(13826,0x603d000) malloc: *** error: can't allocate region
MATLAB(13826,0x603d000) malloc: *** set a breakpoint in szone_error to debug
??? Out of memory. Type HELP MEMORY for your options.

Error in ==> mxCallocTest at 24
[matrix_a,matrix_b,matrix_c]= mymexfunction(mrows,ncols,METHOD,c);
The same script but using second method (mxCallocTest(2)) works fine, without memory problems.

The attached scripts also show one, useful way of passing matrices into mex functions, mapping them into 2D arrays, and returning 2D arrays to Matlab as matrices.

Download

Matlab script and ANSI C mex function are here. The files were tested on Intel Mac X 10.4 with Matlab 7.4 and Solaris 9 (UltraSPARC Sun server) with Matlab 7.0.

Tuesday, February 17, 2009

MATLAB: vector to string conversion

To convert an vector to a string vect2str script by Luca Balbi from MATLAB Central File Exchange can be used. It works very good, but a little problem for me was that it works only with newer versions of MATLAB. For example it works in Matlab 7.4, but it does not work in Matlab 7.0. The reason is that it uses inputParser object, that is not available in version 7.0. For that reason I modified vect2str.m in such a way that instead of inputParse, getargs function is used (getargs is also available in this post).
Some example of how vect2str works:>> vect2str([1:5],'formatString','%.2f','separator','--')

ans =

1.00--2.00--3.00--4.00--5.00

>> vect2str([1:5],'formatString','%2d','separator',' -- ')

ans =

1 -- 2 -- 3 -- 4 -- 5

>> sprintf(vect2str([1:5],'formatString','%.2f','separator','\t'))

ans =

1.00 2.00 3.00 4.00 5.00

>> vect2str([1:5],'formatString','%.2f','separator','\t')

ans =

1.00\t2.00\t3.00\t4.00\t5.00

>>

Download

Scripts are here.

Friday, February 13, 2009

PHP: Check if PHP sessions are working

Sometimes, there can be some problems with sessions in PHP. In such cases, it is useful to have a very simple and short script that can be used to check if session variables are created, saved and deleted correctly. The following two files can be used:
index.php<?php
session_start();
$_SESSION['user']='tom';
$role = "admin";
session_register('role');
?>
<html>
<body>
Created session variables are:
user='<?=$_SESSION['user']?>' and role='<?=$_SESSION['role']?>'<br><br>
<?='<a href="print.php">Check if session variables are saved</a>';?>
<br><br>
Session ID is: <?= session_id(); ?>

</body>
</html>

print.php<?php
session_start();
if (isset($_GET['delete'])) {
session_unset();
//$_SESSION = array();
session_destroy();

}
?>
<html>
<body>
Saved session variables are:<br><br>
user:<?=$_SESSION['user'];?><br>
role:<?=$_SESSION['role'];?>
<br><br>
Session ID is: <?= session_id(); ?><br><br>
<a href="?delete=1">Delete session</a><br><br>
<a href="index.php">Go back</a>
</body>
</html>
The screenshots are:
index.php:
print.php:
After clicking 'Delete session':

Download

The above scripts can be downloaded from here.

Monday, February 09, 2009

bash: extract all lines containing a string in many files

The following bash snippets scans through all txt files in current directory, searches for lines containing string "something" and saves these lines in "out" directory (the directory must be created prior to running this command) in new files that end with "-something.txt"str="something"; for f in *.txt ; do cat $f | grep $str > out/$f-$str.txt; done
Or extract last name in many txt files and save them in new file along with the name of the orginal filesfor a in *.txt; do echo $a `tail -1 $a`; done > out.txt

Friday, February 06, 2009

ubuntu-server: installing different GUIs

By default, ubuntu-server 8.04 does not come with any graphic desktop environment or GUI. Which is natural, because it is server. However, sometimes it would be useful to have some GUI. It is not a problem to install it. Admins have quite a few options for GUIs. Here I will show only few of them.

Method

The following examples were performed using ubuntu-server 8.04.2 installed as a guest os in VirtualBox 2.1.2. The screenshot of a system was performed just after the fresh install of ubuntu. After installation of each desktop the system was reverted to current snapshot. Therefore, in each example, only one GUI was present in the system. For each example /etc/apt/source.list was not modified. All desktops are available in default ubuntu repositories.

To start any of the following GUIs after they have been installed, the following command can be used: startx

ubuntu-desktop

sudo aptitude install ubuntu-desktopThe command will take 560MB of space. This amount includes about 37MB for xorg. It installs full ubuntu/gnome desktop with every software normally available in ubuntu-desktop (OpenOffice, Firefox, etc...). It takes quite a long time to install. The main disadvantage of this, is that it de facto converts ubuntu-server into ubuntu-desktop. This has not much sense, since we want ubuntu-server and not ubuntu-desktop. However, such an option exists, and in some cases it may be useful.Instead of gnome base ubuntu-desktop, in a similar way, user can install kubuntu-desktop or xubuntu-desktop.

gnome-core

sudo aptitude install xorg gnome-coreThe command will download about 160MB. It also takes a bit of time to install. As the name suggests it installs only basic gnome functionality.

xfce4

sudo aptitude install xorg xfce4The command will download about 80MB. Again, as the name suggest, it installs xfce4 desktop environment. Installation time is not so terrible.

enlightenment

sudo aptitude install xorg enlightenmentThis will download about 50MB.

fluxbox

sudo aptitude install xorg fluxboxThe command will download about 39MB. It must be rememberd that xorg is about 37MB! Hance, fluxbox is about 2MB! Most of the installation time is used for installing xorg.

twm

sudo aptitude install xorg twmIt will download about 38MB. It must be remembered that xorg is about 37MB, therefore twm is about 1MB!

ctwm

sudo aptitude install ctwmThe command will download about 38MB. This is slightly improved version of twm. Specifically it adds virtual desktops. However, for me there was some problem in that I could not navigate through the menu.

vtwm

sudo aptitude install vtwmThe command will download about 38MB. Again, this is slightly improved version of twm. Specifically it expands desktop.

Conclusions

There are many desktops that can be installed in ubuntu-server. As far as I am concerned, for me the smaller desktop the better. This is because, they do not consume lots of server resources, their installation is fast, and they are perfect for using with vncserver. Therefore, I would recommend fluxbox, twm, ctwm or vtwm.

Wednesday, February 04, 2009

Sun Fire X4450: serial connection and terminal type

In Sun Fire X4450 Server Installation Guide there is written that in order to apply "Power for the First Time" it is necessary to "connected to the server through the serial management port" using "terminal device or the terminal emulation software running on a laptop or PC". Unfortunately it does not say or does not recommend which terminal emulation software to use, nor I did not find this information in the web. Therefore I decided to use HyperTerminal application available by default in Windows XP (Start - Accessories - Communication). To establish a connection with the SP the terminal must be be set as follows (from the Guide):
  • - 8,N,1: eight data bits, no parity, one stop bit
  • - 9600 baud
  • - Disable hardware flow control (CTS/RTS)
  • - Disable software flow control (XON/XOFF)
With this settings everything worked as written in the Guide - I could start configuration of the pre-installed Solaris 10 system through the serial management port. I was following prompts and I was answering simple configuration questions such as Language, Localization, Terminal etc. However the greatest problem was with questions about terminal!. To be specific, I was asked to choose the type of terminal that I was using from the list of available terminal types". There were types as: ASCI CRT, DEC VT100, ... and I don't remember other types. My problem was that I did not know what type (terminfo) HyperTerminal was. So, what I did is that I changed the emulation type in HyperTermianl to VT100 (in File - Proprieties - Settings) In server, I choose option DEC VT100 (I think it was option no. 3). And it worked!. To sum up I used the following:
VT100 in HyperTerminal and DEC VT100 in Sun

Note

The above was the second trial of setting the server through the console. In the first attempt, the terminal type in question about terminal was incorrectly selected! As a result Sun and HyperTerminal did not work very well and further configuration was not possible. The server was restarted and hopefully the second attempt was good.

Sunday, February 01, 2009

CakePHP: Image Upload

Searching the web for some image upload component for cakePHP I found the following article: Image Upload Component (CakePHP) - Labs. Although this article is very useful, it was written over a year ago and unfortunately, the code of the image upload component available for download did not work straight away for me (I tested it using CakePHP 1.2.1.8004 Stable on xampp 1.7 for linux on Ubuntu 8.10). Some problems were: lack of index action in images controller, some unknown helpers (i.e. labelTag), missing model for images table, typo errors in view file upload.thtml, id field in images table was not a primary key, and some others. Three example screenshots are below:



Therefore, to make this component work I had to perform some modifications. Although, this component can be improved in may ways, I only made modifications necessary to make this component work. I did not perform full test of this componet. I just made it work, i.e. it is possible to upload images now (at least for me now).

Download

The modified image upload component is here. Hope it works and it will be useful. In a similar way I also modified component for the multiple file upload. It can be downloaded from here.