-
Notifications
You must be signed in to change notification settings - Fork 270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parallelism Pulse entities in parallel using divide and conquer #1008
Open
Hagvan
wants to merge
4
commits into
GlowstoneMC:dev
Choose a base branch
from
Hagvan:parallelism
base: dev
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9051779
Added ForkJoinPool along with ForkPulse and ForkPulsePlayers
Hagvan b9325d7
Revert "Added ForkJoinPool along with ForkPulse and ForkPulsePlayers"
Hagvan 18f3d08
Added ForkJoinPool along with ForkPulse and ForkPulsePlayers to pulse
Hagvan cb2bd9e
Added end of line at the end of files
Hagvan File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package net.glowstone.parallelism; | ||
|
||
import net.glowstone.entity.GlowEntity; | ||
import net.glowstone.entity.GlowPlayer; | ||
|
||
import java.util.ArrayList; | ||
import java.util.concurrent.RecursiveAction; | ||
|
||
/** | ||
* Represents a {@link RecursiveAction} to be recursively pulse all entities | ||
* except players in parallel. For players check {@link ForkPulsePlayers}. | ||
*/ | ||
public class ForkPulse extends RecursiveAction { | ||
|
||
private ArrayList<GlowEntity> entities; | ||
private int left; | ||
private int right; | ||
|
||
/** | ||
* Creates a new ForkPulse to recursively pulse through current entity list. | ||
* | ||
* @param entities The {@link ArrayList<GlowEntity>} to associate with entity list. | ||
* @param left The left bound (0 to pulse from the start). | ||
* @param right The right bound (entities.size() - 1 to pulse to the end). | ||
*/ | ||
public ForkPulse(ArrayList<GlowEntity> entities, int left, int right) { // Creates a recursive | ||
this.entities = entities; // action to pulse entities | ||
this.left = left; | ||
this.right = right; | ||
} | ||
|
||
@Override | ||
protected void compute() { | ||
if (entities.size() == 0) { // If for some reason the world has no entities, | ||
return; // this will prevent further execution. | ||
} | ||
if (right - left <= 20) { // TODO: Find a better reasoned threshold | ||
do { // If there are 20 or less entities in the range, pulse them sequentially | ||
GlowEntity entity = entities.get(left); | ||
if (!(entity instanceof GlowPlayer)) { | ||
entity.pulse(); | ||
} | ||
} while (++left <= right); | ||
return; | ||
} | ||
int mid = (right + left) / 2; // Otherwise split the work in two | ||
invokeAll(new ForkPulse(entities, left, mid), | ||
new ForkPulse(entities, mid + 1, right)); | ||
} | ||
} | ||
45 changes: 45 additions & 0 deletions
45
src/main/java/net/glowstone/parallelism/ForkPulsePlayers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package net.glowstone.parallelism; | ||
|
||
import net.glowstone.entity.GlowPlayer; | ||
|
||
import java.util.ArrayList; | ||
import java.util.concurrent.RecursiveAction; | ||
|
||
/** | ||
* Represents a {@link ForkPulse} counterpart to pulse players exclusively | ||
*/ | ||
public class ForkPulsePlayers extends RecursiveAction { | ||
|
||
private ArrayList<GlowPlayer> players; | ||
private int left; | ||
private int right; | ||
|
||
/** | ||
* Creates a new ForkPulse to recursively pulse through current entity players. | ||
* | ||
* @param players The {@link ArrayList<GlowPlayer>} to associate with players list. | ||
* @param left The left bound (0 to pulse from the start). | ||
* @param right The right bound (entities.size() - 1 to pulse to the end). | ||
*/ | ||
public ForkPulsePlayers(ArrayList<GlowPlayer> players, int left, int right) { | ||
this.players = players; | ||
this.left = left; | ||
this.right = right; | ||
} | ||
|
||
@Override | ||
protected void compute() { | ||
if (players.size() == 0) { | ||
return; | ||
} | ||
if (right - left <= 5) { // TODO: Find a better reasoned threshold | ||
do { | ||
players.get(left).pulse(); | ||
} while (++left <= right); | ||
return; | ||
} | ||
int mid = (right + left) / 2; | ||
invokeAll(new ForkPulsePlayers(players, left, mid), | ||
new ForkPulsePlayers(players, mid + 1, right)); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a new line at the end of file. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added a new line at the end but it still refuses to build. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a new line at the end of file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a new line at the end but it still refuses to build.