Skip to content

Commit 48e2131

Browse files
author
Dag Sverre Seljebotn
committed
First draft of SHTs in Python wrapper
1 parent c680bbb commit 48e2131

7 files changed

Lines changed: 397 additions & 17 deletions

File tree

Makefile

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,10 @@ perftest: compile_all
6262
genclean:
6363
rm libsharp/sharp_legendre.c || exit 0
6464

65-
pytest:
66-
rm python/libsharp/libsharp.so || exit 0
67-
cd python && LIBSHARP_INCLUDE=$(INCDIR) LIBSHARP_LIB=$(LIBDIR) python setup.py build_ext --inplace
68-
cd python && nosetests libsharp
69-
65+
python/libsharp/libsharp.so: python/libsharp/libsharp.pyx $(LIB_libsharp)
66+
cython $<
67+
$(CC) -fPIC `python-config --cflags` -I$(INCDIR) -o python/libsharp/libsharp.o -c python/libsharp/libsharp.c
68+
$(CL) -shared python/libsharp/libsharp.o -L$(LIBDIR) -lsharp -lfftpack -lc_utils `python-config --libs` -o $@
7069

70+
pytest: python/libsharp/libsharp.so
71+
cd python && nosetests --nocapture libsharp/tests/test_sht.py

libsharp/sharp.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,4 +992,29 @@ int sharp_nv_oracle (sharp_jobtype type, int spin, int ntrans)
992992

993993
#ifdef USE_MPI
994994
#include "sharp_mpi.c"
995+
996+
int sharp_execute_mpi_maybe (void *pcomm, sharp_jobtype type, int spin,
997+
void *alm, void *map, const sharp_geom_info *geom_info,
998+
const sharp_alm_info *alm_info, int ntrans, int flags, double *time,
999+
unsigned long long *opcnt)
1000+
{
1001+
MPI_Comm comm = *(MPI_Comm*)pcomm;
1002+
sharp_execute_mpi((MPI_Comm)comm, type, spin, alm, map, geom_info, alm_info, ntrans,
1003+
flags, time, opcnt);
1004+
return 0;
1005+
}
1006+
1007+
#else
1008+
1009+
int sharp_execute_mpi_maybe (void *pcomm, sharp_jobtype type, int spin,
1010+
void *alm, void *map, const sharp_geom_info *geom_info,
1011+
const sharp_alm_info *alm_info, int ntrans, int flags, double *time,
1012+
unsigned long long *opcnt)
1013+
{
1014+
/* Suppress unused warning: */
1015+
(void)pcomm; (void)type; (void)spin; (void)alm; (void)map; (void)geom_info;
1016+
(void)alm_info; (void)ntrans; (void)flags; (void)time; (void)opcnt;
1017+
return SHARP_ERROR_NO_MPI;
1018+
}
1019+
9951020
#endif

