MPI: Setup & Hello World: Difference between revisions
Created page with "MPI (Message Passing Interface) adalah standar untuk membuat program paralel yang berjalan di banyak proses terpisah dan saling kirim data lewat <i>message passing</i>. MPI itu bukan library tunggal, tapi spesifikasi. Implementasinya banyak seperti: * OpenMPI * MPICH * Intel MPI * MVAPICH MPI dipakai di: * HPC clusters * supercomputer * multi-node research systems * distributed-memory environments == Cara Install == Install openmpi di semua node <syntaxhighlight lan..." |
m SanzSkuy moved page MPI to MPI: Setup & Hello World without leaving a redirect |
||
| (4 intermediate revisions by the same user not shown) | |||
| Line 12: | Line 12: | ||
* distributed-memory environments | * distributed-memory environments | ||
== | == MPI Hello World == | ||
Install openmpi di semua node | Install openmpi di semua node | ||
<syntaxhighlight lang=bash> | <syntaxhighlight lang=bash> | ||
| Line 80: | Line 80: | ||
target_compile_options(main PRIVATE -Wall -Wextra -O3) | target_compile_options(main PRIVATE -Wall -Wextra -O3) | ||
</syntaxhighlight> | |||
Lalu cara compile: | |||
<syntaxhighlight lang=bash> | |||
mkdir build && cd build | |||
cmake .. | |||
make | |||
./main | |||
</syntaxhighlight> | |||
Cara run: | |||
<syntaxhighlight lang=bash> | |||
mpirun -n 4 ./main | |||
# Output | |||
Hello world from processor tamvanz, rank 2 out of 4 processors | |||
Hello world from processor tamvanz, rank 1 out of 4 processors | |||
Hello world from processor tamvanz, rank 0 out of 4 processors | |||
Hello world from processor tamvanz, rank 3 out of 4 processors | |||
</syntaxhighlight> | |||
Flag <code>-n</code> menentukan jumlah proses MPI yang akan dijalankan. | |||
Secara default, jumlah maksimum proses tanpa oversubscribe mengikuti jumlah slot (biasanya = jumlah CPU logical), tapi bukan berarti harus sama dengan core fisik. | |||
== Multi-node MPI == | |||
Node worker juga harus diinstall openmpi, dan "mungkin" versinya harus sama. Saya belom coba kalau beda versi. Untuk menjalankan MPI multi-node, copy executablenya ke node worker dan pastikan kita sudah bisa login ke sana tanpa password dengan cara copy ssh key | |||
<syntaxhighlight lang=bash> | |||
scp main [email protected]:/home/haidhi | |||
</syntaxhighlight> | |||
Lalu run: | |||
<syntaxhighlight lang=bash> | |||
# rumus | |||
mpirun -n <N_PROCESS> localhost:<SLOT>,node:<SLOT> ./executable | |||
# example | |||
mpirun -n 6 -host localhost:2,192.168.83.117:4 ./main | |||
</syntaxhighlight> | |||
jumlah proses (-n) tidak boleh melebihi total slot, kecuali memakai <code>--oversubscribe</code> | |||
Atau cara yang lebih enak, buat <code>hosts.txt</code> isinya | |||
<syntaxhighlight> | |||
localhost slots=2 | |||
192.168.83.117 slots=4 | |||
</syntaxhighlight> | |||
Lalu run: | |||
<syntaxhighlight> | |||
mpirun -n 6 -hostfile hosts.txt ./main | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Latest revision as of 09:30, 8 January 2026
MPI (Message Passing Interface) adalah standar untuk membuat program paralel yang berjalan di banyak proses terpisah dan saling kirim data lewat message passing. MPI itu bukan library tunggal, tapi spesifikasi. Implementasinya banyak seperti:
- OpenMPI
- MPICH
- Intel MPI
- MVAPICH
MPI dipakai di:
- HPC clusters
- supercomputer
- multi-node research systems
- distributed-memory environments
MPI Hello World
Install openmpi di semua node
sudo pacman -S openmpi
Code hello world
// src/main.cpp
#include <mpi.h>
#include <stdio.h>
int main(int argc, char** argv) {
// Initialize the MPI environment
MPI_Init(NULL, NULL);
/*
function ini menentukan jumlah total proses yang terlibat dalam komunikasi.
Hasilnya disimpan di variabel world_size. MPI_COMM_WORLD adalah komunikator (grup default)
yang mencakup semua proses yang baru diinisialisasi.
*/
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
/*
function ini menentukan peringkat (rank) unik dari proses yang sedang menjalankan baris kode ini.
Peringkat ini adalah ID unik setiap proses, dimulai dari 0 hingga world_size−1.
Hasilnya disimpan di world_rank.
*/
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
/*
proses ini akan mencari nama fisik dari mesin (node) tempat proses ini berjalan.
Ini berguna untuk debugging pada cluster besar.
*/
char processor_name[MPI_MAX_PROCESSOR_NAME];
int name_len;
MPI_Get_processor_name(processor_name, &name_len);
printf("Hello world from processor %s, rank %d out of %d processors\n",
processor_name, world_rank, world_size);
/*
function ini mematikan environment MPI dan me-release semua sumber daya yang digunakan.
Semua proses yang telah menyelesaikan tugasnya akan dipersilakan bubar.
*/
MPI_Finalize();
}
isi dari CMakeLists.txt. Sebenarnya tidak terlalu perlu tapi mungkin akan berguna jika projek nya sudah besar
cmake_minimum_required(VERSION 4.2)
project(MPIHelloWorld LANGUAGES C CXX)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
find_package(MPI REQUIRED)
add_executable(main src/main.cpp)
target_include_directories(main PRIVATE ${MPI_INCLUDE_PATH})
target_link_libraries(main PRIVATE MPI::MPI_CXX)
target_compile_options(main PRIVATE -Wall -Wextra -O3)
Lalu cara compile:
mkdir build && cd build
cmake ..
make
./main
Cara run:
mpirun -n 4 ./main
# Output
Hello world from processor tamvanz, rank 2 out of 4 processors
Hello world from processor tamvanz, rank 1 out of 4 processors
Hello world from processor tamvanz, rank 0 out of 4 processors
Hello world from processor tamvanz, rank 3 out of 4 processors
Flag -n menentukan jumlah proses MPI yang akan dijalankan.
Secara default, jumlah maksimum proses tanpa oversubscribe mengikuti jumlah slot (biasanya = jumlah CPU logical), tapi bukan berarti harus sama dengan core fisik.
Multi-node MPI
Node worker juga harus diinstall openmpi, dan "mungkin" versinya harus sama. Saya belom coba kalau beda versi. Untuk menjalankan MPI multi-node, copy executablenya ke node worker dan pastikan kita sudah bisa login ke sana tanpa password dengan cara copy ssh key
scp main [email protected]:/home/haidhi
Lalu run:
# rumus
mpirun -n <N_PROCESS> localhost:<SLOT>,node:<SLOT> ./executable
# example
mpirun -n 6 -host localhost:2,192.168.83.117:4 ./main
jumlah proses (-n) tidak boleh melebihi total slot, kecuali memakai --oversubscribe
Atau cara yang lebih enak, buat hosts.txt isinya
localhost slots=2
192.168.83.117 slots=4Lalu run:
mpirun -n 6 -hostfile hosts.txt ./main