Skip to content

Commit

Permalink
initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
mreineck committed Jun 27, 2012
0 parents commit 2942428
Show file tree
Hide file tree
Showing 53 changed files with 9,133 additions and 0 deletions.
339 changes: 339 additions & 0 deletions COPYING

Large diffs are not rendered by default.

50 changes: 50 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
SHARP_TARGET?=auto
ifndef SHARP_TARGET
SHARP_TARGET:=$(error SHARP_TARGET undefined. Please see README.compilation for help)UNDEFINED
endif

default: compile_all
SRCROOT:=$(shell pwd)
include $(SRCROOT)/config/config.$(SHARP_TARGET)
include $(SRCROOT)/config/rules.common

all_hdr:=
all_lib:=
all_cbin:=

FULL_INCLUDE:=

include c_utils/planck.make
include libfftpack/planck.make
include libsharp/planck.make
include docsrc/planck.make

$(all_lib): %: | $(LIBDIR)_mkdir
@echo "# creating library $*"
$(ARCREATE) $@ $^

$(all_cbin): %: | $(BINDIR)_mkdir
@echo "# linking C binary $*"
$(CL) -o $@ $^ $(CLFLAGS)
# $(CXX) -o $@ $^ $(CLFLAGS)

compile_all: $(all_cbin) hdrcopy

autotune: sharp_bench
$(BINDIR)/sharp_bench
mv oracle.inc $(SRCROOT)/libsharp
$(MAKE)

hdrclean:
@if [ -d $(INCDIR) ]; then rm -rf $(INCDIR)/* ; fi

hdrcopy: | $(INCDIR)_mkdir
@if [ "$(all_hdr)" ]; then cp -p $(all_hdr) $(INCDIR); fi

$(notdir $(all_cbin)) : % : $(BINDIR)/%

test: compile_all
$(BINDIR)/sharp_acctest && \
$(BINDIR)/sharp_test healpix 2048 1024 1 0 1 && \
$(BINDIR)/sharp_test ecp 2047 4096 0 2 1 && \
$(BINDIR)/sharp_test gauss 2047 4096 0 0 2
16 changes: 16 additions & 0 deletions README.compilation
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
GNU make and GNU gcc (version 4.x) are required for compilation.

Simply run "./configure"; if this fails, please refer to the output of
"./configure --help" for additional hints and, if necessary, provide
additional flags to the configure script.
Once the script finishes successfully, run "make autotune"
(or "gmake autotune"). This should perform some necessary self-tuning and
install the compilation products in the subdirectory "auto/".
NOTE: Autotuning should be done on the the computer where you wish to use
the library later on, and no other CPU-intensive tasks should be running
during the autotuning process.

Documentation can be created by the command "(g)make doc".
However this requires the doxygen application to be installed
on your system.
The documentation will be created in the subdirectory doc/.
145 changes: 145 additions & 0 deletions c_utils/c_utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
* This file is part of libc_utils.
*
* libc_utils is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* libc_utils is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with libc_utils; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

/*
* libc_utils is being developed at the Max-Planck-Institut fuer Astrophysik
* and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
* (DLR).
*/

/*
* Convenience functions
*
* Copyright (C) 2008, 2009, 2010, 2011 Max-Planck-Society
* Author: Martin Reinecke
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "c_utils.h"
#include "vec_utils.h"
#ifdef _OPENMP
#include <omp.h>
#endif
#ifdef USE_MPI
#include <mpi.h>
#endif

void util_fail_ (const char *file, int line, const char *func, const char *msg)
{
fprintf(stderr,"%s, %i (%s):\n%s\n",file,line,func,msg);
exit(1);
}
void util_warn_ (const char *file, int line, const char *func, const char *msg)
{
fprintf(stderr,"%s, %i (%s):\n%s\n",file,line,func,msg);
}

/* This function tries to avoid allocations with a total size close to a high
power of two (called the "critical stride" here), by adding a few more bytes
if necssary. This lowers the probability that two arrays differ by a multiple
of the critical stride in their starting address, which in turn lowers the
risk of cache line contention. */
static size_t manipsize(size_t sz)
{
const size_t critical_stride=4096, cacheline=64, overhead=32;
if (sz < (critical_stride/2)) return sz;
if (((sz+overhead)%critical_stride)>(2*cacheline)) return sz;
return sz+2*cacheline;
}

