Commit d2e21dea17c72bbb8e787a038056ec1a9d46ed18
1 parent
4356a017e6
Exists in
master
Update preys-predators runtime to use let the control of device types to
MCMAS internal logic
Showing 2 changed files with 6 additions and 8 deletions Inline Diff
src/preypredator/PPRuntimeGPU.java
View file @
d2e21de
| package preypredator; | 1 | 1 | package preypredator; | |
| 2 | 2 | |||
| import mcmas.api.ContextType; | 3 | |||
| import mcmas.api.MCMASContext; | 4 | 3 | import mcmas.api.MCMASContext; | |
| import mcmas.core.MCMCommandQueueProperty; | 5 | 4 | import mcmas.core.MCMCommandQueueProperty; | |
| import mcmas.plugins.axb.AXBPlugin; | 6 | 5 | import mcmas.plugins.axb.AXBPlugin; | |
| import mcmas.plugins.gridsearch.GridSearchPlugin; | 7 | 6 | import mcmas.plugins.gridsearch.GridSearchPlugin; | |
| 8 | 7 | |||
| 9 | 8 | |||
| public class PPRuntimeGPU implements PPRuntime { | 10 | 9 | public class PPRuntimeGPU implements PPRuntime { | |
| 11 | 10 | |||
| private final MCMASContext context; | 12 | 11 | private final MCMASContext context; | |
| private final AXBPlugin axbPlugin; | 13 | 12 | private final AXBPlugin axbPlugin; | |
| private final GridSearchPlugin searchPlugin; | 14 | 13 | private final GridSearchPlugin searchPlugin; | |
| 15 | 14 | |||
| public PPRuntimeGPU(ContextType type) { | 16 | 15 | public PPRuntimeGPU() { | |
| this.context = new MCMASContext(type, MCMCommandQueueProperty.ENABLE_PROFILING); | 17 | 16 | this.context = new MCMASContext(MCMCommandQueueProperty.ENABLE_PROFILING); | |
| this.axbPlugin = new AXBPlugin(context); | 18 | 17 | this.axbPlugin = new AXBPlugin(context); | |
| this.searchPlugin = new GridSearchPlugin(context); | 19 | 18 | this.searchPlugin = new GridSearchPlugin(context); | |
| } | 20 | 19 | } | |
| 21 | 20 | |||
| @Override | 22 | 21 | @Override | |
| public void growGrass(PPGrid grass, float factor, int growth, int min, int max) { | 23 | 22 | public void growGrass(PPGrid grass, float factor, int growth, int min, int max) { | |
| axbPlugin.clampedTransform(grass.getStorage(), factor, growth, min, max); | 24 | 23 | axbPlugin.clampedTransform(grass.getStorage(), factor, growth, min, max); | |
| } | 25 | 24 | } | |
| 26 | 25 | |||
| @Override | 27 | 26 | @Override | |
| public void selectMaxTarget(PPGrid grid, int radius, int[] xPositions, | 28 | 27 | public void selectMaxTarget(PPGrid grid, int radius, int[] xPositions, | |
| int[] yPositions, int[] newXPositions, | 29 | 28 | int[] yPositions, int[] newXPositions, | |
| int[] newYPositions) { | 30 | 29 | int[] newYPositions) { | |
| searchPlugin.findMaxCell(grid, radius, xPositions, yPositions, newXPositions, newYPositions); | 31 | 30 | searchPlugin.findMaxCell(grid, radius, xPositions, yPositions, newXPositions, newYPositions); | |
| } | 32 | 31 | } | |
| 33 | 32 | |||
| @Override | 34 | 33 | @Override |
src/preypredator/PreyPredator2.java
View file @
d2e21de
| package preypredator; | 1 | 1 | package preypredator; | |
| import it.unimi.dsi.fastutil.ints.IntArrayList; | 2 | 2 | import it.unimi.dsi.fastutil.ints.IntArrayList; | |
| 3 | 3 | |||
| import java.io.IOException; | 4 | 4 | import java.io.IOException; | |
| import java.util.Arrays; | 5 | 5 | import java.util.Arrays; | |
| import java.util.Random; | 6 | 6 | import java.util.Random; | |
| 7 | 7 | |||
| import org.perf4j.StopWatch; | 8 | 8 | import org.perf4j.StopWatch; | |
| import org.perf4j.slf4j.Slf4JStopWatch; | 9 | 9 | import org.perf4j.slf4j.Slf4JStopWatch; | |
| import org.slf4j.Logger; | 10 | 10 | import org.slf4j.Logger; | |
| import org.slf4j.LoggerFactory; | 11 | 11 | import org.slf4j.LoggerFactory; | |
| 12 | 12 | |||
| import joptsimple.OptionParser; | 13 | 13 | import joptsimple.OptionParser; | |
| import joptsimple.OptionSet; | 14 | 14 | import joptsimple.OptionSet; | |
| import mcmas.api.ContextType; | 15 | |||
| 16 | 15 | |||
| public class PreyPredator2 { | 17 | 16 | public class PreyPredator2 { | |
| 18 | 17 | |||
| private final static Logger logger = LoggerFactory.getLogger(PreyPredator2.class); | 19 | 18 | private final static Logger logger = LoggerFactory.getLogger(PreyPredator2.class); | |
| private final StopWatch watch = new Slf4JStopWatch(); | 20 | 19 | private final StopWatch watch = new Slf4JStopWatch(); | |
| private final StopWatch runWatch = new Slf4JStopWatch(); | 21 | 20 | private final StopWatch runWatch = new Slf4JStopWatch(); | |
| 22 | 21 | |||
| //public static final int WIDTH = 1000; | 23 | 22 | //public static final int WIDTH = 1000; | |
| //public static final int HEIGHT = 1000; | 24 | 23 | //public static final int HEIGHT = 1000; | |
| 25 | 24 | |||
| public static final int GRASS_MIN = 0; | 26 | 25 | public static final int GRASS_MIN = 0; | |
| public static final int GRASS_MAX = 100; | 27 | 26 | public static final int GRASS_MAX = 100; | |
| public static final int GRASS_ORIG = 20; | 28 | 27 | public static final int GRASS_ORIG = 20; | |
| public static final float GRASS_FACT = 1.0f; | 29 | 28 | public static final float GRASS_FACT = 1.0f; | |
| public static final int GRASS_GROWTH = 10; | 30 | 29 | public static final int GRASS_GROWTH = 10; | |
| 31 | 30 | |||
| public static final int PREY_ORIG = 10; | 32 | 31 | public static final int PREY_ORIG = 10; | |
| //public static final int PREY_RADIUS = 20; | 33 | 32 | //public static final int PREY_RADIUS = 20; | |
| public static final int PREY_MAX = 50; | 34 | 33 | public static final int PREY_MAX = 50; | |
| 35 | 34 | |||
| public static final int PRED_ORIG = 10; | 36 | 35 | public static final int PRED_ORIG = 10; | |
| //public static final int PRED_RADIUS = 20; | 37 | 36 | //public static final int PRED_RADIUS = 20; | |
| public static final int PRED_MAX = 100; | 38 | 37 | public static final int PRED_MAX = 100; | |
| 39 | 38 | |||
| // Attributes | 40 | 39 | // Attributes | |
| 41 | 40 | |||
| private final PPGrid grass; | 42 | 41 | private final PPGrid grass; | |
| private final PPGrid preys; | 43 | 42 | private final PPGrid preys; | |
| private final PPGrid preds; | 44 | 43 | private final PPGrid preds; | |
| 45 | 44 | |||
| 46 | 45 | |||
| // Temporary buffers used in each step | 47 | 46 | // Temporary buffers used in each step | |
| 48 | 47 | |||
| private final IntArrayList preysX; | 49 | 48 | private final IntArrayList preysX; | |
| private final IntArrayList preysY; | 50 | 49 | private final IntArrayList preysY; | |
| private final IntArrayList predsX; | 51 | 50 | private final IntArrayList predsX; | |
| private final IntArrayList predsY; | 52 | 51 | private final IntArrayList predsY; | |
| 53 | 52 | |||
| private final int nbPreds; | 54 | 53 | private final int nbPreds; | |
| private final int nbPreys; | 55 | 54 | private final int nbPreys; | |
| private final int range; | 56 | 55 | private final int range; | |
| private final PPRuntime runtime; | 57 | 56 | private final PPRuntime runtime; | |
| 58 | 57 | |||
| //OCLChrono chrono = new OCLChrono("preypredator"); | 59 | 58 | //OCLChrono chrono = new OCLChrono("preypredator"); | |
| 60 | 59 | |||
| private final int size; | 61 | 60 | private final int size; | |
| 62 | 61 | |||
| public PreyPredator2(int size, int nbPreys, int nbPredators, int range, PPRuntime runtime) { | 63 | 62 | public PreyPredator2(int size, int nbPreys, int nbPredators, int range, PPRuntime runtime) { | |
| this.grass = new PPGrid(size, size); | 64 | 63 | this.grass = new PPGrid(size, size); | |
| this.preys = new PPGrid(size, size); | 65 | 64 | this.preys = new PPGrid(size, size); | |
| this.preds = new PPGrid(size, size); | 66 | 65 | this.preds = new PPGrid(size, size); | |
| 67 | 66 | |||
| this.size = size; | 68 | 67 | this.size = size; | |
| this.nbPreds = nbPredators; | 69 | 68 | this.nbPreds = nbPredators; | |
| this.nbPreys = nbPreys; | 70 | 69 | this.nbPreys = nbPreys; | |
| this.range = range; | 71 | 70 | this.range = range; | |
| 72 | 71 | |||
| this.preysX = new IntArrayList(); | 73 | 72 | this.preysX = new IntArrayList(); | |
| this.preysY = new IntArrayList(); | 74 | 73 | this.preysY = new IntArrayList(); | |
| this.predsX = new IntArrayList(); | 75 | 74 | this.predsX = new IntArrayList(); | |
| this.predsY = new IntArrayList(); | 76 | 75 | this.predsY = new IntArrayList(); | |
| 77 | 76 | |||
| this.runtime = runtime; | 78 | 77 | this.runtime = runtime; | |
| } | 79 | 78 | } | |
| 80 | 79 | |||
| public void setup() { | 81 | 80 | public void setup() { | |
| watch.start("pp_setup"); | 82 | 81 | watch.start("pp_setup"); | |
| initGrid(grass.getStorage(), (int) (grass.getStorage().length * 0.8), GRASS_ORIG); | 83 | 82 | initGrid(grass.getStorage(), (int) (grass.getStorage().length * 0.8), GRASS_ORIG); | |
| initGrid(preys.getStorage(), nbPreys, PREY_ORIG); | 84 | 83 | initGrid(preys.getStorage(), nbPreys, PREY_ORIG); | |
| initGrid(preds.getStorage(), nbPreds, PRED_ORIG); | 85 | 84 | initGrid(preds.getStorage(), nbPreds, PRED_ORIG); | |
| watch.stop(); | 86 | 85 | watch.stop(); | |
| } | 87 | 86 | } | |
| 88 | 87 | |||
| public void update_positions() { | 89 | 88 | public void update_positions() { | |
| //chrono.start("update_positions"); | 90 | 89 | //chrono.start("update_positions"); | |
| 91 | 90 | |||
| preysX.clear(); | 92 | 91 | preysX.clear(); | |
| preysY.clear(); | 93 | 92 | preysY.clear(); | |
| predsX.clear(); | 94 | 93 | predsX.clear(); | |
| predsY.clear(); | 95 | 94 | predsY.clear(); | |
| 96 | 95 | |||
| for (int j = 0; j < size; j++) { | 97 | 96 | for (int j = 0; j < size; j++) { | |
| for (int i = 0; i < size; i++) { | 98 | 97 | for (int i = 0; i < size; i++) { | |
| if (preys.get(i, j) > 0) { | 99 | 98 | if (preys.get(i, j) > 0) { | |
| preysX.add(i); | 100 | 99 | preysX.add(i); | |
| preysY.add(j); | 101 | 100 | preysY.add(j); | |
| } | 102 | 101 | } | |
| 103 | 102 | |||
| if (preds.get(i, j) > 0) { | 104 | 103 | if (preds.get(i, j) > 0) { | |
| predsX.add(i); | 105 | 104 | predsX.add(i); | |
| predsY.add(j); | 106 | 105 | predsY.add(j); | |
| } | 107 | 106 | } | |
| } | 108 | 107 | } | |
| } | 109 | 108 | } | |
| 110 | 109 | |||
| //System.out.println(chrono.stop()); | 111 | 110 | //System.out.println(chrono.stop()); | |
| } | 112 | 111 | } | |
| 113 | 112 | |||
| public void step() { | 114 | 113 | public void step() { | |
| watch.start("pp_step"); | 115 | 114 | watch.start("pp_step"); | |
| //OCLChrono stepChrono = new OCLChrono("step"); | 116 | 115 | //OCLChrono stepChrono = new OCLChrono("step"); | |
| //stepChrono.start(); | 117 | 116 | //stepChrono.start(); | |
| 118 | 117 | |||
| update_positions(); | 119 | 118 | update_positions(); | |
| 120 | 119 | |||
| runWatch.start("pp_grass"); | 121 | 120 | runWatch.start("pp_grass"); | |
| runtime.growGrass(grass, GRASS_FACT, GRASS_GROWTH, GRASS_MIN, GRASS_MAX); | 122 | 121 | runtime.growGrass(grass, GRASS_FACT, GRASS_GROWTH, GRASS_MIN, GRASS_MAX); | |
| runWatch.stop(); | 123 | 122 | runWatch.stop(); | |
| 124 | 123 | |||
| int[] newPreysX = new int[preysX.size()]; | 125 | 124 | int[] newPreysX = new int[preysX.size()]; | |
| int[] newPreysY = new int[preysY.size()]; | 126 | 125 | int[] newPreysY = new int[preysY.size()]; | |
| int[] newPredsX = new int[predsX.size()]; | 127 | 126 | int[] newPredsX = new int[predsX.size()]; | |
| int[] newPredsY = new int[predsY.size()]; | 128 | 127 | int[] newPredsY = new int[predsY.size()]; | |
| 129 | 128 | |||
| runWatch.start("pp_move_preys"); | 130 | 129 | runWatch.start("pp_move_preys"); | |
| if (preysX.size() > 0) { | 131 | 130 | if (preysX.size() > 0) { | |
| 132 | 131 | |||
| runtime.selectMaxTarget(grass, range, | 133 | 132 | runtime.selectMaxTarget(grass, range, | |
| preysX.toIntArray(), preysY.toIntArray(), | 134 | 133 | preysX.toIntArray(), preysY.toIntArray(), | |
| newPreysX, newPreysY); | 135 | 134 | newPreysX, newPreysY); | |
| } | 136 | 135 | } | |
| runWatch.stop(); | 137 | 136 | runWatch.stop(); | |
| 138 | 137 | |||
| /* | 139 | 138 | /* | |
| for (int i = 0; i < preysX.size(); i++) { | 140 | 139 | for (int i = 0; i < preysX.size(); i++) { | |
| System.out.println("Prey: (" + preysX.get(i) + ", " + preysY.get(i) + ") => (" + | 141 | 140 | System.out.println("Prey: (" + preysX.get(i) + ", " + preysY.get(i) + ") => (" + | |
| newPreysX[i] + ", " + newPreysY[i] + ")"); | 142 | 141 | newPreysX[i] + ", " + newPreysY[i] + ")"); | |
| }*/ | 143 | 142 | }*/ | |
| 144 | 143 | |||
| // PREYS | 145 | 144 | // PREYS | |
| 146 | 145 | |||
| for (int i = 0; i < preysX.size(); i++) { | 147 | 146 | for (int i = 0; i < preysX.size(); i++) { | |
| final int oldPos = preys.offset(preysX.get(i), preysY.get(i)); | 148 | 147 | final int oldPos = preys.offset(preysX.get(i), preysY.get(i)); | |
| final int newPos = preys.offset(newPreysX[i], newPreysY[i]); | 149 | 148 | final int newPos = preys.offset(newPreysX[i], newPreysY[i]); | |
| 150 | 149 | |||
| // Check an eventual conflict with another prey | 151 | 150 | // Check an eventual conflict with another prey | |
| if (preys.get(newPos) > 0) { | 152 | 151 | if (preys.get(newPos) > 0) { | |
| continue; | 153 | 152 | continue; | |
| } | 154 | 153 | } | |
| 155 | 154 | |||
| // Movement | 156 | 155 | // Movement | |
| preys.set(newPos, preys.get(oldPos)); | 157 | 156 | preys.set(newPos, preys.get(oldPos)); | |
| preys.set(oldPos, 0); | 158 | 157 | preys.set(oldPos, 0); | |
| 159 | 158 | |||
| // Eating | 160 | 159 | // Eating | |
| if (grass.get(newPos) >= 0) { | 161 | 160 | if (grass.get(newPos) >= 0) { | |
| preys.set(newPos, clamp(preys.get(newPos) + grass.get(newPos), 0, PREY_MAX)); | 162 | 161 | preys.set(newPos, clamp(preys.get(newPos) + grass.get(newPos), 0, PREY_MAX)); | |
| grass.set(newPos, 0); | 163 | 162 | grass.set(newPos, 0); | |
| } | 164 | 163 | } | |
| 165 | 164 | |||
| // Reproduction | 166 | 165 | // Reproduction | |
| if (preys.get(newPos) == PREY_MAX) { | 167 | 166 | if (preys.get(newPos) == PREY_MAX) { | |
| preys.set(oldPos, PREY_ORIG); | 168 | 167 | preys.set(oldPos, PREY_ORIG); | |
| } | 169 | 168 | } | |
| } | 170 | 169 | } | |
| 171 | 170 | |||
| runWatch.start("pp_move_preds"); | 172 | 171 | runWatch.start("pp_move_preds"); | |
| if (predsX.size() > 0) { | 173 | 172 | if (predsX.size() > 0) { | |
| int [] predsXbis = predsX.toIntArray(); | 174 | 173 | int [] predsXbis = predsX.toIntArray(); | |
| int [] predsYbis = predsY.toIntArray(); | 175 | 174 | int [] predsYbis = predsY.toIntArray(); | |
| 176 | 175 | |||
| runtime.selectMaxTarget(preys, (int) Math.round(range * 1.5), | 177 | 176 | runtime.selectMaxTarget(preys, (int) Math.round(range * 1.5), | |
| predsXbis, predsYbis, | 178 | 177 | predsXbis, predsYbis, | |
| newPredsX, newPredsY); | 179 | 178 | newPredsX, newPredsY); | |
| } | 180 | 179 | } | |
| runWatch.stop(); | 181 | 180 | runWatch.stop(); | |
| 182 | 181 | |||
| /*for (int i = 0; i < predsX.size(); i++) { | 183 | 182 | /*for (int i = 0; i < predsX.size(); i++) { | |
| System.out.println("Pred: (" + predsX.get(i) + ", " + predsY.get(i) + ") => (" + | 184 | 183 | System.out.println("Pred: (" + predsX.get(i) + ", " + predsY.get(i) + ") => (" + | |
| newPredsX[i] + ", " + newPredsY[i] + ")"); | 185 | 184 | newPredsX[i] + ", " + newPredsY[i] + ")"); | |
| }*/ | 186 | 185 | }*/ | |
| 187 | 186 | |||
| // PREDATOR | 188 | 187 | // PREDATOR | |
| 189 | 188 | |||
| for (int i = 0; i < predsX.size(); i++) { | 190 | 189 | for (int i = 0; i < predsX.size(); i++) { | |
| final int pos = predsY.get(i) * size + predsX.get(i); | 191 | 190 | final int pos = predsY.get(i) * size + predsX.get(i); | |
| final int newPos = newPredsY[i] * size + newPredsX[i]; | 192 | 191 | final int newPos = newPredsY[i] * size + newPredsX[i]; | |
| 193 | 192 | |||
| // Check an eventual conflict with another predator | 194 | 193 | // Check an eventual conflict with another predator | |
| if (preds.get(newPos) > 0) { | 195 | 194 | if (preds.get(newPos) > 0) { | |
| continue; | 196 | 195 | continue; | |
| } | 197 | 196 | } | |
| 198 | 197 | |||
| // Movement | 199 | 198 | // Movement | |
| preds.set(newPos, preds.get(pos)); | 200 | 199 | preds.set(newPos, preds.get(pos)); | |
| preds.set(pos, 0); | 201 | 200 | preds.set(pos, 0); | |
| 202 | 201 | |||
| // Eating | 203 | 202 | // Eating | |
| if (preys.get(newPos) >= 0) { | 204 | 203 | if (preys.get(newPos) >= 0) { | |
| preds.set(newPos, clamp(preds.get(newPos) + preys.get(newPos), 0, PRED_MAX)); | 205 | 204 | preds.set(newPos, clamp(preds.get(newPos) + preys.get(newPos), 0, PRED_MAX)); | |
| preys.set(newPos, 0); | 206 | 205 | preys.set(newPos, 0); | |
| } | 207 | 206 | } | |
| 208 | 207 | |||
| // Reproduction | 209 | 208 | // Reproduction | |
| if (preds.get(newPos) == PRED_MAX) { | 210 | 209 | if (preds.get(newPos) == PRED_MAX) { | |
| preds.set(pos, PRED_ORIG); | 211 | 210 | preds.set(pos, PRED_ORIG); | |
| } | 212 | 211 | } | |
| } | 213 | 212 | } | |
| 214 | 213 | |||
| /*System.out.println(grass); | 215 | 214 | /*System.out.println(grass); | |
| System.out.println(preys);*/ | 216 | 215 | System.out.println(preys);*/ | |
| //System.out.println(preds); | 217 | 216 | //System.out.println(preds); | |
| //logger.info(stepChrono.stop().toString()); | 218 | 217 | //logger.info(stepChrono.stop().toString()); | |
| watch.stop(); | 219 | 218 | watch.stop(); | |
| } | 220 | 219 | } | |
| 221 | 220 | |||
| private void initGrid(int[] grid, int nbCells, int value) { | 222 | 221 | private void initGrid(int[] grid, int nbCells, int value) { | |
| int remainingCells = nbCells; | 223 | 222 | int remainingCells = nbCells; | |
| Random rng = new Random(); | 224 | 223 | Random rng = new Random(); | |
| 225 | 224 | |||
| while (remainingCells > 0) { | 226 | 225 | while (remainingCells > 0) { | |
| final int pos = rng.nextInt(grid.length); | 227 | 226 | final int pos = rng.nextInt(grid.length); | |
| 228 | 227 | |||
| if (grid[pos] == 0) { | 229 | 228 | if (grid[pos] == 0) { | |
| grid[pos] = value; | 230 | 229 | grid[pos] = value; | |
| remainingCells--; | 231 | 230 | remainingCells--; | |
| } | 232 | 231 | } | |
| } | 233 | 232 | } | |
| } | 234 | 233 | } | |
| 235 | 234 | |||
| // Helpers | 236 | 235 | // Helpers |