libsharp/sharp_lowlevel.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,30 @@ void sharp_execute (sharp_jobtype type, int spin, void *alm, void *map,
239239
void sharp_set_chunksize_min(int new_chunksize_min);
240240
void sharp_set_nchunks_max(int new_nchunks_max);
241241

242+
243+
typedef enum { SHARP_ERROR_NO_MPI = 1,
244+
/*!< libsharp not compiled with MPI support */
245+
} sharp_errors;
246+
247+
/*! Works like sharp_execute_mpi, but is always present whether or not libsharp
248+
is compiled with USE_MPI. This is primarily useful for wrapper code etc.
249+
250+
Note that \a pcomm has the type MPI_Comm*, except we declare void* to avoid
251+
pulling in MPI headers. I.e., the comm argument of sharp_execute_mpi
252+
is *(MPI_Comm*)pcomm.
253+
254+
Other parameters are the same as sharp_execute_mpi.
255+
256+
Returns 0 if successful, or SHARP_ERROR_NO_MPI if MPI is not available
257+
(in which case nothing is done).
258+
*/
259+
int sharp_execute_mpi_maybe (void *pcomm, sharp_jobtype type, int spin,
260+
void *alm, void *map, const sharp_geom_info *geom_info,
261+
const sharp_alm_info *alm_info, int ntrans, int flags, double *time,
262+
unsigned long long *opcnt);
263+
264+
265+
242266
/*! \} */
243267

244268
#ifdef __cplusplus

python/libsharp/libsharp.pxd

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
cdef extern from "sharp.h":
2+
ctypedef long ptrdiff_t
3+
4+
void sharp_legendre_transform_s(float *bl, float *recfac, ptrdiff_t lmax, float *x,
5+
float *out, ptrdiff_t nx)
6+
void sharp_legendre_transform(double *bl, double *recfac, ptrdiff_t lmax, double *x,
7+
double *out, ptrdiff_t nx)
8+
void sharp_legendre_transform_recfac(double *r, ptrdiff_t lmax)
9+
void sharp_legendre_transform_recfac_s(float *r, ptrdiff_t lmax)
10+
void sharp_legendre_roots(int n, double *x, double *w)
11+
12+
# sharp_lowlevel.h
13+
ctypedef struct sharp_alm_info:
14+
pass
15+
16+
ctypedef struct sharp_geom_info:
17+
pass
18+
19+
void sharp_make_alm_info (int lmax, int mmax, int stride,
20+
ptrdiff_t *mvstart, sharp_alm_info **alm_info)
21+
22+
void sharp_make_geom_info (int nrings, int *nph, ptrdiff_t *ofs,
23+
int *stride, double *phi0, double *theta,
24+
double *wgt, sharp_geom_info **geom_info)
25+
26+
void sharp_destroy_alm_info(sharp_alm_info *info)
27+
void sharp_destroy_geom_info(sharp_geom_info *info)
28+
29+
ptrdiff_t sharp_map_size(sharp_geom_info *info)
30+
ptrdiff_t sharp_alm_count(sharp_alm_info *self);
31+
32+
33+
ctypedef enum sharp_jobtype:
34+
SHARP_YtW
35+
SHARP_Yt
36+
SHARP_WY
37+
SHARP_Y
38+
39+
ctypedef enum:
40+
SHARP_DP
41+
SHARP_ADD
42+
43+
void sharp_execute(sharp_jobtype type_,
44+
int spin,
45+
void *alm,
46+
void *map,
47+
sharp_geom_info *geom_info,
48+
sharp_alm_info *alm_info,
49+
int ntrans,
50+
int flags,
51+
double *time,
52+
unsigned long long *opcnt) nogil
53+
54+
ctypedef enum:
55+
SHARP_ERROR_NO_MPI
56+
57+
int sharp_execute_mpi_maybe (void *pcomm, sharp_jobtype type, int spin,
58+
void *alm, void *map, sharp_geom_info *geom_info,
59+
sharp_alm_info *alm_info, int ntrans, int flags, double *time,
60+
unsigned long long *opcnt) nogil
61+
62+
63+
cdef extern from "sharp_geomhelpers.h":
64+
void sharp_make_subset_healpix_geom_info(
65+
int nside, int stride, int nrings,
66+
int *rings, double *weight, sharp_geom_info **geom_info)
67+
void sharp_make_gauss_geom_info(
68+
int nrings, int nphi, double phi0,
69+
int stride_lon, int stride_lat, sharp_geom_info **geom_info)
70+
71+
cdef extern from "sharp_almhelpers.h":
72+
void sharp_make_triangular_alm_info (int lmax, int mmax, int stride,
73+
sharp_alm_info **alm_info)
74+
void sharp_make_rectangular_alm_info (int lmax, int mmax, int stride,
75+
sharp_alm_info **alm_info)
76+
void sharp_make_mmajor_real_packed_alm_info (int lmax, int stride,
77+
int nm, const int *ms, sharp_alm_info **alm_info)
78+
79+

python/libsharp/libsharp.pyx

Lines changed: 198 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
11
import numpy as np
22

3-
__all__ = ['legendre_transform', 'legendre_roots']
4-
5-
cdef extern from "sharp.h":
6-
ctypedef long ptrdiff_t
7-
8-
void sharp_legendre_transform_s(float *bl, float *recfac, ptrdiff_t lmax, float *x,
9-
float *out, ptrdiff_t nx)
10-
void sharp_legendre_transform(double *bl, double *recfac, ptrdiff_t lmax, double *x,
11-
double *out, ptrdiff_t nx)
12-
void sharp_legendre_transform_recfac(double *r, ptrdiff_t lmax)
13-
void sharp_legendre_transform_recfac_s(float *r, ptrdiff_t lmax)
14-
void sharp_legendre_roots(int n, double *x, double *w)
3+
__all__ = ['legendre_transform', 'legendre_roots', 'sht', 'synthesis', 'adjoint_synthesis',
4+
'analysis', 'adjoint_analysis', 'healpix_grid', 'triangular_order', 'rectangular_order',
5+
'packed_real_order']
156

167

178
def legendre_transform(x, bl, out=None):
@@ -54,3 +45,198 @@ def legendre_roots(n):
5445
if n > 0:
5546
sharp_legendre_roots(n, &x_buf[0], &w_buf[0])
5647
return x, w
48+
49+
50+
JOBTYPE_TO_CONST = {
51+
'Y': SHARP_Y,
52+
'Yt': SHARP_Yt,
53+
'WY': SHARP_WY,
54+
'YtW': SHARP_YtW
55+
}
56+
57+
58+
def sht(jobtype, geom_info ginfo, alm_info ainfo, double[::1] input,
59+
int spin=0, comm=None, add=False):
60+
cdef void *comm_ptr
61+
cdef int flags = SHARP_DP | (SHARP_ADD if add else 0)
62+
cdef double *palm
63+
cdef double *pmap
64+
cdef int r
65+
cdef sharp_jobtype jobtype_i
66+
cdef double[::1] output_buf
67+
68+
try:
69+
jobtype_i = JOBTYPE_TO_CONST[jobtype]
70+
except KeyError:
71+
raise ValueError('jobtype must be one of: %s' % ', '.join(sorted(JOBTYPE_TO_CONST.keys())))
72+
73+
if jobtype_i == SHARP_Y or jobtype_i == SHARP_WY:
74+
output = np.empty(ginfo.local_size(), dtype=np.float64)
75+
output_buf = output
76+
pmap = &output_buf[0]
77+
palm = &input[0]
78+
else:
79+
output = np.empty(ainfo.local_size(), dtype=np.float64)
80+
output_buf = output
81+
pmap = &input[0]
82+
palm = &output_buf[0]
83+
84+
if comm is None:
85+
with nogil:
86+
sharp_execute (
87+
jobtype_i,
88+
geom_info=ginfo.ginfo, alm_info=ainfo.ainfo,
89+
spin=spin, alm=&palm, map=&pmap,
90+
ntrans=1, flags=flags, time=NULL, opcnt=NULL)
91+
else:
92+
from mpi4py import MPI
93+
if not isinstance(comm, MPI.Comm):
94+
raise TypeError('comm must be an mpi4py communicator')
95+
comm_ptr = <void*><size_t>MPI._addressof(comm)
96+
with nogil:
97+
r = sharp_execute_mpi_maybe (
98+
comm_ptr, jobtype_i,
99+
geom_info=ginfo.ginfo, alm_info=ainfo.ainfo,
100+
spin=spin, alm=&palm, map=&pmap,
101+
ntrans=1, flags=flags, time=NULL, opcnt=NULL)
102+
if r == SHARP_ERROR_NO_MPI:
103+
raise Exception('MPI requested, but not available')
104+
105+
return output
106+
107+
108+
def synthesis(*args, **kw):
109+
return sht('Y', *args, **kw)
110+
111+
def adjoint_synthesis(*args, **kw):
112+
return sht('Yt', *args, **kw)
113+
114+
def analysis(*args, **kw):
115+
return sht('YtW', *args, **kw)
116+
117+
def adjoint_analysis(*args, **kw):
118+
return sht('WY', *args, **kw)
119+
120+
121+
#
122+
# geom_info
123+
#
124+
class NotInitializedError(Exception):
125+
pass
126+
127+
128+
cdef class geom_info:
129+
cdef sharp_geom_info *ginfo
130+
131+
def __cinit__(self, *args, **kw):
132+
self.ginfo = NULL
133+
134+
def local_size(self):
135+
if self.ginfo == NULL:
136+
raise NotInitializedError()
137+
return sharp_map_size(self.ginfo)
138+
139+
def __dealloc__(self):
140+
if self.ginfo != NULL:
141+
sharp_destroy_geom_info(self.ginfo)
142+
self.ginfo = NULL
143+
144+
145+
cdef class healpix_grid(geom_info):
146+
147+
_weight_cache = {} # { (nside, 'T'/'Q'/'U') -> numpy array of ring weights cached from file }
148+
149+
def __init__(self, int nside, stride=1, int[::1] rings=None, double[::1] weights=None):
150+
if weights is not None and weights.shape[0] != 2 * nside:
151+
raise ValueError('weights must have length 2 * nside')
152+
sharp_make_subset_healpix_geom_info(nside, stride,
153+
nrings=4 * nside - 1 if rings is None else rings.shape[0],
154+
rings=NULL if rings is None else &rings[0],
155+
weight=NULL if weights is None else &weights[0],
156+
geom_info=&self.ginfo)
157+
158+
@classmethod
159+
def load_ring_weights(cls, nside, fields):
160+
"""
161+
Loads HEALPix ring weights from file. The environment variable
162+
HEALPIX should be set, and this routine will look in the `data`
163+
subdirectory.
164+
165+
Parameters
166+
----------
167+
168+
nside: int
169+
HEALPix nside parameter
170+
171+
fields: tuple of str
172+
Which weights to extract; pass ('T',) to only get scalar
173+
weights back, or ('T', 'Q', 'U') to get all the weights
174+
175+
Returns
176+
-------
177+
178+
List of NumPy arrays, according to fields parameter.
179+
180+
"""
181+
import os
182+
from astropy.io import fits
183+
data_path = os.path.join(os.environ['HEALPIX'], 'data')
184+
fits_field_names = {
185+
'T': 'TEMPERATURE WEIGHTS',
186+
'Q': 'Q-POLARISATION WEIGHTS',
187+
'U': 'U-POLARISATION WEIGHTS'}
188+
189+
must_load = [field for field in fields if (nside, field) not in cls._weight_cache]
190+
191+
if must_load:
192+
hdulist = fits.open(os.path.join(data_path, 'weight_ring_n%05d.fits' % nside))
193+
try:
194+
for field in must_load:
195+
w = hdulist[1].data.field(fits_field_names[field]).ravel().astype(np.double)
196+
w += 1
197+
cls._weight_cache[nside, field] = w
198+
finally:
199+
hdulist.close()
200+
return [cls._weight_cache[(nside, field)].copy() for field in fields]
201+
202+
#
203+
# alm_info
204+
#
205+
206+
207+
cdef class alm_info:
208+
cdef sharp_alm_info *ainfo
209+
210+
def __cinit__(self, *args, **kw):
211+
self.ainfo = NULL
212+
213+
def local_size(self):
214+
if self.ainfo == NULL:
215+
raise NotInitializedError()
216+
return sharp_alm_count(self.ainfo)
217+
218+
def __dealloc__(self):
219+
if self.ainfo != NULL:
220+
sharp_destroy_alm_info(self.ainfo)
221+
self.ainfo = NULL
222+
223+
224+
cdef class triangular_order(alm_info):
225+
def __init__(self, int lmax, mmax=None, stride=1):
226+
mmax = mmax if mmax is not None else lmax
227+
sharp_make_triangular_alm_info(lmax, mmax, stride, &self.ainfo)
228+
229+
230+
cdef class rectangular_order(alm_info):
231+
def __init__(self, int lmax, mmax=None, stride=1):
232+
mmax = mmax if mmax is not None else lmax
233+
sharp_make_rectangular_alm_info(lmax, mmax, stride, &self.ainfo)
234+
235+
236+
cdef class packed_real_order(alm_info):
237+
def __init__(self, int lmax, stride=1, int[::1] ms=None):
238+
sharp_make_mmajor_real_packed_alm_info(lmax=lmax, stride=stride,
239+
nm=lmax + 1 if ms is None else ms.shape[0],
240+
ms=NULL if ms is None else &ms[0],
241+
alm_info=&self.ainfo)
242+

0 commit comments

Comments
 (0)