inducer / inducer/loopy

ILP not applied to variable loop length

Open
#77 5 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
636
Forks
81
Avg merge
1d 19h
Merged PRs (30d)
7

Description

Playing around with sparse / variable loop length codes, and came across this one

```
import loopy as lp
import numpy as np
lp.set_caching_enabled(False)

k = lp.make_kernel([
"{ [i] : 0 <= i < m }",
"{ [j] : 0 <= j < length }"],
"""
for i
<> rowstart = rowstarts[i]
<> rowend = rowstarts[i]
<> length = rowend - rowstart
y[i] = sum(j, values[rowstart+j] * x[colindices[rowstart + j]])
end
""")
k = lp.add_and_infer_dtypes(k, {'values,x':np.float64, 'rowstarts,colindices':k.index_dtype})
k = lp.split_iname(k, 'i', 2, inner_tag='ilp')
print(lp.generate_code(k)[0])
```

Generates:

```
#define lid(N) ((int) get_local_id(N))
#define gid(N) ((int) get_group_id(N))
#if __OPENCL_C_VERSION__ < 120
#pragma OPENCL EXTENSION cl_khr_fp64: enable
#endif
#define int_floor_div_pos_b(a,b) ( ( (a) - ( ((a)<0) ? ((b)-1) : 0 ) ) / (b) )

__kernel void __attribute__ ((reqd_work_group_size(1, 1, 1))) loopy_kernel(__global int const *restrict colindices, int const m, __global int const *restrict rowstarts, __global double const *restrict values, __global double const *restrict x, __global double *restrict y)
{
double acc_j[2];
**int length[2]**;
int rowend[2];
int rowstart[2];

for (int i_outer = 0; i_outer <= -1 + int_floor_div_pos_b(1 + m, 2); ++i_outer)
{
acc_j[0] = 0.0;
rowend[0] = rowstarts[2 * i_outer];
rowstart[0] = rowstarts[2 * i_outer];
**length[0]** = rowend[0] + -1 * rowstart[0];
if (-2 + -2 * i_outer + m >= 0)
{
acc_j[1] = 0.0;
rowend[1] = rowstarts[2 * i_outer + 1];
rowstart[1] = rowstarts[2 * i_outer + 1];
**length[1]** = rowend[1] + -1 * rowstart[1];
}
for (int j = 0; j <= -1 + **length**; ++j)
{
acc_j[0] = acc_j[0] + values[rowstart[0] + j] * x[colindices[rowstart[0] + j]];
if (-2 + -2 * i_outer + m >= 0)
acc_j[1] = acc_j[1] + values[rowstart[1] + j] * x[colindices[rowstart[1] + j]];
}
y[2 * i_outer] = acc_j[0];
if (-2 + -2 * i_outer + m >= 0)
y[2 * i_outer + 1] = acc_j[1];
}
}
```

Note the starred lines dealing with the length parameter (apparently bolding doesn't work in code markdown). The `length` parameter is correctly split by ILP _except_ for when it's used as the loop bound

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.