New LAM MPI module.

git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@1660 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
This commit is contained in:
nunofonseca 2006-06-02 04:16:31 +00:00
parent 637cb7812c
commit b984d9191e
6 changed files with 1676 additions and 0 deletions

View File

@ -0,0 +1,71 @@
#
# default base directory for YAP installation
# (EROOT for architecture-dependent files)
#
#
# default base directory for YAP installation
# (EROOT for architecture-dependent files)
#
prefix = @prefix@
ROOTDIR = $(prefix)
EROOTDIR = @exec_prefix@
#
# where the binary should be
#
BINDIR = $(EROOTDIR)/bin
#
# where YAP should look for libraries
#
LIBDIR=$(EROOTDIR)/lib/Yap
#
#
CC=@CC@
MPI_CC=mpicc
CFLAGS= @CFLAGS@ $(YAP_EXTRAS) $(DEFS) -I$(srcdir) -I../.. -I$(srcdir)/../../include
#
#
# You shouldn't need to change what follows.
#
INSTALL=@INSTALL@
INSTALL_DATA=@INSTALL_DATA@
INSTALL_PROGRAM=@INSTALL_PROGRAM@
SHELL=/bin/sh
RANLIB=@RANLIB@
srcdir=@srcdir@
SHLIB_CFLAGS=@SHLIB_CFLAGS@
SHLIB_SUFFIX=@SHLIB_SUFFIX@
CWD=$(PWD)
MPIF=`$(MPI_CC) -showme|sed "s/[^ ]*//"|sed "s/-pt/-lpt/"`
#MPIF=-L/usr/local/lib -llammpio -llamf77mpi -lmpi -llam -lutil -lpthread
#
OBJS=yap_mpi.o hash.o prologterms2c.o
SOBJS=yap_mpi@SHLIB_SUFFIX@
#in some systems we just create a single object, in others we need to
# create a libray
all: $(SOBJS)
yap_mpi.o: $(srcdir)/yap_mpi.c $(srcdir)/yap_mpi.c
$(MPI_CC) $(CFLAGS) $(SHLIB_CFLAGS) $(MPIF) -c $(srcdir)/yap_mpi.c -o yap_mpi.o
prologterms2c.o: $(srcdir)/prologterms2c.c $(srcdir)/prologterms2c.h
$(CC) -c $(CFLAGS) $(SHLIB_CFLAGS) $(srcdir)/prologterms2c.c -o prologterms2c.o
hash.o: $(srcdir)/hash.c $(srcdir)/hash.h
$(CC) -c $(CFLAGS) $(SHLIB_CFLAGS) $(srcdir)/hash.c -o hash.o
@DO_SECOND_LD@%@SHLIB_SUFFIX@: %.o
@DO_SECOND_LD@ @SHLIB_LD@ -o $@ $<
@DO_SECOND_LD@yap_mpi@SHLIB_SUFFIX@: $(OBJS)
@DO_SECOND_LD@ @SHLIB_LD@ $(MPIF) -o yap_mpi@SHLIB_SUFFIX@ $(OBJS)
install: all
$(INSTALL_PROGRAM) $(SOBJS) $(DESTDIR)$(LIBDIR)
clean:
rm -f *.o *~ $(OBJS) $(SOBJS) *.BAK

205
library/lammpi/hash.c Normal file
View File

@ -0,0 +1,205 @@
/*===========================================================================================%
* Copyright (c) 2002,2003,2004,2005,2006 Nuno Fonseca. All rights reserved.
* This code is freely available for academic purposes.
* If you intend to use it for commercial purposes then please contact the author first.
*
* Author: Nuno Fonseca
* Date: 2002-07-10
* $Id: hash.c,v 1.1 2006-06-02 04:16:31 nunofonseca Exp $
*
*===========================================================================================*/
#include <stdio.h>
#include <stdlib.h>
#include "hash.h"
#define BUCKET(table,i) table->buckets[i]
#define HASHSIZE(table) table->size
static int mhash(hashtable,ulong);
static hashnode* hash_lookup(hashtable,ulong);
static hashnode* hash_lookup(hashtable table,ulong key){
table->last_node = BUCKET(table,mhash(table,key)); /* set a pointer to the first bucket */
while ( table->last_node != NULL ) {
if( table->last_node->value==key) return table->last_node;
table->last_node = table->last_node->next;
}
return NULL;
}
__ptr_t get_next_object(hashtable table,ulong key)
{
if(table->last_node==NULL)
return NULL;
table->last_node = table->last_node->next;
while ( table->last_node != NULL ) {
if( table->last_node->value==key) return table->last_node->obj;
table->last_node = table->last_node->next;
}
return NULL;
}
/* removes the element with key 'key' and returns the object stored on him */
__ptr_t delete(hashtable table,ulong key)
{
__ptr_t obj;
hashnode *b,*prev=NULL;
int c=mhash(table,key);
b=BUCKET(table,c); /* set a pointer to the first bucket */
while( b!=NULL) {
if( b->value==key){
obj=b->obj;
if(prev==NULL) /* first element */
BUCKET(table,c)=b->next;
else
prev->next=b->next;
free(b);
table->n_entries--;
return obj;
}
prev = b;
b = b->next;
};
return NULL;
}
__ptr_t replace_object(hashtable table,ulong key,__ptr_t newobj)
{
__ptr_t old;
hashnode *b=hash_lookup(table,key);
if(b==NULL)return NULL;
old=b->obj;
b->obj=newobj;
return old;
}
/* looks a 'bucket' in the hashing table whith 'key' and return the
pointer to the object stored in that bucket or NULL if no bucket is found */
__ptr_t get_object(hashtable table,ulong key){
hashnode *b=hash_lookup(table,key);
if(b==NULL)
return NULL;
return b->obj;
}
/* Allocates space to a new hash table */
hashtable new_hashtable(ulong hashsize) {
hashtable new;
register int i;
if( (new = (hashtable)malloc(sizeof(struct hashtable_s)))==NULL) return NULL;
if( (new->buckets = (hashnode**)malloc(sizeof(hashnode*)*hashsize))==NULL)
return NULL;
new->size=hashsize;
new->last_bucket=0;
new->last_node=NULL;
new->n_entries=0;
for(i=0;i<hashsize;++i) BUCKET(new,i) = NULL;
return new;
}
/* A very simple hashing function */
static int mhash(hashtable table,ulong key)
{
return (int)(key%HASHSIZE(table));
}
/* inserts a new element in the hash table*/
int insere(hashtable table,ulong key,__ptr_t obj)
{
int ind;
hashnode *new;
if((new=(hashnode *)malloc(sizeof(hashnode)))==NULL) return -1;
ind=mhash(table,key);
new->next = BUCKET(table,ind);
new->value=key;
new->obj=obj;
BUCKET(table,ind)=new;
table->n_entries++;
return 1;
}
void free_hashtable(hashtable table)
{
register int i;
hashnode *n,*tmp;
//fprintf(stderr,"free_hashtable\n");fflush(stderr);
if (table==NULL) return;
for(i=0;i<HASHSIZE(table);++i) {
n=BUCKET(table,i);
while(n!=NULL) {
tmp=n;
n=n->next;
free(tmp);
}
}
free(table->buckets);
free(table);
}
/*********************************************************************************/
/*
* Returns all objects stored in a basket by making successive calls
*/
void init_hash_traversal(hashtable table) {
table->last_bucket=0;
table->last_node=NULL;
}
/*
* Returns all objects stored in a basket by making successive calls
*/
__ptr_t next_hash_object(hashtable table)
{
// first time....
if( table->last_bucket>=HASHSIZE(table))
return NULL;
if( table->last_node==NULL ) {
// find bucket
// find next bucket
while ( table->last_node == NULL && table->last_bucket+1<HASHSIZE(table)) {
++table->last_bucket;
table->last_node = BUCKET(table,table->last_bucket);
}
if (table->last_node==NULL)
return NULL;
return table->last_node->obj;
}
// Next in bucket
table->last_node=table->last_node->next;
if (table->last_node==NULL) return next_hash_object(table);
return table->last_node->obj;
}
/*
* Returns all hash nodes stored in a basket by making successive calls
*/
__ptr_t next_hashnode(hashtable table)
{
// first time....
if( table->last_bucket>=HASHSIZE(table))
return NULL;
if( table->last_node==NULL ) {
// find bucket
// find next bucket
while ( table->last_node == NULL && table->last_bucket+1<HASHSIZE(table)) {
++table->last_bucket;
table->last_node = BUCKET(table,table->last_bucket);
}
if (table->last_node==NULL)
return NULL;
return table->last_node;
}
// Next in bucket
table->last_node=table->last_node->next;
if (table->last_node==NULL) return next_hashnode(table);
return table->last_node;
}

60
library/lammpi/hash.h Normal file
View File

@ -0,0 +1,60 @@
/*===========================================================================================%
* Copyright (c) 2002,2003,2004,2005,2006 Nuno Fonseca. All rights reserved.
* This code is freely available for academic purposes.
* If you intend to use it for commercial purposes then please contact the author first.
*
* Author: Nuno Fonseca
* Date: 2002-07-10
* $Id: hash.h,v 1.1 2006-06-02 04:16:31 nunofonseca Exp $
*
*===========================================================================================*/
#ifndef HASH
#define HASH
#include <stdlib.h>
#if defined (__cplusplus) || (defined (__STDC__) && __STDC__)
#define __ptr_t void *
#else /* Not C++ or ANSI C. */
#define __ptr_t char *
#endif /* C++ or ANSI C. */
#ifndef ulong
#define ulong unsigned long int
#endif
#ifndef NULL
#define NULL 0
#endif
struct bucket {
struct bucket *next;
ulong value; /* Value >=0 used as key in the hashing*/
__ptr_t obj; /* pointer to a object*/
};
typedef struct bucket hashnode;
struct hashtable_s {
hashnode **buckets; //
ulong size; // number of buckets
ulong last_bucket; // used in searchs/ hash traversals
ulong n_entries; // number of entries in the hashtable
hashnode* last_node;
};
//typedef hashnode **hashtable;
typedef struct hashtable_s* hashtable;
/* functions */
hashtable new_hashtable(ulong hashsize);
__ptr_t get_next_object(hashtable,ulong);
__ptr_t delete(hashtable,ulong);
__ptr_t replace_object(hashtable,ulong,__ptr_t);
__ptr_t get_object(hashtable,ulong);
int insere(hashtable,ulong,__ptr_t);
void free_hashtable(hashtable);
void init_hash_traversal(hashtable table);
__ptr_t next_hash_object(hashtable table);
__ptr_t next_hashnode(hashtable table);
#endif

View File

@ -0,0 +1,227 @@
/************************************************************************
* Copyright (c) 2004 Nuno Fonseca. All rights reserved.
* This code is freely available for academic purposes.
* If you intend to use it for commercial purposes then please contact the author first.
*
* Author: Nuno Fonseca
* File: prologterms2c.c
* Last rev: $Id: prologterms2c.c,v 1.1 2006-06-02 04:16:31 nunofonseca Exp $
* Comments: This file provides a set of functions to convert a prolog term to a C string and back.
*************************************************************************/
#include "prologterms2c.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <malloc.h>
#ifdef COMPRESS
#include "minilzo.h"
#endif
#ifndef Quote_illegal_f
#define Quote_illegal_f 1
#define Ignore_ops_f 2
#define Handle_vars_f 4
#define Use_portray_f 8
#define To_heap_f 16
#endif
#ifdef COMPRESS
#endif
struct buffer_ds buffer;
extern char *Yap_ErrorMessage;
/*********************************************************************************************/
// prototypes
void write_msg(const char *fun,const char *file, int line,const char *format, ...);
size_t write_term_to_stream(const int fd,const YAP_Term term);
YAP_Term read_term_from_stream(const int fd);
/*********************************************************************************************/
/*
* Writes a debug message containing the processid, function name, filename, line, and a user message
*/
void
write_msg(const char *fun,const char *file, int line,
const char *format, ...) {
va_list ap;
va_start(ap, format);
/* Print the message to stderr */
fprintf(stderr,
"[%d:%s in %s at line %d] ", getpid(),fun, file, line);
vfprintf(stderr, format, ap);
va_end(ap);
}
/*********************************************************************************************
* Memory handling functions
*********************************************************************************************/
/*
* Adds 'space' to the size of the currently allocated buffer
*/
static void
expand_buffer(const size_t space ) {
char *oldblock;
// BUFFER_PTR = realloc( BUFFER_PTR, BUFFER_SIZE + space );
oldblock= BUFFER_PTR;
BUFFER_PTR = (char*)malloc( BUFFER_SIZE + space );
if( BUFFER_PTR == NULL ) {
YAP_Error(0,0,"Prolog2Term: Out of memory.\n");
#ifdef MPI
MPI_Finalize();
#endif
YAP_Exit( 1 );
}
memcpy(BUFFER_PTR,oldblock,BUFFER_SIZE);
if(oldblock!=NULL)
free(oldblock);
BUFFER_SIZE+=space;
}
/*
* Changes the size of the buffer to contain at least newsize bytes
*/
void
change_buffer_size(const size_t newsize) {
if ( BUFFER_SIZE>=BLOCK_SIZE && BUFFER_SIZE>newsize)
return;
if(BUFFER_PTR!=NULL)
free(BUFFER_PTR);
BUFFER_PTR = (char*)malloc(newsize);
if( BUFFER_PTR == NULL ) {
YAP_Error(0,0,"Prolog2Term: Out of memory.\n");
#ifdef MPI
MPI_Finalize();
#endif
YAP_Exit( 1 );
}
BUFFER_SIZE=newsize;
}
/*********************************************************************************************
* I/O functions
*********************************************************************************************/
/*
* Function used by YAP to write a char to a string
*/
static void
p2c_putc(const int c) {
// if( buffer.size==buffer.len+1 )
if( BUFFER_SIZE==BUFFER_LEN ) {
#ifdef DEBUG
write_msg(__FUNCTION__,__FILE__,__LINE__,"p2c_putc:buffer expanded: size=%u pos=%u len=%u\n",BUFFER_SIZE,BUFFER_POS,BUFFER_LEN);
#endif
expand_buffer( BLOCK_SIZE );
}
BUFFER_PTR[BUFFER_LEN++] = c;
}
/*
* Function used by YAP to read a char from a string
*/
static int
p2c_getc(void) {
if( BUFFER_POS < BUFFER_LEN )
return BUFFER_PTR[BUFFER_POS++];
return -1;
}
/*
* Writes a term to a stream.
*/
size_t
write_term_to_stream(const int fd,const YAP_Term term) {
RESET_BUFFER;
YAP_Write( term, p2c_putc,3); // 3=canonical
write(fd,(void*)&BUFFER_LEN,sizeof(size_t));// write size of term
write(fd,(void*)BUFFER_PTR,BUFFER_LEN); // write term
return BUFFER_LEN;
}
/*
* Read a prolog term from a stream
* (the prolog term must have been writen by the write_term_to_stream)
*/
YAP_Term
read_term_from_stream(const int fd) {
size_t size;
RESET_BUFFER;
read(fd,(void*)&size,sizeof(size_t)); // read the size of the term
#ifdef DEBUG
write_msg(__FUNCTION__,__FILE__,__LINE__,"read_term_from_stream>>>>size:%d\n",size);
#endif
if ( size> BUFFER_SIZE)
expand_buffer(size-BUFFER_SIZE);
read(fd,BUFFER_PTR,size); // read term from stream
return YAP_Read( p2c_getc );
}
/*********************************************************************************************
* Conversion: Prolog Term->char[] and char[]->Prolog Term
*********************************************************************************************/
/*
* Converts a term t into a string.
* The ascii representation of t is
* copied to ptr if it occupies less than size.
*/
char*
term2string(char *const ptr,size_t *size, const YAP_Term t) {
char *ret;
RESET_BUFFER;
YAP_Write( t, p2c_putc, 3 );// canonical
p2c_putc('\0'); //add terminator
if (BUFFER_LEN<=*size) { // user allocated buffer size is ok
memcpy(ptr,BUFFER_PTR,BUFFER_LEN); // copy data to user block
ret=ptr;
*size=BUFFER_LEN;
} else { // user buffer is too small
ret=BUFFER_PTR;
*size=BUFFER_LEN;
//DEL_BUFFER;
}
return ret;
}
/*
* Converts a string with a ascci representation of a term into a Prolog term.
*/
YAP_Term
string2term(char *const ptr,const size_t *size) {
YAP_Term t;
struct buffer_ds b;
if (BUFFER_PTR!=ptr) { //
#ifdef DEBUG
write_msg(__FUNCTION__,__FILE__,__LINE__,"copy buffer string2term\n");
#endif
COPY_BUFFER_DS(buffer,b); // keep a copy of buffer_ds
BUFFER_PTR=ptr; // make the buffer use the buffer provided
BUFFER_LEN=*size;
BUFFER_SIZE=BUFFER_LEN;
} else { // b aux. struct. not needed
b.ptr=NULL;
}
BUFFER_POS=0;
Yap_ErrorMessage=NULL;
t = YAP_Read(p2c_getc);
if ( t==FALSE ) {
write_msg(__FUNCTION__,__FILE__,__LINE__,"FAILED string2term>>>>size:%d %d %s\n",BUFFER_SIZE,strlen(BUFFER_PTR),Yap_ErrorMessage);
exit(1);
}
if (b.ptr!=NULL)
COPY_BUFFER_DS(b,buffer);
#ifdef DEBUG
write_msg(__FUNCTION__,__FILE__,__LINE__,"ending: buffer(ptr=%p;size=%d;pos=%d;len=%d)\n",BUFFER_PTR,BUFFER_SIZE,BUFFER_POS,BUFFER_LEN);
#endif
return t;
}

