File: algorithms_mpi.cc

package info (click to toggle)
purify 5.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 186,836 kB
  • sloc: cpp: 17,731; python: 510; xml: 182; makefile: 7; sh: 6
file content (321 lines) | stat: -rw-r--r-- 14,442 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include "purify/types.h"
#include <array>
#include <random>
#include "benchmarks/utilities.h"
#include "purify/algorithm_factory.h"
#include "purify/convergence_factory.h"
#include "purify/directories.h"
#include "purify/distribute.h"
#include "purify/logging.h"
#include "purify/measurement_operator_factory.h"
#include "purify/mpi_utilities.h"
#include "purify/operators.h"
#include "purify/utilities.h"
#include "purify/wavelet_operator_factory.h"
#include <sopt/imaging_padmm.h>
#include <sopt/mpi/communicator.h>
#include <sopt/mpi/session.h>
#include <sopt/relative_variation.h>
#include <sopt/utilities.h>
#include <sopt/wavelets.h>
#include <sopt/wavelets/sara.h>

using namespace purify;

class AlgoFixtureMPI : public ::benchmark::Fixture {
 public:
  void SetUp(const ::benchmark::State &state) {
    // Reading image from file and update related quantities
    bool newImage = b_utilities::updateImage(state.range(0), m_image, m_imsizex, m_imsizey);

    // Generating random uv(w) coverage
    bool newMeasurements = b_utilities::updateMeasurements(state.range(1), m_uv_data, m_epsilon,
                                                           newImage, m_image, m_world);

    bool newKernel = m_kernel != state.range(2);

    m_kernel = state.range(2);
    // Create the measurement operator for both distributed algorithms
    const t_real FoV = 1;  // deg
    const t_real cellsize = FoV / m_imsizex * 60. * 60.;
    const bool w_term = false;
    if (state.range(4) == 1) {
      PURIFY_INFO("Using distributed image MPI algorithm");
      m_measurements_distribute_image = factory::measurement_operator_factory<Vector<t_complex>>(
          factory::distributed_measurement_operator::mpi_distribute_image, m_uv_data,
          m_image.rows(), m_image.cols(), cellsize, cellsize, 2, kernels::kernel::kb, m_kernel,
          m_kernel, w_term);
    }

    if (state.range(4) == 2) {
      PURIFY_INFO("Using distributed grid MPI algorithm");
      m_measurements_distribute_grid = factory::measurement_operator_factory<Vector<t_complex>>(
          factory::distributed_measurement_operator::mpi_distribute_grid, m_uv_data, m_image.rows(),
          m_image.cols(), cellsize, cellsize, 2, kernels::kernel::kb, m_kernel, m_kernel, w_term);
    }

    m_sigma = 0.016820222945913496 * std::sqrt(2);  // see test_parameters file
  }

  void TearDown(const ::benchmark::State &state) {}

  sopt::mpi::Communicator m_world;

  std::vector<std::tuple<std::string, t_uint>> const m_sara{
      std::make_tuple("Dirac", 3u), std::make_tuple("DB1", 3u), std::make_tuple("DB2", 3u),
      std::make_tuple("DB3", 3u),   std::make_tuple("DB4", 3u), std::make_tuple("DB5", 3u),
      std::make_tuple("DB6", 3u),   std::make_tuple("DB7", 3u), std::make_tuple("DB8", 3u)};

  Image<t_complex> m_image;
  t_uint m_imsizex;
  t_uint m_imsizey;

  utilities::vis_params m_uv_data;
  t_real m_epsilon;
  t_real m_sigma;
  t_uint m_kernel;

  std::shared_ptr<sopt::LinearTransform<Vector<t_complex>> const> m_measurements_distribute_image;
  std::shared_ptr<sopt::LinearTransform<Vector<t_complex>> const> m_measurements_distribute_grid;
  std::shared_ptr<sopt::algorithm::ImagingProximalADMM<t_complex>> m_padmm;
  std::shared_ptr<sopt::algorithm::ImagingForwardBackward<t_complex>> m_fb;
};