#ifdef __SSE__
#include <xmmintrin.h>
void *util_malloc_ (size_t sz)
{
void *res;
if (sz==0) return NULL;
res = _mm_malloc(manipsize(sz),16);
UTIL_ASSERT(res,"_mm_malloc() failed");
return res;
}
void util_free_ (void *ptr)
{ if ((ptr)!=NULL) _mm_free(ptr); }
#else
void *util_malloc_ (size_t sz)
{
void *res;
if (sz==0) return NULL;
res = malloc(manipsize(sz));
UTIL_ASSERT(res,"malloc() failed");
return res;
}
void util_free_ (void *ptr)
{ if ((ptr)!=NULL) free(ptr); }
#endif

static void OpenMP_status(void)
{
#ifndef _OPENMP
printf("OpenMP: not supported by this binary\n");
#else
int threads = omp_get_max_threads();
if (threads>1)
printf("OpenMP active: max. %d threads.\n",threads);
else
printf("OpenMP active, but running with 1 thread only.\n");
#endif
}

static void MPI_status(void)
{
#ifndef USE_MPI
printf("MPI: not supported by this binary\n");
#else
int tasks;
MPI_Comm_size(MPI_COMM_WORLD,&tasks);
if (tasks>1)
printf("MPI active with %d tasks.\n",tasks);
else
printf("MPI active, but running with 1 task only.\n");
#endif
}

static void vecmath_status(void)
{ printf("Supported vector length: %d\n",VLEN); }

void announce_c (const char *name)
{
size_t m, nlen=strlen(name);
printf("\n+-");
for (m=0; m<nlen; ++m) printf("-");
printf("-+\n");
printf("| %s |\n", name);
printf("+-");
for (m=0; m<nlen; ++m) printf("-");
printf("-+\n\n");
vecmath_status();
OpenMP_status();
MPI_status();
printf("\n");
}

void module_startup_c (const char *name, int argc, int argc_expected,
const char *argv_expected, int verbose)
{
if (verbose) announce_c (name);
if (argc==argc_expected) return;
if (verbose) fprintf(stderr, "Usage: %s %s\n", name, argv_expected);
exit(1);
}
151 changes: 151 additions & 0 deletions c_utils/c_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
* This file is part of libc_utils.
*
* libc_utils is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* libc_utils is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with libc_utils; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

/*
* libc_utils is being developed at the Max-Planck-Institut fuer Astrophysik
* and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
* (DLR).
*/

/*! \file c_utils.h
* Convenience functions
*
* Copyright (C) 2008, 2009, 2010, 2011 Max-Planck-Society
* \author Martin Reinecke
* \note This file should only be included from .c files, NOT from .h files.
*/

#ifndef PLANCK_C_UTILS_H
#define PLANCK_C_UTILS_H

#include <math.h>
#include <stdlib.h>
#include <stddef.h>

#ifdef __cplusplus
extern "C" {
#endif

void util_fail_ (const char *file, int line, const char *func, const char *msg);
void util_warn_ (const char *file, int line, const char *func, const char *msg);
void *util_malloc_ (size_t sz);
void util_free_ (void *ptr);

void announce_c (const char *name);
void module_startup_c (const char *name, int argc, int argc_expected,
const char *argv_expected, int verbose);

#if defined (__GNUC__)
#define UTIL_FUNC_NAME__ __func__
#else
#define UTIL_FUNC_NAME__ "unknown"
#endif

/*! \def UTIL_ASSERT(cond,msg)
If \a cond is false, print an error message containing function name,
source file name and line number of the call, as well as \a msg;
then exit the program with an error status. */
#define UTIL_ASSERT(cond,msg) \
if(!(cond)) util_fail_(__FILE__,__LINE__,UTIL_FUNC_NAME__,msg)
/*! \def UTIL_WARN(cond,msg)
If \a cond is false, print an warning containing function name,
source file name and line number of the call, as well as \a msg. */
#define UTIL_WARN(cond,msg) \
if(!(cond)) util_warn_(__FILE__,__LINE__,UTIL_FUNC_NAME__,msg)
/*! \def UTIL_FAIL(msg)
Print an error message containing function name,
source file name and line number of the call, as well as \a msg;
then exit the program with an error status. */
#define UTIL_FAIL(msg) \
util_fail_(__FILE__,__LINE__,UTIL_FUNC_NAME__,msg)

/*! \def ALLOC(ptr,type,num)
Allocate space for \a num objects of type \a type. Make sure that the
allocation succeeded, else stop the program with an error. Return the
resulting pointer in \a ptr. */
#define ALLOC(ptr,type,num) \
do { (ptr)=(type *)util_malloc_((num)*sizeof(type)); } while (0)
/*! \def RALLOC(type,num)
Allocate space for \a num objects of type \a type. Make sure that the
allocation succeeded, else stop the program with an error. Cast the
resulting pointer to \a (type*). */
#define RALLOC(type,num) \
((type *)util_malloc_((num)*sizeof(type)))
/*! \def DEALLOC(ptr)
Deallocate \a ptr. It must have been allocated using \a ALLOC or
\a RALLOC. */
#define DEALLOC(ptr) \
do { util_free_(ptr); (ptr)=NULL; } while(0)
#define RESIZE(ptr,type,num) \
do { util_free_(ptr); ALLOC(ptr,type,num); } while(0)
#define GROW(ptr,type,sz_old,sz_new) \
do { \
if ((sz_new)>(sz_old)) \
{ RESIZE(ptr,type,2*(sz_new));sz_old=2*(sz_new); } \
} while(0)
/*! \def SET_ARRAY(ptr,i1,i2,val)
Set the entries \a ptr[i1] ... \a ptr[i2-1] to \a val. */
#define SET_ARRAY(ptr,i1,i2,val) \
do { \
ptrdiff_t cnt_; \
for (cnt_=(i1);cnt_<(i2);++cnt_) (ptr)[cnt_]=(val); \
} while(0)
/*! \def COPY_ARRAY(src,dest,i1,i2)
Copy the entries \a src[i1] ... \a src[i2-1] to
\a dest[i1] ... \a dest[i2-1]. */
#define COPY_ARRAY(src,dest,i1,i2) \
do { \
ptrdiff_t cnt_; \
for (cnt_=(i1);cnt_<(i2);++cnt_) (dest)[cnt_]=(src)[cnt_]; \
} while(0)

#define ALLOC2D(ptr,type,num1,num2) \
do { \
size_t cnt_, num1_=(num1), num2_=(num2); \
ALLOC(ptr,type *,num1_); \
ALLOC(ptr[0],type,num1_*num2_); \
for (cnt_=1; cnt_<num1_; ++cnt_) \
ptr[cnt_]=ptr[cnt_-1]+num2_; \
} while(0)
#define DEALLOC2D(ptr) \
do { if(ptr) DEALLOC((ptr)[0]); DEALLOC(ptr); } while(0)

#define FAPPROX(a,b,eps) \
(fabs((a)-(b))<((eps)*fabs(b)))
#define ABSAPPROX(a,b,eps) \
(fabs((a)-(b))<(eps))
#define IMAX(a,b) \
(((a)>(b)) ? (a) : (b))
#define IMIN(a,b) \
(((a)<(b)) ? (a) : (b))

#define SWAP(a,b,type) \
do { type tmp_=(a); (a)=(b); (b)=tmp_; } while(0)

#define CHECK_STACK_ALIGN(align) \
do { \
double foo; \
UTIL_WARN((((size_t)(&foo))&(align-1))==0, \
"WARNING: stack not sufficiently aligned!"); \
} while(0)

#ifdef __cplusplus
}
#endif

#endif
18 changes: 18 additions & 0 deletions c_utils/planck.make
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
PKG:=c_utils

SD:=$(SRCROOT)/$(PKG)
OD:=$(BLDROOT)/$(PKG)

FULL_INCLUDE+= -I$(SD)

HDR_$(PKG):=$(SD)/*.h
LIB_$(PKG):=$(LIBDIR)/libc_utils.a

OBJ:=c_utils.o walltime_c.o
OBJ:=$(OBJ:%=$(OD)/%)

$(OBJ): $(HDR_$(PKG)) | $(OD)_mkdir
$(LIB_$(PKG)): $(OBJ)

all_hdr+=$(HDR_$(PKG))
all_lib+=$(LIB_$(PKG))
Loading

0 comments on commit 2942428

Please sign in to comment.