Hello World Code Example using MPI
Copy kode program hello berikut ini ke project baru Anda. Programnya adalah sebagai berikut.
#include
#include
#include
#include
#include "mpi.h"
main(int argc, char **argv ) {
char message[20];
int i,rank, size, type=99;
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD,&size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if(rank == 0) {
strcpy(message, "Hello, world");
for (i=1; iMPI_Send(message, 20, MPI_CHAR, i, type, MPI_COMM_WORLD);
}
else
MPI_Recv(message, 20, MPI_CHAR, 0, type, MPI_COMM_WORLD, &status);
printf( "Message from process =%d : %.13s\n", rank,message);
MPI_Finalize();
}
Program ini mengirim message "Hello, world" dari proses master (rank = 0) ke semua proses slave (rank != 0) kemudian proses slave menerima pesan dan mencetaknya.
Save, lalu build program Anda sehingga menghasilkan hello.exe (bisa kan? Caranya klik build -> build, atau Ctrl+F9). Kalau berhasil itu artinya Codeblock Anda sudah bisa mengcompile program parallel, yay!
Terima kasih telah membaca artikel Hello World Code Example
message passing interface | mpi windows | mpi tutorial | mpi codeblocks
message passing interface | mpi windows | mpi tutorial | mpi codeblocks