@@ -94,7 +94,8 @@ GlobalOrderWriter::GlobalOrderWriter(
9494 , processed_conditions_(processed_conditions)
9595 , fragment_size_(fragment_size)
9696 , current_fragment_size_(0 )
97- , rows_written_(0 ) {
97+ , rows_written_(0 )
98+ , start_(0 ) {
9899 // Check the layout is global order.
99100 if (layout_ != Layout::GLOBAL_ORDER) {
100101 throw GlobalOrderWriterException (
@@ -1463,16 +1464,6 @@ NDRange GlobalOrderWriter::ndranges_after_split(uint64_t num) {
14631464
14641465 // Calculate how many tiles each row can hold
14651466 uint64_t tiles_per_row = num_tiles_per_row (domain);
1466- auto dim_num = domain.dim_num ();
1467- auto dim{domain.dimension_ptr (0 )};
1468-
1469- auto l = [&](auto T) {
1470- return static_cast <uint64_t >(dim->tile_extent ().rvalue_as <decltype (T)>());
1471- };
1472-
1473- uint64_t tile_extent = apply_with_type (l, dim->type ());
1474- NDRange nd;
1475- nd.reserve (dim_num);
14761467
14771468 if (num % tiles_per_row != 0 ) {
14781469 throw GlobalOrderWriterException (
@@ -1484,13 +1475,33 @@ NDRange GlobalOrderWriter::ndranges_after_split(uint64_t num) {
14841475 // Calculate how many rows we will write in the current fragment
14851476 uint64_t rows_of_tiles_to_write = num / tiles_per_row;
14861477
1487- // Create the range for the index dim (first).
1488- int start = rows_written_ * tile_extent;
1489- int end = start + (rows_of_tiles_to_write * tile_extent) - 1 ;
1490- Range range (&start, &end, sizeof (int ));
1478+ // Create NDRange object and reserve for dims
1479+ auto dim_num = domain.dim_num ();
1480+ NDRange nd;
1481+ nd.reserve (dim_num);
1482+
1483+ // Calculate the range for the index dim (first).
1484+ auto dim{domain.dimension_ptr (0 )};
1485+ auto dim_dom = dim->domain ();
1486+ auto l = [&](auto T) {
1487+ return static_cast <uint64_t >(dim->tile_extent ().rvalue_as <decltype (T)>());
1488+ };
1489+ uint64_t tile_extent = apply_with_type (l, dim->type ());
1490+
1491+ // Calculate start and end
1492+ if (rows_written_ == 0 ) {
1493+ // It means that the start has not been set yet. Set it to the minimum value
1494+ // of the expanded domain for that dim
1495+ auto dim_dom_data = (const uint64_t *)dim_dom.data ();
1496+ start_ = dim_dom_data[0 ];
1497+ }
1498+ uint64_t end = start_ + (rows_of_tiles_to_write * tile_extent) - 1 ;
1499+
1500+ // Add range
1501+ Range range (&start_, &end, sizeof (int ));
14911502 nd.emplace_back (range);
14921503
1493- // Use the domain as ranges for the rest of the dims
1504+ // For the rest of the dims, use their domains as ranges. No split there.
14941505 for (unsigned d = 1 ; d < dim_num; ++d) {
14951506 // begin from second dim
14961507 auto dim{array_schema_.dimension_ptr (d)};
@@ -1500,6 +1511,7 @@ NDRange GlobalOrderWriter::ndranges_after_split(uint64_t num) {
15001511
15011512 // add rows written to the cache
15021513 rows_written_ += rows_of_tiles_to_write;
1514+ start_ = end + 1 ;
15031515
15041516 return nd;
15051517}
0 commit comments