MPI学习一

1.第一个MPI程序

MPI学习一

 

 

 

 1 #include <mpi.h>
 2 #include <stdio.h>
 3 int main(int argc, char **argv)
 4 { 
 5     //your code here
 6     MPI_Init(&argc, &argv);
 7     
 8     printf("Hello World!\n");
 9     
10     MPI_Finalize();
11     //end of your code 
12     
13     return 0;
14 }

2.获取进行数量

MPI学习一

 

 

 

 1 #include <stdio.h>
 2 #include <mpi.h>
 3 
 4 int main(int argc, char **argv)
 5 {
 6     int numprocs;
 7     MPI_Init(&argc, &argv);
 8 
 9     //your code here
10     MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
11     //end of your code
12 
13     printf("Hello World! The number of processes is %d\n",numprocs);
14 
15     MPI_Finalize();
16     return 0;
17 }

3.获取进程ID

MPI学习一

 

 

 

 1 #include <stdio.h>
 2 #include <mpi.h>
 3 
 4 int main(int argc, char **argv)
 5 {
 6     int myid, numprocs;
 7     MPI_Init(&argc, &argv);
 8 
 9     MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
10     
11     //your code here
12     MPI_Comm_rank(MPI_COMM_WORLD, &myid);
13     //end of your code
14 
15     printf("Hello World!I'm rank %d of %d\n", myid, numprocs);
16 
17     MPI_Finalize();
18     return 0;
19 }

4.获取处理器名

MPI学习一

 

 

 

 1 #include <stdio.h>
 2 #include <mpi.h>
 3 
 4 int main(int argc, char **argv)
 5 {
 6     int len;
 7     char name[MPI_MAX_PROCESSOR_NAME];
 8     MPI_Init(&argc, &argv);
 9 
10     //your code here
11     MPI_Get_processor_name (name, &len);
12     //end of your code
13 
14     printf("Hello, world. I am %s.\n", name);
15 
16     MPI_Finalize();
17     return 0;
18 }

5.运行时间

MPI学习一

 

 

 

 1 #include<stdio.h>
 2 #include<mpi.h>
 3 
 4 int main(int argc, char **argv)
 5 {
 6     int myid, numprocs;
 7     double start, finish;
 8     
 9     MPI_Init(&argc, &argv);
10 
11     MPI_Comm_rank(MPI_COMM_WORLD, &myid);
12     MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
13 
14     //your code here
15     start = MPI_Wtime();
16     
17     printf("The precision is: %f\n", MPI_Wtick());
18     
19     finish = MPI_Wtime();
20     //your code here
21     
22     printf("Hello World!I'm rank %d of %d, running %f seconds.\n", myid, numprocs, finish-start);
23 
24     MPI_Finalize();
25     return 0;
26 }

6.同步

MPI学习一

 

 

 

#include<stdio.h>
#include<mpi.h>

int main(int argc, char **argv)
{
    int myid, numprocs;
    double start, finish;
    
    MPI_Init(&argc, &argv);

    MPI_Comm_rank(MPI_COMM_WORLD, &myid);
    MPI_Comm_size(MPI_COMM_WORLD, &numprocs);

    //your code here
    MPI_Barrier(MPI_COMM_WORLD);
    //end of your code
    
    start = MPI_Wtime();
    
    printf("The precision is: %f\n", MPI_Wtick());
    
    finish = MPI_Wtime();
    
    printf("Hello World!I'm rank %d of %d, running %f seconds.\n", myid, numprocs, finish-start);

    MPI_Finalize();
    return 0;
}

7.消息传递

MPI学习一MPI学习一

 

 

 

int MPI_Send(void* msg_buf_p, int msg_size, MPI_Datatype msg_type, int dest, int tag, MPI_Comm communicator)
int MPI_Recv(void* msg_buf_p, int buf_size, MPI_Datatype msg_type, int source, int tag, MPI_Comm communicator, MPI_Status *status_p)
 1 #include <stdio.h>
 2 #include <mpi.h>
 3 
 4 int main(int argc, char **argv)
 5 {
 6     int myid, numprocs, source;
 7     MPI_Status status;
 8     char message[100];
 9 
10     MPI_Init(&argc, &argv);
11     MPI_Comm_rank(MPI_COMM_WORLD, &myid);
12     MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
13     
14     if(myid != 0) {
15         strcpy(message, "hello world!");
16         
17         //your code here
18         MPI_Send(message, strlen(message)+1, MPI_CHAR, 0, 99, MPI_COMM_WORLD);
19         //end of your code
20     }
21     else { //myid == 0
22         for(source=1; source<numprocs; source++) {
23             //your code here
24             MPI_Recv(message, 100, MPI_CHAR, source, 99, MPI_COMM_WORLD, &status);
25             //end of your code
26             
27             printf("%s\n", message);
28         }
29     }
30 
31     MPI_Finalize();
32     return 0;
33 }

8.地址偏移量

