Difference between revisions of "Programming/Perl"
From HPC
m (Pysdlb moved page Perl to Programming/Perl without leaving a redirect) |
m (→MPI program) |
||
(48 intermediate revisions by the same user not shown) | |||
Line 2: | Line 2: | ||
Perl is a family of high-level, general-purpose, interpreted, dynamic programming languages. The languages in this family presently are Perl 5 and Perl 6. | Perl is a family of high-level, general-purpose, interpreted, dynamic programming languages. The languages in this family presently are Perl 5 and Perl 6. | ||
+ | {| | ||
+ | |style="width:5%; border-width: 0" | [[File:icon_pencil.png]] | ||
+ | |style="width:95%; border-width: 0" | When programming with Perl in an HPC environment you will need to change the first line as shown below | ||
+ | |- | ||
+ | |} | ||
+ | <pre style="background-color: #f5f5dc; color: black; font-family: monospace, sans-serif;"> | ||
+ | |||
+ | #!/usr/bin/perl | ||
+ | |||
+ | </pre> | ||
+ | |||
+ | To | ||
+ | |||
+ | <pre style="background-color: #f5f5dc; color: black; font-family: monospace, sans-serif;"> | ||
+ | |||
+ | #!/usr/bin/env perl | ||
+ | |||
+ | </pre> | ||
Line 7: | Line 25: | ||
=== Programming example === | === Programming example === | ||
− | <pre style="background-color: # | + | ==== Non MPI program ==== |
+ | |||
+ | <pre style="background-color: #f5f5dc; color: black; font-family: monospace, sans-serif;"> | ||
#!/usr/bin/env perl | #!/usr/bin/env perl | ||
# | # | ||
− | # | + | # HPC |
− | # | + | # IT Department |
# Hull University | # Hull University | ||
# | # | ||
− | # Written by DBird 25/12/ | + | # Written by DBird 25/12/2020 |
# ---------------- Modules ------------------------------------------ | # ---------------- Modules ------------------------------------------ | ||
Line 39: | Line 59: | ||
</pre> | </pre> | ||
+ | {| | ||
+ | |style="width:5%; border-width: 0" | [[File:icon_exclam3.png]] | ||
+ | |style="width:95%; border-width: 0" | This program does not contain any MPI directives and is shown simply for demonstrative purposes. | ||
+ | Although PERL does support MPI, its implementation is quite limited compared to C, C++, and Fortran. | ||
+ | |- | ||
+ | |} | ||
+ | |||
+ | ==== MPI program ==== | ||
+ | |||
+ | <pre style="background-color: #f5f5dc; color: black; font-family: monospace, sans-serif;"> | ||
+ | |||
+ | #!/usr/bin/env perl | ||
+ | # | ||
+ | # HPC | ||
+ | # IT Department | ||
+ | # Hull University | ||
+ | # | ||
+ | # Written by DBird 25/12/2017 | ||
− | + | use Parallel::MPI::Simple; | |
− | + | ||
+ | MPI_Init(); | ||
+ | my $rank = MPI_Comm_rank(MPI_COMM_WORLD); | ||
+ | |||
+ | if ($rank == 1) | ||
+ | { | ||
+ | my $msg = "Hello, I'm $rank"; | ||
+ | MPI_Send($msg, 0, 123, MPI_COMM_WORLD); | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | my $msg = MPI_Recv(1, 123, MPI_COMM_WORLD); | ||
+ | print "$rank received: '$msg'\n"; | ||
+ | } | ||
+ | MPI_Finalise(); | ||
+ | exit; | ||
+ | </pre> | ||
==== Running the program ==== | ==== Running the program ==== | ||
Line 48: | Line 102: | ||
The program could be executed in the following way for test purposes on the login node: | The program could be executed in the following way for test purposes on the login node: | ||
− | <pre style="background-color: | + | <pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;"> |
− | module | + | [username@login01 ~]$ module add perl/5.24.0 |
− | perl testPerl.pl | + | [username@login01 ~]$ perl testPerl.pl |
</pre> | </pre> | ||
− | = Batch example = | + | == Batch example == |
+ | |||
+ | |||
+ | <pre style="background-color: #C8C8C8; color: black; border: 2px solid #C8C8C8; font-family: monospace, sans-serif;"> | ||
− | |||
#!/bin/bash | #!/bin/bash | ||
Line 63: | Line 119: | ||
#SBATCH -N 1 | #SBATCH -N 1 | ||
#SBATCH --ntasks-per-node 20 | #SBATCH --ntasks-per-node 20 | ||
− | |||
#SBATCH -o %N.%j.%a.out | #SBATCH -o %N.%j.%a.out | ||
#SBATCH -e %N.%j.%a.err | #SBATCH -e %N.%j.%a.err | ||
#SBATCH -p compute | #SBATCH -p compute | ||
#SBATCH --exclusive | #SBATCH --exclusive | ||
+ | #SBATCH --mail-user= your email address here | ||
echo $SLURM_JOB_NODELIST | echo $SLURM_JOB_NODELIST | ||
module purge | module purge | ||
− | module | + | module add perl/5.24.0 |
export I_MPI_DEBUG=5 | export I_MPI_DEBUG=5 | ||
Line 83: | Line 139: | ||
− | <pre style="background-color: | + | <pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;"> |
[username@login01 ~]$ sbatch demoPERL.job | [username@login01 ~]$ sbatch demoPERL.job | ||
− | Submitted batch job | + | Submitted batch job 4289352 |
</pre> | </pre> | ||
− | [[ | + | == Further Information == |
+ | |||
+ | * [https://en.wikipedia.org/wiki/Perl https://en.wikipedia.org/wiki/Perl] | ||
+ | * [https://www.tutorialspoint.com/perl/index.htm https://www.tutorialspoint.com/perl/index.htm] | ||
+ | * [https://www.perl.org/ https://www.perl.org/] | ||
+ | |||
+ | {{Languagespagenav}} |
Latest revision as of 11:18, 23 March 2023
Contents
Programming Details
Perl is a family of high-level, general-purpose, interpreted, dynamic programming languages. The languages in this family presently are Perl 5 and Perl 6.
When programming with Perl in an HPC environment you will need to change the first line as shown below |
#!/usr/bin/perl
To
#!/usr/bin/env perl
Programming example
Non MPI program
#!/usr/bin/env perl # # HPC # IT Department # Hull University # # Written by DBird 25/12/2020 # ---------------- Modules ------------------------------------------ use strict; # perform error checking # ---------------- Variables ---------------------------------------- my ($i); # ------------------------------------------------------------------- # ---------------- Main Program ------------------------------------- # ------------------------------------------------------------------- for($i = 0; $i < 1000; $i++) { print "A-->$i\n"; } # ---------------- finish up and exit ------------------------------- exit;
This program does not contain any MPI directives and is shown simply for demonstrative purposes.
Although PERL does support MPI, its implementation is quite limited compared to C, C++, and Fortran. |
MPI program
#!/usr/bin/env perl # # HPC # IT Department # Hull University # # Written by DBird 25/12/2017 use Parallel::MPI::Simple; MPI_Init(); my $rank = MPI_Comm_rank(MPI_COMM_WORLD); if ($rank == 1) { my $msg = "Hello, I'm $rank"; MPI_Send($msg, 0, 123, MPI_COMM_WORLD); } else { my $msg = MPI_Recv(1, 123, MPI_COMM_WORLD); print "$rank received: '$msg'\n"; } MPI_Finalise(); exit;
Running the program
The program could be executed in the following way for test purposes on the login node:
[username@login01 ~]$ module add perl/5.24.0 [username@login01 ~]$ perl testPerl.pl
Batch example
#!/bin/bash #SBATCH -J openPERL-node #SBATCH -N 1 #SBATCH --ntasks-per-node 20 #SBATCH -o %N.%j.%a.out #SBATCH -e %N.%j.%a.err #SBATCH -p compute #SBATCH --exclusive #SBATCH --mail-user= your email address here echo $SLURM_JOB_NODELIST module purge module add perl/5.24.0 export I_MPI_DEBUG=5 export I_MPI_FABRICS=shm:tmi export I_MPI_FALLBACK=no perl /home/user/PERL/demoPERL.pl
[username@login01 ~]$ sbatch demoPERL.job Submitted batch job 4289352