Skip to content

SPARK-2684: Update ExternalAppendOnlyMap to take an iterator as input - #1607

Closed
mateiz wants to merge 2 commits into
apache:masterfrom
mateiz:spark-2684
Closed

mateiz wants to merge 2 commits into
apache:masterfrom
mateiz:spark-2684

Conversation

@mateiz

@mateiz mateiz commented Jul 27, 2014

Copy link
Copy Markdown
Contributor

This will decrease object allocation from the "update" closure used in map.changeValue.

@SparkQA

SparkQA commented Jul 27, 2014

Copy link
Copy Markdown

QA tests have started for PR 1607. This patch merges cleanly.
View progress: https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/17235/consoleFull

@SparkQA

SparkQA commented Jul 27, 2014

Copy link
Copy Markdown

QA results for PR 1607:
- This patch PASSES unit tests.
- This patch merges cleanly
- This patch adds no public classes

For more information see test ouptut:
https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/17235/consoleFull

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it makes sense to include a more collection-oriented interface, more like scala.collection.mutable.Buffer#insertAll:

def insertAll(coll: Iterable[Product2[K, V]]): Unit = insertAll(coll.iterator)

...so that you can do things like externalAppendOnlyMap.insertAll(aMap) instead of externalAppendOnlyMap.insertAll(aMap.iterator)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that much of the time we compute data in an Iterator, and Iterator is not Iterable AFAIK (you can't iterate over it more than once). We could add a second interface though if you'd like that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW I've now added this.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I saw. Thanks. I'm not really sure how useful it is, because you are correct that we usually have an iterator anyway, but it was simple and clean to add the Iterable interface.

@SparkQA

SparkQA commented Jul 27, 2014

Copy link
Copy Markdown

QA tests have started for PR 1607. This patch merges cleanly.
View progress: https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/17238/consoleFull

@SparkQA

SparkQA commented Jul 27, 2014

Copy link
Copy Markdown

QA results for PR 1607:
- This patch PASSES unit tests.
- This patch merges cleanly
- This patch adds no public classes

For more information see test ouptut:
https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/17238/consoleFull

@aarondav

Copy link
Copy Markdown
Contributor

LGTM, merging into master.

@asfgit asfgit closed this in 9857053 Jul 27, 2014
xiliu82 pushed a commit to xiliu82/spark that referenced this pull request Sep 4, 2014
This will decrease object allocation from the "update" closure used in map.changeValue.

Author: Matei Zaharia <matei@databricks.com>

Closes apache#1607 from mateiz/spark-2684 and squashes the following commits:

b7d89e6 [Matei Zaharia] Add insertAll for Iterables too, and fix some code style
561fc97 [Matei Zaharia] Update ExternalAppendOnlyMap to take an iterator as input
MaxGekk added a commit to MaxGekk/spark that referenced this pull request Nov 22, 2026
…ictMath (apache#1609)

<!-- title: [VARKA-345] Emit fdlibm's exp as IR nodes in the kernel, exact to StrictMath -->

### What changes were proposed in this pull request?

`exp` as the first function written on the `PORT` frame (apache#1607): `VarkaMathPort.exp` expands fdlibm 5.3's `e_exp.c` into ordinary IR nodes (lane arithmetic, compares, blends, the long bit views) inside the kernel's one method. No call is made at run time. Every branch of the C is computed in every lane and the lanes are blended in the C's order of precedence, so no lane is finished by a per-lane `StrictMath.exp`. Three planted faults of the sequence (a dropped correction term, a constant one ulp off, a moved threshold) are a package-private `Fault` argument, so the production expansion has none.

Three supporting changes:

- `D2I` (`DOUBLE` to `INT`, Java's `(int)`) passes `VarkaKernelPlan`'s conversion refusal and is lowered by `convertShape` on every host; `exp` takes its integer `k` through it. No compiler arm builds it for a SQL cast, so no SQL answer moves.
- The auto chain cap of apache#1605 (`VarkaEmitBudget.chainDepthCap`) gets a third arm, `PORT_MAX_CHAIN_DEPTH` = 64, for a tree that holds a `ReinterpretLane`, the node only the ports build. Every other tree keeps its cap (64 for an int-lane tree, 16 else), and an explicit `maxChainDepth` still wins. The emitter's analysis and the compiler's `fitsBudgets` both read `chainDepthCap`, so they agree.
- The test oracle is `io.github.vecbricks:vfdlibm:0.1.0` from Maven Central, a test-scope dependency of `sql/catalyst`. `javap` shows `VFdlibm.exp(DoubleVector[, VectorMask])` public, which is all the suite calls. No source copy, manifest or sync script.

### Why are the changes needed?

The exact default of the math family has to match `StrictMath` bit for bit, and a per-lane call is the only form that does today; it costs the scalar call and keeps the surrounding vector code but not the function. Writing the function's sequence as IR makes it vectorised, and `exp` is the template the other ports follow.

Why the result is bit-identical (the full argument is in `VARKA-345.md` 10.2): `StrictMath.exp` is fdlibm's `e_exp.c`, each step one correctly rounded IEEE double operation, and a vector add, subtract, multiply or divide on a double lane is that same operation, so a lane that follows the C's path gets the C's bits. The only difference is control flow. The port runs the far reduction for every lane with `k = 0` where the C does not reduce, which returns the C's inputs unchanged, tests `|x|`'s bits against constant words as the C tests its high word, and applies the blends in the C's order, so the last blend a lane satisfies is the C's `return`.

The cap is the real reason for the second change. `exp` is 36 levels deep (its polynomial alone ten) over the double, long and int lanes, so the auto cap declined it at 16 ("chain deeper than MAX_CHAIN_DEPTH=16"). Measured: layering `x + 1.0` over the port, the compiler's mirror admits exactly 28 more levels, a stop at 64, and the 65th is declined by the emitter with "MAX_CHAIN_DEPTH=64". At that depth the largest emitted method is 2070 bytes of the 8000-byte budget, so the cap, not the byte budget, is the stop and it is far from the method limit. The alternatives were a higher shared cap (it would change which fuzz draws and compiler folds fit) or a depth that counts only arithmetic (a second notion of depth beside `height`); a branch of the auto value touches only trees that already hold a port.

At `methodByteBudget` 0 (an option matrix arm) the op cap `MAX_FUSED_NODES` (64) applies and `exp` holds 68 ops, so the port is declined there, cleanly, and the row engine runs. A test holds that.

### Does this PR introduce _any_ user-facing change?

No. No compiler arm builds a port yet, so no SQL shape fuses with one and no answer moves.

### How was this patch tested?

`VarkaMathPortSuite` (its own JVM), against `StrictMath.exp` and `VFdlibm.exp` by raw bits, NaN payloads included:

- the sampled differential over four draws, 2^24 inputs;
- the fixed vectors (each `e_exp.c` threshold in both signs with 64 neighbours each way, the subnormal scale-back's start, special values, eight NaN patterns, 4136 inputs) at 2, 4 and 8 lanes over four null patterns and four lengths;
- the reference evaluator over the port's tree; `D2I` against Java's `(int)` at three widths; each planted fault caught; the constants' words;
- the depth and the stop at 64 above, the explicit cap, and the decline at `methodByteBudget` 0.

Also `VarkaEmitterFloatingCastSuite` (D2I admitted) and the option, budget, rounding, reproduction, math-contract and compiler suites around the cap. Scalastyle, scalafmt, errorprone and `dev/varka_push_check.py` are clean.

`/code-review high` (one round): 1 important finding, fixed: the port suite's kernels ran under the option matrix's `maxChainDepth` 16 or 32 and `methodByteBudget` 0, which decline `exp` by design, so the suite now builds its kernels with those two budgets put back (the decline itself has its own tests). Also fixed: errorprone's float-literal warnings on the constants, comments that still named the old cap, a decline test that did not check the cap's name. Left as nits: the port cap keys on `ReinterpretLane` (documented, only the ports build it), and a bare D2I root is untested, only D2I under a widening to long.

No issue was filed against `vfdlibm`: every `exp` lane was bit-identical to `StrictMath.exp` and nothing was measured against its node sequence.

The port's speed against the per-lane call is not measured here; the per-lane form is row 356's and the runners are dispatched separately.

### Was this patch authored or co-authored using generative AI tooling?

Generated-by: Claude Code (Claude Sonnet 5.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants