diff --git a/library/mpi/examples/demo1.pl b/library/mpi/examples/demo1.pl new file mode 100644 index 000000000..a851bb807 --- /dev/null +++ b/library/mpi/examples/demo1.pl @@ -0,0 +1,49 @@ +%% demo1.pl -- Stasinos Konstantopoulos +%% konstant@let.rug.nl, Thu Jan 24 2002 +%% + +%% +%% This the calculation that needs to be performed, in this case +%% the sum of [From..To] +%% + +calc( From, From, Acc, Res ) :- !, + Res is Acc + From. +calc( From, To, Acc, Res ) :- !, + Acc1 is Acc + To, + To1 is To - 1, + calc( From, To1, Acc1, Res ). + +%% +%% This spreads the work among the processors +%% + +do(0, Num) :- + !, + Half is Num // 2, + format( 'Proc 0: Calculating ~q..~q~n', [1, Half] ), + calc( 1, Half, 0, R1 ), + format( 'Proc 0: Done! (~q)~n', [R1] ), + mpi_receive( R2, Source, Tag ), % Receive any Source, any Tag + format( 'Proc ~q said: ~q (Tag: ~q)~n', [Source,R2,Tag] ), + % mpi_receive( R2, 1, 1 ), % Be more particular + Res is R1 + R2, + format( 'Sum(1..~q) = ~q~n', [Num,Res] ). +do(1, Num) :- + !, + Half is Num // 2, + format( 'Proc 1: Calculating ~q..~q~n', [Half,Num] ), + calc( Half, Num, 0, Res ), + format( 'Proc 1: Done! (~q)~n', [Res] ), + mpi_send( Res, 0, 1 ). + + +%% +%% This is the entry point +%% + +start(Num) :- + mpi_open( Rank, NumProc, ProcName ), + format( 'Rank: ~q NumProc: ~q, ProcName: ~q~n', [Rank,NumProc,ProcName] ), + do( Rank, Num ), + format( 'Rank ~q finished!~n', [Rank] ). diff --git a/library/mpi/mpi.c b/library/mpi/mpi.c new file mode 100644 index 000000000..1164be5f3 --- /dev/null +++ b/library/mpi/mpi.c @@ -0,0 +1,341 @@ +/************************************************************************* +* * +* YAP Prolog * +* * +* Yap Prolog was developed at NCCUP - Universidade do Porto * +* * +* Copyright S. Konstantopoulos and Universidade do Porto 2002 * +* * +************************************************************************** +* * +* File: mpi.c * +* Last rev: $Date: 2002-02-11 20:40:09 $ * +* mods: * +* comments: Interface to an MPI library * +* * +*************************************************************************/ + +#ifndef lint +static char *rcsid = "$Header: /Users/vitor/Yap/yap-cvsbackup/library/mpi/mpi.c,v 1.1 2002-02-11 20:40:09 stasinos Exp $"; +#endif + +#include "Yap.h" +#include "Yatom.h" +#include "yapio.h" + +/* for AtomEof */ +#include "Heap.h" + +#if HAVE_MPI + +#include +#include +#include + +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)); + + +/* + * Auxiliary Data and Functions + */ + +static Int rank, numprocs, namelen; +static char processor_name[MPI_MAX_PROCESSOR_NAME]; + +/* mini-stream */ + +static char buf[255]; +static int bufptr, buflen; + +static int +mpi_putc(Int stream, Int ch) +{ + if( ch > 0 ) + buf[bufptr++] = ch; + return ch; +} + +static Int +mpi_getc(Int stream) +{ + return buf[bufptr++]; +} + +static Int +mpi_eob(void) +{ + return (bufptrTok != Ord (eot_tok)) { + /* we got the end of file from an abort */ + if (ErrorMessage == "Abort") { + TR = old_TR; + return(FALSE); + } + /* we need to force the next reading to also give end of file.*/ + buf[bufptr] = EOF; + ErrorMessage = "[ Error: end of file found before end of term ]"; + } else { + /* restore TR */ + TR = old_TR; + return (unify_constant (ARG2, MkAtomTerm (AtomEof))); + } + } + repeat_cycle: + if (ErrorMessage || (t = Parse ()) == 0) { + if (ErrorMessage && (strcmp(ErrorMessage,"Stack Overflow") == 0)) { + /* ignore term we just built */ + H = old_H; + if (growstack_in_parser(&old_TR, &tokstart, &VarTable)) { + tokptr = toktide = tokstart; + ErrorMessage = NULL; + goto repeat_cycle; + } + } + TR = old_TR; + if (ErrorMessage) + YP_fprintf (YP_stderr, "%s", ErrorMessage); + else + syntax_error (tokstart); + YP_fprintf (YP_stderr, " ]\n"); + + Error(SYSTEM_ERROR,TermNil,NULL); + return(FALSE); + + } else { + /* parsing succeeded */ + break; + } + } + + while (TRUE) { + CELL *old_H = H; + + if (setjmp(IOBotch) == 0) { + v = VarNames(VarTable, TermNil); + TR = old_TR; + break; + } else { + /* don't need to recheck tokens */ + tokstart = NULL; + /* restart global */ + H = old_H; + growstack_in_parser(&old_TR, &tokstart, &VarTable); + old_H = H; + } + } + } + + return unify(t, ARG1); +} + + +/* + * Init + */ + + +void +InitMPI(void) +{ + int i,j; + Int mpi_argc; + char **mpi_argv; + + mpi_argv = malloc( yap_argc * sizeof(char *) ); + mpi_argv[0] = strdup( yap_args[0] ); + + for( i=1; i