Skip to content

Commit

Permalink
Sync with French version: add new examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ofisette committed Nov 3, 2024
1 parent 4733f58 commit 27e25b1
Show file tree
Hide file tree
Showing 20 changed files with 346 additions and 5 deletions.
12 changes: 7 additions & 5 deletions 2-resources.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,19 @@
"For example : [`scripts/mpi-hello.sh`](https://github.com/calculquebec/cip201-compute-systems/blob/main/scripts/mpi-hello.sh)\n",
"\n",
"```Bash\n",
"cat scripts/mpi-hello.sh\n",
"cat scripts/mpi-pi/pi-job.sh\n",
"```\n",
"```\n",
"#!/bin/bash\n",
"#SBATCH --ntasks=10\n",
"#SBATCH --mem-per-cpu=1000M\n",
"#SBATCH --time=0-00:10\n",
"\n",
"#SBATCH --job-name=pi\n",
"#SBATCH --ntasks=4\n",
"#SBATCH --mem-per-cpu=1G\n",
"#SBATCH --time=00:05:00\n",
"\n",
"module load StdEnv/2023 gcc/12.3 openmpi/4.1.5\n",
"\n",
"mpirun printenv HOSTNAME OMPI_COMM_WORLD_RANK OMPI_COMM_WORLD_SIZE\n",
"srun ./pi 10000000000\n",
"```\n",
"\n",
"Our documentation about job scripts starts at this page:\n",
Expand Down
11 changes: 11 additions & 0 deletions scripts/array-distrib/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Array job generating random normal distributions
------------------------------------------------

Generate normal distributions (1000 points each) with random mean (0 to 1) and
std (0.5 to 1.5), save them to a CSV file, and print statistics. The Python code
is in `distrib.py`. An array job is used to generate 4 distributions saved to
the `results` directory.

Submit the array job script to the scheduler:

$ sbatch distrib-job.sh
14 changes: 14 additions & 0 deletions scripts/array-distrib/distrib-job.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

#SBATCH --job-name=myjob
#SBATCH --array=1-4
#SBATCH --ntasks=1
#SBATCH --mem=1G
#SBATCH --time=00:05:00

module load StdEnv/2023 gcc/12.3 python/3.11.5 scipy-stack/2024b

mkdir -p results
output_filename="results/distrib-${SLURM_ARRAY_TASK_ID}.csv"

srun python3 ./distrib.py "${output_filename}"
20 changes: 20 additions & 0 deletions scripts/array-distrib/distrib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from sys import argv, exit

import numpy as np
import pandas as pd

if len(argv) != 2:
print('usage: python3 distrib.py <output-csv-filename>')
exit(1)

output_filename = argv[1]

mean = np.random.rand()
std = np.random.rand() + 0.5
dist = np.random.normal(mean, std, size = 1000)
df = pd.DataFrame(dist)

with open(output_filename, 'w') as f:
f.write(df.to_csv())
print(f'Random normal distribution writen to {output_filename}.')
print(df.describe())
11 changes: 11 additions & 0 deletions scripts/job-script-templates/array-job.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

#SBATCH --job-name=myjob
#SBATCH --array=0-9
#SBATCH --ntasks=1
#SBATCH --mem=1G
#SBATCH --time=00:05:00

module load StdEnv/2023 gcc/12.3

srun my-serial-prog ${SLURM_ARRAY_TASK_ID}
14 changes: 14 additions & 0 deletions scripts/job-script-templates/hybrid-by-node-job.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

#SBATCH --job-name=myjob
#SBATCH --nodes=1
#SBATCH --ntasks-per-node=2
#SBATCH --cpus-per-task=2
#SBATCH --mem-per-cpu=1G
#SBATCH --time=00:05:00

export OMP_NUM_THREADS="${SLURM_CPUS_PER_TASK:-1}"

module load StdEnv/2023 gcc/12.3 openmpi/4.1.5

srun my-hybrid-prog arg1 arg2
13 changes: 13 additions & 0 deletions scripts/job-script-templates/hybrid-job.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

#SBATCH --job-name=myjob
#SBATCH --ntasks=2
#SBATCH --cpus-per-task=2
#SBATCH --mem-per-cpu=1G
#SBATCH --time=00:05:00

export OMP_NUM_THREADS="${SLURM_CPUS_PER_TASK:-1}"

module load StdEnv/2023 gcc/12.3 openmpi/4.1.5

srun my-hybrid-prog arg1 arg2
11 changes: 11 additions & 0 deletions scripts/job-script-templates/mpi-by-node-job.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

#SBATCH --job-name=myjob
#SBATCH --nodes=1
#SBATCH --ntasks-per-node=4
#SBATCH --mem-per-cpu=1G
#SBATCH --time=00:05:00

module load StdEnv/2023 gcc/12.3 openmpi/4.1.5

srun my-mpi-prog arg1 arg2
10 changes: 10 additions & 0 deletions scripts/job-script-templates/mpi-job.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

#SBATCH --job-name=myjob
#SBATCH --ntasks=4
#SBATCH --mem-per-cpu=1G
#SBATCH --time=00:05:00

module load StdEnv/2023 gcc/12.3 openmpi/4.1.5

srun my-mpi-prog arg1 arg2
11 changes: 11 additions & 0 deletions scripts/job-script-templates/openmp-job.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

#SBATCH --job-name=myjob
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=4
#SBATCH --mem-per-cpu=1G
#SBATCH --time=00:05:00

module load StdEnv/2023 gcc/12.3

srun my-openmp-prog arg1 arg2
10 changes: 10 additions & 0 deletions scripts/job-script-templates/serial-job.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

