名稱空間
變體
操作

資料並行型別 (SIMD) (C++26 起)

來自 cppreference.com
< cpp‎ | numeric
 
 
 
 

該庫提供了資料並行型別和對這些型別的操作:用於明確宣告資料並行性並透過資料並行執行資源(如 SIMD 暫存器和指令或由通用指令解碼器驅動的執行單元)構造資料的可移植型別。

所有標準整數型別、字元型別和大多數浮點型別都是可向量化型別。可向量化浮點型別包括 floatdouble,以及如果定義了的話,選定的擴充套件浮點型別 std::float16_tstd::float32_tstd::float64_t

一個資料並行型別由一個或多個底層可向量化型別(稱為元素型別)的元素組成。元素的數量(稱為寬度)對於每個資料並行型別都是恆定的。

資料並行型別指的是類模板 basic_simdbasic_simd_mask 的所有已啟用特化。

資料並行型別的一個資料並行物件的行為類似於 T 型別的物件。但是,雖然 T 儲存和操作單個值,但元素型別為 T 的資料並行型別儲存和操作多個值。

對資料並行物件的每個操作都是元素級的(除了水平操作,例如歸約,這些操作明確地標記為水平操作),應用於物件的每個元素或兩個物件的相應元素。每個這樣的應用與其他應用之間沒有順序。這個簡單的規則表達了資料並行性,並將由編譯器用於生成 SIMD 指令和/或獨立的執行流。

對資料並行物件的所有操作(非 constexpr 數學函式過載除外)都是 constexpr:可以在常量表達式的求值中建立和使用資料並行物件。

別名模板 simdsimd_mask 被定義為允許使用者將寬度指定為某個大小。預設寬度由實現在編譯時確定。

定義於標頭檔案 <simd>

目錄

[編輯] 主要類

資料並行向量型別
(類模板) [編輯]
(C++26)
basic_simd 的便捷別名模板,可以指定其寬度
(別名模板)[編輯]
元素型別為 bool 的資料並行型別
(類模板) [編輯]
(C++26)
basic_simd_mask 的便捷別名模板,可以指定其寬度
(別名模板)[編輯]

[編輯] 載入和儲存標誌

資料並行型別的載入和儲存標誌
(類模板) [編輯]
載入和儲存操作中使用的預設標誌
(常量) [編輯]
在載入和儲存操作中啟用非值保留轉換的標誌
(常量) [編輯]
標誌,指示載入-儲存地址與指定儲存的 simd_alignment 值對齊
(常量) [編輯]
標誌,指示載入-儲存地址與指定儲存的指定對齊方式對齊
(變數模板)[編輯]

[編輯] 載入和儲存操作

從連續範圍載入元素到 basic_simd
(函式模板) [編輯]
basic_simd 儲存元素到連續範圍
(函式模板) [編輯]

[編輯] 轉換

將單個數據並行物件拆分為多個
(函式模板) [編輯]
(C++26)
將多個數據並行物件連線成一個
(函式模板) [編輯]

[編輯] 演算法

basic_simd 的元素級 min/max 操作
(函式模板) [編輯]
(C++26)
basic_simd 的元素級 clamp 操作
(函式模板) [編輯]
使用條件運算子進行元素級選擇
(函式模板) [編輯]

[編輯] 歸約

basic_simd 中的所有值透過指定的二元操作歸約到單個值
(函式模板) [編輯]
basic_simd_mask 歸約到 bool
(函式模板) [編輯]
basic_simd_mask 歸約到 true 值的數量
(函式模板) [編輯]
basic_simd_mask 歸約到第一個或最後一個 true 值的索引
(函式模板) [編輯]

[編輯] 特性

獲取 simd_flag_aligned 的適當對齊方式
(類模板) [編輯]
更改資料並行型別的元素型別
(類模板) [編輯]
更改資料並行型別的寬度
(類模板) [編輯]

[編輯] 數學函式

<cmath> 中的所有函式都為 basic_simd 過載。

[編輯] 實現細節

[編輯] ABI 標籤

資料並行型別 basic_simdbasic_simd_maskABI 標籤關聯。這些標籤是指定資料並行物件大小和二進位制表示的型別。該設計旨在使大小和二進位制表示根據目標架構和編譯器標誌而變化。ABI 標籤與元素型別一起確定寬度。

ABI 標籤獨立於機器指令集選擇。所選的機器指令集限制了可用的 ABI 標籤型別。ABI 標籤使使用者能夠安全地跨翻譯單元邊界傳遞資料並行型別的物件。

