Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 73 additions & 10 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ on:

permissions:
contents: write
pull-requests: write

jobs:
test:
name: Python ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} - ${{ github.event_name }}
name: Python ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
Expand All @@ -23,14 +24,15 @@ jobs:
os:
- ubuntu-latest
arch:
- x86
- x64
- x86

steps:
- uses: actions/checkout@v2
# Updated to v4
- uses: actions/checkout@v4

- name: Set up Python ${{ matrix.version }}
uses: actions/setup-python@v2
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.version }}

Expand All @@ -40,29 +42,90 @@ jobs:
pip install coverage
pip install pytest pytest-cov
pip install -r requirements.txt

- name: Run tests

- name: Configure Coverage Parallel Mode
run: |
echo '[run]' > .coveragerc
echo 'parallel = True' >> .coveragerc

- name: Run tests (Parallel Data Collection)
run: |
export RANK=0
export LOCAL_RANK=0
export WORLD_SIZE=1
export MASTER_ADDR=localhost
export MASTER_PORT=12345
export PYTHONPATH=MCintegration
pytest --cov --cov-report=xml --ignore=examples

# 使用 coverage run 运行 pytest 以确保多进程/多核测试数据的收集
coverage run -m pytest --ignore=examples

# 【调试步骤】列出所有 .coverage 文件,检查文件是否被正确生成
- name: Debug Coverage Files
run: ls -F .coverage*

# Updated to v4
- name: Upload coverage data artifact
uses: actions/upload-artifact@v4
with:
name: coverage-data-${{ matrix.os }}-${{ matrix.arch }}
# 确保匹配所有以点开头的覆盖率文件和配置文件
path: |
.coverage*
.coveragerc
# 移除了 match-dot-files: true,因为 actions/upload-artifact@v4 不支持此输入

codecov:
name: Codecov Merge & Upload
runs-on: ubuntu-latest
needs: test

steps:
# Updated to v4
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.12'

- name: Install coverage (for combining)
run: pip install coverage

# Updated to v4
- name: Download all coverage data artifacts
uses: actions/download-artifact@v4
with:
path: coverage-artifacts

# 合并所有数据文件并生成最终 XML 报告
- name: Combine and Report
run: |
# 移动所有数据文件到根目录,以便 coverage combine 找到
# find 命令现在可以安全运行,因为 download-artifact 应该已创建目录
find coverage-artifacts -name ".coverage*" -exec mv {} . \;

# 合并所有 .coverage.* 文件,解决跨 Job 和多进程数据丢失问题
coverage combine

# 生成最终的 XML 报告
coverage xml

# 上传最终的合并报告
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}

files: ./coverage.xml

docs:
name: Documentation
runs-on: ubuntu-latest
needs: test
needs: codecov

steps:
- uses: actions/checkout@v2
# Updated to v4
- uses: actions/checkout@v4

