views:

937

answers:

4

I a medical researcher with code written in MATLAB 2009b that runs very slowly because of a self-referential loop (not sure of the programming lingo here), i.e., the results of the first iteration is used during the second iteration, etc. (I have vectorized it to a fare-thee-well. I have run the Profiler.)

I'd like to convert the slow parts of the code to a mex function. I learned Fortran in the early 1970s but haven't used it since. The code I need to convert doesn't do anything fancy, it is just a long numerical calculation.

My question is: what would be the easiest-to-relearn version of Fortran adequate for this purpose, and what compiler works best on the Intel Mac for this? I found information comparing syntax in MATLAB to Fortran 90 for example, and the conversion doesn't look like it would be too daunting for me. However, again, I am no programmer.

I am using a MacBook Pro with OS 10.6.

Appreciate any help, thanks.

+7  A: 

I'd recommend using modern Fortran, at least 90/95 as the syntax is much more forgiving and almost all compilers now support it.

On a Mac I would recommend gfortran from here. It's not the most recent version, but it's well integrated with Apple build tools (you will need to install Xcode from your Mac OS DVD) and works well. In the numerical python community, which depends a lot of Fortran extensions, this build is highly recommended.

I haven't actually used fortran mex on the mac - but I think it should be fairly straightforward if you follow the mex documentation - and as you say translating code from Matlab to Fortran shouldn't be too bad (it's better if you can avoid calling Matlab functions, but fortran has sensible slicing and array access).

thrope
Downvote with no comment? I think I answered the question...
thrope
+1 to counter the downvote. I think the advice is good (pretty much the thing I was gonna say), only some people here don't like some languages, therefore the downvote (guessing).
ldigas
A: 

For heaven's sake don't do this. Get some intern who actually knows programming to take care of it for you. Fortran is basically a dead language and you simply don't know enough about software engineering to do this right. At best -- at the absolute best -- you'll turn bad Matlab code that shouldn't have been written in the first place into decent Fortran that nobody will be able to maintain. Do you speek ancient Greek in the lab? Do you keep your research notes on wax tablets? Would you want your most sophisticated lab equipment maintained by a janitor who read some manuals in the 1970's? Give this job to a programmer.

Beta
Can't agree with this at all. (modern) Fortran really is simple enough that anyone familiar with Matlab can get going with it relatively quickly - and for coding the inner loops of Matlab or Python code its really perfect. It's not like he's wanting to implement a giant application in Fortran (which I would agree is a bad idea).
thrope
Step on some Fortranists' toes, did I? I have a lot of experience dealing with {Matlab|Fortran} code written by non-programmer scientists, intelligent and well-meaning people who didn't know what they were doing. Your advice may be what Kevin wants to hear; mine will save him and his successors a lot of wasted time and effort.
Beta
Of course - ideally all code should be written by experienced programmers. But unfortunately in the real world most scientists don't have the option of hiring a freelance programmer or a suitable intern, so when it's a case of no results, or some results with messy code, usually messy code is better (the whole popularity of matlab is based on this) - following your logic no one should ever use matlab, they should just higher a programmer to write a proper application - do you see why this isn't a particularly helpful suggestion?
thrope
In any case he's talking about a loop with a single numerical calculation... Fortran will speed it up significantly over matlab, it will be clear enough, and it probably won't be more than 20 or 30 lines (including all the mex business) and a couple of hours to get working... so lighten up a bit and let the amateurs have a go too! And fortrans not dead, it just smells funny! ;)
thrope
1) As a published scientist I think I can talk about this, 2) Matlab is a fine tool in its place but should not be used for heavy calculation, 3) it's a good bet that someone in his lab knows programming better than he does so please spare me your straw man arguments, and 4) I have spent many hours cutting hundreds of lines out of "should be 20 lines" functions, and what I was thinking at the time about the authors, the set of surviving Fortran programmers, and cruise ships is probably not appropriate for this forum.
Beta
I'm torn. I mostly agree with your thesis, but this level of snark makes business users not even bother asking programmers for help in the first place. Especially on a first question. -1.
Andrew Janke
1) I'm also a published computational scientist so hopefully I can talk about this too! 2) "Matlab ... should not be used for heavy calculation". Hmm, you better get the word out to the engineering, physics, computaitonal neuroscience, finite element, financial mathematics, econometrics etc. communities where its so popular 3) Why are you casting aspersions about his programming ability? All he said is he hasn't used fortran since the 70's - he could still be a good programmer 4) Your past bad experiences with Matlab (of which I've also had many) doesn't make your answer any less unhelpful.
thrope
btw, if he said "I want to design an interactive GUI application for medical image processing" I would absolutely agree 100% with your answer... but given the details he provided in the question it doesn't seem such a bad idea for him to have a go at speeding up his loop with an extension.
thrope
@Andrew Janke, you're right, I am too easily baited.
Beta
@Beta: I didn't see any baiting. Your initial answer already had a heaping helping of snark and somewhat insulting assumptions.
gnovice
FORTRAN is not as dead as your politeness, at least.
fortran
A: 

I do appreciate everyone's thoughts. I think I will take a middle road and track down a computer science student who needs a little cash and have him/her both help me to implement the mex file and tutor me a bit in getting back into basic Fortran. (I am a clinician primarily and do not have a computer guy at my beck and call.) It will be interesting to see how things have changed in Fortran over the 32 years since I did my master's project in biomedical engineering on a PDP-11 (!)

Kevin
I don't see the need for that. Fortran, at the time, was developed for exactly people like you - engineers and the like (okey, well, clinicians were maybe not the exact target, but close enough), who needed to solve the problem. Over the past decades, it has changed significantly from fortran66/77 to 90/95, and now 2003, although ti is still completely backward compatible with 77, so your skills are not lost. Matlab on the other hand, was developed for its own purpose (see, Wiki). If it is slowing you down, my advice would be to try to rewrite it in fortran. gfortran, for example, as someone
ldigas
already noticed is a free quality compiler which will suit your needs nicely. ... And don't be discouraged by (most) people's bad alltitudes here towards fortran ... afais, most are based on prejustice and ill knowledge, and should be treated as such.
ldigas
While I agree with Idigas' comments, getting a student programmer might still be the best plan. It is a trade-off between you (presumably valuable) time and money. If you have the budget hiring the work might be reasonable.
dmckee
A: 

Well, you have probably found a solution already. However, I will say this: Matlab has been getting faster and faster. However making full use of Matlab's JIT is sometimes not intuitive. Mathworks used to say vectorize code for speed. Then they said write everything in explicit loops. I'm actually not certain what the current best practice is.

What I'm saying is, before you go to fortran, find out the best practice and implement it. That may give you enough of a speed-up right there.

Also, are you absolutely certain that you have isolated the slowdown to a loop? Have you been using the profiler? You probably have, since you sound experienced. I just thought I'd mention it.

Good luck, Ariel

abalter