         0,                          // Region start
         num_tuple_partials,         // Region end
         0,                          // First segment ID
-        num_segments - 1);          // Last segment ID
+        num_segments);              // Last segment ID (one-past)
 }
 
 


        segment_offsets.push_back(consumed);
    }

    // Allocate simple offsets array and copy STL vector into it
    if (g_verbose) std::cout << "Segment offsets: ";
    h_segment_offsets = new Offset[segment_offsets.size()];
    for (int i = 0; i < segment_offsets.size(); ++i)
    {
        h_segment_offsets[i] = segment_offsets[i];
        if (g_verbose) std::cout << h_segment_offsets[i] << ", ";
    }
    if (g_verbose) std::cout << std::endl << std::endl;

    num_segments = segment_offsets.size() - 1;
}



        printf(", %.3f avg ms, %.3f billion items/s, %.3f logical GB/s", avg_millis, grate, gbandwidth);
    }

    // Device cleanup
    if (d_values) CubDebugExit(g_allocator.DeviceFree(d_values));
    if (d_segment_offsets) CubDebugExit(g_allocator.DeviceFree(d_segment_offsets));
    if (d_output) CubDebugExit(g_allocator.DeviceFree(d_output));
    if (d_temp_storage) CubDebugExit(g_allocator.DeviceFree(d_temp_storage));

    // Host cleanup
    if (h_values)           delete[] h_values;
    if (h_segment_offsets)  delete[] h_segment_offsets;
    if (h_reference)        delete[] h_reference;
}


