Reputation: 1971
I am trying to initialise Armadillo sparse matriix sp_mat within MPI as follows:
if(rank==0)
{ // some code for locations, values
sp_mat X(locations,values)
}
// this is where I want to use X
if(rank==0)
some_fun(X)
As you can see, Armadillo constructor is local to the if block
and as such can not use it after if block
.
Putting the same question in another way:
extern sp_mat X
if(rank==0)
{ // some code for locations, values
sp_mat X(locations,values)
}
// this is where I want to use X
if(rank==0)
some_fun(X)
Using extern sp_mat X
before if block
also does not help (I got undefined reference error).
How can I initialise X and reuse it afterward?
Upvotes: 0
Views: 291
Reputation: 1605
Rather than using pointer tricks, the much cleaner std::move()
from C++11 can be used:
sp_mat X;
if(rank==0)
{ // some code for locations, values
X = std::move( sp_mat(locations,values) );
}
Upvotes: 1
Reputation: 23497
Use (smart) pointers:
std::unique_ptr<sp_mat> X; // or std::shared_ptr<sp_mat> or sp_mat*
if (rank == 0) {
// some code for locations and values
X = std::unique_ptr<sp_mat>(new sp_mat(locations, values));
}
...
if (rank == 0)
some_fun(*X);
Upvotes: 1