File: sr.c

package info (click to toggle)
mpich 3.2-7
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 81,040 kB
  • ctags: 68,664
  • sloc: ansic: 358,905; f90: 54,597; perl: 18,527; cpp: 10,203; sh: 9,839; xml: 8,195; fortran: 7,799; makefile: 4,868; ruby: 53; sed: 9; php: 8
file content (120 lines) | stat: -rw-r--r-- 2,385 bytes parent folder | download | duplicates (2)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
/*
 *  (C) 2001 by Argonne National Laboratory.
 *      See COPYRIGHT in top-level directory.
 */

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

int main(int argc, char **argv)
{
    int size;
    int rank;
    int * msg = NULL;
    int msg_sz = 0;
    int niter = 1;
    int iter;
    int i;
    
    if (MPI_Init(&argc, &argv) != MPI_SUCCESS)
    {
        printf("ERROR: problem with MPI_Init\n"); fflush(stdout);
    }

    if (MPI_Comm_size(MPI_COMM_WORLD, &size) != MPI_SUCCESS)
    {
	printf("ERROR: problem with MPI_Comm_size\n"); fflush(stdout);
    }
    
    if (MPI_Comm_rank(MPI_COMM_WORLD, &rank) != MPI_SUCCESS)
    {
	printf("ERROR: problem with MPI_Comm_rank\n"); fflush(stdout);
    }

    printf("sr: size %d rank %d\n", size, rank); fflush(stdout);
    
    if (size < 2)
    {
	printf("ERROR: needs to be run with at least 2 procs\n");
	fflush(stdout);
    }

    if (argc > 1)
    {
	sscanf(argv[1], "%d", &msg_sz);
    }
    
    if (argc > 2)
    {
	sscanf(argv[2], "%d", &niter);
    }

    if (rank == 0)
    {
	printf("msg_sz=%d, niter=%d\n", msg_sz, niter);
	fflush(stdout);
    }
    
    if (msg_sz > 0)
    {
	msg = (int *) malloc(msg_sz * sizeof(int));
    }

    if (rank == 0)
    {
	/* usleep(10000); */
	    
	for (iter = 0; iter < niter; iter++)
	{
	    for (i = 0; i < msg_sz; i++)
	    {
		msg[i] = iter * msg_sz + i;
	    }
	    
	    if (MPI_Send(msg, msg_sz, MPI_INT, 1, iter, MPI_COMM_WORLD)
		!= MPI_SUCCESS)
	    {
		printf("ERROR: problem with MPI_Send\n"); fflush(stdout);
	    }
	}
	
    }
    else if (rank == 1)
    {
	MPI_Status status;
	    
	for (iter = 0; iter < niter; iter++)
	{
	    if (MPI_Recv(msg, msg_sz, MPI_INT, 0, iter, MPI_COMM_WORLD,
			 &status) != MPI_SUCCESS)
	    {
		printf("ERROR: problem with MPI_Recv\n"); fflush(stdout);
	    }
	
	    for (i = 0; i < msg_sz; i++)
	    {
		if (msg[i] != iter * msg_sz + i)
		{
		    printf("ERROR: %d != %d, i=%d iter=%d\n", msg[i],
			   iter * msg_sz + i, i, iter);
		    fflush(stdout);
		    abort();
		}
	    }
	}
	
	printf("All messages successfully received!\n");
	fflush(stdout);
    }

    printf("sr: process %d finished\n", rank); fflush(stdout);
    
    if (MPI_Finalize() != MPI_SUCCESS)
    {
        printf("ERROR: problem with MPI_Finalize\n"); fflush(stdout);
    }

    return 0;
}