BENCHMARK_DEFINE_F(AlgoFixtureMPI, PadmmDistributeImage)(benchmark::State &state) {
  // Create the algorithm - has to be done there to reset the internal state.
  // If done in the fixture repeats would start at the solution and converge immediately.
  auto const wavelets = factory::wavelet_operator_factory<Vector<t_complex>>(
      factory::distributed_wavelet_operator::mpi_sara, m_sara, m_imsizey, m_imsizex);

  m_padmm = factory::padmm_factory<sopt::algorithm::ImagingProximalADMM<t_complex>>(
      factory::algo_distribution::mpi_distributed, m_measurements_distribute_image, wavelets,
      m_uv_data, m_sigma, m_imsizey, m_imsizex, m_sara.size(), state.range(3) + 1, true, true,
      false, 1e-3, 1e-2, 50);

  // Benchmark the application of the algorithm
  while (state.KeepRunning()) {
    auto start = std::chrono::high_resolution_clock::now();
    auto result = (*m_padmm)();
    auto end = std::chrono::high_resolution_clock::now();
    std::cout << "Converged? " << result.good << " , niters = " << result.niters << std::endl;
    state.SetIterationTime(b_utilities::duration(start, end, m_world));
  }
}

BENCHMARK_DEFINE_F(AlgoFixtureMPI, PadmmDistributeGrid)(benchmark::State &state) {
  // Create the algorithm - has to be done there to reset the internal state.
  // If done in the fixture repeats would start at the solution and converge immediately.
  auto const wavelets = factory::wavelet_operator_factory<Vector<t_complex>>(
      factory::distributed_wavelet_operator::mpi_sara, m_sara, m_imsizey, m_imsizex);

  m_padmm = factory::padmm_factory<sopt::algorithm::ImagingProximalADMM<t_complex>>(
      factory::algo_distribution::mpi_distributed, m_measurements_distribute_grid, wavelets,
      m_uv_data, m_sigma, m_imsizey, m_imsizex, m_sara.size(), state.range(3) + 1, true, true,
      false, 1e-3, 1e-2, 50);

  // Benchmark the application of the algorithm
  while (state.KeepRunning()) {
    auto start = std::chrono::high_resolution_clock::now();
    auto result = (*m_padmm)();
    auto end = std::chrono::high_resolution_clock::now();
    std::cout << "Converged? " << result.good << " , niters = " << result.niters << std::endl;
    state.SetIterationTime(b_utilities::duration(start, end, m_world));
  }
}

BENCHMARK_DEFINE_F(AlgoFixtureMPI, FbDistributeImage)(benchmark::State &state) {
  // Create the algorithm - has to be done there to reset the internal state.
  // If done in the fixture repeats would start at the solution and converge immediately.
  auto const wavelets = factory::wavelet_operator_factory<Vector<t_complex>>(
      factory::distributed_wavelet_operator::mpi_sara, m_sara, m_imsizey, m_imsizex);

  t_real const beta = m_sigma * m_sigma;
  t_real const gamma = 0.0001;

  m_fb = factory::fb_factory<sopt::algorithm::ImagingForwardBackward<t_complex>>(
      factory::algo_distribution::mpi_serial, m_measurements_distribute_image, wavelets, m_uv_data,
      m_sigma, beta, gamma, m_imsizey, m_imsizex, m_sara.size(), state.range(3), true, true, false,
      1e-3, 1e-2, 50);

  // Benchmark the application of the algorithm
  while (state.KeepRunning()) {
    auto start = std::chrono::high_resolution_clock::now();
    auto result = (*m_fb)();
    auto end = std::chrono::high_resolution_clock::now();
    std::cout << "Converged? " << result.good << " , niters = " << result.niters << std::endl;
    state.SetIterationTime(b_utilities::duration(start, end, m_world));
  }
}

BENCHMARK_DEFINE_F(AlgoFixtureMPI, FbDistributeGrid)(benchmark::State &state) {
  // Create the algorithm - has to be done there to reset the internal state.
  // If done in the fixture repeats would start at the solution and converge immediately.
  auto const wavelets = factory::wavelet_operator_factory<Vector<t_complex>>(
      factory::distributed_wavelet_operator::mpi_sara, m_sara, m_imsizey, m_imsizex);

  t_real const beta = m_sigma * m_sigma;
  t_real const gamma = 0.0001;

  m_fb = factory::fb_factory<sopt::algorithm::ImagingForwardBackward<t_complex>>(
      factory::algo_distribution::mpi_serial, m_measurements_distribute_grid, wavelets, m_uv_data,
      m_sigma, beta, gamma, m_imsizey, m_imsizex, m_sara.size(), state.range(3), true, true, false,
      1e-3, 1e-2, 50);

  // Benchmark the application of the algorithm
  while (state.KeepRunning()) {
    auto start = std::chrono::high_resolution_clock::now();
    auto result = (*m_fb)();
    auto end = std::chrono::high_resolution_clock::now();
    std::cout << "Converged? " << result.good << " , niters = " << result.niters << std::endl;
    state.SetIterationTime(b_utilities::duration(start, end, m_world));
  }
}

