Does `sudo apt install gfortran mpich` support Apple M1 chip?

Just a quick question, what is the most easy way to use gfortran + mpi for M1 chip Mac?

I found that at least on Linux, the most easy for Fortran and mpi work seems to be just this simple command, Fortran with MPI : 001 : Introduction and Installation - YouTube

sudo apt install gfortran mpich

I was able to compile and build mpi code with the mpif90 provided from the command.
But I wonder, on Mac M1 chip, does things like sudo apt install gfortran mpich work?
If so, does that gfortran and mpi run properly on M1 chip?

Thanks much in advance!

Hi @CRquantum

I can answer a few of your questions:

what is the most easy way to use gfortran + mpi for M1 chip Mac?

A lot of Apple computer users (both Apple M1 chip and Intel x86 chip) use Homebrew or MacPorts to install and manage packages from the command line on their macOS computers.

I have tried both, and currently use Homebrew. Once setup, you can install gfortran with the command: brew install gfortran. I have not used mpich but a quick search with brew info mpich does show it exists at version: stable 4.0.1.

More details here: Homebrew ‘mpich’

The command shown in you message using apt is to install the packages under Linux, where apt is package manager (ie similar to Homebrew) - but used on Linux distributions such as Debian and Ubuntu, plus others. The equivalent command using Homebrew would be:

brew install gfortran mpich

NB: the use of sudo is not required for Homebrew.

does that gfortran and mpi run properly on M1 chip?

I use gfortran on my M1 based Apple computers and it works well. The currently installed version via Homebrew is:

GNU Fortran (Homebrew GCC 11.2.0_3) 11.2.0

There are other approaches available as well - but the above is probably the most common for macOS computers.

Hopefully someone else can help with your question about mpi running correctly :slight_smile:

1 Like

Thank you so much @wiremoons !

I can confirm that the mpi works on M1 chip.

It is just seems that my code when using -O2, -O3, -Ofast the M1 chip compile file. But when it run is give a segment 11 error. (it has nothing to do with mpi)

But it runs fine on the same version 11.2.0 gfortran on ubuntu and windows.
So I am not very sure if it is my code’s issue or some compatibility issue with gfortran and M1.

1 Like

Hi @CRquantum

It is quite common to compile with optimisation flags such as -O3 -march=native to optimise final code, and it has worked fine here so far on programs built on my M1 machines.

A segmentation 11 error sounds like some out of bounds type bug possibly. Have you you compiled it on the M1 using some more debug friendly type flags such as:

gfortran -fcheck=all -Og -g -fbacktrace -Wall -Wextra -pedantic 

The -fbacktrace option may provide more detailed output when the error occurs, to help track down what the issue might be…

1 Like

Thank you very much for your suggestion and reply @wiremoons ! Awesome!

I do not have M1 at hand, but I remember when I use

-O0 -fcheck=all -fbacktrace

the M1 did give segment 11 error. However there is no traceback information.

I suspect there may be some array out of bounds or something. Yeah, I tried using gfortran on Ubuntu with you flag without -pedantic, because I use the ODEPACK which needs -std=legacy, and with -pedantic it seems will turn some warnings in ODEPACK to error and then I cannot finish the compile. So anyway, I use

gfortran -fcheck=all -Og -g -fbacktrace -Wall -Wextra

It runs and I did not see error or warning.

I also tried some additional flags on my Ubuntu machine,

-Warray-temporaries -ffree-line-length-0 -ffpe-trap=invalid,zero,overflow,underflow, -finit-real=nan  

It did catch some some underflow issue, I have some tiny(1.0)**2 operations stuff which is unnecessary I think, should just use 0.0 instead of ```tiny(1.0)’’’, I may begin with these places, to see if there are underflow, overflow, nan etc.

Since I use some other people’s Fortran ODE solver, besides ODEPACK, also use Hairer’s and FLINT.
Then I will try to turn off these ODE solvers on M1 and do further check to locate where the problem may be.

1 Like

Be aware that Mac OS X has a rather small stack size and exceeding the stack limit will generate a segmentation fault.


@CRquantum You might be interested in a project called " Asahi-Linux" where there is a group porting the Linux kernel to the Apple M1. I am sure that when the work with Asahi-Linux is complete, you’d be able to boot up a Linux distro on an M1 and use sudo apt gfortran to get set up.