簡體   English   中英

使用OpenMP和MPI混合的“hello world”C程序

[英]“hello world” C program using hybrid of OpenMP and MPI

我正在嘗試使用OpenMP和MPI工作的“hello world”程序。 我從這里的例子開始

http://www.slac.stanford.edu/comp/unix/farm/mpi_and_openmp.html

但我無法重現輸出。 這是我使用的確切來源:

#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);

  //omp_set_num_threads(4);

#pragma omp parallel default(shared) private(iam, np)
  {
    np = omp_get_num_threads();
    iam = omp_get_thread_num();
    printf("Hello from thread %d out of %d from process %d out of %d on %s\n",
           iam, np, rank, numprocs, processor_name);
  }

  MPI_Finalize();
}

我正在使用運行Ubuntu 12.10的雙處理器Xeon工作站(2x6內核)。 我可以輕松獲得使用MPI或OpenMP(但不是兩者)的程序。

我使用命令編譯了上面的源代碼

mpicc -fopenmp hello.c -o hello

然后運行使用

export OMP_NUM_THREADS=4
mpirun ./hello -np 2 -x OMP_NUM_THREADS

這是我得到的輸出:

Hello from thread 0 out of 4 from process 0 out of 1 on SteinbergT5600Linux
Hello from thread 2 out of 4 from process 0 out of 1 on SteinbergT5600Linux
Hello from thread 1 out of 4 from process 0 out of 1 on SteinbergT5600Linux
Hello from thread 3 out of 4 from process 0 out of 1 on SteinbergT5600Linux

根據這個例子,我應該得到這樣的東西:

Hello from thread 0 out of 4 from process 0 out of 2 on SteinbergT5600Linux
Hello from thread 2 out of 4 from process 0 out of 2 on SteinbergT5600Linux
Hello from thread 1 out of 4 from process 0 out of 2 on SteinbergT5600Linux
Hello from thread 3 out of 4 from process 0 out of 2 on SteinbergT5600Linux
Hello from thread 0 out of 4 from process 1 out of 2 on SteinbergT5600Linux
Hello from thread 2 out of 4 from process 1 out of 2 on SteinbergT5600Linux
Hello from thread 1 out of 4 from process 1 out of 2 on SteinbergT5600Linux
Hello from thread 3 out of 4 from process 1 out of 2 on SteinbergT5600Linux

誰能給我一個暗示我做錯了什么? 據我所知,我正在復制上面鏈接中的示例。

您將程序名稱指定為mpirun的第一個參數,因此其余參數將被忽略(特別是: -np 2 )。 因此,對於-np ,您獲得了系統范圍的默認值。

更改:

mpirun ./hello -np 2 -x OMP_NUM_THREADS

成:

mpirun -np 2 -x OMP_NUM_THREADS ./hello

旁注:我在我的機器上測試了這個。 -np的默認值為3 在您的計算機上,默認值為1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM