type_set_attr.c 6.78 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_set_attr */
#if defined(HAVE_PRAGMA_WEAK)
#pragma weak MPI_Type_set_attr = PMPI_Type_set_attr
#elif defined(HAVE_PRAGMA_HP_SEC_DEF)
#pragma _HP_SECONDARY_DEF PMPI_Type_set_attr  MPI_Type_set_attr
#elif defined(HAVE_PRAGMA_CRI_DUP)
#pragma _CRI duplicate MPI_Type_set_attr as PMPI_Type_set_attr
18
19
#elif defined(HAVE_WEAK_ATTRIBUTE)
int MPI_Type_set_attr(MPI_Datatype datatype, int type_keyval, void *attribute_val) __attribute__((weak,alias("PMPI_Type_set_attr")));
20
21
22
23
24
25
26
27
#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_set_attr
#define MPI_Type_set_attr PMPI_Type_set_attr
28
29
30

#undef FUNCNAME
#define FUNCNAME MPIR_TypeSetAttr
31
int MPIR_TypeSetAttr(MPI_Datatype datatype, int type_keyval, void *attribute_val,
32
		     MPIR_AttrType attrType )
33
{
34
    static const char FCNAME[] = "MPIR_TypeSetAttr";
35
36
37
38
    int mpi_errno = MPI_SUCCESS;
    MPID_Datatype *type_ptr = NULL;
    MPID_Keyval *keyval_ptr = NULL;
    MPID_Attribute *p, **old_p;
39
    MPID_MPI_STATE_DECL(MPID_STATE_MPIR_TYPE_SET_ATTR);
40
41
42
43
44

    MPIR_ERRTEST_INITIALIZED_ORDIE();
    
    /* The thread lock prevents a valid attr delete on the same datatype
       but in a different thread from causing problems */
45
    MPIU_THREAD_CS_ENTER(ALLFUNC,);
46
    MPID_MPI_FUNC_ENTER(MPID_STATE_MPIR_TYPE_SET_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
59
60
61
	    MPIR_ERRTEST_KEYVAL(type_keyval, MPID_DATATYPE, "datatype", mpi_errno);
	    MPIR_ERRTEST_KEYVAL_PERM(type_keyval, mpi_errno);
        }
        MPID_END_ERROR_CHECKS;
    }
#   endif

    /* Convert MPI object handles to object pointers */
62
    MPID_Datatype_get_ptr( datatype, type_ptr );
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
    MPID_Keyval_get_ptr( type_keyval, keyval_ptr );
    
    /* Validate parameters and objects (post conversion) */
#   ifdef HAVE_ERROR_CHECKING
    {
        MPID_BEGIN_ERROR_CHECKS;
        {
            /* Validate type_ptr */
            MPID_Datatype_valid_ptr( type_ptr, mpi_errno );
	    /* If type_ptr is not valid, it will be reset to null */
	    /* Validate keyval_ptr */
		MPID_Keyval_valid_ptr( keyval_ptr, mpi_errno );
            if (mpi_errno) goto fn_fail;
        }
        MPID_END_ERROR_CHECKS;
    }
