Skip to main content

Python bindings for Monte Carlo eXtreme (OpenCL) photon transport simulator

Project description

PMCX-CL - Python bindings for Monte Carlo eXtreme (OpenCL) photon transport simulator

Linux Python Module
MacOS Python Module
Windows Python Module

This module provides a Python binding for Monte Carlo eXtreme for OpenCL (MCXCL). For other binaries, including the standalone executable and the MATLAB bindings, see our website.

Monte Carlo eXtreme (MCX) is a fast photon transport simulation software for 3D heterogeneous turbid media. By taking advantage of the massively parallel threads and extremely low memory latency in a modern graphics processing unit (GPU), MCX is capable of performing Monte Carlo (MC) photon simulations at a blazing speed, typically hundreds to a thousand times faster than a single-threaded CPU-based MC implementation.

How to Install

Runtime Dependencies

  • CPU or GPU: An OpenCL-capable CPU or GPU; most modern CPUs or GPUs support OpenCL - an industrial-standard heterogeneous computing library and specification (https://www.khronos.org/opencl/)
  • OpenCL CPU or GPU runtime/driver: Both NVIDIA and AMD GPU graphics drivers should contain out-of-box OpenCL runtimes or drivers; for Intel GPUs, one should install additional OpenCL runtime support from https://github.com/intel/compute-runtime or install the intel-opencl-icd package if the OS provides (such as Ubuntu 22.04); one can also install an open-source OpenCL runtime POCL, using package manager such as sudo apt-get install pocl-opencl-icd. However, POCL's support is largely limited to CPUs. You do not need to install CUDA SDK to use pmcxcl.
  • Python: Python 3.6 and newer is required. Python 2 is not supported.
  • numpy: Used to pass/receive volumetric information to/from pmcxcl. To install, use either conda or pip package managers: pip install numpy or conda install numpy
  • (optional) jdata: Only needed to read/write JNIfTI output files. To install, use pip: pip install jdata on all operating systems; For Debian-based Linux distributions, you can also install to the system interpreter using apt-get: sudo apt-get install python3-jdata. See https://pypi.org/project/jdata/ for more details.
  • (optional) bjdata: Only needed to read/write BJData/UBJSON files. To install, run pip install bjdata on all operating systems; For Debian-based Linux distributions, you can also install to the system interpreter using apt-get: sudo apt-get install python3-bjdata. See https://pypi.org/project/bjdata/ for more details.
  • (optional) matplotlib: For plotting the results. To install, run either pip install matplotlib or conda install matplotlib

Build Instructions

Build Dependencies

  • Operating System: pmcxcl and mcxcl can be compiled on most OSes, including Windows, Linux and MacOS.

  • OpenCL library: compiling mcxcl or pmcxcl requires to link with libOpenCL.so on Linux, or libOpenCL.dylib on MacOS or OpenCL.dll on Windows. These libraries should have been installed by either graphics driver or OpenCL runtimes.

  • Python Interpreter: Python 3.6 or above. The pip Python package manager and the wheel package (available via pip) are not required but recommended.

  • C/C++ Compiler: pmcxcl can be compiled using a wide variety of C compilers, including

    • GNU GCC for Linux, MacOS (intalled via MacPorts or brew), and Windows (installed via msys2, mingw64 or cygwin64)
    • Microsoft Visual Studio C/C++ Compiler for Windows.
    • Apple Clang for macOS, available via Xcode.

    Refer to each OS's online documentations for more in-depth information on how to install these compilers. MacOS provides built-in OpenCL library support.

  • OpenMP: The installed C/C++ Compiler should have support for OpenMP. GCC and Microsoft Visual Studio compiler support OpenMP out of the box. Apple Clang, however, requires manual installation of OpenMP libraries for Apple Clang. The easiest way to do this is via the Brew package manager, preferably after selecting the correct Xcode version:

      brew install libomp
      brew link --force libomp
    
  • CMake: CMake version 3.15 and later is required. Refer to the CMake website for more information on how to download. CMake is also widely available on package managers across all operating systems.

Build Steps

  1. Ensure that cmake, python and the C/C++ compiler are all located over your PATH. This can be queried via echo $env:PATH on Windows or echo $PATH on Linux. If not, locate them and add their folder to the PATH.

  2. Clone the repository and switch to the pmcxcl/ folder:

        git clone --recursive https://github.com/fangq/mcx.git
        cd mcx/pmcxcl
    
  3. One can run python3 setup.py install or python3 -m pip install . to both locally build and install the module

  4. If one only wants to locally build the module, one should run python3 -m pip wheel .

  5. If the binary module is successfully built locally, you should see a binary wheel file pmcxcl-X.X.X-cpXX-cpXX-*.whl stored inside the mcxcl/pmcxcl folder. You can install this wheel package using python3 -m pip install --force-reinstall pmcxcl-*.whl to force installing this locally compiled pmcxcl module and overwrite any previously installed versions.

How to use

The PMCXCL module is easy to use. You can use the pmcxcl.gpuinfo() function to first verify if you have NVIDIA/CUDA compatible GPUs installed; if there are NVIDIA GPUs detected, you can then call the run() function to launch a photon simulation.

A simulation can be defined conveniently in two approaches - a one-liner and a two-liner:

  • For the one-liner, one simply pass on each MCX simulation setting as positional argument. The supported setting names are compatible to nearly all the input fields for the MATLAB version of MCX/MCXCL - MCXLAB)
