{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"
\n",
"\n",
"### Programming large-scale parallel systems\n",
"### Lectures on Julia for HPC\n",
"\n",
"\n",
"# Tutorial: Getting started with Julia\n",
"\n",
"by Francesc Verdugo (VU Amsterdam)\n",
"\n",
"Version fall 2022"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Contents\n",
"\n",
"- Using the Julia REPL\n",
"- Running serial and parallel code\n",
"- Installing and managing packages"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Introduction\n",
"\n",
"Julia has its own way of running code and using packages. Many educational sources about Julia assume that you have this basic knowledge, which can be confusing to new users. In this lesson, we will learn these basic skills so that you can start learning more on Julia."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Preliminaries\n",
"\n",
"This is a tutorial. To follow it:\n",
"\n",
"- Download and install Julia from https://julialang.org/\n",
"- Download and install VSCode and its Julia extension. How to: https://www.julia-vscode.org/docs/dev/gettingstarted/\n",
"- Continue reading this document \n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## The Julia REPL\n",
"\n",
"There are several ways of opening Julia depending on your operating system and your IDE, but it is usually as simple as launching the Julia app. With VSCode, open a folder (File > Open Folder). Then, press `Ctrl`+`Shift`+`P` to open the command bar, and execute `Julia: Start REPL`. If this does not work, make sure you have the Julia extension for VSCode installed. Independently of the method you use, opening Julia results in a window with some text ending with:\n",
"\n",
"```\n",
"julia> \n",
"```\n",
"\n",
"You have just opened the Julia *read-evaluate-print loop*, or simply the Julia *REPL*. Congrats! You will spend most of time using the REPL, when working in Julia. The REPL is a console waiting for user input. Just as in other consoles, the string of text right before the input area (`julia>` in the case) is called the *command prompt* or simply the *prompt*.\n",
"\n",
"\n",
"The usage is as follows:\n",
"- You write some input\n",
"- press enter\n",
"- you get the output\n",
"\n",
"For instance, try this\n",
"\n",
"```julia\n",
"julia> 1 + 1\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Hello world!\n",
"\n",
"A \"Hello world\" example looks like this in Julia\n",
"\n",
"```julia\n",
"julia> println(\"Hello, world!\")\n",
"```\n",
"\n",
"Try to run it in the REPL."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Help mode\n",
"\n",
"\n",
"\n",
"Curious about what function `println` does? Enter into *help* mode to look into the documentation. This is done by typing a question mark (`?`) into the inut field:\n",
"\n",
"```julia\n",
"julia> ?\n",
"```\n",
"\n",
"After typing `?`, the command prompt changes to `help?>`. It means we are in help mode. Now, we can type a function name to see its documentation.\n",
"\n",
"```julia\n",
"help?> println\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Package and shell modes\n",
"\n",
"The REPL comes with two more modes, namely *package* and *shell* modes. To enter package mode type\n",
"\n",
"```julia\n",
"julia> ]\n",
"```\n",
"\n",
"Package mode is used to install and manage packages. We are going to discuss the package mode in greater detail later. To return back to normal mode press the backspace key several times.\n",
"\n",
"To enter shell mode type semicolon (`;`)\n",
"```julia\n",
"julia> ;\n",
"```\n",
"The prompt should have changed to `shell>` indicating that we are in shell mode. Now you can type commands that you would normally do on your system command line. For instance, \n",
"\n",
"```julia\n",
"shell> ls\n",
"```\n",
"will display the contents of the current folder in Mac or Linux. Using shell mode in Windows is not straightforward, and thus not recommended for beginners.\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Running more complex code\n",
"\n",
"Real-world Julia programs are not typed in the REPL in practice. They are written in one or more files and *included* in the REPL. To try this, create a new file called `hello.jl`, write the code of the \"Hello world\" example above, and save it. If you are using VSCode, you can create the file using File > New File > Julia File. Once the file is saved with the name `hello.jl`, execute it as follows\n",
"\n",
"```julia\n",
"julia> include(\"hello.jl\")\n",
"```\n",
"\n",
"
\"hello.jl\" is located in the current working directory of your Julia session. You can query the current directory with function pwd(). You can change to another directory with function cd() if needed. Also, make sure that the file extension is .jl.\n",
"$, it should be run in the terminal. Otherwise, the code is to be run in the Julia REPL.\n",
".jl. This is just a way of signaling that a package is written in Julia. When using such packages, the .jl needs to be ommited. In this case, we have isntalled the MPI.jl package even though we have only typed MPI in the REPL.\n",
"MPI.jl. Note that it is not a MPI library by itself. It is just a thin wrapper between MPI and Julia. To use this interface, you need an actual MPI library installed in your system such as OpenMPI or MPICH. Julia downloads and installs a MPI library for you, but it is also possible to use a MPI library already available in your system. This is useful, e.g., when running on HPC clusters. See the documentation of MPI.jl for further details.\n",
"--project flag. The command $ julia --project=. will open Julia and activate a project in the current directory. You can also achieve the same effect by setting the environment variable JULIA_PROJECT with the path of the folder you want to activate.\n",
" (@v1.8) pkg> activate folderB and then julia> cd(\"folderA\"), will activate the project in folderB and change the current working directory to folderA.\n",
" \n",
"