2014年4月22日 星期二

[ MPI 文章收集 ] Mixing MPI and OpenMP

來源自 這裡
Preface:
下面範例說明如何在 MPI 上使用 OpenMP. 程式的邏輯是每個 process 都執行 rank+1 次的 for loop, 並將該 for loop 使用 OpenMP 平行化並列印 Hello 訊息.

Example:
- 範例代碼 mpi_with_openmp.c:
  1. #include   
  2. #include   
  3. #include   
  4.   
  5. int main(int argc, char *argv[])  
  6. {  
  7.     int numprocs, rank, namelen;  
  8.     char processor_name[MPI_MAX_PROCESSOR_NAME];  
  9.     int iam=0, np=1, i;  
  10.   
  11.     MPI_Init(&argc, &argv);  
  12.     MPI_Comm_size(MPI_COMM_WORLD, &numprocs);  
  13.     MPI_Comm_rank(MPI_COMM_WORLD, &rank);  
  14.     MPI_Get_processor_name(processor_name, &namelen);  
  15.   
  16.     #pragma omp parallel for private(iam, np)  
  17.     for(i=0; i1; i++)  
  18.     {  
  19.         np = omp_get_num_threads();  
  20.         iam = omp_get_thread_num();  
  21.         printf("Hello from thread %d out of %d from process %d out of %d on  %s - Round%d\n",   
  22.                iam, np, rank, numprocs, processor_name, i);  
  23.     }  
  24.   
  25.     MPI_Finalize();  
  26. }  
接著可以如下編譯與執行:
# mpic++ -fopenmp mpi_with_openmp.c -o mpi_with_openmp
# mpiexec -n 3 mpi_with_openmp
Hello from thread 2 out of 24 from process 2 out of 3 on linux1 - Round2
Hello from thread 0 out of 24 from process 2 out of 3 on linux1 - Round0
Hello from thread 0 out of 24 from process 0 out of 3 on linux1 - Round0
Hello from thread 0 out of 24 from process 1 out of 3 on linux1 - Round0
Hello from thread 1 out of 24 from process 1 out of 3 on linux1 - Round1
Hello from thread 1 out of 24 from process 2 out of 3 on linux1 - Round1


沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...