import pmcxcl
import numpy as np
import matplotlib.pyplot as plt

res = pmcxcl.run(nphoton=1000000, vol=np.ones([60, 60, 60], dtype='uint8'), tstart=0, tend=5e-9, 
               tstep=5e-9, srcpos=[30,30,0], srcdir=[0,0,1], prop=np.array([[0, 0, 1, 1], [0.005, 1, 0.01, 1.37]]))
res['flux'].shape

plt.imshow(np.log10(res['flux'][30,:, :]))
plt.show()
  • Alternatively, one can also define a Python dict object containing each setting as a key, and pass on the dict object to pmcxcl.run()
import pmcxcl
import numpy as np
cfg = {'nphoton': 1000000, 'vol':np.ones([60,60,60],dtype='uint8'), 'tstart':0, 'tend':5e-9, 'tstep':5e-9,
       'srcpos': [30,30,0], 'srcdir':[0,0,1], 'prop':[[0,0,1,1],[0.005,1,0.01,1.37]]}
res = pmcxcl.run(cfg)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

pmcxcl-0.0.12-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

pmcxcl-0.0.12-pp39-pypy39_pp73-win_amd64.whl (876.4 kB view hashes)

Uploaded PyPy Windows x86-64

pmcxcl-0.0.12-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

pmcxcl-0.0.12-pp39-pypy39_pp73-macosx_11_0_x86_64.whl (532.6 kB view hashes)

Uploaded PyPy macOS 11.0+ x86-64

pmcxcl-0.0.12-pp38-pypy38_pp73-win_amd64.whl (877.3 kB view hashes)

Uploaded PyPy Windows x86-64

pmcxcl-0.0.12-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

pmcxcl-0.0.12-pp38-pypy38_pp73-macosx_11_0_x86_64.whl (532.7 kB view hashes)

Uploaded PyPy macOS 11.0+ x86-64

pmcxcl-0.0.12-pp37-pypy37_pp73-win_amd64.whl (877.0 kB view hashes)

Uploaded PyPy Windows x86-64

pmcxcl-0.0.12-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

pmcxcl-0.0.12-pp37-pypy37_pp73-macosx_11_0_x86_64.whl (532.3 kB view hashes)

Uploaded PyPy macOS 11.0+ x86-64

pmcxcl-0.0.12-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

pmcxcl-0.0.12-cp311-cp311-win_amd64.whl (878.6 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

pmcxcl-0.0.12-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

pmcxcl-0.0.12-cp311-cp311-macosx_11_0_universal2.whl (534.4 kB view hashes)

Uploaded CPython 3.11 macOS 11.0+ universal2 (ARM64, x86-64)

pmcxcl-0.0.12-cp310-cp310-win_amd64.whl (877.4 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

pmcxcl-0.0.12-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

pmcxcl-0.0.12-cp310-cp310-macosx_11_0_x86_64.whl (533.2 kB view hashes)

Uploaded CPython 3.10 macOS 11.0+ x86-64

pmcxcl-0.0.12-cp39-cp39-win_amd64.whl (877.3 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

pmcxcl-0.0.12-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

pmcxcl-0.0.12-cp39-cp39-macosx_11_0_x86_64.whl (533.3 kB view hashes)

Uploaded CPython 3.9 macOS 11.0+ x86-64

pmcxcl-0.0.12-cp38-cp38-win_amd64.whl (877.1 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

pmcxcl-0.0.12-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

pmcxcl-0.0.12-cp38-cp38-macosx_11_0_x86_64.whl (533.2 kB view hashes)

Uploaded CPython 3.8 macOS 11.0+ x86-64

pmcxcl-0.0.12-cp37-cp37m-win_amd64.whl (880.9 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

pmcxcl-0.0.12-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

pmcxcl-0.0.12-cp37-cp37m-macosx_11_0_x86_64.whl (531.6 kB view hashes)

Uploaded CPython 3.7m macOS 11.0+ x86-64

pmcxcl-0.0.12-cp36-cp36m-win_amd64.whl (880.8 kB view hashes)

Uploaded CPython 3.6m Windows x86-64

pmcxcl-0.0.12-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ x86-64

pmcxcl-0.0.12-cp36-cp36m-macosx_11_0_x86_64.whl (531.7 kB view hashes)

Uploaded CPython 3.6m macOS 11.0+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page