File: srvec.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 (207 lines) | stat: -rw-r--r-- 4,202 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
/*
 *  (C) 2001 by Argonne National Laboratory.
 *      See COPYRIGHT in top-level directory.
 */

/* define TEST_RECV_VECTOR to receive the data using the vector datatype.
   undefine TEST_RECV_VECTOR to receive the data into a contiguous array. */
#define TEST_RECV_VECTOR

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

int MPID_Progress_test(void);

int main(int argc, char **argv)
{
    int size;
    int rank;
    int niter = 1;
    int msg_count = 0;
    int msg_blocklength = 1;
    int msg_stride = 1;
    int msg_sz;
    int * buf;
    int buf_sz;
    int iter;
    int i;
    MPI_Datatype dt;
    
    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("srvec: size %d rank %d\n", size, rank); fflush(stdout);
    
    if (size < 2)
    {
	if (rank == 0)
	{
	    printf("ERROR: needs to be run with at least 2 procs\n");
	    fflush(stdout);
	}
	goto main_exit;
    }

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

    if (argc > 2)
    {
	sscanf(argv[2], "%d", &msg_count);
    }
    
    if (argc > 3)
    {
	sscanf(argv[3], "%d", &msg_blocklength);
    }
    
    if (argc > 4)
    {
	sscanf(argv[4], "%d", &msg_stride);
    }

    if (msg_stride < msg_blocklength)
    {
	if (rank == 0)
	{
	    printf("ERROR: stride < blocklength\n");
	    fflush(stdout);
	}
	goto main_exit;
    }
    

    msg_sz = msg_count * msg_blocklength;
    buf_sz = msg_count * msg_stride;
    
    if (rank == 0)
    {
	printf("niter=%d, msg_count=%d, msg_blocklength=%d, msg_stride=%d\n",
	    niter, msg_count, msg_blocklength, msg_stride);
	printf("msg_sz=%d, buf_sz=%d\n", msg_sz, buf_sz);
	fflush(stdout);
    }

    if (buf_sz > 0)
    {
	buf = (int *) malloc(buf_sz * sizeof(int));
	/* printf("%d: buf=%p\n", rank, buf); fflush(stdout); */
    }
    else
    {
	buf = NULL;
    }

    MPI_Type_vector(msg_count, msg_blocklength, msg_stride, MPI_INT, &dt);
    MPI_Type_commit(&dt);

    if (rank == 0)
    {
	/* usleep(10000); */

	for (i = 0; i < buf_sz; i++)
	{
	    buf[i] = INT_MAX;
	}
	
	for (iter = 0; iter < niter; iter++)
	{
	    for (i = 0; i < buf_sz; i++)
	    {
		buf[i] = iter * buf_sz + i;
	    }
	    
	    if (MPI_Send(buf, 1, dt, 1, iter, MPI_COMM_WORLD) != MPI_SUCCESS)
	    {
		printf("ERROR: problem with MPI_Send\n"); fflush(stdout);
	    }
	}
	
    }
    else if (rank == 1)
    {
	MPI_Status status;
	    
	for (i = 0; i < buf_sz; i++)
	{
	    buf[i] = INT_MIN;
	}

	for (iter = 0; iter < niter; iter++)
	{
#	    if defined(TEST_RECV_VECTOR)
	    {
		if (MPI_Recv(buf, 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++)
		{
		    const int expected = iter * buf_sz + i / msg_blocklength *
			msg_stride + i % msg_blocklength;
		    if (buf[i] != expected)
		    {
			printf("ERROR: %d != %d, i=%d iter=%d\n", buf[i],
			    expected, i, iter);
			fflush(stdout);
			abort();
		    }
		}
	    }
#	    else
	    {
		if (MPI_Recv(buf, 1, dt, 0, iter, MPI_COMM_WORLD, &status)
		    != MPI_SUCCESS)
		{
		    printf("ERROR: problem with MPI_Recv\n"); fflush(stdout);
		}
		
		for (i = 0; i < buf_sz; i++)
		{
		    if (i % msg_stride < msg_blocklength && buf[i] != iter *
			buf_sz + i)
		    {
			printf("ERROR: %d != %d, i=%d iter=%d\n", buf[i],
			    iter * buf_sz + i, i, iter);
			fflush(stdout);
			abort();
		    }
		}
	    }
#	    endif
	}
	
	printf("All messages successfully received!\n");
	fflush(stdout);
    }

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

    return 0;
}