<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.bwhpc.de/wiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=P+Hematty</id>
	<title>bwHPC Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.bwhpc.de/wiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=P+Hematty"/>
	<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/e/Special:Contributions/P_Hematty"/>
	<updated>2026-04-06T20:04:16Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.39.17</generator>
	<entry>
		<id>https://wiki.bwhpc.de/wiki/index.php?title=Helix/Software/Matlab&amp;diff=15793</id>
		<title>Helix/Software/Matlab</title>
		<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=Helix/Software/Matlab&amp;diff=15793"/>
		<updated>2026-03-05T12:18:56Z</updated>

		<summary type="html">&lt;p&gt;P Hematty: added MATLAB Runtime instructions&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{| style=&amp;quot;border: 2px solid #d33; background-color: #fee7e6; padding: 10px; margin-bottom: 1em; width: 100%;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;⚠ MATLAB modules will be removed on March 31, 2026.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The MathWorks state license (Landeslizenz) expires on March 31, 2026 and will not be renewed. After this date, the MATLAB modules under &amp;lt;code&amp;gt;math/matlab&amp;lt;/code&amp;gt; will no longer be available on Helix.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Alternatives:&#039;&#039;&#039;&lt;br /&gt;
* &#039;&#039;&#039;GNU Octave&#039;&#039;&#039; (&amp;lt;code&amp;gt;module load math/octave&amp;lt;/code&amp;gt;): MATLAB scripts that do not use special features of toolboxes may run with Octave without or with minor changes.&lt;br /&gt;
* &#039;&#039;&#039;Matlab Compiler / Matlab Runtime&#039;&#039;&#039;: Use the [https://de.mathworks.com/products/compiler.html MATLAB Compiler] to build stand-alone binaries on your local computer and run them with the MATLAB Runtime on the cluster — no license required at runtime (see section &amp;quot;Compile MATLAB binaries with mcc&amp;quot; below).&lt;br /&gt;
* &#039;&#039;&#039;Institute license&#039;&#039;&#039;: If your institute has its own MathWorks network license, it may be possible to use it from the cluster. This must be checked on a case-by-case basis — please [https://www.bwhpc.de/supportportal.php submit a ticket].&lt;br /&gt;
* &#039;&#039;&#039;Heidelberg users&#039;&#039;&#039;: Uni Heidelberg may provide MATLAB through a local university license in the future. If available, it will be accessible via a separate module path.&lt;br /&gt;
* Other alternatives: &amp;lt;code&amp;gt;math/julia&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;math/R&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;devel/python&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
{{Softwarepage|math/matlab}}&lt;br /&gt;
&lt;br /&gt;
{| width=600px class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Description !! Content&lt;br /&gt;
|-&lt;br /&gt;
| module load&lt;br /&gt;
| math/matlab&lt;br /&gt;
|-&lt;br /&gt;
| License&lt;br /&gt;
| [https://de.mathworks.com/pricing-licensing/index.html?intendeduse=edu&amp;amp;prodcode=ML Academic License/Commercial]&lt;br /&gt;
|-&lt;br /&gt;
| Citing&lt;br /&gt;
| n/a&lt;br /&gt;
|-&lt;br /&gt;
| Links&lt;br /&gt;
| [https://de.mathworks.com/products/matlab/ MATLAB Homepage] &amp;amp;#124; [https://de.mathworks.com/index.html?s_tid=gn_logo MathWorks Homepage] &amp;amp;#124; [https://de.mathworks.com/support/?s_tid=gn_supp Support and more]&lt;br /&gt;
|-&lt;br /&gt;
| Graphical Interface&lt;br /&gt;
| No&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Description =&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;MATLAB&#039;&#039;&#039; (MATrix LABoratory) is a high-level programming language and interactive computing environment for numerical calculation and data visualization.&lt;br /&gt;
&lt;br /&gt;
= Loading MATLAB =&lt;br /&gt;
&lt;br /&gt;
The preferable way is to run the MATLAB command line interface without GUI:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ matlab -nodisplay&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An interactive MATLAB session with graphical user interface (GUI) can be started with the command (requires X11 forwarding enabled for your ssh login):&lt;br /&gt;
&amp;lt;pre&amp;gt;$ matlab&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: Do not start a long-duration interactive MATLAB session on a login node of the cluster. Submit an [[Helix/Slurm#Interactive_Jobs | interactive job]] and start MATLAB from within the dedicated compute node assigned to you by the queueing system.&lt;br /&gt;
&lt;br /&gt;
The following generic command will execute a MATLAB script or function named &amp;quot;example&amp;quot;:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ matlab -nodisplay -batch example &amp;gt; result.out 2&amp;gt;&amp;amp;1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of this session will be redirected to the file result.out. The option &amp;lt;syntaxhighlight style=&amp;quot;border:0px&amp;quot; inline=1&amp;gt;-batch&amp;lt;/syntaxhighlight&amp;gt; executes the MATLAB statement non-interactively.&lt;br /&gt;
&lt;br /&gt;
= Parallel Computing Using MATLAB =&lt;br /&gt;
&lt;br /&gt;
Parallelization of MATLAB jobs is realized via the built-in multi-threading provided by MATLAB&#039;s BLAS and FFT implementation and the parallel computing functionality of MATLAB&#039;s Parallel Computing Toolbox (PCT).&lt;br /&gt;
&lt;br /&gt;
== Implicit Threading ==&lt;br /&gt;
&lt;br /&gt;
A large number of built-in MATLAB functions may utilize multiple cores automatically without any code modifications required. This is referred to as implicit multi-threading and must be strictly distinguished from explicit parallelism provided by the Parallel Computing Toolbox (PCT) which requires specific commands in your code in order to create threads.&lt;br /&gt;
&lt;br /&gt;
Implicit threading particularly takes place for linear algebra operations (such as the solution to a linear system A\b or matrix products A*B) and FFT operations. Many other high-level MATLAB functions do also benefit from multi-threading capabilities of their underlying routines. If multi-threading is not desired, single-threading can be enforced by adding the command line option &amp;lt;syntaxhighlight style=&amp;quot;border:0px&amp;quot; inline=1&amp;gt;-singleCompThread&amp;lt;/syntaxhighlight&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Whenever implicit threading takes place, MATLAB will detect the total number of cores that exist on a machine and by default makes use of all of them. This has very important implications for MATLAB jobs in HPC environments with shared-node job scheduling policy (i.e. with multiple users sharing one compute node). Due to this behaviour, a MATLAB job may take over more compute resources than assigned by the queueing system of the cluster (and thereby taking away these resources from all other users with running jobs on the same node - including your own jobs).&lt;br /&gt;
&lt;br /&gt;
Therefore, when running in multi-threaded mode, MATLAB always requires the user&#039;s intervention to not allocate all cores of the machine (unless requested so from the queueing system). The number of threads must be controlled from within the code by means of the &amp;lt;syntaxhighlight style=&amp;quot;border:0px&amp;quot; inline=1&amp;gt;maxNumCompThreads(N)&amp;lt;/syntaxhighlight&amp;gt; function or, alternatively, with the &amp;lt;syntaxhighlight style=&amp;quot;border:0px&amp;quot; inline=1&amp;gt;feature(&#039;numThreads&#039;, N)&amp;lt;/syntaxhighlight&amp;gt; function (which is undocumented).&lt;br /&gt;
&lt;br /&gt;
== Using the Parallel Computing Toolbox (PCT) ==&lt;br /&gt;
&lt;br /&gt;
By using the PCT one can make explicit use of several cores on multicore processors to parallelize MATLAB applications without MPI programming. Under MATLAB version 8.4 and earlier, this toolbox provides 12 workers (MATLAB computational engines) to execute applications locally on a single multicore node. Under MATLAB version 8.5 and later, the number of workers available is equal to the number of cores on a single node (up to a maximum of 512).&lt;br /&gt;
&lt;br /&gt;
If multiple PCT jobs are running at the same time, they all write temporary MATLAB job information to the same location. This race condition can cause one or more of the parallel MATLAB jobs fail to use the parallel functionality of the toolbox.&lt;br /&gt;
&lt;br /&gt;
To solve this issue, each MATLAB job should explicitly set a unique location where these files are created. This can be accomplished by the following snippet of code added to your MATLAB script.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% create a local cluster object&lt;br /&gt;
pc = parcluster(&#039;local&#039;)&lt;br /&gt;
&lt;br /&gt;
% get the number of dedicated cores from environment&lt;br /&gt;
nprocs = str2num(getenv(&#039;SLURM_NPROCS&#039;))&lt;br /&gt;
&lt;br /&gt;
% you may explicitly set the JobStorageLocation to the tmp directory that is unique to each cluster job (and is on local, fast scratch)&lt;br /&gt;
parpool_tmpdir = [getenv(&#039;TMP&#039;),&#039;/.matlab/local_cluster_jobs/slurm_jobID_&#039;,getenv(&#039;SLURM_JOB_ID&#039;)]&lt;br /&gt;
mkdir(parpool_tmpdir)&lt;br /&gt;
pc.JobStorageLocation = parpool_tmpdir&lt;br /&gt;
&lt;br /&gt;
% start the parallel pool&lt;br /&gt;
parpool(pc, nprocs)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If a large number of MATLAB-jobs are run in parallel, they can also conflict when writing generic information to &amp;lt;syntaxhighlight style=&amp;quot;border:0px&amp;quot; inline=1&amp;gt;~/.matlab&amp;lt;/syntaxhighlight&amp;gt;. This can be circumvented by setting &amp;lt;syntaxhighlight style=&amp;quot;border:0px&amp;quot; inline=1&amp;gt;$MATLAB_PREFDIR&amp;lt;/syntaxhighlight&amp;gt; to different directories in your Batch-script, e.g.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;export MATLAB_PREFDIR=$TMP&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Using a different implementation of BLAS/LAPACK ==&lt;br /&gt;
&lt;br /&gt;
By default, Matlab uses a version of Intel MKL as its BLAS/LAPACK library. It is possible to manually change this to different libraries by setting the &amp;lt;syntaxhighlight style=&amp;quot;border:0px&amp;quot; inline=1&amp;gt;BLAS_VERSION&amp;lt;/syntaxhighlight&amp;gt; and &amp;lt;syntaxhighlight style=&amp;quot;border:0px&amp;quot; inline=1&amp;gt;LAPACK_VERSION&amp;lt;/syntaxhighlight&amp;gt; environment variables. The following lines can be added to the batch-script to change it, in this example to BLIS and Flame, which are optimized for AMD processors: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module load numlib/aocl/3.2.0&lt;br /&gt;
&lt;br /&gt;
export BLAS_VERSION=$AOCL_LIB_DIR/libblis-mt.so&lt;br /&gt;
export LAPACK_VERSION=$AOCL_LIB_DIR/libflame.so&lt;br /&gt;
&lt;br /&gt;
export BLIS_NUM_THREADS=$SLURM_NTASKS&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can increase performance depending on the task, for example large matrix multiplications, but caution is advised.&lt;br /&gt;
&lt;br /&gt;
= General Performance Tips for MATLAB =&lt;br /&gt;
&lt;br /&gt;
MATLAB data structures (arrays or matrices) are dynamic in size, i.e. MATLAB will automatically resize the structure on demand. Although this seems to be convenient, MATLAB continually needs to allocate a new chunk of memory and copy over the data to the new block of memory as the array or matrix grows in a loop. This may take a significant amount of extra time during execution of the program.&lt;br /&gt;
&lt;br /&gt;
Code performance can often be drastically improved by pre-allocating memory for the final expected size of the array or matrix before actually starting the processing loop. In order to pre-allocate an array of strings, you can use MATLAB&#039;s build-in cell function. In order to pre-allocate an array or matrix of numbers, you can use MATLAB&#039;s build-in zeros function.&lt;br /&gt;
&lt;br /&gt;
The performance benefit of pre-allocation is illustrated with the following example code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% prealloc.m&lt;br /&gt;
&lt;br /&gt;
clear all;&lt;br /&gt;
&lt;br /&gt;
num=10000000;&lt;br /&gt;
&lt;br /&gt;
disp(&#039;Without pre-allocation:&#039;)&lt;br /&gt;
tic&lt;br /&gt;
for i=1:num&lt;br /&gt;
    a(i)=i;&lt;br /&gt;
end&lt;br /&gt;
toc&lt;br /&gt;
&lt;br /&gt;
disp(&#039;With pre-allocation:&#039;)&lt;br /&gt;
tic&lt;br /&gt;
b=zeros(1,num);&lt;br /&gt;
for i=1:num&lt;br /&gt;
    b(i)=i;&lt;br /&gt;
end&lt;br /&gt;
toc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On a compute node, the result may look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Without pre-allocation:&lt;br /&gt;
Elapsed time is 2.879446 seconds.&lt;br /&gt;
With pre-allocation:&lt;br /&gt;
Elapsed time is 0.097557 seconds.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Please recognize that the code runs almost 30 times faster with pre-allocation.&lt;br /&gt;
&lt;br /&gt;
== Compile MATLAB binaries with mcc ==&lt;br /&gt;
&lt;br /&gt;
MATLAB on Helix comes with a compiler, &amp;lt;code&amp;gt;mcc&amp;lt;/code&amp;gt;, that can be used to create binaries from MATLAB-code.&lt;br /&gt;
Stand-alone MATLAB programs compiled with &amp;lt;code&amp;gt;mcc&amp;lt;/code&amp;gt; do not require any license tokens at runtime and you can start jobs in parallel without any risk of running out of licences.&lt;br /&gt;
&lt;br /&gt;
=== Running compiled binaries with the MATLAB Runtime ===&lt;br /&gt;
&lt;br /&gt;
If you do not have access to a full MATLAB installation on Helix, you can still run compiled MATLAB applications using the MATLAB Runtime module:&lt;br /&gt;
&lt;br /&gt;
 module load math/matlab-runtime&lt;br /&gt;
 ./my_compiled_app&lt;br /&gt;
&lt;br /&gt;
The MATLAB Runtime requires no license. To use it:&lt;br /&gt;
&lt;br /&gt;
# Compile your MATLAB code on a machine where you have a MATLAB license (e.g., your local workstation via an institute license):&lt;br /&gt;
#: &amp;lt;code&amp;gt;mcc -m my_script.m&amp;lt;/code&amp;gt;&lt;br /&gt;
# Transfer the compiled binary to Helix.&lt;br /&gt;
# Load the matching Runtime module and run:&lt;br /&gt;
#: &amp;lt;code&amp;gt;module load math/matlab-runtime/R2025a&amp;lt;/code&amp;gt;&lt;br /&gt;
#: &amp;lt;code&amp;gt;./my_script&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Important:&#039;&#039;&#039; The Runtime version must &#039;&#039;&#039;exactly&#039;&#039;&#039; match the MATLAB version used for compilation. For example, code compiled with R2023a requires &amp;lt;code&amp;gt;math/matlab-runtime/R2023a&amp;lt;/code&amp;gt;. Available versions:&lt;br /&gt;
&lt;br /&gt;
 module avail math/matlab-runtime&lt;br /&gt;
&lt;br /&gt;
Currently installed: R2022a, R2023a, R2024a, R2025a.&lt;br /&gt;
&lt;br /&gt;
The MATLAB Runtime is [https://de.mathworks.com/products/compiler/matlab-runtime.html freely available from MathWorks] and can also be installed locally if needed.&lt;/div&gt;</summary>
		<author><name>P Hematty</name></author>
	</entry>
	<entry>
		<id>https://wiki.bwhpc.de/wiki/index.php?title=Helix/Software/Matlab&amp;diff=15792</id>
		<title>Helix/Software/Matlab</title>
		<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=Helix/Software/Matlab&amp;diff=15792"/>
		<updated>2026-03-03T08:10:51Z</updated>

		<summary type="html">&lt;p&gt;P Hematty: Deprecation Note für allgemeinen Matlab Zugang&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{| style=&amp;quot;border: 2px solid #d33; background-color: #fee7e6; padding: 10px; margin-bottom: 1em; width: 100%;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;⚠ MATLAB modules will be removed on March 31, 2026.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The MathWorks state license (Landeslizenz) expires on March 31, 2026 and will not be renewed. After this date, the MATLAB modules under &amp;lt;code&amp;gt;math/matlab&amp;lt;/code&amp;gt; will no longer be available on Helix.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Alternatives:&#039;&#039;&#039;&lt;br /&gt;
* &#039;&#039;&#039;GNU Octave&#039;&#039;&#039; (&amp;lt;code&amp;gt;module load math/octave&amp;lt;/code&amp;gt;): MATLAB scripts that do not use special features of toolboxes may run with Octave without or with minor changes.&lt;br /&gt;
* &#039;&#039;&#039;Matlab Compiler / Matlab Runtime&#039;&#039;&#039;: Use the [https://de.mathworks.com/products/compiler.html MATLAB Compiler] to build stand-alone binaries on your local computer and run them with the MATLAB Runtime on the cluster — no license required at runtime (see section &amp;quot;Compile MATLAB binaries with mcc&amp;quot; below).&lt;br /&gt;
* &#039;&#039;&#039;Institute license&#039;&#039;&#039;: If your institute has its own MathWorks network license, it may be possible to use it from the cluster. This must be checked on a case-by-case basis — please [https://www.bwhpc.de/supportportal.php submit a ticket].&lt;br /&gt;
* &#039;&#039;&#039;Heidelberg users&#039;&#039;&#039;: Uni Heidelberg may provide MATLAB through a local university license in the future. If available, it will be accessible via a separate module path.&lt;br /&gt;
* Other alternatives: &amp;lt;code&amp;gt;math/julia&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;math/R&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;devel/python&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
{{Softwarepage|math/matlab}}&lt;br /&gt;
&lt;br /&gt;
{| width=600px class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Description !! Content&lt;br /&gt;
|-&lt;br /&gt;
| module load&lt;br /&gt;
| math/matlab&lt;br /&gt;
|-&lt;br /&gt;
| License&lt;br /&gt;
| [https://de.mathworks.com/pricing-licensing/index.html?intendeduse=edu&amp;amp;prodcode=ML Academic License/Commercial]&lt;br /&gt;
|-&lt;br /&gt;
| Citing&lt;br /&gt;
| n/a&lt;br /&gt;
|-&lt;br /&gt;
| Links&lt;br /&gt;
| [https://de.mathworks.com/products/matlab/ MATLAB Homepage] &amp;amp;#124; [https://de.mathworks.com/index.html?s_tid=gn_logo MathWorks Homepage] &amp;amp;#124; [https://de.mathworks.com/support/?s_tid=gn_supp Support and more]&lt;br /&gt;
|-&lt;br /&gt;
| Graphical Interface&lt;br /&gt;
| No&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Description =&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;MATLAB&#039;&#039;&#039; (MATrix LABoratory) is a high-level programming language and interactive computing environment for numerical calculation and data visualization.&lt;br /&gt;
&lt;br /&gt;
= Loading MATLAB =&lt;br /&gt;
&lt;br /&gt;
The preferable way is to run the MATLAB command line interface without GUI:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ matlab -nodisplay&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An interactive MATLAB session with graphical user interface (GUI) can be started with the command (requires X11 forwarding enabled for your ssh login):&lt;br /&gt;
&amp;lt;pre&amp;gt;$ matlab&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: Do not start a long-duration interactive MATLAB session on a login node of the cluster. Submit an [[Helix/Slurm#Interactive_Jobs | interactive job]] and start MATLAB from within the dedicated compute node assigned to you by the queueing system.&lt;br /&gt;
&lt;br /&gt;
The following generic command will execute a MATLAB script or function named &amp;quot;example&amp;quot;:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ matlab -nodisplay -batch example &amp;gt; result.out 2&amp;gt;&amp;amp;1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of this session will be redirected to the file result.out. The option &amp;lt;syntaxhighlight style=&amp;quot;border:0px&amp;quot; inline=1&amp;gt;-batch&amp;lt;/syntaxhighlight&amp;gt; executes the MATLAB statement non-interactively.&lt;br /&gt;
&lt;br /&gt;
= Parallel Computing Using MATLAB =&lt;br /&gt;
&lt;br /&gt;
Parallelization of MATLAB jobs is realized via the built-in multi-threading provided by MATLAB&#039;s BLAS and FFT implementation and the parallel computing functionality of MATLAB&#039;s Parallel Computing Toolbox (PCT).&lt;br /&gt;
&lt;br /&gt;
== Implicit Threading ==&lt;br /&gt;
&lt;br /&gt;
A large number of built-in MATLAB functions may utilize multiple cores automatically without any code modifications required. This is referred to as implicit multi-threading and must be strictly distinguished from explicit parallelism provided by the Parallel Computing Toolbox (PCT) which requires specific commands in your code in order to create threads.&lt;br /&gt;
&lt;br /&gt;
Implicit threading particularly takes place for linear algebra operations (such as the solution to a linear system A\b or matrix products A*B) and FFT operations. Many other high-level MATLAB functions do also benefit from multi-threading capabilities of their underlying routines. If multi-threading is not desired, single-threading can be enforced by adding the command line option &amp;lt;syntaxhighlight style=&amp;quot;border:0px&amp;quot; inline=1&amp;gt;-singleCompThread&amp;lt;/syntaxhighlight&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Whenever implicit threading takes place, MATLAB will detect the total number of cores that exist on a machine and by default makes use of all of them. This has very important implications for MATLAB jobs in HPC environments with shared-node job scheduling policy (i.e. with multiple users sharing one compute node). Due to this behaviour, a MATLAB job may take over more compute resources than assigned by the queueing system of the cluster (and thereby taking away these resources from all other users with running jobs on the same node - including your own jobs).&lt;br /&gt;
&lt;br /&gt;
Therefore, when running in multi-threaded mode, MATLAB always requires the user&#039;s intervention to not allocate all cores of the machine (unless requested so from the queueing system). The number of threads must be controlled from within the code by means of the &amp;lt;syntaxhighlight style=&amp;quot;border:0px&amp;quot; inline=1&amp;gt;maxNumCompThreads(N)&amp;lt;/syntaxhighlight&amp;gt; function or, alternatively, with the &amp;lt;syntaxhighlight style=&amp;quot;border:0px&amp;quot; inline=1&amp;gt;feature(&#039;numThreads&#039;, N)&amp;lt;/syntaxhighlight&amp;gt; function (which is undocumented).&lt;br /&gt;
&lt;br /&gt;
== Using the Parallel Computing Toolbox (PCT) ==&lt;br /&gt;
&lt;br /&gt;
By using the PCT one can make explicit use of several cores on multicore processors to parallelize MATLAB applications without MPI programming. Under MATLAB version 8.4 and earlier, this toolbox provides 12 workers (MATLAB computational engines) to execute applications locally on a single multicore node. Under MATLAB version 8.5 and later, the number of workers available is equal to the number of cores on a single node (up to a maximum of 512).&lt;br /&gt;
&lt;br /&gt;
If multiple PCT jobs are running at the same time, they all write temporary MATLAB job information to the same location. This race condition can cause one or more of the parallel MATLAB jobs fail to use the parallel functionality of the toolbox.&lt;br /&gt;
&lt;br /&gt;
To solve this issue, each MATLAB job should explicitly set a unique location where these files are created. This can be accomplished by the following snippet of code added to your MATLAB script.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% create a local cluster object&lt;br /&gt;
pc = parcluster(&#039;local&#039;)&lt;br /&gt;
&lt;br /&gt;
% get the number of dedicated cores from environment&lt;br /&gt;
nprocs = str2num(getenv(&#039;SLURM_NPROCS&#039;))&lt;br /&gt;
&lt;br /&gt;
% you may explicitly set the JobStorageLocation to the tmp directory that is unique to each cluster job (and is on local, fast scratch)&lt;br /&gt;
parpool_tmpdir = [getenv(&#039;TMP&#039;),&#039;/.matlab/local_cluster_jobs/slurm_jobID_&#039;,getenv(&#039;SLURM_JOB_ID&#039;)]&lt;br /&gt;
mkdir(parpool_tmpdir)&lt;br /&gt;
pc.JobStorageLocation = parpool_tmpdir&lt;br /&gt;
&lt;br /&gt;
% start the parallel pool&lt;br /&gt;
parpool(pc, nprocs)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If a large number of MATLAB-jobs are run in parallel, they can also conflict when writing generic information to &amp;lt;syntaxhighlight style=&amp;quot;border:0px&amp;quot; inline=1&amp;gt;~/.matlab&amp;lt;/syntaxhighlight&amp;gt;. This can be circumvented by setting &amp;lt;syntaxhighlight style=&amp;quot;border:0px&amp;quot; inline=1&amp;gt;$MATLAB_PREFDIR&amp;lt;/syntaxhighlight&amp;gt; to different directories in your Batch-script, e.g.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;export MATLAB_PREFDIR=$TMP&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Using a different implementation of BLAS/LAPACK ==&lt;br /&gt;
&lt;br /&gt;
By default, Matlab uses a version of Intel MKL as its BLAS/LAPACK library. It is possible to manually change this to different libraries by setting the &amp;lt;syntaxhighlight style=&amp;quot;border:0px&amp;quot; inline=1&amp;gt;BLAS_VERSION&amp;lt;/syntaxhighlight&amp;gt; and &amp;lt;syntaxhighlight style=&amp;quot;border:0px&amp;quot; inline=1&amp;gt;LAPACK_VERSION&amp;lt;/syntaxhighlight&amp;gt; environment variables. The following lines can be added to the batch-script to change it, in this example to BLIS and Flame, which are optimized for AMD processors: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module load numlib/aocl/3.2.0&lt;br /&gt;
&lt;br /&gt;
export BLAS_VERSION=$AOCL_LIB_DIR/libblis-mt.so&lt;br /&gt;
export LAPACK_VERSION=$AOCL_LIB_DIR/libflame.so&lt;br /&gt;
&lt;br /&gt;
export BLIS_NUM_THREADS=$SLURM_NTASKS&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can increase performance depending on the task, for example large matrix multiplications, but caution is advised.&lt;br /&gt;
&lt;br /&gt;
= General Performance Tips for MATLAB =&lt;br /&gt;
&lt;br /&gt;
MATLAB data structures (arrays or matrices) are dynamic in size, i.e. MATLAB will automatically resize the structure on demand. Although this seems to be convenient, MATLAB continually needs to allocate a new chunk of memory and copy over the data to the new block of memory as the array or matrix grows in a loop. This may take a significant amount of extra time during execution of the program.&lt;br /&gt;
&lt;br /&gt;
Code performance can often be drastically improved by pre-allocating memory for the final expected size of the array or matrix before actually starting the processing loop. In order to pre-allocate an array of strings, you can use MATLAB&#039;s build-in cell function. In order to pre-allocate an array or matrix of numbers, you can use MATLAB&#039;s build-in zeros function.&lt;br /&gt;
&lt;br /&gt;
The performance benefit of pre-allocation is illustrated with the following example code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% prealloc.m&lt;br /&gt;
&lt;br /&gt;
clear all;&lt;br /&gt;
&lt;br /&gt;
num=10000000;&lt;br /&gt;
&lt;br /&gt;
disp(&#039;Without pre-allocation:&#039;)&lt;br /&gt;
tic&lt;br /&gt;
for i=1:num&lt;br /&gt;
    a(i)=i;&lt;br /&gt;
end&lt;br /&gt;
toc&lt;br /&gt;
&lt;br /&gt;
disp(&#039;With pre-allocation:&#039;)&lt;br /&gt;
tic&lt;br /&gt;
b=zeros(1,num);&lt;br /&gt;
for i=1:num&lt;br /&gt;
    b(i)=i;&lt;br /&gt;
end&lt;br /&gt;
toc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On a compute node, the result may look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Without pre-allocation:&lt;br /&gt;
Elapsed time is 2.879446 seconds.&lt;br /&gt;
With pre-allocation:&lt;br /&gt;
Elapsed time is 0.097557 seconds.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Please recognize that the code runs almost 30 times faster with pre-allocation.&lt;br /&gt;
&lt;br /&gt;
= Compile MATLAB binaries with mcc =&lt;br /&gt;
&lt;br /&gt;
MATLAB on Helix comes with a compiler, &amp;lt;code&amp;gt;mcc&amp;lt;/code&amp;gt;, that can be used to create binaries from MATLAB-code.&lt;br /&gt;
Stand-alone MATLAB programs compiled with &amp;lt;code&amp;gt;mcc&amp;lt;/code&amp;gt; do not require any license tokens at runtime and you can start jobs in parallel without any risk of running out of licences.&lt;/div&gt;</summary>
		<author><name>P Hematty</name></author>
	</entry>
	<entry>
		<id>https://wiki.bwhpc.de/wiki/index.php?title=Development/Python&amp;diff=15314</id>
		<title>Development/Python</title>
		<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=Development/Python&amp;diff=15314"/>
		<updated>2025-10-06T11:36:43Z</updated>

		<summary type="html">&lt;p&gt;P Hematty: /* Virtual Environments and Package Management */  UV ersetzt Pixi nicht, weil Pixi auch Conda Pakete installieren kann. Daher habe ich das aus dem Beschreibungstext rausgenommen. Außerdem gibts bei pipx soweit ich weiß keine Möglichkeit was bei PyPI zu publishen, was wahrscheinlich aber auch eher ein Fehler beim Übertragen war oder so.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Python is a versatile, easy-to-learn, interpreted programming language. It offers a wide range of libraries for scientific tasks and visualization. Python counts to the best languages for machine learning. Python can be used in particular as an open source alternative for tasks that have usually been used for Matlab.&lt;br /&gt;
&lt;br /&gt;
== Installation and Versions ==&lt;br /&gt;
Python is available on all systems. With &amp;lt;code&amp;gt;python --version&amp;lt;/code&amp;gt; you can see the currently active default python version. In general, you can choose from various types of python installations:&lt;br /&gt;
* &#039;&#039;&#039;System python:&#039;&#039;&#039; This python version comes together with the operating system and is available upon login to the cluster. Other python versions might be installed along with it. All versions can be seen with &lt;br /&gt;
*: &amp;lt;code&amp;gt;ls /usr/bin/python[0-9].*[0-9] | sort -V | cut -d&amp;quot;/&amp;quot; -f4 | xargs&amp;lt;/code&amp;gt;&lt;br /&gt;
*: They can change over time. You can access a specific Python version by specifying the version in the Python command.&lt;br /&gt;
* &#039;&#039;&#039;[[Environment_Modules | Software module]]:&#039;&#039;&#039; Available versions can be identified via&lt;br /&gt;
*:  &amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
  module avail devel/python&lt;br /&gt;
  &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* &#039;&#039;&#039;Python distributions and virtual environments:&#039;&#039;&#039; By using python distributions such as Anaconda, you can easily install the needed python version into a virtual environment. For the use of conda on bwHPC clusters, please refer to [[Development/Conda|Conda]]. Alternatively, you can use more python specific tools for installing python. Some options are listed in [[#Virtual Environments and Package Management | Virtual Environments and Package Management]]&lt;br /&gt;
* &#039;&#039;&#039;[[Development/Containers | Container]]:&#039;&#039;&#039; Containers can contain their own python installation. Keep this in mind when you are working with containers provided by others.&lt;br /&gt;
&lt;br /&gt;
== Running Python Code ==&lt;br /&gt;
&lt;br /&gt;
There are three ways to run Python commands:&lt;br /&gt;
&lt;br /&gt;
* Within a &#039;&#039;&#039;terminal&#039;&#039;&#039; by executing the comand &amp;lt;code&amp;gt;python&amp;lt;/code&amp;gt;. This starts a Python shell, where all commands are evaluated by the Python interpreter. &lt;br /&gt;
* Within a &#039;&#039;&#039;script&#039;&#039;&#039; (file ends with &#039;&#039;.py&#039;&#039; and can be run with &amp;lt;code&amp;gt;python myProgram.py&amp;lt;/code&amp;gt;)&lt;br /&gt;
* Within a &#039;&#039;&#039;notebook&#039;&#039;&#039; (file ends with .ipynb). You can use other programming languages and markdown within a notebook next to your python code. Besides software development itself, teaching, prototyping and visualization are good use cases for notebooks as well.  &lt;br /&gt;
&lt;br /&gt;
== Development Environments ==&lt;br /&gt;
&lt;br /&gt;
Development Environments are usually more comfortable than running code directly from the shell. Some common options are: &lt;br /&gt;
&lt;br /&gt;
* [[Jupyter]]&lt;br /&gt;
* [[Development/VS_Code | VS Code]]&lt;br /&gt;
* PyCharm&lt;br /&gt;
&lt;br /&gt;
== Virtual Environments and Package Management ==&lt;br /&gt;
&lt;br /&gt;
Packages contain a set of functions that offer additional functionality. A package can be installed by using a package manager. Virtual environments prevent conflicts between different Python packages by using separate installation directories. &lt;br /&gt;
&lt;br /&gt;
At least one virtual environment should be defined per project. This way, it is clear which packages are needed by a specific project. All virtual environments allow to save the corresponding packages with their specific version numbers to a file. This allows to reinstall them in another place and therefore improves the reproducibility of projects. Furthermore, it makes finding and removing packages, that aren&#039;t needed anymore, easier. &lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The following table provides an overview of common tools in the field of virtual environments and package management. The main differences between the various options are highlighted. After deciding on a specific tool, it can be installed by following the given link. &lt;br /&gt;
To make it short, if you plan to use...&lt;br /&gt;
* ...Python only: &lt;br /&gt;
** venv is the most basic option. Other options build upon it. &lt;br /&gt;
** poetry is widely used and offers a broad set of functionality&lt;br /&gt;
** uv is the latest option and much faster than poetry while offering the same (or more) functionality.&lt;br /&gt;
* ...Python + Conda: &lt;br /&gt;
** We wouldn&#039;t recommend it but if you want to use conda only: Install the conda packages first into the conda environment and afterwards the python packages. Otherwise, problems might arise.&lt;br /&gt;
** For a faster and more up to date solution, choose pixi. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; &lt;br /&gt;
|- style=&amp;quot;text-align:center;&amp;quot;&lt;br /&gt;
! Tool&lt;br /&gt;
! Description&lt;br /&gt;
! Can install python versions&lt;br /&gt;
! Installs packages from PyPI&lt;br /&gt;
! Installs packages from conda&lt;br /&gt;
! Dependency Resolver&lt;br /&gt;
! Dependency Management&lt;br /&gt;
! Creates Virtual Environments&lt;br /&gt;
! Supports building, packaging and publishing code (to PyPI)&lt;br /&gt;
|-&lt;br /&gt;
| pyenv&lt;br /&gt;
| Manages python versions on your system.&lt;br /&gt;
| yes&lt;br /&gt;
| no&lt;br /&gt;
| no&lt;br /&gt;
| no&lt;br /&gt;
| no&lt;br /&gt;
| no&lt;br /&gt;
| no&lt;br /&gt;
|-&lt;br /&gt;
| pip&lt;br /&gt;
| For installing python packages.&lt;br /&gt;
| no&lt;br /&gt;
| yes&lt;br /&gt;
| no&lt;br /&gt;
| yes&lt;br /&gt;
| no&lt;br /&gt;
| no&lt;br /&gt;
| no&lt;br /&gt;
|-&lt;br /&gt;
| venv&lt;br /&gt;
| For installing and managing python packages. Part of Python&#039;s standard library.&lt;br /&gt;
| no&lt;br /&gt;
| no&lt;br /&gt;
| no&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| no&lt;br /&gt;
|-&lt;br /&gt;
| poetry&lt;br /&gt;
| For installing and managing python packages. Install it with pipx.&lt;br /&gt;
| no&lt;br /&gt;
| yes&lt;br /&gt;
| no&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|-&lt;br /&gt;
| pipx&lt;br /&gt;
| For installing and running python applications (like poetry) globally while having them in isolated environments. It is useful for keeping applications globally available and at the same time separated in their own virtual environments. Use it when the installation instructions of an application offer you this way of installation.&lt;br /&gt;
| no&lt;br /&gt;
| no&lt;br /&gt;
| no&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes (only for single applications)&lt;br /&gt;
| no&lt;br /&gt;
|-&lt;br /&gt;
| uv&lt;br /&gt;
| Replaces poetry, pyenv, pip etc. and is very fast (https://www.loopwerk.io/articles/2025/uv-keeps-getting-better/)&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| no&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|-&lt;br /&gt;
| pixi&lt;br /&gt;
| For installing and managing python as well as conda packages. Uses uv in the background&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
| yes&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== Pip ===&lt;br /&gt;
&lt;br /&gt;
The standard package manager under Python is &amp;lt;code&amp;gt;pip&amp;lt;/code&amp;gt;. It can be used to install, update and delete packages. Pip can be called directly or via &amp;lt;code&amp;gt;python -m pip&amp;lt;/code&amp;gt;. The standard repository from which packages are obtained is PyPI (https://pypi.org/). When a package depends on others, they are automatically installed as well.&lt;br /&gt;
In the following, the most common pip commands are shown exemplarily. Packages should always be installed within virtual environments to avoid conflicting installations. If you decide to not use a virtual environment, the install commands have to be accomplished by a &amp;lt;code&amp;gt;--user&amp;lt;/code&amp;gt; flag or controlled via environment variables. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Installation of packages&amp;lt;/b&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
pip install pandas           # Installs the latest compatible version of pandas and its required dependencies&lt;br /&gt;
pip install pandas=1.5.3     # Installs exact version&lt;br /&gt;
pip install pandas&amp;gt;=1.5.3    # Installs version newer or equal to 1.5.3&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The packages from PyPI usually consist of precompiled libraries. However, &amp;lt;code&amp;gt;pip&amp;lt;/code&amp;gt; is also capable of creating packages from source code. However, this may require the C/C++ compiler and other dependencies needed to build the libraries. In the example, pip obtains the source code of matplotlib from github.com, installs its dependencies, compiles the library and installs it:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
pip install git+https://github.com/matplotlib/matplotlib&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Upgrade packages&amp;lt;/b&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
pip install --upgrade pandas # Updates the library if update is available&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Removing packages&amp;lt;/b&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
pip uninstall pandas         # Removes pandas&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Show packages&amp;lt;/b&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
pip list           # Shows the installed packages&lt;br /&gt;
pip freeze         # Shows the installed packages and their versions&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Save State&amp;lt;/b&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
To allow for reproducibility it is important to provide information about the full list of packages and their exact versions [https://pip.pypa.io/en/stable/topics/repeatable-installs/ see version pinning]. &lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
pip freeze &amp;gt; requirements.txt     # redirect package and version information to a textfile&lt;br /&gt;
pip install -r requirements.txt   # Installs all packages that are listed in the file&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Venv ===&lt;br /&gt;
&lt;br /&gt;
The module &amp;lt;code&amp;gt;venv&amp;lt;/code&amp;gt; enables the creation of a virtual environment and is a standard component of Python. Creating a &amp;lt;code&amp;gt;venv&amp;lt;/code&amp;gt; means that a folder is created which contains a separate copy of the Python binary file as well as &amp;lt;code&amp;gt;pip&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;setuptools&amp;lt;/code&amp;gt;. After activating the &amp;lt;code&amp;gt;venv&amp;lt;/code&amp;gt;, the binary file in this folder is used when &amp;lt;code&amp;gt;python&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;pip&amp;lt;/code&amp;gt; is called. This folder is also the installation target for other Python packages.&lt;br /&gt;
&lt;br /&gt;
==== Creation ====&lt;br /&gt;
&lt;br /&gt;
Create, activate, install software, deactivate: &lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3.11 -m venv myEnv        # Create venv&lt;br /&gt;
source myEnv/bin/activate       # Activate venv&lt;br /&gt;
pip install --upgrade pip       # Update of the venv-local pip&lt;br /&gt;
pip install &amp;lt;list of packages&amp;gt;  # Install packages/modules&lt;br /&gt;
deactivate&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Install list of software:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
pip install -r requirements.txt # Install packages/modules&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Usage ====&lt;br /&gt;
To use the virtual environment after all dependencies have been installed there, it is sufficient to simply activate it:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
source myEnv/bin/activate       # Activate venv&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With &amp;lt;code&amp;gt;venv&amp;lt;/code&amp;gt; activated the terminal prompt will reflect that accordingly:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
(myEnv) $ &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
It is no longer necessary to specify the Python version, the simple command &amp;lt;code&amp;gt;python&amp;lt;/code&amp;gt; starts the Python version that was used to create the virtual environment.&amp;lt;br/&amp;gt;&lt;br /&gt;
You can check, which Python version is in use via:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
(myEnv) $ which python&lt;br /&gt;
&amp;lt;/path/to/project&amp;gt;/myEnv/bin/python&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Poetry ===&lt;br /&gt;
&lt;br /&gt;
==== Creation ====&lt;br /&gt;
When you want to create a virtual environment for an already existing project, you can go to the top directory and run&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
poetry init      # Create virtual environment&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Otherwise start with the demo project: &lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
poetry new poetry-demo&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
You can set the allowed python versions in the pyproject.toml. To switch between python installations on your sytem, you can use&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
poetry env use python3.11&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Usage ====&lt;br /&gt;
&lt;br /&gt;
Install and update packages&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
poetry install &amp;lt;package_name&amp;gt;&lt;br /&gt;
poetry update      # Update to latest versions of packages&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To execute something within the virtual environment:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
poetry run &amp;lt;command&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Helpful links: &lt;br /&gt;
* [https://python-poetry.org/docs/managing-environments/#activating-the-environment Activate Environment]&lt;br /&gt;
&lt;br /&gt;
Helpful commands&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
poetry env info # show environment information&lt;br /&gt;
poetry env list # list all virtual environments associated with the current project &lt;br /&gt;
poetry env list --full-path&lt;br /&gt;
poetry env remove # delete virtual environment&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== uv ===&lt;br /&gt;
&lt;br /&gt;
==== Creation ====&lt;br /&gt;
&lt;br /&gt;
With uv you can create two types of new projects. Either an application (default) or a library.&lt;br /&gt;
&lt;br /&gt;
&amp;amp;rarr; &#039;&#039;&#039;[https://docs.astral.sh/uv/concepts/projects/ projects]&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
uv init &amp;lt;application_name&amp;gt;&lt;br /&gt;
# If you want to create a python package: &lt;br /&gt;
uv init --package &amp;lt;application_name&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When you have an existing project, you can create a virtual environment at .venv with a specific python version as follows: &lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
uv venv --python 3.11&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
A specific name or path can be given instead of &amp;quot;venv&amp;quot; as well. If the python version is not available on the system, uv downloads it. &lt;br /&gt;
&lt;br /&gt;
==== Usage ====&lt;br /&gt;
The currently active environment is saved in the environment variable &amp;lt;code&amp;gt;VIRTUAL_ENV&amp;lt;/code&amp;gt;. The environment can be activated and deactivated as follows:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
source .venv/bin/activate&lt;br /&gt;
deactivate&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
If you activated the virtual environment, you can install packagages as usual. Otherwise, you need to add &amp;quot;uv&amp;quot; before the command to run it within the virtual environment: &lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
uv pip install &amp;lt;package_name&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To execute a python file within the virtual environment:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
uv run &amp;lt;file.py&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If one project has multiple different groups of packages that are used, it could make sense to look into uv workspaces: &lt;br /&gt;
&lt;br /&gt;
&amp;amp;rarr; &#039;&#039;&#039;[https://docs.astral.sh/uv/concepts/projects/workspaces/ workspaces]&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Pixi ===&lt;br /&gt;
&lt;br /&gt;
==== Creation ====&lt;br /&gt;
&lt;br /&gt;
With uv you can create two types of new projects. Either an application (default) or a library.&lt;br /&gt;
&lt;br /&gt;
&amp;amp;rarr; &#039;&#039;&#039;[https://pixi.sh/latest/python/tutorial/ Python Tutorial]&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
pixi init &amp;lt;project_name&amp;gt; --format pyproject&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Usage ====&lt;br /&gt;
Add a new package to the environment: &lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Get it from conda:&lt;br /&gt;
pixi add &amp;lt;package_name&amp;gt;&lt;br /&gt;
# Get it from PyPi&lt;br /&gt;
pixi add &amp;lt;package_name&amp;gt; --pypi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To execute a command within the virtual environment:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
pixi run &amp;lt;command&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Workflow Management Systems ==&lt;br /&gt;
&lt;br /&gt;
There are a lot of different workflow management systems. One example is [https://snakemake.readthedocs.io/en/stable/index.html Snakemake] which is often used in the field of biology/medicine. Two use cases for snakemake workflows would be: &lt;br /&gt;
* Use an existing workflow to analyze data. &lt;br /&gt;
* Create your own workflow to make your research more reproducible. &lt;br /&gt;
Leaving the python world, a common choice for bioinformaticians would be Nextflow as there is a strong community that focuses on standardisation. Those more standardised pipelines can be found at the [https://nf-co.re/pipelines/ nf-core website].&lt;br /&gt;
In other research fields, other workflow management systems might be more common. &lt;br /&gt;
&lt;br /&gt;
== Best Practices ==&lt;br /&gt;
* Always use virtual environments! Use one environment per project.&lt;br /&gt;
* If a new or existing Python project is to be created or used, the following procedure is recommended:&lt;br /&gt;
# Version the Python source files and the &amp;lt;code&amp;gt;requirements.txt&amp;lt;/code&amp;gt; file with a version control system, e.g. git. Exclude unnecessary folders and files like for example &amp;lt;code&amp;gt;venv&amp;lt;/code&amp;gt; via an entry in the ignore file, for example &amp;lt;code&amp;gt;.gitignore&amp;lt;/code&amp;gt;.&lt;br /&gt;
# Create and activate a virtual environment.&lt;br /&gt;
# Use specialized number crunching python modules (e.g. numpy and scipy), don&#039;t use plain python for serious caluclations&lt;br /&gt;
## check if optimized compiled modules are available on the cluster (numpy, scipy) &lt;br /&gt;
# Update pip &amp;lt;code&amp;gt;pip install --upgrade pip&amp;lt;/code&amp;gt;.&lt;br /&gt;
# Install all required packages via the &amp;lt;code&amp;gt;requirements.txt&amp;lt;/code&amp;gt; file in the case of venv. Or by using the corresponding command of your chosen tool.&lt;br /&gt;
* List or dictionary comprehensions are to be prefered over loops as they are in general faster.&lt;br /&gt;
* Be aware of the differences between references, shallow and deep copies. &lt;br /&gt;
* Do not parallelize by hand but use libraries were possible (Dask, ...).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
== Example ==&lt;br /&gt;
== Do&#039;s and don&#039;ts ==&lt;br /&gt;
&lt;br /&gt;
Using Conda and Pip/Venv together&lt;br /&gt;
In some cases, Pip/Venv might be the preferred method for installing an environment. This could be because a central Python package comes with installation instructions that only show Pip as a supported option (like Tensorflow) or the use of projects which were written with the use of Pip in mind, for example by offering a requirements.txt and a rewrite with testing is not feasable.&lt;br /&gt;
In this case, it makes sense to use Conda as a replacement for Venv and use it to supply the virtual enviroment with the required Python version. After activating the environment, Pip will we available and refer to this Python enviroment.&lt;br /&gt;
&lt;br /&gt;
--&amp;gt;&lt;/div&gt;</summary>
		<author><name>P Hematty</name></author>
	</entry>
</feed>