Commit f2b992b1 authored by jclamber's avatar jclamber

add nemolight lib in case of no NEMO installation

git-svn-id: http://svn.oamp.fr/repos/unsio/trunk@109 ce2cc22f-6084-46ce-a062-084b172ee5dc
parent 0ff18472
# ============================================================================
# Copyright Jean-Charles LAMBERT - 2009-2012
# e-mail: Jean-Charles.Lambert@oamp.fr
# address: Dynamique des galaxies
# Laboratoire d'Astrophysique de Marseille
# Pole de l'Etoile, site de Chateau-Gombert
# 38, rue Frederic Joliot-Curie
# 13388 Marseille cedex 13 France
# CNRS U.M.R 6110
# ============================================================================
# CMakeListst.txt file for nemo library
# ============================================================================
cmake_minimum_required(VERSION 2.6)
# project name
project (nemo)
# set CMAKE Module PATH (NEMO)
SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_BINARY_DIR})
MESSAGE( STATUS "CMAKE_MODULE_PATH=" ${CMAKE_MODULE_PATH} )
# detect MacOS
SET(OSX FALSE)
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
SET(OSX TRUE)
endif (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
# contains the full path to the top level directory of your build tree
MESSAGE( STATUS "PROJECT_BINARY_DIR: " ${PROJECT_BINARY_DIR} )
MESSAGE( STATUS "PROJECT_SOURCE_DIR: " ${PROJECT_SOURCE_DIR} )
# Lib : libnemo.so
# Find all the sources for the utils LIB
FILE(GLOB LIBUTILS ../src/*.c )
# create the library "libnemo.so"
add_library (nemo SHARED ${LIBUTILS})
# Lib : libnemomaing77.so
# Find all the sources for old fortran compiler
FILE(GLOB LIBUTILS ../src/fortran_old_gcc/*.c )
# create the library "libnemomaing77.so"
add_library (nemomaing77 SHARED ${LIBUTILS})
if(OSX)
set_target_properties(MNESSutils PROPERTIES LINK_FLAGS "-undefined suppress -flat_namespace")
endif(OSX)
# Destination path for the lib
SET(LIBRARY_OUTPUT_PATH ../lib)
# compilation options
add_definitions(-Wall -O2 -g -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE)
# SET NEMOPATH
SET(NEMOLIB ${PROJECT_SOURCE_DIR}/../lib)
SET(NEMOINC ${PROJECT_SOURCE_DIR}/../src/inc)
# Make sure the compiler can find include files from our Hello library.
include_directories (../src/inc )
# ----------------------------------------------------------
# install target
set (CMAKE_INSTALL_PREFIX $ENV{NEMO})
IF (OSX) # Mac OSX
SET(SOEXT "dylib")
ELSE (OSX) # Linux
SET(SOEXT "so")
ENDIF(OSX)
#
# install bin targets
#
/*
* ALLOCATE: memory (re)allocation with fatal error checking.
*
* <dark ages> created Josh?
* 4-oct-90 added realloc Peter
* 16-nov-90 made realloc understand NULL pointers PJT
* 25-feb-92 somebody commented out malloc()?
* 15-apr-92 AIX
* 26-jan-94 extra error() when nb < 1 pjt
* 3-jul-94 hmmm, allow 0 bytes, but alloc 1! pjt
* 22-jan-95 proto
* 3-may-95 added dprintf()
* 6-apr-01 changed malloc -> calloc pjt
* jan-02 experimenting with exception handling pjt/nas
* 7-sep-05 TOOLBOX benchmark pjt/chapman
* 31-may-07 use size_t to better match malloc() pjt/Pierre Fortin <pierre.fortin@oamp.fr>
* 12-jun-08 allocate_FL etc, see stdinc.h WD
* 12-jun-08 removed tests for size_t < 0 WD
* 03-oct-08 debugged error in debug_info reporting WD
*/
#include <stdinc.h>
#include <errno.h>
void *allocate_FL(size_t nb, const_string file, int line)
{
void *mem;
if (sizeof(size_t) == 4 && nb > 2147483647)
warning("allocate: 32bit machine allocate");
if (nb==0) nb++; /* never allocate 0 bytes */
mem = (void *) calloc(nb, 1);
if (mem == NULL) {
nemo_dprintf(0,"solaris csh: limit datasize unlimited\n");
nemo_dprintf(0,"solaris ksh: ulimit -d unlimited\n");
if(file) error("[%s:%d]: cannot allocate %lu bytes",file,line,nb);
else error("cannot allocate %lu bytes",nb);
}
if(file)
nemo_dprintfN(8,"[%s:%d]: allocated %lu bytes @ %p\n",file,line,nb,mem);
else
nemo_dprintfN(8,"allocated %lu bytes @ %p\n",nb,mem);
return mem;
}
void *reallocate_FL(void *bp, size_t nb, const_string file, int line)
{
void *mem;
if (nb == 0) nb++;
if(bp==NULL)
mem = (void *) calloc(nb, 1);
else
mem = (void *) realloc((void *)bp,nb);
if (mem == NULL) {
if(file) error("[%s:%d]: cannot reallocate %lu bytes",file,line,nb);
else error("cannot reallocate %lu bytes",nb);
}
if(file)
nemo_dprintfN(8,"[%s:%d]: reallocated %lu bytes @ %p\n",file,line,nb,mem);
else
nemo_dprintfN(8,"reallocated %lu bytes @ %p\n",nb,mem);
return mem;
}
void *
my_calloc(size_t nmemb, size_t size)
{
static int my_counter = 0;
my_counter++;
if (my_counter == 5) return NULL;
return calloc( nmemb, size);
}
#if defined(TESTBED)
#include <nemo.h>
string defv[] = {
"size=16\n Size of a single bucket (in kB) to allocate",
"nalloc=0\n Number of extra times to allocate <size>",
"incr=16\n Increment size (in kB) to reallocate the size\n",
"nrealloc=0\n Number of times to increment and reallocate\n",
"repeat=1\n How often to repeat the whole test",
"big1=100\n Allocate the product of these two",
"big2=100\n Allocate the product of these two",
"doubling=f\n Doubling the big1*big2 allocation until failure",
"VERSION=2.0\n 4-jul-2010 PJT",
NULL,
};
string usage = "(re)allocate benchmark";
void nemo_main(void) {
int size, size0 = getiparam("size")*1024;
int nalloc = getiparam("nalloc");
int incr = getiparam("incr")*1024;
int nrealloc = getiparam("nrealloc");
int repeat = getiparam("repeat");
int big1 = getiparam("big1");
int big2 = getiparam("big2");
int big = big1*big2;
size_t big64 = (size_t)big1*(size_t)big2; /* this is crucial to cast */
size_t i64;
int i;
bool Qdouble = getbparam("doubling");
char *data;
nemo_dprintf(0," Alloc: %d * %d bytes\n",nalloc,size0);
nemo_dprintf(0,"ReAlloc: %d * %d bytes\n",nrealloc,incr);
if (big < 0) warning("big < 0: %d overflow? (%d x %d) %ld",big,big1,big2,big64);
if (big64 < 0) warning("big64 < 0: %d overflow? (%d x %d)" ,big,big1,big2);
dprintf(0,"sizeof(size_t) = %d\n",sizeof(size_t));
data = allocate(big64);
free(data);
dprintf(0,"Passed big64 allocating %ld\n",big64);
while (Qdouble) {
big64 *= 2;
data = allocate(big64);
for (i64=0; i64<big64; i64++)
data[i64] = 0;
free(data);
dprintf(0,"Passed big64 allocating %ld\n",big64);
}
while (repeat-- > 0) { /* repeat loop */
size = size0; /* allocate loop */
data = allocate(size);
if (nalloc > 1) {
for (i=0; i<nalloc; i++) {
free(data);
size += size0;
data = allocate(size);
}
free(data);
}
size = size0; /* reallocate loop */
data = reallocate(0,size);
nemo_dprintf(1,"%d %d %d\n",repeat,size,incr);
if (nrealloc > 1) {
for (i=0; i<nrealloc; i++) {
size += incr;
data = reallocate(data,size);
}
}
free(data);
}
}
#endif
/*
* BSWAP: swap bytes - called by lower level filestruct routines
* if a byte swapped data set is detected at input.
* dat pointer to the data
* len item length in bytes, this amount will be swapped
* cnt total number of data-items of length 'len' to be
* swapped
*
* TESTBED: can also be used to tweek this routine and measure
* simple CPU operations.
* BUGS: cannot check if dat is long enough (len*cnt)
* NOTES: An IBM RISC is about 40% slower than a SPARC1
*
* See also: RFC 1832 e.g.: http://www.faqs.org/rfcs/rfc1832.html
*
* History:
* 12-oct-90 V1.0 PJT Written
* 20-nov-91 V1.1 PJT NEMO V2.x nemo_main interface, added out=
* 21-nov-91 TOOLBOX version called bswap for Ultrix-SUN
* word swap for Stephen White; 'dd conv=swab' will do
* 27-nov-91 fixed typo (missing declared k; l->k)
* 25-feb-92 happy gcc2.0
* 20-nov-92 TOOLBOX, not TESTBED
* 25-feb-94 argument now (void *) from (char *) for ansi
* 9-apr-94 also handle pipes in TOOLBOX section
* 4-aug-94 warn about small files where t1==t0 etc. -- TOOLBOX only
* 23-feb-97 allow offset until which no swapping done
* 15-dec-98 TOOLBOX : allow streaming mode
* 12-sep-01 file_size
* 30-sep-03 testing memcpy, and improved the testing
* 20-sep-05 little and big endian versions
*/
#include <stdinc.h>
void bswap(void *vdat, int len, int cnt)
{
char tmp, *dat = (char *) vdat;
int k;
if (len==1)
return;
else if (len==2)
while (cnt--) {
tmp = dat[0]; dat[0] = dat[1]; dat[1] = tmp;
dat += 2;
}
else if (len==4)
while (cnt--) {
tmp = dat[0]; dat[0] = dat[3]; dat[3] = tmp;
tmp = dat[1]; dat[1] = dat[2]; dat[2] = tmp;
dat += 4;
}
else if (len==8)
while (cnt--) {
tmp = dat[0]; dat[0] = dat[7]; dat[7] = tmp;
tmp = dat[1]; dat[1] = dat[6]; dat[6] = tmp;
tmp = dat[2]; dat[2] = dat[5]; dat[5] = tmp;
tmp = dat[3]; dat[3] = dat[4]; dat[4] = tmp;
dat += 8;
}
else { /* the general SLOOOOOOOOOWE case */
for(k=0; k<len/2; k++) {
tmp = dat[k];
dat[k] = dat[len-1-k];
dat[len-1-k] = tmp;
}
}
}
/*
* bswap_bigend: bswap only if the source data was big endian
*/
void bswap_bigend(void *vdat, int len, int cnt)
{
#if !defined(WORDS_BIGENDIAN)
bswap(vdat,len,cnt);
#endif
}
/*
* bswap_litend: bswap only if the source data was little endian
*/
void bswap_litend(void *vdat, int len, int cnt)
{
#if defined(WORDS_BIGENDIAN)
bswap(vdat,len,cnt);
#endif
}
#if defined(TOOLBOX)
#include <getparam.h>
string defv[] = {
"in=???\n File to read and swap bytes of",
"out=\n Filename if the swapped file to be output",
"len=2\n Itemlength in bytes during swapping",
"oneswap=t\n One swap call? (or many) - for testing only",
"offset=0\n Offset (in bytes) before which no swapping done",
"endian=\n assume 'Little' (l) or 'Big' (b) endian input file",
"memcpy=f\n Testing swapping double another way with memcpy",
"repeat=0\n How many times to repeat the swapping (speed testing)",
"VERSION=1.6\n 20-sep-05 PJT",
NULL,
};
string usage="swap bytes in a file";
extern int nemo_file_size(string);
void byteswap_doubles(double *a)
{
unsigned char b[8],c[8];
memcpy(b,a,8);
c[0]=b[7]; /* swap data around */
c[1]=b[6];
c[2]=b[5];
c[3]=b[4];
c[4]=b[3];
c[5]=b[2];
c[6]=b[1];
c[7]=b[0];
memcpy(a,c,8);
}
typedef void (*bswap_proc)(void *, int, int);
void nemo_main(void)
{
stream instr, outstr;
string fname, endian;
char *data, *dp;
real t0, t1, t2, rspeed=0, wspeed=0;
int i, len, cnt, offset, repeat, nrepeat;
bool onetrip;
bool Qmemcpy = getbparam("memcpy");
bswap_proc bptr;
fname = getparam("in");
len = getiparam("len");
offset = getiparam("offset");
nrepeat = repeat = getiparam("repeat");
onetrip = getbparam("oneswap");
if (hasvalue("endian")) {
endian = getparam("endian");
if (*endian == 'l' || *endian == 'L')
bptr = bswap_litend;
else if (*endian == 'b' || *endian == 'B')
bptr = bswap_bigend;
else
error("Bad endian=%s; need 'big' or 'little' endian",endian);
} else
bptr = bswap;
if (hasvalue("out"))
outstr = stropen(getparam("out"),"w");
else {
warning("No swapped output file created");
outstr = NULL;
}
if (Qmemcpy)
if (len != 8) {
warning("Cannot do memcpy, len=%d, needs to be 8",len);
Qmemcpy = FALSE;
} else
warning("memcpy mode");
instr = stropen(fname,"r");
cnt = nemo_file_size(fname); /* size of the file in bytes */
if (cnt < 0) { /* streaming mode */
dprintf(0,"Streaming mode\n");
data = (char *) allocate(len);
while (fread(data,len,1,instr) == 1) {
bswap(data,len,1);
fwrite(data,1,len,outstr);
}
strclose(outstr);
} else {
if (cnt % len)
warning("Filesize is not a multiple of itemlength: %d / %d", cnt, len);
cnt /= len; /* cnt is now number of items of 'len' */
if (cnt==0) error("File %s too small; nemo_file_size() returned 0",fname);
data = (char *) allocate(cnt*len);
t0 = 60 * cputime();
if (cnt != fread(data,len,cnt,instr)) error("Error reading %s",fname);
t1 = 60 * cputime();
do {
if (onetrip) {
/* by doing a one-trip, speed is about 160 ; 275 for double */
bswap(data,len,cnt);
} else {
/*
* by incrementing 'dp += len' speed went from 70 to 45 (167 to 116 for double)
*/
i = cnt;
for (i=0, dp=data; i<cnt; i++, dp += len) {
if (Qmemcpy) {
/* 40 for double */
byteswap_doubles((double *)dp);
} else
/* 116 for double */
bswap(dp,len,1);
}
}
} while (repeat-- > 0);
nrepeat++;
t2 = 60 * cputime();
if (t1==t0) warning("Inaccurate measurement for reading");
else rspeed=cnt*len/(t1-t0)/1048576.0*nrepeat;
if (t1==t2) warning("Inaccurate measurement for writing");
else wspeed=cnt*len/(t2-t1)/1048576.0*nrepeat;
dprintf(1,"Read %d: %f s; %d x swap %d * %d bytes: %f s; %g %g Mswap\n",
cnt*len, t1-t0, nrepeat, cnt, len, t2-t1,
rspeed,wspeed);
printf("%d %d %d %g %g\n",
cnt,len,(onetrip?1:0),
rspeed,wspeed);
if (outstr) {
if (cnt != fwrite(data,len,cnt,outstr))
error("Error writing %s",getparam("out"));
strclose(outstr);
}
}
}
#endif
/* burststring, burst2string, freestrings, splitstring(not tested!!!)
*
* 25-dec-94 added burst2string()
* 20-may-96 added burst0string() for checker
* 28-nov-00 changed some names to make it compile for C++
* 20-jun-01 prototypes to help gcc3
*
* BURSTSTRING: break a string of the form "word1, word2, ..." into
* seperate strings "word1", "word2", ... and return them in an
* extended-string (ie, NULL-terminated sequence of pointers).
*/
#include <stdinc.h>
#include <extstring.h>
#define MWRD 1024 /* max words in list */
#define MSTR 256 /* max chars per word */
string *burststring(string lst, string sep)
{
string wrdbuf[MWRD], *wp;
char strbuf[MSTR], *sp, *lp;
wp = wrdbuf;
sp = strbuf;
lp = lst;
do { /* scan over list */
if (*lp == 0 || strchr(sep, *lp) != NULL) { /* is this a sep? */
if (sp > strbuf) { /* got a word? */
*sp = 0;
*wp++ = (string) copxstr(strbuf, sizeof(char));
if (wp == &wrdbuf[MWRD]) /* no room? */
error("burststring: too many words (%d)",MWRD);
sp = strbuf; /* for next 1 */
}
} else { /* part of word */
*sp++ = *lp; /* so copy it */
if (sp == &strbuf[MSTR]) /* no room? */
error("burststring: word too long (%d)",MSTR);
}
} while (*lp++ != 0); /* until list ends */
*wp = NULL;
return ((string *) copxstr((char *)wrdbuf, sizeof(string))); /*PPAP*/
}
/*
* BURST1STRING: similar to burststring, but now each separator
* triggers a new word for output so you can have zero-length
* words coming out. Use with care.
*/
string *burst0string(string lst, string sep)
{
string wrdbuf[MWRD], *wp;
char strbuf[MSTR], *sp, *lp;
wp = wrdbuf;
sp = strbuf;
lp = lst;
do { /* scan over list */
if (*lp == 0 || strchr(sep, *lp) != NULL) { /* is this a sep? */
*sp = 0;
*wp++ = (string) copxstr(strbuf, sizeof(char));
if (wp == &wrdbuf[MWRD]) /* no room? */
error("burststring: too many words\n");
sp = strbuf; /* for next 1 */
} else { /* part of word */
*sp++ = *lp; /* so copy it */
if (sp == &strbuf[MSTR]) /* no room? */
error("burststring: word too long\n");
}
} while (*lp++ != 0); /* until list ends */
*wp = NULL;
return ((string *) copxstr((char *)wrdbuf, sizeof(string))); /*PPAP*/
}
/*
* BURST2STRING: like burstring() but also copies the separaters
* in the alternating parts of the extneded string.
* To find out if the first 'word' is a word, or a separator 'word'
* you can try strchr(sep,*b2s[0]): should return NULL is the first
* word is a true word.
* This routine is useful if you need to paste the 'words' back together,
* perhaps with some editing, into the original string (lst)
*
*/
string *burst2string(string lst, string sep)
{
string wrdbuf[MWRD], *wp;
char strbuf[MSTR], *sp, *lp;
int olds, news;
wp = wrdbuf;
sp = strbuf;
lp = lst;
if (*lp)
olds = (strchr(sep,*lp) != NULL);
else
olds = 0; /* doesn't matter what this is */
do { /* scan over list */
if (*lp)
news = strchr(sep,*lp) != NULL;
else
news = !olds; /* force update */