VDSCopy.cpp 15.5 KB
Newer Older
Jørgen Lind's avatar
VDSCopy  
Jørgen Lind committed
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
#include <fmt/printf.h>

#include <OpenVDS/OpenVDS.h>
#include <OpenVDS/VolumeDataLayout.h>

#include <cxxopts/cxxopts.hpp>
#include <ThreadPool/ThreadPool.h>
#include <PrintHelpers.h>

#include <assert.h>

inline char asciitolower(char in) {
  if (in <= 'Z' && in >= 'A')
    return in - ('Z' - 'z');
  return in;
}

inline int32_t GetVoxelFormatByteSize(OpenVDS::VolumeDataChannelDescriptor::Format format)
{
  int32_t iRetval = -1;
  switch (format) {
  case OpenVDS::VolumeDataChannelDescriptor::Format_R64:
  case OpenVDS::VolumeDataChannelDescriptor::Format_U64:
    iRetval = 8;
    break;
  case OpenVDS::VolumeDataChannelDescriptor::Format_R32:
  case OpenVDS::VolumeDataChannelDescriptor::Format_U32:
    iRetval = 4;
    break;
  case OpenVDS::VolumeDataChannelDescriptor::Format_U16:
    iRetval = 2;
    break;
  case OpenVDS::VolumeDataChannelDescriptor::Format_U8:
  case OpenVDS::VolumeDataChannelDescriptor::Format_1Bit:
    iRetval =1;
    break;
  default:
    fprintf(stderr, "Unknown voxel format");
    abort();
  }
  return iRetval;
}

struct CopyError
{
  int code = 0;
  std::string message;
};

bool flushFutureBufer(std::vector<std::future<CopyError>>& futures, bool jsonOutput, int64_t totalChunks, int64_t &doneChunks, int &percentage, CopyError& copyError)
{
  for (auto& future : futures)
  {
    auto error = future.get();
    if (error.code)
    {
      copyError = error;
      return false;
    }
    int newPercentage = int(++doneChunks * 100 / totalChunks);
    if (newPercentage != percentage)
    {
      percentage = newPercentage;
      if (!jsonOutput)
        fmt::print(stdout, "\r {:3}% Done.", percentage);
    }
  }
  futures.clear();
  return true;
}

int main(int argc, char **argv)
{
Jørgen Lind's avatar
Jørgen Lind committed
74
  cxxopts::Options options("VDSCopy", "VDSCopy - A tool for copying a VDS between locations\n\nSee online documentation for connection paramters:\nhttp://osdu.pages.community.opengroup.org/platform/domain-data-mgmt-services/seismic/open-vds/connection.html\n");
Jørgen Lind's avatar
VDSCopy  
Jørgen Lind committed
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
  options.positional_help("<source_url> <destination_url>");

  std::vector<std::string> urlarg;
  std::string sourceConnection;
  std::string destinationConnection;
  std::string compressionMethodString;
  float compressionTolerance = std::nanf("nan");

  bool jsonOutput = false;
  bool help = false;
  bool version = false;

  std::string supportedCompressionMethods = "None";
  if(OpenVDS::IsCompressionMethodSupported(OpenVDS::CompressionMethod::Wavelet)) supportedCompressionMethods += ", Wavelet";
  if(OpenVDS::IsCompressionMethodSupported(OpenVDS::CompressionMethod::RLE)) supportedCompressionMethods += ", RLE";
  if(OpenVDS::IsCompressionMethodSupported(OpenVDS::CompressionMethod::Zip)) supportedCompressionMethods += ", Zip";
  if(OpenVDS::IsCompressionMethodSupported(OpenVDS::CompressionMethod::WaveletNormalizeBlock)) supportedCompressionMethods += ", WaveletNormalizeBlock";
  if(OpenVDS::IsCompressionMethodSupported(OpenVDS::CompressionMethod::WaveletLossless)) supportedCompressionMethods += ", WaveletLossless";
  if(OpenVDS::IsCompressionMethodSupported(OpenVDS::CompressionMethod::WaveletNormalizeBlockLossless)) supportedCompressionMethods += ", WaveletNormalizeBlockLossless";

//connection options
  options.add_option("", "", "urls", "Urls with vendor specific protocol.", cxxopts::value<std::vector<std::string>>(urlarg), "<string>");
  options.add_option("", "s", "source-connection", "Vendor specific connection string.", cxxopts::value<std::string>(sourceConnection), "<string>");
  options.add_option("", "d", "destination-connection", "Vendor specific connection string.", cxxopts::value<std::string>(destinationConnection), "<string>");

  options.add_option("", "", "compression-method", std::string("Compression method. Supported compression methods are: ") + supportedCompressionMethods + ".", cxxopts::value<std::string>(compressionMethodString), "<string>");
  options.add_option("", "", "tolerance", "This parameter specifies the compression tolerance when using the wavelet compression method. This value is the maximum deviation from the original data value when the data is converted to 8-bit using the value range. A value of 1 means the maximum allowable loss is the same as quantizing to 8-bit (but the average loss will be much much lower than quantizing to 8-bit). It is not a good idea to directly relate the tolerance to the quality of the compressed data, as the average loss will in general be an order of magnitude lower than the allowable loss.", cxxopts::value<float>(compressionTolerance), "<value>");

  options.add_option("", "", "json-output", "Enable json output.", cxxopts::value<bool>(jsonOutput), "");
  options.add_option("", "h", "help", "Print this help information", cxxopts::value<bool>(help), "");
  options.add_option("", "", "version", "Print version information.", cxxopts::value<bool>(version), "");

  options.parse_positional("urls");

  if(argc == 1)
  {
    OpenVDS::printInfo(jsonOutput, "Args", options.help());
    return EXIT_SUCCESS;
  }

  try
  {
    options.parse(argc, argv);
  }
  catch(cxxopts::OptionParseException &e)
  {
    OpenVDS::printError(jsonOutput, "Args", e.what());
    return EXIT_FAILURE;
  }

  if(help)
  {
    OpenVDS::printInfo(jsonOutput, "Args", options.help());
    return EXIT_SUCCESS;
  }

  if (version)
  {
    OpenVDS::printVersion(jsonOutput, "VDSCopy");
    return EXIT_SUCCESS;
  }

  if (urlarg.size() != 2)
  {
    OpenVDS::printError(jsonOutput, "Args", "Failed - missing url/vdsfile argument");
    return EXIT_FAILURE;
  }
  
  std::string sourceUrl = urlarg[0];
  std::string destinationUrl = urlarg[1];


  OpenVDS::Error error;

  std::unique_ptr<OpenVDS::VDS, decltype(&OpenVDS::Close)> sourceHandle(nullptr, &OpenVDS::Close);

  if(OpenVDS::IsSupportedProtocol(sourceUrl))
  {
    sourceHandle.reset(OpenVDS::Open(sourceUrl, sourceConnection, error));
  }
  else
  {
    sourceHandle.reset(OpenVDS::Open(OpenVDS::VDSFileOpenOptions(sourceUrl), error));
  }

  if(error.code != 0)
  {
    OpenVDS::printError(jsonOutput, "VDS", fmt::format("Could not open VDS {}", sourceUrl), error.string);
    return EXIT_FAILURE;
  }

  auto layout = OpenVDS::GetLayout(sourceHandle.get());
  if (!layout)
  {
    OpenVDS::printError(jsonOutput, "VDS", "Internal error, no layout");
    return EXIT_FAILURE;
  }

  if (std::isnan(compressionTolerance))
  {
    compressionTolerance = OpenVDS::GetCompressionTolerance(sourceHandle.get());
  }
  OpenVDS::CompressionMethod compressionMethod = OpenVDS::CompressionMethod::None;

  std::transform(compressionMethodString.begin(), compressionMethodString.end(), compressionMethodString.begin(), asciitolower);

  if(compressionMethodString.empty()) compressionMethod = OpenVDS::GetCompressionMethod(sourceHandle.get());
  else if(compressionMethodString == "none")                          compressionMethod = OpenVDS::CompressionMethod::None;
  else if(compressionMethodString == "wavelet")                       compressionMethod = OpenVDS::CompressionMethod::Wavelet;
  else if(compressionMethodString == "rle")                           compressionMethod = OpenVDS::CompressionMethod::RLE;
  else if(compressionMethodString == "zip")                           compressionMethod = OpenVDS::CompressionMethod::Zip;
  else if(compressionMethodString == "waveletnormalizeblock")         compressionMethod = OpenVDS::CompressionMethod::WaveletNormalizeBlock;
  else if(compressionMethodString == "waveletlossless")               compressionMethod = OpenVDS::CompressionMethod::WaveletLossless;
  else if(compressionMethodString == "waveletnormalizeblocklossless") compressionMethod = OpenVDS::CompressionMethod::WaveletNormalizeBlockLossless;
  else
  {
    OpenVDS::printError(jsonOutput, "CompressionMethod", "Unknown compression method", compressionMethodString);
    return EXIT_FAILURE;
  }

  if(!OpenVDS::IsCompressionMethodSupported(compressionMethod))
  {
    OpenVDS::printError(jsonOutput, "CompressionMethod", "Unsupported compression method", compressionMethodString);
    return EXIT_FAILURE;
  }

  int dimensionCount = layout->GetDimensionality();
  std::vector<OpenVDS::VolumeDataAxisDescriptor> axisDescriptors;
  axisDescriptors.reserve(dimensionCount);
  for (int i = 0; i < dimensionCount; i++)
  {
    axisDescriptors.emplace_back(layout->GetAxisDescriptor(i));
  }

  int channelCount = layout->GetChannelCount();
  std::vector<OpenVDS::VolumeDataChannelDescriptor> channelDescriptors;
  channelDescriptors.reserve(channelCount);
  for (int i = 0; i < channelCount; i++)
  {
    channelDescriptors.emplace_back(layout->GetChannelDescriptor(i));
  }
  OpenVDS::VolumeDataLayoutDescriptor layoutDescriptor = layout->GetLayoutDescriptor();


  std::unique_ptr<OpenVDS::VDS, decltype(&OpenVDS::Close)> destinationHandle(nullptr, &OpenVDS::Close);
  destinationHandle.reset(OpenVDS::Create(destinationUrl, destinationConnection, layoutDescriptor, axisDescriptors, channelDescriptors, *layout, compressionMethod, compressionTolerance, error));
  if(error.code != 0)
  {
    OpenVDS::printError(jsonOutput, "VDS", fmt::format("Could not create VDS {}", destinationUrl), error.string);
    return EXIT_FAILURE;
  }
  auto sourceAccessManager = OpenVDS::GetAccessManager(sourceHandle.get());
  auto destinationAccessManager = OpenVDS::GetAccessManager(destinationHandle.get());

  auto sourceAccessorDestroyer = [&sourceAccessManager](OpenVDS::VolumeDataPageAccessor* acc) { if (acc) sourceAccessManager.DestroyVolumeDataPageAccessor(acc); };
  auto destinationAccessorDestroyer = [&destinationAccessManager](OpenVDS::VolumeDataPageAccessor* acc)
  {
    if (!acc)
      return;
    acc->Commit();
    destinationAccessManager.DestroyVolumeDataPageAccessor(acc);
  };

  int64_t totalChunks = 0;
  int64_t doneChunks = 0;
  int percentage = 0;
  for (int lod = 0; lod <= layoutDescriptor.GetLODLevels(); lod++)
  {
    for (int dim = 0; dim <= OpenVDS::DimensionsND::Dimensions_45; dim++)
    {
      for (int channel = 0; channel < channelCount; channel++)
      {
        if (sourceAccessManager.GetVDSProduceStatus(OpenVDS::DimensionsND(dim), lod, channel) == OpenVDS::VDSProduceStatus::Normal)
        {
          totalChunks += sourceAccessManager.GetVDSChunkCount(OpenVDS::DimensionsND(dim), lod, channel);
        }
      }
    }
  }

  OpenVDS::printInfo(jsonOutput, destinationUrl, fmt::format("Copying {} to {}. Total chunks to copy is {}", sourceUrl, destinationUrl, totalChunks));
  if (!jsonOutput)
    fmt::print(stdout, "\n");
  ThreadPool threadPool(16);

  for (int lod = 0; lod <= layoutDescriptor.GetLODLevels(); lod++)
  {
    for (int dim = 0; dim <= OpenVDS::DimensionsND::Dimensions_45; dim++)
    {
      std::vector<std::unique_ptr<OpenVDS::VolumeDataPageAccessor, decltype(sourceAccessorDestroyer)>> sourceAccessors;
      sourceAccessors.reserve(channelCount);
      std::vector<std::unique_ptr<OpenVDS::VolumeDataPageAccessor, decltype(destinationAccessorDestroyer)>> destinationAccessors;
      destinationAccessors.reserve(channelCount);
      for (int channel = 0; channel < channelCount; channel++)
      {
        if (sourceAccessManager.GetVDSProduceStatus(OpenVDS::DimensionsND(dim), lod, channel) == OpenVDS::VDSProduceStatus::Normal)
        {
          sourceAccessors.emplace_back(std::unique_ptr<OpenVDS::VolumeDataPageAccessor, decltype(sourceAccessorDestroyer)>(sourceAccessManager.CreateVolumeDataPageAccessor(OpenVDS::DimensionsND(dim), lod, channel, OpenVDS::VolumeDataAccessManager::maxPagesDefault, OpenVDS::VolumeDataPageAccessor::AccessMode_ReadOnly), sourceAccessorDestroyer));
          destinationAccessors.emplace_back(std::unique_ptr<OpenVDS::VolumeDataPageAccessor, decltype(destinationAccessorDestroyer)>(destinationAccessManager.CreateVolumeDataPageAccessor(OpenVDS::DimensionsND(dim), lod, channel, OpenVDS::VolumeDataAccessManager::maxPagesDefault, OpenVDS::VolumeDataPageAccessor::AccessMode_Create), destinationAccessorDestroyer));
        }
        else
        {
          sourceAccessors.emplace_back(std::unique_ptr<OpenVDS::VolumeDataPageAccessor, decltype(sourceAccessorDestroyer)>(nullptr, sourceAccessorDestroyer));
          destinationAccessors.emplace_back(std::unique_ptr<OpenVDS::VolumeDataPageAccessor, decltype(destinationAccessorDestroyer)>(nullptr, destinationAccessorDestroyer));
        }
      }

      if (!sourceAccessors[0])
        continue;

      std::vector<std::future<CopyError>> futures[2];
      futures[0].reserve(32);
      futures[1].reserve(32);
      bool futureBuffer = false;
      CopyError copyError;
      for (int64_t chunk = 0; chunk < sourceAccessors[0]->GetChunkCount(); chunk++)
      {
        for (int channel = 0; channel < channelCount; channel++)
        {
          if (sourceAccessors[channel])
          {
            int64_t mappedChunk = sourceAccessors[channel]->GetMappedChunkIndex(chunk);
            if (sourceAccessors[channel]->GetPrimaryChannelChunkIndex(mappedChunk) == chunk)
            {
              if (futures[futureBuffer].size() == futures[futureBuffer].capacity())
              {
                futureBuffer = !futureBuffer;
                if (!flushFutureBufer(futures[futureBuffer], jsonOutput, totalChunks, doneChunks, percentage, copyError))
                {
                    if (!jsonOutput)
                      fprintf(stderr, "\n");
                    OpenVDS::printError(jsonOutput, "Failed to copy chunk ", copyError.message);
                    return copyError.code;
                }
              }
              futures[futureBuffer].emplace_back(threadPool.Enqueue([&sourceAccessors, channel, mappedChunk, &destinationAccessors, &channelDescriptors, lod, &layout]
              {
                CopyError retError;
                auto sourcePage = sourceAccessors[channel]->ReadPage(mappedChunk);
                auto error = sourcePage->GetError();
                if (error.errorCode)
                {
                  retError.code = error.errorCode;
                  retError.message = error.message;
                  return retError;
                }
                auto destinationPage = destinationAccessors[channel]->CreatePage(mappedChunk);
                int sourcePitch[6];
                auto sourceBuffer = sourcePage->GetBuffer(sourcePitch);
                int destinationPitch[6];
                auto destinationBuffer = destinationPage->GetWritableBuffer(destinationPitch);
                auto maxPitch = std::distance(sourcePitch, std::max_element(sourcePitch, &sourcePitch[6]));
                int pageMin[6];
                int pageMax[6];
                sourcePage->GetMinMax(pageMin, pageMax);
                int formatSize = GetVoxelFormatByteSize(channelDescriptors[channel].GetFormat());
                int bufferSize = sourcePitch[maxPitch] * OpenVDS::GetLODSize(pageMin[maxPitch], pageMax[maxPitch], lod) * formatSize * layout->GetChannelComponents(channel);
                assert(memcmp(sourcePitch, destinationPitch, sizeof(sourcePitch)) == 0);
                memcpy(destinationBuffer, sourceBuffer, bufferSize);
                sourcePage->Release();
                destinationPage->Release();
                return retError;
              }));
            }
          }
        }
      }
      for (auto& futuesInBuffer : futures)
      {
        if (!flushFutureBufer(futuesInBuffer, jsonOutput, totalChunks, doneChunks, percentage, copyError))
        {
          if (!jsonOutput)
            fprintf(stderr, "\n");
          OpenVDS::printError(jsonOutput, "Failed to copy chunk ", copyError.message);
          return copyError.code;
        }
      }
    }
  }

  if (!jsonOutput)
    fmt::print(stdout, "\r {:3}% Done.\n", 100);

  OpenVDS::printInfo(jsonOutput, destinationUrl, fmt::format("Successfully copied {} to {}", sourceUrl, destinationUrl));
  return EXIT_SUCCESS;
}