Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Sunday, May 20, 2007

Open source codes of each paper

Usually when you read a journal paper and happen to to like it. So you want to implement their idea. Now the problem occurs, how can I implement those ideas? I need to work out every detail to work it out. Also only when you work it out in detail, you can say you understand it since you make computer understand it.

Why do all papers have their code attached? I will do it for all my papers from now on.
  • to make my code more organized
  • to make my work better known by others if they can see how concrete results come out
  • to improve the work if I can get the feedback from others.

Saturday, October 21, 2006

Beamer's columns

\frame
{
\frametitle{Interview}

\begin{columns}
\begin{column}{0.5\textwidth}
\begin{itemize}
\item current symptoms
\item past medical history
\item family history
\item social history
\item review of systems
\end{itemize}
\end{column}

\begin{column}{0.5\textwidth}
\begin{itemize}
\item detect hypertension
\item recognize emergency
\item recognize reversible causes
\item find chronic target organ damage
\item discover co-morbid conditions
\item plan therapy
\end{itemize}
\end{column}
\end{columns}
}

You can have as many columns, of whatever widths, as you like. Note that
within a column, \textwidth will correspond to the column width, not the
width of the beamer slide, so it works pretty much like minipage.
Material within columns is vertically centred, and you can have part of
the slide in normal (single column) format, and other parts in more than
one column.

Friday, October 20, 2006

Insert List into Tabular in Latex

Here is the code. But I still cannot figure out how to do under Beamer. The ultimate goal is to insert the Figure into the left top corner and put the list on the right.

\documentclass[12pt]{article}
\makeatletter % Yes, it's horrible
\def\spacehack{%
\@minipagetrue% % Disables spacing above
\expandafter\everypar\expandafter{% % Add stuff to \everypar
\the\everypar% % Do what was there before
\@minipagefalse% % Clear the mystic flag
\everypar={}% % And reset \everypar
}%
\let\@oldbs=\\% % Remember old \\ command
\def\\{\nointerlineskip\@oldbs}% % Turn off final odd spacing
}
\makeatother

%
\begin{document}
%
\begin{tabular}{p{40mm}p{40mm}p{40mm}}\hline
& Advantages & Disadvantages \\ \hline
Method 1 & \spacehack\begin{enumerate}
\setlength{\itemsep}{-\parsep}
\item item one
\item item two
\item item three
\end{enumerate} & \spacehack\begin{enumerate}
\item item one
\item item two
\end{enumerate} \\[-12pt] \hline
Method 2 & \begin{enumerate}
\item item one
\end{enumerate} & \begin{enumerate}
\item item one
\item item two
\end{enumerate} \\ \hline
Method 3 & \begin{enumerate}
\item item one
\end{enumerate} & \begin{enumerate}
\item item one
\end{enumerate} \\ \hline
\end{tabular}

\end{document}

Tuesday, September 12, 2006

(-1)^(1/3)=0.5000 + 0.8660i ?

This time, I meet another problem about math operation in software.
In matlab, when you try
(-1)^(1/3)
What result do we get. It is not -1 but 0.5000 + 0.8660i
You need to use nthroot(-1,3) to get -1.

This time, excel gives me the answer I want.

I am so frustrated now. This time I use more than 4 hours to debug my program which contains similar operations in my matlab code.

Do I know too little about basic math or Matlab provides too much.

Sunday, September 10, 2006

EXCEL vs GOOGLE

When I try to verify the correctness of a sequence. I need to compute the following fomula

=-1*((0.4^4-3*(0.4^3)+0.4^2)*10+(0.4^3-2*(0.4^2))*15+(0.4^2-0.4)*20+0.4*25+30)+100*(0.4^4-2*(0.4^3)+1)+50*(-0.4^4+2*(0.4^3))

Excel gives 66.144. I takes me more an hour to debug. Finally I find it is not my fault

But google gives 63.58400

Actually google gives the correct result.

How can I trust excel anymore?

I still remember two year before Excel can generate negative number from [0,1] random variable. Luckly I get the patch to fix it. This time, I don't how I should do.

Friday, July 07, 2006

GLPK under windows

I experience a hard time to use GLPK under windows. 2 months ago, I can run the program under vc6. But yesterday, linkage errors persist to appear. Finally, I get the answer that I didn't specify glpk.lib in the project settings. But last time, I also not set it. Anyway, I get a blog for the GLPK. http://glpk.blogspot.com/. The one and the only one post is about how to set GLPK under VC 6.0 and Borland C++ 5.0.

Thursday, March 30, 2006

Replies about my post "How to speed up the code?'

I got several interesting and useful replies from various people. Some of those people have experience the same problem before. Except for improving the algorithm, they give various solution, like 1): change to the compilers which can compile your code optimally. like icc. 2): optimal cache/memory. 3) MMX. 4) calling intel performance lib...


