Ole.Moller.Nielsen@gmail.com
|
My favorites
▼
|
Profile
|
Sign out
pypar
Parallel programming for Python
Project Home
Downloads
Wiki
Issues
Source
Administer
Checkout
Browse
Changes
Request code review
Source path:
svn
/
source
/
mpi_test.c
Edit file
r98
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
Simple MPI communication test.
Ole Moller Nielsen - 2011
*/
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#define M 500000 /* Data size */
int main(int argc, char **argv) {
int repeats = 3, msgid = 0;
int myid, procs;
int j, k;
double A[M];
int namelen;
char processor_name[MPI_MAX_PROCESSOR_NAME];
MPI_Status stat;
/* Initialize */
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &procs);
MPI_Comm_rank(MPI_COMM_WORLD, &myid);
MPI_Get_processor_name(processor_name, &namelen);
if (myid == 0) {
printf("Number of processes = %d\n", procs);
printf("Test repeated %d times for reliability\n", repeats);
}
if (procs < 2) {
printf("Program needs at least two processors - aborting\n");
MPI_Abort(MPI_COMM_WORLD,999);
}
/* Create the data */
for (j=0; j<M; j++) {
A[j]=rand();
}
/* Synchronize */
MPI_Barrier(MPI_COMM_WORLD);
printf("I am process %d on node %s\n", myid, processor_name);
/* Pass msg circularly a number of times*/
for (k=0; k<repeats; k++) {
if (myid == 0) {
printf("Run %d of %d\n", k+1, repeats);
}
/* Communicate*/
if (myid == 0) {
printf("P%i: Sending to P%i\n", myid, 1);
MPI_Send(&A[0], M, MPI_DOUBLE, 1, msgid, MPI_COMM_WORLD);
MPI_Recv(&A[0], M, MPI_DOUBLE, procs-1, msgid, MPI_COMM_WORLD, &stat);
printf("P%i: Received from to P%i\n", myid, procs-1);
} else {
printf("P%i: Waiting to receive from to P%i\n", myid, myid-1);
MPI_Recv(&A[0], M, MPI_DOUBLE, myid-1, msgid, MPI_COMM_WORLD, &stat);
printf("P%i: Sending to to P%i\n", myid, (myid+1)%procs);
MPI_Send(&A[0], M, MPI_DOUBLE, (myid+1)%procs, msgid, MPI_COMM_WORLD);
}
}
printf("P%i: Done\n", myid);
MPI_Finalize();
return 0;
}
Show details
Hide details
Change log
r98
by Ole.Moller.Nielsen on Today (3 minutes ago)
Diff
Added small MPI test program
Go to:
/source/mpi_test.c
Publish your comments
Double click a line to add a comment
Older revisions
All revisions of this file
File info
Size: 1846 bytes, 78 lines
View raw file
Powered by
Google Project Hosting