2002-02-11 20:40:09 +00:00
|
|
|
/*************************************************************************
|
|
|
|
* *
|
|
|
|
* YAP Prolog *
|
|
|
|
* *
|
|
|
|
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
|
|
|
* *
|
2003-07-03 16:01:18 +01:00
|
|
|
* Copyright S. Konstantopoulos and Universidade do Porto 2002-2003 *
|
2002-02-11 20:40:09 +00:00
|
|
|
* *
|
|
|
|
**************************************************************************
|
|
|
|
* *
|
|
|
|
* File: mpi.c *
|
2003-07-03 16:01:18 +01:00
|
|
|
* Last rev: $Date: 2003-07-03 15:01:18 $ *
|
2002-02-11 20:40:09 +00:00
|
|
|
* mods: *
|
2003-07-03 16:01:18 +01:00
|
|
|
* comments: Interface to MPI libraries *
|
2002-02-11 20:40:09 +00:00
|
|
|
* *
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
#ifndef lint
|
2003-07-03 16:01:18 +01:00
|
|
|
static char *rcsid = "$Header: /Users/vitor/Yap/yap-cvsbackup/library/mpi/mpi.c,v 1.20 2003-07-03 15:01:18 stasinos Exp $";
|
2002-02-11 20:40:09 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "Yap.h"
|
2002-02-12 17:38:38 +00:00
|
|
|
|
|
|
|
#if HAVE_MPI
|
|
|
|
|
2002-02-11 20:40:09 +00:00
|
|
|
#include "Yatom.h"
|
|
|
|
#include "yapio.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <mpi.h>
|
|
|
|
|
2003-07-03 16:01:18 +01:00
|
|
|
void STD_PROTO(YAP_Write, (Term, void (*)(int), int));
|
|
|
|
|
2002-02-11 20:40:09 +00:00
|
|
|
STATIC_PROTO (Int p_mpi_open, (void));
|
|
|
|
STATIC_PROTO (Int p_mpi_close, (void));
|
|
|
|
STATIC_PROTO (Int p_mpi_send, (void));
|
|
|
|
STATIC_PROTO (Int p_mpi_receive, (void));
|
2002-03-12 20:03:55 +00:00
|
|
|
STATIC_PROTO (Int p_mpi_bcast3, (void));
|
|
|
|
STATIC_PROTO (Int p_mpi_bcast2, (void));
|
2002-02-26 15:34:08 +00:00
|
|
|
STATIC_PROTO (Int p_mpi_barrier, (void));
|
2002-02-11 20:40:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
2003-07-03 16:01:18 +01:00
|
|
|
* Auxiliary Data
|
2002-02-11 20:40:09 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
static Int rank, numprocs, namelen;
|
|
|
|
static char processor_name[MPI_MAX_PROCESSOR_NAME];
|
|
|
|
|
2002-02-27 13:41:24 +00:00
|
|
|
static Int mpi_argc;
|
|
|
|
static char **mpi_argv;
|
|
|
|
|
2003-07-03 16:01:18 +01:00
|
|
|
/* this should eventually be moved to config.h */
|
2002-10-03 18:28:37 +01:00
|
|
|
#define RECV_BUF_SIZE 1024*32
|
2002-02-27 13:41:24 +00:00
|
|
|
|
2003-07-03 16:01:18 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* A simple stream
|
|
|
|
*/
|
|
|
|
|
2002-03-13 09:01:39 +00:00
|
|
|
static size_t bufsize, bufstrlen;
|
2002-02-27 13:41:24 +00:00
|
|
|
static char *buf;
|
2002-03-13 09:01:39 +00:00
|
|
|
static int bufptr;
|
2002-02-27 13:41:24 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
expand_buffer( int space )
|
|
|
|
{
|
2002-10-03 18:28:37 +01:00
|
|
|
#if MPI_AVOID_REALLOC
|
|
|
|
/*
|
|
|
|
realloc() has been SIGSEGV'ing on HP-UX 10.20, but there is
|
|
|
|
no problem in HP-UX 11.0. We can remove this bit here as soon
|
|
|
|
as Yap stops compiling on 10.20 anyway. If removed, also remove
|
|
|
|
the MPI_AVOID_REALLOC bits from configure.in and config.h.in
|
|
|
|
*/
|
2002-02-27 13:41:24 +00:00
|
|
|
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
tmp = malloc( bufsize + space );
|
|
|
|
if( tmp == NULL ) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(SYSTEM_ERROR, TermNil, "out of memory" );
|
|
|
|
Yap_exit( EXIT_FAILURE );
|
2002-02-27 13:41:24 +00:00
|
|
|
}
|
|
|
|
memcpy( tmp, buf, bufsize );
|
|
|
|
free( buf );
|
|
|
|
buf = tmp;
|
2002-10-03 18:28:37 +01:00
|
|
|
#else /* use realloc */
|
2002-02-27 13:41:24 +00:00
|
|
|
buf = realloc( buf, bufsize + space );
|
|
|
|
if( buf == NULL ) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(SYSTEM_ERROR, TermNil, "out of memory");
|
|
|
|
Yap_exit( EXIT_FAILURE );
|
2002-02-27 13:41:24 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bufsize += space;
|
|
|
|
}
|
2002-02-11 20:40:09 +00:00
|
|
|
|
2003-07-03 16:01:18 +01:00
|
|
|
static void
|
|
|
|
mpi_putc(Int ch)
|
2002-02-11 20:40:09 +00:00
|
|
|
{
|
2002-02-27 13:41:24 +00:00
|
|
|
if( ch > 0 ) {
|
|
|
|
if( bufptr >= bufsize ) expand_buffer( RECV_BUF_SIZE );
|
2002-02-11 20:40:09 +00:00
|
|
|
buf[bufptr++] = ch;
|
2002-02-27 13:41:24 +00:00
|
|
|
}
|
2002-02-11 20:40:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* C Predicates
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_mpi_open(void) /* mpi_open(?rank, ?num_procs, ?proc_name) */
|
|
|
|
{
|
2002-02-22 14:31:45 +00:00
|
|
|
Term t_rank = Deref(ARG1), t_numprocs = Deref(ARG2), t_procname = Deref(ARG3);
|
|
|
|
Int retv;
|
2002-02-11 20:40:09 +00:00
|
|
|
|
2002-10-31 11:13:21 +00:00
|
|
|
/*
|
|
|
|
With MPICH MPI_Init() must be called during initialisation,
|
|
|
|
but with LAM it can be called from Prolog (mpi_open/3)
|
|
|
|
|
|
|
|
The symptoms match a known RedHat bug, see
|
|
|
|
http://email.osc.edu/pipermail/mpiexec/2002-July/000067.html
|
|
|
|
for a suggested workaround:
|
|
|
|
Redhat have somehow broken their sem.h and ipc.h. If you use your own
|
|
|
|
kernel, copy from ../src/kernel/include/asm & ../src/kernel/include/linux
|
|
|
|
the file ipc.h and sem.h to /usr/include/sys, recompile your mpich and
|
|
|
|
everything might start working. (it did for us)
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2003-06-26 15:35:52 +01:00
|
|
|
Note that if MPI_Init() fails, Yap/MPICH and Yap/LAM behave differently:
|
|
|
|
in Yap/MPICH we are still at the Yap initialisation phase, so we get
|
2002-10-31 11:13:21 +00:00
|
|
|
Yap exit(FAILURE), whereas in Yap/LAM mpi_open/3 simply fails.
|
|
|
|
*/
|
|
|
|
|
|
|
|
retv = MPI_Init( &mpi_argc, &mpi_argv );
|
|
|
|
if( retv ) {
|
|
|
|
Term t;
|
|
|
|
|
|
|
|
t = MkIntegerTerm(retv);
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error( SYSTEM_ERROR, t, "MPI_Init() returned non-zero" );
|
2002-10-31 11:13:21 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2002-02-27 13:41:24 +00:00
|
|
|
MPI_Comm_size( MPI_COMM_WORLD, &numprocs );
|
|
|
|
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
|
|
|
|
MPI_Get_processor_name( processor_name, &namelen );
|
|
|
|
|
2002-11-18 18:18:05 +00:00
|
|
|
retv = Yap_unify(t_rank, MkIntTerm(rank));
|
|
|
|
retv = retv && Yap_unify(t_numprocs, MkIntTerm(numprocs));
|
|
|
|
retv = retv && Yap_unify(t_procname, MkAtomTerm(Yap_LookupAtom(processor_name)));
|
2002-02-11 20:40:09 +00:00
|
|
|
|
2002-02-22 14:31:45 +00:00
|
|
|
return retv;
|
2002-02-11 20:40:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Int /* mpi_close */
|
|
|
|
p_mpi_close()
|
|
|
|
{
|
|
|
|
MPI_Finalize();
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_mpi_send() /* mpi_send(+data, +destination, +tag) */
|
|
|
|
{
|
|
|
|
Term t_data = Deref(ARG1), t_dest = Deref(ARG2), t_tag = Deref(ARG3);
|
|
|
|
int tag, dest, retv;
|
|
|
|
|
|
|
|
/* The first argument (data) must be bound */
|
|
|
|
if (IsVarTerm(t_data)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(INSTANTIATION_ERROR, t_data, "mpi_send");
|
2002-02-11 20:40:09 +00:00
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The second and third args must be bount to integers */
|
|
|
|
if (IsVarTerm(t_dest)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(INSTANTIATION_ERROR, t_dest, "mpi_send");
|
2002-02-11 20:40:09 +00:00
|
|
|
return (FALSE);
|
|
|
|
} else if( !IsIntegerTerm(t_dest) ) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_INTEGER, t_dest, "mpi_send");
|
2002-02-11 20:40:09 +00:00
|
|
|
return (FALSE);
|
|
|
|
} else {
|
|
|
|
dest = IntOfTerm( t_dest );
|
|
|
|
}
|
|
|
|
if (IsVarTerm(t_tag)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(INSTANTIATION_ERROR, t_tag, "mpi_send");
|
2002-02-11 20:40:09 +00:00
|
|
|
return (FALSE);
|
|
|
|
} else if( !IsIntegerTerm(t_tag) ) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_INTEGER, t_tag, "mpi_send");
|
2002-02-11 20:40:09 +00:00
|
|
|
return (FALSE);
|
|
|
|
} else {
|
|
|
|
tag = IntOfTerm( t_tag );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Turn the term into its ASCII representation */
|
2003-07-03 16:01:18 +01:00
|
|
|
bufptr = 0;
|
|
|
|
YAP_Write( t_data, mpi_putc, Quote_illegal_f|Handle_vars_f );
|
2002-02-27 13:41:24 +00:00
|
|
|
|
2002-10-03 18:28:37 +01:00
|
|
|
/* The buf is not NULL-terminated and does not have the
|
|
|
|
trailing ". " required by the parser */
|
2003-07-03 16:01:18 +01:00
|
|
|
mpi_putc( '.' );
|
|
|
|
mpi_putc( ' ' );
|
|
|
|
mpi_putc( 0 );
|
|
|
|
bufstrlen = strlen(buf);
|
2002-02-11 20:40:09 +00:00
|
|
|
|
2002-11-05 11:14:08 +00:00
|
|
|
/* send the data */
|
2003-07-03 16:01:18 +01:00
|
|
|
bufptr = 0;
|
2002-10-03 18:28:37 +01:00
|
|
|
retv = MPI_Send( &buf[bufptr], bufstrlen, MPI_CHAR, dest, tag, MPI_COMM_WORLD );
|
|
|
|
if( retv != MPI_SUCCESS ) return FALSE;
|
2002-02-27 13:41:24 +00:00
|
|
|
|
|
|
|
return TRUE;
|
2002-02-11 20:40:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_mpi_receive() /* mpi_receive(-data, ?orig, ?tag) */
|
|
|
|
{
|
2003-06-26 15:35:52 +01:00
|
|
|
Term t, t_data = Deref(ARG1), t_orig = Deref(ARG2), t_tag = Deref(ARG3);
|
2002-02-11 20:40:09 +00:00
|
|
|
int tag, orig, retv;
|
|
|
|
MPI_Status status;
|
|
|
|
|
2002-02-12 17:38:38 +00:00
|
|
|
/* The first argument (data) must be unbound */
|
|
|
|
if(!IsVarTerm(t_data)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(INSTANTIATION_ERROR, t_data, "mpi_receive");
|
2002-02-12 17:38:38 +00:00
|
|
|
return FALSE;
|
2002-02-11 20:40:09 +00:00
|
|
|
}
|
2002-02-12 17:38:38 +00:00
|
|
|
|
2002-02-11 20:40:09 +00:00
|
|
|
/* The second argument (source) must be bound to an integer
|
|
|
|
(the rank of the source) or left unbound (i.e. any source
|
|
|
|
is OK) */
|
|
|
|
if (IsVarTerm(t_orig)) {
|
|
|
|
orig = MPI_ANY_SOURCE;
|
|
|
|
} else if( !IsIntegerTerm(t_orig) ) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_INTEGER, t_orig, "mpi_receive");
|
2002-02-11 20:40:09 +00:00
|
|
|
return (FALSE);
|
|
|
|
} else {
|
|
|
|
orig = IntOfTerm( t_orig );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The third argument must be bound to an integer (the tag)
|
2002-02-12 17:38:38 +00:00
|
|
|
or left unbound (i.e. any tag is OK) */
|
2002-02-11 20:40:09 +00:00
|
|
|
if (IsVarTerm(t_tag)) {
|
|
|
|
tag = MPI_ANY_TAG;
|
|
|
|
} else if( !IsIntegerTerm(t_tag) ) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_INTEGER, t_tag, "mpi_receive");
|
2002-02-11 20:40:09 +00:00
|
|
|
return (FALSE);
|
|
|
|
} else
|
|
|
|
tag = IntOfTerm( t_tag );
|
|
|
|
|
2002-11-05 11:14:08 +00:00
|
|
|
/* probe for the size of the term */
|
|
|
|
retv = MPI_Probe( orig, tag, MPI_COMM_WORLD, &status );
|
2002-10-03 18:28:37 +01:00
|
|
|
if( retv != MPI_SUCCESS ) {
|
|
|
|
return FALSE;
|
2002-02-27 13:41:24 +00:00
|
|
|
}
|
2002-11-05 11:14:08 +00:00
|
|
|
MPI_Get_count( &status, MPI_CHAR, &bufstrlen );
|
2002-02-11 20:40:09 +00:00
|
|
|
|
2002-10-03 18:28:37 +01:00
|
|
|
/* adjust the buffer */
|
|
|
|
if( bufsize < bufstrlen ) expand_buffer(bufstrlen-bufsize);
|
2002-02-11 20:40:09 +00:00
|
|
|
|
2002-11-05 11:14:08 +00:00
|
|
|
/* Already know the source from MPI_Probe() */
|
2002-10-03 18:28:37 +01:00
|
|
|
if( orig == MPI_ANY_SOURCE ) {
|
|
|
|
orig = status.MPI_SOURCE;
|
2002-11-18 18:18:05 +00:00
|
|
|
retv = Yap_unify(t_orig, MkIntTerm(orig));
|
2002-11-12 10:25:11 +00:00
|
|
|
if( retv == FALSE ) {
|
|
|
|
printf("PROBLEM: file %s, line %d\n", __FILE__, __LINE__);
|
|
|
|
}
|
2002-10-03 18:28:37 +01:00
|
|
|
}
|
|
|
|
|
2002-11-05 11:14:08 +00:00
|
|
|
/* Already know the tag from MPI_Probe() */
|
2002-10-03 18:28:37 +01:00
|
|
|
if( tag == MPI_ANY_TAG ) {
|
|
|
|
tag = status.MPI_TAG;
|
2002-11-18 18:18:05 +00:00
|
|
|
retv = Yap_unify(t_tag, MkIntTerm(status.MPI_TAG));
|
2002-11-12 10:25:11 +00:00
|
|
|
if( retv == FALSE ) {
|
|
|
|
printf("PROBLEM: file %s, line %d\n", __FILE__, __LINE__);
|
|
|
|
}
|
2002-10-03 18:28:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Receive the message as a C string */
|
2002-11-21 15:57:23 +00:00
|
|
|
retv = MPI_Recv( buf, bufstrlen, MPI_CHAR, orig, tag,
|
2002-10-03 18:28:37 +01:00
|
|
|
MPI_COMM_WORLD, &status );
|
|
|
|
if( retv != MPI_SUCCESS ) {
|
2002-11-05 11:14:08 +00:00
|
|
|
/* Getting in here would be weird; it means the first package
|
|
|
|
(size) was sent properly, but there was a glitch with
|
|
|
|
the actual content! */
|
2002-10-03 18:28:37 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2002-02-11 20:40:09 +00:00
|
|
|
/* parse received string into a Prolog term */
|
2003-07-03 16:01:18 +01:00
|
|
|
|
2002-10-03 18:28:37 +01:00
|
|
|
bufptr = 0;
|
2011-07-22 15:49:40 +01:00
|
|
|
t = YAP_ReadBuffer( buf, NULL );
|
2002-11-05 11:14:08 +00:00
|
|
|
|
2003-06-26 15:35:52 +01:00
|
|
|
if( t == TermNil ) {
|
|
|
|
retv = FALSE;
|
|
|
|
}
|
|
|
|
else {
|
2003-07-03 16:01:18 +01:00
|
|
|
retv = Yap_unify(t, t_data);
|
2002-11-05 11:14:08 +00:00
|
|
|
}
|
2002-10-03 18:28:37 +01:00
|
|
|
|
|
|
|
return retv;
|
2002-02-12 17:38:38 +00:00
|
|
|
}
|
2002-02-11 20:40:09 +00:00
|
|
|
|
|
|
|
|
2002-02-12 17:38:38 +00:00
|
|
|
static Int
|
2002-03-12 20:03:55 +00:00
|
|
|
p_mpi_bcast3() /* mpi_bcast( ?data, +root, +max_size ) */
|
2002-02-12 17:38:38 +00:00
|
|
|
{
|
2002-02-27 13:41:24 +00:00
|
|
|
Term t_data = Deref(ARG1), t_root = Deref(ARG2), t_max_size = Deref(ARG3);
|
|
|
|
int root, retv, max_size;
|
2002-02-12 17:38:38 +00:00
|
|
|
|
|
|
|
/* The second argument must be bound to an integer (the rank of
|
|
|
|
root processor */
|
|
|
|
if (IsVarTerm(t_root)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(INSTANTIATION_ERROR, t_root, "mpi_bcast");
|
2002-02-12 17:38:38 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
root = IntOfTerm( t_root );
|
|
|
|
|
|
|
|
/* If this is the root processor, then the first argument must
|
|
|
|
be bound to the term to be sent. */
|
|
|
|
if( root == rank ) {
|
|
|
|
if( IsVarTerm(t_data) ) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(INSTANTIATION_ERROR, t_data, "mpi_bcast");
|
2002-02-12 17:38:38 +00:00
|
|
|
return FALSE;
|
2002-02-11 20:40:09 +00:00
|
|
|
}
|
2002-02-12 17:38:38 +00:00
|
|
|
/* Turn the term into its ASCII representation */
|
2003-07-03 16:01:18 +01:00
|
|
|
bufptr = 0;
|
|
|
|
YAP_Write( t_data, mpi_putc, Quote_illegal_f|Handle_vars_f );
|
2002-02-12 17:38:38 +00:00
|
|
|
/* NULL-terminate the string and add the ". " termination
|
|
|
|
required by the parser. */
|
2003-07-03 16:01:18 +01:00
|
|
|
mpi_putc( '.' );
|
|
|
|
mpi_putc( ' ' );
|
|
|
|
mpi_putc( 0 );
|
|
|
|
bufstrlen = strlen(buf);
|
2002-02-11 20:40:09 +00:00
|
|
|
}
|
2002-02-12 17:38:38 +00:00
|
|
|
|
2002-02-27 13:41:24 +00:00
|
|
|
/* The third argument must be bound to an integer (the maximum length
|
|
|
|
of the broadcast term's ASCII representation */
|
|
|
|
if (IsVarTerm(t_max_size)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(INSTANTIATION_ERROR, t_max_size, "mpi_bcast");
|
2002-02-27 13:41:24 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
/* allow for the ". " bit and the NULL at the end */
|
|
|
|
max_size = IntOfTerm( t_max_size ) + 3;
|
|
|
|
|
2003-06-26 15:35:52 +01:00
|
|
|
if( max_size < bufstrlen ) {
|
2002-02-27 13:41:24 +00:00
|
|
|
/* issue a warning? explode? bcast s'thing unparsable? */
|
2003-06-26 15:35:52 +01:00
|
|
|
printf( "MAYDAY: max_size == %d, bufstrlen == %d\n ", max_size, bufstrlen);
|
2002-03-13 09:01:39 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2002-02-27 13:41:24 +00:00
|
|
|
|
|
|
|
/* adjust the buffer size, if necessary */
|
|
|
|
if( max_size > bufsize ) {
|
2003-06-26 15:35:52 +01:00
|
|
|
expand_buffer( max_size-bufsize );
|
2002-02-27 13:41:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
retv = MPI_Bcast( buf, max_size, MPI_CHAR, root, MPI_COMM_WORLD );
|
2002-03-13 09:01:39 +00:00
|
|
|
if( retv != MPI_SUCCESS ) {
|
|
|
|
printf( "OOOPS! MPI_Bcast() returned %d.\n", retv );
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2002-02-12 17:38:38 +00:00
|
|
|
if( root == rank ) return TRUE;
|
|
|
|
else {
|
|
|
|
/* ARG1 must be unbound so that it can receive data */
|
|
|
|
if( !IsVarTerm(t_data) ) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(INSTANTIATION_ERROR, t_data, "mpi_bcast");
|
2002-02-12 17:38:38 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2002-02-27 13:41:24 +00:00
|
|
|
bufstrlen = strlen(buf);
|
2002-02-12 17:38:38 +00:00
|
|
|
bufptr = 0;
|
|
|
|
|
|
|
|
/* parse received string into a Prolog term */
|
2011-07-22 15:49:40 +01:00
|
|
|
return Yap_unify( YAP_ReadBuffer( buf, NULL ), ARG1 );
|
2002-02-12 17:38:38 +00:00
|
|
|
}
|
2002-02-11 20:40:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-10-03 18:28:37 +01:00
|
|
|
/*
|
|
|
|
This is the same as above, but for dynamic data size.
|
|
|
|
It is implemented as two broadcasts, the first being the size
|
|
|
|
and the second the actual data.
|
2002-03-12 20:03:55 +00:00
|
|
|
*/
|
2003-06-26 15:35:52 +01:00
|
|
|
|
2002-03-12 20:03:55 +00:00
|
|
|
static Int
|
|
|
|
p_mpi_bcast2() /* mpi_bcast( ?data, +root ) */
|
|
|
|
{
|
|
|
|
Term t_data = Deref(ARG1), t_root = Deref(ARG2);
|
|
|
|
int root, retv;
|
|
|
|
|
|
|
|
/* The second argument must be bound to an integer (the rank of
|
|
|
|
root processor */
|
|
|
|
if (IsVarTerm(t_root)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(INSTANTIATION_ERROR, t_root, "mpi_bcast");
|
2002-03-12 20:03:55 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
root = IntOfTerm( t_root );
|
|
|
|
|
2002-10-03 18:28:37 +01:00
|
|
|
|
2002-03-12 20:03:55 +00:00
|
|
|
/* If this is the root processor, then the first argument must
|
|
|
|
be bound to the term to be sent. */
|
|
|
|
if( root == rank ) {
|
|
|
|
if( IsVarTerm(t_data) ) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(INSTANTIATION_ERROR, t_data, "mpi_bcast");
|
2002-03-12 20:03:55 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
bufptr = 0;
|
|
|
|
/* Turn the term into its ASCII representation */
|
2003-07-03 16:01:18 +01:00
|
|
|
YAP_Write( t_data, mpi_putc, Quote_illegal_f|Handle_vars_f );
|
2002-03-12 20:03:55 +00:00
|
|
|
/* NULL-terminate the string and add the ". " termination
|
|
|
|
required by the parser. */
|
|
|
|
buf[bufptr] = 0;
|
|
|
|
strcat( buf, ". " );
|
|
|
|
bufstrlen = bufptr + 2;
|
|
|
|
}
|
2002-10-03 18:28:37 +01:00
|
|
|
/* Otherwise, it must a variable */
|
|
|
|
else {
|
|
|
|
if( !IsVarTerm(t_data) ) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(INSTANTIATION_ERROR, t_data, "mpi_bcast");
|
2002-10-03 18:28:37 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-03-12 20:03:55 +00:00
|
|
|
|
|
|
|
/* Broadcast the data size */
|
|
|
|
retv = MPI_Bcast( &bufstrlen, sizeof bufstrlen, MPI_INT, root, MPI_COMM_WORLD );
|
2002-10-03 18:28:37 +01:00
|
|
|
if( retv != MPI_SUCCESS ) {
|
|
|
|
printf("PROBLEM: file %s, line %d\n", __FILE__, __LINE__);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2002-03-12 20:03:55 +00:00
|
|
|
|
|
|
|
/* adjust the buffer size, if necessary */
|
2002-03-13 09:01:39 +00:00
|
|
|
if( bufstrlen > bufsize ) {
|
2002-03-12 20:03:55 +00:00
|
|
|
expand_buffer( bufstrlen - bufsize );
|
|
|
|
}
|
|
|
|
/* Broadcast the data */
|
|
|
|
retv = MPI_Bcast( buf, bufstrlen, MPI_CHAR, root, MPI_COMM_WORLD );
|
2002-10-03 18:28:37 +01:00
|
|
|
if( retv != MPI_SUCCESS ) {
|
|
|
|
printf("PROBLEM: file %s, line %d\n", __FILE__, __LINE__);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2002-03-12 20:03:55 +00:00
|
|
|
|
|
|
|
if( root == rank ) return TRUE;
|
|
|
|
else {
|
|
|
|
/* ARG1 must be unbound so that it can receive data */
|
|
|
|
if( !IsVarTerm(t_data) ) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(INSTANTIATION_ERROR, t_data, "mpi_bcast");
|
2002-03-12 20:03:55 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bufstrlen = strlen(buf);
|
|
|
|
bufptr = 0;
|
|
|
|
|
2011-07-22 15:49:40 +01:00
|
|
|
return Yap_unify(YAP_ReadBuffer( buf, NULL ), ARG1);
|
2003-06-26 15:35:52 +01:00
|
|
|
}
|
2002-03-12 20:03:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-02-26 15:34:08 +00:00
|
|
|
static Int
|
|
|
|
p_mpi_barrier() /* mpi_barrier/0 */
|
|
|
|
{
|
|
|
|
int retv;
|
|
|
|
|
|
|
|
retv = MPI_Barrier( MPI_COMM_WORLD );
|
|
|
|
|
|
|
|
return (retv == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-02-12 17:38:38 +00:00
|
|
|
|
2002-02-11 20:40:09 +00:00
|
|
|
/*
|
|
|
|
* Init
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_InitMPI(void)
|
2002-02-11 20:40:09 +00:00
|
|
|
{
|
|
|
|
int i,j;
|
|
|
|
|
2011-05-25 16:40:36 +01:00
|
|
|
mpi_argv = malloc( GLOBAL_argc * sizeof(char *) );
|
|
|
|
mpi_argv[0] = strdup( GLOBAL_argv[0] );
|
2002-02-11 20:40:09 +00:00
|
|
|
|
2002-02-27 13:41:24 +00:00
|
|
|
bufsize = RECV_BUF_SIZE;
|
|
|
|
buf = malloc(bufsize * sizeof(char));
|
|
|
|
|
2011-05-25 16:40:36 +01:00
|
|
|
for( i=1; i<GLOBAL_argc; ++i ) {
|
|
|
|
if( !strcmp(GLOBAL_argv[i], "--") ) { ++i; break; }
|
2002-02-11 20:40:09 +00:00
|
|
|
}
|
2011-05-25 16:40:36 +01:00
|
|
|
for( j=1; i<GLOBAL_argc; ++i, ++j ) {
|
|
|
|
mpi_argv[j] = strdup( GLOBAL_argv[i] );
|
2002-02-11 20:40:09 +00:00
|
|
|
}
|
|
|
|
mpi_argc = j;
|
|
|
|
|
2011-05-25 16:40:36 +01:00
|
|
|
mpi_argv[0] = strdup( GLOBAL_argv[0] );
|
2002-02-11 20:40:09 +00:00
|
|
|
|
2003-06-26 15:35:52 +01:00
|
|
|
Yap_InitCPred( "mpi_open", 3, p_mpi_open, SafePredFlag|SyncPredFlag );
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_InitCPred( "mpi_close", 0, p_mpi_close, SafePredFlag|SyncPredFlag );
|
|
|
|
Yap_InitCPred( "mpi_send", 3, p_mpi_send, SafePredFlag|SyncPredFlag );
|
2003-06-26 15:35:52 +01:00
|
|
|
Yap_InitCPred( "mpi_receive", 3, p_mpi_receive, SafePredFlag|SyncPredFlag );
|
|
|
|
Yap_InitCPred( "mpi_bcast", 3, p_mpi_bcast3, SafePredFlag|SyncPredFlag );
|
|
|
|
Yap_InitCPred( "mpi_bcast", 2, p_mpi_bcast2, SafePredFlag|SyncPredFlag );
|
|
|
|
Yap_InitCPred( "mpi_barrier", 0, p_mpi_barrier, SafePredFlag|SyncPredFlag );
|
2002-02-11 20:40:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAVE_MPI */
|