 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Wolfram Brenig Guest
|
Posted: Sun Nov 13, 2005 3:43 pm Post subject: How to suppress template specialization? |
|
|
I need to use bool type of vectors in in a
code based on STL and and MPI2 library
routines. My straightforward attempt was to
instantiate vector<bool> variables:
void SetGrid(ProcInfo &p) {
// ...
vector<bool> periods(l);
// ...
p.ccom = MPI::COMM_WORLD.Create_cart(d,&(dims[0]),
&(periods[0]),1);
// ...
}
However, my gcc v3.3.4 with its STL chokes on that with
error: no matching function for call to
`MPI::Intracomm:: Create_cart(int, int*,
std::_Bit_reference*, int)'
/usr/local/include/mpicxx.h:1485: error:
candidates are: virtual MPI::Cartcomm
MPI::Intracomm::Create_cart(int,
const int*, const bool*, bool) const
This is ok from the systems point of view, since
I had to realize that there is a
vector<bool> specialization in the STL
/usr/include/g++/bits/stl_bvector.h
But this is not what I want, since the MPI interface
requires 'standard' bool variables. So, the question
is:
How can I 'do away' with the template specialization
in this particular case (and maybe if you wish in general)
without getting into trouble with the STL at some
later stage.
Thanks.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alec Ross Guest
|
Posted: Sun Nov 13, 2005 10:54 pm Post subject: Re: How to suppress template specialization? |
|
|
In article <dl72ns$l2h$1 (AT) online (DOT) de>, Wolfram Brenig <w.brenig (AT) tu-bs (DOT) de>
writes
| Quote: | I need to use bool type of vectors in in a
code based on STL and and MPI2 library
routines. My straightforward attempt was to
instantiate vector<bool> variables:
.... |
You've seen Meyers' ESTL Item 18: "Avoid using vector<bool>"?
He suggests deque<bool> and bitset as alternatives.
--
Alec Ross
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alberto Ganesh Barbati Guest
|
Posted: Sun Nov 13, 2005 10:54 pm Post subject: Re: How to suppress template specialization? |
|
|
Wolfram Brenig wrote:
| Quote: | I need to use bool type of vectors in in a
code based on STL and and MPI2 library
routines. My straightforward attempt was to
instantiate vector<bool> variables:
snip
How can I 'do away' with the template specialization
in this particular case (and maybe if you wish in general)
without getting into trouble with the STL at some
later stage.
|
You just can't. You have to use a different container. As you have the
requirement of contiguous storage, the only possibile solution is to use
basic_string<bool>. Notice that:
1) it might not be as efficient as vector depending on the use case
2) you might need to specialize std::char_traits<bool> or provide your
own traits class, as implementations are not required to provide one
3) you should no longer use &(periods[0]) but periods.data() instead
HTH,
Ganesh
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Carl Barron Guest
|
Posted: Mon Nov 14, 2005 9:24 am Post subject: Re: How to suppress template specialization? |
|
|
Alec Ross <alec (AT) arlross (DOT) demon.co.uk> wrote:
| Quote: | In article <dl72ns$l2h$1 (AT) online (DOT) de>, Wolfram Brenig <w.brenig (AT) tu-bs (DOT) de
writes
I need to use bool type of vectors in in a
code based on STL and and MPI2 library
routines. My straightforward attempt was to
instantiate vector
...
You've seen Meyers' ESTL Item 18: "Avoid using vector<bool>"?
He suggests deque<bool> and bitset as alternatives.
deque and bitset will have problems. A boost::shared_array<bool |
or scoped_array
having to roll your own dynamically allocated at construction array of
bool.
class bool_array
{
boost::shared_array<bool> p_data;
std::size_t n_data;
public:
bool_array(std::size_t n) :p_data(new bool[n]),n_data(n){}
// cpy, assignment and dtor defaults are ok...
bool *data() const {return &p_data[0];}
std::size_t size() const {return n_data;}
// any thing else you want/need from vector...
};
void foo(bool *,std::size_t);
void usage()
{
bool_array array(10);
// fill the array...
foo(array.data(),array.size());
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Daniel Krügler Guest
|
Posted: Mon Nov 14, 2005 10:22 am Post subject: Re: How to suppress template specialization? |
|
|
Alec Ross wrote:
| Quote: | In article <dl72ns$l2h$1 (AT) online (DOT) de>, Wolfram Brenig <w.brenig (AT) tu-bs (DOT) de
writes
I need to use bool type of vectors in in a
code based on STL and and MPI2 library
routines. My straightforward attempt was to
instantiate vector
...
You've seen Meyers' ESTL Item 18: "Avoid using vector<bool>"?
He suggests deque<bool> and bitset as alternatives.
|
In general a good idea, but not in connection with the OP's requirement
to use the MPI2 library, which (most probably) needs continous memory
access, which is not provided by deque<bool>. std::bitset wouldn't help
him, because it has practically the same implementation as
std::vector<bool> but is even stronger restricted, because its size
needs to be a compile-time constant (Its name is: template <size_t N>
class bitset).
If my assumptions are correct, than the best thing the OP can do is
using std::vector<(unsigned/signed) char> instead. He could even use
enum Bool {False, True};
std::vector<Bool>;
but that is also not such a good idea for MPI2, I think, because I
strongly assume, that for the MPI interface one should use data types of
same layout on both sides (which is not guaranteed for enumeration
types). So std::vector<uint_8> would be the safest thing to use.
Greetings from Bremen,
Daniel Krügler
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Daniel Krügler Guest
|
Posted: Mon Nov 14, 2005 10:23 am Post subject: Re: How to suppress template specialization? |
|
|
Alberto Ganesh Barbati wrote:
| Quote: | Wolfram Brenig wrote:
I need to use bool type of vectors in in a
code based on STL and and MPI2 library
routines. My straightforward attempt was to
instantiate vector<bool> variables:
snip
How can I 'do away' with the template specialization
in this particular case (and maybe if you wish in general)
without getting into trouble with the STL at some
later stage.
You just can't. You have to use a different container. As you have the
requirement of contiguous storage, the only possibile solution is to use
basic_string<bool>. Notice that:
1) it might not be as efficient as vector depending on the use case
2) you might need to specialize std::char_traits<bool> or provide your
own traits class, as implementations are not required to provide one
3) you should no longer use &(periods[0]) but periods.data() instead
|
I don't think, that basic_string<bool> would be a useful substitute:
First, the basic_string<> memory layout is not necessarily continous, so
he could only use data() for read-only access.
Second, I don't think that he should use bool at all, because bool
does not have a fixed size (which I assume is necessary for MPI).
To my opinion he should use something like std::vector<uint8_t>.
Greetings from Bremen,
Daniel Krügler
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alberto Ganesh Barbati Guest
|
Posted: Tue Nov 15, 2005 9:07 am Post subject: Re: How to suppress template specialization? |
|
|
Daniel Krügler wrote:
| Quote: | Alberto Ganesh Barbati wrote:
You just can't. You have to use a different container. As you have the
requirement of contiguous storage, the only possibile solution is to use
basic_string<bool>. Notice that:
1) it might not be as efficient as vector depending on the use case
2) you might need to specialize std::char_traits<bool> or provide your
own traits class, as implementations are not required to provide one
3) you should no longer use &(periods[0]) but periods.data() instead
I don't think, that basic_string<bool> would be a useful substitute:
First, the basic_string<> memory layout is not necessarily continous, so
he could only use data() for read-only access.
|
The OP only requested for read-only access, so using data() (as I
stressed out in my post) looks reasonable to me. As I'm not aware of
other requirements, I don't think this is really a problem.
| Quote: | Second, I don't think that he should use bool at all, because bool
does not have a fixed size (which I assume is necessary for MPI).
To my opinion he should use something like std::vector<uint8_t>.
|
The library the OP is using is explicitly calling for a const bool*.
Using any other type (such as uint8_t), although it might be a better
idea in principle, would require that either the OP modifies the library
or that he writes some kind of adapter. Neither of these solutions looks
appealing to me as the former solution might be impossible (not all
software is open-source!) and the latter would be inefficient as it
eventually requires a making a copy of the entire data.
I'm not saying that basic_string<bool> is a good solution, but it's not
a bad one, it relies on standard containers only, it fits with the OP's
requirements and it would probably require very little modifications in
the OP's code. What do you want more?
Regards,
Ganesh
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Daniel Krügler Guest
|
Posted: Tue Nov 15, 2005 2:33 pm Post subject: Re: How to suppress template specialization? |
|
|
Alberto Ganesh Barbati wrote:
| Quote: | Daniel Krügler wrote:
Alberto Ganesh Barbati wrote:
You just can't. You have to use a different container. As you have the
requirement of contiguous storage, the only possibile solution is to use
basic_string<bool>. Notice that:
1) it might not be as efficient as vector depending on the use case
2) you might need to specialize std::char_traits<bool> or provide your
own traits class, as implementations are not required to provide one
3) you should no longer use &(periods[0]) but periods.data() instead
I don't think, that basic_string<bool> would be a useful substitute:
First, the basic_string<> memory layout is not necessarily continous, so
he could only use data() for read-only access.
The OP only requested for read-only access, so using data() (as I
stressed out in my post) looks reasonable to me. As I'm not aware of
other requirements, I don't think this is really a problem.
|
You are absolutely right. I didn't carefully read the OP's quote of
the signature of the MPI2 function. Obviously MPI2 also accepts C++
types like bool. I wonder how they solve the problem of interprocess
transport of bool values. As far as I remember MPI only supported some
fixed-size types like int32.
Greetings from Bremen,
Daniel
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Allan W Guest
|
Posted: Wed Nov 16, 2005 6:16 pm Post subject: Re: How to suppress template specialization? |
|
|
Wolfram Brenig wrote:
| Quote: | void SetGrid(ProcInfo &p) {
// ...
vector<bool> periods(l);
// ...
p.ccom = MPI::COMM_WORLD.Create_cart(d,&(dims[0]),
&(periods[0]),1);
// ...
}
error: no matching function for call to
`MPI::Intracomm:: Create_cart(int, int*,
std::_Bit_reference*, int)'
/usr/local/include/mpicxx.h:1485: error:
candidates are: virtual MPI::Cartcomm
MPI::Intracomm::Create_cart(int,
const int*, const bool*, bool) const
How can I 'do away' with the template specialization
in this particular case (and maybe if you wish in general)
without getting into trouble with the STL at some
later stage.
|
In general, if your library function needs a pointer to an array of T,
you can create a vector of T and pass the address of the first element.
As you've found, this doesn't work with bool.
Create a non-portable typedef. (Go ahead, give yourself permission to
write one line of non-portable code this time -- put it somewhere
that's easy to find and modify, so that if you ever need to port your
program, you can find and fix it quickly.) Type bool is probably
compatible with SOME other integral type -- probably unsigned short.
So...
// GlobalSettings.h
#if sizeof(unsigned short)==sizeof(bool)
typedef std::vector<unsigned short> ReplaceVectorOfBool;
#else
// ... Other replacements
#endif
Now you can write:
void SetGrid(ProcInfo &p) {
// ...
ReplaceVectorOfBool periods(l);
// ...
p.ccom = MPI::COMM_WORLD.Create_cart(d,&(dims[0]),
(bool*)&(periods[0]),1);
// ...
}
Good luck.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|