-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Add support for vectorized null suppression for block serde #26919
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.trino.simd; | ||
|
||
import com.google.inject.Inject; | ||
import com.google.inject.Singleton; | ||
import io.trino.FeaturesConfig; | ||
import io.trino.spi.SimdSupport; | ||
import io.trino.util.MachineInfo; | ||
import jdk.incubator.vector.ByteVector; | ||
import jdk.incubator.vector.IntVector; | ||
import jdk.incubator.vector.LongVector; | ||
import jdk.incubator.vector.ShortVector; | ||
import oshi.hardware.CentralProcessor.ProcessorIdentifier; | ||
|
||
import java.util.EnumSet; | ||
import java.util.Set; | ||
|
||
import static io.trino.FeaturesConfig.BlockSerdeVectorizedNullSuppressionStrategy.AUTO; | ||
import static io.trino.FeaturesConfig.BlockSerdeVectorizedNullSuppressionStrategy.NONE; | ||
import static io.trino.util.MachineInfo.readCpuFlags; | ||
import static java.util.Locale.ENGLISH; | ||
|
||
@Singleton | ||
public final class BlockEncodingSimdSupport | ||
{ | ||
public static final int MINIMUM_SIMD_LENGTH = 512; | ||
private final SimdSupport simdSupport; | ||
|
||
public static final BlockEncodingSimdSupport TESTING_BLOCK_ENCODING_SIMD_SUPPORT = new BlockEncodingSimdSupport(new FeaturesConfig().setBlockSerdeVectorizedNullSuppressionStrategy(AUTO)); | ||
|
||
@Inject | ||
public BlockEncodingSimdSupport( | ||
FeaturesConfig featuresConfig) | ||
{ | ||
simdSupport = detectSimd(featuresConfig); | ||
} | ||
|
||
private static SimdSupport detectSimd(FeaturesConfig featuresConfig) | ||
{ | ||
if (featuresConfig.getBlockSerdeVectorizedNullSuppressionStrategy().equals(NONE)) { | ||
return SimdSupport.NONE; | ||
} | ||
|
||
ProcessorIdentifier id = MachineInfo.getProcessorInfo(); | ||
|
||
String vendor = id.getVendor().toLowerCase(ENGLISH); | ||
|
||
if (vendor.contains("intel") || vendor.contains("amd")) { | ||
return detectX86SimdSupport(); | ||
} | ||
|
||
return SimdSupport.NONE; | ||
} | ||
|
||
private static SimdSupport detectX86SimdSupport() | ||
{ | ||
enum X86Isa { | ||
avx512f, | ||
avx512vbmi2 | ||
} | ||
|
||
Set<String> flags = readCpuFlags(); | ||
EnumSet<X86Isa> x86Flags = EnumSet.noneOf(X86Isa.class); | ||
|
||
if (!flags.isEmpty()) { | ||
for (X86Isa isa : X86Isa.values()) { | ||
if (flags.contains(isa.name())) { | ||
x86Flags.add(isa); | ||
} | ||
} | ||
} | ||
|
||
return new SimdSupport( | ||
(ByteVector.SPECIES_PREFERRED.vectorBitSize() >= MINIMUM_SIMD_LENGTH) && x86Flags.contains(X86Isa.avx512vbmi2), | ||
(ShortVector.SPECIES_PREFERRED.vectorBitSize() >= MINIMUM_SIMD_LENGTH) && x86Flags.contains(X86Isa.avx512vbmi2), | ||
(IntVector.SPECIES_PREFERRED.vectorBitSize() >= MINIMUM_SIMD_LENGTH) && x86Flags.contains(X86Isa.avx512f), | ||
(LongVector.SPECIES_PREFERRED.vectorBitSize() >= MINIMUM_SIMD_LENGTH) && x86Flags.contains(X86Isa.avx512f)); | ||
} | ||
|
||
public SimdSupport getSimdSupport() | ||
{ | ||
return simdSupport; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,14 +14,25 @@ | |
package io.trino.util; | ||
|
||
import com.google.common.base.StandardSystemProperty; | ||
import com.google.common.collect.ImmutableSet; | ||
import oshi.SystemInfo; | ||
import oshi.hardware.CentralProcessor; | ||
import oshi.hardware.CentralProcessor.ProcessorIdentifier; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import static com.google.common.collect.ImmutableSet.toImmutableSet; | ||
import static java.lang.Math.min; | ||
import static java.util.Locale.ENGLISH; | ||
|
||
public final class MachineInfo | ||
{ | ||
// cache physical processor count, so that it's not queried multiple times during tests | ||
private static volatile int physicalProcessorCount = -1; | ||
private static final SystemInfo SYSTEM_INFO = new SystemInfo(); | ||
|
||
private MachineInfo() {} | ||
|
||
|
@@ -38,7 +49,7 @@ public static int getAvailablePhysicalProcessorCount() | |
if ("amd64".equals(osArch) || "x86_64".equals(osArch)) { | ||
// Oshi can recognize physical processor count (without hyper threading) for x86 platforms. | ||
// However, it doesn't correctly recognize physical processor count for ARM platforms. | ||
totalPhysicalProcessorCount = new SystemInfo() | ||
totalPhysicalProcessorCount = SYSTEM_INFO | ||
.getHardware() | ||
.getProcessor() | ||
.getPhysicalProcessorCount(); | ||
|
@@ -52,4 +63,67 @@ public static int getAvailablePhysicalProcessorCount() | |
physicalProcessorCount = min(totalPhysicalProcessorCount, availableProcessorCount); | ||
return physicalProcessorCount; | ||
} | ||
|
||
public static ProcessorIdentifier getProcessorInfo() | ||
{ | ||
return SYSTEM_INFO.getHardware().getProcessor().getProcessorIdentifier(); | ||
} | ||
|
||
public static Set<String> readCpuFlags() | ||
{ | ||
CentralProcessor cpu = SYSTEM_INFO.getHardware().getProcessor(); | ||
List<String> flags = cpu.getFeatureFlags(); | ||
if (flags == null || flags.isEmpty()) { | ||
return ImmutableSet.of(); | ||
} | ||
|
||
Set<String> intersection = null; | ||
|
||
for (String line : flags) { | ||
if (line == null || line.isBlank()) { | ||
continue; | ||
} | ||
|
||
// Strip the "flags:" / "Features:" prefix if present. | ||
String body = line; | ||
int colon = line.indexOf(':'); | ||
if (colon >= 0) { | ||
body = line.substring(colon + 1); | ||
} | ||
|
||
// Tokenize + normalize. | ||
Set<String> tokens = Arrays.stream(body.trim().split("\\s+")) | ||
.map(token -> normalizeFlag(token)) | ||
.filter(token -> !token.isEmpty()) | ||
.collect(toImmutableSet()); | ||
|
||
if (tokens.isEmpty()) { | ||
continue; | ||
} | ||
|
||
if (intersection == null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's going on with the intersection logic here? Is line of flags the hardware support for an individual core and we're looking for only flags advertised by all cores? If so, let's add an inline comment to that effect to make this easier to read. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure will do |
||
intersection = new HashSet<>(tokens); | ||
} | ||
else { | ||
intersection.retainAll(tokens); | ||
if (intersection.isEmpty()) { | ||
break; // nothing in common | ||
} | ||
} | ||
} | ||
|
||
return intersection == null ? ImmutableSet.of() : intersection; | ||
} | ||
|
||
public static String normalizeFlag(String flag) | ||
{ | ||
flag = flag.toLowerCase(ENGLISH).replace("_", "").trim(); | ||
|
||
// Skip stray keys that may sneak in if the colon wasn’t found. | ||
if (flag.equals("flags") || flag.equals("features")) { | ||
return ""; | ||
} | ||
|
||
return flag; | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.