當前位置:首頁 » 編程語言 » c語言mpi並行程序

c語言mpi並行程序

發布時間: 2023-05-24 12:27:52

c語言實現MPI並行計算程序。要求使用partitioning and divide and conquer思想

http://wenku..com/link?url=gAn5gITm-_haS-Hx4kMJ16TidLl8cr2GXxMvKJ_xMSQ7d2dUhI9Lp39ha

㈡ 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

㈢ 在Linux進行C語言編程的時候,程序里使用了mpi或者openmp或者都使用了該怎麼進行編譯執行謝謝

mpi或者openmp
這個,你指的是庫?
如果是的話,編譯時,加上鏈接庫的編譯選項就可以。
比如 gcc -hello.c -o hello -lm -lxml -L/usr/local/lib -lts
-lm 鏈接了math庫
-lxml鏈接了xml庫
-L/usr/local/lib -lts 鏈接了ts庫,ts庫存在目錄/usr/local/lib中

㈣ 急求一關於求解大規模稀疏矩陣的MPI並行計算程序(基於C語言)

這個大哥牛逼啊!!!!楊老闆要看到了肯定怨念重重啊!!!

㈤ 我們來講解以下如何才能編寫並行程序,以及如何編譯運行

我們下面以C 語言為例。
具體語法規則可參看《高性能計算並行編程技術-MPI 並行程序設計》一書。
mpicc -o outfilename cpi.c
其中outfilename 為編譯後的輸出文件,cpi.c 為源代碼.
可將cpi.c 下載後上傳的自己目錄下編譯.
例如:mpicc -o cpi cpi.c
如沒有安裝OpenPBS 則:
mpirun -np 4 cpi
否則:(一般安裝了)
之後需寫一作業提交腳本.例如:submit 內容如下:
#PBS -l nodes=nodes number
#PBS -N jobname#PBS -j oecd /home/xmin/Project
/usr/local/bin/mpiexec cpi
其中 #PBS -l nodes=nodes number 為指定幾個節點計算.如: nodes=4
#PBS -N jobname 為用戶命名的提交作業名稱.如: #PBS -N xmin
#PBS -j oe 為結果和錯誤輸出同文件.如無此項則分別在兩個文件中.
cd /home/xmin/Project 編譯後的輸出文件所在路徑(從根目錄開始).
/usr/local/bin/mpiexec cpi 為mpiexec 所在路徑.
下面是完整例子:
#PBS -l nodes=4
#PBS -N xmin#PBS -j oecd /home/xmin/Project
/usr/local/bin/mpiexec cpi
提交腳本如下:
qsub submit得到如下:3565.isc.math.nankai.e.cn
此為你的作業編號.
這樣你就可得到類似xmin.o2666 的文件,打開即可看到結果.
你還可以查詢作業提交情況.命令如下:qstat

㈥ 求解並行題目: 以下是一段用MPI 實現的並行程序代碼,用來並行求一組數的和。 #include <mpi.h> #include

#include <mpi.h>

#include <stdio.h>

#include <math.h>

#define SIZE 10

void main(int argc, char *argv)

{

int myid, numprocs;

int data[SIZE], i, x, low, high, myresult, result;

char fn[255];

char *fp;

MPI_Init(&argc,&argv);

MPI_Comm_size(MPI_COMM_WORLD,&numprocs);

MPI_Comm_rank(MPI_COMM_WORLD,&myid);

if (myid == 0) { /* Open input file and initialize data */

strcpy(fn,getenv("HOME"));

strcat(fn,"/data");

if ((fp = fopen(fn,"r"答斗)) == NULL) {

printf("Can』t open the input file: %s\n\n", fn);

exit(1);

}

for(i = 0; i <消陸 SIZE; i++) fscanf(fp,"%d", &data[i]);

}

/* broadcast data */

MPI_Bcast(data, SIZE, MPI_INT, 0, MPI_COMM_WORLD);

/* Add my portion Of data */清橋磨

x = SIZE/numprocs;

low = myid * x;

high = low + x;

if(myid == numprocs - 1) high = SIZE;

myresult = 0;

for(i = low; i < high; i++)

myresult += data[i];

/* Compute global sum */

MPI_Rece(&myresult, &result, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);

if (myid == 0) printf("The sum is %d.\n", result);

MPI_Finalize();

}

㈦ C語言中的MPI編程和多線程有什麼區別,MPI編程中針對的是一台電腦多核還是多台電腦謝謝!

MPI(MPI是一個標准,有不同的具體實現,比如MPICH等)是多主機聯網協作進行並行計算的工具,當然也可以用於單主機上多核/多CPU的並行計算,不過效率低。它能協調多台主機間的並行計算,因此並行規模上的可伸縮性很強,能在從個人電腦到世界TOP10的超級計算機上使用。缺點是使用進程間通信的方式協調並行計算,這導致並行效率較低、內存開銷大、不直觀、編程麻煩。OpenMP是針對單主機上多核/多CPU並行計算而設計的工具,換句話說,OpenMP更適合單台計算機共享內存結構上的並行計算。由於使用線程間共享內存的方式協調並行計算,它在多核/多CPU結構上的效率很高、內存開銷小、編程語句簡潔直觀,因此編程容易、編譯器實現也容易(現在最新版的C、C++、Fortran編譯器基本上都內置OpenMP支持)。不過OpenMP最大的缺點是只能在單台主機上工作,不能用於多台主機間的並行計算!如果要多主機聯網使用OpenMP(比如在超級計算機上),那必須有額外的工具幫助,比如MPI+OpenMP混合編程。或者是將多主機虛擬成一個共享內存環境(Intel有這樣的平台),但這么做效率還不如混合編程,唯一的好處是編程人員可以不必額外學習MPI編程。

熱點內容
ct4哪個配置性價比最高 發布:2025-05-19 15:38:02 瀏覽:952
如何設置強緩存的失效時間 發布:2025-05-19 15:21:28 瀏覽:695
winxp無法訪問 發布:2025-05-19 15:19:48 瀏覽:947
文件預編譯 發布:2025-05-19 15:14:04 瀏覽:643
怎麼在伺服器上掛公網 發布:2025-05-19 15:14:02 瀏覽:272
濟南平安e通如何找回密碼 發布:2025-05-19 14:56:58 瀏覽:176
安卓手機如何找到iccid碼 發布:2025-05-19 14:46:51 瀏覽:227
編譯的內核為什麼那麼大 發布:2025-05-19 14:45:21 瀏覽:179
什麼控制壓縮 發布:2025-05-19 14:28:13 瀏覽:931
網路伺服器忙指什麼 發布:2025-05-19 14:28:10 瀏覽:189