Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Generating Fortran code within Visual Studio 2019

New Contributor III

What Fortran code generation tools work well with Visual Studio 2019?

We develop an technical application with a C# front end and a Fortran DLL for number crunching within Visual Studio (2013 but 2019 real soon now).  One part of the code we want to improve has a lot of boiler-plate code - essentially a map/reduce over a hierarchy of derived types, but with a small number of different reduce operations (max, min, sum, weighted average, etc) for the components.  Much of the code could be generated automatically from templates.

Now I come from a unix background.  In the past I would have used make and awk, cpp, m4 or python to do this.  I am now working in a Visual Studio environment with C# programmers and they have expressed a STRONG preference for a solution within Visual Studio.

The Intel Fortran pre-proccessor may do the job.  Are there any alternatives?  

0 Kudos
2 Replies
Valued Contributor III

if you do a search on C# in this forum  you will find a note on connecting C# to Fortran -- it is not nice and it is not easy takes several hours to do it. And the results are less than brilliant, I code in both languages, not because I want to -- I don't but the main sensor we use has only C# drivers and we cannot communicate with Fortran - but if you are doing anything computational then Fortran is better -- at least twice as fast. 

Good luck -- sorry I could not find the old program where I got it running - you could try doing it as a check program - run the Fortran program in the background and let the C# flag when it wants something done, this will be better as you should run in different threads -- which is always faster, I would just use a file to transfer the data, even a lot of data transferred through a file if you multithread will be faster than any single thread for most applications. 

Good luck.. 


0 Kudos
Honored Contributor III

Look at the C# to C/C++ interfacing information. C# uses managed code with structured exception handling. Whereas Fortran uses unmanaged code without structured exception handling. So your C# has to call unmanaged code, either directly to Fortran or to a shell function written in C/C++ which then calls the unmanaged Fortran code. The argument passing must be compatible. IOW no C# container (e.g. string, C# array descriptor, ...).

Additionally, be aware that the typical C# multi-threaded programming paradigm is:

    Spawn new thread, do work, join/terminate thread

resulting in each "do work" having separate thread ID (which may get reused after some long period).

This will not be a problem calling Fortran (or C/C++) *** provided that the Fortran (or C/C++) is serial (thread-safe) code.
If (when) your Fortran (or C/C++) is OpenMP parallel code, this will be problematic due to each C# thread (with different thread ID) calling the Fortran (or C/C++) OpenMP code will instantiate a new OpenMP thread pool. This will look like a memory leak at least, oversubscription as an annoyance, and out of memory/resources eventually at worst.

The work around (when the Fortran/C/C++ portion uses OpenMP) is to assure that the same C# thread (ID) makes all the calls to said code.

Jim Dempsey

0 Kudos