File: itkTimeStampTest.cxx

package info (click to toggle)
insighttoolkit5 5.4.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 704,384 kB
  • sloc: cpp: 783,592; ansic: 628,724; xml: 44,704; fortran: 34,250; python: 22,874; sh: 4,078; pascal: 2,636; lisp: 2,158; makefile: 464; yacc: 328; asm: 205; perl: 203; lex: 146; tcl: 132; javascript: 98; csh: 81
file content (194 lines) | stat: -rw-r--r-- 6,633 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*=========================================================================
 *
 *  Copyright NumFOCUS
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         https://www.apache.org/licenses/LICENSE-2.0.txt
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *=========================================================================*/

#include "itkTimeStamp.h"
#include "itkMultiThreaderBase.h"

#include <iostream>
#include <memory> // For make_unique.
#include <type_traits>

static_assert(std::is_nothrow_default_constructible_v<itk::TimeStamp>, "Check TimeStamp default-constructibility");
static_assert(std::is_trivially_copy_constructible_v<itk::TimeStamp>, "Check TimeStamp copy-constructibility");
static_assert(std::is_trivially_copy_assignable_v<itk::TimeStamp>, "Check TimeStamp copy-assignability");
static_assert(std::is_trivially_destructible_v<itk::TimeStamp>, "Check TimeStamp destructibility");

// A helper struct for the test, the idea is to have one timestamp per thread.
// To ease the writing of the test, we use  MultiThreaderBase::SingleMethodExecute
// with an array of timestamps in the shared data
struct TimeStampTestHelper
{
  std::vector<itk::TimeStamp> timestamps;
  std::vector<unsigned long>  counters;
};

ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION
modified_function(void * ptr)
{
  using WorkUnitInfoType = itk::MultiThreaderBase::WorkUnitInfo;

  auto * infoStruct = static_cast<WorkUnitInfoType *>(ptr);

  const itk::ThreadIdType workUnitID = infoStruct->WorkUnitID;

  auto * helper = static_cast<TimeStampTestHelper *>(infoStruct->UserData);

  helper->timestamps[workUnitID].Modified();
  helper->counters[workUnitID]++;

  return ITK_THREAD_RETURN_DEFAULT_VALUE;
}

int
itkTimeStampTest(int, char *[])
{
  bool success = true;

  try
  {
    TimeStampTestHelper helper;

    // Set up the multithreader
    itk::MultiThreaderBase::Pointer multithreader = itk::MultiThreaderBase::New();
    multithreader->SetNumberOfWorkUnits(itk::ITK_MAX_THREADS + 10); // this will be clamped
    multithreader->SetSingleMethod(modified_function, &helper);

    // Test that the number of threads has actually been clamped
    const itk::ThreadIdType numberOfThreads = multithreader->GetMaximumNumberOfThreads();

    if (numberOfThreads > itk::ITK_MAX_THREADS)
    {
      std::cerr << "[TEST FAILED]" << std::endl;
      std::cerr << "numberOfThreads > ITK_MAX_THREADS" << std::endl;
      return EXIT_FAILURE;
    }

    const itk::ThreadIdType numberOfWorkUnits = multithreader->GetNumberOfWorkUnits();

    // Set up the helper class
    helper.counters.resize(numberOfWorkUnits);
    helper.timestamps.resize(numberOfWorkUnits);
    for (itk::ThreadIdType k = 0; k < numberOfWorkUnits; ++k)
    {
      helper.counters[k] = 0;
    }

    // Declare an array to test whether the all modified times have
    // been used
    const auto istimestamped = std::make_unique<bool[]>(numberOfWorkUnits);

    // Call Modified once  on any object to make it up-to-date
    multithreader->Modified();

    const itk::ModifiedTimeType init_mtime = multithreader->GetMTime();
    std::cout << "init_mtime: " << init_mtime << std::endl;

    itk::ModifiedTimeType prev_mtime = init_mtime;

    constexpr unsigned int num_exp = 500;

    for (unsigned int i = 0; i < num_exp; ++i)
    {
      multithreader->SingleMethodExecute();

      itk::ModifiedTimeType min_mtime = helper.timestamps[0].GetMTime();
      itk::ModifiedTimeType max_mtime = helper.timestamps[0].GetMTime();
      for (itk::ThreadIdType k = 0; k < numberOfWorkUnits; ++k)
      {
        const itk::ModifiedTimeType & mtime = helper.timestamps[k].GetMTime();
        if (mtime > max_mtime)
        {
          max_mtime = mtime;
        }
        else if (mtime < min_mtime)
        {
          min_mtime = mtime;
        }

        // initialize the array to false
        istimestamped[k] = false;
      }

      bool iter_success = (((max_mtime - prev_mtime) == numberOfWorkUnits) && (min_mtime == prev_mtime + 1));

      if (iter_success)
      {
        for (itk::ThreadIdType k = 0; k < numberOfWorkUnits; ++k)
        {
          // Test whether the all modified times have
          // been used
          const itk::ModifiedTimeType index = helper.timestamps[k].GetMTime() - min_mtime;

          if (istimestamped[index])
          {
            iter_success = false;
            std::cerr << helper.timestamps[k].GetMTime() << " was used twice as a timestamp!" << std::endl;
          }
          else
          {
            istimestamped[index] = true;
          }

          // Test the counters
          if (helper.counters[k] != i + 1)
          {
            iter_success = false;
            std::cerr << "counter[" << k << "] = " << helper.counters[k];
            std::cerr << " at iteration " << i << std::endl;
          }
        }
      }

      if (!iter_success)
      {
        std::cerr << "[Iteration " << i << " FAILED]" << std::endl;
        std::cerr << "max_mtime       : " << max_mtime << std::endl;
        std::cerr << "min_mtime       : " << min_mtime << std::endl;
        std::cerr << "prev_mtime      : " << prev_mtime << std::endl;
        std::cerr << "num_threads     : " << numberOfThreads << std::endl;
        std::cerr << "num_work_units  : " << numberOfWorkUnits << std::endl;
        std::cerr << "max - prev mtime: " << max_mtime - prev_mtime << std::endl;
        std::cerr << std::endl;
        success = false;

        // Note that in a more general setting,  (max_mtime-prev_mtime)>numberOfWorkUnits
        // might be a normal case since the modified time of a time stamp
        // is global. If a new itk object is created this will also increment
        // the time. In our specific test, there's no reason for another ITK object to be
        // modified though
      }

      prev_mtime = max_mtime;
    }
  }
  catch (const itk::ExceptionObject & e)
  {
    std::cerr << "[TEST FAILED]" << std::endl;
    std::cerr << "Exception caught: " << e << std::endl;
    return EXIT_FAILURE;
  }

  if (!success)
  {
    std::cerr << "[TEST FAILED]" << std::endl;
    return EXIT_FAILURE;
  }

  std::cout << "[TEST PASSED]" << std::endl;
  return EXIT_SUCCESS;
}