This repo contains a suite of codes to calculate correlation functions and other clustering statistics for simulated galaxies in a cosmological box (co-moving XYZ) and on observed galaxies with on-sky positions (RA, DEC, CZ). Read the documentation on corrfunc.rtfd.io.
- Fast Theory pair-counting is 7x faster than
SciPy cKDTree, and at least 2x faster than all existing public codes. - OpenMP Parallel All pair-counting codes can be done in parallel (with strong scaling efficiency >~ 95% up to 10 cores)
- Python Extensions Python extensions allow you to do the compute-heavy bits using C while retaining all of the user-friendliness of python.
- Weights All correlation functions now support arbitrary, user-specified weights for individual points
- Modular The code is written in a modular fashion and is easily extensible to compute arbitrary clustering statistics.
- Future-proof As we get access to newer instruction-sets, the codes will get updated to use the latest and greatest CPU features.
If you use the codes for your analysis, please star this repo -- that helps us keep track of the number of users.
Please see this gist for some benchmarks with current codes. If you have a pair-counter that you would like to compare, please add in a corresponding function and update the timings.
make >= 3.80- OpenMP capable compiler like
icc,gcc>=4.6orclang >= 3.7. If not available, please disableUSE_OMPoption option intheory.optionsandmocks.options. You might need to ask your sys-admin for system-wide installs of the compiler; if you prefer to install your own thenconda install gcc(MAC/linux) or(sudo) port install gcc5(on MAC) should work. gsl >= 2.4. Use eitherconda install -c conda-forge gsl(MAC/linux) or(sudo) port install gsl(MAC) to installgslif necessary.python >= 2.7orpython>=3.4for compiling the C extensions.numpy>=1.7for compiling the C extensions.
$ git clone https://github.com/manodeep/Corrfunc/ $ make $ make install $ python -m pip install . (--user) $ make tests
Assuming you have gcc in your PATH, make and
make install should compile and install the C libraries + python
extensions within the source directory. If you would like to install the
python C extensions in your environment, then
python -m pip install . (--user) should be sufficient. If you are primarily
interested in the python interface, you can condense all of the steps
by using python -m pip install . CC=yourcompiler (--user) after git clone.
- If python and/or numpy are not available, then the C extensions will not be compiled.
make installsimply copies files into thelib/bin/includesub-directories. You do not needrootpermissions- Default compiler on MAC is set to
clang, if you want to specify a different compiler, you will have to callmake CC=yourcompiler,make install CC=yourcompiler,make tests CC=yourcompileretc. If you want to permanently change the default compiler, then please edit the common.mk file in the base directory. - If you are directly using
python -m pip install . CC=yourcompiler (--user), please run amake distcleanbeforehand (especially if switching compilers)
The python package is directly installable via pip install Corrfunc. However, in that case you will lose the ability to recompile the code according to your needs. Installing via pip is not recommended, please open an install issue on this repo first; doing so helps improve the code-base and saves future users from running into similar install issues.
Automatically detecting OpenMP support from the compiler and the runtime is a bit tricky. If you run into any issues compiling (or running) with OpenMP, please refer to the FAQ for potential solutions.
If compilation went smoothly, please run make tests to ensure the
code is working correctly. Depending on the hardware and compilation
options, the tests might take more than a few minutes. Note that the
tests are exhaustive and not traditional unit tests.
While I have tried to ensure that the package compiles and runs out of
the box, cross-platform compatibility turns out to be incredibly hard.
If you run into any issues during compilation and you have all of the
pre-requisites, please see the FAQ or email
the Corrfunc mailing list. Also, feel free to create a new issue
with the Installation label.
The input galaxies (or any discrete distribution of points) are derived from a simulation. For instance, the galaxies could be a result of an Halo Occupation Distribution (HOD) model, a Subhalo Abundance matching (SHAM) model, a Semi-Empirical model (SEM), or a Semi-Analytic model (SAM) etc. The input set of points can also be the dark matter halos, or the dark matter particles from a cosmological simulation. The input set of points are expected to have positions specified in Cartesian XYZ.
All codes that work on cosmological boxes with co-moving positions are
located in the theory directory. The various clustering measures
are:
DD-- Measures auto/cross-correlations between two boxes. The boxes do not need to be cubes.xi-- Measures 3-d auto-correlation in a cubic cosmological box. Assumes PERIODIC boundary conditions.wp-- Measures auto 2-d point projected correlation function in a cubic cosmological box. Assumes PERIODIC boundary conditions.DDrppi-- Measures the auto/cross correlation function between two boxes. The boxes do not need to be cubes.DDsmu-- Measures the auto/cross correlation function between two boxes. The boxes do not need to be cubes.vpf-- Measures the void probability function + counts-in-cells.
The input galaxies are typically observed galaxies coming from a large-scale galaxy survey. In addition, simulated galaxies that have been projected onto the sky (i.e., where observational systematics have been incorporated and on-sky positions have been generated) can also be used. We generically refer to both these kinds of galaxies as "mocks".
The input galaxies are expected to have positions specified in spherical co-ordinates with at least right ascension (RA) and declination (DEC). For spatial correlation functions, an approximate "co-moving" distance (speed of light multiplied by redshift, CZ) is also required.
All codes that work on mock catalogs (RA, DEC, CZ) are located in the
mocks directory. The various clustering measures are:
DDrppi_mocks-- The standard auto/cross correlation between two data sets. The outputs, DD, DR and RR can be combined usingwprpto produce the Landy-Szalay estimator for wp(rp).DDsmu_mocks-- The standard auto/cross correlation between two data sets. The outputs, DD, DR and RR can be combined using the python utilityconvert_3d_counts_to_cfto produce the Landy-Szalay estimator for xi(s, mu).DDtheta_mocks-- Computes angular correlation function between two data sets. The outputs fromDDtheta_mocksneed to be combined withwthetato get the full omega(theta)vpf_mocks-- Computes the void probability function on mocks.
If you plan to use the command-line, then you will have to specify the code runtime options at compile-time. For theory routines, these options are in the file theory.options while for the mocks, these options are in file mocks.options.
Note All options can be specified at
runtime if you use the python interface or the static libraries. Each one of
the following Makefile option has a corresponding entry for the runtime
libraries.
Theory (in theory.options)
PERIODIC(ignored in case of wp/xi) -- switches periodic boundary conditions on/off. Enabled by default.OUTPUT_RPAVG-- switches on output of<rp>in eachrpbin. Can be a massive performance hit (~ 2.2x in case of wp). Disabled by default.
Mocks (in mocks.options)
OUTPUT_RPAVG-- switches on output of<rp>in eachrpbin forDDrppi_mocks. Enabled by default.OUTPUT_THETAAVG-- switches on output of in each theta bin. Can be extremely slow (~5x) depending on compiler, and CPU capabilities. Disabled by default.LINK_IN_DEC-- creates binning in declination forDDtheta_mocks. Please check that for your desired limits\theta, this binning does not produce incorrect results (due to numerical precision). Generally speaking, if your\thetamax(the max.\thetato consider pairs within) is too small (probaly less than 1 degree), then you should check with and without this option. Errors are typically sub-percent level.LINK_IN_RA-- creates binning in RA once binning in DEC has been enabled forDDtheta_mocks. Same numerical issues asLINK_IN_DECFAST_ACOS-- Relevant only whenOUTPUT_THETAAVGis enabled forDDtheta_mocks. Disabled by default. Anarccosis required to calculate<\theta>. In absence of vectorizedarccos(intel compiler,iccprovides one via intel Short Vector Math Library), this calculation is extremely slow. However, we can approximatearccosusing polynomials (with Remez Algorithm). The approximations are taken from implementations released by Geometric Tools. Depending on the level of accuracy desired, this implementation offast acoscan be tweaked in the file utils/fast_acos.h. An alternate, less accurate implementation is already present in that file. Please check that the loss of precision is not important for your use-case.COMOVING_DIST-- Currently there is no support inCorrfuncfor different cosmologies. However, for the mocks routines like,DDrppi_mocksandvpf_mocks, cosmology parameters are required to convert between redshift and co-moving distance. BothDDrppi_mocksandvpf_mocksexpects to receive aredshiftarray as input; however, with this option enabled, theredshiftarray will be assumed to contain already converted co-moving distances. So, if you have redshifts and want to use an arbitrary cosmology, then convert the redshifts into co-moving distances, enable this option, and pass the co-moving distance array into the routines.
DOUBLE_PREC-- switches on calculations in double precision. Calculations are performed in double precision when enabled. This option is disabled by default in theory and enabled by default in the mocks routines.USE_OMP-- uses OpenMP parallelization. Scaling is great for DD (close to perfect scaling up to 12 threads in our tests) and okay (runtime becomes constant ~6-8 threads in our tests) forDDrppiandwp. Enabled by default. TheMakefilewill compare the CC variable with known OpenMP enabled compilers and set compile options accordingly. Set in common.mk by default.ENABLE_MIN_SEP_OPT-- uses some further optimisations based on the minimum separation between pairs of cells. Enabled by default.COPY_PARTICLES-- whether or not to create a copy of the particle positions (and weights, if supplied). Enabled by default (copies of the particle arrays are created)FAST_DIVIDE-- Disabled by default. Divisions are slow but requiredDDrppi_mocks(r_p,\pi),DDsmu_mocks(s, \mu)andDD(s, \mu). Enabling this option, replaces the divisions with a reciprocal followed by a Newton-Raphson. The code will run ~20% faster at the expense of some numerical precision. Please check that the loss of precision is not important for your use-case.
Optimization for your architecture
- The values of
bin_refine_factorand/orzbin_refine_factorin thecountpairs\_\*.cfiles control the cache-misses, and consequently, the runtime. In my trial-and-error methods, I have seen any values larger than 3 are generally slower for theory routines but can be faster for mocks. But some different combination of 1/2 for(z)bin_refine_factormight be faster on your platform. - If you are using the angular correlation function and need
thetaavg, you might benefit from using the INTEL MKL library. The vectorized trigonometric functions provided by MKL can provide significant speedup.
Read the documentation on corrfunc.rtfd.io.
Navigate to the correct directory. Make sure that the options, set in
either theory.options or mocks.options in the root directory are
what you want. If not, edit those two files (and possibly
common.mk), and recompile. Then, you can use the command-line
executables in each individual subdirectory corresponding to the
clustering measure you are interested in. For example, if you want to
compute the full 3-D correlation function, \xi(r), then run the
executable theory/xi/xi. If you run executables without any arguments,
the program will output a message with all the required arguments.
Look under the run_correlations.c and
run_correlations_mocks.c to see examples of
calling the C API directly. If you run the executables,
run_correlations and run_correlations_mocks, the output will
also show how to call the command-line interface for the various
clustering measures.
If all went well, the codes can be directly called from python.
Please see call_correlation_functions.py and
call_correlation_functions_mocks.py for examples on how to
use the C extensions directly. Here are a few examples:
from __future__ import print_function
import os.path as path
import numpy as np
import Corrfunc
from Corrfunc.theory import wp
# Setup the problem for wp
boxsize = 500.0
pimax = 40.0
nthreads = 4
# Create a fake data-set.
Npts = 100000
x = np.float32(np.random.random(Npts))
y = np.float32(np.random.random(Npts))
z = np.float32(np.random.random(Npts))
x *= boxsize
y *= boxsize
z *= boxsize
# Setup the bins
rmin = 0.1
rmax = 20.0
nbins = 20
# Create the bins
rbins = np.logspace(np.log10(0.1), np.log10(rmax), nbins + 1)
# Call wp
wp_results = wp(boxsize, pimax, nthreads, rbins, x, y, z, verbose=True, output_rpavg=True)
# Print the results
print("#############################################################################")
print("## rmin rmax rpavg wp npairs")
print("#############################################################################")
print(wp_results)Corrfunc was designed by Manodeep Sinha and is currently maintained by Lehman Garrison and Manodeep Sinha
If you use Corrfunc for research, please cite using the MNRAS code paper with the following
bibtex entry:
@ARTICLE{2020MNRAS.491.3022S,
author = {{Sinha}, Manodeep and {Garrison}, Lehman H.},
title = "{CORRFUNC - a suite of blazing fast correlation functions on
the CPU}",
journal = {\mnras},
keywords = {methods: numerical, galaxies: general, galaxies:
haloes, dark matter, large-scale structure of Universe, cosmology:
theory},
year = "2020",
month = "Jan",
volume = {491},
number = {2},
pages = {3022-3041},
doi = {10.1093/mnras/stz3157},
adsurl =
{https://ui.adsabs.harvard.edu/abs/2020MNRAS.491.3022S},
adsnote = {Provided by the SAO/NASA
Astrophysics Data System}
}
If you are using Corrfunc v2.3.0 or later, and you benefit from the
enhanced vectorised kernels, then please additionally cite this paper:
@InProceedings{10.1007/978-981-13-7729-7_1,
author="Sinha, Manodeep and Garrison, Lehman",
editor="Majumdar, Amit and Arora, Ritu",
title="CORRFUNC: Blazing Fast Correlation Functions with AVX512F SIMD Intrinsics",
booktitle="Software Challenges to Exascale Computing",
year="2019",
publisher="Springer Singapore",
address="Singapore",
pages="3--20",
isbn="978-981-13-7729-7",
url={https://doi.org/10.1007/978-981-13-7729-7_1}
}
If you have questions or comments about the package, please do so on the mailing list: https://groups.google.com/forum/#!forum/corrfunc
Corrfunc is released under the MIT license. Basically, do what you want with the code, including using it in commercial application.
- Documentation (http://corrfunc.rtfd.io/)
- Source Repository (https://github.com/manodeep/Corrfunc)
- Entry in the Astrophysical Source Code Library (ASCL)
- Zenodo Releases
