2009年11月6日星期五

Tips in Mex Fortran, linux version

Starting from the day before yesterday, I need to write a Matlab interface for a Fortran source code, which is almost a black box for me, so that I can use "mex" to compile it and call it in Matlab.

The first thing is the compiler. In Windows, you can only choose a specific compiler, say, C/C++ or Fortran. So if you work with both C and Fortran code, you will have to use "mex -setup" from time to time, which is kinda annoying for a lazy person like me. But in Linux, you can work on a configuration file so that Matlab will automatically select the compiler for you.

First, in "/opt/R2009a/unix/bin/mexopts.sh", you can see the following segment:

#
FC='g95'
FFLAGS='-fexceptions'
FFLAGS="$FFLAGS -fPIC -fno-omit-frame-pointer"
FLIBS="$RPATH $MLIBS -lm"
FOPTIMFLAGS='-O'
FDEBUGFLAGS='-g'
#

The first line indicates the compiler for Fortran. "g95" is not provided, so, we will change it to gfortran which I think comes with Ubuntu. Thus, change this segement into:

#
FC='gfortran'
FFLAGS='-fexceptions'
FFLAGS="$FFLAGS -fPIC -fno-omit-frame-pointer"
FLIBS="$RPATH $MLIBS -lm"
FOPTIMFLAGS='-O'
FDEBUGFLAGS='-g'
#

Then, in Matlab, if you type
mex -setup

You will see something like:
The options files available for mex are:

1: /opt/R2009a/unix/bin/gccopts.sh :
Template Options file for building gcc MEX-files

2: /opt/R2009a/unix/bin/mexopts.sh :
Template Options file for building MEX-files via the system ANSI compiler

Choose 2. Then it's ok to compile.

The next thing is the interface. The help in Matlab has everything you know to create a wrapper, even some examples for you to follow. So although I know nothing about Fortran, I managed to write a "almost" complete interface which passed the compilation.

However, there're several important tips that costs me a whole day to figure out.
  1. The name of the source file must be ".F", otherwise it won't compile.
  2. If the source code you want to wrap was not developed by you, be very careful about all the data types in that code. For example, if one input for that function is "REAL", then it is 4-byte floating number. And in Matlab, unless specified, everything will be "double", which is 8-byte floating number. If you pass the double value directly into it, the value will NOT be preserved. So when you call the function in Matlab, use something like "nlam = int32(nlam);

    beta = single(beta);" Also, inside the interface, use something like "mxCopyPtrToReal4(beta_pr,beta,size)".
  3. The next thing is managing the output. When you create the output matrices, do not use mxCreateDoubleMatrix. Use "mxCreateNumericMatrix", which will let you to specify the output types like int32 or single.

2009年11月5日星期四

How-To: What should you do if you deleted files in NTFS by mistake

OK. Here is how it happens. One day, I was using the filezilla, pulling down a tiny little file to my hard disk of the desktop machine in my office. It is the major machine I use for research, and all my LaTeX papers, matlab codes are there.

Then, I select the whole "Research" disk, and downloaded that file into it. Then, assuming I am still selecting the tiny file on the remote server, I hit "delete" button. The computer seems to be hanging there with no response, which happens quite a lot when I use filezilla, by the way. So I force quit (thank god).

Then, after maybe a few minutes, I found that all my codes are gone somehow. WTF?!

It was all my fault, can't blame anyone.

So what I did was I delete almost everything in a NTFS disk, when I was using Ubuntu Linux. Bad news. There is no trash can, and everything is somehow "gone forever".

OK, enough for my miserable story.

The first thing I try is under Windows, a commercial software called "Recover my files". It will recover a lot of files, only without the file names. But file names and original locations are SO important because there are thousands of them and most of them are codes, which basically called by its name.

Then, I tried free wares in Ubuntu, such as ntfsundelete and photorec. They all can recover the files, but not the name and location. Well, at least I can recover them, it just takes time. My final choice is photorec.

There is one oerticular software that can recover your file, name and location, though. It is called: "Autopsy". Also a linux free ware, but it works like a webpage, and you can only recover the file one by one. But I did use it to recover a lot of vital source code.

One tip is, after you hurt your disk, you can use "ddrescue" to mirror the whole disk to another externel hard disk (should be much bigger than the one you want to backup). Then, the file you create works just like a disk, and yu can recover based on that without further damage.

The only thing you can do to prevent this from happening is to back up your system regularly. The reason that I can survive this with only one week of frustraion is that I uploaded the most vital part of my work to a remote server, for computation purpose. Because of that, I only need to recover a very small ratio, using the combanation of those softwares.

But starting from now, I will start to use file sync softwares to back up my works to another disk on a regular basis. My recommendation is Grsync. You can setup some commonly profiles, and just click excecute it will sync by only dealing with the differences. Very fast.

You can also write a script to automaticly sync the folders, say, every night. I do not have the time right now. Here are some useful links:

https://help.ubuntu.com/community/BackupYourSystem#grsync
http://www.unixgeeks.org/security/newbie/unix/cron-1.html
http://linuxbasement.com/content/backups-using-rsync-bash-cron
http://linuxgazette.net/104/odonovan.html

2009年10月6日星期二

我父母的母校竟然会作出这样的事情

我基本不开QQ的,今天突然想找人问一件事情,就点开了,不出意外各个群都蹦了出来。在握大学班级的群里,我看到了这样的一段话:

ICQ 04:12:07:
京理工大学也是很久没有消息了,我就是被南京理工大学隐藏起来的秘密,我的同学都很少有知道我的情况的。我本想把自己的经历写下来,但是,由于校方当初的 承诺,我一直在苦苦等待。熬了一年了,我也自费了一年,母亲累的连临时工都打不了了,我最近一直在发烧,生怕哪天没机会再写动了。我现在已经烧了2个月 了,其中高烧39度以上就熬了10多天,平常最少也是38度左右,这便使我完全与南京失去了联系。

看看南京理工大学是如何对待自己的学生的吧。在校的研究生,自己的学生生了病都不理不管。希望大家看看,帮我四处院校bbs上传传,谢谢大家。

ICQ 04:13:08:
南京理工大学是如何对待自己的学生的,在校研究生因患白血病而不与报销医疗费用!至使我曾因没钱治疗而复发两次!

给我留下唯一的就是我同学和大学母校关爱,以及南京理工大学把责任退给穷苦的大学生,而不率而自己的义务。

我叫赵磊,男,23岁,曾经是南京邮电大学的学生。2008年在南京理工大学读研期间检查不幸患上了急性淋巴白血病。因为顾虑到经费问题(北京治疗相对江 苏省会便宜一些,而且,要是在南京治疗还要租房,吃饭,等等。再三考虑下,回京治疗会便宜一些),在北京的母亲在我检查出来的当天就要求我的导师把我送回 北京治疗,校方当时也保证相关的医疗问题会在南京那边帮忙询问解决。
但是,南京理工大学除了鼓动学生捐款之外基本上没有任何举动,只是一味的安抚我和我的母亲。在北京因为我的档案问题而不能报销

他 是我大学的室友,就睡在我的对床。知道他的消息之后我一直以来的校内状态都是为他祈福。因为距离的缘故,我除了寄钱也做不了太多。可是看着这个可能只是部 分的留言,我实在不能平静。南京理工是我父母的母校,我一直将它当作我的第二故乡来崇敬。可是学生在学校生了重病,校方却没有任何实质上的帮助。鼓动学生 捐款又能捐多少呢?公费医疗难道是墙上画的饼么?学生身体好的时候就从学生身上榨取各种杂费,学生真正需要帮助的时候呢?怎么能不让人心寒?