/*
 *
 * Copyright 1998-1999, University of Notre Dame.
 * Authors: Jeffrey M. Squyres, Kinis L. Meyer with M. D. McNally 
 *          and Andrew Lumsdaine
 *
 * This file is part of the Notre Dame LAM implementation of MPI.
 *
 * You should have received a copy of the License Agreement for the
 * Notre Dame LAM implementation of MPI along with the software; see
 * the file LICENSE.  If not, contact Office of Research, University
 * of Notre Dame, Notre Dame, IN 46556.
 *
 * Permission to modify the code and to distribute modified code is
 * granted, provided the text of this NOTICE is retained, a notice that
 * the code was modified is included with the above COPYRIGHT NOTICE and
 * with the COPYRIGHT NOTICE in the LICENSE file, and that the LICENSE
 * file is distributed with the modified code.
 *
 * LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED.
 * By way of example, but not limitation, Licensor MAKES NO
 * REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY
 * PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS
 * OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS
 * OR OTHER RIGHTS.  
 *
 * Additional copyrights may follow.
 *
 *	Ohio Trollius
 *	Copyright 1997 The Ohio State University
 *	NJN
 *
 *	$Id: handles.c,v 1.1 1999/09/02 01:32:53 jsquyres Exp $
 *
 *	Function:	- handle conversion C<->Fortran 
 */

#include <mpi.h>
#include <mpisys.h>
#include <MPISYSF.h>
#include <rpisys.h>

/*
 *	MPI_Comm_f2c
 *
 *	Function:	- communicator handle conversion Fortran->C
 *	Accepts:	- Fortran communicator handle 
 *	Returns:	- C communicator handle
 */
MPI_Comm
MPI_Comm_f2c(MPI_Fint f_handle)

{
	return(GETHDL(f_handle));
}

/*
 *	MPI_Comm_c2f
 *
 *	Function:	- communicator handle conversion C->Fortran
 *	Accepts:	- C communicator handle 
 *	Returns:	- Fortran communicator handle
 */
MPI_Fint
MPI_Comm_c2f(MPI_Comm comm)

{
	int		f77_handle;

	if (comm->c_f77handle < 0) {
		if ((f77_handle = lam_F_alloc_hdl()) >= 0) {
			lam_F_handles[f77_handle] = comm;
			comm->c_f77handle = f77_handle;
		}
	}

	return((MPI_Fint) comm->c_f77handle);
}

/*
 *	MPI_Group_f2c
 *
 *	Function:	- group handle conversion Fortran->C
 *	Accepts:	- Fortran group handle 
 *	Returns:	- C group handle
 */
MPI_Group
MPI_Group_f2c(MPI_Fint f_handle)

{
	return(GETHDL(f_handle));
}

/*
 *	MPI_Group_c2f
 *
 *	Function:	- group handle conversion C->Fortran
 *	Accepts:	- C group handle 
 *	Returns:	- Fortran group handle
 */
MPI_Fint
MPI_Group_c2f(MPI_Group group)

{
	int		f77_handle;

	if (group->g_f77handle < 0) {
		if ((f77_handle = lam_F_alloc_hdl()) >= 0) {
			lam_F_handles[f77_handle] = group;
			group->g_f77handle = f77_handle;
		}
	}

	return((MPI_Fint) group->g_f77handle);
}

/*
 *	MPI_Type_f2c
 *
 *	Function:	- datatype handle conversion Fortran->C
 *	Accepts:	- Fortran datatype handle 
 *	Returns:	- C datatype handle
 */
MPI_Datatype
MPI_Type_f2c(MPI_Fint f_handle)

{
	return(GETHDL(f_handle));
}

/*
 *	MPI_Type_c2f
 *
 *	Function:	- datatype handle conversion C->Fortran
 *	Accepts:	- C datatype handle 
 *	Returns:	- Fortran datatype handle
 */
MPI_Fint
MPI_Type_c2f(MPI_Datatype datatype)

{
	int		f77_handle;

	if (datatype->dt_f77handle < 0) {
		if ((f77_handle = lam_F_alloc_hdl()) >= 0) {
			lam_F_handles[f77_handle] = datatype;
			datatype->dt_f77handle = f77_handle;
		}
	}

	return((MPI_Fint) datatype->dt_f77handle);
}

/*
 *	MPI_Request_f2c
 *
 *	Function:	- request handle conversion Fortran->C
 *	Accepts:	- Fortran request handle 
 *	Returns:	- C request handle
 */
