-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a Demo2 notebook and a test for it See merge request lfortran/lfortran!624
- Loading branch information
Showing
2 changed files
with
226 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |