LoginSignup
2
1

WSL2 による gfortran と intel fortran の環境構築

Last updated at Posted at 2024-03-13

概要

WSL2 を使って,Fortran の実行環境を整備する方法をまとめました.
具体的には,gfortranIntel Fortran を WSL2 上でインストール方法を紹介します.

  • 利用可能になるまでの所要時間
    • gfortran : 1分
    • Intel Fortran : 30分

  • 実行環境は Windows 11 です

WSL2 のインストール方法については以下の記事をご覧ください.

目次

1. gfortran のインストール方法

Windows では GNU Fortran のダウンロードページに行く必要がありますが,WSL2 ではUbuntu ターミナルに sudo apt install gfortran と入力するだけで OK です.

Ubuntu
sudo apt install gfortran

確認方法

gfortran と入力して,以下の応答があれば gfortran が利用可能な状態です.

Ubuntu
gfortran
gfortran: fatal error: no input files
compilation terminated.

共有メモリ型の並列計算に使う OpenMP も利用可能です.

GNU make コマンドの利用

gfortran のコンパイルに便利な GNU makeインストール済み です.
試しに make --version と入力してみましょう.

Ubuntu
make --version
GNU Make 4.3
Built for x86_64-pc-linux-gnu
Copyright (C) 1988-2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

2. Intel Fortran のインストール方法

Intel Fortran とは インテル社が提供する 無償 の fortran コンパイラです.
gfortran に比べてオプション依存性が高く,通常 gfortran よりも非常に高速です.
使い方はコンパイル時に gfortran と書いていた部分を ifort と変えるだけです.
なお,ifort オプションは gfortran とは異なるので注意してください.

これを利用するには,以下の二つをインストールする必要があります.

  • Intel oneAPI Base Toolkit
  • Intel oneAPI HPC Toolkit

gfortran のインストール方法と異なり,インテル社のダウンロードページにアクセスする必要があるため,やや複雑です.


主に,以下の記事を参考にさせて頂きました.

2-1. Intel Fortran のインストール

  1. Intel のリポジトリにアクセスして,インストールする準備を行う (コピペでOK)
    Ubuntu
    wget -O- https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB | gpg --dearmor | sudo tee /usr/share/keyrings/oneapi-archive-keyring.gpg > /dev/null
    
  2. 上のリポジトリを確認するように設定する (コピペでOK)
    Ubuntu
    echo "deb [signed-by=/usr/share/keyrings/oneapi-archive-keyring.gpg] https://apt.repos.intel.com/oneapi all main" | sudo tee /etc/apt/sources.list.d/oneAPI.list 
    
  3. 次を実行
    Ubuntu
    sudo apt update
    
  4. Intel oneAPI Base Toolkit と Intel oneAPI HPC Toolkit を順番にインストールする
    (20分くらいかかります)
    Ubuntu
    sudo apt install intel-basekit
    sudo apt install intel-hpckit
    

これで Intel Fortran 自体のインストールは完了しました.
インストール先は /opt/intel/oneapi/ 以下です.

一応,最後に sudo apt update をしておきましょう.

エラーが出る場合は sudo apt updatesudo apt upgrade を実行してから,再試行してみてください.

2-2. Intel Fortran の有効化

Intel Fortran はインストールしただけでは利用できません.
試しに,ifort --version と入力してみると,次のように返答されます.

Ubuntu
ifort --version
Command 'ifort' not found, ...

Intel Fortran を有効化するには,source /opt/intel/oneapi/setvars.sh と入力します.

Ubuntu
source /opt/intel/oneapi/setvars.sh

改めて ifort --version と入力してみると,無事動くことが確認できます.

Ubuntu
ifort --version
ifort: remark #10448: Intel(R) Fortran Compiler Classic (ifort) is now deprecated and will be discontinued late 2024. Intel recommends that customers transition now to using the LLVM-based Intel(R) Fortran Compiler (ifx) for continued Windows* and Linux* support, new language support, new language features, and optimizations. Use '-diag-disable=10448' to disable this message.
ifort (IFORT) 2021.11.1 20231117
Copyright (C) 1985-2023 Intel Corporation.  All rights reserved.

2-3. Ubuntu 起動時に Intel Fortran を有効化しておく

いちいち,source /opt/intel/setvars.sh と打つのは面倒なので,Ubuntu 起動時に自動で有効化するようにしておきましょう.
具体的には,~/.bashrcsource /opt/intel/oneapi/setvars.sh を追記します.

Ubuntu
vim ~/.bashrc
# 最後尾に以下を追加
source /opt/intel/oneapi/setvars.sh

確認のため,Ubuntu を閉じて再度 ifort --version と入力してみましょう.

Ubuntu
ifort --version
ifort: remark #10448: Intel(R) Fortran Compiler Classic (ifort) is now deprecated and will be discontinued late 2024. Intel recommends that customers transition now to using the LLVM-based Intel(R) Fortran Compiler (ifx) for continued Windows* and Linux* support, new language support, new language features, and optimizations. Use '-diag-disable=10448' to disable this message.
ifort (IFORT) 2021.11.1 20231117
Copyright (C) 1985-2023 Intel Corporation.  All rights reserved.

無事,自動で起動していることが確認できます.

2-4. remark #10448って何?

ifort --version を実行すると,remark #10448 とコメントが付きます.
実は Intel Fortran は2024年末を持ってサービスを終了するようで,そのことを親切に教えてくれていたのです.
毎回コメントされてお節介と言う人は,コメントにもあるように,コンパイルオプションに -diag-disable=10448 を追加しておきましょう.

[参考記事]

まとめ

WSL2 上に gfortran と Intel Fortran を導入する方法をまとめました.
何かの参考になれば大変幸いです.

WSL2 上に Python の実行環境を構築する方法をまとめた記事も書いていますので,興味のある方はご覧ください.

参考文献

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1