1 #include <stdio.h>
2 #include "mpi.h"
3 /*
4 * Increase/decrease this value to test if a process of a particular size can
5 * be spawned to a particular machine
6 */
7 #define WASTE_SIZE 100
8 #define NPROC 4
main(int narg,char ** args)9 main(int narg, char **args)
10 /*
11 * This program checks to make sure that you can run a basic program on your
12 * machine using MPI. Can increase WASTE_SIZE if you think size of executable
13 * may be causing launching problems.
14 */
15 {
16 int i, Iam, Np;
17 int irank[NPROC];
18 double WasteOfSpace[WASTE_SIZE];
19 MPI_Comm mcom;
20 MPI_Group wgrp, mgrp;
21 MPI_Status stat;
22
23 MPI_Init(&narg, &args);
24 MPI_Comm_size(MPI_COMM_WORLD, &Np);
25 if (Np < NPROC)
26 {
27 fprintf(stderr, "Not enough processes to run sanity check; need %d, but I've only got %d\n", NPROC, Np);
28 MPI_Abort(MPI_COMM_WORLD, -1);
29 }
30
31 for (i=0; i != WASTE_SIZE; i++) WasteOfSpace[i] = 0.0; /* page in Waste */
32 /*
33 * Form context with NPROC members
34 */
35 for (i=0; i != NPROC; i++) irank[i] = i;
36 MPI_Comm_group(MPI_COMM_WORLD, &wgrp);
37 MPI_Group_incl(wgrp, NPROC, irank, &mgrp);
38 MPI_Comm_create(MPI_COMM_WORLD, mgrp, &mcom);
39 MPI_Group_free(&mgrp);
40 /*
41 * Everyone in new communicator sends a message to his neighbor
42 */
43 if (mcom != MPI_COMM_NULL)
44 {
45 MPI_Comm_rank(mcom, &Iam);
46 /*
47 * Odd nodes receive first, so we don't hang if MPI_Send is globally blocking
48 */
49 if (Iam % 2)
50 {
51 MPI_Recv(&i, 1, MPI_INT, (NPROC+Iam-1)%NPROC, 0, mcom, &stat);
52 MPI_Send(&Iam, 1, MPI_INT, (Iam+1)%NPROC, 0, mcom);
53 }
54 else
55 {
56 MPI_Send(&Iam, 1, MPI_INT, (Iam+1)%NPROC, 0, mcom);
57 MPI_Recv(&i, 1, MPI_INT, (NPROC+Iam-1)%NPROC, 0, mcom, &stat);
58 }
59 /*
60 * Make sure we've received the right information
61 */
62 if (i != (NPROC+Iam-1)%NPROC)
63 {
64 fprintf(stderr, "Communication does not seem to work properly!!\n");
65 MPI_Abort(MPI_COMM_WORLD, -1);
66 }
67 }
68 fprintf(stdout, "%d: C MPI sanity test passed\n", Iam);
69 MPI_Finalize();
70 exit(0);
71 }
72