You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lectures/software_engineering/tools_editors.md
+55-75Lines changed: 55 additions & 75 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,26 +18,72 @@ kernelspec:
18
18
</div>
19
19
```
20
20
21
-
# Julia Tools and Editors
21
+
# Visual Studio Code and other Julia Tools
22
22
23
23
```{contents} Contents
24
24
:depth: 2
25
25
```
26
26
27
-
Co-authored with Arnav Sood
27
+
While Jupyter notebooks are a great way to get started with the language, eventually you will want to use more powerful tools. Visual Studio Code (VS Code) in particular, is the most popular open source editor for programming, and has a huge set of extensions and strong industry support.
28
28
29
-
While Jupyter notebooks are a great way to get started with the language, eventually you will want to use more powerful tools.
29
+
While you can use source code control, run terminals/REPLs, without VS Code, we will concentrate on using it as a full IDE for all of these features.
30
30
31
-
We'll discuss a few of them here, such as
31
+
## Installing VS Code
32
32
33
-
* Text editors like Atom, which come with rich Julia support for debugging, documentation, git integration, plotting and inspecting data, and code execution.
34
-
* The Julia REPL, which has specialized modes for package management, shell commands, and help.
33
+
To install VS Code and the Julia Extension,
34
+
35
+
1. Follow the instructions for setting up Julia {ref}`on your local computer <jl_jupyterlocal>`.
36
+
2. Install [VS Code](https://code.visualstudio.com/) for your platform and open it
37
+
3. Install the [VS Code Julia](https://marketplace.visualstudio.com/items?itemName=julialang.language-julia) extension
38
+
- After installation of VS Code, you should be able to choose `Install` on the webpage of any extensions and it will open on your desktop.
39
+
- Otherwise, open the extensions with `<Ctrl-Shift-X>` or selecting extensions in the left-hand side of the VS Code window. Then search for `Julia` in the Marketplace
40
+
41
+
See the [Julia VS Code Documentation](https://www.julia-vscode.org/docs/dev/gettingstarted/#Installation-and-Configuration-1) for more details.
42
+
43
+
If you have done a typical Julia installation, then this may be all that is needed and no configuration may be necessary. However, if you have installed Julia in a non-standard location you may need to manually set the executable path. See [here](https://www.julia-vscode.org/docs/dev/gettingstarted/#Configuring-the-Julia-extension-1) for instructions if it errors when starting Julia terminals.
44
+
45
+
### Optional Extensions
46
+
47
+
While not required for these lectures, consider installing the following extensions. As before, you can search for them on the Marketplace or choose `Install` from the webpages themselves.
48
+
49
+
1.[Jupyter](https://marketplace.visualstudio.com/items?itemName=ms-toolsai.jupyter): VS Code increasingly supports Jupyter notebooks directly, and this extension provides the ability to open and edit `.ipynb` notebook files wihtout running `jupyter lab`, etc.
50
+
1.[GitLens](https://marketplace.visualstudio.com/items?itemName=eamodio.gitlens): An extension that provides an enormous amount of detail on exact code changes within github repositories (e.g., seamless information on the time and individual who [last modified](https://marketplace.visualstudio.com/items?itemName=eamodio.gitlens#current-line-blame-) each line of code)
51
+
2.[GitHub Pull Requests and Issues](https://marketplace.visualstudio.com/items?itemName=GitHub.vscode-pull-request-github): while VS Code supports the git {doc}`version control <../software_engineering/version_control>` natively, these extension provides additional features for working with repositories on GitHub itself.
52
+
3.[Markdown All in One](https://marketplace.visualstudio.com/items?itemName=yzhang.markdown-all-in-one): For editing the markdown format, such as `README.md` and similar files.
53
+
4. Finally, VS Code is an excellent latex editor. To install support,
54
+
- Install a recent version of miktex or texlive for your platform
- Optionally install [Code Spell Checker](https://marketplace.visualstudio.com/items?itemName=streetsidesoftware.code-spell-checker) for spell checking
57
+
- See [documentation](https://github.com/James-Yu/LaTeX-Workshop#manual). The easiest use is to put the magic comment `!TEX program = pdflatex` at the top of a `.tex` file.
58
+
-
59
+
60
+
61
+
See the [VS Code documentation](https://code.visualstudio.com/docs/getstarted/userinterface) for an introduction.
62
+
63
+
A key feature is the [Command Palette](https://code.visualstudio.com/docs/getstarted/userinterface#_command-palette), which can be accessed with `<Ctrl+Shift+P>`.
With this, you can type partial strings for different commands and it helps you to find features of vscode and its extensions. This is so common that in these notes we
70
+
denote opening the command palette and searching for a command with things like `> Julia: Start REPL` , etc. You will only need to type part of the string, and the command palette remembers
71
+
your most commonly used
72
+
73
+
74
+
### Optional Extensions and Settings
75
+
76
+
77
+
Open the settings, either with `> Preferences: User Settings` (recall this can be found with the palette and `<Ctrl-Shift-P>`) or with the menu.
78
+
79
+
As a few optional suggestions for working with the settings,
80
+
81
+
- In the settings, search for `Tab Size` and you should find `Editor: Tab Size` which you can modify to 4.
82
+
- Search for `quick open` and change `Workbench > Editor: Enable Preview from Quick Open` and consider setting it to false, though this is a matter of personal taste.
83
+
- Finally, if you are on Windows, search for `eol` and change `Files: Eol` to be `\n`.
35
84
36
-
Note that we assume you've already completed the {doc}`getting started <../getting_started_julia/getting_started>` and {doc}`interacting with Julia <../getting_started_julia/julia_environment>` lectures.
37
85
38
-
## Preliminary Setup
39
86
40
-
Follow the instructions for setting up Julia {ref}`on your local computer <jl_jupyterlocal>`.
41
87
42
88
(repl_main)=
43
89
## The REPL
@@ -120,72 +166,6 @@ The most feature-rich one for Julia development is [Atom](https://atom.io/), wit
120
166
121
167
There are several reasons to use a text editor like Atom, including
122
168
123
-
* Git integration (more on this in the {doc}`next lecture <../software_engineering/version_control>`).
124
-
* Painless inspection of variables and data.
125
-
* Easily run code blocks, and drop in custom snippets of code.
126
-
* Integration with Julia documentation and plots.
127
-
128
-
### Installation and Configuration
129
-
130
-
#### Installing Atom
131
-
132
-
1. Download and Install Atom from the [Atom website](https://atom.io/).
133
-
1. (Optional, but recommended): Change default Atom settings
134
-
* Use `Ctrl-,` to get the `Settings` pane
135
-
* Choose the `Packages` tab
136
-
* Type `line-ending-selector` into the Filter and then click "Settings" for that package
137
-
* Change the default line ending to `LF` (only necessary on Windows)
138
-
* Choose the Editor tab
139
-
* Turn on `Soft Wrap`
140
-
* Set the `Tab Length` default to `4`
141
-
142
-
#### Installing Juno
143
-
144
-
1. Use `Ctrl-,` to get the Settings pane.
145
-
1. Go to the `Install` tab.
146
-
1. Type `uber-juno` into the search box and then click Install on the package that appears.
147
-
1. Wait while Juno installs dependencies.
148
-
1. When it asks you whether or not to use the standard layout, click `yes`.
149
-
150
-
At that point, you should see a built-in REPL at the bottom of the screen and be able to start using Julia and Atom.
151
-
152
-
(atom_troubleshooting)=
153
-
#### Troubleshooting
154
-
155
-
Sometimes, Juno will fail to find the Julia executable (say, if it's installed somewhere nonstandard, or you have multiple).
156
-
157
-
To do this
158
-
1.`Ctrl-,` to get Settings pane, and select the Packages tab.
159
-
2. Type in `julia-client` and choose Settings.
160
-
3. Find the Julia Path, and fill it in with the location of the Julia binary.
161
-
162
-
* To find the binary, you could run `Sys.BINDIR` in the REPL, then add in an additional `/julia` to the end of the screen.
163
-
* e.g. `C:\Users\YOURUSERNAME\AppData\Local\Julia-1.0.1\bin\julia.exe` on Windows as `/Applications/Julia-1.0.app/Contents/Resources/julia/bin/julia` on OSX.
164
-
165
-
>
166
-
167
-
See the [setup instructions for Juno](http://docs.junolab.org/latest/man/installation.html) if you have further issues.
168
-
169
-
If you upgrade Atom and it breaks Juno, run the following in a terminal.
170
-
171
-
```{code-block} none
172
-
apm uninstall ink julia-client
173
-
apm install ink julia-client
174
-
```
175
-
176
-
If you aren't able to install `apm` in your PATH, you can do the above by running the following in PowerShell:
177
-
178
-
```{code-block} none
179
-
cd $ENV:LOCALAPPDATA/atom/bin
180
-
```
181
-
182
-
Then navigating to a folder like `C:\Users\USERNAME\AppData\Local\atom\bin` (which will contain the `apm` tool), and running:
Copy file name to clipboardExpand all lines: lectures/tools_and_techniques/numerical_linear_algebra.md
+73-1Lines changed: 73 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -54,7 +54,7 @@ The theme of this lecture, and numerical linear algebra in general, comes down t
54
54
---
55
55
tags: [hide-output]
56
56
---
57
-
using LinearAlgebra, Statistics, BenchmarkTools, SparseArrays, Random
57
+
using LinearAlgebra, Statistics, BenchmarkTools, SparseArrays, Random, Parameters
58
58
Random.seed!(42); # seed random numbers for reproducibility
59
59
```
60
60
@@ -980,6 +980,78 @@ A[2,1] = 100.0
980
980
But again, you will often find that doing `@view` leads to slower code. Benchmark
981
981
instead, and generally rely on it for large matrices and for contiguous chunks of memory (e.g., columns rather than rows).
982
982
983
+
## Patterns for Preallocated Caches of Results
984
+
985
+
When the matrices and vectors get large, it can reach a point where it is important to cache the results and reduce allocations. In general, this should only be attempted when the vectors are large and they would otherwise need to be reallocated many times.
986
+
987
+
One approach is to create a {doc}`custom type<../getting_started_julia/introduction_to_types>` to hold the results, being very careful to ensure that the type is concrete.
988
+
989
+
990
+
```{code-cell} julia
991
+
struct MyResults{T}
992
+
x::Array{T,1}
993
+
A::Array{T,2}
994
+
end
995
+
MyResults(N) = MyResults(Array{Float64,1}(undef, N), Array{Float64,2}(undef, N, N))
996
+
997
+
#Support for inplace copying
998
+
import Base.copy!
999
+
function copy!(dst::MyResults, src::MyResults)
1000
+
dst.x .= src.x
1001
+
dst.A .= src.A
1002
+
end
1003
+
```
1004
+
1005
+
The above code is an **immutable** structure for holding the `x` and `A` buffers. The fact that it is immutable ensures that the `x` vector itself cannot be changed (even if the values within `x` can be). This can help ensure that you do not accidentally reallocate and assign a new vector to `x`.
1006
+
1007
+
The only other code is an implementation of the in-place `copy!` function, which will allow us to copy all of the results as required by some algorithms.
1008
+
1009
+
To create a contrived algorithm, see the following code:
1010
+
1011
+
```{code-cell} julia
1012
+
# By convention, name has ! to denote mutating, and mutate first argument
1013
+
function calculate_results!(results, val, params)
1014
+
@unpack N, b, C = params
1015
+
B = rand(N,N) # Contrived. Assume complicated
1016
+
lmul!(val, B) # val * B -> B inplace, no allocation
1017
+
mul!(results.A, B, C) # B * C -> results.A
1018
+
ldiv!(results.x, factorize(results.A), b) # x = A \ b inplace
1019
+
end
1020
+
1021
+
# Some iterative algorithm
1022
+
function iterate_values(vals, params)
1023
+
@unpack N = params
1024
+
1025
+
# preallocate
1026
+
results = MyResults(N)
1027
+
prev_results = MyResults(N)
1028
+
norms = similar(vals)
1029
+
1030
+
for (i, val) in enumerate(vals)
1031
+
calculate_results!(results, val, params)
1032
+
norms[i] = norm(results.x- prev_results.x)
1033
+
println("|x_new - x_old| = ", norms[i])
1034
+
copy!(prev_results, results)
1035
+
end
1036
+
return norms
1037
+
end
1038
+
1039
+
params = (N = 5, C = rand(5,5), b = rand(5))
1040
+
vals = range(0.0, 1.0, length = 10)
1041
+
iterate_values(vals, params)
1042
+
```
1043
+
1044
+
A few points:
1045
+
- This creates an inplace function, `calculate_results!`, which modifies the results given a value and parameters.
1046
+
- Within the function, it attemmpts to use inplace versions of the operations where possible, which can help cut down on other allocations
1047
+
- The iteration simply goes through a list of values, calls the `calculate_results!` and then checks how the `results.x` has changed with a norm.
1048
+
- Finally, the iteration copys the new results into the previous ones. This ensure that only a single copy is required for comparison.
1049
+
1050
+
1051
+
1052
+
This approach can be very helpful for large matrices and arrays, but should be used judiciously and only after {doc}`profiling<../software_engineering/need_for_speed>` .
0 commit comments