#include "Bdef.h"

#if (INTFACE == C_CALL)
void Cblacs_exit(NotDone)
int  NotDone;
#else
void blacs_exit_(NotDone)
int  *NotDone;
#endif
{
#if (INTFACE == C_CALL)
   void Cblacs_gridexit();
#else
   void blacs_gridexit_();
#endif
   char *getbuff();
   int i;
   extern int *pvmtids00;
   extern int MaxNCtxt00, Np00, minID00, maxID00, availID00;
   extern BLACSCONTEXT **MyConTxts00;
/*
 * Destroy all contexts
 */
#if (INTFACE == C_CALL)
   for (i=0; i < MaxNCtxt00; i++) if (MyConTxts00[i]) Cblacs_gridexit(i);
#else
   for (i=0; i < MaxNCtxt00; i++) if (MyConTxts00[i]) blacs_gridexit_(&i);
#endif
   free(MyConTxts00);

   pvm_freebuf(pvm_getsbuf());   /* free PVM buffer */
   pvm_freebuf(pvm_getrbuf());   /* free PVM buffer */
   if (!Mpval(NotDone)) pvm_exit();
   if (pvmtids00 != NULL) free(pvmtids00);
   getbuff(-1);                  /* free BLACS buffer */

/*
 * Reset parameters to initial values
 */
   MaxNCtxt00 = 0;
   MyConTxts00 = NULL;
   Np00 = -1;
   availID00 = -1;
   minID00=0;
   maxID00=2147483647;
   pvmtids00=NULL;
}
