type_get_attr.c 6.39 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
/*
 *
 *  (C) 2001 by Argonne National Laboratory.
 *      See COPYRIGHT in top-level directory.
 */

#include "mpiimpl.h"
#include "attr.h"

/* -- Begin Profiling Symbol Block for routine MPI_Type_get_attr */
#if defined(HAVE_PRAGMA_WEAK)
#pragma weak MPI_Type_get_attr = PMPI_Type_get_attr
#elif defined(HAVE_PRAGMA_HP_SEC_DEF)
#pragma _HP_SECONDARY_DEF PMPI_Type_get_attr  MPI_Type_get_attr
#elif defined(HAVE_PRAGMA_CRI_DUP)
#pragma _CRI duplicate MPI_Type_get_attr as PMPI_Type_get_attr
18
19
#elif defined(HAVE_WEAK_ATTRIBUTE)
int MPI_Type_get_attr(MPI_Datatype datatype, int type_keyval, void *attribute_val, int *flag) __attribute__((weak,alias("PMPI_Type_get_attr")));
20
21
22
23
24
25
26
27
28
#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_Type_get_attr
#define MPI_Type_get_attr PMPI_Type_get_attr

29
30
31
#undef FUNCNAME
#define FUNCNAME MPIR_TypeGetAttr

32
int MPIR_TypeGetAttr( MPI_Datatype datatype, int type_keyval, void *attribute_val,
33
		      int *flag, MPIR_AttrType outAttrType )
34
35
36
37
38
39
40
{
#ifdef HAVE_ERROR_CHECKING
    static const char FCNAME[] = "MPI_Type_get_attr";
#endif
    int mpi_errno = MPI_SUCCESS;
    MPID_Datatype *type_ptr = NULL;
    MPID_Attribute *p;
41
    MPID_MPI_STATE_DECL(MPID_STATE_MPIR_TYPE_GET_ATTR);
42
43
44

    MPIR_ERRTEST_INITIALIZED_ORDIE();
    
45
    MPIU_THREAD_CS_ENTER(ALLFUNC,);
46
    MPID_MPI_FUNC_ENTER(MPID_STATE_MPIR_TYPE_GET_ATTR);
47
48
49
50
51
52
    
    /* Validate parameters, especially handles needing to be converted */
#   ifdef HAVE_ERROR_CHECKING
    {
        MPID_BEGIN_ERROR_CHECKS;
        {
53
	    MPIR_ERRTEST_DATATYPE(datatype, "datatype", mpi_errno);
54
55
56
57
58
	    MPIR_ERRTEST_KEYVAL(type_keyval, MPID_DATATYPE, "datatype", mpi_errno);
#           ifdef NEEDS_POINTER_ALIGNMENT_ADJUST
            /* A common user error is to pass the address of a 4-byte
	       int when the address of a pointer (or an address-sized int)
	       should have been used.  We can test for this specific
59
	       case.  Note that this code assumes sizeof(MPIR_Pint) is 
60
	       a power of 2. */
61
	    if ((MPIR_Pint)attribute_val & (sizeof(MPIR_Pint)-1)) {
62
		MPIU_ERR_SETANDSTMT(mpi_errno,MPI_ERR_ARG,goto fn_fail,"**attrnotptr");
63
64
65
66
67
68
69
70
	    }
#           endif
        }
        MPID_END_ERROR_CHECKS;
    }
#   endif

    /* Convert MPI object handles to object pointers */
71
    MPID_Datatype_get_ptr( datatype, type_ptr );
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
    
    /* Validate parameters and objects (post conversion) */
#   ifdef HAVE_ERROR_CHECKING
    {
        MPID_BEGIN_ERROR_CHECKS;
        {
	    /* Validate datatype pointer */
	    MPID_Datatype_valid_ptr( type_ptr, mpi_errno );
	    /* If type_ptr is not valid, it will be reset to null */
            if (mpi_errno) goto fn_fail;
        }
        MPID_END_ERROR_CHECKS;
    }
#   endif /* HAVE_ERROR_CHECKING */

    /* ... body of routine ...  */
    
    *flag = 0;
    p = type_ptr->attributes;
    while (p) {
	if (p->keyval->handle == type_keyval) {
	    *flag = 1;
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
	    if (outAttrType == MPIR_ATTR_PTR) {
		if (p->attrType == MPIR_ATTR_INT) {
		    /* This is the tricky case: if the system is
		       bigendian, and we have to return a pointer to
		       an int, then we may need to point to the 
		       correct location in the word. */
#if defined(WORDS_LITTLEENDIAN) || (SIZEOF_VOID_P == SIZEOF_INT)
		    *(void**)attribute_val = &(p->value);
#else
		    int *p_loc = (int *)&(p->value);
#if SIZEOF_VOID_P == 2 * SIZEOF_INT
		    p_loc++;
#else 
#error Expected sizeof(void*) to be either sizeof(int) or 2*sizeof(int)
#endif
		    *(void **)attribute_val = p_loc;
#endif
		}
		else if (p->attrType == MPIR_ATTR_AINT) {
		    *(void**)attribute_val = &(p->value);
		}
		else {
116
		    *(void**)attribute_val = (void *)(MPIR_Pint)(p->value);
117
118
		}
	    }
119
	    else
120
		*(void**)attribute_val = (void *)(MPIR_Pint)(p->value);
121
	    
122
123
124
125
126
127
128
129
130
131
	    break;
	}
	p = p->next;
    }
    
    /* ... end of body of routine ... */

#ifdef HAVE_ERROR_CHECKING
  fn_exit:
#endif
132
    MPID_MPI_FUNC_EXIT(MPID_STATE_MPIR_TYPE_GET_ATTR);
133
    MPIU_THREAD_CS_EXIT(ALLFUNC,);
134
135
136
137
    return mpi_errno;

    /* --BEGIN ERROR HANDLING-- */
#   ifdef HAVE_ERROR_CHECKING
138
139
140
141
142
143
  fn_fail:
    {
	mpi_errno = MPIR_Err_create_code(
	    mpi_errno, MPIR_ERR_RECOVERABLE, FCNAME, __LINE__, MPI_ERR_OTHER, 
	    "**mpir_type_get_attr",
	    "**mpir_type_get_attr %D %d %p %p", 
144
	    datatype, type_keyval, attribute_val, flag);
145
146
147
148
149
150
151
152
153
154
    }
    mpi_errno = MPIR_Err_return_comm( NULL, FCNAME, mpi_errno );
    goto fn_exit;
#   endif
    /* --END ERROR HANDLING-- */
}
#endif

#undef FUNCNAME
#define FUNCNAME MPI_Type_get_attr
155
156
#undef FCNAME
#define FCNAME MPIU_QUOTE(FUNCNAME)
157
158
159
160

/*@
   MPI_Type_get_attr - Retrieves attribute value by key

161
Input Parameters:
162
+ datatype - datatype to which the attribute is attached (handle)
163
164
- type_keyval - key value (integer) 

165
Output Parameters:
166
167
168
169
170
171
172
173
174
+ attribute_val - attribute value, unless flag = false 
- flag - false if no attribute is associated with the key (logical) 

   Notes:
    Attributes must be extracted from the same language as they were inserted  
    in with 'MPI_Type_set_attr'.  The notes for C and Fortran below explain 
    why. 

Notes for C:
175
    Even though the 'attr_value' argument is declared as 'void *', it is
176
177
178
179
180
181
182
183
184
185
186
187
    really the address of a void pointer.  See the rationale in the 
    standard for more details. 

.N ThreadSafe

.N Fortran

.N Errors
.N MPI_SUCCESS
.N MPI_ERR_KEYVAL
.N MPI_ERR_ARG
@*/
188
int MPI_Type_get_attr(MPI_Datatype datatype, int type_keyval, void *attribute_val,
189
190
191
192
193
194
195
196
197
198
		      int *flag)
{
    int mpi_errno = MPI_SUCCESS;
    MPID_MPI_STATE_DECL(MPID_STATE_MPI_TYPE_GET_ATTR);

    MPIR_ERRTEST_INITIALIZED_ORDIE();
    
    MPID_MPI_FUNC_ENTER(MPID_STATE_MPI_TYPE_GET_ATTR);
    
    /* ... body of routine ...  */
199
    mpi_errno = MPIR_TypeGetAttr( datatype, type_keyval, attribute_val, flag,
200
201
202
203
204
205
206
207
208
209
				  MPIR_ATTR_PTR );
    if (mpi_errno) goto fn_fail;
    
    /* ... end of body of routine ... */

  fn_exit:
    MPID_MPI_FUNC_EXIT(MPID_STATE_MPI_TYPE_GET_ATTR);
    return mpi_errno;

    /* --BEGIN ERROR HANDLING-- */
210
  fn_fail:
211
#ifdef HAVE_ERROR_CHECKING
212
213
214
215
216
    {
	mpi_errno = MPIR_Err_create_code(
	    mpi_errno, MPIR_ERR_RECOVERABLE, FCNAME, __LINE__, MPI_ERR_OTHER, 
	    "**mpi_type_get_attr",
	    "**mpi_type_get_attr %D %d %p %p", 
217
	    datatype, type_keyval, attribute_val, flag);
218
    }
219
#endif
220
    mpi_errno = MPIR_Err_return_comm( NULL, FCNAME, mpi_errno );
221
222
223
    goto fn_exit;
    /* --END ERROR HANDLING-- */
}