當前位置:首頁 » 編程語言 » c語言mpi測試代碼
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言mpi測試代碼

發布時間: 2023-01-29 17:43:33

❶ 關於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