MPI_Request
MPI_Request_f2c(MPI_Fint f_handle)

{
	return(GETHDL(f_handle));
}

/*
 *	MPI_Request_c2f
 *
 *	Function:	- request handle conversion C->Fortran
 *	Accepts:	- C request handle 
 *	Returns:	- Fortran request handle
 */
MPI_Fint
MPI_Request_c2f(MPI_Request request)

{
	int		f77_handle;

	if (request->rq_f77handle < 0) {
		if ((f77_handle = lam_F_alloc_hdl()) >= 0) {
			lam_F_handles[f77_handle] = request;
			request->rq_f77handle = f77_handle;
		}
	}

	return((MPI_Fint) request->rq_f77handle);
}

/*
 *	MPI_Op_f2c
 *
 *	Function:	- operation handle conversion Fortran->C
 *	Accepts:	- Fortran operation handle 
 *	Returns:	- C operation handle
 */
MPI_Op
MPI_Op_f2c(MPI_Fint f_handle)

{
	return(GETHDL(f_handle));
}

/*
 *	MPI_Op_c2f
 *
 *	Function:	- operation handle conversion C->Fortran
 *	Accepts:	- C operation handle 
 *	Returns:	- Fortran operation handle
 */
MPI_Fint
MPI_Op_c2f(MPI_Op op)

{
	int		f77_handle;

	if (op->op_f77handle < 0) {
		if ((f77_handle = lam_F_alloc_hdl()) >= 0) {
			lam_F_handles[f77_handle] = op;
			op->op_f77handle = f77_handle;
		}
	}

	return((MPI_Fint) op->op_f77handle);
}

/*
 *	MPI_Info_f2c
 *
 *	Function:	- info handle conversion Fortran->C
 *	Accepts:	- Fortran info handle 
 *	Returns:	- C info handle
 */
MPI_Info
MPI_Info_f2c(MPI_Fint f_handle)

{
	return(GETHDL(f_handle));
}

/*
 *	MPI_Info_c2f
 *
 *	Function:	- info handle conversion C->Fortran
 *	Accepts:	- C info handle 
 *	Returns:	- Fortran info handle
 */
MPI_Fint
MPI_Info_c2f(MPI_Info info)

{
	int		f77_handle;

	if (info->info_f77handle < 0) {
		if ((f77_handle = lam_F_alloc_hdl()) >= 0) {
			lam_F_handles[f77_handle] = info;
			info->info_f77handle = f77_handle;
		}
	}

	return((MPI_Fint) info->info_f77handle);
}

/*
 *	MPI_Win_f2c
 *
 *	Function:	- window handle conversion Fortran->C
 *	Accepts:	- Fortran window handle 
 *	Returns:	- C window handle
 */
MPI_Win
MPI_Win_f2c(MPI_Fint f_handle)

{
	return(GETHDL(f_handle));
}

/*
 *	MPI_Win_c2f
 *
 *	Function:	- window handle conversion C->Fortran
 *	Accepts:	- C window handle 
 *	Returns:	- Fortran window handle
 */
MPI_Fint
MPI_Win_c2f(MPI_Win win)

{
	int		f77_handle;

	if (win->w_f77handle < 0) {
		if ((f77_handle = lam_F_alloc_hdl()) >= 0) {
			lam_F_handles[f77_handle] = win;
			win->w_f77handle = f77_handle;
		}
	}

	return((MPI_Fint) win->w_f77handle);
}


/*
 *	MPI_Status_f2c
 *
 *	Function:	- status conversion Fortran->C
 *	Accepts:	- Fortran status
 *			- C status (out)
 *	Returns:	- MPI_SUCCESS
 */
int
MPI_Status_f2c(f_status, c_status)

MPI_Fint		*f_status;
MPI_Status		*c_status;

{
	*c_status = *((MPI_Status *) f_status);
	return(MPI_SUCCESS);
}


/*
 *	MPI_Status_c2f
 *
 *	Function:	- status conversion C->Fortran
 *	Accepts:	- pointer C status
 *			- Fortran status (out)
 *	Returns:	- MPI_SUCCESS
 */
int
MPI_Status_c2f(c_status, f_status)

MPI_Status		*c_status;
MPI_Fint		*f_status;

{
	*((MPI_Status *) f_status) = *c_status;
	return(MPI_SUCCESS);
}
