How is this SIMD?

I am a beginner with ODIN and low-level programming. I would greatly appreciate any feedback on this SIMD code.

package main

import "core:fmt"
import "core:math/linalg"
import "core:simd"
import "base:intrinsics"
import "core:time"

// 1. Define our SIMD width to the largest power of 2 that fits (8 elements)
SIMD_WIDTH :: 8

Load_Types :: enum {
	DC1,
	DC2,
	DW,
	LL,
	WS,
	WL,
	TU,
	CDC1,
	CDC2,
	CLL,
	CWS,
}

Load_Vector :: [Load_Types]f64
Load_Vector_simd :: #simd[SIMD_WIDTH]f64
Load_Zero_Vector :: #partial Load_Vector{}
LOAD_VECTOR_LENGTH :: 11


main :: proc() {
	loads := Load_Vector {
		.DC1  = -55_680.0 * 12,
		.DC2  =  -4_958.0 * 12,
		.DW   =  -3_584.0 * 12,
		.LL   =   3_268.0 * 12,
		.WS   =   8_940.0 * 12,
		.WL   =      91.0 * 12,
		.TU   =     521.0 * 12,
		.CDC1 = -55_759.0 * 12,
		.CDC2 =       0.0 * 12,
		.CLL  =       0.0 * 12,
		.CWS  =   5_047.0 * 12,
	}
	fmt.printfln("%v", loads)

	max_factors := #partial Load_Vector {
		.DC1  = 1.25,
		.DC2  = 1.25,
		.DW   = 1.50,
		.LL   = 1.75,
		// .WS   = 0.00,
		// .WL   = 0.00,
		.TU   = 1.00,
		// .CDC1 = 0.00,
		// .CDC2 = 0.00,
		// .CLL  = 0.00,
		// .CWS  = 0.00,
	}
	fmt.printfln("%v", max_factors)

	min_factors := #partial Load_Vector {
		.DC1  = 0.90,
		.DC2  = 0.90,
		.DW   = 0.00,
		.LL   = 0.00,
		// .WS   = 0.00,
		// .WL   = 0.00,
		.TU   = 0.00,
		// .CDC1 = 0.00,
		// .CDC2 = 0.00,
		// .CLL  = 0.00,
		// .CWS  = 0.00,
	}
	fmt.printfln("%v", min_factors)

    sw: time.Stopwatch
    
    time.stopwatch_start(&sw)
    STRI_load := simd_factor_loads(&loads, &max_factors, &min_factors)
    time.stopwatch_stop(&sw)
    
    duration := time.stopwatch_duration(sw)
    fmt.printf("Elapsed time: %v\n", duration)


	fmt.printfln("STR_I Load: %v", STRI_load)

    time.stopwatch_start(&sw)
    STRI_load = calc_factor_loads(&loads, &max_factors, &min_factors)
    time.stopwatch_stop(&sw)
    duration = time.stopwatch_duration(sw)
    fmt.printf("Elapsed time: %v\n", duration)

	fmt.printfln("STR_I Load: %v", STRI_load)
	
}

simd_factor_loads :: proc(loads, max_factors, min_factors: ^Load_Vector) -> (factored_load: f64) {
    
    // Cast the first 8 elements into explicit SIMD types
    loads_simd := transmute(^Load_Vector_simd)loads
    max_simd := transmute(^Load_Vector_simd)max_factors
    min_simd := transmute(^Load_Vector_simd)min_factors
    zero_simd := Load_Vector_simd{}
    
    // Compute the absolute worst case maximum combination
    // Extract the additive (positive) and reductive (negative) parts
    pos_simd := simd.max(loads_simd^, zero_simd)
    neg_simd := simd.min(loads_simd^, zero_simd)

	// Apply max factors to positive loads, and min factors to negative loads
    factored_simd := simd.add(simd.mul(pos_simd, max_simd^), simd.mul(neg_simd, min_simd^))

    // Sum components to get the total worse case
    factored_load = simd.reduce_add_bisect(factored_simd)
    
    
    // Repeat steps for tail portion
    for i in SIMD_WIDTH..<LOAD_VECTOR_LENGTH {
    	i := cast(Load_Types)i
        pos_part := max(loads[i], 0.00)
        neg_part := min(loads[i], 0.00)
        factored_component := (pos_part * max_factors[i]) + (neg_part * min_factors[i]) 
        factored_load += factored_component
    }
    
    return factored_load
}

calc_factor_loads :: proc(loads, max_factors, min_factors: ^Load_Vector) -> (factored_load: f64) {
    
    for i in Load_Types {
        pos_part := max(loads[i], 0.00)
        neg_part := min(loads[i], 0.00)
        factored_component := (pos_part * max_factors[i]) + (neg_part * min_factors[i]) 
        factored_load += factored_component
    }
    
    return factored_load
}

It outputs the following on my machine:

[.DC1 = -668160, .DC2 = -59496, .DW = -43008, .LL = 39216, .WS = 107280, .WL = 1092, .TU = 6252, .CDC1 = -669108, .CDC2 = 0, .CLL = 0, .CWS = 60564]
[.DC1 = 1.25, .DC2 = 1.25, .DW = 1.5, .LL = 1.75, .WS = 0, .WL = 0, .TU = 1, .CDC1 = 0, .CDC2 = 0, .CLL = 0, .CWS = 0]
[.DC1 = 0.9, .DC2 = 0.9, .DW = 0, .LL = 0, .WS = 0, .WL = 0, .TU = 0, .CDC1 = 0, .CDC2 = 0, .CLL = 0, .CWS = 0]
Elapsed time: 84ns
STR_I Load: -580010.4
Elapsed time: 250ns
STR_I Load: -580010.4

It looks like it is speeding things up which is great. I am just looking for any tips.

1 Like

I haven’t used simd much firsthand, but here’s a tip I heard from Casey Muratori regarding the “tail” portion (the leftover part that doesn’t fit in the simd width). Instead of writing a second separate “scalar” portion of the code to deal with the tail, just fit those values into the main simd path and pad the remaining space with zero values.

The reason why you want to do this is that now you have only one implementation of the actual operation instead of two (a vector version and a scalar version) that could possibly differ from each other and cause a bug. And that bug would only show up if you had a workload with a tail portion, which may or may not be rare.

One caveat is that if you are writing very generic library code, where you don’t know if you have simd or not, then you will still need to write a scalar portion to handle the tail.

And if you haven’t yet, I would highly recommend reading Everyone Should Know SIMD – Mitchell Hashimoto.

2 Likes