2024-09-12 09:27:51 +00:00

19 lines
36 KiB
HTML

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><title>Getting started · XM_40017</title><meta name="title" content="Getting started · XM_40017"/><meta property="og:title" content="Getting started · XM_40017"/><meta property="twitter:title" content="Getting started · XM_40017"/><meta name="description" content="Documentation for XM_40017."/><meta property="og:description" content="Documentation for XM_40017."/><meta property="twitter:description" content="Documentation for XM_40017."/><meta property="og:url" content="https://fverdugo.github.io/XM_40017/getting_started_with_julia/"/><meta property="twitter:url" content="https://fverdugo.github.io/XM_40017/getting_started_with_julia/"/><link rel="canonical" href="https://fverdugo.github.io/XM_40017/getting_started_with_julia/"/><script data-outdated-warner src="../assets/warner.js"></script><link href="https://cdnjs.cloudflare.com/ajax/libs/lato-font/3.0.0/css/lato-font.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/juliamono/0.050/juliamono.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/fontawesome.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/solid.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/brands.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.8/katex.min.css" rel="stylesheet" type="text/css"/><script>documenterBaseURL=".."</script><script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js" data-main="../assets/documenter.js"></script><script src="../search_index.js"></script><script src="../siteinfo.js"></script><script src="../../versions.js"></script><link class="docs-theme-link" rel="stylesheet" type="text/css" href="../assets/themes/catppuccin-mocha.css" data-theme-name="catppuccin-mocha"/><link class="docs-theme-link" rel="stylesheet" type="text/css" href="../assets/themes/catppuccin-macchiato.css" data-theme-name="catppuccin-macchiato"/><link class="docs-theme-link" rel="stylesheet" type="text/css" href="../assets/themes/catppuccin-frappe.css" data-theme-name="catppuccin-frappe"/><link class="docs-theme-link" rel="stylesheet" type="text/css" href="../assets/themes/catppuccin-latte.css" data-theme-name="catppuccin-latte"/><link class="docs-theme-link" rel="stylesheet" type="text/css" href="../assets/themes/documenter-dark.css" data-theme-name="documenter-dark" data-theme-primary-dark/><link class="docs-theme-link" rel="stylesheet" type="text/css" href="../assets/themes/documenter-light.css" data-theme-name="documenter-light" data-theme-primary/><script src="../assets/themeswap.js"></script><link href="../assets/favicon.ico" rel="icon" type="image/x-icon"/><script src="../assets/iframeResizer.min.js"></script><link href="../assets/custom.css" rel="stylesheet" type="text/css"/></head><body><div id="documenter"><nav class="docs-sidebar"><a class="docs-logo" href="../"><img src="../assets/logo.png" alt="XM_40017 logo"/></a><div class="docs-package-name"><span class="docs-autofit"><a href="../">XM_40017</a></span></div><button class="docs-search-query input is-rounded is-small is-clickable my-2 mx-auto py-1 px-2" id="documenter-search-query">Search docs (Ctrl + /)</button><ul class="docs-menu"><li><a class="tocitem" href="../">Home</a></li><li class="is-active"><a class="tocitem" href>Getting started</a><ul class="internal"><li><a class="tocitem" href="#Introduction"><span>Introduction</span></a></li><li><a class="tocitem" href="#Why-Julia?"><span>Why Julia?</span></a></li><li><a class="tocitem" href="#Installing-Julia"><span>Installing Julia</span></a></li><li><a class="tocitem" href="#The-Julia-REPL"><span>The Julia REPL</span></a></li><li><a class="tocitem" href="#Running-Julia-code"><span>Running Julia code</span></a></li><li><a class="tocitem" href="#Package-manager"><span>Package manager</span></a></li><li><a class="tocitem" href="#Conclusion"><span>Conclusion</span></a></li></ul></li><li><span class="tocitem">Notebooks</span><ul><li><a class="tocitem" href="../julia_basics/">Julia Basics</a></li><li><a class="tocitem" href="../julia_async/">Asynchronous programming in Julia</a></li><li><a class="tocitem" href="../julia_distributed/">Distributed computing in Julia</a></li><li><a class="tocitem" href="../matrix_matrix/">Matrix-matrix multiplication</a></li><li><a class="tocitem" href="../julia_mpi/">MPI (point-to-point)</a></li><li><a class="tocitem" href="../mpi_collectives/">MPI (collectives)</a></li><li><a class="tocitem" href="../jacobi_method/">Jacobi method</a></li></ul></li><li><a class="tocitem" href="../solutions_for_all_notebooks/">Solutions</a></li></ul><div class="docs-version-selector field has-addons"><div class="control"><span class="docs-label button is-static is-size-7">Version</span></div><div class="docs-selector control is-expanded"><div class="select is-fullwidth is-size-7"><select id="documenter-version-selector"></select></div></div></div></nav><div class="docs-main"><header class="docs-navbar"><a class="docs-sidebar-button docs-navbar-link fa-solid fa-bars is-hidden-desktop" id="documenter-sidebar-button" href="#"></a><nav class="breadcrumb"><ul class="is-hidden-mobile"><li class="is-active"><a href>Getting started</a></li></ul><ul class="is-hidden-tablet"><li class="is-active"><a href>Getting started</a></li></ul></nav><div class="docs-right"><a class="docs-navbar-link" href="https://github.com/fverdugo/XM_40017/blob/main/docs/src/getting_started_with_julia.md#" title="Edit source on GitHub"><span class="docs-icon fa-solid"></span></a><a class="docs-settings-button docs-navbar-link fa-solid fa-gear" id="documenter-settings-button" href="#" title="Settings"></a><a class="docs-article-toggle-button fa-solid fa-chevron-up" id="documenter-article-toggle-button" href="javascript:;" title="Collapse all docstrings"></a></div></header><article class="content" id="documenter-page"><h1 id="Getting-started"><a class="docs-heading-anchor" href="#Getting-started">Getting started</a><a id="Getting-started-1"></a><a class="docs-heading-anchor-permalink" href="#Getting-started" title="Permalink"></a></h1><h2 id="Introduction"><a class="docs-heading-anchor" href="#Introduction">Introduction</a><a id="Introduction-1"></a><a class="docs-heading-anchor-permalink" href="#Introduction" title="Permalink"></a></h2><p>The programming of this course will be done using the <a href="https://julialang.org">Julia programming language</a>. Thus, we start by explaining how to get up and running with Julia. After studying this page, you will be able to:</p><ul><li>Use the Julia REPL,</li><li>Run serial and parallel code,</li><li>Install and manage Julia packages.</li></ul><h2 id="Why-Julia?"><a class="docs-heading-anchor" href="#Why-Julia?">Why Julia?</a><a id="Why-Julia?-1"></a><a class="docs-heading-anchor-permalink" href="#Why-Julia?" title="Permalink"></a></h2><p>Courses related with high-performance computing (HPC) often use languages such as C, C++, or Fortran. We use Julia instead to make the course accessible to a wider set of students, including the ones that have no experience with C/C++ or Fortran, but are willing to learn parallel programming. Julia is a relatively new programming language specifically designed for scientific computing. It combines a high-level syntax close to interpreted languages like Python with the performance of compiled languages like C, C++, or Fortran. Thus, Julia will allow us to write efficient parallel algorithms with a syntax that is convenient in a teaching setting. In addition, Julia provides easy access to different programming models to write distributed algorithms, which will be useful to learn and experiment with them.</p><div class="admonition is-success"><header class="admonition-header">Tip</header><div class="admonition-body"><p>You can run the code <a href="https://github.com/JuliaAcademy/JuliaTutorials/blob/724e15a350d150a9773afe51a3830709dbed422f/introductory-tutorials/intro-to-julia/09.%20Julia%20is%20fast.ipynb">in this link</a> to learn how Julia compares to other languages (C and Python) in terms of performance.</p></div></div><h2 id="Installing-Julia"><a class="docs-heading-anchor" href="#Installing-Julia">Installing Julia</a><a id="Installing-Julia-1"></a><a class="docs-heading-anchor-permalink" href="#Installing-Julia" title="Permalink"></a></h2><p>This is a tutorial-like page. Follow these steps before you continue reading the document.</p><ul><li>Download and install Julia from <a href="https://julialang.org">julialang.org</a>;</li><li>Follow the specific instructions for your operating system: <a href="https://julialang.org/downloads/platform/#windows">Windows</a>, <a href="https://julialang.org/downloads/platform/#macos">MacOS</a>, or <a href="https://julialang.org/downloads/platform/#linux_and_freebsd">Linux</a></li><li>Download and install <a href="https://www.julia-vscode.org/docs/dev/gettingstarted/">VSCode and its Julia extension</a>;</li></ul><h2 id="The-Julia-REPL"><a class="docs-heading-anchor" href="#The-Julia-REPL">The Julia REPL</a><a id="The-Julia-REPL-1"></a><a class="docs-heading-anchor-permalink" href="#The-Julia-REPL" title="Permalink"></a></h2><h3 id="Starting-Julia"><a class="docs-heading-anchor" href="#Starting-Julia">Starting Julia</a><a id="Starting-Julia-1"></a><a class="docs-heading-anchor-permalink" href="#Starting-Julia" title="Permalink"></a></h3><p>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 &gt; Open Folder). Then, press <code>Ctrl</code>+<code>Shift</code>+<code>P</code> to open the command bar, and execute <code>Julia: Start REPL</code>. 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:</p><pre><code class="nohighlight hljs">julia&gt;</code></pre><p>You have just opened the Julia <em>read-evaluate-print loop</em>, or simply the Julia <em>REPL</em>. 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 (<code>julia&gt;</code> in the case) is called the <em>command prompt</em> or simply the <em>prompt</em>.</p><h3 id="Basic-usage"><a class="docs-heading-anchor" href="#Basic-usage">Basic usage</a><a id="Basic-usage-1"></a><a class="docs-heading-anchor-permalink" href="#Basic-usage" title="Permalink"></a></h3><p>The usage of the REPL is as follows:</p><ul><li>You write some input</li><li>press enter</li><li>you get the output</li></ul><p>For instance, try this</p><pre><code class="language-julia hljs">julia&gt; 1 + 1</code></pre><p>A &quot;Hello world&quot; example looks like this in Julia</p><pre><code class="language-julia hljs">julia&gt; println(&quot;Hello, world!&quot;)</code></pre><p>Try to run it in the REPL.</p><h3 id="Help-mode"><a class="docs-heading-anchor" href="#Help-mode">Help mode</a><a id="Help-mode-1"></a><a class="docs-heading-anchor-permalink" href="#Help-mode" title="Permalink"></a></h3><p>Curious about what the function <code>println</code> does? Enter into <em>help</em> mode to look into the documentation. This is done by typing a question mark (<code>?</code>) into the input field:</p><pre><code class="language-julia hljs">julia&gt; ?</code></pre><p>After typing <code>?</code>, the command prompt changes to <code>help?&gt;</code>. It means we are in help mode. Now, we can type a function name to see its documentation.</p><pre><code class="language-julia hljs">help?&gt; println</code></pre><h3 id="Package-and-shell-modes"><a class="docs-heading-anchor" href="#Package-and-shell-modes">Package and shell modes</a><a id="Package-and-shell-modes-1"></a><a class="docs-heading-anchor-permalink" href="#Package-and-shell-modes" title="Permalink"></a></h3><p>The REPL comes with two more modes, namely <em>package</em> and <em>shell</em> modes. To enter package mode type</p><pre><code class="language-julia hljs">julia&gt; ]</code></pre><p>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.</p><p>To enter shell mode type semicolon (<code>;</code>)</p><pre><code class="language-julia hljs">julia&gt; ;</code></pre><p>The prompt should have changed to <code>shell&gt;</code> indicating that we are in shell mode. Now you can type commands that you would normally do on your system command line. For instance,</p><pre><code class="language-julia hljs">shell&gt; ls</code></pre><p>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.</p><h2 id="Running-Julia-code"><a class="docs-heading-anchor" href="#Running-Julia-code">Running Julia code</a><a id="Running-Julia-code-1"></a><a class="docs-heading-anchor-permalink" href="#Running-Julia-code" title="Permalink"></a></h2><h3 id="Running-more-complex-code"><a class="docs-heading-anchor" href="#Running-more-complex-code">Running more complex code</a><a id="Running-more-complex-code-1"></a><a class="docs-heading-anchor-permalink" href="#Running-more-complex-code" title="Permalink"></a></h3><p>Real-world Julia programs are not typed in the REPL in practice. They are written in one or more files and <em>included</em> in the REPL. To try this, create a new file called <code>hello.jl</code>, write the code of the &quot;Hello world&quot; example above, and save it. If you are using VSCode, you can create the file using File &gt; New File &gt; Julia File. Once the file is saved with the name <code>hello.jl</code>, execute it as follows</p><pre><code class="language-julia hljs">julia&gt; include(&quot;hello.jl&quot;)</code></pre><div class="admonition is-warning"><header class="admonition-header">Warning</header><div class="admonition-body"><p>Make sure that the file <code>&quot;hello.jl&quot;</code> is located in the current working directory of your Julia session. You can query the current directory with function <code>pwd()</code>. You can change to another directory with function <code>cd()</code> if needed. Also, make sure that the file extension is <code>.jl</code>.</p></div></div><p>The recommended way of running Julia code is using the REPL as we did. But it is also possible to run code directly from the system command line. To this end, open a terminal and call Julia followed by the path to the file containing the code you want to execute.</p><pre><code class="nohighlight hljs">$ julia hello.jl</code></pre><p>The previous line assumes that you have Julia properly installed in the system and that it&#39;s usable from the terminal. In UNIX systems (Linux and Mac), the Julia binary needs to be in one of the directories listed in the <code>PATH</code> environment variable. To check that Julia is properly installed, you can use</p><pre><code class="nohighlight hljs">$ julia --version</code></pre><p>If this runs without error and you see a version number, you are good to go!</p><p>You can also run julia code from the terminal using the <code>-e</code> flag:</p><pre><code class="nohighlight hljs">$ julia -e &#39;println(&quot;Hello, world!&quot;)&#39;</code></pre><div class="admonition is-info"><header class="admonition-header">Note</header><div class="admonition-body"><p>In this tutorial, when a code snipped starts with <code>$</code>, it should be run in the terminal. Otherwise, the code is to be run in the Julia REPL.</p></div></div><div class="admonition is-success"><header class="admonition-header">Tip</header><div class="admonition-body"><p>Avoid calling Julia code from the terminal, use the Julia REPL instead! Each time you call Julia from the terminal, you start a fresh Julia session and Julia will need to compile your code from scratch. This can be time consuming for large projects. In contrast, if you execute code in the REPL, Julia will compile code incrementally, which is much faster. Running code in a cluster (like in DAS-5 for the Julia assignment) is among the few situations you need to run Julia code from the terminal. Visit this link (<a href="https://docs.julialang.org/en/v1/manual/workflow-tips/">Julia workflow tips</a>) from the official Julia documentation for further information about how to develop Julia code effectivelly.</p></div></div><h3 id="Running-parallel-code"><a class="docs-heading-anchor" href="#Running-parallel-code">Running parallel code</a><a id="Running-parallel-code-1"></a><a class="docs-heading-anchor-permalink" href="#Running-parallel-code" title="Permalink"></a></h3><p>Since we are in a parallel computing course, let&#39;s run a parallel &quot;Hello world&quot; example in Julia. Open a Julia REPL and write</p><pre><code class="language-julia hljs">julia&gt; using Distributed
julia&gt; @everywhere println(&quot;Hello, world! I am proc $(myid()) from $(nprocs())&quot;)</code></pre><p>Here, we are using the <code>Distributed</code> package, which is part of the Julia standard library that provides distributed memory parallel support. The code prints the process id and the number of processes in the current Julia session.</p><p>You will probably only see output from 1 process. We need to add more processes to run the example in parallel. This is done with the <code>addprocs</code> function.</p><pre><code class="language-julia hljs">julia&gt; addprocs(3)</code></pre><p>We have added 3 new processes. Plus the old one, we have 4 processes. Run the code again.</p><pre><code class="language-julia hljs">julia&gt; @everywhere println(&quot;Hello, world! I am proc $(myid()) from $(nprocs())&quot;)</code></pre><p>Now, you should see output from 4 processes.</p><p>It is possible to specify the number of processes when starting Julia from the terminal with the <code>-p</code> argument (useful, e.g., when running in a cluster). If you launch Julia from the terminal as</p><pre><code class="nohighlight hljs">$ julia -p 3</code></pre><p>and then run</p><pre><code class="language-julia hljs">julia&gt; @everywhere println(&quot;Hello, world! I am proc $(myid()) from $(nprocs())&quot;)</code></pre><p>You should get output from 4 processes as before.</p><h3 id="Installing-packages"><a class="docs-heading-anchor" href="#Installing-packages">Installing packages</a><a id="Installing-packages-1"></a><a class="docs-heading-anchor-permalink" href="#Installing-packages" title="Permalink"></a></h3><p>One of the most useful features of Julia is its package manager. It allows one to install Julia packages in a straightforward and platform independent way. To illustrate this, let us consider the following parallel &quot;Hello world&quot; example. This example uses the Message Passing Interface (MPI). We will learn more about MPI later in the course.</p><p>Copy the following block of code into a new file named <code>&quot;hello_mpi.jl&quot;</code></p><pre><code class="language-julia hljs"># file hello_mpi.jl
using MPI
MPI.Init()
comm = MPI.COMM_WORLD
rank = MPI.Comm_rank(comm)
nranks = MPI.Comm_size(comm)
println(&quot;Hello world, I am rank $rank of $nranks&quot;)</code></pre><p>As you can see from this example, one can access MPI from Julia in a clean way, without type annotations and other complexities of C/C++ code.</p><p>Now, run the file from the REPL</p><pre><code class="language-julia hljs">julia&gt; include(&quot;hello_mpi.jl&quot;)</code></pre><p>It probably didn&#39;t work, right? Read the error message and note that the MPI package needs to be installed to run this code.</p><p>To install a package, we need to enter <em>package</em> mode. Remember that we entered into help mode by typing <code>?</code>. Package mode is activated by typing <code>]</code> : </p><pre><code class="language-julia hljs">julia&gt; ]</code></pre><p>At this point, the prompt should have changed to <code>(@v1.10) pkg&gt;</code> indicating that we are in package mode. The text between the parentheses indicates which is the active <em>project</em>, i.e., where packages are going to be installed. In this case, we are working with the global project associated with our Julia installation (which is Julia 1.10 in this example, but it can be another version in your case).</p><p>To install the MPI package, type</p><pre><code class="language-julia hljs">(@v1.10) pkg&gt; add MPI</code></pre><p>Congrats, you have installed MPI!</p><div class="admonition is-info"><header class="admonition-header">Note</header><div class="admonition-body"><p>Many Julia package names end with <code>.jl</code>. This is just a way of signaling that a package is written in Julia. When using such packages, the <code>.jl</code> needs to be omitted. In this case, we have installed the <code>MPI.jl</code> package even though we have only typed <code>MPI</code> in the REPL.</p></div></div><div class="admonition is-info"><header class="admonition-header">Note</header><div class="admonition-body"><p>The package you have installed is the Julia interface to MPI, called <code>MPI.jl</code>. 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 <a href="https://juliaparallel.org/MPI.jl/stable/configuration/">documentation</a> of <code>MPI.jl</code> for further details.</p></div></div><p>To check that the package was installed properly, exit package mode by pressing the backspace key several times, and run it again</p><pre><code class="language-julia hljs">julia&gt; include(&quot;hello_mpi.jl&quot;)</code></pre><p>Now, it should work, but you probably get output from a single MPI rank only.</p><h3 id="Running-MPI-code"><a class="docs-heading-anchor" href="#Running-MPI-code">Running MPI code</a><a id="Running-MPI-code-1"></a><a class="docs-heading-anchor-permalink" href="#Running-MPI-code" title="Permalink"></a></h3><p>To run MPI applications in parallel, you need a launcher like <code>mpiexec</code>. MPI codes written in Julia are not an exception to this rule. From the system terminal, you can run</p><pre><code class="nohighlight hljs">$ mpiexec -np 4 julia hello_mpi.jl</code></pre><p>But it will probably not work since the version of <code>mpiexec</code> needs to match with the MPI version we are using from Julia. Don&#39;t worry if you could not make it work! A more elegant way to run MPI code is from the Julia REPL directly, by using these commands:</p><pre><code class="language-julia hljs">julia&gt; using MPI
julia&gt; run(`$(mpiexec()) -np 4 julia hello_mpi.jl`)</code></pre><p>Now, you should see output from 4 ranks.</p><h2 id="Package-manager"><a class="docs-heading-anchor" href="#Package-manager">Package manager</a><a id="Package-manager-1"></a><a class="docs-heading-anchor-permalink" href="#Package-manager" title="Permalink"></a></h2><h3 id="Installing-packages-locally"><a class="docs-heading-anchor" href="#Installing-packages-locally">Installing packages locally</a><a id="Installing-packages-locally-1"></a><a class="docs-heading-anchor-permalink" href="#Installing-packages-locally" title="Permalink"></a></h3><p>We have installed the <code>MPI</code> package globally and it will be available in all Julia sessions. However, in some situations, we want to work with different versions of the same package or to install packages in an isolated way to avoid potential conflicts with other packages. This can be done by using local projects.</p><p>A project is simply a folder in your file system. To use a particular folder as your project, you need to <em>activate</em> it. This is done by entering package mode and using the <code>activate</code> command followed by the path to the folder you want to activate.</p><pre><code class="language-julia hljs">(@v1.10) pkg&gt; activate .</code></pre><p>The previous command will activate the current working directory. Note that the dot <code>.</code> is indeed the path to the current folder.</p><p>The prompt has changed to <code>(lessons) pkg&gt;</code> indicating that we are in the project within the <code>lessons</code> folder. The particular folder name can be different in your case.</p><div class="admonition is-success"><header class="admonition-header">Tip</header><div class="admonition-body"><p>You can activate a project directly when opening Julia from the terminal using the <code>--project</code> flag. The command <code>$ julia --project=.</code> will open Julia and activate a project in the current directory. You can also achieve the same effect by setting the environment variable <code>JULIA_PROJECT</code> with the path of the folder you want to activate.</p></div></div><div class="admonition is-info"><header class="admonition-header">Note</header><div class="admonition-body"><p>The active project folder and the current working directory are two independent concepts! For instance, <code>(@v1.10) pkg&gt; activate folderB</code> and then <code>julia&gt; cd(&quot;folderA&quot;)</code>, will activate the project in <code>folderB</code> and change the current working directory to <code>folderA</code>.</p></div></div><p>At this point all package-related operations will be local to the new project. For instance, install the <code>DataFrames</code> package.</p><pre><code class="language-julia hljs">(lessons) pkg&gt; add DataFrames</code></pre><p>Use the package to check that it is installed</p><pre><code class="language-julia hljs">julia&gt; using DataFrames
julia&gt; DataFrame(a=[1,2],b=[3,4])</code></pre><p>Now, we can return to the global project to check that <code>DataFrames</code> has not been installed there. To return to the global environment, use <code>activate</code> without a folder name.</p><pre><code class="language-julia hljs">(lessons) pkg&gt; activate</code></pre><p>The prompt is again <code>(@v1.10) pkg&gt;</code></p><p>Now, try to use <code>DataFrames</code>.</p><pre><code class="language-julia hljs">julia&gt; using DataFrames
julia&gt; DataFrame(a=[1,2],b=[3,4])</code></pre><p>You should get an error or a warning unless you already had <code>DataFrames</code> installed globally.</p><h3 id="Project-and-Manifest-files"><a class="docs-heading-anchor" href="#Project-and-Manifest-files">Project and Manifest files</a><a id="Project-and-Manifest-files-1"></a><a class="docs-heading-anchor-permalink" href="#Project-and-Manifest-files" title="Permalink"></a></h3><p>The information about a project is stored in two files <code>Project.toml</code> and <code>Manifest.toml</code>.</p><ul><li><p><code>Project.toml</code> contains the packages explicitly installed (the direct dependencies)</p></li><li><p><code>Manifest.toml</code> contains direct and indirect dependencies along with the concrete version of each package.</p></li></ul><p>In other words, <code>Project.toml</code> contains the packages relevant for the user, whereas <code>Manifest.toml</code> is the detailed snapshot of all dependencies. The <code>Manifest.toml</code> can be used to reproduce the same environment in another machine.</p><p>You can see the path to the current <code>Project.toml</code> file by using the <code>status</code> operator (or <code>st</code> in its short form) while in package mode</p><pre><code class="language-julia hljs">(@v1.10) pkg&gt; status</code></pre><p>The information about the <code>Manifest.toml</code> can be inspected by passing the <code>-m</code> flag.</p><pre><code class="language-julia hljs">(@v1.10) pkg&gt; status -m</code></pre><h3 id="Installing-packages-from-a-project-file"><a class="docs-heading-anchor" href="#Installing-packages-from-a-project-file">Installing packages from a project file</a><a id="Installing-packages-from-a-project-file-1"></a><a class="docs-heading-anchor-permalink" href="#Installing-packages-from-a-project-file" title="Permalink"></a></h3><p>Project files can be used to install lists of packages defined by others. E.g., to install all the dependencies of a Julia application.</p><p>Assume that a colleague has sent to you a <code>Project.toml</code> file with this content:</p><pre><code class="nohighlight hljs">[deps]
BenchmarkTools = &quot;6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf&quot;
DataFrames = &quot;a93c6f00-e57d-5684-b7b6-d8193f3e46c0&quot;
MPI = &quot;da04e1cc-30fd-572f-bb4f-1f8673147195&quot;</code></pre><p>Copy the contents of previous code block into a file called <code>Project.toml</code> and place it in an empty folder named <code>newproject</code>. It is important that the file is named <code>Project.toml</code>. You can create a new folder from the REPL with</p><pre><code class="language-julia hljs">julia&gt; mkdir(&quot;newproject&quot;)</code></pre><p>To install all the packages registered in this file you need to activate the folder containing your <code>Project.toml</code> file</p><pre><code class="language-julia hljs">(@v1.10) pkg&gt; activate newproject</code></pre><p>and then <em>instantiating</em> it</p><pre><code class="language-julia hljs">(newproject) pkg&gt; instantiate</code></pre><p>The instantiate command will download and install all listed packages and their dependencies in just one click.</p><h3 id="Getting-help-in-package-mode"><a class="docs-heading-anchor" href="#Getting-help-in-package-mode">Getting help in package mode</a><a id="Getting-help-in-package-mode-1"></a><a class="docs-heading-anchor-permalink" href="#Getting-help-in-package-mode" title="Permalink"></a></h3><p>You can get help about a particular package operator by writing <code>help</code> in front of it</p><pre><code class="language-julia hljs">(@v1.10) pkg&gt; help activate</code></pre><p>You can get an overview of all package commands by typing <code>help</code> alone</p><pre><code class="language-julia hljs">(@v1.10) pkg&gt; help</code></pre><h3 id="Package-operations-in-Julia-code"><a class="docs-heading-anchor" href="#Package-operations-in-Julia-code">Package operations in Julia code</a><a id="Package-operations-in-Julia-code-1"></a><a class="docs-heading-anchor-permalink" href="#Package-operations-in-Julia-code" title="Permalink"></a></h3><p>In some situations it is required to use package commands in Julia code, e.g., to automatize installation and deployment of Julia applications. This can be done using the <code>Pkg</code> package. For instance</p><pre><code class="language-julia hljs">julia&gt; using Pkg
julia&gt; Pkg.status()</code></pre><p>is equivalent to calling <code>status</code> in package mode.</p><pre><code class="language-julia hljs">(@v1.10) pkg&gt; status</code></pre><h3 id="Creating-you-own-package"><a class="docs-heading-anchor" href="#Creating-you-own-package">Creating you own package</a><a id="Creating-you-own-package-1"></a><a class="docs-heading-anchor-permalink" href="#Creating-you-own-package" title="Permalink"></a></h3><p>In many situations, it is useful to create your own package, for instance, when working with a large code base, when you want to reduce compilation latency using <a href="https://github.com/timholy/Revise.jl"><code>Revise.jl</code></a>, or if you want to eventually <a href="https://github.com/JuliaRegistries/Registrator.jl">register your package</a> and share it with others.</p><p>The simplest way of generating a package (called <code>MyPackage</code>) is as follows. Open Julia, go to package mode, and type</p><pre><code class="language-julia hljs">(@v1.10) pkg&gt; generate MyPackage</code></pre><p>This will crate a minimal package consisting of a new folder <code>MyPackage</code> with two files:</p><ul><li><code>MyPackage/Project.toml</code>: Project file defining the direct dependencies of your package.</li><li><code>MyPackage/src/MyPackage.jl</code>: Main source file of your package. You can split your code in several files if needed, and include them in the package main file using function <code>include</code>.</li></ul><div class="admonition is-success"><header class="admonition-header">Tip</header><div class="admonition-body"><p>This approach only generates a very minimal package. To create a more sophisticated package skeleton (including unit testing, code coverage, readme file, licence, etc.) use <a href="https://github.com/JuliaCI/PkgTemplates.jl"><code>PkgTemplates.jl</code></a> or <a href="https://github.com/abelsiqueira/BestieTemplate.jl"><code>BestieTemplate.jl</code></a>. The later one is developed in Amsterdam at the <a href="https://www.esciencecenter.nl/">Netherlands eScience Center</a>.</p></div></div><p>You can add dependencies to the package by activating the <code>MyPackage</code> folder in package mode and adding new dependencies as always:</p><pre><code class="language-julia hljs">(@v1.10) pkg&gt; activate MyPackage
(MyPackage) pkg&gt; add MPI</code></pre><p>This will add MPI to your package dependencies.</p><h3 id="Using-your-own-package"><a class="docs-heading-anchor" href="#Using-your-own-package">Using your own package</a><a id="Using-your-own-package-1"></a><a class="docs-heading-anchor-permalink" href="#Using-your-own-package" title="Permalink"></a></h3><p>To use your package you first need to add it to a package environment of your choice. This is done by changing to package mode and typing <code>develop</code> followed by the path to the folder containing the package. For instance:</p><pre><code class="language-julia hljs">(@v1.10) pkg&gt; develop MyPackage</code></pre><div class="admonition is-info"><header class="admonition-header">Note</header><div class="admonition-body"><p>You do not need to &quot;develop&quot; your package if you activated the package folder <code>MyPackage</code>.</p></div></div><p>Now, we can go back to standard Julia mode and use it as any other package:</p><pre><code class="language-julia hljs">using MyPackage
MyPackage.greet()</code></pre><p>Here, we just called the example function defined in <code>MyPackage/src/MyPackage.jl</code>.</p><h2 id="Conclusion"><a class="docs-heading-anchor" href="#Conclusion">Conclusion</a><a id="Conclusion-1"></a><a class="docs-heading-anchor-permalink" href="#Conclusion" title="Permalink"></a></h2><p>We have learned the basics of how to work with Julia, including how to run serial and parallel code, and how to manage, create, and use Julia packages. This knowledge will allow you to follow the course effectively! If you want to further dig into the topics we have covered here, you can take a look at the following links:</p><ul><li><a href="https://docs.julialang.org/en/v1/manual/getting-started/">Julia Manual</a></li><li><a href="https://pkgdocs.julialang.org/v1/getting-started/">Package manager</a></li></ul></article><nav class="docs-footer"><a class="docs-footer-prevpage" href="../">« Home</a><a class="docs-footer-nextpage" href="../julia_basics/">Julia Basics »</a><div class="flexbox-break"></div><p class="footer-message">Powered by <a href="https://github.com/JuliaDocs/Documenter.jl">Documenter.jl</a> and the <a href="https://julialang.org/">Julia Programming Language</a>.</p></nav></div><div class="modal" id="documenter-settings"><div class="modal-background"></div><div class="modal-card"><header class="modal-card-head"><p class="modal-card-title">Settings</p><button class="delete"></button></header><section class="modal-card-body"><p><label class="label">Theme</label><div class="select"><select id="documenter-themepicker"><option value="auto">Automatic (OS)</option><option value="documenter-light">documenter-light</option><option value="documenter-dark">documenter-dark</option><option value="catppuccin-latte">catppuccin-latte</option><option value="catppuccin-frappe">catppuccin-frappe</option><option value="catppuccin-macchiato">catppuccin-macchiato</option><option value="catppuccin-mocha">catppuccin-mocha</option></select></div></p><hr/><p>This document was generated with <a href="https://github.com/JuliaDocs/Documenter.jl">Documenter.jl</a> version 1.7.0 on <span class="colophon-date" title="Thursday 12 September 2024 09:27">Thursday 12 September 2024</span>. Using Julia version 1.10.5.</p></section><footer class="modal-card-foot"></footer></div></div></div></body></html>