Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Julia MPI Examples #51

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Parallel_Computing/Julia/Example1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
This example demonstrates basic usage of MPI in Julia.

### Contents
- <code>hello_world_mpi.jl</code>: Julia source code
- <code>hello_world_mpi.sh</code>: slurm submission script.

### Example Usage:

```bash
sbatch hello_world_mpi.sh
```

### Example Output:

```bash
$ cat hello_world_mpi.out
Hello world, I am rank 3 of 4
Hello world, I am rank 2 of 4
Hello world, I am rank 0 of 4
Hello world, I am rank 1 of 4
```
7 changes: 7 additions & 0 deletions Parallel_Computing/Julia/Example1/hello_world_mpi.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using MPI
MPI.Init()

comm = MPI.COMM_WORLD
print("Hello world, I am rank $(MPI.Comm_rank(comm)) of $(MPI.Comm_size(comm))\n")
MPI.Barrier(comm)
MPI.Finalize()
15 changes: 15 additions & 0 deletions Parallel_Computing/Julia/Example1/hello_world_mpi.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash
#SBATCH -J hello_world_mpi
#SBATCH -p test # partition
#SBATCH -n 4 # number of cores
#SBATCH --mem-per-cpu=5GB
#SBATCH -t 0-01:00 # time (D-HH-MM)
#SBATCH -e hello_world_mpi.err
#SBATCH -o hello_world_mpi.out

export UCX_WARN_UNUSED_ENV_VARS=n
module load gcc/12.2.0-fasrc01 openmpi/4.1.4-fasrc01

julia --project=~/MPIenv -e 'using MPIPreferences; MPIPreferences.use_system_binary()'
julia --project=~/MPIenv -e 'using Pkg; Pkg.build("MPI"; verbose=true)'
mpiexec -n 4 julia --project=~/MPIenv hello_world_mpi.jl
18 changes: 18 additions & 0 deletions Parallel_Computing/Julia/Example2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
This example demonstrates Monte Carlo computation of pi by MPI simulation.

### Contents
- <code>compute_pi.jl</code>: Julia source code
- <code>compute_pi.sh</code>: slurm submission script.

### Example Usage:

```bash
sbatch compute_pi.sh
```

### Example Output:

```bash
$ cat compute_pi.out
Estimate of pi is: 3.14167883
```
34 changes: 34 additions & 0 deletions Parallel_Computing/Julia/Example2/compute_pi.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using MPI
using LinearAlgebra

MPI.Init()

comm = MPI.COMM_WORLD
rank = MPI.Comm_rank(comm)
comm_size = MPI.Comm_size(comm)
root = 0
MPI.Barrier(comm)

N = 1e8;
function count_pi(N)
R = 1;
local_count = 0
for i in 1:N
coords = rand(2)
if norm(coords) < R
local_count = local_count + 1
end
end
return local_count
end
local_count = count_pi(N)

MPI.Barrier(comm)
total_count = MPI.Reduce(local_count, +, root, comm)

if rank == root
print("\n Estimate of pi is: ")
print(total_count / N / comm_size * 4)
end

MPI.Finalize()
15 changes: 15 additions & 0 deletions Parallel_Computing/Julia/Example2/compute_pi.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash
#SBATCH -J compute_pi
#SBATCH -p test # partition
#SBATCH -n 4 # number of cores
#SBATCH --mem-per-cpu=5GB
#SBATCH -t 0-01:00 # time (D-HH-MM)
#SBATCH -e compute_pi.err
#SBATCH -o compute_pi.out

export UCX_WARN_UNUSED_ENV_VARS=n
module load gcc/12.2.0-fasrc01 openmpi/4.1.4-fasrc01

julia --project=~/MPIenv -e 'using MPIPreferences; MPIPreferences.use_system_binary()'
julia --project=~/MPIenv -e 'using Pkg; Pkg.build("MPI"; verbose=true)'
mpiexec -n 4 julia --project=~/MPIenv compute_pi.jl
29 changes: 29 additions & 0 deletions Parallel_Computing/Julia/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# MPI with Julia on FASRC

### Installing Julia

First, install Julia on the cluster using [juliaup](https://github.com/JuliaLang/juliaup):

```bash
curl -fsSL https://install.julialang.org | sh
```

### Creating an environment for MPI

```bash
cd ~
mkdir MPIenv
```

Then in julia, enter Package Manager Mode: Type <code>]</code> to enter the package manager mode. Run

```bash
(@v1.11) pkg> activate ~/MPIenv
(@v1.11) pkg> add MPI MPIPreferences
```

to install [MPI.jl](https://github.com/JuliaParallel/MPI.jl). After creating this environment, any parallel script can be run in this environment, for example:

```bash
mpiexec -n 4 julia --project=~/MPIenv script.jl
```