Fortran stdlib: sufficient for scientific programming?

I am very new to Fortran stdlib; however, I have been using C++ for my scientific programming since 2018. For fast analysis and a low program footprint, I wanted to switch to Fortran. As I am observing, LA algorithms are present with stdlib. Is the Fortran-stdlib sufficient for scientific programming, and can it replace LAPACK/BLAS?

Are there any detailed documents available for stdlib that have a stdlib - python/Matlab conversation table for the new Fortran programmer?

7 Likes

Welcome to the Forum @Bhanu! Absolutely yes - the Fortran Standard Library now implements both full BLAS/LAPACK libraries, as well as high level functions that are similar to NumPy/SciPy/Matlab.

To get started, you can:

  • Look at the detailed examples

  • Read the linear algebra documentation

  • Ask here on the Discourse for help

  • The Compiler Explorer comes with the pre-built Fortran standard library, so you can play with it there e.g. by copying and pasting the examples - I am providing one in the link. I find this a very easy way to get started.

Note that we should be releasing several more algebra functions in an upcoming release, so stay tuned to not miss the updates -

11 Likes

I followed the documentation link and happened to look at eye() identity matrix function. The returned matrix is of int8 type to save storage but I wonder what happens if an implementation lacks int8 type. AFAIK the Standard does not require it to be present.

Also, do I understand right that the content of the arrays is shown (in comments) row-wise? It uses sort of array-constructor syntax, so it may be confusing.

1 Like

Thank you, @FedericoPerini, for the comment. It is fascinating to learn Fortran at the moment (after learning several others). Interestingly, I am looking for any conversion table (such as one given in Armadillo: C++ library for linear algebra & scientific computing - API Documentation for the Matlab/octave to c++ equivalent for the armadillo library) that exponentially increases the learning curve with less frustration (in past, that was the reason I avoided the Fortran and lapack and switched to eigen/armadillo library for C++). I am always looking forward to learning this package. (BTW, I also understood it’s very easy to interface Gnuplot to Fortran, and I implemented it within 15 min. of starting). Again, thanks for your encouragement.

2 Likes

In that case it will fail at compilation time because the kind specifier int8 will be set to a negative value.

Yes. It’s how MATLAB or Python programmers tend to view array construction. In Fortran you’d need something like:

real :: A(3,3)
A = reshape([[1,0,0],[0,1,0],[0,0,1]],shape=[3,3],order=[2,1])

Obviously, since the identity matrix is symmetric, the order argument is unnecessary here.

1 Like

The identity matrix is indeed symmetric, but the output of eye(2,3) is not, so the question is not completely nonsense.

2 Likes

Are we diverted, or is it meant for a different post? (lost the relevance to the question, Sorry)

1 Like

@FedericoPerini While exploring the web, I came across one webpage (Python Fortran Rosetta Stone — Fortran Programming Language). This page highlights the conversion table between Python and Fortran. Similarly, if any one of the users knows any document/webpage which suggests “Learn Fortran for Python/Matlab users”, it would be great. I am looking for more constructive and positive suggestions.

2 Likes

Ok, this might be a long shot since this document is literally 23 years old, but it covers some of the basics of OOP in Matlab, Fortran and C++ side by side.

Object Oriented Programming via Fortran 90/95, Ed Akin, 2001

If you look at Chapter 4, “Features of Programming Languages”, it provides tables with the instructions in all three languages (plus FORTRAN 77). I remember using this document to learn Fortran 90/95 and it was sufficient for me to build an (openMP) Finite Element Solver for my scientific programming classes back then at university

1 Like

Thank you @kimala. It is really helpful information. It partially solved my purpose. In addition to that, there is one more web page (https://wwwold.mathematik.tu-dortmund.de/~featflow/en/software/featflow2/tutorial/tutorial_lang.html) I explored that will be helpful to others.

1 Like

Lol that was one of few old functions that was not covered by the current effort - I will open an issue to improve it. I think a good way to extend it would be to have an interface:

A = eye(m,n [,mold]) 

Where mold is used to give a return type to the function. I think It makes sense that the default version returns int8

2 Likes

Thank you all for providing this useful tool!
Is there a guide for dummies on how to install the library on a Windows computer? I tried some time ago (1 year ago) but I failed. As a Fortran compiler I use ifx and ifort

1 Like

@aledinola I build it routinely with CMake and have no issues with it. Another option is via the pre-built stdlib-fpm branch that can be built (including on Windows) with fpm.

2 Likes

@aledinola Yes, I agree with you that it’s not an easy task to use stdlib in the Windows operating system. I also struggled to install stdlib. Following is the best way (using now in 3 different computer and installed in these 2-3 days) to use stdlib in a Windows environment:

  1. install recent gcc (and gfortran) (mine is gcc 13; don’t use gcc <10) and include path in windows environment (so once you type gfortran in command line, it will show you the error regarding the input file)
  2. Install git on your computer and ensure it works through the command line. (you also need an active internet connection)
  3. download fpm and place it in any folder (say my_fpm). Start the command line and point to the my_fpm folder. Now enter the following command: fpm.exe new my_code (this will generate few files and folder required) (and don’t close command line at this moment)
  4. open fpm.toml file using Notepad and add the following lines at the end of the file:

[dependencies]
stdlib = { git=“GitHub - fortran-lang/stdlib: Fortran Standard Library”, branch=“stdlib-fpm” }

  1. Now go to “app” folder and edit main.f90 file (Fortran code that is using stdlib).
  2. again, go back to the command line (where you executed step 3 successfully!!) and type “fpm.exe build” it will create a build folder and download files using git (esp. stdlib). Wait for a while and bingo!! It will be completed in 2-3 min (up to this point you need live internet connection).
  3. now type “fpm.exe run”; this will execute the exe file and show you the results. The exe file will be in the build folder within any of the folders (better search, I am exploring)
  4. Now if you need to compile again, you either need an active internet connection or edit (so that no need of internet connection)

[dependencies]
stdlib = { git=“GitHub - fortran-lang/stdlib: Fortran Standard Library”, branch=“stdlib-fpm” }

to

[dependencies]
stdlib = “*”

I hope this will help. Let me know if you need further help. (@FedericoPerini @ivanpribec feel free to share your experience if missed something or I can make it more efficient)
Best and welcome to the fortran world!!

2 Likes

Thanks a lot! Glad to hear that it’s not so easy, last time I tried I had to give up

1 Like

Still, I believe that it is not as simple as I think. For example, if I wish to compile lapack, I just copy the related folder to the include folder and lib. in the library folder and compile the code. But currently, it’s working for me. @FedericoPerini if you can help if stdlib can work with g77 (g77 download for windows - Browse Files at SourceForge.net)?

1 Like

@Bhanu g77 is a superseded compiler - however, there are options on Windows, let me list some:

  • gfortran via MSYS2
  • gfortran, lFortran via conda (not an expert myself, but I believe this option currently gives the most cross-platform experience)
  • ifx via oneAPI
  • gfortran with the Equation.com installer
  • The quickstart-fortran installer

However, these questions are not really stdlib related, but more about how to setup an environment for working with Fortran on Windows. For that, can I suggest reading some of the excellent tutorials on the fortran-lang page? E.g.,

The reason package managers such as conda or msys2 (for windows) are so successful is that you can easily install many of the usual developer tools such as CMake or git without having to install each of them separately.

3 Likes

@FedericoPerini Thank you for your reply. I wish to explore this for one reason: I have to carry my laptop everywhere. However, if stdlib is simple to use, I can install everything on my pendrive (as a portable installation, as I did for the C/C++ compiler) so that it is within my university or home or in my lab, I can use it. I am looking for comments from experts on how to deal with such issue (especially those who use different computers/PCs every time in public places). If someone can comment, can I use libstdlib.a as a static library (and is this sufficient, or is there any way I can create a static library including all the modules) directly if I can use this in my pendrive? Thank you.

1 Like

About setting up a Fortran environment in Windows: I use ifort and ifx downloaded and installed via Intel Oneapi. The installation is very easy and comes with Microsoft Visual Studio. However, I prefer to use Visual Studio Code as editor. Given this Fortran environment, can I install stlib easily? Is there a user guide? I believe that the combination of Windows and intel oneapi is not uncommon among Fortran users. It would be really helpful if the Fortran standard library worked with this environemnt :slight_smile:

P.S. When I write “work” I also mean that it must be easy to install

2 Likes

Exactly! This is the way to use it. Once you have it built as libfortran_stdlib.a for example, you can just carry it over (together with the module and submodule files, .mod and .smod) and don’t worry about compiling it again.

One very easy way to do this is via fpm install as that will put all necessary file in your installation folder (for example, on your pendrive). then you just compile with -I/path/to/stdlib and -lfortran_stdlib.

Let’s not forget this is a Standard Library: long-lasting code with a stable API, not something that’s supposed to change / have to be compiled every day

2 Likes