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

     1  // +build ignore
     2  // +build !race
     3  
     4  package tensor
     5  
     6  import (
     7  	"testing"
     8  	"unsafe"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  // This test will fail the `go test -race`.
    14  //
    15  // This is because FromMemory() will use uintptr in a way that is incorrect according to the checkptr directive of Go 1.14+
    16  //
    17  // Though it's incorrect, it's the only way to use heterogenous, readable memory (i.e. CUDA).
    18  func TestFromMemory(t *testing.T) {
    19  	// dummy memory - this could be an externally malloc'd memory, or a mmap'ed file.
    20  	// but here we're just gonna let Go manage memory.
    21  	s := make([]float64, 100)
    22  	ptr := uintptr(unsafe.Pointer(&s[0]))
    23  	size := uintptr(100 * 8)
    24  
    25  	T := New(Of(Float32), WithShape(50, 4), FromMemory(ptr, size))
    26  	if len(T.Float32s()) != 200 {
    27  		t.Error("expected 200 Float32s")
    28  	}
    29  	assert.Equal(t, make([]float32, 200), T.Data())
    30  	assert.True(t, T.IsManuallyManaged(), "Unamanged %v |%v | q: %v", ManuallyManaged, T.flag, (T.flag>>ManuallyManaged)&MemoryFlag(1))
    31  
    32  	fail := func() { New(FromMemory(ptr, size), Of(Float32)) }
    33  	assert.Panics(t, fail, "Expected bad New() call to panic")
    34  }