public static void ReferenceThenCopyThenAssignGpu() { const int n = 1000; var arrayA = GenerateRandomDoubleData(n, 1, 100); var arrayB = new double[n]; var cpuA = arrayA.AsTensor(); var cpuB = arrayB.AsTensor(); var gpuA = gpu.Device.Allocate <double>(Shape.Create(n)); var gpuB = gpu.Device.Allocate <double>(Shape.Create(n)); gpu.Copy(gpuA, cpuA); gpu.Assign(gpuB, gpuA); // this copy need to sync, since cpuB is just a reference gpu.Copy(cpuB, gpuB).Wait(); gpuB.Print(); Assert.IsTrue(arrayB.SequenceEqual(arrayA)); }
public static Tensor <T> Allocate <T>(this Context context, T[,,] initValues) { var shape = Shape.GetArrayShape(initValues); var tensor = context.Device.Allocate <T>(shape); var initTensor = initValues.AsTensor(); context.Copy(tensor, initTensor); return(tensor); }
public static T[,] ToArray2D <T>(this Context context, Tensor <T> tensor) { Util.EnsureTrue(tensor.Layout.Rank >= 2); if (!tensor.Layout.IsInnerChangeMostFullyPacked) { var tempTensor = context.Device.Allocate <T>(tensor.Layout, tensor.Buffer.Memory.Length); context.Copy(tempTensor, tensor).Wait(); tensor = tempTensor; } var rows = tensor.Layout.Shape[0]; var cols = tensor.Layout.Shape.Skip(1).Aggregate(ScalarOps.Mul); var array = new T[rows, cols]; var cpuTensor = array.AsTensor(); context.Copy(cpuTensor, tensor).Wait(); return(array); }
public static T[] ToArray <T>(this Context context, Tensor <T> tensor) { // if the tensor's layout is C Style, then we just make one copy and return it if (tensor.Layout.IsInnerChangeMostFullyPacked) { var layout = tensor.Layout; var shape = layout.Shape; var array = new T[shape.Length]; var cpuTensor = array.AsTensor(shape); context.Copy(cpuTensor, tensor).Wait(); return(array); } // not c style layout, we need allocate a temp tensor on that device, assign it, then copy it back throw new NotImplementedException(); }
public static void Print <T>(this Context context, Tensor <T> tensor, bool all = false) { Task task; // print must work on cpu side, if the tensor is on cpu side already, we // can directly print it (using RDE to sync the resource) if (tensor.Device == Device.CpuDevice) { task = new Task(() => tensor.Layout.Print(tensor.Buffer.RawReader, all)); } else { // if the tensor is not on cpu, then we need make a copy of it and print it. var cpuTensor = Device.CpuDevice.Allocate <T>(tensor.Layout, tensor.Memory.Length); context.Copy(cpuTensor, tensor).Wait(); task = new Task(() => tensor.Layout.Print(cpuTensor.Buffer.RawReader, all)); } // TODO:@RDE task.Start(); task.Wait(); }
public static Task Assign <T>(this Context context, Tensor <T> tensor, T[] newValues) { var newValuesTensor = newValues.AsTensor(); return(context.Copy(tensor, newValuesTensor)); }