-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sync with French version: add new examples
- Loading branch information
Showing
20 changed files
with
346 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |