Fork island#
New in version 2.9.
#include <pagmo/islands/fork_island.hpp>
Note
The fork_island class is available only on POSIX platforms (e.g., Linux, OSX,
the BSDs, but not Microsoft Windows, unless some POSIX compatibility layer has been installed
on the system).
-
class fork_island#
This user-defined island (UDI) will use the POSIX
fork()system call to offload the evolution of a population to a child process.Generally speaking, users are encouraged to use
thread_islandrather thanfork_island:thread_islandperforms better, it works also with problems and algorithms which are not serialisable, and it is available on all platforms.fork_islandshould however be preferred when dealing with problems and algorithms which do not offer the basicthread_safetyguarantee. By offloading the optimisation to a separate process (rather than a separate thread),fork_islandcan ensure that thread-unsafe problems and algorithms are always run in only one thread at a time. This capability is particularly useful when wrapping in pagmo third-party code which does not support execution in multithreaded contexts (a notable example is theipoptalgorithm, which uses the thread-unsafe Ipopt optimiser).fork_islandis the UDI type automatically selected by the constructors ofislandon POSIX platforms when the island’s problem and/or algorithm do not provide the basicthread_safetyguarantee.Note
When using memory checking tools such as valgrind, or the address/memory sanitizers from GCC/clang, be aware that memory leaks in the child process may be flagged by such tools. These are spurious warnings due to the fact that the child process is exited via
std::exit()(which does not invoke the destructors of objects with automatic storage duration). Thus, such warnings can be safely ignored.Note
The ability of the forked process to handle errors raised during evolution is dependent on the specific
fork()implementation in use. For instance, it has been reported that on recent OSX versions (i.e., since High Sierra), error handling in the forked process does not work, possibly because it employs code which is not asynchronous-safe.-
fork_island()#
-
fork_island(const fork_island&)#
-
fork_island(fork_island&&) noexcept#
fork_islandis default, copy and move-constructible. The copy and move constructor are equivalent to the default constructor.
-
void run_evolve(island &isl) const#
This method will fork the calling process, and, in the child process, the
populationof isl will be evolved using thealgorithmof isl. At the end of the evolution, the evolved population and the algorithm used for the evolution will be sent back to the parent process, where they will replace, in isl, the original population and algorithm. The child process will then terminate viastd::exit(0).If any exception is raised during the evolution, the error message from the exception will be transferred back to the parent process, where a
std::runtime_errorcontaining the error message from the child will be raised.- Parameters
isl – the
islandthat will be evolved.- Throws
std::runtime_error – if any error arises from the use of POSIX primitives (
fork(), pipes, etc.), or if any error is generated in the child process.unspecified –
any exception raised by:
the serialisation of
populationoralgorithm,pagmo::island::set_population()orpagmo::island::set_algorithm().
-
std::string get_name() const#
- Returns
the string
"Fork island".
-
std::string get_extra_info() const#
- Returns
if an evolution is ongoing, this method will return a string representation of the ID of the child process. Otherwise, the
"No active child"string will be returned.
-
pid_t get_child_pid() const#
- Returns
a signed integral value representing the process ID of the child process, if an evolution is ongoing. Otherwise,
0will be returned.
-
fork_island()#