Installing Fortran Compiler on Ubuntu
The most widely used Fortran compiler for Ubuntu is gfortran (part of the GNU Compiler Collection), which is open-source and well-supported. To install it:
- Update your package list to ensure access to the latest versions:
sudo apt update - Install gfortran using the following command:
sudo apt install gfortran - Verify the installation by checking the compiler version:
gfortran --version
For users needing a specific gfortran version (e.g., 7.x), add the Ubuntu Toolchain PPA, update the package list, and install the desired version:
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt update
sudo apt install gfortran-7
Switch between installed versions using update-alternatives:
sudo update-alternatives --config gfortran
Follow the prompts to select your preferred default version.
Writing and Compiling Fortran Programs
Create a Fortran source file (e.g., hello.f90) using a text editor (nano, vim, or gedit):
nano hello.f90
Add a simple “Hello, World!” program:
program hello
implicit none
print *, 'Hello, World!'
end program hello
Save and exit the editor. Compile the program with gfortran:
gfortran -o hello hello.f90
Run the executable:
./hello
This will output `Hello, World!’ to the terminal.
Key Libraries for Scientific Computing
Scientific computing in Fortran relies on optimized libraries for numerical operations. Essential libraries include:
- BLAS (Basic Linear Algebra Subprograms): Handles vector/matrix operations. Install via:
sudo apt install libblas-dev - LAPACK (Linear Algebra PACKage): Solves linear systems, eigenvalue problems, and singular value decompositions. Install via:
sudo apt install liblapack-dev - FFTW (Fastest Fourier Transform in the West): Optimized for fast Fourier transforms (FFT). Install via:
sudo apt install libfftw3-dev - Intel MKL (Math Kernel Library): Provides highly optimized math routines (requires Intel Parallel Studio XE or standalone MKL). Install via:
sudo apt install libmkl-dev
Link these libraries during compilation (e.g., for LAPACK):
gfortran -o my_program my_program.f90 -llapack -lblas
.
Enabling High Performance with Parallelism
For large-scale scientific computations, parallelism is critical. Two primary approaches are:
- OpenMP: Supports shared-memory parallelism (multi-threading). Add OpenMP directives to your code (e.g.,
!$omp parallel do) and compile with:gfortran -fopenmp -o parallel_program parallel_program.f90 - MPI (Message Passing Interface): Enables distributed-memory parallelism (multi-node). Install OpenMPI and compile with:
sudo apt install libopenmpi-dev openmpi-bin mpif90 -o mpi_program mpi_program.f90 mpirun -np 4 ./mpi_program # Run with 4 processes
These tools allow you to leverage multi-core CPUs and clusters for faster execution.
Recommended IDEs for Fortran Development
While text editors suffice for basic coding, IDEs enhance productivity with features like syntax highlighting, debugging, and project management:
- Visual Studio Code (VS Code): Install the “Fortran” extension (by Juniper Networks) for syntax highlighting, IntelliSense, and debugging.
- Eclipse with Eclipse Fortran Plugin: Provides a full-featured IDE for Fortran projects, including code refactoring and project exploration.
- PyCharm/IntelliJ IDEA: With the “Fortran” plugin, these IDEs offer robust support for Fortran development, including debugging and version control integration.
Best Practices for Fortran Development
To write efficient, maintainable Fortran code:
- Use Compiler Optimizations: Enable
-O2or-O3for advanced optimizations and-march=nativeto tailor code to your CPU architecture:gfortran -O3 -march=native -o optimized_program optimized_program.f90 - Leverage High-Performance Libraries: Avoid reinventing the wheel—use BLAS, LAPACK, and FFTW for numerical operations.
- Profile and Optimize: Use tools like
gprof(for profiling) orperf(for hardware-level analysis) to identify bottlenecks:gfortran -pg -o profiled_program profiled_program.f90 ./profiled_program gprof profiled_program gmon.out > analysis.txt - Refactor Regularly: Remove redundant code, use meaningful variable names, and modularize programs into subroutines/functions.
- Adopt Modern Fortran Features: Use
implicit noneto prevent undeclared variables,allocatablearrays for dynamic memory, and modules for code organization.
以上就是关于“Ubuntu上Fortran科学计算应用”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm