A simple package for parallel computing with Python
Project description
pckit
This is a simple package for parallel computing with Python.
Usage
Multiprocessing
If you want to use any solver from the package you have to wrap your functions into a model.
Here the example with square of 2 and 3 are evaluated by 2 workers.
MyModel
is a subclass of the package Model
. The method results
is required.
import pckit
class MyModel(pckit.Model):
def results(self, x: int) -> int:
# Solve here problem f(x) = x^2
return x ** 2
if __name__ == '__main__':
model = MyModel()
worker = pckit.MultiprocessingWorker(model)
with pckit.get_solver(worker, workers_num=2) as solver:
# Create tasks to solve
tasks = [2, 3]
results = solver.solve(tasks)
print(results)
# >>> [4, 9]
Workers number can be controlled by workers_num
argument.
MPI
You can easily run scripts on the cluster with mpi4py implementation on MPI (See mpi4py installation docs).
Simply change MultiprocessingWorker
to MPIWorker
in the previous example and start the script with MPI mpiexec -np <n> python -m mpi4py your_script.py
, where <n>
is a number of workers.
worker = pckit.MPIWorker(model)
By default, zero rank process is also used as a worker.
It can be controlled by zero_rank_usage
argument of get_solver
function.
Single thread
Single threaded execution is also available with Worker
worker = pckit.Worker(model)
Examples
Features
Cache
Dict based cache is available by caching
argument in get_solver()
.
Tasks are required to be hashable.
with pckit.get_solver(worker, caching=True) as solver:
tasks = [2, 2]
The second task's solution will be reused from the cache.
You can create your own cache by implementing __contains__
, __getitem__
, __setitem__
of the BaseCache
class.
See example for more details.
Custom iterators
You can send emails or print anything during evaluation with custom iterator. tqdm is also supported.
import tqdm
results = solver.solve(tasks, iterator=tqdm.tqdm)
See example to create your own iterator.
Logging with MPI
See example
Comsol Models, Solvers, Workers
Based on MPh package.
TBDocumented
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pckit-0.5.3.tar.gz
.
File metadata
- Download URL: pckit-0.5.3.tar.gz
- Upload date:
- Size: 11.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.9.18 Linux/6.5.0-1016-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4b24bb18961c6cf5857e7b255da7427f4abb19d76c112c8a373ccd271b369b50 |
|
MD5 | 706ee45190e87d1d77d30ec353fd50c1 |
|
BLAKE2b-256 | bdca650b81a9be7f184bd62df97857728718d61033007a5ae18bd1da3eac661e |
File details
Details for the file pckit-0.5.3-py3-none-any.whl
.
File metadata
- Download URL: pckit-0.5.3-py3-none-any.whl
- Upload date:
- Size: 14.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.9.18 Linux/6.5.0-1016-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 74b72860bd24c5bdaa19111432bb2edccfde0a954a6cf29a7c0e48d9637270e1 |
|
MD5 | 34e4bf3ab84816c2e299c63e828684db |
|
BLAKE2b-256 | f3ab3e8354603ba4930fe2ffff91bd48816bdbbefaad1f79a7b6569dc59f57ef |