|
| 1 | +""" |
| 2 | + Copyright (c) 2022, UChicago Argonne, LLC |
| 3 | + All Rights Reserved |
| 4 | +
|
| 5 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + you may not use this file except in compliance with the License. |
| 7 | + You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | + Unless required by applicable law or agreed to in writing, software |
| 12 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + See the License for the specific language governing permissions and |
| 15 | + limitations under the License. |
| 16 | +""" |
| 17 | + |
| 18 | +from dlio_benchmark.common.enumerations import Compression |
| 19 | +from dlio_benchmark.data_generator.data_generator import DataGenerator |
| 20 | + |
| 21 | +import logging |
| 22 | +import numpy as np |
| 23 | + |
| 24 | +from dlio_benchmark.utils.utility import progress, utcnow |
| 25 | +from dlio_profiler.logger import fn_interceptor as Profile |
| 26 | +from shutil import copyfile |
| 27 | +from dlio_benchmark.common.constants import MODULE_DATA_GENERATOR |
| 28 | +import struct |
| 29 | + |
| 30 | +dlp = Profile(MODULE_DATA_GENERATOR) |
| 31 | + |
| 32 | +""" |
| 33 | +Generator for creating data in NPZ format. |
| 34 | +""" |
| 35 | +class IndexedBinaryGenerator(DataGenerator): |
| 36 | + def __init__(self): |
| 37 | + super().__init__() |
| 38 | + |
| 39 | + def index_file_path_off(self, prefix_path): |
| 40 | + return prefix_path + '.off.idx' |
| 41 | + |
| 42 | + def index_file_path_size(self, prefix_path): |
| 43 | + return prefix_path + '.sz.idx' |
| 44 | + |
| 45 | + @dlp.log |
| 46 | + def generate(self): |
| 47 | + """ |
| 48 | + Generator for creating data in NPZ format of 3d dataset. |
| 49 | + """ |
| 50 | + super().generate() |
| 51 | + np.random.seed(10) |
| 52 | + GB=1073741824 |
| 53 | + for i in dlp.iter(range(self.my_rank, int(self.total_files_to_generate), self.comm_size)): |
| 54 | + dim1, dim2 = self.get_dimension() |
| 55 | + sample_size = dim1 * dim2 |
| 56 | + total_size = sample_size * self.num_samples |
| 57 | + write_size = total_size |
| 58 | + memory_size = self._args.generation_buffer_size |
| 59 | + if total_size > memory_size: |
| 60 | + write_size = memory_size - (memory_size % sample_size) |
| 61 | + out_path_spec = self.storage.get_uri(self._file_list[i]) |
| 62 | + out_path_spec_off_idx = self.index_file_path_off(out_path_spec) |
| 63 | + out_path_spec_sz_idx = self.index_file_path_size(out_path_spec) |
| 64 | + progress(i + 1, self.total_files_to_generate, "Generating Indexed Binary Data") |
| 65 | + prev_out_spec = out_path_spec |
| 66 | + written_bytes = 0 |
| 67 | + data_file = open(out_path_spec, "wb") |
| 68 | + off_file = open(out_path_spec_off_idx, "wb") |
| 69 | + sz_file = open(out_path_spec_sz_idx, "wb") |
| 70 | + records = np.random.randint(255, size=write_size, dtype=np.uint8) |
| 71 | + while written_bytes < total_size: |
| 72 | + data_to_write = write_size if written_bytes + write_size <= total_size else total_size - written_bytes |
| 73 | + samples_to_write = data_to_write // sample_size |
| 74 | + |
| 75 | + # Write data |
| 76 | + myfmt = 'B' * data_to_write |
| 77 | + binary_data = struct.pack(myfmt, *records[:data_to_write]) |
| 78 | + data_file.write(binary_data) |
| 79 | + |
| 80 | + # Write offsets |
| 81 | + myfmt = 'Q' * samples_to_write |
| 82 | + offsets = range(0, data_to_write, sample_size) |
| 83 | + offsets = offsets[:samples_to_write] |
| 84 | + binary_offsets = struct.pack(myfmt, *offsets) |
| 85 | + off_file.write(binary_offsets) |
| 86 | + |
| 87 | + # Write sizes |
| 88 | + myfmt = 'Q' * samples_to_write |
| 89 | + sample_sizes = [sample_size] * samples_to_write |
| 90 | + binary_sizes = struct.pack(myfmt, *sample_sizes) |
| 91 | + sz_file.write(binary_sizes) |
| 92 | + |
| 93 | + written_bytes = written_bytes + data_to_write |
| 94 | + data_file.close() |
| 95 | + off_file.close() |
| 96 | + sz_file.close() |
| 97 | + np.random.seed() |
0 commit comments