Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
N
NiuTrans.Tensor
概览
Overview
Details
Activity
Cycle Analytics
版本库
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
问题
0
Issues
0
列表
Board
标记
里程碑
合并请求
0
Merge Requests
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
Snippets
成员
Collapse sidebar
Close sidebar
活动
图像
聊天
创建新问题
作业
提交
Issue Boards
Open sidebar
杨迪
NiuTrans.Tensor
Commits
3d31727d
Commit
3d31727d
authored
Apr 03, 2019
by
xiaotong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add Get and Set methods to fetch and save int-type items
parent
fe2e0d79
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
65 行增加
和
36 行删除
+65
-36
source/tensor/XTensor.cpp
+65
-36
没有找到文件。
source/tensor/XTensor.cpp
查看文件 @
3d31727d
...
...
@@ -1117,15 +1117,29 @@ DTYPE XTensor::Get3D(int d0, int d1, int d2)
}
/*
get the int value of a cell by its offset
>> offset - offset of the item
*/
int
XTensor
::
GetInt
(
int
offset
)
{
CheckNTErrors
(
offset
>=
0
&&
offset
<
unitNum
,
"Invalid index!"
);
CheckNTErrors
(
data
!=
NULL
,
"Cannot use an uninitialized tensor!"
);
int
*
value
=
(
int
*
)
data
+
offset
;
return
ToCPUInt
(
devID
,
value
);
}
/*
get the value of a cell in a 1d tensor in int type
>> i - index
<< return - value of cell(i) in int
*/
int
XTensor
::
Get1DInt
(
int
i
)
{
CheckNTErrors
(
(
order
==
1
)
,
"Cannot get a 2d cell for a tensor whose order is not 2!"
);
CheckNTErrors
(
(
i
>=
0
&&
i
<
dimSize
[
0
])
,
"dimension 0 is out of range!"
);
CheckNTErrors
(
(
dataType
==
X_INT
)
,
"The tensor is not in int type."
);
CheckNTErrors
(
order
==
1
,
"Cannot get a 2d cell for a tensor whose order is not 2!"
);
CheckNTErrors
(
i
>=
0
&&
i
<
dimSize
[
0
]
,
"dimension 0 is out of range!"
);
CheckNTErrors
(
dataType
==
X_INT
,
"The tensor is not in int type."
);
int
dimSize
[
1
]
=
{
i
};
void
*
value
=
GetCell
(
dimSize
,
1
);
...
...
@@ -1141,10 +1155,10 @@ get the value of a cell in a 2d tensor in int type
*/
int
XTensor
::
Get2DInt
(
int
ni
,
int
mi
)
{
CheckNTErrors
(
(
order
==
2
)
,
"Cannot get a 2d cell for a tensor whose order is not 2!"
);
CheckNTErrors
(
(
ni
>=
0
&&
ni
<
dimSize
[
0
])
,
"dimension 0 is out of range!"
);
CheckNTErrors
(
(
mi
>=
0
&&
mi
<
dimSize
[
1
])
,
"dimension 1 is out of range!"
);
CheckNTErrors
(
(
dataType
==
X_INT
)
,
"The tensor is not in default type."
);
CheckNTErrors
(
order
==
2
,
"Cannot get a 2d cell for a tensor whose order is not 2!"
);
CheckNTErrors
(
ni
>=
0
&&
ni
<
dimSize
[
0
]
,
"dimension 0 is out of range!"
);
CheckNTErrors
(
mi
>=
0
&&
mi
<
dimSize
[
1
]
,
"dimension 1 is out of range!"
);
CheckNTErrors
(
dataType
==
X_INT
,
"The tensor is not in default type."
);
int
dims
[
2
]
=
{
ni
,
mi
};
void
*
value
=
GetCell
(
dims
,
2
);
...
...
@@ -1161,11 +1175,11 @@ get the value of a cell in a 3d tensor in int type
*/
int
XTensor
::
Get3DInt
(
int
d0
,
int
d1
,
int
d2
)
{
CheckNTErrors
(
(
order
==
3
)
,
"Cannot get a 2d cell for a tensor whose order is not 2!"
);
CheckNTErrors
(
(
d0
>=
0
&&
d0
<
dimSize
[
0
])
,
"dimension 0 is out of range!"
);
CheckNTErrors
(
(
d1
>=
0
&&
d1
<
dimSize
[
1
])
,
"dimension 1 is out of range!"
);
CheckNTErrors
(
(
d2
>=
0
&&
d2
<
dimSize
[
2
])
,
"dimension 2 is out of range!"
);
CheckNTErrors
(
(
dataType
==
X_INT
)
,
"The tensor is not in default type."
);
CheckNTErrors
(
order
==
3
,
"Cannot get a 2d cell for a tensor whose order is not 2!"
);
CheckNTErrors
(
d0
>=
0
&&
d0
<
dimSize
[
0
]
,
"dimension 0 is out of range!"
);
CheckNTErrors
(
d1
>=
0
&&
d1
<
dimSize
[
1
]
,
"dimension 1 is out of range!"
);
CheckNTErrors
(
d2
>=
0
&&
d2
<
dimSize
[
2
]
,
"dimension 2 is out of range!"
);
CheckNTErrors
(
dataType
==
X_INT
,
"The tensor is not in default type."
);
int
dims
[
3
]
=
{
d0
,
d1
,
d2
};
void
*
value
=
GetCell
(
dims
,
3
);
...
...
@@ -1180,8 +1194,8 @@ get the value of a cell in the sparse tensor
*/
DTYPE
XTensor
::
GetInSparse
(
int
i
)
{
CheckNTErrors
(
(
i
>=
0
&&
i
<
unitNum
)
,
"Index is out of range!"
);
CheckNTErrors
(
(
dataType
==
DEFAULT_DTYPE
)
,
"The tensor is not in default type."
);
CheckNTErrors
(
i
>=
0
&&
i
<
unitNum
,
"Index is out of range!"
);
CheckNTErrors
(
dataType
==
DEFAULT_DTYPE
,
"The tensor is not in default type."
);
char
*
d
=
(
char
*
)
data
+
sizeof
(
int
);
DTYPE
*
value
=
(
DTYPE
*
)(
d
+
(
sizeof
(
int
)
+
sizeof
(
DTYPE
))
*
i
+
sizeof
(
int
));
...
...
@@ -1196,8 +1210,8 @@ get the key value of a tuple in a sparse tensor
*/
int
XTensor
::
GetKeyInSparse
(
int
i
)
{
CheckNTErrors
(
(
i
>=
0
&&
i
<
unitNum
)
,
"Index is out of range!"
);
CheckNTErrors
(
(
dataType
==
DEFAULT_DTYPE
)
,
"The tensor is not in default type."
);
CheckNTErrors
(
i
>=
0
&&
i
<
unitNum
,
"Index is out of range!"
);
CheckNTErrors
(
dataType
==
DEFAULT_DTYPE
,
"The tensor is not in default type."
);
char
*
d
=
(
char
*
)
data
+
sizeof
(
int
);
int
*
key
=
(
int
*
)(
d
+
(
sizeof
(
int
)
+
sizeof
(
DTYPE
))
*
i
);
...
...
@@ -1213,7 +1227,7 @@ set the value of a cell
*/
bool
XTensor
::
Set
(
DTYPE
value
,
int
index
[],
int
size
)
{
CheckNTErrors
(
(
dataType
==
DEFAULT_DTYPE
)
,
"The tensor is not in default type."
);
CheckNTErrors
(
dataType
==
DEFAULT_DTYPE
,
"The tensor is not in default type."
);
return
SetToDevice
(
devID
,
GetCell
(
index
,
size
),
value
);
}
...
...
@@ -1226,9 +1240,9 @@ set the value of a cell in a 1d tensor
*/
bool
XTensor
::
Set1D
(
DTYPE
value
,
int
i
)
{
CheckNTErrors
(
(
order
==
1
)
,
"Cannot get a 2d cell for a tensor whose order is not 2!"
);
CheckNTErrors
(
(
i
>=
0
&&
i
<
dimSize
[
0
])
,
"dimension 0 is out of range!"
);
CheckNTErrors
(
(
dataType
==
DEFAULT_DTYPE
)
,
"The tensor is not in default type."
);
CheckNTErrors
(
order
==
1
,
"Cannot get a 2d cell for a tensor whose order is not 2!"
);
CheckNTErrors
(
i
>=
0
&&
i
<
dimSize
[
0
]
,
"dimension 0 is out of range!"
);
CheckNTErrors
(
dataType
==
DEFAULT_DTYPE
,
"The tensor is not in default type."
);
int
dims
[
1
]
=
{
i
};
...
...
@@ -1244,10 +1258,10 @@ set the value of a cell in a 2d tensor in default type
*/
bool
XTensor
::
Set2D
(
DTYPE
value
,
int
ni
,
int
mi
)
{
CheckNTErrors
(
(
order
==
2
)
,
"Cannot get a 2d cell for a tensor whose order is not 2!"
);
CheckNTErrors
(
(
ni
>=
0
&&
ni
<
dimSize
[
0
])
,
"dimension 0 is out of range!"
);
CheckNTErrors
(
(
mi
>=
0
&&
mi
<
dimSize
[
1
])
,
"dimension 1 is out of range!"
);
CheckNTErrors
(
(
dataType
==
DEFAULT_DTYPE
)
,
"The tensor is not in default type."
);
CheckNTErrors
(
order
==
2
,
"Cannot get a 2d cell for a tensor whose order is not 2!"
);
CheckNTErrors
(
ni
>=
0
&&
ni
<
dimSize
[
0
]
,
"dimension 0 is out of range!"
);
CheckNTErrors
(
mi
>=
0
&&
mi
<
dimSize
[
1
]
,
"dimension 1 is out of range!"
);
CheckNTErrors
(
dataType
==
DEFAULT_DTYPE
,
"The tensor is not in default type."
);
int
dims
[
2
]
=
{
ni
,
mi
};
...
...
@@ -1275,6 +1289,21 @@ bool XTensor::Set3D(DTYPE value, int d0, int d1, int d2)
return
SetToDevice
(
devID
,
GetCell
(
dims
,
3
),
value
);
}
/*
set the integer value of a cell by its offset
>> value - value we tend to set to the item
>> offset - offset of the item
*/
bool
XTensor
::
SetInt
(
int
value
,
int
offset
)
{
CheckNTErrors
(
offset
>=
0
&&
offset
<
unitNum
,
"Invalid index!"
);
CheckNTErrors
(
data
!=
NULL
,
"Cannot use an uninitialized tensor!"
);
int
*
d
=
(
int
*
)
data
+
offset
;
return
SetToDevice
(
devID
,
d
,
value
);
}
/*
set the integer value of a cell
...
...
@@ -1285,7 +1314,7 @@ set the integer value of a cell
*/
bool
XTensor
::
SetInt
(
int
value
,
int
index
[],
int
size
)
{
CheckNTErrors
(
(
dataType
==
X_INT
)
,
"The tensor is not in integer type."
);
CheckNTErrors
(
dataType
==
X_INT
,
"The tensor is not in integer type."
);
return
SetToDeviceInt
(
devID
,
GetCell
(
index
,
size
),
value
);
}
...
...
@@ -1298,9 +1327,9 @@ set the integer value of a cell in a 1d tensor
*/
bool
XTensor
::
Set1DInt
(
int
value
,
int
i
)
{
CheckNTErrors
(
(
order
==
1
)
,
"Cannot get a 2d cell for a tensor whose order is not 2!"
);
CheckNTErrors
(
(
i
>=
0
&&
i
<
dimSize
[
0
])
,
"dimension 0 is out of range!"
);
CheckNTErrors
(
(
dataType
==
X_INT
)
,
"The tensor is not in integer type."
);
CheckNTErrors
(
order
==
1
,
"Cannot get a 2d cell for a tensor whose order is not 2!"
);
CheckNTErrors
(
i
>=
0
&&
i
<
dimSize
[
0
]
,
"dimension 0 is out of range!"
);
CheckNTErrors
(
dataType
==
X_INT
,
"The tensor is not in integer type."
);
int
dims
[
1
]
=
{
i
};
...
...
@@ -1316,10 +1345,10 @@ set the integer value of a cell in a 2d tensor in default type
*/
bool
XTensor
::
Set2DInt
(
int
value
,
int
ni
,
int
mi
)
{
CheckNTErrors
(
(
order
==
2
)
,
"Cannot get a 2d cell for a tensor whose order is not 2!"
);
CheckNTErrors
(
(
ni
>=
0
&&
ni
<
dimSize
[
0
])
,
"dimension 0 is out of range!"
);
CheckNTErrors
(
(
mi
>=
0
&&
mi
<
dimSize
[
1
])
,
"dimension 1 is out of range!"
);
CheckNTErrors
(
(
dataType
==
X_INT
)
,
"The tensor is not in integer type."
);
CheckNTErrors
(
order
==
2
,
"Cannot get a 2d cell for a tensor whose order is not 2!"
);
CheckNTErrors
(
ni
>=
0
&&
ni
<
dimSize
[
0
]
,
"dimension 0 is out of range!"
);
CheckNTErrors
(
mi
>=
0
&&
mi
<
dimSize
[
1
]
,
"dimension 1 is out of range!"
);
CheckNTErrors
(
dataType
==
X_INT
,
"The tensor is not in integer type."
);
int
dims
[
2
]
=
{
ni
,
mi
};
...
...
@@ -1356,10 +1385,10 @@ increase the value of a cell in a 2d tensor
*/
bool
XTensor
::
Add2D
(
DTYPE
value
,
int
ni
,
int
mi
)
{
CheckNTErrors
(
(
ni
>=
0
&&
ni
<
dimSize
[
0
])
,
"the row index is out of range!"
);
CheckNTErrors
(
(
mi
>=
0
&&
mi
<
dimSize
[
1
])
,
"the column index is out of range!"
);
CheckNTErrors
(
(
dataType
==
DEFAULT_DTYPE
)
,
"The tensor is not in default type."
);
CheckNTErrors
(
(
isSparse
==
false
)
,
"TODO!"
);
CheckNTErrors
(
ni
>=
0
&&
ni
<
dimSize
[
0
]
,
"the row index is out of range!"
);
CheckNTErrors
(
mi
>=
0
&&
mi
<
dimSize
[
1
]
,
"the column index is out of range!"
);
CheckNTErrors
(
dataType
==
DEFAULT_DTYPE
,
"The tensor is not in default type."
);
CheckNTErrors
(
isSparse
==
false
,
"TODO!"
);
if
(
devID
<
0
){
DTYPE
*
p
=
(
DTYPE
*
)
data
+
ni
*
dimSize
[
1
]
+
mi
;
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论