- name: Install dependencies
run: |
Expand Down
141 changes: 141 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,144 @@
[![alpha](https://img.shields.io/badge/docs-alpha-blue.svg)](https://numericaleft.github.io/MCintegration.py/)
[![Build Status](https://github.com/numericalEFT/MCIntegration.py/workflows/CI/badge.svg)](https://github.com/numericalEFT/MCIntegration.py/actions)
[![codecov](https://codecov.io/gh/numericalEFT/MCintegration.py/graph/badge.svg?token=851N2CNOTN)](https://codecov.io/gh/numericalEFT/MCintegration.py)
A Python library for Monte Carlo integration with support for multi-CPU and GPU computations.

## Overview

MCintegration is a specialized library designed for numerical integration using Monte Carlo methods. It provides efficient implementations of various integration algorithms with focus on applications in computational physics and effective field theories (EFT).

The library offers:
- Multiple Monte Carlo integration algorithms
- Support for multi-CPU parallelization
- GPU acceleration capabilities
- Integration with PyTorch for tensor-based computations

## Installation

```bash
pip install mcintegration
```

Or install from source:

```bash
python setup.py install
```

## Usage

### Example 1: Unit Circle Integration

This example demonstrates different Monte Carlo methods for integrating functions over [-1,1]×[-1,1]:

```python
from MCintegration import MonteCarlo, MarkovChainMonteCarlo, Vegas
import torch

# Define integrand function
def unit_circle(x, f):
r2 = x[:, 0]**2 + x[:, 1]**2
f[:, 0] = (r2 <= 1).float()
return f.mean(dim=-1)

# Set up integration parameters
dim = 2
bounds = [(-1, 1)] * dim
n_eval = 6400000
batch_size = 10000
n_therm = 100

# Create integrator instances
mc = MonteCarlo(f=unit_circle, bounds=bounds, batch_size=batch_size)
mcmc = MarkovChainMonteCarlo(f=unit_circle, bounds=bounds, batch_size=batch_size, nburnin=n_therm)

# Perform integration
result_mc = mc(n_eval)
result_mcmc = mcmc(n_eval)
```

### Example 2: Singular Function Integration

This example shows integration of a function with a singularity at x=0:

```python
# Integrate log(x)/sqrt(x) which has a singularity at x=0
def singular_func(x, f):
f[:, 0] = torch.log(x[:, 0]) / torch.sqrt(x[:, 0])
return f[:, 0]

# Set up integration parameters
dim = 1
bounds = [(0, 1)]
n_eval = 6400000
batch_size = 10000
n_therm = 100

# Use VEGAS algorithm which adapts to the singular structure
vegas_map = Vegas(dim, ninc=1000)
vegas_map.adaptive_training(batch_size, singular_func)

# Create integrator instances using the adapted vegas map
vegas_mc = MonteCarlo(f=singular_func, bounds=bounds, batch_size=batch_size, maps=vegas_map)
vegas_mcmc = MarkovChainMonteCarlo(f=singular_func, bounds=bounds, batch_size=batch_size, nburnin=n_therm, maps=vegas_map)

# Perform integration
result_vegas = vegas_mc(n_eval)
result_vegas_mcmc = vegas_mcmc(n_eval)
```

### Example 3: Multiple Sharp Peak Integrands in Higher Dimensions

This example demonstrates integration of a sharp Gaussian peak and its moments in 4D space:

```python
# Define a sharp peak and its moments integrands
# This represents a Gaussian peak centered at (0.5, 0.5, 0.5, 0.5)
def sharp_integrands(x, f):
f[:, 0] = torch.sum((x - 0.5) ** 2, dim=-1) # Distance from center
f[:, 0] *= -200 # Scale by width parameter
f[:, 0].exp_() # Exponentiate to create Gaussian
f[:, 1] = f[:, 0] * x[:, 0] # First moment
f[:, 2] = f[:, 0] * x[:, 0] ** 2 # Second moment
return f.mean(dim=-1)

# Set up 4D integration with sharp peak
dim = 4
bounds = [(0, 1)] * dim
n_eval = 6400000
batch_size = 10000
n_therm = 100

# Use VEGAS algorithm which adapts to the peak structure
vegas_map = Vegas(dim, ninc=1000)
vegas_map.adaptive_training(batch_size, sharp_integrands, f_dim=3)

# Create integrator instances using the adapted vegas map
vegas_mc = MonteCarlo(f=sharp_integrands, f_dim=3, bounds=bounds, batch_size=batch_size, maps=vegas_map)
vegas_mcmc = MarkovChainMonteCarlo(f=sharp_integrands, f_dim=3, bounds=bounds, batch_size=batch_size, nburnin=n_therm, maps=vegas_map)

# Perform integration
result_vegas = vegas_mc(n_eval)
result_vegas_mcmc = vegas_mcmc(n_eval)
```

## Features

- **Base integration methods**: Core Monte Carlo algorithms in `MCintegration/base.py`
- **Integrator implementations**: Various MC integration strategies in `MCintegration/integrators.py`
- **Variable transformations**: Coordinate mapping utilities in `MCintegration/maps.py`
- **Utility functions**: Helper functions for numerical computations in `MCintegration/utils.py`
- **Multi-CPU support**: Parallel processing capabilities demonstrated in `MCintegration/mc_multicpu_test.py`
- **GPU acceleration**: CUDA-enabled functions through PyTorch in the examples directory


## Requirements

- Python 3.7+
- NumPy
- PyTorch
- gvar

## Acknowledgements and Related Packages
The development of `MCIntegration.py` has been greatly inspired and influenced by `vegas` package. We would like to express our appreciation to the following:
- [vegas](https://github.com/gplepage/vegas) A Python package offering Monte Carlo estimations of multidimensional integrals, with notable improvements on the original Vegas algorithm. It's been a valuable reference for us. Learn more from the vegas [documentation](https://vegas.readthedocs.io/). **Reference: G. P. Lepage, J. Comput. Phys. 27, 192 (1978) and G. P. Lepage, J. Comput. Phys. 439, 110386 (2021) [arXiv:2009.05112](https://arxiv.org/abs/2009.05112)**.
2 changes: 1 addition & 1 deletion codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ coverage:
target: 80%
project:
default:
target: 95%
target: 95%
7 changes: 7 additions & 0 deletions license.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright (c) 2025: Pengcheng Hou, Tao Wang, Caiyu Fan, and Kun Chen.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Loading