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.
-
Wiki Contents
- Debugging fortran using visual studio code
- Coming soon, debugging using nvfortran and OpenACC, no clue if this will work but lets find out
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)
- Install fortdepends using `pip install fortdepend`
- 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
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
{
// 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.
{
// 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"
}
]
}
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 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
- Install the following
- gfortran `sudo apt-get install gfortran`
- build-essential `sudo apt-get install build-essential` we need gcc and all the standard libraries to compile c++ code
- Install CUDA on Ubuntu https://developer.nvidia.com/cuda-downloads
- This sets up the nvcc compiler and that's located in /opt/nvidia/hpc_sdk/Linux_x86_64/2021/compilers/bin
- 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
- 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
- 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!
- 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.
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.