Overall, I still think the best approach for now would be to use a separate program, and some sort of IPC solution. I’ve started prototyping something like this:
The fortran “library” is a standalone program with it’s own main function. The main function itself is an infinite loop that does the following (on image 1 only)
- Reads a single byte from stdin
- Looks that value up in a table that determines what procedure to call
- Calls the indicated procedure
- Writes a byte to stdout to indicate that execution is complete.
All actual DATA communication would be done via shared memory between the calling program and the Fortran program. On POSIX, both programs would open named shared memory segments to pass arguments and return values. If we assume that only image 1 is the recipient of the shared memory, and then use co_broadcast to get the values out to other images, we could even support off-node parallelization this way…
The best way to expose this functionality to a program that wants to consume it would be to have a “wrapper” library, probably written in C since basically everything can call C. That wrapper library would have an init/deinit function, plus the actual library routines of interest - exposed as normal functions. The library init function would use something like
popen in order to
mpiexec the fortran program and hold pipes to the process’s stdin and stdout. Then, the individual function wrappers would do whatever they need to do in order to get data to and from the shared memory segments and write the appropriate bytes to the stdin/stdout pipes.
This sounds like an abomination of complexity, but:
- To actually use the fortran library in a client application, a programmer just needs to make normal C function calls, and,
- The wrapper functions could be automatically generated by a suitable tool, making it pretty easy on the Fortran programmer too.
Perhaps LFortran would be a good place to implement that sort of automatic client wrapper generation - I think the ASR has all the necessary information to do so.
Of course this is just a high level overview - there would be significant details to work out re: error reporting, what if the remote program crashes, etc… I’ll post some code in a couple of days that will hopefully clarify this if the above is confusing.