130 inline void head_write(std::string s) {
131 shamalgs::collective::write_header_raw(mfile, s, file_head_ptr);
135 inline void write_buf(sycl::buffer<T> &buf,
u32 len,
u32 sum_len) {
137 details::write_buffer_vtktype<f32>(mfile, buf, len, sum_len,
false, file_head_ptr);
139 details::write_buffer_vtktype<i32>(mfile, buf, len, sum_len,
false, file_head_ptr);
141 details::write_buffer_vtktype<i32>(mfile, buf, len, sum_len,
false, file_head_ptr);
146 inline void write_buf_no_buf(
u32 sum_len) {
148 details::write_buffer_vtktype_no_buf<f32, T>(mfile, sum_len,
false, file_head_ptr);
150 details::write_buffer_vtktype_no_buf<i32, T>(mfile, sum_len,
false, file_head_ptr);
152 details::write_buffer_vtktype_no_buf<i32, T>(mfile, sum_len,
false, file_head_ptr);
157 inline std::string get_buf_type_name() {
170 bool has_written_points =
false;
173 bool has_written_cells =
false;
176 inline LegacyVtkWriter(std::string fname,
bool binary, DataSetTypes type)
177 : fname(fname), binary(binary), file_head_ptr(0_u64) {
183 shamlog_debug_ln(
"VtkWriter",
"opening :", fname);
185 if (fname.find(
".vtk") == std::string::npos) {
187 "the extension should be .vtk");
192 std::stringstream ss;
195 ss << (
"# vtk DataFile Version 4.2\nvtk output\nBINARY\n");
197 ss << (
"# vtk DataFile Version 4.2\nvtk output\nASCII\n");
200 if (type == UnstructuredGrid) {
201 ss << (
"DATASET UNSTRUCTURED_GRID");
206 std::string write_str = ss.str();
208 head_write(write_str);
212 void write_points(sycl::buffer<sycl::vec<T, 3>> &buf,
u32 len) {
215 shamlog_debug_mpi_ln(
"VTK write",
"write_points");
217 u32 sum_len = shamalgs::collective::allreduce_sum(len);
219 std::stringstream ss;
222 ss <<
" " << get_buf_type_name<sycl::vec<T, 3>>();
225 head_write(ss.str());
227 write_buf(buf, len, sum_len);
229 has_written_points =
true;
230 points_count = sum_len;
234 void write_points_no_buf() {
237 shamlog_debug_mpi_ln(
"VTK write",
"write_points no buf");
239 u32 sum_len = shamalgs::collective::allreduce_sum(0);
241 std::stringstream ss;
244 ss <<
" " << get_buf_type_name<sycl::vec<T, 3>>();
247 head_write(ss.str());
249 write_buf_no_buf<T>(sum_len);
251 has_written_points =
true;
252 points_count = sum_len;
256 void write_points(std::unique_ptr<sycl::buffer<sycl::vec<T, 3>>> &buf,
u32 len) {
260 write_points_no_buf<T>();
265 void write_voxel_cells(
266 sycl::buffer<sycl::vec<T, 3>> &buf_min,
267 sycl::buffer<sycl::vec<T, 3>> &buf_max,
270 sycl::buffer<sycl::vec<T, 3>> pos_points(len * 8);
272 auto view = shamalgs::collective::fetch_view(len);
273 u32 sum_len = view.total_byte_count;
274 u32 len_offset = view.head_offset;
276 shamsys::instance::get_compute_queue().submit([&](sycl::handler &cgh) {
277 sycl::accessor acc_min{buf_min, cgh, sycl::read_only};
278 sycl::accessor acc_max{buf_max, cgh, sycl::read_only};
280 sycl::accessor acc_points{pos_points, cgh, sycl::write_only, sycl::no_init};
282 cgh.parallel_for(sycl::range<1>{len}, [=](sycl::item<1> id) {
283 u32 idx =
id.get_linear_id() * 8;
285 sycl::vec<T, 3> pmin = acc_min[id];
286 sycl::vec<T, 3> pmax = acc_max[id];
288 acc_points[idx + 0] = pmin;
289 acc_points[idx + 1] = {pmax.x(), pmin.y(), pmin.z()};
290 acc_points[idx + 2] = {pmin.x(), pmax.y(), pmin.z()};
291 acc_points[idx + 3] = {pmax.x(), pmax.y(), pmin.z()};
292 acc_points[idx + 4] = {pmin.x(), pmin.y(), pmax.z()};
293 acc_points[idx + 5] = {pmax.x(), pmin.y(), pmax.z()};
294 acc_points[idx + 6] = {pmin.x(), pmax.y(), pmax.z()};
295 acc_points[idx + 7] = pmax;
299 write_points(pos_points, len * 8);
301 std::stringstream ss;
304 ss <<
" " << sum_len * 9;
306 head_write(ss.str());
308 sycl::buffer<i32> idx_cells(len * 9);
309 sycl::buffer<i32> type_cell(len);
311 shamsys::instance::get_compute_queue().submit([&](sycl::handler &cgh) {
312 sycl::accessor idxs{idx_cells, cgh, sycl::write_only, sycl::no_init};
313 sycl::accessor cellt{type_cell, cgh, sycl::write_only, sycl::no_init};
315 u32 idp_off = len_offset * 8;
317 cgh.parallel_for(sycl::range<1>{len}, [=](sycl::item<1> item) {
318 u32 idp = item.get_linear_id() * 8;
319 u32 idx = item.get_linear_id() * 9;
322 idxs[idx + 1] = idp_off + idp + 0;
323 idxs[idx + 2] = idp_off + idp + 1;
324 idxs[idx + 3] = idp_off + idp + 2;
325 idxs[idx + 4] = idp_off + idp + 3;
326 idxs[idx + 5] = idp_off + idp + 4;
327 idxs[idx + 6] = idp_off + idp + 5;
328 idxs[idx + 7] = idp_off + idp + 6;
329 idxs[idx + 8] = idp_off + idp + 7;
335 write_buf(idx_cells, len * 9, sum_len * 9);
337 std::stringstream ss2;
338 ss2 <<
"\n\nCELL_TYPES ";
341 head_write(ss2.str());
343 write_buf(type_cell, len, sum_len);
345 cells_count = sum_len;
346 has_written_cells =
true;
349 void add_point_data_section() {
351 if (!has_written_points) {
353 "no points had been written");
356 std::stringstream ss;
357 ss <<
"\n\nPOINT_DATA ";
360 head_write(ss.str());
363 void add_cell_data_section() {
365 if (!has_written_cells) {
367 "no cells had been written");
370 std::stringstream ss;
371 ss <<
"\n\nCELL_DATA ";
374 head_write(ss.str());
377 void add_field_data_section(
u32 num_field) {
379 if (!has_written_points) {
381 "no points had been written");
384 std::stringstream ss;
385 ss <<
"\nFIELD FieldData ";
388 head_write(ss.str());
392 void write_field(std::string name, sycl::buffer<T> &buf,
u32 len) {
394 u32 sum_len = shamalgs::collective::allreduce_sum(len);
396 std::stringstream ss;
398 ss <<
" " << details::repr_count<T>;
399 ss <<
" " << sum_len;
400 ss <<
" " << get_buf_type_name<T>();
402 head_write(ss.str());
404 write_buf(buf, len, sum_len);
408 void write_field_no_buf(std::string name) {
410 u32 sum_len = shamalgs::collective::allreduce_sum(0);
412 std::stringstream ss;
414 ss <<
" " << details::repr_count<T>;
415 ss <<
" " << sum_len;
416 ss <<
" " << get_buf_type_name<T>();
418 head_write(ss.str());
420 write_buf_no_buf<T>(sum_len);
424 void write_field(std::string name, std::unique_ptr<sycl::buffer<T>> &buf,
u32 len) {
427 if (buf_ref.size() < len) {
429 "the buffer is smaller than expected write field size\n buf size = {}, "
434 write_field(name, buf_ref, len);
436 write_field_no_buf<T>(name);
441 shamlog_debug_mpi_ln(
"LegacyVtkWriter",
"calling : shamcomm::mpi::File_close");
442 shamcomm::mpi::File_close(&mfile);
449 "dump to {}\n - took {}, bandwidth = {}/s",
459 : mfile(other.mfile), fname(std::move(other.fname)), binary(other.binary),
460 file_head_ptr(other.file_head_ptr) {}