Skip to content

Instantly share code, notes, and snippets.

@huzhifeng
Last active January 31, 2024 06:35
Show Gist options
  • Save huzhifeng/d1cda3f0474261eda72b36ca83f24e21 to your computer and use it in GitHub Desktop.
Save huzhifeng/d1cda3f0474261eda72b36ca83f24e21 to your computer and use it in GitHub Desktop.
MPI and OpenMP Example
#include <stdio.h>
#include <mpi.h>
#include <omp.h>
int main(int argc, char *argv[])
{
int numprocs, rank, namelen;
char processor_name[MPI_MAX_PROCESSOR_NAME];
int iam = 0, np = 1;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Get_processor_name(processor_name, &namelen);
#pragma omp parallel default(shared) private(iam, np)
{
np = omp_get_num_threads();
iam = omp_get_thread_num();
printf("Hybrid: Hello from thread %d out of %d from process %d out of %d on %s\n",
iam, np, rank, numprocs, processor_name);
}
MPI_Finalize();
return 0;
}
BINS:= mpi greetings omp hybrid
all: $(BINS)
mpi: mpi-hello.c
mpicc -o mpi mpi-hello.c
greetings: mpi-greetings.c
mpicc -o greetings mpi-greetings.c
omp: omp-hello.c
gcc -fopenmp -o omp omp-hello.c
hybrid: hybrid.c
mpicc -openmp -lgomp -o hybrid hybrid.c
test: $(BINS)
@echo "Test MPI..."
@mpirun -np 4 ./mpi
@mpirun -np 4 ./greetings
@echo "Test OpenMP..."
@./omp
@echo "Test Hybrid..."
@mpirun -np 4 ./hybrid
clean:
@rm -f $(BINS)
#include <mpi.h>
#include <stdio.h>
#include <string.h>
#define MSG_LEN 128
#define MSG_TAG 99
int main(int argc, char *argv[])
{
int world_rank = 0, world_size = 0, name_len = 0, i = 0;
char processor_name[MPI_MAX_PROCESSOR_NAME] = { 0x0 };
char msg[MSG_LEN] = { 0x0 };
MPI_Status status;
/* MPI_Init(NULL, NULL); */
MPI_Init(&argc, &argv); /* 程序初始化 */
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); /* 得到当前进程号 */
MPI_Comm_size(MPI_COMM_WORLD, &world_size); /* 得到总的进程数 */
MPI_Get_processor_name(processor_name, &name_len); /* 得到机器名 */
if (world_rank != 0) {
sprintf(msg, "MPI: Greetings from %s, rank %d out of %d", processor_name, world_rank, world_size);
MPI_Send(msg, strlen(msg) + 1, MPI_CHAR, 0, MSG_TAG, MPI_COMM_WORLD);
} else {
printf("MPI: Greetings from %s, rank %d out of %d\n", processor_name, world_rank, world_size);
for(i = 1; i < world_size; i++) {
MPI_Recv(msg, MSG_LEN, MPI_CHAR, i, MSG_TAG, MPI_COMM_WORLD, &status);
printf("%s\n", msg);
}
}
MPI_Finalize(); /* 结束 */
return 0;
}
#include <mpi.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
int world_rank, world_size;
int name_len;
char processor_name[MPI_MAX_PROCESSOR_NAME];
/* MPI_Init(NULL, NULL); */
MPI_Init(&argc, &argv); /* 程序初始化 */
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); /* 得到当前进程号 */
MPI_Comm_size(MPI_COMM_WORLD, &world_size); /* 得到总的进程数 */
MPI_Get_processor_name(processor_name, &name_len); /* 得到机器名 */
printf("MPI: Hello world from %s, rank %d out of %d\n", processor_name, world_rank, world_size);
MPI_Finalize(); /* 结束 */
return 0;
}
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int nthreads, tid;
/* Fork a team of threads giving them their own copies of variables */
#pragma omp parallel private(nthreads, tid)
{
/* Obtain thread number */
tid = omp_get_thread_num();
printf("OpenMP: Hello World from thread = %d\n", tid);
/* Only master thread does this */
if (tid == 0)
{
nthreads = omp_get_num_threads();
printf("OpenMP: Number of threads = %d\n", nthreads);
}
} /* All threads join master thread and disband */
return 0;
}
@ehsaneshaghi
Copy link

ehsaneshaghi commented Oct 1, 2019

in the Makefile there is a typo:
at line 15 you should add "-fopenmp". To be specific line 15 of Makefile should be
mpicc -openmp -lgomp -fopenmp -o hybrid hybrid.c
otherwise hyprid.c is not compiled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment