MPI: Setup & Hello World: Difference between revisions
Appearance
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..." |
No edit summary |
||
| 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 81: | Line 81: | ||
target_compile_options(main PRIVATE -Wall -Wextra -O3) | target_compile_options(main PRIVATE -Wall -Wextra -O3) | ||
</syntaxhighlight> | </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> adalah jumlah process yang akan jalan berbarengan. Dia bukan jumlah thread CPU, tapi jumlah core fisik. Cara melihat jumlah <code>-n</code> yang bisa digunakan jalankan command <code>lscpu</code> lalu lihat di bagian "Core(s) per socket" | |||
Revision as of 07:28, 27 November 2025
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 adalah jumlah process yang akan jalan berbarengan. Dia bukan jumlah thread CPU, tapi jumlah core fisik. Cara melihat jumlah -n yang bisa digunakan jalankan command lscpu lalu lihat di bagian "Core(s) per socket"