首先,程序编写与mpi单独要有区别,MPI_Init()要改为MPI_Init_thread(),还需要判断一下环境是否满足需求。
其次,程序不能使用默认openmp 的线程数,因为,torque不能用qsub脚本设置计算节点的环境变量。而openmp默认线程数是由OMP_NUM_THREADS环境变量设置的。为了更好的适用性和可移植,把线程数用参数传入,使用omp_set_num_threads()动态设置。
测试代码:
#include <stdio.h> #include <omp.h> #include <mpi.h> #include <stdlib.h> int rank, size; int main (int argc, char* argv[]) { int provided; MPI_Init_thread (&argc, &argv, MPI_THREAD_FUNNELED, &provided); if (MPI_THREAD_FUNNELED != provided) { printf ("%d != required %d", MPI_THREAD_FUNNELED, provided); return 0; } MPI_Comm_rank (MPI_COMM_WORLD, &rank); MPI_Comm_size (MPI_COMM_WORLD, &size); char name[MPI_MAX_PROCESSOR_NAME]; int length; MPI_Get_processor_name (name, &length); int omp_num_threads = 1; if (argc > 1) { omp_num_threads = atoi (argv[1]); } printf ("%s \n", name); omp_set_num_threads (omp_num_threads); #pragma omp parallel { printf ("%d omp thread from %d mpi process\n", omp_get_thread_num(), rank); } MPI_Finalize(); return 0; }
提交脚本:
#!/bin/sh -f #PBS -N hybrid #PBS -l nodes=2:ppn=2 #PBS -l walltime=00:05:00 #PBS -l mem=1024mb #PBS -o moe.txt #PBS -e mee.txt #PBS -q default cd $PBS_O_WORKDIR MVA='/usr/mpi/intel/mvapich-1.2.0/bin/mpirun ' eval $MVA -n 2 ./hybrid 2