❶ 关于MPI 自定义结构体的问题
你使用了默认的调试 这种情况等下只有rank==0的 用MPICH提供的 wmpiexec.exe 运行 注意number of processes选2 可以运行的
❷ 求c语言程序高手帮忙看一段程序
我是刚才加你的。
$$init
doubleval_avg;doubledenom=0.0;inti;
if(init){
nElements=0;
}else{
//averagevalue
if(divisor>0.0)val_avg=sum/divisor;
elseval_avg=0.0;
//
for(i=0;i<nElements;i++)
denom+=fabs(values[i]-val_avg)*sizes[i];
MPI_SUM_D(denom);//accountforMPI
if(sum>0.0)sum=1.0-0.5*denom/sum;
if(BOOL_additional_output_to_fla&&IAMPRO<2){
print"Formula:",name;
print"numberoffaces/cells:",nElements;
print"areaorvolume:",divisor,"[m2]or[m3]";
print"averageequivalenceratio:",val_avg;
print"uniformityindex:",sum;
}
divisor=1.0;
}
❸ MPI程序求阶乘,求数组最大值
C语言利用数组计算超大整数的阶乘代码
#include <stdio.h>
int main()
{
int n;
int a[9000]; //确保保存最终运算结果的数组足够大
int digit = 1; //位数
int temp; //阶乘的任一元素与临时结果的某位的乘积结果
int i, j, carry; //carry:进位
printf("please in put n:\n");
scanf("%d",&n);
a[0] = 1; //将结果先初始化为1
for ( i=2; i<=n; i++ ) //开始阶乘,阶乘元素从2开始依次"登场"
{ //按最基本的乘法运算思想来考虑,将临时结果的每位与阶乘元素相乘
for( j=1, carry=0; j<=digit; j++ )
{
temp = a[j-1] * i + carry; //相应阶乘中的一项与当前所得临时结果的某位相乘(加上进位)
a[j-1] = temp % 10; //更新临时结果的位上信息
carry = temp / 10; //看是否有进位
}
while(carry)
{ //如果有进位
a[++digit-1] = carry % 10; //新加一位,添加信息。位数增1
carry = carry / 10; //看还能不能进位
}
}
printf("n ! = "); //显示结果
for(j = digit; j >=1;j--)
{
printf("%d",a[j-1]);
}
printf("\n");
return 0;
} #include <stdio.h> int main() { int n; int a[9000]; //确保保存最终运算结果的数组足够大 int digit = 1; //位数 int temp; //阶乘的任一元素与临时结果的某位的乘积结果 int i, j, carry; //carry:进位 printf("please in put n:\n"); scanf("%d",&n); a[0] = 1; //将结果先初始化为1 for ( i=2; i<=n; i++ ) //开始阶乘,阶乘元素从2开始依次"登场" { //按最基本的乘法运算思想来考虑,将临时结果的每位与阶乘元素相乘 for( j=1, carry=0; j<=digit; j++ ) { temp = a[j-1] * i + carry; //相应阶乘中的一项与当前所得临时结果的某位相乘(加上进位) a[j-1] = temp % 10; //更新临时结果的位上信息 carry = temp / 10; //看是否有进位 } while(carry) { //如果有进位 a[++digit-1] = carry % 10; //新加一位,添加信息。位数增1 carry = carry / 10; //看还能不能进位 } } printf("n ! = "); //显示结果 for(j = digit; j >=1;j--) { printf("%d",a[j-1]); } printf("\n"); return 0; }
❹ MPI 程序 计算时间问题
关于MPI程序中计时,推荐用函数MPI_Wtick.
至于单进程排序时间变少,应该是因为你用的机器的性能太好,排序操作所花费的时间相对与进程通信所花费的时间并不明显,于是就出现了多进程处理比单进程处理花费的时间大很多的表象。
❺ c语言实现MPI并行计算程序。要求使用partitioning and divide and conquer思想
http://wenku..com/link?url=gAn5gITm-_haS-Hx4kMJ16TidLl8cr2GXxMvKJ_xMSQ7d2dUhI9Lp39ha
❻ C语言程序太慢,想用MPI实现
可以用多线程,也可以用MPI;用多线程的前提要有多个处理器,现在的机器大多是多核的,还是比较实用。可以考虑一下。不管用哪种方式加速,都要分析你的程序是否存在可并发的部分,并发比重越大,加速比越高。不知道你的程序是个什么,所以简单建议一下啦
❼ 在Linux进行C语言编程的时候,程序里使用了mpi或者openmp或者都使用了该怎么进行编译执行谢谢
mpi是一个并行计算的东西,使用的是mpicc,mpic++来进行编译,例如
$ mpicc hello.c -o hello
$ ./hello
❽ linux下用mpi写c程序总是编译不了,求助
看设置了,我的反正可以运行。\r\n不过 X 里面运行字符界面的程序有个问题就是 X 一般不会显示终端窗口,所以除非你的程序是窗口程序,不然什么显示都没有就运行完成退出了。 \r\n--------------\r\n有的 linux 有一个\\“在终端中运行”的项目,这个可以保留终端字符界面来显示。不过现在大部分都没有了。\r\n现在你似乎要么写界面,要么这个程序自己启动一个终端窗口来运行。
❾ mpi 矩阵相乘 c语言
!
! a cross b.f
!
! Fixed-Format Fortran Source File
! Generated by PGI Visual Fortran(R)
! 2010-12-12 21:58:04
!
!Parallel matrix multiplication: main program
program cross
implicit double precision (a-h, o-z)
include 'mpif.h'
parameter (nbuffer=128*1024*1024/8)
dimension buf(nbuffer),buf2(nbuffer)
double precision time_start, time_end
external init, check, matmul
call MPI_Init(ierr)
call MPI_Comm_rank(MPI_COMM_WORLD, myrank, ierr)
call MPI_Comm_size(MPI_COMM_WORLD, nprocs, ierr)
if (myrank.eq.0) then
print *, 'Enter M, N, L: '
call flush(6)
read(*,*) M, N, L
endif
call MPI_Bcast(M, 1, MPI_INTEGER, 0, MPI_COMM_WORLD, ierr)
call MPI_Bcast(N, 1, MPI_INTEGER, 0, MPI_COMM_WORLD, ierr)
call MPI_Bcast(L, 1, MPI_INTEGER, 0, MPI_COMM_WORLD, ierr)
if ( mod(m,nprocs).ne.0 .or. mod(l,nprocs).ne.0 ) then
if (myrank.eq.0) print *, 'M or L cannot be divided by nprocs!'
call MPI_Finalize(ierr)
stop
endif
ia = 1
ib = ia + m/nprocs ! n
ic = ib + n ! l/nprocs
iwk = ic + m/nprocs ! l
iend = iwk + n ! l/nprocs
if ( iend .gt. nbuffer+1 ) then
if (myrank.eq.0) print *, 'Insufficient buffer size!'
call MPI_Finalize(ierr)
stop
endif
call init( m, n, l, myrank, nprocs, buf(ia), buf(ib), buf(ic)
& , buf2(ia),buf2(ib),buf2(ic) )
time_start = MPI_Wtime()
call matmul( m, n, l, myrank, nprocs, buf2(ia), buf2(ib), buf2(ic)
& , buf2(iwk) )
time_end = MPI_Wtime()
call check( m, n, l, myrank, nprocs, buf2(ia), buf2(ib), buf2(ic))
if ( myrank .eq. 0 ) then
print *, 'time = ', time_end-time_start
print *, 'mflops = ', m*(n+n-1.0)*l/(time_end-time_start)*1d-6
endif
print*,'ok'
call MPI_Finalize(ierr)
stop
end
!------------------------------------------------------------------
subroutine init(m, n, l, myrank, nprocs, a, b, c, a2, b2,c2)
implicit double precision (a-h, o-z)
include 'mpif.h'
dimension a(m/nprocs, n), b(n, l/nprocs), c(m/nprocs, l)
dimension a2(n, m/nprocs), b2(l/nprocs, n), c2(l,m/nprocs)
mloc = m/nprocs
lloc = l/nprocs
! Init. a, b
do j=1, n
do i=1, mloc
a(i,j) = i+myrank*mloc
enddo
enddo
do j=1, lloc
do i=1, n
b(i,j) = j+myrank*lloc
enddo
enddo
! Tranpose a, b -> a2, b2
do j=1, mloc
do i=1,n
a2(i,j) = a(j,i)
enddo
enddo
do j=1, n
do i=1,lloc
b2(i,j) = b(j,i)
enddo
enddo
return
end
!------------------------------------------------------------------
subroutine check(m, n, l, myrank, nprocs, a, b, c)
implicit double precision (a-h, o-z)
include 'mpif.h'
dimension a(m/nprocs, n), b(n, l/nprocs), c(m/nprocs, l)
!dimension a(n,m/nprocs), b(l/nprocs,n), c(l,m/nprocs)
integer local_code, code
mloc = m/nprocs
lloc = l/nprocs
!Check the results
local_code = 0
do i=1, l
do j=1, mloc
if ( abs(c(i,j) - n*dble(j+myrank*lloc)*i) .gt. 1d-10 ) then
local_code = 1
print*,'local_code=',local_code
goto 10
endif
enddo
enddo
10 call MPI_Rece( local_code, code, 1, MPI_INTEGER, MPI_SUM, 0,
& MPI_COMM_WORLD, ierr)
!
if ( myrank .eq. 0 ) then
print *, 'code = ', code
endif
!
return
end
* !Parallel multiplication of matrices using MPI_Isend/MPI_Irecv
*
subroutine matmul(m, n, l, myrank, nprocs, a, b, c, work)
implicit double precision (a-h, o-z)
include 'mpif.h'
dimension a(n,m/nprocs), b(l/nprocs,n), c(l/nprocs,m),
& work(n,m/nprocs)
integer src, dest, tag
integer status(MPI_STATUS_SIZE, 2), request(2)
*
mloc = m/nprocs
lloc = l/nprocs
*
dest = mod( myrank-1+nprocs, nprocs )
src = mod( myrank+1, nprocs )
*
jpos=myrank*mloc
print*,'myrank=',myrank
c print*,'dest=',dest,'src=',src
c print*,'jpos=',jpos,'tag=',tag
*
do ip=1, nprocs - 1
tag = 10000 + ip
*
call MPI_Isend( a, n*mloc, MPI_DOUBLE_PRECISION, dest, tag,
& MPI_COMM_WORLD, request(1), ierr )
call MPI_Irecv( work, n*mloc, MPI_DOUBLE_PRECISION, src, tag,
& MPI_COMM_WORLD, request(2), ierr )
*
do i=1, lloc
do j=1, mloc
sum=0.d0
do k=1, n
sum = sum + b(i,k) * a(k,j)
enddo
c(i, j+jpos) = sum
enddo
enddo
*
call MPI_Waitall(2, request, status, ierr)
*
* 拷贝 work -> b (可以通过在计算/通信中交替使用 b/work 来避该免操作)
do i=1, n
do j=1, mloc
a(i,j) = work(i,j)
enddo
enddo
*
jpos = jpos + mloc
if ( jpos .ge. m ) jpos = 0
*
enddo
*
do i=1, lloc
do j=1, mloc
sum=0.d0
do k=1, n
sum = sum + b(i,k) * a(k,j)
enddo
c(i, j+jpos) = sum
enddo
enddo
*
print*,'c(1,mloc)=',c(1,mloc)
print*,'c(1,2)=', c(1,2)
print*,'c(2,1)=', c(2,1)
print*,'c(lloc,1)=',c(lloc,1)
return
end