#SBATCH --job-name=myjob
#SBATCH --ntasks=1
#SBATCH --mem=1G
#SBATCH --time=00:05:00

module load StdEnv/2023 gcc/12.3

srun my-serial-prog arg1 arg2
15 changes: 15 additions & 0 deletions scripts/mpi-pi/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
MPI pi estimate using Monte Carlo
---------------------------------

Estimate the pi number, the ratio of a circle’s circumference to its diameter,
using a Monte Carlo approach. This is an MPI (Message Passing Interface)
parallel program. The C code is in `pi.c`.

1. Compile the program:

$ module load StdEnv/2023 gcc/12.3 openmpi/4.1.5
$ mpicc -o pi pi.c -lm

2. Submit the job script to the scheduler:

$ sbatch pi-job.sh
10 changes: 10 additions & 0 deletions scripts/mpi-pi/pi-job.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

#SBATCH --job-name=pi
#SBATCH --ntasks=4
#SBATCH --mem-per-cpu=1G
#SBATCH --time=00:05:00

module load StdEnv/2023 gcc/12.3 openmpi/4.1.5

srun ./pi 10000000000
57 changes: 57 additions & 0 deletions scripts/mpi-pi/pi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <math.h>
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

double monte_carlo_pi(unsigned long long n, MPI_Comm comm)
{
int rank, nranks;
MPI_Comm_rank(comm, &rank);
MPI_Comm_size(comm, &nranks);
srand(time(NULL) + rank);
double x, y;
unsigned long long local_p = 0;
for (unsigned long long i = rank; i < n; i += nranks)
{
x = (double) rand() / RAND_MAX;
y = (double) rand() / RAND_MAX;
if (sqrt(x*x + y*y) <= 1.0)
{
local_p++;
}
}
unsigned long long p;
MPI_Allreduce(&local_p, &p, 1, MPI_UNSIGNED_LONG_LONG, MPI_SUM, comm);
return 4*((double)p)/n;
}

int main(int argc, char* argv[])
{
MPI_Init(&argc, &argv);

int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);

if (argc == 2)
{
char* endptr;
unsigned long long n = strtoll(argv[1], &endptr, 10);

double pi = monte_carlo_pi(n, MPI_COMM_WORLD);
if (rank == 0)
{
printf("After %llu points, pi estimate is %f.\n", n, pi);
}
}
else
{
if (rank == 0)
{
printf("usage: pi <n-points>\n");
}
}

MPI_Finalize();
return 0;
}
14 changes: 14 additions & 0 deletions scripts/openmp-primes/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
OpenMP count of prime numbers
-----------------------------

Count the number of prime numbers from 1 to N. This is a parallel OpenMP
program. The C code is in `primes.c`.

1. Compile the program:

$ module load StdEnv/2023 gcc/12.3
$ mpicc -o primes primes.c -fopenmp

2. Submit the job script to the scheduler:

$ sbatch primes-job.sh
11 changes: 11 additions & 0 deletions scripts/openmp-primes/primes-job.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

#SBATCH --job-name=primes
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=4
#SBATCH --mem-per-cpu=1G
#SBATCH --time=00:05:00

module load StdEnv/2023 gcc/12.3

srun ./primes 400000
50 changes: 50 additions & 0 deletions scripts/openmp-primes/primes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <omp.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>

bool* find_primes(unsigned long n)
{
bool* is_prime = malloc(n * sizeof(bool));
#pragma omp parallel
{
int thread = omp_get_thread_num();
int nthreads = omp_get_num_threads();
for (unsigned long i = thread; i < n; i+= nthreads)
{
is_prime[i] = true;
for (unsigned long j = 2; j < i; j++)
{
if (i % j == 0)
{
is_prime[i] = false;
}
}
}
}
is_prime[0] = false;
return is_prime;
}

int main(int argc, char* argv[])
{
if (argc != 2)
{
printf("usage: primes <up-to-n>\n");
return(1);
}
unsigned long n = atol(argv[1]);

bool* is_prime = find_primes(n);
int nprimes = 0;
for (int i = 0; i < n; i++)
{
if (is_prime[i])
{
nprimes++;
}
}
printf("There are %d primes up to %lu.\n", nprimes, n);

return 0;
}
15 changes: 15 additions & 0 deletions scripts/serial-fibonacci/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Serial Fibonacci using recursion
--------------------------------

Compute the Nth number in the Fibonacci sequence using recursion. This is a
serial program: computing Fibonacci numbers cannot be done in parallel. The C
code is in `fibonacci.c`.

1. Compile the program:

$ module load StdEnv/2023 gcc/12.3
$ gcc -o fibonacci fibonacci.c

2. Submit the job script to the scheduler:

$ sbatch fibonacci-job.sh
10 changes: 10 additions & 0 deletions scripts/serial-fibonacci/fibonacci-job.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

#SBATCH --job-name=fibo
#SBATCH --ntasks=1
#SBATCH --mem=1G
#SBATCH --time=00:05:00

module load StdEnv/2023 gcc/12.3

srun ./fibo 50
32 changes: 32 additions & 0 deletions scripts/serial-fibonacci/fibonacci.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <stdlib.h>
#include <stdio.h>

unsigned long fibonacci(unsigned int nth)
{
if (nth == 0)
{
return 0;
}
else if (nth == 1)
{
return 1;
}
else
{
return fibonacci(nth - 1) + fibonacci(nth - 2);
}
}

int main(int argc, char* argv[])
{
if (argc != 2)
{
printf("usage: fibo <nth-number>\n");
return(1);
}
unsigned int nth = atoi(argv[1]);

printf("The %uth Fibonacci number is %lu.\n", nth, fibonacci(nth));

return 0;
}

0 comments on commit 27e25b1

Please sign in to comment.