]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Add tests.
authorWolfgang Bangerth <bangerth@colostate.edu>
Mon, 30 Oct 2023 17:40:40 +0000 (11:40 -0600)
committerWolfgang Bangerth <bangerth@colostate.edu>
Tue, 31 Oct 2023 00:08:14 +0000 (18:08 -0600)
tests/multithreading/task_15.cc [new file with mode: 0644]
tests/multithreading/task_15.output [new file with mode: 0644]
tests/multithreading/task_16.cc [new file with mode: 0644]
tests/multithreading/task_16.output [new file with mode: 0644]

diff --git a/tests/multithreading/task_15.cc b/tests/multithreading/task_15.cc
new file mode 100644 (file)
index 0000000..a9f843e
--- /dev/null
@@ -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 <deal.II/base/thread_management.h>
+
+#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<double> 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<std::ofstream *>(&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 (file)
index 0000000..d239d4d
--- /dev/null
@@ -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 (file)
index 0000000..8277a6a
--- /dev/null
@@ -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 <deal.II/base/thread_management.h>
+
+#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<int> 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 (file)
index 0000000..0859f86
--- /dev/null
@@ -0,0 +1,11 @@
+
+DEAL::0
+DEAL::2
+DEAL::4
+DEAL::6
+DEAL::8
+DEAL::10
+DEAL::12
+DEAL::14
+DEAL::16
+DEAL::18

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.