for_range
功能说明
代表tik的for循环语句,可在for循环中开启double buffer和多核运行功能。
函数原型
for_range(begint, endt, name="i", thread_num=1, thread_type="whole", block_num=1,dtype="int32", for_type="serial")
参数说明
参数名称 |
输入/输出 |
含义 |
---|---|---|
begint |
输入 |
for循环起始。 begint和endt为立即数(int, uint)、Scalar(int, uint)、Expr,Expr化简后的值也需要为整数,需要满足 0<=begint<=endt<=2147483647。 |
endt |
输入 |
for循环结束。 begint和endt为立即数(int, uint)、Scalar(int, uint)、Expr,Expr化简后的值也需要为整数,需要满足 0<=begint<=endt<=2147483647。 说明:
begint和endt使用Scalar,性能会有所下降。 |
name |
输入 |
for循环中的循环变量名字,默认"i"。 |
thread_num |
输入 |
for循环中是否开启double buffer。取值:
|
thread_type |
输入 |
代表for循环展开的代码排列方式,当前为保留参数,对运行无影响。取值:whole |
block_num |
输入 |
for循环中使用的核数。最大值为65535。
|
dtype |
输入 |
for循环变量的类型,当前为保留参数,对运行无影响。取值:int32 |
for_type |
输入 |
代表此for循环本身的类型,取值:serial |
注意事项
- 如果开启多核,多核循环起始值必须为0,多核的核数和循环的终止值必须相等。
- 在一个for循环内,不能同时开启多核和double buffer。如果想同时开启多核和double buffer,采用多个循环方式。
- 如果开启多核,在多核循环内使用到的tensor,必须要定义在多核循环内。因为多核循环外和多核循环内的tensor内存分配同时从0开始分配,可能导致地址重叠,最后导致数据错误。
- 开启double buffer时,只有定义在for循环内的tensor ,才会分配2份内存。
- 循环的执行体内不能修改for_range的endt值,否则会导致算子执行任务挂起。
- 使用循环变量时的注意事项:
# 使用循环变量 with self.tik_instance.for_range(0,10) as i: with self.tik_instance.if_scope(i==0): # 注意不能使用if i==0: do_someting with self.tik_instance.else_scope(): # 注意不能使用else: do_someting
返回值
TikWithScope对象
调用示例
with self.tik_instance.for_range(0,1,thread_num=1): do_someting # 开启double buffer,注意,tensor的定义要在for range内部定义才能会分配2份内存 with self.tik_instance.for_range(0,2,thread_num=2): tensor定义 do_someting # 开启多核运行 with self.tik_instance.for_range(0,2,block_num=2): do_someting
