Reputation: 65
I'm implementing a MPI code and I'm using MPI_Bcast
function. One of the function's components is the number of data sent which is here the size of a vector called l_nMinplacesPos
. I want to share the size of this vector. I tried once sizeof l_nMinplacesPos
which returned 32 and when I used l_nMinplacesPos.size()
I got 2 as a size of the vector!!!. I'm confused which one of them displays the actual size of the vector? and what is the difference between both of them?
void ParaStochSimulator::broad_casting(long j){
std::cout << "i'm broad_casting" << std::endl;
l_nMinplacesPos = (*m_pcTransitionsInfo)[j]->GetManipulatedPlaces();
double val;
l_anMarking.reserve(l_nMinplacesPos.size());
for (auto lnpos : l_nMinplacesPos)
{
val = m_anCurrentMarking[lnpos];
l_anMarking.push_back(val);
}
for (auto marking : l_anMarking)
{
std::cout << marking << std::endl;
}
MPI_Bcast(&l_anMarking, sizeof l_nMinplacesPos, MPI_DOUBLE, 0, MPI_COMM_WORLD); //->here i used l_nMinplacesPos.size() instead.
}
void ParaStochSimulator::SimulateSingleRun()
{
//prepare a run
PrepareRun();
while ((m_nCurrentTime < m_nOutputEndPoint) && IsSimulationRunning())
{
deterMinTau();
if (mnprocess_id == 0)
{
SimulateSingleStep1();
std::cout << "current time:*****" << m_nCurrentTime << std::endl;
broad_casting(m_nMinTransPos);
std::cout << "size of mani place :" << sizeof l_nMinplacesPos << std::endl;
}
}
PostProcessRun();
MPI_Bcast(&l_anMarking, sizeof l_nMinplacesPos, MPI_DOUBLE, 0, MPI_COMM_WORLD); //->here i used l_nMinplacesPos.size() instead.
std::cout << "size of mani place :" << sizeof l_nMinplacesPos << std::endl;
}
Upvotes: 2
Views: 12782
Reputation: 56
Worth mentioning about sizeof()
is also the fact, that when you work directly on your array's cells' content (especially strings), it may cut off the length of that specific cell to 8 bytes. On my practical exam I had a task to create a C/C++ program, which locates the first letter of every word inserted into the array. After using sizeof()
it returned only first 8 characters of that array, the rest has been thrown out of memory, so be careful.
Upvotes: 1
Reputation: 308206
.size()
returns the number of elements in the vector. That's what you should be using.
sizeof
gives you the number of bytes used by the object definition, excluding any additional storage it has allocated through the use of pointers. It is a static constant generated by the compiler at compile time, based on the declaration of the vector
class.
Upvotes: 11