Skip to content
Snippets Groups Projects
Commit a6b60b34 authored by Guilhem Saurel's avatar Guilhem Saurel
Browse files

cp pfc-2 pfc-2g

parent f1bf0e57
No related branches found
No related tags found
No related merge requests found
# pfcalcul 2
extend pfc-1 with a venv
You'll need:
- understanding of pfc-1
- a copy of this example in `/pfcalcul/work/$USER/pfc-2`
- create a venv inside and instadd dependencies, eg.:
```bash
cd /pfcalcul/work/$USER/pfc-2
python3 -m venv venv
source venv/bin/activate
python3 -m pip install --upgrade pip
python3 -m pip install -r requirements.txt
```
Then, after starting a Manager and a Boss as in pfc-1, you should be good for `sbatch ./schedule.sh`
#!/usr/bin/env python3
import random
from manager import QueueClient
class Boss(QueueClient):
def run(self):
for _ in range(100):
task = random.randint(5, 1e4)
print("new task:", task)
self.queue.put(task)
if __name__ == "__main__":
Boss().run()
#!/usr/bin/env python3
import multiprocessing
import os
from multiprocessing.managers import BaseManager
PORT = 7481
KEY = b"AiZa5Uavcoh3PiajvaeTee5z" # keep it secret, keep it safe !
class QueueManager(BaseManager):
"""This Manager holds a Queue and waits for clients to use it."""
pass
class QueueClient:
"""Base class for users of the Queue."""
def __init__(self):
QueueManager.register("get_queue")
manager = QueueManager(
address=(os.environ.get("HOST", "localhost"), PORT), authkey=KEY
)
manager.connect()
self.queue = manager.get_queue()
if __name__ == "__main__":
queue = multiprocessing.Queue()
QueueManager.register("get_queue", callable=lambda: queue)
QueueManager(address=("", PORT), authkey=KEY).get_server().serve_forever()
#!/usr/bin/env python3
"""
minion intended to be launched by slurm
"""
import argparse
import queue
import time
from manager import QueueClient
from work import find_prime
class Minion(QueueClient):
def __init__(self, n_tasks: int):
self.n_tasks = n_tasks
super().__init__()
def run(self):
for n in range(self.n_tasks):
try:
task = self.queue.get(block=False)
except queue.Empty:
print("All work is already done !")
break
start = time.perf_counter()
print(f"start work {n + 1}/{self.n_tasks} on task {task}...")
result, norm = find_prime(task)
perf = time.perf_counter() - start
print(f"The {task}-th prime number is {result}")
print(f"Its associated quaternion norm is {norm}")
print(f"found in {perf:.3f}s")
print("done !")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("n_tasks", type=int, default=1, nargs="?")
Minion(**vars(parser.parse_args())).run()
pin
#!/bin/bash
#SBATCH --ntasks=1
#SBATCH --array=0-10
#SBATCH --mail-type=ALL
#SBATCH --output=output-%A-%a-%N.log
#SBATCH --error=output-%A-%a-%N.err
source venv/bin/activate
HOST=pfcalcul.laas.fr ./minion.py 10
#!/usr/bin/env python3
"""Hard work here. Not efficient, but hard."""
from typing import Tuple
import pinocchio as pin # type: ignore
def is_prime(number: int) -> bool:
"""Check if a number is prime."""
return not any(number % i == 0 for i in range(2, number))
def find_prime(goal: int = 10, verbose: bool = False) -> Tuple[int, int]:
"""Find the goal-th prime number."""
found = 0
i = 1 # let's enumerate all numbers
while found < goal: # until we have enough prime ones.
i += 1
if is_prime(i):
found += 1
if verbose:
print(f"the prime number n°{found} is {i}")
n = pin.Quaternion(i, i, i, i).norm()
print(f"the norm of a quaternion with {i} as all members is {n}")
return i, n
if __name__ == "__main__":
print(find_prime(50, verbose=True))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment