第一次的执行结果:
8
rank / nProcs newRank / hostname= 0 / 8 / 0 / e09r1n01
rank / nProcs newRank / hostname= 3 / 8 / 3 / e09r1n01
rank / nProcs newRank / hostname= 1 / 8 / 1 / e09r1n01
rank / nProcs newRank / hostname= 2 / 8 / 2 / e09r1n01
rank / nProcs newRank / hostname= 6 / 8 / 6 / e09r1n02
rank / nProcs newRank / hostname= 4 / 8 / 4 / e09r1n02
rank / nProcs newRank / hostname= 5 / 8 / 5 / e09r1n02
rank / nProcs newRank / hostname= 7 / 8 / 7 / e09r1n02
tail: Testout: file truncated
第二次的执行结果:
8
rank / nProcs newRank / hostname= 1 / 8 / 1 / e11r4n07
rank / nProcs newRank / hostname= 0 / 8 / 0 / e11r4n07
rank / nProcs newRank / hostname= 2 / 8 / 2 / e11r4n07
rank / nProcs newRank / hostname= 3 / 8 / 3 / e11r4n07
rank / nProcs newRank / hostname= 6 / 8 / 2 / e11r4n08
rank / nProcs newRank / hostname= 7 / 8 / 3 / e11r4n08
rank / nProcs newRank / hostname= 4 / 8 / 0 / e11r4n08
rank / nProcs newRank / hostname= 5 / 8 / 1 / e11r4n08
源程序:
//##include <iostream>
//#include <vector>
//#include <cstdlib>
//#include <time.h>
#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
//#include <cassert>
#include <string.h>
//#include <cmath>
//using namespace std;
#include <mpi.h>
int root, rank, nsize, tag, /status(MPI_STATUS_SIZE),/ ierr, sendtag, recvtag, currentShot,
source_tag, data_tag, terminate_tag, result_tag, slave, count;
void sortBuff(char **buff,int len){
char *temp; //临时交换变量
int i, j;
/*选择排序法**/
for (i = 0; i < len; ++i){
for (j = i + 1; j < len; ++j){
if( strcmp(buff[i], buff[j]) > 0)
{ //应用string.h
// int strcmp(
temp = buff[i]; // const char *string1,
// const char *string2
buff[i] = buff[j]; // );
//string1 > string2 返回值大于0 , == 为等于0, < 为小于0
buff[j] = temp;
}
}
char *temp; //临时交换变量
int i, j;
/*选择排序法**/
for (i = 0; i < len; ++i){
for (j = i + 1; j < len; ++j){
if( strcmp(buff[i], buff[j]) > 0)
{ //应用string.h
// int strcmp(
temp = buff[i]; // const char *string1,
// const char *string2
buff[i] = buff[j]; // );
//string1 > string2 返回值大于0 , == 为等于0, < 为小于0
buff[j] = temp;
}
}
char *temp; //临时交换变量
int i, j;
/*选择排序法**/
for (i = 0; i < len; ++i){
for (j = i + 1; j < len; ++j){
if( strcmp(buff[i], buff[j]) > 0)
{ //应用string.h
// int strcmp(
temp = buff[i]; // const char *string1,
// const char *string2
buff[i] = buff[j]; // );
//string1 > string2 返回值大于0 , == 为等于0, < 为小于0
buff[j] = temp;
}
}
}
}
//------------------------------------------------------------------------------
void start_mpi(int argc, char *argv[])
{
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&nsize);
}
//------------------------------------------------------------------------------
void stop_mpi()
{
MPI_Finalize();
}
int getProcessRank()
{
int i, namelength;
int nProcs , myrank , newRank , ierr;
MPI_Comm newComm;
MPI_Comm_size(MPI_COMM_WORLD , &nProcs);
MPI_Comm_rank(MPI_COMM_WORLD , &myrank);
//! allocate array of hostnames
//allocate ( hosts (0: nProcs -1))
char hosts;
char hostname[MPI_MAX_PROCESSOR_NAME];
hosts = (char)malloc(nProcs * sizeof(char));
for(i = 0; i < nProcs; i=i+1)
hosts[i] = (char)malloc(MPI_MAX_PROCESSOR_NAME * sizeof(char));
//!Every process collects the hostname of all the nodes
MPI_Get_processor_name(hostname, &namelength);
strncpy(hosts[myrank], hostname , namelength);
for(i = 0; i < nProcs; i=i+1)
MPI_Comm newComm;
MPI_Comm_size(MPI_COMM_WORLD , &nProcs);
MPI_Comm_rank(MPI_COMM_WORLD , &myrank);
//! allocate array of hostnames
//allocate ( hosts (0: nProcs -1))
char hosts;
char hostname[MPI_MAX_PROCESSOR_NAME];
hosts = (char)malloc(nProcs * sizeof(char));
for(i = 0; i < nProcs; i=i+1)
hosts[i] = (char)malloc(MPI_MAX_PROCESSOR_NAME * sizeof(char));
//!Every process collects the hostname of all the nodes
MPI_Get_processor_name(hostname, &namelength);
strncpy(hosts[myrank], hostname , namelength);
for(i = 0; i < nProcs; i=i+1)
MPI_Comm newComm;
MPI_Comm_size(MPI_COMM_WORLD , &nProcs);
MPI_Comm_rank(MPI_COMM_WORLD , &myrank);
//! allocate array of hostnames
//allocate ( hosts (0: nProcs -1))
char hosts;
char hostname[MPI_MAX_PROCESSOR_NAME];
hosts = (char)malloc(nProcs * sizeof(char));
for(i = 0; i < nProcs; i=i+1)
hosts[i] = (char)malloc(MPI_MAX_PROCESSOR_NAME * sizeof(char));
//!Every process collects the hostname of all the nodes
MPI_Get_processor_name(hostname, &namelength);
strncpy(hosts[myrank], hostname , namelength);
for(i = 0; i < nProcs; i=i+1)
MPI_Comm newComm;
MPI_Comm_size(MPI_COMM_WORLD , &nProcs);
MPI_Comm_rank(MPI_COMM_WORLD , &myrank);
//! allocate array of hostnames
//allocate ( hosts (0: nProcs -1))
char hosts;
char hostname[MPI_MAX_PROCESSOR_NAME];
hosts = (char)malloc(nProcs * sizeof(char));
for(i = 0; i < nProcs; i=i+1)
hosts[i] = (char)malloc(MPI_MAX_PROCESSOR_NAME * sizeof(char));
//!Every process collects the hostname of all the nodes
MPI_Get_processor_name(hostname, &namelength);
strncpy(hosts[myrank], hostname , namelength);
for(i = 0; i < nProcs; i=i+1)
MPI_Comm newComm;
MPI_Comm_size(MPI_COMM_WORLD , &nProcs);
MPI_Comm_rank(MPI_COMM_WORLD , &myrank);
//! allocate array of hostnames
//allocate ( hosts (0: nProcs -1))
char hosts;
char hostname[MPI_MAX_PROCESSOR_NAME];
hosts = (char)malloc(nProcs * sizeof(char));
for(i = 0; i < nProcs; i=i+1)
hosts[i] = (char)malloc(MPI_MAX_PROCESSOR_NAME * sizeof(char));
//!Every process collects the hostname of all the nodes
MPI_Get_processor_name(hostname, &namelength);
strncpy(hosts[myrank], hostname , namelength);
for(i = 0; i < nProcs; i=i+1)
MPI_Comm newComm;
MPI_Comm_size(MPI_COMM_WORLD , &nProcs);
MPI_Comm_rank(MPI_COMM_WORLD , &myrank);
//! allocate array of hostnames
//allocate ( hosts (0: nProcs -1))
char hosts;
char hostname[MPI_MAX_PROCESSOR_NAME];
hosts = (char)malloc(nProcs * sizeof(char));
for(i = 0; i < nProcs; i=i+1)
hosts[i] = (char)malloc(MPI_MAX_PROCESSOR_NAME * sizeof(char));
//!Every process collects the hostname of all the nodes
MPI_Get_processor_name(hostname, &namelength);
strncpy(hosts[myrank], hostname , namelength);
for(i = 0; i < nProcs; i=i+1)
MPI_Comm newComm;
MPI_Comm_size(MPI_COMM_WORLD , &nProcs);
MPI_Comm_rank(MPI_COMM_WORLD , &myrank);
//! allocate array of hostnames
//allocate ( hosts (0: nProcs -1))
char hosts;
char hostname[MPI_MAX_PROCESSOR_NAME];
hosts = (char)malloc(nProcs * sizeof(char));
for(i = 0; i < nProcs; i=i+1)
hosts[i] = (char)malloc(MPI_MAX_PROCESSOR_NAME * sizeof(char));
//!Every process collects the hostname of all the nodes
MPI_Get_processor_name(hostname, &namelength);
strncpy(hosts[myrank], hostname , namelength);
for(i = 0; i < nProcs; i=i+1)
{
MPI_Bcast(hosts[i], MPI_MAX_PROCESSOR_NAME, MPI_CHARACTER, i, MPI_COMM_WORLD);
}
sortBuff(hosts,nProcs);
int color = 0;
for(i = 0; i < nProcs; i=i+1)
{
if (i > 0)
{
if ( 0 != strcmp( hosts[i -1] , hosts[i]) )
{
color = color + 1;
}
}
if ( 0 == strcmp( hostname , hosts[i]) )
{
break;
}
}
MPI_Comm_split( MPI_COMM_WORLD , color,0, &newComm);
MPI_Comm_rank( newComm , &newRank);
printf(
{
MPI_Bcast(hosts[i], MPI_MAX_PROCESSOR_NAME, MPI_CHARACTER, i, MPI_COMM_WORLD);
}
sortBuff(hosts,nProcs);
int color = 0;
for(i = 0; i < nProcs; i=i+1)
{
if (i > 0)
{
if ( 0 != strcmp( hosts[i -1] , hosts[i]) )
{
color = color + 1;
}
}
if ( 0 == strcmp( hostname , hosts[i]) )
{
break;
}
}
MPI_Comm_split( MPI_COMM_WORLD , color,0, &newComm);
MPI_Comm_rank( newComm , &newRank);
printf(" rank / nProcs || newRank / hostname= %d / %d / %d / %s\n", myrank , nProcs, newRank, hostname);
for(i = 0; i < nProcs; i++)
free(hosts[i]);
free(hosts);
return newRank;
{
MPI_Bcast(hosts[i], MPI_MAX_PROCESSOR_NAME, MPI_CHARACTER, i, MPI_COMM_WORLD);
}
sortBuff(hosts,nProcs);
int color = 0;
for(i = 0; i < nProcs; i=i+1)
{
if (i > 0)
{
if ( 0 != strcmp( hosts[i -1] , hosts[i]) )
{
color = color + 1;
}
}
if ( 0 == strcmp( hostname , hosts[i]) )
{
break;
}
}
MPI_Comm_split( MPI_COMM_WORLD , color,0, &newComm);
MPI_Comm_rank( newComm , &newRank);
printf(
}
int main(int argc,char** argv){
start_mpi(argc, argv); //get rank
int processIDThisNode = getProcessRank();
stop_mpi();
}
运行脚本:
cat r.slurm
#!/bin/bash
#SBATCH -J test
#SBATCH -o Testout
#SBATCH -n 8
#SBATCH -N 2
#SBATCH --get-user-env
#SBATCH --cpus-per-task=1
#SBATCH --ntasks-per-node=4
#SBATCH -p kshdnormal
#SBATCH --gres=dcu:4
#SBATCH --gres-flags=disable-binding
#SBATCH --exclusive
#module use /public/software/modules
#module rm mpi/hpcx/2.4.1/gcc-7.3.1
#module load compiler/devtoolset/7.3.1
#module load compiler/rocm/2.9
#module load compiler/intel/2017.5.239
#module load mpi/hpcx/2.4.1/gcc-7.3.1
#module load apps/nwchem-DCU/6.8.1/hpcx-2.4.1-intel2017
######################srun ./test1111 -np 2 par=parfile
scontrol show hostname > nd
cat nd
sleep 10
sed -i 's/$/ slots=4/g' nd
NNODE=$(cat nd | wc -l)
NP=$[4*NNODE]
echo $NP
export HCC_LAZYINIT=ON
mpirun -np $NP -hostfile nd ./testmpi

暂无数据
建议采用断点办法,中间过程跟踪一下,看看到底是哪个环节出现的结果偏差
我们希望得到第二种结果,但是得到第一种结果的概率大于第二种结果,好奇怪呀