I'm working on a project where I need heavy numerical calculations to be real-time visualized with something flexible like D3.js. Are there frameworks out there that would let me painlessly achieve this? Or do I need to use some sort of "glue" code written in, say, Python?
JavaScript – How to Combine C/Fortran with JavaScript?
cfortraninteroperabilityjavascript
Related Solutions
The demands you have put actually put Fortran at the top of the list, for problems like this:
a) number crunching
b) parallelable
c) it was and still is the de facto language taught outside of CS studies (to engineers who aren't professional programmers).
d) has an incredible(!) industry backing, number-of-industry-grade-compilers-wise, with none of the vendors showing the least signs of abandoning that branch. One of Intel's representatives not far ago revealed that sales of their Fortran products are higher than any other in their development tools.
It is also a language which is incredibly easy to pick up. I don't agree that it takes time for bringing research assistants up to speed. My first textbook on it had no more than, oh I don't know, 30 (?) pages of sparse printed text. It is a language in which after learning 10 keywords, one can write medium-sized programs. I would dare say that those 30 pages written in default Word text would make a more than comprehensive "Fortran manual" for most users.
If you're interested in CUDA, you might want to check Portland Group's compiler, which supports it. I'm not familiar with the finer details, but people generally talk of it with praise.
Apart from that, for paralleling programs you have available OpenMP, MPI and now the upcoming (and long awaited) co-arrays, which Intel's compiler has recently implemented. To not waste words, Fortran has a very fine gamma of "libraries" for parallelizing programs.
Industry standard numerical libraries are developed for it foremost, other languages following more or less in the function/routines portfolio.
All that being said, I would however (depends on when it was originally written) recommend if it is, let's say, F77 code or older, rewriting it partially through time to newer dialects - F90 at least, if possible with F2003 features. A paper / thesis on that topic was recently published (medium-sized PDF file ahead). Not only can that, if done properly, ensure portability across multiple platforms, but will also make it more easy for future maintenance.
p.s. As far as "future maintenance" goes, just an anecdote which I sometimes like to mention. While writing my thesis, I reused some code from my mentor, written 35 years ago from the time of writing. It compiled with only one error; a statement missing at the end, due to copy-paste mistake :)
@DaveMateer (reply to comment) - I'm going to make a comment in the following which may be a bit impolite, but please don't take it the wrong way, for it is in the fair intentions.
It seems to me you're tackling this "problem" in the wrong way. What I mean in a few short points (for it is very late in here, and my ability to make up readable (let alone comprehensible) sentences leaves me after 10p.m.)
a) You mentioned you're trying to minimize extra coding time, yet you're considering a rewrite from a language specialized for numerical computing to one from a colorful choice of languages, if you'll pardon my expression
- some of which don't have support for multidimensional arrays, amongst other things
- most of them are unsuitable for heavy numerical work (of parallel processing capabilities of Haskell and Hadoop I admit, I know nothing about ... but have never heard them even mentioned in those circles)
- it possibly has been tried, but I've never heard of a rewrite from Fortran, a language for discretized problems, to a functional language
- there has been a discussion recently on comp.lang.fortran (try searching
through Google Groups) on the aspects of scientific computing "in the cloud"
(wouldn't like to demotivate you, but to be fair, no one was really sure what that term even represents, let alone had an example of a successful application. Most people agreed that potential exists, but so far they're happy the way things work for now.). A lot of problems are not suitable for that kind of parallelisation either.
b) What would be the costs of such a rewrite? People/hours.
c) Correct versions of the libraries to compile...- is a problem in any language that cannot be avoided, however you look at it.
d) I've heard of Python (a nice language really) used in parallel applications on a few occasions, but its penetration of that market still doesn't seem to be rising, and its ever changing nature makes it a very poor choice for a long term project (think backward compatibility). Some people like it very much as a "glue" language.
Ugh, if I think of anything else, will add it tomorrow. Gotta get some sleep...
I do not believe that you should focus your optimizations looking for magic keywords, but here goes some tips:
"inline" might do wonders in numerical apps if you have many short numerical functions
Be careful when copying values all around when you could use a reference. This sounds basic but if you have many classes that are copy/assignment compatible your code could be hiding some copies. Make sure your constructors are defined with the "explicit" keyword, that way your compiler will show you where implicit copies are done.
Give C++11 a try. There are some interesting optimizations. There are more things that are solved statically. Also one very interesting thing for your case is the rvalue reference optimization. It is a very important optimization on the return value of functions. You will notice that a lot if you overload operators, which you will do if you operate different units and try to structure your old code.
There are lots of things that can be done with STL and really interesting template metaprograming, but that is a whole different level to what most people is used to.
Do not rely too much on your compiler optimizations. C++ syntax is well known to make life easier for low-level programmers and a nightmare for compiler designers. That is one of the reasons for which good static analyzers took so long to appear. I know that Scott Mayers made that comment and how C++11 improvements will help to get better tools, but cannot quote where he did.
On the line with compilers... do not expect to get SIMD and overall paralel optimizations. Compilers are really bad at it and unless you get your hands dirty with some library for it, your code will target only one core, one thread of your machine.
It might sound silly but the keyword "const" is really important. If you make clear that some value does not change, the compiler will apply a whole extra set of optimizations. Detecting if a variable could be considered const takes some extra parsing that most compilers do not perform not even with optimizations maxed out. Related with this for example, if your loops do not seem to have a clear/constant end, the compiler will not unroll them (or maybe not as good as it could)
However I wonder why you are migrating from Fortran77 to C++. Fortran sure is an old language, but it is the king for mathematics no matter how ugly it looks, the best way to deal with old code is building a good layer and operate it from outside with the new infrastructure and language you want. It is easier said than done, because sure you will have still to fix some Fortran code in order to make it easier to operate from outside.
If you decide to go on with C++ at least you will get much nicer libraries for multithreading (and that is one of the biggest improvements of C++11) where Fortran lags behind. Try to avoid CUDA if not absolutely necessary, it is really good and mature but focused on NVIDIA. I would keep an eye on OpenCL
Be aware that there is one huge difference for Math. In C++ (like most languages), the values of a matrix are stored as a set of rows while Fotran stores them as a set of columns. This makes a huge difference on the best memory usage of your algorithms. Hence if your algorithms are optimized for Fortran, their loops will target columns instead of rows. (look here) That is how seriously Fortran optimized for Mathematics goes.
One final concern. You want to use C++ and not Fotran77. Because you can get a better abstraction and design? Well, that is a good reason, but please, be wary of the market obsession with object oriented programming. It is a good thing, but not a silver bullet. A GUI for example should always be object oriented, but if you do lots of math and move huge amounts of memory, some aspect oriented style would be nice. Even better for your domain problem, have a look at data oriented design as an alternative to object oriented design (and see one case where OOP fails)
Best Answer
What you're asking for is interoperation between C/Fortran and Javascript, interopation can be done in a great many ways, the most common two that come to mind however are:
Foreign Function Interface (FFI) bindings and Inter-Process Communication (IPC).
FFI is where a standard agreed upon protocol is built into the assembly at compile time which allows access to the assemblies facilities by foreign languages. There are a variety of FFI standard interfaces, but I am familiar with none that allows an interpreted language to interface with a compiled language or vice versa; the concept doesn't fit with using interpreters as their scripts are plain text with no assemblies to interface.
That said, the interpreter may have facilities for FFI access. Node.JS comes to mind as a javascript interpreter that likely has facilities for FFI access. Alternatively you could use your C program to host a javascript interpreter like Chrome's V8 engine to have your C execute the javascript directly if this would help you, though I don't suspect it would.
Inter-Process Communication (IPC) is where you run the process in one language, and the other process in the other language, and then use a form of IPC to communicate between the two. Commonly this is done via shared memory space where both processes can put data into that memory space and read from it to send messages back and forth. The same technique is used with files sometimes though this is a high-overhead technique due to the IO costs, it does however afford a durability in the event of either or both processes crashing, or even the machine losing power.
The shared memory technique for IPC often comes in a couple flavors:
Other than shared memory or disk, the other largely used IPC technique is network communication. Whenever your browser accesses a website it is communicating with another process, so this is IPC, and in this way web services have become a hugely common way of accomplishing IPC. Direct TCP techniques are still used as well as even UDP for certain tasks, but the point is the network may act as a communication layer between processes just as a memory space or disk space may.
All of this said, my suggestion is you use web services for IPC as your language interoperation technique, because you wish for visualization on a browser.
That's all. I hope you learned something, carry on :)