forked from SciSharp/Tensor.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeConvert.cs
More file actions
24 lines (23 loc) · 1.16 KB
/
Copy pathTypeConvert.cs
File metadata and controls
24 lines (23 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using Tensornet.Native;
using Tensornet.Common;
using Tensornet.Native.Param;
namespace Tensornet{
public partial class Tensor<T>{
/// <summary>
/// Convert the tensor to the target type.
/// Please note that whether the target type is same of different, a new memory will be alloced. Therefore, it could alse be used as a copy method.
/// </summary>
/// <typeparam name="TD"></typeparam>
/// <returns></returns>
public Tensor<TD> ToTensor<TD>() where TD : struct, IEquatable<TD>, IConvertible{
Tensor<TD> res = new Tensor<TD>(new TensorLayout(TLayout, TensorTypeInfo.GetTypeInfo(typeof(TD))._dtype));
TypeConvertInternal(TMemory, res.TMemory, TLayout, res.TLayout);
return res;
}
private static unsafe void TypeConvertInternal(ITensorMemory mA, ITensorMemory mB, TensorLayout lA, TensorLayout lB){
TypeConvertParam param = new TypeConvertParam() { targetType = lB.DType };
IntPtr status = NativeExecutor.Execute(NativeApi.TypeConvert, mA, mB, lA, lB, new IntPtr(¶m), Tensor<T>.Provider);
NativeStatus.AssertOK(status);
}
}
}