MPI学习一

 

 

 

 1 #include<stdio.h>
 2 #include<mpi.h>
 3 
 4 int main(int argc, char **argv)
 5 {
 6     int myid, numprocs;
 7     MPI_Aint address1, address2, address3;
 8     int a, b, c, dist1, dist2;
 9     
10     a = 1;
11     b = 2;
12     c = 3;
13     
14     MPI_Init(&argc, &argv);
15     
16     MPI_Comm_rank(MPI_COMM_WORLD, &myid);
17     MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
18     
19     
20     // your code here
21     MPI_Address(&a, &address1);
22     MPI_Address(&b, &address2);
23     MPI_Address(&c, &address3);
24     // end of your code
25     
26     dist1 = address2 - address1 ;
27     dist2 = address3 - address1 ;
28     
29     if(myid == 0) {
30         printf("The distance between a and b is %d\n", dist1);
31         printf("The distance between a and c is %d\n", dist2);
32     }
33 
34     MPI_Finalize();
35     return 0;
36 }

9.数据的打包

MPI学习一

 

 

 

int MPI_Pack(void* inbuf, int incount, MPI_datatype datatype, void *outbuf, int outcount, int *position, MPI_Comm comm) 

void* inbuf : 输入缓冲区地址;
int incount :输入数据项数目;
MPI_datatype datatype :数据项的类型;
void *outbuf :输出缓冲区地址;
int outcount :输出缓冲区大小;
int *position :缓冲区当前位置;
MPI_Comm comm :通信子;
 1 #include <stdio.h>
 2 #include <mpi.h>
 3 
 4 int main(int argc, char **argv)
 5 {
 6     int myid, numprocs, source;
 7     MPI_Status status;
 8     int i, j, position;
 9     int k[2];
10     int buf[1000];
11 
12     MPI_Init(&argc, &argv);
13     MPI_Comm_rank(MPI_COMM_WORLD, &myid);
14     MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
15     
16     i = 1;
17     j = 2;
18     
19     if(myid == 0) {
20         
21         position = 0 ;
22         
23         // your code here
24         MPI_Pack(&i, 1, MPI_INT, buf, 1000, &position, MPI_COMM_WORLD); 
25         MPI_Pack(&j, 1, MPI_INT, buf, 1000, &position, MPI_COMM_WORLD); 
26         // end of your code
27         
28         MPI_Send(buf, position, MPI_PACKED, 1, 99, MPI_COMM_WORLD); 
29     }
30     else if (myid == 1){ 
31         MPI_Recv(k, 2, MPI_INT, 0, 99, MPI_COMM_WORLD, &status);
32         
33         position = 0 ;
34         
35         MPI_Unpack(k, 2, &position, &i, 1, MPI_INT, MPI_COMM_WORLD);
36         MPI_Unpack(k, 2, &position, &j, 1, MPI_INT, MPI_COMM_WORLD);
37         
38         printf("The number is %d and %d", i, j);
39     }
40 
41     MPI_Finalize();
42     return 0;
43 }

10.数据的解包

MPI学习一

 

 

 

int MPI_Unpack(void* inbuf, int insize, int *position, void *outbuf, int outcount, MPI_Datatype datatype, MPI_Comm comm) 

void* inbuf : 输入缓冲区地址;
int insize :输入数据项数目;
MPI_datatype datatype :数据项的类型;
void *outbuf :输出缓冲区地址;
int outcount :输出缓冲区大小;
int *position :缓冲区当前位置;
MPI_Comm comm :通信子;
 1 #include <stdio.h>
 2 #include <mpi.h>
 3 
 4 int main(int argc, char **argv)
 5 {
 6     int myid, numprocs, source;
 7     MPI_Status status;
 8     int i, j, position;
 9     int k[2];
10     int buf[1000];
11 
12     MPI_Init(&argc, &argv);
13     MPI_Comm_rank(MPI_COMM_WORLD, &myid);
14     MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
15     
16     i = 1;
17     j = 2;
18     
19     if(myid == 0) {
20         
21         position = 0 ;
22         
23         MPI_Pack(&i, 1, MPI_INT, buf, 1000, &position, MPI_COMM_WORLD); 
24         MPI_Pack(&j, 1, MPI_INT, buf, 1000, &position, MPI_COMM_WORLD); 
25         
26         MPI_Send(buf, position, MPI_PACKED, 1, 99, MPI_COMM_WORLD); 
27     }
28     else if (myid == 1){ 
29         MPI_Recv(k, 2, MPI_INT, 0, 99, MPI_COMM_WORLD, &status);
30         
31         position = 0 ;
32         i = j = 0;
33         
34         // your code here
35         MPI_Unpack(k, 2, &position, &i, 1, MPI_INT, MPI_COMM_WORLD);
36         MPI_Unpack(k, 2, &position, &j, 1, MPI_INT, MPI_COMM_WORLD);
37         // end of your code
38         
39         printf("The number is %d and %d", i, j);
40     }
41 
42     MPI_Finalize();
43     return 0;
44 }

 来自:MPI编程实训学习(超算习堂)

上一篇:vs下安装配置使用mpi


下一篇:高性能计算——MPI并行编程