gitee.com/quant1x/num@v0.3.2/arithmetics_add.go (about)

     1  package num
     2  
     3  import (
     4  	"gitee.com/quant1x/num/x32"
     5  	"gitee.com/quant1x/num/x64"
     6  	//"github.com/mjibson/go-cuda"
     7  	"slices"
     8  )
     9  
    10  // Add arithmetics 加法
    11  func Add[T Number](x []T, y any) []T {
    12  	return v1Add(x, y)
    13  }
    14  
    15  func v1Add[T Number](x []T, y any) []T {
    16  	return BinaryOperations(x, y, x32.Add, x64.Add, __add_go[T])
    17  }
    18  
    19  func __add_go[T Number](x, y []T) []T {
    20  	x = slices.Clone(x)
    21  	for i := 0; i < len(x); i++ {
    22  		x[i] += y[i]
    23  	}
    24  	return x
    25  }
    26  
    27  //func v2AddFloat64(x, y []float64) []float64 {
    28  //	cuda.Init(0)
    29  //	defer cuda.Cleanup()
    30  //
    31  //	size := 1024
    32  //	a := cuda.MakeFloat32s(size)
    33  //	defer cuda.Free(unsafe.Pointer(a))
    34  //
    35  //	b := cuda.MakeFloat32s(size)
    36  //	defer cuda.Free(unsafe.Pointer(b))
    37  //
    38  //	c := cuda.MakeFloat32s(size)
    39  //	defer cuda.Free(unsafe.Pointer(c))
    40  //
    41  //	for i := 0; i < size; i++ {
    42  //		a[i] = float32(i)
    43  //		b[i] = float32(size - i)
    44  //	}
    45  //
    46  //	add := cuda.NewFunction(`
    47  //        __global__ void add(float *a, float *b, float *c) {
    48  //            int i = threadIdx.x;
    49  //            c[i] = a[i] + b[i];
    50  //        }
    51  //    `)
    52  //
    53  //	add.Launch(size, 1, 1, a, b, c)
    54  //
    55  //	for i := 0; i < size; i++ {
    56  //		fmt.Printf("%.2f + %.2f = %.2f\n", a[i], b[i], c[i])
    57  //	}
    58  //}