#ifdef PURIFY_ONNXRT
BENCHMARK_DEFINE_F(AlgoFixtureMPI, FbOnnxDistributeImage)(benchmark::State &state) {
  // Create the algorithm - has to be done there to reset the internal state.
  // If done in the fixture repeats would start at the solution and converge immediately.

  // TODO: Wavelets are constructed but not used in the factory method
  auto const wavelets = factory::wavelet_operator_factory<Vector<t_complex>>(
      factory::distributed_wavelet_operator::serial, m_sara, m_imsizey, m_imsizex);

  t_real const beta = m_sigma * m_sigma;
  t_real const gamma = 0.0001;

  std::string tf_model_path = purify::models_directory() + "/snr_15_model_dynamic.onnx";

  m_fb = factory::fb_factory<sopt::algorithm::ImagingForwardBackward<t_complex>>(
      factory::algo_distribution::mpi_serial, m_measurements_distribute_image, wavelets, m_uv_data,
      m_sigma, beta, gamma, m_imsizey, m_imsizex, m_sara.size(), state.range(3), true, true, false,
      1e-3, 1e-2, 50, tf_model_path, nondiff_func_type::Denoiser);

  // Benchmark the application of the algorithm
  while (state.KeepRunning()) {
    auto start = std::chrono::high_resolution_clock::now();
    auto result = (*m_fb)();
    auto end = std::chrono::high_resolution_clock::now();
    std::cout << "Converged? " << result.good << " , niters = " << result.niters << std::endl;
    state.SetIterationTime(b_utilities::duration(start, end, m_world));
  }
}

BENCHMARK_DEFINE_F(AlgoFixtureMPI, FbOnnxDistributeGrid)(benchmark::State &state) {
  // Create the algorithm - has to be done there to reset the internal state.
  // If done in the fixture repeats would start at the solution and converge immediately.

  // TODO: Wavelets are constructed but not used in the factory method
  auto const wavelets = factory::wavelet_operator_factory<Vector<t_complex>>(
      factory::distributed_wavelet_operator::serial, m_sara, m_imsizey, m_imsizex);

  t_real const beta = m_sigma * m_sigma;
  t_real const gamma = 0.0001;

  std::string tf_model_path = purify::models_directory() + "/snr_15_model_dynamic.onnx";

  m_fb = factory::fb_factory<sopt::algorithm::ImagingForwardBackward<t_complex>>(
      factory::algo_distribution::mpi_serial, m_measurements_distribute_grid, wavelets, m_uv_data,
      m_sigma, beta, gamma, m_imsizey, m_imsizex, m_sara.size(), state.range(3), true, true, false,
      1e-3, 1e-2, 50, tf_model_path, nondiff_func_type::Denoiser);

  // Benchmark the application of the algorithm
  while (state.KeepRunning()) {
    auto start = std::chrono::high_resolution_clock::now();
    auto result = (*m_fb)();
    auto end = std::chrono::high_resolution_clock::now();
    std::cout << "Converged? " << result.good << " , niters = " << result.niters << std::endl;
    state.SetIterationTime(b_utilities::duration(start, end, m_world));
  }
}

BENCHMARK_REGISTER_F(AlgoFixtureMPI, FbOnnxDistributeImage)
    //->Apply(b_utilities::Arguments)
    ->Args({128, 10000, 4, 10, 1})
    ->Args({1024, static_cast<t_int>(1e6), 4, 10, 1})
    ->Args({1024, static_cast<t_int>(1e7), 4, 10, 1})
    ->Args({2048, static_cast<t_int>(1e6), 4, 10, 1})
    ->Args({2048, static_cast<t_int>(1e7), 4, 10, 1})
    ->Args({4096, static_cast<t_int>(1e6), 4, 10, 1})
    ->Args({4096, static_cast<t_int>(1e7), 4, 10, 1})
    ->UseManualTime()
    ->MinTime(60.0)
    ->MinWarmUpTime(10.0)
    ->Repetitions(3)  //->ReportAggregatesOnly(true)
    ->Unit(benchmark::kMillisecond);