I gather them as following

  1. Intel P4 Manual
  2. no offense, but i think these made-up test cases are waste of time
  3. He at least speed up his own code by 15% by trying different things.
    tiling/padding the for loop to improve the cache performance.
  4. Yes, support LZ.
  5. These test cases are not designed correctly. For example, looping a lot oftimes to do a simple assignment is not right. If you compile in releasemode with optimization on, it will be optimized. In VC, just compiling in release mode usually will double the speed. Your 15% is really nothing. Atleast, you need to do all the test again in release mode, which might already optimized the performance and you modification might not helpat all.Double precision calculation is slow. But float is quite fast. Look at the benchmark of the functions in intel performance primitive lib. Many float point based operations are faster than integer.Memory bandwith is another bottle neck. Trying to improve L1/L2 cache hit rate is important.I once optimized an algorithm and got it more than 10 times faster (of courseincluding algorithm chnage and MMX and calling intel performance lib).
  6. well it's good to do some experiments, but i don't think his experiment is really valuable...
  7. I don't think he has much clue either.I think we should still encourage the spirit of trying.
  8. the most part for your 10 fold increasing is likely due to algorithm change,other code optimization is not likely to improve the efficiency sodramatically.
  9. For large dataset applications,optimize toward cache/memory can even speed up 100x or more.
  10. Of course. I just want to point out that the most important thing isto optimize the algorithm and data/work flow. Then some mmx intrinsicand cache and so on. He should start from the code he had instead of testing some trivialthings (not mentioning those unrealistic testing cases). Find the most time consuming parts first and then focus on those parts.I have found out some approximated calculation of arctan is even faster than lookup table. But those optimization depends on your algorithm andthe accuracy you need. Get deep understanding of the code you are optimizing is the most important thing.
  11. The utmost rule for optimization, is to shorten your CODING time.Only in very rare case the code need special optimization tohit the speed requirement on modern CPUs, even for commercial programs.90% of codes have much more coding time than run time......If I estimate that a program can get result in one week on one CPU,I will not optimize it at all. I have to use SSE float instructionsto optimize some of my programs, because they runs more than onemonth on 7 CPUs. I give up to optimize a program running 2 weekson 6 CPUs recently, although I know I can speed it up 2x or more.The coding time, is much longer and need more attention.
  12. Faint, I wouldn't suggest SSE optimization for this. I still suggestlooking into the algorithm or optimizing it by multi-threading as youmight have done.If you need to debug, tune parameters, I am not sure how can you not runit several times. Then, several months have passed. Are you saying youwrite the code for more than 1 year? Otherwise, I think it belongs to the10% code you categerized as having more running time than coding time andshould be worth the optimization.Anyway, I prefer to write code that will be used more frequently at acceptable speed. But for different fields, it is hard to compare.
  13. Once you turn the optimization on, compilers can make usually makebetter decisions regarding how to make array access fast. Optimizationby hand is not recommended. But then again, not every compiler can optimize well enough. GCC forexample is not strong at that.
  14. nod. different compilers perform differently. For some c++ codes I recentlywrote, optimized executable by g++ runs faster than icpc optimized executable.
  15. that means you didn't turn on right flags of icpc. gcc/g++ is very hard to beat icc.
  16. Probably not. I only turn on -O2 for both g++ and icpc though.What options are usually recommended for icpc?
  17. what i usually use is -ipo -fast -fp-model -unroll0
  18. I recall that -ipo requires some extra work on the programming side, is thatcorrect?
  19. that's true for some code.
  20. If there is any possible of algorithm optimization, how could I performSSE optimization? I use 7 CPUs of cluster, if not multi-threading,I'm really mad, and maybe the most stupid person in the world.It can burn hundreds of CPUs at the same time if applicapable.The SSE speeds up around 4-5x. It's certainly worth to save the 3-4months of run time on 7-CPU cluster. And the optimization is onlyperformed on less than 500 lines of C++ code, within a project ofmore than 10k lines. And no one will run debug version on full dataset. There will alwaysbe a small dataset for debuging. And the program should write intermediatedata to disk and restart from last saved state, if you really needrun it for months.You write code of what you need, not what you prefered.You have NO choice.For more than 90% of the code, execution speed is not a issue at all,even it will run millions of times.

Wednesday, March 29, 2006

Saturday, March 25, 2006

How to speed your code in C++: int vs double & Global vs Local & Array vs two dimensional Array

Today, my main task is to speed up one of my algorithm. I am really novice in C++. I try seveal modifications.

int vs double
For example, change unnecessary double functions/parameter to int. If you really need double parameter like 1.2, you can compute based on 12. After finishing major computation, you can scale it correspondingly. It will save time especial when there is float multiplication.

global vs local variable
global variables really consume time.
For example, if you define a global variable

int c = 20;

% Then you do the loop in main()
for ( ni = 0; ni <=900000000; ni++)
x = c;

The seconds I got is 2.25, 2.4, 2.31, 2.21, 2.2, 2.27, 2.39

if you define a local variable in main()
Then the second consumed sample is 1.83, 1.83, 1.82, 1.83, 1.82, 1.82, 1.82

The gap is obvious. Another interesting thing is variance of first experiment is also significant.

One dimension array vs two dimension array
I create one dimension array and two dimension array with the same size.

int e[20000];
int d[40][500];

then I do the similar experiment 3
for ( ni = 0; ni <=900000000; ni++)
x = e[10000];
The seconds consumed is 1.83, 1.83, 1.82, 1.83, 1.82, 1.82, 1.82, 1.82

experiment 4
for ( ni = 0; ni <=900000000; ni++)
x = d[20][500];
The seconds consumed is 2.28, 2.27, 2.27, 2.32, 2.27, 2.21

It seems it is better to project two dimension array to one dimension array.

But in individual case, you need consider time of converting two dimension index to one dimension.

Finally, my code speeds up average by 15%.

Anyone can provide his experience about speeding up code?