github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/vector/generate/example.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  )
     7  
     8  func main() {
     9  	ctx := NewNaive(Config{
    10  		Package: "f32",
    11  		Type: Type{
    12  			Name: "float32",
    13  			Size: 4,
    14  		},
    15  		Unroll:  4,
    16  		Pointer: true,
    17  	})
    18  
    19  	All(ctx)
    20  
    21  	for _, file := range ctx.Files {
    22  		fmt.Println("===", file.Path, "===")
    23  
    24  		content, err := file.Formatted()
    25  		if err != nil {
    26  			fmt.Fprintln(os.Stderr, err)
    27  		}
    28  		fmt.Println(string(content))
    29  	}
    30  }
    31  
    32  func All(ctx Context) {
    33  	Axpy(ctx.In("axpy.go"))
    34  	Scale(ctx.In("scale.go"))
    35  }
    36  
    37  func Axpy(ctx File) {
    38  	ctx.Func("AxpyUnitary(alpha $Type, xs, ys []$Type)",
    39  		Iterate{
    40  			Ranges: []It{
    41  				Slice("ys", 0, 1),
    42  				Slice("xs", 0, 1),
    43  			},
    44  			For: "$ys += alpha * $xs",
    45  		})
    46  
    47  	ctx.Func("AxpyUnitaryTo(dst []$Type, alpha $Type, xs, ys []$Type)",
    48  		Iterate{
    49  			Ranges: []It{
    50  				Slice("dst", 0, 1),
    51  				Slice("ys", 0, 1),
    52  				Slice("xs", 0, 1),
    53  			},
    54  			For: "$dst = $ys + alpha * $xs",
    55  		})
    56  
    57  	ctx.Func("AxpyIncTo(dst []$Type, incDst, idst uintptr, alpha $Type, xs, ys []$Type, n, incx, incy, ix, iy uintptr)",
    58  		Iterate{
    59  			Ranges: []It{
    60  				Range("i", 0, "n"),
    61  				Slice("dst", "idst", "incDst"),
    62  				Slice("xs", "ix", "incx"),
    63  				Slice("ys", "iy", "incy"),
    64  			},
    65  			For: "$dst = $ys + alpha * $xs",
    66  		})
    67  }
    68  
    69  func Scale(ctx File) {
    70  	ctx.Func("ScalIncUnitaryTo(dst []$Type, incdst uintptr, alpha $Type, xs []$Type, n, incx uintptr)",
    71  		Iterate{
    72  			Ranges: []It{
    73  				Range("i", 0, "n"),
    74  				Slice("dst", 0, "incdst"),
    75  				Slice("xs", 0, "incx"),
    76  			},
    77  			For: "$dst = alpha * $xs",
    78  		})
    79  }