zig-simd - 課題

概要

ZigのSIMD(Single Instruction, Multiple Data)機能を使用して、ベクトル演算を最適化する課題です。

マンダトリーパート

必須要件

  • ベクトル加算の実装
- f32配列の要素ごとの加算 - SIMD版と非SIMD版の両方を実装

  • ドット積の計算
- 2つのベクトルの内積を計算 - SIMD最適化版を実装

  • 行列乗算
- 4x4行列の乗算 - SIMDを活用した高速実装

インターフェース

const std = @import("std");

pub fn vectorAdd(a: []const f32, b: []const f32, result: []f32) void {
    // 実装
}

pub fn vectorAddSimd(a: []const f32, b: []const f32, result: []f32) void {
    // SIMD実装
}

pub fn dotProduct(a: []const f32, b: []const f32) f32 {
    // 実装
}

pub fn dotProductSimd(a: []const f32, b: []const f32) f32 {
    // SIMD実装
}

pub const Mat4 = struct {
    data: [4][4]f32,

    pub fn multiply(self: Mat4, other: Mat4) Mat4 {
        // 実装
    }

    pub fn multiplySimd(self: Mat4, other: Mat4) Mat4 {
        // SIMD実装
    }
};

使用例

const a = [_]f32{ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 };
const b = [_]f32{ 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0 };
var result: [8]f32 = undefined;

vectorAddSimd(&a, &b, &result);
// result = { 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0 }

const dot = dotProductSimd(&a, &b);
// dot = 120.0

ボーナスパート

ボーナス1: 画像処理

  • グレースケール変換のSIMD実装
  • RGB → Grayscale

ボーナス2: 文字列検索

  • SIMDを使用した高速文字列検索
  • memchrのSIMD版

ボーナス3: ベンチマーク

  • SIMD版と非SIMD版の性能比較
  • 結果のレポート出力
  • 提出要件

  • src/simd.zig - SIMD実装
  • src/main.zig - デモとベンチマーク
  • build.zig - ビルド設定
  • README.md - 使用方法と性能結果
  • 制限事項

  • @Vector 型を使用すること
  • インラインアセンブリは使用不可
  • 外部ライブラリは使用不可