📄 63.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <style type="text/css"> body { font-family: Verdana, Arial, Helvetica, sans-serif;} a.at-term { font-style: italic; } </style> <title>Mapping Functions</title> <meta name="Generator" content="ATutor"> <meta name="Keywords" content=""></head><body> <h3>Mapping process grid coordinates to ranks</h3>
<p class="codelang">C:</p>
<pre><code>int MPI_Cart_rank(MPI_Comm comm, int *coords, int *rank)</code></pre>
<p class="codelang">Fortran:</p>
<pre><code>INTEGER COMM, COORDS(*), RANK, IERROR
CALL MPI_CART_RANK(COMM, COORDS, RANK, IERROR)</code></pre>
<h3>Mapping ranks to process grid coordinates</h3>
<p class="codelang">C:</p>
<pre><code>int MPI_Cart_coords(MPI_Comm comm, int rank, int maxdims, int *coords)</code></pre>
<p class="codelang">Fortran:</p>
<pre><code>INTEGER COMM, RANK, MAXDIMS, COORDS(*), IERROR
CALL MPI_CART_RANK(COMM, RANK, MAXDIMS, COORDS, IERROR)</code></pre>
<h3>Sample Program - C</h3>
<pre><code>#include <mpi.h>
#include <stdio.h>
#define TRUE 1
#define FALSE 0
/* Run with 12 processes */
int main(int argc, char *argv[]) {
int rank;
MPI_Comm vu;
int dim[2],period[2],reorder;
int coord[2],id;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
dim[0]=4; dim[1]=3;
period[0]=TRUE; period[1]=FALSE;
reorder=TRUE;
MPI_Cart_create(MPI_COMM_WORLD,2,dim,period,reorder,&vu);
if(rank==5){
MPI_Cart_coords(vu,rank,2,coord);
printf("P:%d My coordinates are %d %d\n",rank,coord[0],coord[1]);
}
if(rank==0) {
coord[0]=3; coord[1]=1;
MPI_Cart_rank(vu,coord,&id);
printf("The processor at position (%d, %d) has rank %d\n",coord[0],coord[1],id);
}
MPI_Finalize();
}</code></pre>
<h3>Program Output:</h3>
<pre><code>The processor at position (3,1) has rank 10
P:5 My coordinates are 1 2</code></pre>
<h3>Sample Program - Fortran</h3>
<pre><code> PROGRAM Cartesian
C
C Run with 12 processes
C
INCLUDE 'mpif.h'
INTEGER err, rank, size
integer vu,dim(2),coord(2),id
logical period(2),reorder
CALL MPI_INIT(err)
CALL MPI_COMM_RANK(MPI_COMM_WORLD,rank,err)
CALL MPI_COMM_SIZE(MPI_COMM_WORLD,size,err)
dim(1)=4
dim(2)=3
period(1)=.true.
period(2)=.false.
reorder=.true.
call MPI_CART_CREATE(MPI_COMM_WORLD,2,dim,period,reorder,vu,err)
if(rank.eq.5) then
call MPI_CART_COORDS(vu,rank,2,coord,err)
print*,'P:',rank,' my coordinates are',coord
end if
if(rank.eq.0) then
coord(1)=3
coord(2)=1
call MPI_CART_RANK(vu,coord,id,err)
print*,'P:',rank,' processor at position',coord,' is',id
end if
CALL MPI_FINALIZE(err)
END</code></pre>
<h3>Program output:</h3>
<pre><code>P:5 my coordinates are 1, 2
P:0 processor at position 3, 1 is 10</code></pre></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -