Newsgroups: comp.parallel.pvm,comp.unix.cray
From: glover@hikimi.cray.com (Roger Glover)
Subject: Re: String passing between Fortran & C on a Cray T3D
Organization: Cray Research, Inc.
Date: 21 Feb 95 10:00:47 CST
Message-ID: <1995Feb21.100047.5956@walter.cray.com>

In article <3hq6ms$dis@whitbeck.ncl.ac.uk>, "D. Jain" <D.Jain@ncl.ac.uk> writes:
> Quick call for some help on what seems like a trivial problem but which we
> havent been able to solve:
> 
> We are using a Cray T3D to carry out Solid State Physics calculations. The
> parallelization is carried out by using pvm calls and libraries such as
> scalapack (version 2), pbblas, pblas, pblacs. Now for our problem ...
> 
> When passing strings between C and Fortran routines we encountered "operand out
> of range" errors. Eventually we discovered that fortran, when passing strings,
> passes additional "phantom" integers which equal 8* the length of the string.
> Adding these extra variables to the call statement did the trick for most of
> the calls however this didnt work for all. In some cases there are calls (to
> the pbblas routines) which pass, say, 2 strings. The first is passed correctly
> but the second results in a crash when it is accessed. On investigation the
> second seems to result in a null pointer being passed. We have tried various
> test routines which use the same call as the pbblas routine and our routines
> seem to work.

Frank Robijn's solution for the C98 does indeed also work for the T3D.
That is, you need to convert your C character pointers into "Fortran
character descriptor" (fcd) format.  Frank gave an excellent example of
passing Fortran character strings into C.

I thought that, in your case, an example of passing C character pointer
to Fortran might be helpful also.  I do not know anything about phblas,
pblas or pblacs, but here is an example from the blacs package in libsci:

	#include <fortran.h> /* defines the type _fcd and prototypes *
	                      * conversion functions                 */
	#define AROW 20
	#define ACOL 50

	int lda = AROW, m = AROW, n = ACOL;
	double a[ACOL][AROW];
	double rdest, cdest; /* not used in the CRI implementation */
	char *scope = "R", *top = "H";
	.
	.

	SGSUM2D(_cptofcd(scope), _cptofcd(top), &m, &n, a, &lda, &rdest, &cdest);

The above corresponds roughly to the following Fortran 77 implementation:

	Parameter (Arow=20, Acol=50)
	Parameter(Lda=Arow, M=Arow, N=Acol)
	Real A(Arow,Acol)
C The following are not used in the CRI implementation
	Real Rdest, Cdest
	Character*1 Scope, Top
	Parameter(Scope='R', Top='H')
	.
	.

	Call Sgsum2d(Scope, Top, M, N, A, Lda, Rdest, Cdest)

See the manpage for _cptofcd for more details.

I hope this helps!

----------------------------------------------- Roger Glover
XXXX\ \ / \ /XXX  \ / \ X   \ /\\\  X///X /\\\  Cray Research, Inc.
/ \ / \/ /\ / \ / \X /\ X  \  / \   X\  \ X     DISCLAIMER HAIKU:
//X/  X\\\X //X/  X \ X X\\   / \   X/X \ X \\\   CRI may not
/ \   X///X / \/  X//XX X  \  / \   X  \\ X   \   Share these opinions of mine
/ \   X   X /\\\/ X   X X///X /XXX/ X///X /XXX/   This is not my fault
----------------------------------------------- 

For those who are truly interested...

The internal format of fcd on the T3D uses two 64-bit words, a byte
address and a byte length.  This is different than the format on most
Cray Research PVP (parallel-vector processor) systems which uses one
64-bit word to hold a word address, byte offset, and byte length.
Furthermore, our latest high-end PVP system uses a two-word fdc format
that is different than either of the other two formats.

Regardless of the underlying format of fcd, the point to remember is
that the same high-level interface (using _fcdtocp or _cptofcd) works
in all cases.

