背景:我的任务是在C中编写一个MPI程序,计算所有素数到达给定数量。这个程序运行正常。
我使用openmpi和-O3优化编译程序。
当使用1个进程在我的个人计算机(Ubuntu 11.10 x64)上运行它时,我得到了我期望的结果(对于4E9的所有素数,约为13秒)。我的CS部门的机器也是如此。
但是,当我在NERSC的Carver上运行时,时间会急剧增加(1个进程约为61秒)。
我尝试过使用openmpi和intel编译器......没有区别。我让它运行一次适当的时间,但我不记得我做了什么(如果有的话)我的代码中有一个轻微的索引错误,我已经修复了(与实际执行计算无关,所以时间准确)。
我试图尽可能清楚;如果您还有其他问题,我很乐意回答。谢谢!
#include <stdio.h>
#include <mpi.h>
#include <stdlib.h>
#include <math.h>
#define MAX(x,y) ((x)>(y) ? (x) : (y) )
#define MIN(x,y) ((x)>(y) ? (y) : (x) )
#define A(i,j) A[(i)*M+j]
#define b(i) b[i]
#define c(i) c[i]
long* preamble(long N,char* mark){
N = sqrt(N)+1;
long size;
long curr, index;
long i, j,n;
long count;
long* primes;
//Pierre Dusart proven upper bound for number of primes up to N
//found at http://primes.utm.edu/howmany.shtml
size = (N/log(N))*(1+(1.2762/log(N)))*sizeof(long);
primes = (long *)malloc(size);
if(N%2)
n=N/2 - 2;
else
n=(N-1)/2 -1;
index = 0;
curr = 3;
while (curr*curr<=N) {
for (i=(curr*curr-3)/2; i<=n; i+=curr){
mark[i]=1;
}
while (mark[++index]) ;
curr = index*2+3;
}
/*number of primes*/
count = 0;
for(i = 0; i <=n; i+=1){
if(mark[i] == 0) {
primes[++count]=i*2+3;
}
}
primes[0]=count;
return primes;
}
long FMIB(long p, long b){
if(b%p==0 && b!=p) return b;
long i = b + p - b % p;
if(i%2){return i;}else{return i+p;}
}
int main(int argc, char **argv) {
long N = 4000000000;
long BKSIZE = 500000;
char *mark;
long *primes;
long *loopprimes;
long size, offset;
long numprimes;
long i, j, n, ii, start, index;
long count, total;
double time;
if ( argc > 1 ) N = atol(argv[1]);
if ( argc > 2 ) BKSIZE = atol(argv[2]);
int id, p;
BKSIZE = (BKSIZE-3)/2 +1;
if(N%2)
n=N/2 - 2;
else
n=(N-1)/2 -1;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &id);
MPI_Comm_size(MPI_COMM_WORLD, &p);
MPI_Barrier(MPI_COMM_WORLD);
if(id==0) time = MPI_Wtime();
size = (n/p+1)*sizeof(char);
mark = (char *)malloc(size);
for (i=1; i<=n/p+1; i++){
mark[i]=0;
}
primes = preamble(N,mark);
if(id!=0){
for (i=0; i<=n/p+1; i++){
mark[i]=0;
}
}
offset = (1+n/p)*id;
numprimes=primes[0];
if(id==0){
start = (sqrt(N)-3)/2+1; //mark index to start at
}else{
start = offset;
}
//MAIN COMPUTATION - BLOCKING
for(ii=start; ii<=MIN(ii+BKSIZE,offset+n/p); ii+=BKSIZE){
for(j=0; j < numprimes; j++){
for(i=(FMIB(primes[j+1],ii*2+3)-3)/2; i<=MIN(ii+BKSIZE,offset+n/p); i+=primes[j+1]){
mark[i-offset]=1;
}
}
}
/*number of primes*/
if(id==0){
count = 1;
}else{
count = 0;
}
for(i = 0; i <= n/p && (i+offset)*2+3 <= N; i++){
if(mark[i] == 0) {
++count;
}
}
MPI_Barrier(MPI_COMM_WORLD);
MPI_Reduce(&count, &total, 1, MPI_LONG, MPI_SUM, 0, MPI_COMM_WORLD);
MPI_Barrier(MPI_COMM_WORLD);
if(id==0){
time = MPI_Wtime() - time;
printf("There are %ld primes less than %ld\n", total, N);
printf("First three primes:");
j = 1;
printf(" %d", 2);
for ( i=0 ; i <= n && j < 3; i+=1 ) {
if (mark[i]==0){
printf(" %ld", (i*2+3));
++j;
}
}
printf("\n");
}
MPI_Barrier(MPI_COMM_WORLD);
if(id == p-1){
printf("Last three primes:");
j = 0;
for (i = n-offset; i >= 0 && j < 3; i--){
if (mark[i]==0){
printf(" %ld", ((offset+i)*2+3));
j++;
}
}
if(j < 3){
printf(" %d",2);
}
printf("\n");
}
MPI_Barrier(MPI_COMM_WORLD);
if(id == 0){
printf("Elapsed time: %f seconds\n",time);
fflush(stdout);
}
MPI_Finalize();
//free(mark);
return 0;
}
脚本:
#!/bin/csh
#used for intel compiler
#module unload pgi openmpi
#module load intel openmpi-intel mkl
make clean
make
set x = "sieve_mpi"
set n = 4000000000
set b = 500000
foreach p ( 1 2 3 4 5 6 7 8 )
cat > ${x}${p}.pbs <<EOF
#PBS -q regular
#PBS -l nodes=1:ppn=8
#PBS -l walltime=00:01:00
#PBS -N ${x}${p}
#PBS -e err/${x}${p}.err
#PBS -o out/${x}${p}.out
#used when using intel compiler
#module unload pgi openmpi
#module load intel openmpi-intel mkl
cd \$PBS_O_WORKDIR
echo ${x}
echo ${p}
mpirun -np ${p} ${x} ${n} ${b}
EOF
qsub ${x}${p}.pbs
end
生成文件:
CC = mpicc
EXEC = pi_cyc pi_block sieve_mpi
OBJS =
H_FILE =
MATHFLAG = -lm
FLAGS = -O3
SEQFLAGS = -O3
all: $(EXEC)
pi_cyc: pi_cyc.c $(OBJS) $(H_FILE)
$(CC) $(FLAGS) -o $@ pi_cyc.c $(OBJS) $(MATHFLAG)
pi_block: pi_block.c $(OBJS) $(H_FILE)
$(CC) $(FLAGS) -o $@ pi_block.c $(OBJS) $(MATHFLAG)
sieve_mpi: sieve_mpi.c $(OBJS) $(H_FILE)
$(CC) $(FLAGS) -o $@ sieve_mpi.c $(OBJS) $(MATHFLAG)
clean:
rm -f *.o *.pgm $(OBJS) $(EXEC)
答案 0 :(得分:1)
你有太多的障碍,不是吗?删除其中的每一个。
如果您想测量时间,请在完成后将每个进程的时间和MPI_Reduce与MPI_MAX一起排名为0。
围绕MPI_Reduce的障碍一事无成。 MPI_Reduce是集体的,将强加任何需要的同步
您正在尝试获得排名0和最后排名来打印出来的东西。你可能只是让排名0从最后一个等级中获得这三个素数并用其余的0级输出打印出来吗?