View File

@ -0,0 +1,85 @@
/************************************************************************
* Copyright (c) 2004,2005,2006 Nuno Fonseca. All rights reserved.
* This code is freely available for academic purposes.
* If you intend to use it for commercial purposes then please contact the author first.
*
* Author: Nuno Fonseca
* File: prologterms2c.c
* Last rev: $Id: prologterms2c.h,v 1.1 2006-06-02 04:16:31 nunofonseca Exp $
* Comments: This file provides a set of functions to convert a prolog term to a C string and back.
*************************************************************************/
#ifndef PROLOGTERMS2C
#define PROLOGTERMS2C 1
#ifndef _yap_c_interface_h
#include <YapInterface.h>
//#include <yap_structs.h>
#endif
#ifndef size_t
#include <unistd.h>
#endif
#include <stdarg.h>
/*
* Converts a term t into a string.
* The ascii representation of t is
* copied to ptr if it occupies less than size. Otherwise the
* necessary memory is aloccated (dyn_ptr) and the ascii
* representation of the term is copied to there.
*/
char* term2string(char *const ptr,size_t *size, const YAP_Term t);
/*
* Converts a string with a ascci representation of a term into a term.
* The ascii representation of t is
* copied to ptr if it occupies less than *size. Otherwise the
* necessary memory is aloccated and the ascii
* representation of the term is copied to there.
*/
YAP_Term string2term(char *const ptr,const size_t *size);
/*
* Read a prolog term from a stream
* (the prolog term must have been writen by the write_term_to_stream)
*/
YAP_Term read_term_from_stream(const int fd);
/*
* Writes a term to a stream.
*/
size_t write_term_to_stream(const int fd,const YAP_Term term);
/*
* Changes the size of the buffer to contain at least newsize bytes.
* Useful to reduce the number of reallocs,mallocs, and frees
*/
void change_buffer_size(const size_t newsize);
void write_msg(const char *fun,const char *file, int line,const char *format, ...);
/*********************************************************************************************
* Macros to manipulate the buffer
*********************************************************************************************/
#define BLOCK_SIZE 4*1024
// deletes the buffer (all fields) but does not release the memory of the buffer.ptr
#define DEL_BUFFER {buffer.ptr=NULL;buffer.size=0;buffer.len=0;}
// informs the prologterm2c module that the buffer is now used and should not be messed
#define USED_BUFFER() DEL_BUFFER
// initialize buffer
#define RESET_BUFFER {buffer.len=0;change_buffer_size(BLOCK_SIZE);buffer.pos=0;}
#define BUFFER_PTR buffer.ptr
#define BUFFER_SIZE buffer.size
#define BUFFER_LEN buffer.len
#define BUFFER_POS buffer.pos
// copies two buffers
#define COPY_BUFFER_DS(src,dst) {dst.size=src.size;dst.len=src.len;dst.ptr=src.ptr;dst.pos=src.pos;}
/*********************************************************************************************
* Buffer
*********************************************************************************************/
struct buffer_ds {
size_t size, // size of the buffer
len; // size of the string
char *ptr; // pointer to the buffer
size_t pos; // position (used while reading)
};
extern struct buffer_ds buffer;
#endif

1028
library/lammpi/yap_mpi.c Normal file

File diff suppressed because it is too large Load Diff