Skip to content

Commit

Permalink
Merge branch 'nb3' into 'master'
Browse files Browse the repository at this point in the history
Add a Demo2 notebook and a test for it

See merge request lfortran/lfortran!624
  • Loading branch information
certik committed Sep 17, 2020
2 parents 9a3a2c9 + 33d5a4d commit 3a14a7f
Show file tree
Hide file tree
Showing 2 changed files with 226 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,11 @@ jupyter_kernel:
- python ci/test_fortran_kernel.py -v
- cd share/lfortran/nb
- jupyter nbconvert --to notebook --execute --ExecutePreprocessor.timeout=60 --output Demo1_out.ipynb Demo1.ipynb
- jupyter nbconvert --to notebook --execute --ExecutePreprocessor.timeout=60 --output Demo2_out.ipynb Demo2.ipynb
artifacts:
paths:
- share/lfortran/nb/Demo1_out.ipynb
- share/lfortran/nb/Demo2_out.ipynb
when: always

# Test LFortran without the LLVM backend
Expand Down
224 changes: 224 additions & 0 deletions share/lfortran/nb/Demo2.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# LFortran: Interactive Fortran Compiler\n",
"\n",
"This Demo notebook shows available magic commands in LFortran and how to use them."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Magic Commands"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Initialize some variables"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"integer :: i, j, n\n",
"n = 5\n",
"j = 0"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## AST"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Show the Abstract Syntax Tree (AST) after parsing (based on syntax only, no semantics):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%showast\n",
"do i = 1, n\n",
" j = j + i\n",
"end do"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## ASR\n",
"\n",
"Show the Abstract Semantic Representation (ASR), which contains all the semantics."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%showasr\n",
"do i = 1, n\n",
" j = j + i\n",
"end do"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## LLVM\n",
"\n",
"Show LLVM code:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%showllvm\n",
"do i = 1, n\n",
" j = j + i\n",
"end do"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## ASM\n",
"\n",
"Show assembly code:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%showasm\n",
"do i = 1, n\n",
" j = j + i\n",
"end do"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## C++\n",
"\n",
"Transform Fortran code to C++"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%showcpp\n",
"subroutine triad(a, b, scalar, c)\n",
"real, intent(in) :: a(:), b(:), scalar\n",
"real, intent(out) :: c(:)\n",
"integer :: N, i\n",
"N = size(a)\n",
"do concurrent (i = 1:N)\n",
" c(i) = a(i) + scalar * b(i)\n",
"end do\n",
"end subroutine"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%showcpp\n",
"program doconcurrentloop_01\n",
"implicit none\n",
"real, dimension(10000) :: a, b, c\n",
"real :: scalar\n",
"integer :: i, nsize\n",
"scalar = 10\n",
"nsize = size(a)\n",
"do concurrent (i = 1:nsize)\n",
" a(i) = 5\n",
" b(i) = 5\n",
"end do\n",
"call triad(a, b, scalar, c)\n",
"print *, \"End Stream Triad\"\n",
"\n",
"contains\n",
"\n",
" subroutine triad(a, b, scalar, c)\n",
" real, intent(in) :: a(:), b(:), scalar\n",
" real, intent(out) :: c(:)\n",
" integer :: N, i\n",
" N = size(a)\n",
" do concurrent (i = 1:N)\n",
" c(i) = a(i) + scalar * b(i)\n",
" end do\n",
" end subroutine\n",
"\n",
"end program"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Format Fortran code\n",
"\n",
"You can use `lfortran fmt` to format Fortran code, or the `%%showfmt` command."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%showfmt\n",
"subroutine triad(a, b, scalar, c); real, intent(in) :: a(:), b(:), scalar; real, intent(out) :: c(:); integer :: N, i; N = size(a); do concurrent (i = 1:N); c(i) = a(i) + scalar * b(i); end do; end subroutine"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Fortran",
"language": "fortran",
"name": "fortran"
},
"language_info": {
"file_extension": ".f90",
"mimetype": "text/x-fortran",
"name": "fortran",
"version": "2018"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 3a14a7f

Please sign in to comment.