From 85dab755c60ed29fc5dcc2940fa18fa38001ae41 Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Mon, 30 Oct 2023 11:40:40 -0600 Subject: [PATCH] Add tests. --- tests/multithreading/task_15.cc | 67 +++++++++++++++++++++++++++++ tests/multithreading/task_15.output | 6 +++ tests/multithreading/task_16.cc | 53 +++++++++++++++++++++++ tests/multithreading/task_16.output | 11 +++++ 4 files changed, 137 insertions(+) create mode 100644 tests/multithreading/task_15.cc create mode 100644 tests/multithreading/task_15.output create mode 100644 tests/multithreading/task_16.cc create mode 100644 tests/multithreading/task_16.output diff --git a/tests/multithreading/task_15.cc b/tests/multithreading/task_15.cc new file mode 100644 index 0000000000..a9f843ed4c --- /dev/null +++ b/tests/multithreading/task_15.cc @@ -0,0 +1,67 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2016 - 2020 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE.md at +// the top level directory of deal.II. +// +// --------------------------------------------------------------------- + + +// Verify that we can create tasks both via lambdas that take +// arguments, and then passing these arguments as follow-up arguments +// to new_task(). In other words, this is the equivalent strategy to +// passing a function pointer as the first argument, and the arguments +// to the function following. +// +// task_13 already checks the case of lambda functions that take no +// arguments. + + +#include + +#include "../tests.h" + + +// return a double, to make sure we correctly identify the return type +// of the expressions used in new_task(...) +double +test(int i) +{ + deallog << "Task " << i << " starting..." << std::endl; + std::this_thread::sleep_for(std::chrono::seconds(1)); + deallog << "Task " << i << " finished!" << std::endl; + + return 3.141; +} + + + +int +main() +{ + initlog(); + + Threads::TaskGroup tg; + + // use variations of ways we can declare lambdas + tg += Threads::new_task([](int i) -> double { return test(i); }, 1); + tg += Threads::new_task([](int i) -> double { return (float)test(i); }, 2); + + tg.join_all(); + + deallog << "OK" << std::endl; + + std::ofstream *out_stream = + dynamic_cast(&deallog.get_file_stream()); + Assert(out_stream != nullptr, ExcInternalError()); + deallog.detach(); + out_stream->close(); + sort_file_contents("output"); +} diff --git a/tests/multithreading/task_15.output b/tests/multithreading/task_15.output new file mode 100644 index 0000000000..d239d4dd83 --- /dev/null +++ b/tests/multithreading/task_15.output @@ -0,0 +1,6 @@ +DEAL::OK +DEAL::Task 1 finished! +DEAL::Task 1 starting... +DEAL::Task 2 finished! +DEAL::Task 2 starting... + diff --git a/tests/multithreading/task_16.cc b/tests/multithreading/task_16.cc new file mode 100644 index 0000000000..8277a6a646 --- /dev/null +++ b/tests/multithreading/task_16.cc @@ -0,0 +1,53 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2016 - 2022 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE.md at +// the top level directory of deal.II. +// +// --------------------------------------------------------------------- + + +// Like the _15 test, but check that arguments are properly copied. + + +#include + +#include "../tests.h" + + +// return a double, to make sure we correctly identify the return type +// of the expressions used in new_task(...) +int +test(int i) +{ + return 2 * i; +} + + + +int +main() +{ + initlog(); + + // Create a bunch of tasks, and put them into a TaskGroup. The test + // also checks that the call to `new_task()` needs to copy the + // passed in value for `i` rather than take a reference to it, + // because it would otherwise use outdated values. + Threads::TaskGroup tg; + for (unsigned int i = 0; i < 10; ++i) + tg += Threads::new_task([](int ii) { return test(ii); }, i); + + // Then wait for them all to finish and output their respective + // return values. They should be ordered the same way the tasks were + // created, and should be twice the input argument: + for (const int r : tg.return_values()) + deallog << r << std::endl; +} diff --git a/tests/multithreading/task_16.output b/tests/multithreading/task_16.output new file mode 100644 index 0000000000..0859f86735 --- /dev/null +++ b/tests/multithreading/task_16.output @@ -0,0 +1,11 @@ + +DEAL::0 +DEAL::2 +DEAL::4 +DEAL::6 +DEAL::8 +DEAL::10 +DEAL::12 +DEAL::14 +DEAL::16 +DEAL::18 -- 2.39.5