I've posted this to MPICH2-Dev, and then decided to re-post this here, at the behavior of Open MPI is exactly the same.
You may also want to try the code right below, and next the one at the end of the forwarder message. #include <mpi.h> #include <stdio.h> int main( int argc, char ** argv ) { int Key1, tmp1, Key2, tmp2; MPI_Init(&argc, &argv); MPI_Keyval_create(MPI_NULL_COPY_FN, MPI_NULL_DELETE_FN, &Key1,(void *) 0); tmp1=Key1; MPI_Keyval_free(&tmp1); MPI_Keyval_create(MPI_NULL_COPY_FN, MPI_NULL_DELETE_FN, &Key2, (void *) 0); tmp2=Key2; MPI_Keyval_free(&tmp2); MPI_Finalize(); printf("MPI_KEYVAL_INVALID: %d\n", MPI_KEYVAL_INVALID); printf("Key1: %d\n", Key1); printf("tmp1: %d\n", tmp1); printf("Key2: %d\n", Key2); printf("tmp2: %d\n", tmp2); return 0; } ---------- Forwarded message ---------- From: Lisandro Dalcin <dalc...@gmail.com> List-Post: devel@lists.open-mpi.org Date: Fri, Mar 13, 2009 at 4:01 PM Subject: some comments on attribute catching, create/free() keyvals and all that. To: mpich2-...@mcs.anl.gov As I've shown in a previous emal, MPICH2 likely implement create()/free() for keyvals using a counter that is incr/decr ... Now, give a try to the code pasted below. This shows that (at least in MPICH2), MPI_Keyval_free() have to be used with great care as it is IMHO dangerous, and basically these calls should be all done near MPI_Finalize() time... or bad things could happen... The only reference I can found in the MPI standard is at (http://www.mpi-forum.org/docs/mpi21-report-bw/node147.htm#Node147) where MPI_Comm_free_keyval() is explained... However, I believe that description is talking about different things... Should MPICH2 stop decrefing the keyval counter? You know, about 2<<31 values should be enough, right ;-) ? But then.. What the pourpose of having MPI_Keyval_free()? Just to invalidate de passed value by setting it to KEYVAL_INVALID? #include <mpi.h> #include <stdio.h> int free_KeyVal(MPI_Comm c, int k, void *v,void *ctx) { printf("free_KeyVal()\n"); return MPI_SUCCESS; } int main( int argc, char ** argv ) { int Key1, Key2, Val1=1, Val2=2, ValOut; MPI_Init(&argc, &argv); MPI_Keyval_create(MPI_NULL_COPY_FN, free_KeyVal, &Key1,(void *) 0); MPI_Attr_put(MPI_COMM_SELF, Key1, &Val1); MPI_Keyval_free(&Key1); MPI_Keyval_create(MPI_NULL_COPY_FN, MPI_NULL_DELETE_FN, &Key2,(void *) 0); MPI_Attr_put(MPI_COMM_SELF, Key2, &Val2); MPI_Keyval_free(&Key2); MPI_Finalize(); return 0; } -- Lisandro Dalcín --------------- Centro Internacional de Métodos Computacionales en Ingeniería (CIMEC) Instituto de Desarrollo Tecnológico para la Industria Química (INTEC) Consejo Nacional de Investigaciones Científicas y Técnicas (CONICET) PTLC - Güemes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594 -- Lisandro Dalcín --------------- Centro Internacional de Métodos Computacionales en Ingeniería (CIMEC) Instituto de Desarrollo Tecnológico para la Industria Química (INTEC) Consejo Nacional de Investigaciones Científicas y Técnicas (CONICET) PTLC - Güemes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594