hbayraktaroglu / fortran

Documenting my Fortran learning with examples

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Paht's Fortran learning README

Documenting my Fortran learning with examples. This tutorial covers Auto-dependancy generation using fortdepend, debugging using gfortran, and setting up nvidia fortran compiler sdk for cuda and openacc development. Prerequisites: You have to have visual studio code installed. Have access to linux server either ssh or openvnc (debug purposes). If you are new to visual studio code there's lots of youtube videos on how to install extensions. This tutorial is a bit advance, it covers the creation of launch.json and task.json for build and debug purposes.

## Makefile Autodependancy generation These sets of codes are compiled using a makefile and fortdepend for auto-dependancy generation. This means that you do not need to specify ``` something.o: depends_on_1.o depends_on_2.o ```

This is automatically generated by fortdepend and is included in the makefile include(my_project.dep) To get started (this works on linux but can work on windows if you want)

  1. Install fortdepends using `pip install fortdepend`
  2. In terminal do `find -name fortdepend` that is your reference path that you need to set in the make file. It should reference the binary. You should simply be able to call fortdepend in your folder and have it generate a dependancy file `fortdepend -o Makefile.dep`. If this doesn't work then that's not the right file

Debugging using Visual Code

gfortran Windows WSL and Mac

Step 1. Install Visual studio code

Step 2. Install fortran language server pip install fortran-language-server

Step 3. Install fortran intellisense https://marketplace.visualstudio.com/items?itemName=hansec.fortran-ls

Step 4. Install GDB. Windows (https://rpg.hamsterrepublic.com/ohrrpgce/GDB_on_Windows) Mac (https://www.ics.uci.edu/~pattis/common/handouts/macmingweclipse/allexperimental/mac-gdb-install.html)

Step 5 (optional) Set up visual studio code to build by creating a task.json inside the .vscode folder https://code.visualstudio.com/docs/editor/tasks Note: It doesn't matter what is in this file because you will be replacing it with the following

task.json file

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": ["make clean","make all","",""],
            "args": [],
            "group": "build",
            "presentation": {
                // Reveal the output only if unrecognized errors occur.
                "reveal": "silent"
            },
            // Use the standard MS compiler pattern to detect errors, warnings and infos
            "problemMatcher": "$msCompile"
        }
    ]
}

Step 5. Now that you've created the build task, it's time to set up the debug task. Create a Launch.json (https://code.visualstudio.com/docs/editor/debugging). Replace the launch json with the following code.

launch.json file

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Debug Fortran",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/main.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "build"
        }
    ]
}

Setting up your linux environment for nvfortran (NVIDIA's CUDA fortran compiler)

This compiler started out as pgfortran. I think NVIDIA bought them out recently and integrated their compiler into their HPC SDK (https://developer.nvidia.com/hpc-sdk). This compiler is tricky to set up. You will need a dedicated linux machine with a NVIDIA Graphics card. I tried using WSL 2 with Ubuntu 20.04 and no luck. I ended up installing Ubuntu 20.04 on my laptop and I was able to install the cuda drivers.

nvfortran Installation Steps

nvfortran is a compiler for cuda fortran. It should support open acc along with cuda Nvidia OpenACC. Compiler directives: nvfortran OpenACC Compiler Directives

Basic CUDA install

  1. Install the following
    1. gfortran `sudo apt-get install gfortran`
    2. build-essential `sudo apt-get install build-essential` we need gcc and all the standard libraries to compile c++ code
  2. Install CUDA on Ubuntu https://developer.nvidia.com/cuda-downloads
  3. This sets up the nvcc compiler and that's located in /opt/nvidia/hpc_sdk/Linux_x86_64/2021/compilers/bin
  4. The samples should work. Try going into /usr/local/cuda/samples/4_Finance/BlackScholes and compiling. Note: you may want to copy the sample code to the home directory
  5. One more thing, nvcc won't be included in your path so typing nvcc in terminal won't work. This is okay because the makefile references the propper location. (call make to compile)

CUDA HPC SDK Install

  1. Download the latest https://developer.nvidia.com/nvidia-hpc-sdk-downloads There may be an option to get the latest and the previous versions, get that one!
  2. The sdk is installed in the local folder. this is the path to nvfortran `hpc_install_path=/opt/nvidia/hpc_sdk/Linux_x86_64/2020/compilers/bin`

Now you are ready to add breakpoints and debug your code.

Notes on CUDA with Fortran Debugging

Debugging CUDA fortran applications is not as easy or common on the internet as C++. I tried using visual studio code to do my debugging. After hours of research and debugging as of 12/24/2020, I have determined that it is not worth it to pursue integrating visual studio code with cuda fortran files (cuf). We simply cannot debug these files easily without writing a custom visual studio code extension. nvfortran recognizes the .cuf differently and offers you access to custom cuda modules that are not available with conventional fortran extensions. If anyone has any new information on this please submit an issue and let me know.

Debugging is currently only available with Arm Forge but that's not a free program.

It might be worth it to compile small codes in cuda fortran that can be included in the overall fortran code or just use openacc.

About

Documenting my Fortran learning with examples

License:MIT License


Languages

Language:Makefile 60.8%Language:Fortran 37.7%Language:C 1.0%Language:Cuda 0.5%