Originally posted by: Born2bwire
Meh, my advisor once told me that you could write a Yee code in an afternoon and he is right. My 2D code that I wrote is around 750 lines, but most of that is just constructors, functions to read in a file for all the data, and functions to output the data. The actual FDTD portion of the code is only about 150 lines. The rest is just extra busy work I tacked on.
MATLAB is perfect for FD implementation. Doing it in C shouldn't be too bad though - just make sure you're systematic about it.Originally posted by: TecHNooB
Hm.. finally starting to understand what's going on after working on the 1D case. However, keeping track of indices makes me sad![]()
Originally posted by: CycloWizard
MATLAB is perfect for FD implementation. Doing it in C shouldn't be too bad though - just make sure you're systematic about it.Originally posted by: TecHNooB
Hm.. finally starting to understand what's going on after working on the 1D case. However, keeping track of indices makes me sad![]()
Originally posted by: eLiu
Originally posted by: CycloWizard
MATLAB is perfect for FD implementation. Doing it in C shouldn't be too bad though - just make sure you're systematic about it.Originally posted by: TecHNooB
Hm.. finally starting to understand what's going on after working on the 1D case. However, keeping track of indices makes me sad![]()
Not if speed is an issue, no. Even if you're smart about it and take advantage of the compiled code for sparse matrices (i.e. not filling the matrix one element or even one row at a time), going to C can easily gain you a factor of 4-10 in performance.
That said, doing this stuff in matlab is WAY easier on the programmer. WAY easier.
Originally posted by: eLiu
Originally posted by: CycloWizard
MATLAB is perfect for FD implementation. Doing it in C shouldn't be too bad though - just make sure you're systematic about it.Originally posted by: TecHNooB
Hm.. finally starting to understand what's going on after working on the 1D case. However, keeping track of indices makes me sad![]()
Not if speed is an issue, no. Even if you're smart about it and take advantage of the compiled code for sparse matrices (i.e. not filling the matrix one element or even one row at a time), going to C can easily gain you a factor of 4-10 in performance.
That said, doing this stuff in matlab is WAY easier on the programmer. WAY easier.
Originally posted by: Born2bwire
Originally posted by: eLiu
Originally posted by: CycloWizard
MATLAB is perfect for FD implementation. Doing it in C shouldn't be too bad though - just make sure you're systematic about it.Originally posted by: TecHNooB
Hm.. finally starting to understand what's going on after working on the 1D case. However, keeping track of indices makes me sad![]()
Not if speed is an issue, no. Even if you're smart about it and take advantage of the compiled code for sparse matrices (i.e. not filling the matrix one element or even one row at a time), going to C can easily gain you a factor of 4-10 in performance.
That said, doing this stuff in matlab is WAY easier on the programmer. WAY easier.
I have to agree. FDTD is very computationally intensive and there are few optimizations that you can do to speed it up. Since the program is very simple in the first place, it is usually better to write it in C, C++ or Fortran for speed. Matlab was much nicer for quick and dirty FEM and MOM codes. Matlab could generate a 2D mesh for my FEM problem, has a sparse matrix class to use, and it also will do the matrix solving. You can do this in C/++ and Fortran using the BLAS and LAPACK libraries but it is so much easier in Matlab.
Originally posted by: eLiu
Originally posted by: Born2bwire
Originally posted by: eLiu
Originally posted by: CycloWizard
MATLAB is perfect for FD implementation. Doing it in C shouldn't be too bad though - just make sure you're systematic about it.Originally posted by: TecHNooB
Hm.. finally starting to understand what's going on after working on the 1D case. However, keeping track of indices makes me sad![]()
Not if speed is an issue, no. Even if you're smart about it and take advantage of the compiled code for sparse matrices (i.e. not filling the matrix one element or even one row at a time), going to C can easily gain you a factor of 4-10 in performance.
That said, doing this stuff in matlab is WAY easier on the programmer. WAY easier.
I have to agree. FDTD is very computationally intensive and there are few optimizations that you can do to speed it up. Since the program is very simple in the first place, it is usually better to write it in C, C++ or Fortran for speed. Matlab was much nicer for quick and dirty FEM and MOM codes. Matlab could generate a 2D mesh for my FEM problem, has a sparse matrix class to use, and it also will do the matrix solving. You can do this in C/++ and Fortran using the BLAS and LAPACK libraries but it is so much easier in Matlab.
Well, matlab can produce some nice, short FD code too:
spectral method for 1D Poisson's equation with period BCs, rhs() and n given:
h = 2*pi/n;
x = h*(1:n)';
fhat = fft( rhs(x) );
freq = [0:n/2, -n/2+1:-1]';
uhat = fhat./(freq.^2);
uhat(1) = 0;
u = real(ifft(uhat));
Or 1D convection-diffusion, homogenous dirichlet, 2nd order FD with rhs(), n, and diffusion coef mu given:
h = 2/(n+1);
x = (-1+h:h:1-h)';
K1D = spdiags(ones(n,1)*[-1*mu-h/2, 2*mu, -1*mu+h/2],-1:1,n,n)/h^2;
u = K1D\rhs(x);
Heh, anyway I should add that you'd get a 4-10x speed up over very well-written (read: vectorized to death) matlab. Over un-vectorized matlab, the speedup can be factors of 100 or even 1000.
The other big disadvantage of matlab is problem size. It just can't handle large matrices; clearly you can get farther by taking advantage of sparse() (if your problem is in fact sparse), but there's a limit... and for big problems, that limit is too low. Oh and there's "no" parallel matlab (there is star-p, but it isn't perfect and it isn't full-fledged).
Though you can greatly extend matlab's abilities with the MEX interface. MEX allows matlab to call compiled C code. I took a class in DG finite elements and while my matlab code was by far the fastest matlab, one student MEX-ed some parts of his code and was about 10x faster than I.
OP: if you ever get into doing scientific computing for a living... well, replicating that code in C will be life! Enjoy!![]()
I've never seen a benchmark where ML is more than 2-3x slower than C code for any application, so I would really appreciate if you have any that indicate a greater slowdown. Now that I have a little more C under my belt, it might be worth my while to convert some of my more computationally-intensive programs if there's really that big of a difference for the type of work I'm doing.Originally posted by: eLiu
Not if speed is an issue, no. Even if you're smart about it and take advantage of the compiled code for sparse matrices (i.e. not filling the matrix one element or even one row at a time), going to C can easily gain you a factor of 4-10 in performance.
That said, doing this stuff in matlab is WAY easier on the programmer. WAY easier.
Originally posted by: CycloWizard
I've never seen a benchmark where ML is more than 2-3x slower than C code for any application, so I would really appreciate if you have any that indicate a greater slowdown. Now that I have a little more C under my belt, it might be worth my while to convert some of my more computationally-intensive programs if there's really that big of a difference for the type of work I'm doing.Originally posted by: eLiu
Not if speed is an issue, no. Even if you're smart about it and take advantage of the compiled code for sparse matrices (i.e. not filling the matrix one element or even one row at a time), going to C can easily gain you a factor of 4-10 in performance.
That said, doing this stuff in matlab is WAY easier on the programmer. WAY easier.
Thanks for the reply. I'm not saying ML is good for all of these large problems - I've encountered my share of problems where it's very slow. I'm just trying to get a feel for the cases where it might be worth my while to do the programming in C rather than ML, given that it will take me approximately 10x as long (if not longer) to code in C.Originally posted by: eLiu
...
Originally posted by: Biftheunderstudy
Well if you remember my problem, it is basically a FD method. If Matlab/Maple really is a 5 to 10X more time I shudder to think the effect this would have on my code. Albeit poorly optimized it takes roughly 5 hours to do a run on a given set of parameters. Its also vaguely E&Mish, complex numbers, fun integrals etc.
The problem that comes to mind is, ironically enough, a finite difference problem: four coupled PDEs for reaction-diffusion over long time scales. The goal was to determine the best-fit reaction-diffusion model parameters (I think there were nine?), so I had to solve the whole thing many, many times. Each time took a few hours I think, so I would just let it run for days on my home computer. The code was pretty simple and (I think) fully vectorized, but it was just a lot of computation. There was probably a more efficient method for solving the PDE's (like FE'sOriginally posted by: eLiu
CycloWizard: Hmm well it's hard to make generalizing statements about when C will beat matlab significantly. What kind of problems do you work on? For virtually everything associated with solving PDEs, C will win. So in my case, unless I'm doing a one-off code or I'm just solving some small problems to demonstrate that an idea works, I'll ultimately go to C. (Since I often fail at C, my first step usually involves doing something in matlab so I can compare results. lol)
The "MATLAB Compiler":Also, what compiler are you talking about? You mean like EML? Or the (related) thing attached to simulink?
Doesn't seem to make a big difference in computation time, but that could be because I don't know anything about optimizing compilers so I just use default options for everything.mcc is the MATLAB command that invokes the MATLAB Compiler. You can issue the mcc command either from the MATLAB command prompt (MATLAB mode) or the DOS or UNIX command line (stand-alone mode)
Originally posted by: CycloWizard
The problem that comes to mind is, ironically enough, a finite difference problem: four coupled PDEs for reaction-diffusion over long time scales. The goal was to determine the best-fit reaction-diffusion model parameters (I think there were nine?), so I had to solve the whole thing many, many times. Each time took a few hours I think, so I would just let it run for days on my home computer. The code was pretty simple and (I think) fully vectorized, but it was just a lot of computation. There was probably a more efficient method for solving the PDE's (like FE'sOriginally posted by: eLiu
CycloWizard: Hmm well it's hard to make generalizing statements about when C will beat matlab significantly. What kind of problems do you work on? For virtually everything associated with solving PDEs, C will win. So in my case, unless I'm doing a one-off code or I'm just solving some small problems to demonstrate that an idea works, I'll ultimately go to C. (Since I often fail at C, my first step usually involves doing something in matlab so I can compare results. lol)), which is probably where I could make the biggest difference, but when I was setting it up I didn't know much about other PDE methods, let alone coding anything by ML.
The "MATLAB Compiler":Also, what compiler are you talking about? You mean like EML? Or the (related) thing attached to simulink?
Doesn't seem to make a big difference in computation time, but that could be because I don't know anything about optimizing compilers so I just use default options for everything.mcc is the MATLAB command that invokes the MATLAB Compiler. You can issue the mcc command either from the MATLAB command prompt (MATLAB mode) or the DOS or UNIX command line (stand-alone mode)
Pretty simple domain - just 1-d with one Neumann and one Dirichlet BC. 24*60*60*10 time steps (i.e. 0.1 s stepping for 24 hours) with only about 20 spatial nodes. The source term for this set of reactions was probably pretty expensive (A^a+b^b+C^c-D^d or something like that).Originally posted by: eLiu
Long time scales = how many iterations and what time stepping method? Can you estimate roughly how long each iteration takes and/or tell me what the size of the problem is (dimension of your matrix). Is the source term for reactions expensive to evaluate? Is your domain simple or do you employ mapping, embedded boundaries, or any other fancy dances?
Yeah, that makes sense. I've only recently started compiling and writing GUIs for ML because I've started collaborating with some biologists. I'm used to just running from a script file, but I found out I could install MCR on their computers and give them something pretty to look at, which has made them pretty happy. From what I can tell, the CTF file is basically a zip file containing the auxiliary install files that are called by the executable. Since everything I've compiled is basically image processing, I give them a picture on the GUI to show what is going on (they're not too worried about performance I don't think).Also, mcc is not quite what you think it is. Unless i'm wrong, mcc enables the running of m-code outside of matlab. It does this by making aspects of the matlab environment available outside of matlab through standalone libraries (i think this is called the MCR? I forget). I also remember some stuff happening with CTF files (some kind of encrypted thing) but I dunno wth was going on there. My understanding is that you probably shouldn't expect too big of a performance increase b/c the matlab core is still basically running in the background. Like when you start up an application created using mcc, I would expect that it'd take as long as starting matlab. Also I'd expect the memory usage to be comparable to running matlab (you can often do a lot better w/memory usage than matlab does.)
That said, I'm not super familiar with mcc, so I apologize if anything I've said is wrong. I tried using experimenting with it briefly to compare the performance of the code it generated with the C code that I wrote. Something didn't work so I moved on to more productive things, lol. Maybe I should revisit that. Anyway the only bit of advice I can offer is to run mcc with the -nojvm option. You won't be able to plot or do anything graphical, but that might speed the code up.
Originally posted by: CycloWizard
Pretty simple domain - just 1-d with one Neumann and one Dirichlet BC. 24*60*60*10 time steps (i.e. 0.1 s stepping for 24 hours) with only about 20 spatial nodes. The source term for this set of reactions was probably pretty expensive (A^a+b^b+C^c-D^d or something like that).Originally posted by: eLiu
snip
Yeah, that makes sense. I've only recently started compiling and writing GUIs for ML because I've started collaborating with some biologists. I'm used to just running from a script file, but I found out I could install MCR on their computers and give them something pretty to look at, which has made them pretty happy. From what I can tell, the CTF file is basically a zip file containing the auxiliary install files that are called by the executable. Since everything I've compiled is basically image processing, I give them a picture on the GUI to show what is going on (they're not too worried about performance I don't think).snip.
Yes, very tiny, but a lot of different time scales at work - fast, reversible reactions and slow to fast diffusion. I wrote explicit code because I didn't have much time to work on it (was originally a simple problem for a course project) and just let it run. And run. And run. I really had to shrink the time step a lot to get it to match up with the exact solution of the simplified case. If I ever get time to work on that particular problem again, I probably will go with C just for practice if nothing else.Originally posted by: eLiu
20 spatial nodes? That's a tiny problem, lol. Is this implicit or explicit stepping? In either case, with nearly 1 million time iterations, I'd bet that you'll see a 4x speedup with C, maybe more. But if it's implicit and you solve a 20x20 system each time, you'll want to call LAPACK's tridiagonal or banded solver depending on what stencil you use. Take care that you only call pow() for fractional powers (except 1/2, then just use sqrt). For integer powers, compute by multiplication.
I've found that one afternoon coding for a biologist will get me a paper, whereas solving a new engineering problem will take months. It's always fun to be able to solve someone else's huge problem with a few lines of code, saving them months of tracing things in ImageJ.Oh ok. I thought you were trying to use mcc to make your matlab code go faster; my bad. haha... generating pretty pictures is half the point of doing computationI guess I spend too much time with engineering types; I think everyone I know has a copy of matlab (maybe not legally but still).