BENCHMARK_REGISTER_F(AlgoFixtureMPI, FbOnnxDistributeGrid)
    //->Apply(b_utilities::Arguments)
    ->Args({128, 10000, 4, 10, 1})
    ->Args({1024, static_cast<t_int>(1e6), 4, 10, 2})
    ->Args({1024, static_cast<t_int>(1e7), 4, 10, 2})
    ->Args({2048, static_cast<t_int>(1e6), 4, 10, 2})
    ->Args({2048, static_cast<t_int>(1e7), 4, 10, 2})
    ->Args({4096, static_cast<t_int>(1e6), 4, 10, 2})
    ->Args({4096, static_cast<t_int>(1e7), 4, 10, 2})
    ->UseManualTime()
    ->MinTime(9.0)
    ->MinWarmUpTime(1.0)
    ->Repetitions(3)  //->ReportAggregatesOnly(true)
    ->Unit(benchmark::kMillisecond);

#endif

BENCHMARK_REGISTER_F(AlgoFixtureMPI, FbDistributeImage)
    //->Apply(b_utilities::Arguments)
    ->Args({128, 10000, 4, 10, 1})
    ->Args({1024, static_cast<t_int>(1e6), 4, 10, 1})
    ->Args({1024, static_cast<t_int>(1e7), 4, 10, 1})
    ->Args({2048, static_cast<t_int>(1e6), 4, 10, 1})
    ->Args({2048, static_cast<t_int>(1e7), 4, 10, 1})
    ->Args({4096, static_cast<t_int>(1e6), 4, 10, 1})
    ->Args({4096, static_cast<t_int>(1e7), 4, 10, 1})
    ->UseManualTime()
    ->MinTime(60.0)
    ->MinWarmUpTime(10.0)
    ->Repetitions(3)  //->ReportAggregatesOnly(true)
    ->Unit(benchmark::kMillisecond);

BENCHMARK_REGISTER_F(AlgoFixtureMPI, FbDistributeGrid)
    //->Apply(b_utilities::Arguments)
    ->Args({128, 10000, 4, 10, 2})
    ->Args({1024, static_cast<t_int>(1e6), 4, 10, 2})
    ->Args({1024, static_cast<t_int>(1e7), 4, 10, 2})
    ->Args({2048, static_cast<t_int>(1e6), 4, 10, 2})
    ->Args({2048, static_cast<t_int>(1e7), 4, 10, 2})
    ->Args({4096, static_cast<t_int>(1e6), 4, 10, 2})
    ->Args({4096, static_cast<t_int>(1e7), 4, 10, 2})
    ->UseManualTime()
    ->MinTime(60.0)
    ->MinWarmUpTime(10.0)
    ->Repetitions(3)  //->ReportAggregatesOnly(true)
    ->Unit(benchmark::kMillisecond);

BENCHMARK_REGISTER_F(AlgoFixtureMPI, PadmmDistributeImage)
    //->Apply(b_utilities::Arguments)
    ->Args({128, 10000, 4, 10, 1})
    ->Args({1024, static_cast<t_int>(1e6), 4, 10, 1})
    ->Args({1024, static_cast<t_int>(1e7), 4, 10, 1})
    ->Args({2048, static_cast<t_int>(1e6), 4, 10, 1})
    ->Args({2048, static_cast<t_int>(1e7), 4, 10, 1})
    ->Args({4096, static_cast<t_int>(1e6), 4, 10, 1})
    ->Args({4096, static_cast<t_int>(1e7), 4, 10, 1})
    ->UseManualTime()
    ->MinTime(120.0)
    ->MinWarmUpTime(10.0)
    ->Repetitions(3)  //->ReportAggregatesOnly(true)
    ->Unit(benchmark::kMillisecond);

BENCHMARK_REGISTER_F(AlgoFixtureMPI, PadmmDistributeGrid)
    //->Apply(b_utilities::Arguments)
    ->Args({128, 10000, 4, 10, 2})
    ->Args({1024, static_cast<t_int>(1e6), 4, 10, 2})
    ->Args({1024, static_cast<t_int>(1e7), 4, 10, 2})
    ->Args({2048, static_cast<t_int>(1e6), 4, 10, 2})
    ->Args({2048, static_cast<t_int>(1e7), 4, 10, 2})
    ->Args({4096, static_cast<t_int>(1e6), 4, 10, 2})
    ->Args({4096, static_cast<t_int>(1e7), 4, 10, 2})
    ->UseManualTime()
    ->MinTime(120.0)
    ->MinWarmUpTime(10.0)
    ->Repetitions(3)  //->ReportAggregatesOnly(true)
    ->Unit(benchmark::kMillisecond);