![]() |
In order to write an application which uses services from the HPX runtime system you need to initialize the HPX library by inserting certain calls into the code of your application. Depending on your use case, this can be done in 3 different ways:
main()
function as the main HPX entry point.
main()
function as the main HPX entry point
This method is the least intrusive to your code. It however provides you with the smallest flexibility in terms of initializing the HPX runtime system only. The following code snippet shows what a minimal HPX application using this technique looks like:
#include <hpx/hpx_main.hpp
>
int main(int argc, char* argv[])
{
return 0;
}
The only change to your code you have to make is to include the file hpx/hpx_main.hpp
.
In this case the function main()
will be invoked as the first HPX
thread of the application. The runtime system will be initialized behind
the scenes before the function main()
is executed and will automatically stopped
after main()
has returned. All HPX API functions can be used from
within this function now.
![]() |
Note |
---|---|
The function |
All command line arguments specific to HPX will still
be processed by the HPX runtime system as usual. However,
those command line options will be removed from the list of values passed
to argc
/argv
of the function main()
.
The list of values passed to main()
will hold only the commandline options
which are not recognized by the HPX runtime system (see
the section HPX
Command Line Options for more details on what options are recognized
by HPX).
The value returned from the function main()
as shown above will be returned to the
operating system as usual.
![]() |
Important |
---|---|
To achieve this seamless integration, the header file #define main hpx_startup::user_main which could result in unexpected behavior. |
With this method you need to provide an explicit main thread function named
hpx_main
at global scope. This function will be invoked as the main entry point of
your HPX application on the console locality only (this
function will be invoked as the first HPX thread of
your application). All HPX API functions can be used
from within this function.
The thread executing the function hpx::init
will block waiting for the runtime system to exit. The value returned from
hpx_main
will be returned from hpx::init
after the runtime system has stopped.
The function hpx::finalize
has to be called on one of the HPX localities in order
to signal that all work has been scheduled and the runtime system should
be stopped after the scheduled work has been executed.
This method of invoking HPX has the advantage of you
being able to decide which version of hpx::init
to call. This allows to pass additional configuration parameters while initializing
the HPX runtime system.
#include <hpx/hpx_init.hpp
> inthpx_main
(int argc, char* argv[]) { // Any HPX application logic goes here... returnhpx::finalize
(); } int main(int argc, char* argv[]) { // Initialize HPX, run hpx_main as the first HPX thread, and // wait for hpx::finalize being called. returnhpx::init
(argc, argv); }
![]() |
Note |
---|---|
The function int hpx_main(); int hpx_main(int argc, char* argv[]); int hpx_main(boost::program_options::variables_map& vm);
This is consistent with (and extends) the usually allowed prototypes for
the function |
The header file to include for this method of using HPX
is hpx/hpx_init.hpp
.
With this method you need to provide an explicit main thread function named
hpx_main
at global scope. This function will be invoked as the main entry point of
your HPX application on the console locality only (this
function will be invoked as the first HPX thread of
your application). All HPX API functions can be used
from within this function.
The thread executing the function hpx::start
will not block waiting for the runtime system
to exit, but will return immediatlely.
![]() |
Important |
---|---|
You cannot use any of the HPX API functions other
that |
The function hpx::finalize
has to be called on one of the HPX localities in order
to signal that all work has been scheduled and the runtime system should
be stopped after the scheduled work has been executed.
This method of invoking HPX is useful for applications
where the main thread is used for special operations, such a GUIs. The function
hpx::stop
can be used to wait for the HPX runtime system to exit
and should be at least used as the last function called in main()
.
The value returned from hpx_main
will be returned from hpx::stop
after the runtime system has stopped.
#include <hpx/hpx_start.hpp
> inthpx_main
(int argc, char* argv[]) { // Any HPX application logic goes here... returnhpx::finalize
(); } int main(int argc, char* argv[]) { // Initialize HPX, run hpx_main.hpx::start
(argc, argv); // ...Execute other code here... // Wait for hpx::finalize being called. returnhpx::stop
(); }
![]() |
Note |
---|---|
The function int hpx_main(); int hpx_main(int argc, char* argv[]); int hpx_main(boost::program_options::variables_map& vm);
This is consistent with (and extends) the usually allowed prototypes for
the function |
The header file to include for this method of using HPX
is hpx/hpx_start.hpp
.