Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constructing a vector of components only correctly initializes the first component #2323

Closed
DavidPfander-UniStuttgart opened this issue Sep 7, 2016 · 4 comments · Fixed by #2335

Comments

@DavidPfander-UniStuttgart

Constructing a vector of components only correctly initializes the first component. There seem to be some std::move calls that hinder the construction of the other components, as they receive values that have been moved away, see the following example:

#include <iostream>
#include <hpx/hpx_init.hpp>
#include <hpx/include/actions.hpp>
#include <hpx/include/async.hpp>
#include <hpx/include/util.hpp>
#include <hpx/include/components.hpp>

struct component_server: hpx::components::component_base<
  component_server> {

  std::vector<double> A;

  component_server() {
  }

  component_server(const std::vector<double> &A) :
    A(A) {
    std::cout << "initializing A A.s: " << A.size() << std::endl;
  }

  void do_stuff() {

  }

  HPX_DEFINE_COMPONENT_ACTION(component_server, do_stuff,
                  do_stuff_action);
};

HPX_REGISTER_ACTION_DECLARATION(component_server::do_stuff_action);

struct component_client: hpx::components::client_base<
  component_client, component_server> {

  using base_type = hpx::components::client_base<
    component_client, component_server>;

  component_client(const hpx::naming::id_type& id): base_type(id) {

  }

  hpx::future<void> do_stuff_client() {
    return hpx::async<component_server::do_stuff_action>(
                             get_id());
  }
};

HPX_REGISTER_COMPONENT(hpx::components::component<component_server>,
        component_server_component);

HPX_REGISTER_ACTION(component_server::do_stuff_action);

int hpx_main() {

  std::vector<double> A(1000);

  std::vector<component_client> mass_construct = hpx::new_<
    component_client[]>(hpx::find_here(), 10, A).get();

  return hpx::finalize();
}

int main(int argc, char **argv) {
// Initialize and run HPX
 return hpx::init(argc, argv);
}

Expected output:

initializing A A.s: 1000
initializing A A.s: 1000
initializing A A.s: 1000
initializing A A.s: 1000
initializing A A.s: 1000
initializing A A.s: 1000
initializing A A.s: 1000
initializing A A.s: 1000
initializing A A.s: 1000
initializing A A.s: 1000

Actual output:

initializing A A.s: 1000
initializing A A.s: 0
initializing A A.s: 0
initializing A A.s: 0
initializing A A.s: 0
initializing A A.s: 0
initializing A A.s: 0
initializing A A.s: 0
initializing A A.s: 0
initializing A A.s: 0
@AntonBikineev
Copy link
Contributor

@hkaiser
Copy link
Member

hkaiser commented Sep 7, 2016

@AntonBikineev Yes, thanks. The arguments of this function could also be converted to forwarding references.

@AntonBikineev
Copy link
Contributor

@hkaiser I've thought about it too, but it seems to me there should still be explicit copies in that loop

@hkaiser
Copy link
Member

hkaiser commented Sep 7, 2016

@AntonBikineev yes, I agree. I'll submit a patch later today.

hkaiser added a commit that referenced this issue Sep 17, 2016
hkaiser added a commit that referenced this issue Sep 18, 2016
hkaiser added a commit that referenced this issue Sep 19, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants