Message Passing Interface (消息传递接口 MPI) is a standardized and portable message-passing standard designed to function on parallel computing architectures.[1]
The MPI standard defines the syntax 语法 and semantics 语意 of library routines that are useful to a wide range of users writing portable message-passing programs in C, C++, and Fortran.
There are several open-source MPI implementations (MPICH,Open MPI), which fostered the development of a parallel software industry, and encouraged development of portable and scalable large-scale parallel applications.
MPI hardware research focuses on implementing MPI directly in hardware, for example via processor-in-memory, building MPI operations into the microcircuitry of the RAM chips in each node. By implication, this approach is independent of language, operating system, and CPU, but cannot be readily updated or removed. MPI硬件研究的重点是直接在硬件中实现MPI,例如通过内存处理器,将MPI操作构建到每个节点中的RAM芯片的微电路中。通过暗示,这种方法独立于语言、操作系统和CPU,但是不能容易地更新或删除。
Another approach has been to add hardware acceleration to one or more parts of the operation, including hardware processing of MPI queues and using RDMA to directly transfer data between memory and the network interface controller(NIC 网卡) without CPU or OS kernel intervention. 另一种方法是将硬件加速添加到操作的一个或多个部分,包括MPI队列的硬件处理以及使用RDMA在存储器和网络接口控制器之间直接传输数据,而无需CPU或OS内核干预。
#include <unistd.h> char hostname[100]; gethostname(hostname,sizeof(hostname)); printf( "Hello world from process %d of %d: host: %s\n", rank, size, hostname);
int MPI_Pack(const void *inbuf, int incount, MPI_Datatype datatype, void *outbuf, int outsize, int *position, MPI_Comm comm) int MPI_Unpack(const void *inbuf, int insize, int *position, void *outbuf, int outcount, MPI_Datatype datatype, MPI_Comm comm)
The input value of position is the first location in the output buffer to be used for packing. position is incremented by the size of the packed message,
and the output value of position is the first location in the output buffer following the locations occupied by the packed message. The comm argument is the communicator that will be subsequently used for sending the packed message.
1 2
//Returns the upper bound on the amount of space needed to pack a message int MPI_Pack_size(int incount, MPI_Datatype datatype, MPI_Comm comm, int *size)
例子: 这里的A+i*j应该写成A+i*2吧???
派生数据类型(Derived Data Type)
来定义由数据类型不同且地址空间不连续的数据项组成的消息。
1 2 3 4 5 6 7 8
//启用与弃用数据类型 int MPI_Type_commit(MPI_Datatype * datatype) int MPI_Type_free(MPI_Datatype * datatype) //相同数据类型 int MPI_Type_contiguous(int count, MPI_Datatype oldtype, MPI_Datatype * newtype) //成块的相同元素组成的类型,块之间具有相同间隔 int MPI_Type_vector(int count, int blocklength, int stride, MPI_Datatype oldtype, MPI_Datatype * newtype)
1 2 3 4 5 6
//成块的相同元素组成的类型,块长度和偏移由参数指定 int MPI_Type_indexed(int count, const int *array_of_blocklengths, const int *array_of_displacements, MPI_Datatype oldtype, MPI_Datatype * newtype)
1 2 3 4 5
//由不同数据类型的元素组成的类型, 块长度和偏移(肯定也不一样)由参数指定 int MPI_Type_struct(int count, int *array_of_blocklengths, MPI_Aint * array_of_displacements, MPI_Datatype * array_of_types, MPI_Datatype * newtype)
// Makes a new communicator to which topology拓扑 information has been attached int MPI_Cart_create( MPI_Comm old_comm,//旧的通信域。这个通讯域中的所有进程都要调用该函数 int dims,//网格维数 number of dimensions of cartesian grid (integer) int* size,//长度为dims的数组,size[j]是第j维的进程数, integer array of size ndims specifying the number of processes in each dimension int* periodic,//长度为dims的数组,如果第j维有周期性,那么periodic[j]=1,否则为0 int reorder,//进程是否能重新被编号,如果为0则进程在新的通信域中仍保留在旧通信域的标号 MPI_Comm* cart_comm//该函数返回后,此变量将指向新的笛卡尔通信域 );
int MPI_Cart_rank(MPI_Comm comm, const int coords[], int *rank) //Determines process rank in communicator given Cartesian location //该函数的作用是通过进程在网格中的坐标获得它的进程号
int MPI_Cart_coords(MPI_Comm comm, int rank, int maxdims, int coords[]) //Determines process coords in cartesian topology given rank in group //该函数的作用是确定某个线程在虚拟网格中的坐标
通信域划分
1 2 3 4 5 6
int MPI_Comm_create(MPI_Comm comm, MPI_Group group, MPI_Comm * newcomm) //Creates a new communicator
int MPI_Comm_split(MPI_Comm comm, int color, int key, MPI_Comm * newcomm) 将某个通信域进一步划分为几组
组间通信域
点对点通信
特殊的函数
1 2 3 4 5 6 7
int MPI_Sendrecv(const void *sendbuf, int sendcount, MPI_Datatype sendtype, int dest, int sendtag, void *recvbuf, int recvcount, MPI_Datatype recvtype, int source, int recvtag, MPI_Comm comm, MPI_Status * status) int MPI_Sendrecv_replace(void *buf, int count, MPI_Datatype datatype, int dest, int sendtag, int source, int recvtag, MPI_Comm comm, MPI_Status * status)
特别适用于在进程链(环)中进行“移位”操作,而避免在通讯为阻塞方式时出现死锁。
There is also another error. The MPI standard requires that the send and the receive buffers be disjoint不相交 (i.e. they should not overlap重叠), which is not the case with your code. Your send and receive buffers not only overlap but they are one and the same buffer. If you want to perform the swap in the same buffer, MPI provides the MPI_Sendrecv_replace operation.
const double a = 0.0; const double b = 3.1415926; int n = 100; double h = (b - a) / n;
double trap(double a, double b, int n, double h) { double*x = new double[n + 1]; double*f = new double[n + 1]; double inte = (sin(a) + sin(b)) / 2; for (int i = 1; i<n + 1; i++) { x[i] = x[i - 1] + h; /*x_0=a,x_n=b*/ f[i] = sin(x[i]); inte += f[i]; } inte = inte*h; /* inte=h*[f(a)/2+f(x_1)+...f(x_{n-1})+f(b)/2]*/ return inte; }
int main(int argc, char * argv[]) { int myid, nprocs; int local_n; double local_a; double local_b; double total_inte;
MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &myid); /* get current process id */ MPI_Comm_size(MPI_COMM_WORLD, &nprocs); /* get number of processes */
int count(1); #pragma omp parallel num_threads(64) { #pragma omp single { int c = 0; while(c < count) { for( ; c < count; c++ ) { #pragma omp task{ for( int n = 0; n < 4; n++ ) { int x = xvec[c] + dx4[n]; int y = yvec[c] + dy4[n];
if( (x >= 0 && x < width) && (y >= 0 && y < height) ) { int nindex = y*width + x;