#   endif /* HAVE_ERROR_CHECKING */

    /* ... body of routine ...  */
    /* Look for attribute.  They are ordered by keyval handle.  This uses 
       a simple linear list algorithm because few applications use more than a 
       handful of attributes */
    
    old_p = &type_ptr->attributes;
    p = type_ptr->attributes;
    while (p) {
	if (p->keyval->handle == keyval_ptr->handle) {
	    /* If found, call the delete function before replacing the 
	       attribute */
92
	    mpi_errno = MPIR_Call_attr_delete( datatype, p );
93
94
95
96
97
	    /* --BEGIN ERROR HANDLING-- */
	    if (mpi_errno) { 
		goto fn_fail;
	    }
	    /* --END ERROR HANDLING-- */
98
99
	    p->value    = (MPID_AttrVal_t)(MPIR_Pint)attribute_val;
	    p->attrType = attrType;
100
101
102
	    break;
	}
	else if (p->keyval->handle > keyval_ptr->handle) {
103
	    MPID_Attribute *new_p = MPID_Attr_alloc();
104
105
106
	    MPIU_ERR_CHKANDJUMP1(!new_p,mpi_errno,MPI_ERR_OTHER,
				 "**nomem","**nomem %s", "MPID_Attribute" );
	    new_p->keyval	 = keyval_ptr;
107
	    new_p->attrType      = attrType;
108
	    new_p->pre_sentinal	 = 0;
109
	    new_p->value	 = (MPID_AttrVal_t)(MPIR_Pint)attribute_val;
110
111
112
113
114
115
116
117
118
119
120
	    new_p->post_sentinal = 0;
	    new_p->next		 = p->next;
	    MPIR_Keyval_add_ref( keyval_ptr );
	    p->next		 = new_p;
	    break;
	}
	old_p = &p->next;
	p = p->next;
    }
    if (!p)
    {
121
	MPID_Attribute *new_p = MPID_Attr_alloc();
122
123
124
125
	MPIU_ERR_CHKANDJUMP1(!new_p,mpi_errno,MPI_ERR_OTHER,
			     "**nomem","**nomem %s", "MPID_Attribute" );
	/* Did not find in list.  Add at end */
	new_p->keyval	     = keyval_ptr;
126
	new_p->attrType      = attrType;
127
	new_p->pre_sentinal  = 0;
128
	new_p->value	     = (MPID_AttrVal_t)(MPIR_Pint)attribute_val;
129
130
131
132
133
134
135
136
137
138
139
140
141
142
	new_p->post_sentinal = 0;
	new_p->next	     = 0;
	MPIR_Keyval_add_ref( keyval_ptr );
	*old_p		     = new_p;
    }
    
    /* Here is where we could add a hook for the device to detect attribute
       value changes, using something like
       MPID_Dev_type_attr_hook( type_ptr, keyval, attribute_val );
    */

    /* ... end of body of routine ... */

  fn_exit:
143
    MPID_MPI_FUNC_EXIT(MPID_STATE_MPIR_TYPE_SET_ATTR);
144
    MPIU_THREAD_CS_EXIT(ALLFUNC,);
145
146
147
148
149
150
151
152
    return mpi_errno;

  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_type_set_attr", 
153
	    "**mpi_type_set_attr %D %d %p", datatype, type_keyval, attribute_val);
154
155
156
157
158
159
    }
#   endif
    mpi_errno = MPIR_Err_return_comm( NULL, FCNAME, mpi_errno );
    goto fn_exit;
    /* --END ERROR HANDLING-- */
}
160
161
162
163
164
165
166
167
168
169

#endif

#undef FUNCNAME
#define FUNCNAME MPI_Type_set_attr

/*@
   MPI_Type_set_attr - Stores attribute value associated with a key

Input Parameters:
170
+ datatype - MPI Datatype to which attribute will be attached (handle)
171
. type_keyval - key value, as returned by  'MPI_Type_create_keyval' (integer)
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
- attribute_val - attribute value 

Notes:

The type of the attribute value depends on whether C or Fortran is being used.
In C, an attribute value is a pointer ('void *'); in Fortran, it is an 
address-sized integer.

If an attribute is already present, the delete function (specified when the
corresponding keyval was created) will be called.
.N Fortran

.N Errors
.N MPI_SUCCESS
.N MPI_ERR_TYPE
.N MPI_ERR_KEYVAL
@*/
189
int MPI_Type_set_attr(MPI_Datatype datatype, int type_keyval, void *attribute_val)
190
191
192
193
194
195
196
197
198
{
    static const char FCNAME[] = "MPI_Type_set_attr";
    int mpi_errno = MPI_SUCCESS;
    MPID_MPI_STATE_DECL(MPID_STATE_MPI_TYPE_SET_ATTR);

    MPIR_ERRTEST_INITIALIZED_ORDIE();
    
    MPID_MPI_FUNC_ENTER(MPID_STATE_MPI_TYPE_SET_ATTR);

199
    mpi_errno = MPIR_TypeSetAttr( datatype, type_keyval, attribute_val,
200
201
202
203
204
205
206
207
208
209
210
211
212
				  MPIR_ATTR_PTR );
    if (mpi_errno) goto fn_fail;

  fn_exit:
    MPID_MPI_FUNC_EXIT(MPID_STATE_MPI_TYPE_SET_ATTR);
    return mpi_errno;

  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_type_set_attr", 
213
	    "**mpi_type_set_attr %D %d %p", datatype, type_keyval, attribute_val);
214
215
216
217
218
219
    }
#   endif
    mpi_errno = MPIR_Err_return_comm( NULL, FCNAME, mpi_errno );
    goto fn_exit;
    /* --END ERROR HANDLING-- */
}