waitany.c 7.35 KB
Newer Older
1
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
 *
 *  (C) 2001 by Argonne National Laboratory.
 *      See COPYRIGHT in top-level directory.
 */

#include "mpiimpl.h"

#if !defined(MPID_REQUEST_PTR_ARRAY_SIZE)
#define MPID_REQUEST_PTR_ARRAY_SIZE 16
#endif

/* -- Begin Profiling Symbol Block for routine MPI_Waitany */
#if defined(HAVE_PRAGMA_WEAK)
#pragma weak MPI_Waitany = PMPI_Waitany
#elif defined(HAVE_PRAGMA_HP_SEC_DEF)
#pragma _HP_SECONDARY_DEF PMPI_Waitany  MPI_Waitany
#elif defined(HAVE_PRAGMA_CRI_DUP)
#pragma _CRI duplicate MPI_Waitany as PMPI_Waitany
21
22
#elif defined(HAVE_WEAK_ATTRIBUTE)
int MPI_Waitany(int count, MPI_Request array_of_requests[], int *indx, MPI_Status *status) __attribute__((weak,alias("PMPI_Waitany")));
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#endif
/* -- End Profiling Symbol Block */

/* Define MPICH_MPI_FROM_PMPI if weak symbols are not supported to build
   the MPI routines */
#ifndef MPICH_MPI_FROM_PMPI
#undef MPI_Waitany
#define MPI_Waitany PMPI_Waitany

#endif

#undef FUNCNAME
#define FUNCNAME MPI_Waitany

