Skip to content

Commit

Permalink
M array/polyarray.go
Browse files Browse the repository at this point in the history
  • Loading branch information
drmingdrmer committed Jun 7, 2019
1 parent c2c8b57 commit 328f1d0
Showing 1 changed file with 20 additions and 24 deletions.
44 changes: 20 additions & 24 deletions array/polyarray.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ const (
twoPow36 = float64(int64(1) << 36)

// In a segment we want:
// residual position = start + (i%1024) * residualWidth
// But if preceding span has smaller residual width, the "start" could be
// residual position = offset + (i%1024) * residualWidth
// But if preceding span has smaller residual width, the "offset" could be
// negative, e.g.: span[0] has residual of width 0 and 16 residuals,
// span[1] has residual of width 4.
// Then the "start" of span[1] is -16*4 in order to satisify:
// Then the "offset" of span[1] is -16*4 in order to satisify:
// (-16*4) + i * 4 is the correct residual position, for i in [16, 32).
maxStartOffset = int64(segWidth) * int64(maxResidualWidth)
maxNegOffset = int64(segWidth) * int64(maxResidualWidth)
)

var (
Expand Down Expand Up @@ -111,15 +111,15 @@ func (m *PolyArray) Get(i int32) int32 {
v := int32(p[j] + p[j+1]*x + p[j+2]*x*x)

// read the config of this polynomial:
// eltWidth: how many bits a residual needs.
// start: where the 0-th elt is at in Residuals.
// residualWidth: how many bits a residual needs.
// offset.

config := p[j+3]
residualWidth := int64(config)
start := int64((config-float64(residualWidth))*twoPow36) - maxStartOffset
offset := int64((config-float64(residualWidth))*twoPow36) - maxNegOffset

// where the elt is
ibit := start + int64(i)*residualWidth
// where the residual is
ibit := offset + int64(i)*residualWidth

d := m.Residuals[ibit>>6]
d = d >> uint(ibit&63)
Expand Down Expand Up @@ -178,14 +178,14 @@ func (m *PolyArray) Stat() map[string]int32 {
return st
}

func packConfig(start int64, residualWidth int64) float64 {
return float64(residualWidth) + float64(start)/twoPow36
func packConfig(offset int64, residualWidth int64) float64 {
return float64(residualWidth) + float64(offset)/twoPow36
}

func unpackConfig(config float64) (int64, int64) {
residualWidth := int64(config)
start := int64((config - float64(residualWidth)) * twoPow36)
return start, residualWidth
offset := int64((config - float64(residualWidth)) * twoPow36)
return offset, residualWidth
}

func (m *PolyArray) addSeg(nums []int32, rank uint64) uint64 {
Expand All @@ -210,7 +210,7 @@ func (m *PolyArray) addSeg(nums []int32, rank uint64) uint64 {
// Using a bitmap to describe which spans a polynomial spans
segPolyBitmap := uint64(0)

// Where the first elt of this segment is in m.Residuals
// Where the residual of the first elt of this segment is in m.Residuals
segStart := int64(len(m.Residuals) * 64)

// Where a residual is in m.Residuals, relatvie to the 0-th bit in
Expand All @@ -235,22 +235,19 @@ func (m *PolyArray) addSeg(nums []int32, rank uint64) uint64 {

polys = append(polys, sp.poly...)

// "start" at max is 2^31 * 16 = 2^35
// thus start /2^36 is always less than 1

// We want eltIndex = stBySeg + i * eltWidth
// min of stBySeg is -segWidth * eltWidth = -1024 * 16;
// We want eltIndex = stBySeg + i * residualWidth
// min of stBySeg is -segWidth * residualWidth = -1024 * 16;
// Add this value to make it a positive number.
stBySeg := resI - int64(sp.s)*int64(width) + maxStartOffset
config := packConfig(stBySeg, int64(width))
offset := resI - int64(sp.s)*int64(width)
config := packConfig(offset+maxNegOffset, int64(width))
polys = append(polys, config)

for j := sp.s; j < sp.e; j++ {

v := evalpoly2(sp.poly, xs[j])

d := int32(nums[j]) - int32(v)
if d > int32(margin) || d < 0 {
d := nums[j] - int32(v)
if d > margin || d < 0 {
panic(fmt.Sprintf("d=%d must smaller than %d and > 0", d, margin))
}

Expand Down Expand Up @@ -364,7 +361,6 @@ func findMinFittingsNew(xs, ys []float64, fts []*polyfit.Fitting) []span {
}
} else {
// Even the minimal merge does not reduce memory cost.
// fmt.Println("min reg does not reduce mem")
break
}
}
Expand Down

0 comments on commit 328f1d0

Please sign in to comment.