| 1 |
#include <array> |
| 2 |
#include <cstdio> |
| 3 |
#include <cstdlib> |
| 4 |
#include <iostream> |
| 5 |
#include <memory> |
| 6 |
|
| 7 |
#define FLOAT float |
| 8 |
|
| 9 |
// |
| 10 |
// Given a big volume with dimension `dims` and logical indices `ijk`, |
| 11 |
// return a location in the 1D array representing the volume. |
| 12 |
// |
| 13 |
size_t translate_idx(const std::array<size_t, 3>& dims, std::array<size_t, 3> ijk) |
| 14 |
{ |
| 15 |
const auto plane = ijk[2] * dims[0] * dims[1]; |
| 16 |
const auto col = ijk[1] * dims[0]; |
| 17 |
return (ijk[0] + col + plane); |
| 18 |
} |
| 19 |
|
| 20 |
int main(int argc, char* argv[]) |
| 21 |
{ |
| 22 |
if (argc != 12) { |
| 23 |
std::cerr << "Usage: big_vol_name, big_vol_NX, big_vol_NY, big_vol_NZ, \n" |
| 24 |
<< "small_vol_name, small_vol_nx, small_vol_ny, small_vol_nz, \n" |
| 25 |
<< "put_at_big_x, Oput_at_big_y, put_at_big_z. " << std::endl; |
| 26 |
exit(1); |
| 27 |
} |
| 28 |
|
| 29 |
// |
| 30 |
// It is the user's responsibility to make sure the range and dimensions are |
| 31 |
// good. |
| 32 |
// |
| 33 |
auto big_dims = std::array<size_t, 3>{std::atol(argv[2]), std::atol(argv[3]), std::atol(argv[4])}; |
| 34 |
auto small_dims = |
| 35 |
std::array<size_t, 3>{std::atol(argv[6]), std::atol(argv[7]), std::atol(argv[8])}; |
| 36 |
auto big_n_vals = big_dims[0] * big_dims[1] * big_dims[2]; |
| 37 |
auto small_n_vals = small_dims[0] * small_dims[1] * small_dims[2]; |
| 38 |
size_t put_at_big_x = std::atol(argv[9]); |
| 39 |
size_t put_at_big_y = std::atol(argv[10]); |
| 40 |
size_t put_at_big_z = std::atol(argv[11]); |
| 41 |
|
| 42 |
// |
| 43 |
// Read in the big file |
| 44 |
// |
| 45 |
std::FILE* big_file = std::fopen(argv[1], "rb"); |
| 46 |
if (big_file == nullptr) { |
| 47 |
std::cerr << "big file open error: " << argv[1] << std::endl; |
| 48 |
exit(1); |
| 49 |
} |
| 50 |
auto big_buf = std::make_unique<FLOAT[]>(big_n_vals); |
| 51 |
auto cnt = std::fread(big_buf.get(), sizeof(FLOAT), big_n_vals, big_file); |
| 52 |
if (cnt != big_n_vals) { |
| 53 |
std::cerr << "big file read error: " << argv[1] << std::endl; |
| 54 |
exit(1); |
| 55 |
} |
| 56 |
std::fclose(big_file); |
| 57 |
|
| 58 |
// |
| 59 |
// Read in the small file |
| 60 |
// |
| 61 |
std::FILE* small_file = std::fopen(argv[5], "rb"); |
| 62 |
if (small_file == nullptr) { |
| 63 |
std::cerr << "small file open error: " << argv[5] << std::endl; |
| 64 |
exit(1); |
| 65 |
} |
| 66 |
auto small_buf = std::make_unique<FLOAT[]>(small_n_vals); |
| 67 |
cnt = std::fread(small_buf.get(), sizeof(FLOAT), small_n_vals, small_file); |
| 68 |
if (cnt != small_n_vals) { |
| 69 |
std::cerr << "small file read error: " << argv[5] << std::endl; |
| 70 |
exit(1); |
| 71 |
} |
| 72 |
std::fclose(small_file); |
| 73 |
|
| 74 |
// |
| 75 |
// Put values from the small file buffer to locations at the big file buffer. |
| 76 |
// |
| 77 |
size_t small_idx = 0; |
| 78 |
for (size_t z = put_at_big_z; z < put_at_big_z + small_dims[2]; z++) { |
| 79 |
for (size_t y = put_at_big_y; y < put_at_big_y + small_dims[1]; y++) { |
| 80 |
for (size_t x = put_at_big_x; x < put_at_big_x + small_dims[0]; x++) { |
| 81 |
auto big_idx = translate_idx(big_dims, {x, y, z}); |
| 82 |
big_buf[big_idx] = small_buf[small_idx++]; |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
// |
| 88 |
// Write back the big file buffer |
| 89 |
// |
| 90 |
big_file = std::fopen(argv[1], "wb"); |
| 91 |
if (big_file == nullptr) { |
| 92 |
std::cerr << "big file open error: " << argv[1] << std::endl; |
| 93 |
exit(1); |
| 94 |
} |
| 95 |
cnt = std::fwrite(big_buf.get(), sizeof(FLOAT), big_n_vals, big_file); |
| 96 |
if (cnt != big_n_vals) { |
| 97 |
std::cerr << "output file write error: " << argv[5] << std::endl; |
| 98 |
exit(1); |
| 99 |
} |
| 100 |
std::fclose(big_file); |
| 101 |
|
| 102 |
return 0; |
| 103 |
} |