MPI和OpenMP混合编程,hello

#include "stdio.h"
#include "mpi.h"
#include "omp.h"
#define NUM_THREADS 8
int main(int argc,char*argv[])
{
    int my_rank,numprocs,thread_id,nthreads;
    MPI_Init(&argc,&argv);
    MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
    MPI_Comm_rank(MPI_COMM_WORLD,&my_rank);
     #pragma omp parallel num_threads(NUM_THREADS) private(thread_id, nthreads)
    {
        thread_id=omp_get_thread_num();
        nthreads=omp_get_num_threads();
        printf("Hello,The World!from thread number %d (on %d)for the MPI process number %d (on %d)\n",thread_id, nthreads, my_rank, numprocs);
    }
    MPI_Finalize();
    return 0;
}
// mpicc demo2.c -o demo2 -fopenmp
// mpirun -np 6 ./demo2

编译运行结果如下,会有6*8=48行若你设置的为4则是32行
在这里插入图片描述

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