[編輯] 僅供說明的實體

using /*simd-size-type*/ = /* 參閱描述 */;
(1) (僅作說明*)
template< std::size_t Bytes >
using /*integer-from*/ = /* 參閱描述 */;
(2) (僅作說明*)
template< class T, class Abi >
constexpr /*simd-size-type*/ /*simd-size-v*/ = /* 參閱描述 */;
(3) (僅作說明*)
template< class T >
constexpr std::size_t /*mask-element-size*/ = /* 參閱描述 */;
(4) (僅作說明*)
template< class T >
concept /*constexpr-wrapper-like*/ = /* 參閱描述 */;
(5) (僅作說明*)
template< class T >
using /*deduced-simd-t*/ = /* 參閱描述 */;
(6) (僅作說明*)
template< class V, class T >
using /*make-compatible-simd-t*/ = /* 參閱描述 */;
(7) (僅作說明*)
1) /*simd-size-type*/ 是一個有符號整數型別的別名。實現可以自由選擇任何有符號整數型別。
2) /*integer-from*/<Bytes> 是一個有符號整數型別 T 的別名,使得 sizeof(T) 等於 Bytes
3) /*simd-size-v*/<T, Abi> 表示已啟用特化 basic_simd<T, Abi> 的寬度,否則為 0
4) 如果 T 表示 std::basic_simd_mask<Bytes, Abi>,則 /*mask-element-size*/<T> 等於 Bytes
5) 概念 /*constexpr-wrapper-like*/ 定義為
template< class T >
concept /*constexpr-wrapper-like*/ =
    std::convertible_to<T, decltype(T::value)> &&
    std::equality_comparable_with<T, decltype(T::value)> &&
    std::bool_constant<T() == T::value>::value &&
    std::bool_constant<static_cast<decltype(T::value)>(T()) == T::value>::value;
6)xconst T 型別的左值。/*deduced-simd-t*/<T> 是一個等價於以下內容的別名:
  • decltype(x + x),如果 x + x 的型別是 basic_simd 的已啟用特化;否則為
  • void.
7)xconst T 型別的左值。/*make-compatible-simd-t*/<V, T> 是一個等價於以下內容的別名:
  • /*deduced-simd-t*/<T>,如果該型別不是 void,否則為
  • std::simd<decltype(x + x), V​::​size()>.
數學函式要求
template< class V >
concept /*simd-floating-point*/ = /* 參閱描述 */;
(8) (僅作說明*)
template< class... Ts >
concept /*math-floating-point*/ = /* 參閱描述 */;
(9) (僅作說明*)
template< class... Ts >

  requires /*math-floating-point*/<Ts...>

using /*math-common-simd-t*/ = /* 參閱描述 */;
(10) (僅作說明*)
template< class BinaryOp, class T >
concept /*reduction-binary-operation*/ = /* 參閱描述 */;
(11) (僅作說明*)
8) 概念 /*simd-floating-point*/ 定義為
template< class V >
concept /*simd-floating-point*/ =
    std::same_as<V, std::basic_simd<typename V::value_type, typename V::abi_type>> &&
    std::is_default_constructible_v<V> && 
    std::floating_point<typename V::value_type>;
9) 概念 /*math-floating-point*/ 定義為
template< class... Ts >
concept /*math-floating-point*/ =
    (/*simd-floating-point*/</*deduced-simd-t*/<Ts>> || ...);
10)T0 表示 Ts...[0]T1 表示 Ts...[1],而 TRest 表示一個包,使得 T0, T1, TRest... 等價於 Ts...。然後,/*math-common-simd-t*/<Ts...> 是一個等價於以下內容的別名:
  • /*deduced-simd-t*/<T0>,如果 sizeof...(Ts) == 1true
  • 否則,std::common_type_t</*deduced-simd-t*/<T0>, /*deduced-simd-t*/<T1>>,如果 sizeof...(Ts) == 2true/*math-floating-point*/<T0> && /*math-floating-point*/<T1>true
  • 否則,std::common_type_t</*deduced-simd-t*/<T0>, T1>,如果 sizeof...(Ts) == 2true/*math-floating-point*/<T0>true
  • 否則,std::common_type_t<T0, /*deduced-simd-t*/<T1>>,如果 sizeof...(Ts) == 2true
  • 否則,std::common_type_t</*math-common-simd-t*/<T0, T1>, TRest...>,如果 /*math-common-simd-t*/<T0, T1> 是一個有效型別,
  • 否則,std::common_type_t</*math-common-simd-t*/<TRest...>, T0, T1>
11) 概念 /*reduction-binary-operation*/ 定義為
template< class BinaryOp, class T >
concept /*reduction-binary-operation*/ =
    requires (const BinaryOp binary_op, const std::simd<T, 1> v) {
        { binary_op(v, v) } -> std::same_as<std::simd<T, 1>>;
    };

/*reduction-binary-operation*/<BinaryOp, T> 僅當滿足以下條件時才被建模:

  • BinaryOp 是一個可交換的二元元素級操作,且
  • 型別為 BinaryOp 的物件可使用兩個型別為 std::basic_simd<T, Abi> 的引數呼叫,其中 Abi 是未指定的 ABI 標籤,並返回一個 std::basic_simd<T, Abi>
SIMD ABI 標籤
template< class T >
using /*native-abi*/ = /* 參閱描述 */;
(12) (僅作說明*)
template< class T, /*simd-size-type*/ N >
using /*deduce-abi-t*/ = /* 參閱描述 */;
(13) (僅作說明*)
12) /*native-abi*/<T> 是一個實現定義的 ABI 標籤別名。這是用於高效顯式向量化的主要 ABI 標籤。因此,basic_simd<T, /*native-abi*/<T>> 是一個已啟用的特化。
13) /*deduce-abi-t*/<T, N> 是一個命名 ABI 標籤型別的別名,使得
  • /*simd-size-v*/<T, /*deduce-abi-t*/<T, N>> 等於 N
  • std::basic_simd<T, /*deduce-abi-t*/<T, N>> 是一個已啟用的特化,且
  • std::basic_simd_mask<sizeof(T), /*deduce-abi-t*/</*integer-from*/<sizeof(T)>, N>> 是一個已啟用的特化。
它僅當 T 是可向量化型別,並且 N > 0 && N <= Mtrue 時定義,其中 M 是一個實現定義的最小值,至少為 64,並且可能因 T 而異。
載入和儲存標誌
struct /*convert-flag*/;
(14) (僅作說明*)
struct /*aligned-flag*/;
(15) (僅作說明*)
template< std::size_t N >
struct /*overaligned-flag*/;
(16) (僅作說明*)
14-16) 這些標籤型別用作 std::simd_flags 的模板引數。有關其相應用法,請參閱載入和儲存標誌

[編輯] 注意

特性測試 標準 特性
__cpp_lib_simd 202411L (C++26) 資料並行型別和操作

[編輯] 示例

#include <iostream>
#include <simd>
#include <string_view>
 
void println(std::string_view name, auto const& a)
{
    std::cout << name << ": ";
    for (std::size_t i{}; i != a.size(); ++i)
        std::cout << a[i] << ' ';
    std::cout << '\n';
}
 
template<class A>
constexpr std::basic_simd<int, A> my_abs(std::basic_simd<int, A> x)
{
    return std::simd_select(x < 0, -x, x);
}
 
int main()
{
    constexpr std::simd<int> a = 1;
    println("a", a);
 
    constexpr std::simd<int> b([](int i) { return i - 2; });
    println("b", b);
 
    constexpr auto c = a + b;
    println("c", c);
 
    constexpr auto d = my_abs(c);
    println("d", d);
 
    constexpr auto e = d * d;
    println("e", e);
 
    constexpr auto inner_product = std::reduce(e);
    std::cout << "inner product: " << inner_product << '\n';
 
    constexpr std::simd<double, 16> x([](int i) { return i; });
    println("x", x);
    // overloaded math functions are defined in <simd>
    println("cos²(x) + sin²(x)", std::pow(std::cos(x), 2) + std::pow(std::sin(x), 2));
}

輸出

a: 1 1 1 1 
b: -2 -1 0 1 
c: -1 0 1 2 
d: 1 0 1 2 
e: 1 0 1 4 
inner product: 6
x: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 
cos²(x) + sin²(x): 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

[編輯] 參閱

數值陣列、陣列掩碼和陣列切片
(類模板) [編輯]

[編輯] 外部連結

1.  ISO/IEC TS 19570:2018 第 9 節“資料並行型別”的實現 — github.com
2.  TS 實現涵蓋 GCC/libstdc++ (std::experimental::simd 隨 GCC-11 釋出) — gcc.gnu.org