|
| 1 | +import os |
| 2 | +import importlib |
| 3 | +import unittest |
| 4 | +import shutil |
| 5 | + |
| 6 | +from executorlib import SlurmClusterExecutor |
| 7 | +from executorlib.standalone.serialize import cloudpickle_register |
| 8 | + |
| 9 | +if shutil.which("srun") is not None: |
| 10 | + skip_slurm_test = False |
| 11 | +else: |
| 12 | + skip_slurm_test = True |
| 13 | + |
| 14 | +skip_mpi4py_test = importlib.util.find_spec("mpi4py") is None |
| 15 | + |
| 16 | +try: |
| 17 | + from executorlib.task_scheduler.file.hdf import dump |
| 18 | + |
| 19 | + skip_h5py_test = False |
| 20 | +except ImportError: |
| 21 | + skip_h5py_test = True |
| 22 | + |
| 23 | +submission_template = """\ |
| 24 | +#!/bin/bash |
| 25 | +#SBATCH --output=time.out |
| 26 | +#SBATCH --job-name={{job_name}} |
| 27 | +#SBATCH --chdir={{working_directory}} |
| 28 | +#SBATCH --get-user-env=L |
| 29 | +#SBATCH --cpus-per-task={{cores}} |
| 30 | +
|
| 31 | +{{command}} |
| 32 | +""" |
| 33 | + |
| 34 | + |
| 35 | +def mpi_funct(i): |
| 36 | + from mpi4py import MPI |
| 37 | + |
| 38 | + size = MPI.COMM_WORLD.Get_size() |
| 39 | + rank = MPI.COMM_WORLD.Get_rank() |
| 40 | + return i, size, rank |
| 41 | + |
| 42 | + |
| 43 | +@unittest.skipIf( |
| 44 | + skip_slurm_test or skip_mpi4py_test or skip_h5py_test, |
| 45 | + "h5py or mpi4py or SLRUM are not installed, so the h5py, slurm and mpi4py tests are skipped.", |
| 46 | +) |
| 47 | +class TestCacheExecutorPysqa(unittest.TestCase): |
| 48 | + def test_executor(self): |
| 49 | + with SlurmClusterExecutor( |
| 50 | + resource_dict={"cores": 2, "cwd": "executorlib_cache", "submission_template": submission_template}, |
| 51 | + block_allocation=False, |
| 52 | + cache_directory="executorlib_cache", |
| 53 | + terminate_tasks_on_shutdown=False, |
| 54 | + ) as exe: |
| 55 | + cloudpickle_register(ind=1) |
| 56 | + fs1 = exe.submit(mpi_funct, 1) |
| 57 | + self.assertFalse(fs1.done()) |
| 58 | + self.assertEqual(fs1.result(), [(1, 2, 0), (1, 2, 1)]) |
| 59 | + self.assertEqual(len(os.listdir("executorlib_cache")), 3) |
| 60 | + self.assertTrue(fs1.done()) |
| 61 | + |
| 62 | + def test_executor_no_cwd(self): |
| 63 | + with SlurmClusterExecutor( |
| 64 | + resource_dict={"cores": 2, "submission_template": submission_template}, |
| 65 | + block_allocation=False, |
| 66 | + cache_directory="executorlib_cache", |
| 67 | + terminate_tasks_on_shutdown=True, |
| 68 | + ) as exe: |
| 69 | + cloudpickle_register(ind=1) |
| 70 | + fs1 = exe.submit(mpi_funct, 1) |
| 71 | + self.assertFalse(fs1.done()) |
| 72 | + self.assertEqual(fs1.result(), [(1, 2, 0), (1, 2, 1)]) |
| 73 | + self.assertEqual(len(os.listdir("executorlib_cache")), 2) |
| 74 | + self.assertTrue(fs1.done()) |
| 75 | + |
| 76 | + def test_executor_existing_files(self): |
| 77 | + with SlurmClusterExecutor( |
| 78 | + resource_dict={"cores": 2, "cwd": "executorlib_cache", "submission_template": submission_template}, |
| 79 | + block_allocation=False, |
| 80 | + cache_directory="executorlib_cache", |
| 81 | + ) as exe: |
| 82 | + cloudpickle_register(ind=1) |
| 83 | + fs1 = exe.submit(mpi_funct, 1) |
| 84 | + self.assertFalse(fs1.done()) |
| 85 | + self.assertEqual(fs1.result(), [(1, 2, 0), (1, 2, 1)]) |
| 86 | + self.assertTrue(fs1.done()) |
| 87 | + self.assertEqual(len(os.listdir("executorlib_cache")), 3) |
| 88 | + for file_name in os.listdir("executorlib_cache"): |
| 89 | + file_path = os.path.join("executorlib_cache", file_name ) |
| 90 | + os.remove(file_path) |
| 91 | + if ".h5" in file_path: |
| 92 | + task_key = file_path[:-5] + "_i.h5" |
| 93 | + dump(file_name=task_key, data_dict={"a": 1}) |
| 94 | + |
| 95 | + with SlurmClusterExecutor( |
| 96 | + resource_dict={"cores": 2, "cwd": "executorlib_cache", "submission_template": submission_template}, |
| 97 | + block_allocation=False, |
| 98 | + cache_directory="executorlib_cache", |
| 99 | + ) as exe: |
| 100 | + cloudpickle_register(ind=1) |
| 101 | + fs1 = exe.submit(mpi_funct, 1) |
| 102 | + self.assertFalse(fs1.done()) |
| 103 | + self.assertEqual(fs1.result(), [(1, 2, 0), (1, 2, 1)]) |
| 104 | + self.assertTrue(fs1.done()) |
| 105 | + self.assertEqual(len(os.listdir("executorlib_cache")), 3) |
| 106 | + |
| 107 | + def tearDown(self): |
| 108 | + shutil.rmtree("executorlib_cache", ignore_errors=True) |
0 commit comments