121 class LegacyVtkWriter {
131 inline void head_write(std::string s) {
136 inline void write_buf(sycl::buffer<T> &buf,
u32 len,
u32 sum_len) {
137 if constexpr (shambase::VectorProperties<T>::is_float_based) {
138 details::write_buffer_vtktype<f32>(mfile, buf, len, sum_len,
false, file_head_ptr);
139 }
else if constexpr (shambase::VectorProperties<T>::is_int_based) {
140 details::write_buffer_vtktype<i32>(mfile, buf, len, sum_len,
false, file_head_ptr);
141 }
else if constexpr (shambase::VectorProperties<T>::is_uint_based) {
142 details::write_buffer_vtktype<i32>(mfile, buf, len, sum_len,
false, file_head_ptr);
147 inline void write_buf_no_buf(
u32 sum_len) {
148 if constexpr (shambase::VectorProperties<T>::is_float_based) {
149 details::write_buffer_vtktype_no_buf<f32, T>(mfile, sum_len,
false, file_head_ptr);
150 }
else if constexpr (shambase::VectorProperties<T>::is_int_based) {
151 details::write_buffer_vtktype_no_buf<i32, T>(mfile, sum_len,
false, file_head_ptr);
152 }
else if constexpr (shambase::VectorProperties<T>::is_uint_based) {
153 details::write_buffer_vtktype_no_buf<i32, T>(mfile, sum_len,
false, file_head_ptr);
158 inline std::string get_buf_type_name() {
159 if constexpr (shambase::VectorProperties<T>::is_float_based) {
161 }
else if constexpr (shambase::VectorProperties<T>::is_int_based) {
163 }
else if constexpr (shambase::VectorProperties<T>::is_uint_based) {
171 bool has_written_points =
false;
174 bool has_written_cells =
false;
177 inline LegacyVtkWriter(std::string fname,
bool binary, DataSetTypes type)
178 : fname(fname), binary(binary), file_head_ptr(0_u64) {
184 shamlog_debug_ln(
"VtkWriter",
"opening :", fname);
186 if (fname.find(
".vtk") == std::string::npos) {
188 "the extension should be .vtk");
193 std::stringstream ss;
196 ss << (
"# vtk DataFile Version 4.2\nvtk output\nBINARY\n");
198 ss << (
"# vtk DataFile Version 4.2\nvtk output\nASCII\n");
201 if (type == UnstructuredGrid) {
202 ss << (
"DATASET UNSTRUCTURED_GRID");
207 std::string write_str = ss.str();
209 head_write(write_str);
213 void write_points(sycl::buffer<sycl::vec<T, 3>> &buf,
u32 len) {
216 shamlog_debug_mpi_ln(
"VTK write",
"write_points");
218 u32 sum_len = shamalgs::collective::allreduce_sum(len);
220 std::stringstream ss;
223 ss <<
" " << get_buf_type_name<sycl::vec<T, 3>>();
226 head_write(ss.str());
228 write_buf(buf, len, sum_len);
230 has_written_points =
true;
231 points_count = sum_len;
235 void write_points_no_buf() {
238 shamlog_debug_mpi_ln(
"VTK write",
"write_points no buf");
240 u32 sum_len = shamalgs::collective::allreduce_sum(0);
242 std::stringstream ss;
245 ss <<
" " << get_buf_type_name<sycl::vec<T, 3>>();
248 head_write(ss.str());
250 write_buf_no_buf<T>(sum_len);
252 has_written_points =
true;
253 points_count = sum_len;
257 void write_points(std::unique_ptr<sycl::buffer<sycl::vec<T, 3>>> &buf,
u32 len) {
261 write_points_no_buf<T>();
266 void write_voxel_cells(
267 sycl::buffer<sycl::vec<T, 3>> &buf_min,
268 sycl::buffer<sycl::vec<T, 3>> &buf_max,
271 sycl::buffer<sycl::vec<T, 3>> pos_points(len * 8);
273 auto view = shamalgs::collective::fetch_view(len);
274 u32 sum_len = view.total_byte_count;
275 u32 len_offset = view.head_offset;
278 sycl::accessor acc_min{buf_min, cgh, sycl::read_only};
279 sycl::accessor acc_max{buf_max, cgh, sycl::read_only};
281 sycl::accessor acc_points{pos_points, cgh, sycl::write_only, sycl::no_init};
283 cgh.parallel_for(sycl::range<1>{len}, [=](sycl::item<1> id) {
284 u32 idx =
id.get_linear_id() * 8;
286 sycl::vec<T, 3> pmin = acc_min[id];
287 sycl::vec<T, 3> pmax = acc_max[id];
289 acc_points[idx + 0] = pmin;
290 acc_points[idx + 1] = {pmax.x(), pmin.y(), pmin.z()};
291 acc_points[idx + 2] = {pmin.x(), pmax.y(), pmin.z()};
292 acc_points[idx + 3] = {pmax.x(), pmax.y(), pmin.z()};
293 acc_points[idx + 4] = {pmin.x(), pmin.y(), pmax.z()};
294 acc_points[idx + 5] = {pmax.x(), pmin.y(), pmax.z()};
295 acc_points[idx + 6] = {pmin.x(), pmax.y(), pmax.z()};
296 acc_points[idx + 7] = pmax;
300 write_points(pos_points, len * 8);
302 std::stringstream ss;
305 ss <<
" " << sum_len * 9;
307 head_write(ss.str());
309 sycl::buffer<i32> idx_cells(len * 9);
310 sycl::buffer<i32> type_cell(len);
313 sycl::accessor idxs{idx_cells, cgh, sycl::write_only, sycl::no_init};
314 sycl::accessor cellt{type_cell, cgh, sycl::write_only, sycl::no_init};
316 u32 idp_off = len_offset * 8;
318 cgh.parallel_for(sycl::range<1>{len}, [=](sycl::item<1> item) {
319 u32 idp = item.get_linear_id() * 8;
320 u32 idx = item.get_linear_id() * 9;
323 idxs[idx + 1] = idp_off + idp + 0;
324 idxs[idx + 2] = idp_off + idp + 1;
325 idxs[idx + 3] = idp_off + idp + 2;
326 idxs[idx + 4] = idp_off + idp + 3;
327 idxs[idx + 5] = idp_off + idp + 4;
328 idxs[idx + 6] = idp_off + idp + 5;
329 idxs[idx + 7] = idp_off + idp + 6;
330 idxs[idx + 8] = idp_off + idp + 7;
336 write_buf(idx_cells, len * 9, sum_len * 9);
338 std::stringstream ss2;
339 ss2 <<
"\n\nCELL_TYPES ";
342 head_write(ss2.str());
344 write_buf(type_cell, len, sum_len);
346 cells_count = sum_len;
347 has_written_cells =
true;
350 void add_point_data_section() {
352 if (!has_written_points) {
354 "no points had been written");
357 std::stringstream ss;
358 ss <<
"\n\nPOINT_DATA ";
361 head_write(ss.str());
364 void add_cell_data_section() {
366 if (!has_written_cells) {
368 "no cells had been written");
371 std::stringstream ss;
372 ss <<
"\n\nCELL_DATA ";
375 head_write(ss.str());
378 void add_field_data_section(
u32 num_field) {
380 if (!has_written_points) {
382 "no points had been written");
385 std::stringstream ss;
386 ss <<
"\nFIELD FieldData ";
389 head_write(ss.str());
393 void write_field(std::string name, sycl::buffer<T> &buf,
u32 len) {
395 u32 sum_len = shamalgs::collective::allreduce_sum(len);
397 std::stringstream ss;
399 ss <<
" " << details::repr_count<T>;
400 ss <<
" " << sum_len;
401 ss <<
" " << get_buf_type_name<T>();
403 head_write(ss.str());
405 write_buf(buf, len, sum_len);
409 void write_field_no_buf(std::string name) {
411 u32 sum_len = shamalgs::collective::allreduce_sum(0);
413 std::stringstream ss;
415 ss <<
" " << details::repr_count<T>;
416 ss <<
" " << sum_len;
417 ss <<
" " << get_buf_type_name<T>();
419 head_write(ss.str());
421 write_buf_no_buf<T>(sum_len);
425 void write_field(std::string name, std::unique_ptr<sycl::buffer<T>> &buf,
u32 len) {
428 if (buf_ref.size() < len) {
430 "the buffer is smaller than expected write field size\n buf size = {}, "
435 write_field(name, buf_ref, len);
437 write_field_no_buf<T>(name);
441 inline ~LegacyVtkWriter() {
442 shamlog_debug_mpi_ln(
"LegacyVtkWriter",
"calling : shamcomm::mpi::File_close");
450 "dump to {}\n - took {}, bandwidth = {}/s",
452 timer.get_time_str(),
457 LegacyVtkWriter(
const LegacyVtkWriter &) =
delete;
458 LegacyVtkWriter &operator=(
const LegacyVtkWriter &) =
delete;
459 LegacyVtkWriter(LegacyVtkWriter &&other)
460 : mfile(other.mfile), fname(std::move(other.fname)), binary(other.binary),
461 file_head_ptr(other.file_head_ptr), points_count(other.points_count),
462 has_written_points(other.has_written_points), cells_count(other.cells_count),
463 has_written_cells(other.has_written_cells) {}
464 LegacyVtkWriter &operator=(LegacyVtkWriter &&other) =
delete;