/*@
    MPI_Waitany - Waits for any specified MPI Request to complete

Input Parameters:
+ count - list length (integer) 
- array_of_requests - array of requests (array of handles) 

Output Parameters:
45
+ indx - index of handle for operation that completed (integer).  In the
46
47
48
49
range '0' to 'count-1'.  In Fortran, the range is '1' to 'count'.
- status - status object (Status).  May be 'MPI_STATUS_IGNORE'.

Notes:
50
If all of the requests are 'MPI_REQUEST_NULL', then 'indx' is returned as
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
'MPI_UNDEFINED', and 'status' is returned as an empty status.

While it is possible to list a request handle more than once in the
array_of_requests, such an action is considered erroneous and may cause the
program to unexecpectedly terminate or produce incorrect results.

.N waitstatus

.N ThreadSafe

.N Fortran

.N Errors
.N MPI_SUCCESS
.N MPI_ERR_REQUEST
.N MPI_ERR_ARG
@*/
68
int MPI_Waitany(int count, MPI_Request array_of_requests[], int *indx,
69
70
71
72
73
74
75
76
77
		MPI_Status *status)
{
    static const char FCNAME[] = "MPI_Waitany";
    MPID_Request * request_ptr_array[MPID_REQUEST_PTR_ARRAY_SIZE];
    MPID_Request ** request_ptrs = request_ptr_array;
    MPID_Progress_state progress_state;
    int i;
    int n_inactive;
    int active_flag;
78
79
    int init_req_array;
    int found_nonnull_req;
80
    int last_disabled_anysource = -1;
81
82
83
84
85
86
    int mpi_errno = MPI_SUCCESS;
    MPIU_CHKLMEM_DECL(1);
    MPID_MPI_STATE_DECL(MPID_STATE_MPI_WAITANY);

    MPIR_ERRTEST_INITIALIZED_ORDIE();
    
87
    MPIU_THREAD_CS_ENTER(ALLFUNC,);
88
89
90
91
92
93
94
95
96
97
98
99
100
101
    MPID_MPI_PT2PT_FUNC_ENTER(MPID_STATE_MPI_WAITANY);

    /* Check the arguments */
#   ifdef HAVE_ERROR_CHECKING
    {
        MPID_BEGIN_ERROR_CHECKS;
        {
	    MPIR_ERRTEST_COUNT(count, mpi_errno);

	    if (count != 0) {
		MPIR_ERRTEST_ARGNULL(array_of_requests, "array_of_requests", mpi_errno);
		/* NOTE: MPI_STATUS_IGNORE != NULL */
		MPIR_ERRTEST_ARGNULL(status, "status", mpi_errno);
	    }
102
	    MPIR_ERRTEST_ARGNULL(indx, "indx", mpi_errno);
103
104
105
106
107
108
109
110
111
112
113
114
115
116
	}
        MPID_END_ERROR_CHECKS;
    }
#   endif /* HAVE_ERROR_CHECKING */
    
    /* ... body of routine ...  */
    
    /* Convert MPI request handles to a request object pointers */
    if (count > MPID_REQUEST_PTR_ARRAY_SIZE)
    {
	MPIU_CHKLMEM_MALLOC_ORJUMP(request_ptrs, MPID_Request **, count * sizeof(MPID_Request *), mpi_errno, "request pointers");
    }

    n_inactive = 0;
117
118
    init_req_array = TRUE;
    found_nonnull_req = FALSE;
119
120
121
122
123
124
    
    MPID_Progress_start(&progress_state);
    for(;;)
    {
	for (i = 0; i < count; i++)
	{
125
126
127
128
129
            if (init_req_array)
            {   
#ifdef HAVE_ERROR_CHECKING
                MPID_BEGIN_ERROR_CHECKS;
                {
130
                    MPIR_ERRTEST_ARRAYREQUEST_OR_NULL(array_of_requests[i], i, mpi_errno);
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
                }
                MPID_END_ERROR_CHECKS;
#endif /* HAVE_ERROR_CHECKING */
                if (array_of_requests[i] != MPI_REQUEST_NULL)
                {
                    MPID_Request_get_ptr(array_of_requests[i], request_ptrs[i]);
                    /* Validate object pointers if error checking is enabled */
#ifdef HAVE_ERROR_CHECKING
                    {
                        MPID_BEGIN_ERROR_CHECKS;
                        {
                            MPID_Request_valid_ptr( request_ptrs[i], mpi_errno );
                            if (mpi_errno != MPI_SUCCESS) goto fn_progress_end_fail;
                        }
                        MPID_END_ERROR_CHECKS;
                    }
#endif	    
                }
                else
                {
                    request_ptrs[i] = NULL;
                    ++n_inactive;
                }
            }
            if (request_ptrs[i] == NULL)
                continue;
            /* we found at least one non-null request */
            found_nonnull_req = TRUE;
159

160
            if (request_ptrs[i]->kind == MPID_UREQUEST && request_ptrs[i]->greq_fns->poll_fn != NULL)
161
	    {
162
                /* this is a generalized request; make progress on it */
163
                mpi_errno = (request_ptrs[i]->greq_fns->poll_fn)(request_ptrs[i]->greq_fns->grequest_extra_state, status);
164
		if (mpi_errno != MPI_SUCCESS) goto fn_progress_end_fail;
165
	    }
166
            if (MPID_Request_is_complete(request_ptrs[i]))
167
168
169
170
171
172
	    {
		mpi_errno = MPIR_Request_complete(&array_of_requests[i], 
						  request_ptrs[i], status, 
						  &active_flag);
		if (active_flag)
		{
173
		    *indx = i;
174
175
176
177
		    goto break_l1;
		}
		else
		{
178
		    ++n_inactive;
179
180
181
182
		    request_ptrs[i] = NULL;

		    if (n_inactive == count)
		    {
183
			*indx = MPI_UNDEFINED;
184
185
186
187
			/* status is set to empty by MPIR_Request_complete */
			goto break_l1;
		    }
		}
188
            } else if (unlikely(MPIR_CVAR_ENABLE_FT &&
189
                        MPID_Request_is_anysource(request_ptrs[i]) &&
190
                        !MPID_Comm_AS_enabled(request_ptrs[i]->comm))) {
191
192
                last_disabled_anysource = i;
            }
193
	}
194
195
196
197
198
        init_req_array = FALSE;

        if (!found_nonnull_req)
        {
            /* all requests were NULL */
199
            *indx = MPI_UNDEFINED;
200
201
202
203
            if (status != NULL)    /* could be null if count=0 */
                MPIR_Status_set_empty(status);
            goto break_l1;
        }
204

205
206
207
208
209
210
211
212
213
        /* If none of the requests completed, mark the last anysource request
         * as pending failure and break out. */
        if (unlikely(last_disabled_anysource != -1))
        {
            MPIU_ERR_SET(mpi_errno, MPIX_ERR_PROC_FAILED_PENDING, "**failure_pending");
            if (status != MPI_STATUS_IGNORE) status->MPI_ERROR = mpi_errno;
            goto fn_progress_end_fail;
        }

214
	mpi_errno = MPID_Progress_wait(&progress_state);
215
	if (mpi_errno != MPI_SUCCESS) goto fn_progress_end_fail;
216
217
218
219
220
221
222
223
224
225
226
227
228
    }
  break_l1:
    MPID_Progress_end(&progress_state);

    /* ... end of body of routine ... */
    
  fn_exit:
    if (count > MPID_REQUEST_PTR_ARRAY_SIZE)
    {
	MPIU_CHKLMEM_FREEALL();
    }

    MPID_MPI_PT2PT_FUNC_EXIT(MPID_STATE_MPI_WAITANY);
229
    MPIU_THREAD_CS_EXIT(ALLFUNC,);
230
231
    return mpi_errno;

232
233
234
  fn_progress_end_fail:
    MPID_Progress_end(&progress_state);

235
236
237
238
239
240
241
  fn_fail:
    /* --BEGIN ERROR HANDLING-- */
#ifdef HAVE_ERROR_CHECKING
    mpi_errno = MPIR_Err_create_code(mpi_errno, MPIR_ERR_RECOVERABLE, 
				     FCNAME, __LINE__, MPI_ERR_OTHER,
				     "**mpi_waitany", 
				     "**mpi_waitany %d %p %p %p", 
242
				     count, array_of_requests, indx, status);
243
244
245
246
247
#endif
    mpi_errno = MPIR_Err_return_comm(NULL, FCNAME, mpi_errno);
    goto fn_exit;
    /* --END ERROR HANDLING-- */
}