def test_for_loop_is_not_run_twice(): """This test checks for an issue where if the number of CPUs exceeded the number of jobs for a task, the entire task could be run twice""" tangos.get_halo(1)['test_count'] = 0 tangos.get_default_session().commit() pt.launch(_test_not_run_twice, 5) assert tangos.get_halo(1)['test_count'] == 3
def test_parallel_writing(fresh_database): parallel_tasks.use('multiprocessing') try: parallel_tasks.launch(run_writer_with_args, 3, ["dummy_property"]) finally: parallel_tasks.use('null') _assert_properties_as_expected()
def test_blocking_avoids_exception(): assert tangos.get_halo("sim/ts1/6") is None db.core.get_default_session().commit() with log.LogCapturer(): pt.launch(_perform_test, 3, (True, )) assert tangos.get_halo("sim/ts1/6") is not None
def test_non_blocking_exception(): with _suppress_exception_report(): with assert_raises(sqlalchemy.exc.OperationalError): with log.LogCapturer(): pt.launch(_perform_test, 3, (False, )) db.core.get_default_session().rollback()
def test_synchronize_db_creator(): pt.launch(_test_synchronize_db_creator, 3) assert tangos.get_halo(1)['db_creator_test_property'] == 1.0 assert tangos.get_halo(2)['db_creator_test_property'] == 1.0 creator_1, creator_2 = ( tangos.get_halo(i).get_objects('db_creator_test_property')[0].creator for i in (1, 2)) assert creator_1 == creator_2
def test_parallel_writing(): init_blank_simulation() parallel_tasks.use('multiprocessing') try: parallel_tasks.launch(run_writer_with_args,3,["dummy_property"]) finally: parallel_tasks.use('null') _assert_properties_as_expected()
def test_add_two_properties_different_ranges(): pt.launch(_add_two_properties_different_ranges, 3) for i in range(1, 10): assert tangos.get_halo(i)['my_test_property_2'] == i if i < 8: assert 'my_test_property_3' in tangos.get_halo(i) assert tangos.get_halo(i)['my_test_property_3'] == i else: assert 'my_test_property_3' not in tangos.get_halo(i)
def test_blocking_avoids_exception(): if testing_db_backend != "sqlite": skip("This test is only relevant for sqlite databases") assert tangos.get_halo("sim/ts1/6") is None db.core.get_default_session().commit() with log.LogCapturer(): pt.launch(_perform_test,3, (True,)) assert tangos.get_halo("sim/ts1/6") is not None
def test_non_blocking_exception(): if testing_db_backend != "sqlite": skip("This test is only relevant for sqlite databases") with _suppress_exception_report(): with assert_raises(sqlalchemy.exc.OperationalError): with log.LogCapturer(): pt.launch(_perform_test,3, (False,)) db.core.get_default_session().rollback()
def test_property_import(): importer = tools.property_importer.PropertyImporter() importer.parse_command_line( "X Y Z Mvir --for test_gadget_rockstar".split()) with log.LogCapturer(): parallel_tasks.use('multiprocessing') parallel_tasks.launch(importer.run_calculation_loop, 2) Mvir_test, = tangos.get_timestep( "test_gadget_rockstar/snapshot_013").calculate_all("Mvir") npt.assert_allclose( Mvir_test, [1.160400e+13, 8.341900e+12, 5.061400e+12, 5.951900e+12])
def test_property_import(): importer = tools.property_importer.PropertyImporter() importer.parse_command_line( "Xc Yc Zc Mvir --for test_ahf_merger_tree".split()) with log.LogCapturer(): parallel_tasks.use('multiprocessing') parallel_tasks.launch(importer.run_calculation_loop, 2) Mvir_test, = tangos.get_timestep( "test_ahf_merger_tree/tiny_000832").calculate_all("Mvir") npt.assert_allclose(Mvir_test, [ 2.34068e+11, 4.94677e+10, 4.58779e+10, 4.24798e+10, 2.31297e+10, 2.1545e+10, 1.85704e+10, 1.62538e+10, 1.28763e+10 ])
def test_writer_handles_sim_properties(fresh_database): """Test for issue where simulation properties could be queried from within a calculation. This could lead to unexpected database locks. Tangos 1.3 provides a safe route to doing this. The test ensures that the results are cached to prevent hammering the database However it does not directly test that the parallel_tasks locking mechanism is called, which is hard. Ideally this test would therefore be completed at some point...""" parallel_tasks.use('multiprocessing') try: parallel_tasks.launch(run_writer_with_args, 3, ["dummy_property_accessing_simulation_property"]) finally: parallel_tasks.use('null') for i in range(1,3): ts = db.get_timestep("dummy_sim_1/step.%d"%i) x, = ts.calculate_all("dummy_property_accessing_simulation_property") npt.assert_equal(x,[1]*ts.halos.count())
def main(): parallel_tasks.launch(run)
def run_importer(argv): from tangos import parallel_tasks, core from tangos.tools.property_importer import PropertyImporter importer = PropertyImporter() importer.parse_command_line(argv) parallel_tasks.launch(importer.run_calculation_loop, 2, [])
def test_empty_loop(): pt.launch(_test_empty_loop, 3)
def test_add_property(): pt.launch(_add_property, 3) for i in range(1, 10): assert tangos.get_halo(i)['my_test_property'] == i
def teardown_module(): tangos.core.close_db() pt.launch(tangos.core.close_db, 6)
def test_remote_file_index(): pt.launch(_test_remote_file_index, 2)
def test_lazy_evaluation_is_local(): pt.launch(_test_lazy_evaluation_is_local, 2)
def main(): print(""" The 'tangos_timelink' command line is deprecated in favour of 'tangos link'. 'tangos_timelink' may be removed in future versions. """) parallel_tasks.launch(run_dbwriter, 2, [sys.argv[1:]])
def test_simsnap_properties(): pt.launch(_test_simsnap_properties, 2)
def test_get_array(): pt.launch(_get_array, 3)
def test_correct_object_loading(): """This regression test looks for a bug where the pynbody_server module assumed halos could be loaded just by calling f.halos() where f was the SimSnap. This is not true in general; for example, for SubFind catalogues one has both halos and groups and the correct arguments must be passed.""" pt.launch(_test_correct_object_loading, 2)
def test_underlying_class(): pt.launch(_test_underlying_class, 2)
def test_nonexistent_array(): pt.launch(_test_nonexistent_array, 2)
def test_shared_locks(): pt.launch(_test_shared_locks, 4) pt.launch(_test_shared_locks_in_queue, 6)
def test_simsnap_arrays(): pt.launch(_test_simsnap_arrays, 2)
def test_halo_array(): pt.launch(_test_halo_array, 2)
#!/usr/bin/env python import sys import time import tangos.parallel_tasks as pt def test_function(): lock = pt.ExclusiveLock("hello") print("Hello from rank", pt.backend.rank()) for i in pt.distributed(range(10)): with lock: print("Task", i) time.sleep(0.1) if pt.backend.rank() == 1: print() print("OK") if len(sys.argv) != 2: print("Syntax: test_mpi.py [backend name]") else: pt.use(sys.argv[1]) pt.launch(test_function, 8)
def test_empty_then_non_empty_loop(): pt.launch(_test_empty_then_non_empty_loop, 3)