Changeset 21010

Show
Ignore:
Timestamp:
01/17/08 07:30:22 (8 months ago)
Author:
patrick
Message:

Simplified the implementation of vpr::AveragingAllocationStrategy? so that
the member variable mAllocCounter is not needed. I had to change the tests
for this strategy slightly because I think that the previous implementation of
vpr::AveragingAllocationStrategy? had an off-by-one error. The way that the
tests are written now matches up with my expectation of how the allocation
strategy should behave.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • juggler/trunk/modules/vapor/test/TestSuite/TestCases/IO/Socket/AveragingAllocationStrategyTest.cpp

    r21007 r21010  
    3737   for ( unsigned int i = 0; i < inputs.size(); ++i ) 
    3838   { 
    39       // Keep track of the current average of allocation sizes. 
    40       const size_t average(cur_total / (i + 1)); 
    41  
    4239      // Get the allocation size for the case of a buffer that is currently 
    4340      // empty. 
    4441      const size_t result = s(0, inputs[i]); 
    4542      cur_total += result; 
     43 
     44      // Keep track of the current average of allocation sizes. 
     45      const size_t average(cur_total / (i + 1)); 
    4646 
    4747      // The allocation size for this strategy is the maximum of the requested 
     
    6969   expected[9] = 25600; 
    7070 
    71    size_t writes(1); 
     71   size_t writes(0); 
    7272 
    7373   for ( unsigned int i = 0; i < 10; ++i, ++writes ) 
  • juggler/trunk/modules/vapor/vpr/IO/Socket/AveragingAllocationStrategy.cpp

    r21007 r21010  
    5050} 
    5151 
    52 template<typename T> 
    53 inline T minValue(const T v1, const T v2) 
    54 { 
    55    return v1 < v2 ? v1 : v2; 
    56 } 
    57  
    5852} 
    5953 
     
    6559   : mWindowSize(windowSize) 
    6660   , mIndex(0) 
    67    // mAllocSizes is set to a fixed size so that it is not necessary to resize 
    68    // it as allocations occur, nor is it necessary to stop resizing it when 
    69    // the number of allocations eventually equals windowSize. 
    70    , mAllocSizes(windowSize, 0) 
    71    // mAllocOffset is initialized to 1 because we add it to 
    72    // mAllocSizes.begin() in order to compute the average of the allocations 
    73    // made. See computeAverage(). 
    74    , mAllocOffset(1) 
    7561{ 
    76    /* Do nothing. */ ; 
     62   // Reserve the memory required for storing the sliding window of allocation 
     63   // sizes so that the buffer does not need to grow as we add values to it. 
     64   mAllocSizes.reserve(windowSize); 
    7765} 
    7866 
     
    8674 
    8775   // Record the current amount allocated for future computations of the 
    88    // average allocation amount. 
    89    mAllocSizes[mIndex] = alloc_size; 
     76   // average allocation amount. We accomplish this by first populating 
     77   // mAllocSizes until its size equals mWindowSize. After that, we just 
     78   // assign to the entry identified by mIndex. 
     79   if ( mAllocSizes.size() < mWindowSize ) 
     80   { 
     81      mAllocSizes.push_back(alloc_size); 
     82   } 
     83   else 
     84   { 
     85      mAllocSizes[mIndex] = alloc_size; 
     86   } 
     87 
    9088   mIndex = (mIndex + 1) % mWindowSize; 
    91  
    92    // Incrementing mAllocOffset beyond mWindowSize eliminates the need for it. 
    93    // Perhaps there is a better way of keeping track of the number of 
    94    // allocation sizes stored in mAllocSizes. 
    95    ++mAllocOffset; 
    9689 
    9790   return alloc_size; 
     
    10093size_t AveragingAllocationStrategy::computeAverage() const 
    10194{ 
    102    // The smaller of mAllocOffset and mWindowSize provide the number of 
    103    // allocation sizes that have been stored in mAllocSizes. 
    104    const size_type count(minValue(mAllocOffset, mWindowSize)); 
    105    const size_t sum = std::accumulate(mAllocSizes.begin(), 
    106                                       mAllocSizes.begin() + count, 0); 
     95   size_t average(0); 
    10796 
    108    // Return the average allocation size. 
    109    return sum / count; 
     97   if ( ! mAllocSizes.empty() ) 
     98   { 
     99      const size_t sum(std::accumulate(mAllocSizes.begin(), 
     100                                       mAllocSizes.end(), 0)); 
     101 
     102      // Compute the average allocation size. 
     103      average = sum / mAllocSizes.size(); 
     104   } 
     105 
     106   return average; 
    110107} 
    111108 
  • juggler/trunk/modules/vapor/vpr/IO/Socket/AveragingAllocationStrategy.h

    r21007 r21010  
    100100   size_type           mIndex; 
    101101   std::vector<size_t> mAllocSizes; 
    102    size_type           mAllocOffset; 
    103102   //@} 
    104103};