github.com/wzzhu/tensor@v0.9.24/defaultengine.go (about)

     1  package tensor
     2  
     3  import (
     4  	"github.com/pkg/errors"
     5  	"github.com/wzzhu/tensor/internal/execution"
     6  )
     7  
     8  // StdEng is the default execution engine that comes with the tensors. To use other execution engines, use the WithEngine construction option.
     9  type StdEng struct {
    10  	execution.E
    11  }
    12  
    13  // makeArray allocates a slice for the array
    14  func (e StdEng) makeArray(arr *array, t Dtype, size int) {
    15  	arr.Raw = malloc(t, size)
    16  	arr.t = t
    17  }
    18  
    19  func (e StdEng) AllocAccessible() bool             { return true }
    20  func (e StdEng) Alloc(size int64) (Memory, error)  { return nil, noopError{} }
    21  func (e StdEng) Free(mem Memory, size int64) error { return nil }
    22  func (e StdEng) Memset(mem Memory, val interface{}) error {
    23  	if ms, ok := mem.(MemSetter); ok {
    24  		return ms.Memset(val)
    25  	}
    26  	return errors.Errorf("Cannot memset %v with StdEng", mem)
    27  }
    28  
    29  func (e StdEng) Memclr(mem Memory) {
    30  	if z, ok := mem.(Zeroer); ok {
    31  		z.Zero()
    32  	}
    33  	return
    34  }
    35  
    36  func (e StdEng) Memcpy(dst, src Memory) error {
    37  	switch dt := dst.(type) {
    38  	case *array:
    39  		switch st := src.(type) {
    40  		case *array:
    41  			copyArray(dt, st)
    42  			return nil
    43  		case arrayer:
    44  			copyArray(dt, st.arrPtr())
    45  			return nil
    46  		}
    47  	case arrayer:
    48  		switch st := src.(type) {
    49  		case *array:
    50  			copyArray(dt.arrPtr(), st)
    51  			return nil
    52  		case arrayer:
    53  			copyArray(dt.arrPtr(), st.arrPtr())
    54  			return nil
    55  		}
    56  	}
    57  	return errors.Errorf("Failed to copy %T %T", dst, src)
    58  }
    59  
    60  func (e StdEng) Accessible(mem Memory) (Memory, error) { return mem, nil }
    61  
    62  func (e StdEng) WorksWith(order DataOrder) bool { return true }
    63  
    64  func (e StdEng) checkAccessible(t Tensor) error {
    65  	if !t.IsNativelyAccessible() {
    66  		return errors.Errorf(inaccessibleData, t)
    67  	}
    68  	return nil
    69  }