Compare View
Commits (7)
Diff
Showing 8 changed files Inline Diff
ivy.xml
View file @
1d0884a
<ivy-module version="2.0"> | 1 | 1 | <ivy-module version="2.0"> | |
<info organisation="org.apache" module="geotools"></info> | 2 | 2 | <info organisation="org.apache" module="geotools"></info> | |
<configurations> | 3 | 3 | <configurations> | |
<conf name="default"/> | 4 | 4 | <conf name="default"/> | |
</configurations> | 5 | 5 | </configurations> | |
6 | 6 | |||
<dependencies> | 7 | 7 | <dependencies> | |
<dependency org="org.geotools" name="gt-shapefile-ng" rev="8.6"/> | 8 | 8 | <dependency org="org.geotools" name="gt-shapefile-ng" rev="8.6"/> | |
<dependency org="jfree" name="jfreechart" rev="1.0.13"/> | 9 | 9 | <dependency org="jfree" name="jfreechart" rev="1.0.13"/> | |
<dependency org="org.jocl" name="jocl" rev="0.1.9"/> | 10 | 10 | <dependency org="org.jocl" name="jocl" rev="0.1.9"/> | |
<dependency org="org.slf4j" name="slf4j-api" rev="1.6.6" conf="default->master"/> | 11 | 11 | <dependency org="org.slf4j" name="slf4j-api" rev="1.6.6" conf="default->master"/> | |
<dependency org="org.perf4j" name="perf4j" rev="0.9.16" conf="default->master"/> | 12 | 12 | <dependency org="org.perf4j" name="perf4j" rev="0.9.16" conf="default->master"/> | |
13 | <dependency org="ch.qos.logback" name="logback-core" rev="1.0.7" conf="default->master"/> | |||
<dependency org="ch.qos.logback" name="logback-core" rev="1.0.7" conf="default->master"/> | 13 | 14 | <dependency org="ch.qos.logback" name="logback-classic" rev="1.0.7" conf="default->master"/> | |
<dependency org="ch.qos.logback" name="logback-classic" rev="1.0.7" conf="default->master"/> | 14 | 15 | <dependency org="net.sf.jopt-simple" name="jopt-simple" rev="4.3" conf="default->master"/> | |
<dependency org="net.sf.jopt-simple" name="jopt-simple" rev="4.3" conf="default->master"/> | 15 | 16 | <dependency org="it.unimi.dsi" name="fastutil" rev="6.5.15"/> | |
<dependency org="it.unimi.dsi" name="fastutil" rev="6.5.15"/> | 16 | 17 | </dependencies> | |
</dependencies> | 17 | 18 | </ivy-module> | |
</ivy-module> | 18 | 19 |
src/mcmas/core/MCMContext.java
View file @
1d0884a
package mcmas.core; | 1 | 1 | package mcmas.core; | |
2 | 2 | |||
import java.nio.Buffer; | 3 | 3 | import java.nio.Buffer; | |
4 | 4 | |||
import mcmas.utils.Objects7; | 5 | 5 | import mcmas.utils.Objects7; | |
6 | 6 | |||
import org.jocl.CL; | 7 | 7 | import org.jocl.CL; | |
import org.jocl.Pointer; | 8 | 8 | import org.jocl.Pointer; | |
import org.jocl.cl_context; | 9 | 9 | import org.jocl.cl_context; | |
import org.jocl.cl_device_id; | 10 | 10 | import org.jocl.cl_device_id; | |
import org.jocl.cl_image_format; | 11 | 11 | import org.jocl.cl_image_format; | |
import org.jocl.cl_platform_id; | 12 | 12 | import org.jocl.cl_platform_id; | |
13 | 13 | |||
14 | 14 | |||
public class MCMContext extends MCMObject { | 15 | 15 | public class MCMContext extends MCMObject { | |
16 | 16 | |||
private final cl_context context; | 17 | 17 | private final cl_context context; | |
//private final static Logger logger = LoggerFactory.getLogger(OCLContext.class); | 18 | 18 | //private final static Logger logger = LoggerFactory.getLogger(OCLContext.class); | |
19 | 19 | |||
20 | /** | |||
21 | * Create a new context using the first available hardware among | |||
22 | * default devices types. | |||
23 | */ | |||
/** | 20 | 24 | public MCMContext() { | |
* Create a new context using the first available hardware among | 21 | 25 | this(getDefaultContextTypes()); | |
* default devices types. | 22 | |||
*/ | 23 | |||
public MCMContext() { | 24 | |||
this(getDefaultContextTypes()); | 25 | |||
} | 26 | |||
27 | ||||
/** | 28 | |||
* Create a new context using the first available hardware among | 29 | |||
* indicated device types. | 30 | |||
* | 31 | |||
* @param deviceTypes device types to search for | 32 | |||
*/ | 33 | |||
public MCMContext(long... deviceTypes) { | 34 | |||
MCMPlatform[] platforms = MCMPlatform.getPlatforms(); | 35 | |||
36 | ||||
if (platforms.length <= 0) { | 37 | 26 | } | |
throw new RuntimeException("No OpenCL platform found"); | 38 | 27 | ||
28 | /** | |||
29 | * Create a new context using the first available hardware among | |||
30 | * indicated device types. | |||
31 | * | |||
32 | * @param deviceTypes device types to search for | |||
33 | */ | |||
} | 39 | 34 | public MCMContext(long... deviceTypes) { | |
40 | 35 | MCMPlatform[] platforms = MCMPlatform.getPlatforms(); | ||
this.context = MCMHelpers.createContextFromType(platforms[0].getId(), deviceTypes); | 41 | 36 | ||
42 | 37 | if (platforms.length <= 0) { | ||
if (context == null) { | 43 | 38 | throw new RuntimeException("No OpenCL platform found"); | |
throw new RuntimeException("No OpenCL device found"); | 44 | 39 | } | |
} | 45 | 40 | ||
} | 46 | 41 | this.context = MCMHelpers.createContextFromType(platforms[0].getId(), deviceTypes); | |
47 | 42 | |||
/** | 48 | 43 | if (context == null) { | |
* Create a new context using the indicated platform. | 49 | 44 | throw new RuntimeException("No OpenCL device found"); | |
* | 50 | 45 | } | |
* @param platform platform to consider | 51 | 46 | } | |
*/ | 52 | 47 | ||
48 | /** | |||
49 | * Create a new context using the indicated platform. | |||
50 | * | |||
51 | * @param platform platform to consider | |||
52 | */ | |||
53 | public MCMContext(MCMPlatform platform) { | |||
54 | this(platform, getDefaultContextTypes()); | |||
55 | } | |||
56 | ||||
57 | /** | |||
58 | * Create a new context using the indicated platform and device types. | |||
59 | * | |||
60 | * @param platform platform to consider | |||
61 | * @param deviceTypes device types to search for | |||
62 | */ | |||
public MCMContext(MCMPlatform platform) { | 53 | 63 | public MCMContext(MCMPlatform platform, long... deviceTypes) { | |
this(platform, getDefaultContextTypes()); | 54 | 64 | platform = Objects7.requireNonNull(platform, "Can't create an OpenCL context with a null platform"); | |
} | 55 | 65 | ||
56 | 66 | System.out.println("platform vendor : " + platform.getInfoString(CL.CL_PLATFORM_VENDOR)); | ||
/** | 57 | 67 | System.out.println("platform name : " + platform.getInfoString(CL.CL_PLATFORM_NAME)); | |
* Create a new context using the indicated platform and device types. | 58 | 68 | ||
* | 59 | 69 | this.context = MCMHelpers.createContextFromType(platform.getId(), deviceTypes); | |
* @param platform platform to consider | 60 | 70 | ||
* @param deviceTypes device types to search for | 61 | 71 | if (context == null) { | |
*/ | 62 | 72 | throw new RuntimeException("No OpenCL device found"); | |
public MCMContext(MCMPlatform platform, long... deviceTypes) { | 63 | 73 | } | |
platform = Objects7.requireNonNull(platform, "Can't create an OpenCL context with a null platform"); | 64 | 74 | } | |
65 | 75 | |||
76 | /** | |||
77 | * Return a list of default device types to test when nothing | |||
78 | * is indicated at the MCMContext creation. | |||
79 | * | |||
80 | * The default list indicates to use these kind of hardware in this order: | |||
81 | * GPU, Accelerators (such as Xeon Phi) and CPU. | |||
82 | * | |||
83 | * It can be overriden using the MCM_CONTEXT_TYPE environment variable, | |||
84 | * which admits the following values: "CPU", "GPU", "ACCELERATOR", "ALL". | |||
85 | * | |||
86 | * "ALL" considers all the devices available on the system, but the | |||
87 | * order is not garanteed (even for the same type of device) and depends | |||
88 | * of the installed runtimes. | |||
89 | * | |||
90 | * @return a list of context type to try. | |||
91 | */ | |||
92 | public static long[] getDefaultContextTypes() { | |||
93 | String context_type = System.getenv("MCM_CONTEXT_TYPE"); | |||
94 | ||||
95 | if ("CPU".equals(context_type)) { | |||
96 | return new long[] { CL.CL_DEVICE_TYPE_CPU }; | |||
97 | } else if ("GPU".equals(context_type)) { | |||
98 | return new long[] { CL.CL_DEVICE_TYPE_GPU }; | |||
99 | } else if ("ACCELERATOR".equals(context_type)) { | |||
100 | return new long[] { CL.CL_DEVICE_TYPE_ACCELERATOR }; | |||
101 | } else if ("ALL".equals(context_type)) { | |||
102 | return new long[] { CL.CL_DEVICE_TYPE_ALL }; | |||
103 | } else { | |||
104 | return new long[] { | |||
105 | CL.CL_DEVICE_TYPE_GPU, | |||
106 | CL.CL_DEVICE_TYPE_ACCELERATOR, | |||
107 | CL.CL_DEVICE_TYPE_CPU | |||
108 | }; | |||
109 | } | |||
110 | } | |||
111 | ||||
System.out.println("platform vendor : " + platform.getInfoString(CL.CL_PLATFORM_VENDOR)); | 66 | 112 | public cl_context getContext() { | |
System.out.println("platform name : " + platform.getInfoString(CL.CL_PLATFORM_NAME)); | 67 | 113 | return context; | |
68 | 114 | } | ||
this.context = MCMHelpers.createContextFromType(platform.getId(), deviceTypes); | 69 | 115 | ||
70 | 116 | public MCMPlatform[] getPlatforms() { | ||
if (context == null) { | 71 | 117 | cl_platform_id[] platform_ids = MCMHelpers.getPlatforms(); | |
throw new RuntimeException("No OpenCL device found"); | 72 | 118 | MCMPlatform[] platforms = new MCMPlatform[platform_ids.length]; | |
} | 73 | 119 | ||
} | 74 | 120 | for (int i = 0; i < platform_ids.length; i++) { | |
75 | 121 | platforms[i] = new MCMPlatform(platform_ids[i]); | ||
/** | 76 | 122 | } | |
* Return a list of default device types to test when nothing | 77 | 123 | ||
* is indicated at the MCMContext creation. | 78 | 124 | return platforms; | |
* | 79 | 125 | } | |
* The default list indicates to use these kind of hardware in this order: | 80 | 126 | ||
* GPU, Accelerators (such as Xeon Phi) and CPU. | 81 | 127 | public MCMDevice[] getDevices() { | |
* | 82 | 128 | cl_device_id[] device_ids = MCMHelpers.getDevices(context); | |
* It can be overriden using the MCM_CONTEXT_TYPE environment variable, | 83 | 129 | MCMDevice[] devices = new MCMDevice[device_ids.length]; | |
* which admits the following values: "CPU", "GPU", "ACCELERATOR", "ALL". | 84 | 130 | for (int i = 0; i < device_ids.length; i++) { | |
* | 85 | 131 | devices[i] = new MCMDevice(device_ids[i]); | |
* "ALL" considers all the devices available on the system, but the | 86 | 132 | } | |
* order is not garanteed (even for the same type of device) and depends | 87 | 133 | return devices; | |
* of the installed runtimes. | 88 | 134 | } | |
* | 89 | 135 | ||
* @return a list of context type to try. | 90 | 136 | public MCMCommandQueue createCommandQueue(MCMCommandQueueProperty... properties) { | |
*/ | 91 | 137 | MCMCommandQueue q = new MCMCommandQueue(this, properties); | |
public static long[] getDefaultContextTypes() { | 92 | 138 | this.register(q); | |
String context_type = System.getenv("MCM_CONTEXT_TYPE"); | 93 | 139 | return q; | |
94 | 140 | } | ||
if ("CPU".equals(context_type)) { | 95 | 141 | ||
return new long[] { CL.CL_DEVICE_TYPE_CPU }; | 96 | 142 | public MCMCommandQueue createCommandQueue(MCMDevice device, MCMCommandQueueProperty... properties) { | |
} else if ("GPU".equals(context_type)) { | 97 | 143 | MCMCommandQueue q = new MCMCommandQueue(this, device, properties); | |
return new long[] { CL.CL_DEVICE_TYPE_GPU }; | 98 | 144 | this.register(q); | |
} else if ("ACCELERATOR".equals(context_type)) { | 99 | 145 | return q; | |
return new long[] { CL.CL_DEVICE_TYPE_ACCELERATOR }; | 100 | 146 | } | |
} else if ("ALL".equals(context_type)) { | 101 | 147 | ||
return new long[] { CL.CL_DEVICE_TYPE_ALL }; | 102 | 148 | public MCMProgram createProgram(String source, String... options) { | |
} else { | 103 | 149 | MCMProgram p = new MCMProgram(this, source, options); | |
return new long[] { | 104 | 150 | this.register(p); | |
CL.CL_DEVICE_TYPE_GPU, | 105 | 151 | return p; | |
CL.CL_DEVICE_TYPE_ACCELERATOR, | 106 | 152 | } | |
CL.CL_DEVICE_TYPE_CPU | 107 | 153 | ||
}; | 108 | 154 | public MCMProgram createProgram(String source, MCMProgramOptions options) { | |
} | 109 | 155 | MCMProgram p = new MCMProgram(this, source, options.toString()); | |
} | 110 | 156 | this.register(p); | |
111 | 157 | return p; | ||
public cl_context getContext() { | 112 | 158 | } | |
return context; | 113 | 159 | ||
} | 114 | 160 | public MCMProgram createProgramFromFile(String file, String... options) { | |
115 | 161 | MCMProgram p = new MCMProgram(this, MCMUtils.readFileAsString(file), options); | ||
public MCMPlatform[] getPlatforms() { | 116 | 162 | this.register(p); | |
cl_platform_id[] platform_ids = MCMHelpers.getPlatforms(); | 117 | 163 | return p; | |
MCMPlatform[] platforms = new MCMPlatform[platform_ids.length]; | 118 | 164 | } | |
119 | 165 | |||
for (int i = 0; i < platform_ids.length; i++) { | 120 | 166 | public MCMProgram createProgramFromFile(String file, MCMProgramOptions options) { | |
platforms[i] = new MCMPlatform(platform_ids[i]); | 121 | 167 | MCMProgram p = new MCMProgram(this, MCMUtils.readFileAsString(file), options.toString()); | |
} | 122 | 168 | this.register(p); | |
123 | 169 | return p; | ||
return platforms; | 124 | 170 | } | |
} | 125 | 171 | ||
126 | 172 | public MCMMemBuilder newBuffer() { | ||
public MCMDevice[] getDevices() { | 127 | 173 | return new MCMMemBuilder(this); | |
cl_device_id[] device_ids = MCMHelpers.getDevices(context); | 128 | 174 | } | |
MCMDevice[] devices = new MCMDevice[device_ids.length]; | 129 | 175 | ||
for (int i = 0; i < device_ids.length; i++) { | 130 | 176 | public MCMBufferBuilder newBuffer2() { | |
devices[i] = new MCMDevice(device_ids[i]); | 131 | 177 | return new MCMBufferBuilder(this); | |
} | 132 | 178 | } | |
return devices; | 133 | 179 | ||
180 | @Deprecated | |||
} | 134 | 181 | public MCMMem createImage2d(long flags, int channelType, int width, int height) { | |
135 | 182 | cl_image_format[] format = new cl_image_format[1]; | ||
public MCMCommandQueue createCommandQueue(MCMCommandQueueProperty... properties) { | 136 | 183 | format[0].image_channel_order = CL.CL_RGBA; | |
MCMCommandQueue q = new MCMCommandQueue(this, properties); | 137 | 184 | format[0].image_channel_data_type = channelType; | |
this.register(q); | 138 | 185 | ||
return q; | 139 | 186 | return new MCMMem(CL.clCreateImage2D(context, flags, format, width, height, 0, null, null)); | |
} | 140 | 187 | } | |
141 | 188 | |||
189 | @Deprecated | |||
public MCMCommandQueue createCommandQueue(MCMDevice device, MCMCommandQueueProperty... properties) { | 142 | 190 | public MCMMem createImage2d(long flags, int channelType, int width, int height, Buffer data) { | |
MCMCommandQueue q = new MCMCommandQueue(this, device, properties); | 143 | 191 | cl_image_format[] format = new cl_image_format[1]; | |
this.register(q); | 144 | 192 | format[0].image_channel_order = CL.CL_RGBA; | |
return q; | 145 | 193 | format[0].image_channel_data_type = channelType; | |
} | 146 | 194 | ||
147 | 195 | return new MCMMem(CL.clCreateImage2D(context, flags, format, width, height, 0, Pointer.to(data), null)); | ||
public MCMProgram createProgram(String source, String... options) { | 148 | 196 | } | |
MCMProgram p = new MCMProgram(this, source, options); | 149 | 197 | ||
198 | @Deprecated | |||
this.register(p); | 150 | 199 | public MCMMem createImage2d(long flags, int channelType, int width, int height, Pointer data) { | |
return p; | 151 | 200 | cl_image_format[] format = new cl_image_format[1]; | |
} | 152 | 201 | format[0].image_channel_order = CL.CL_RGBA; | |
153 | 202 | format[0].image_channel_data_type = channelType; | ||
public MCMProgram createProgram(String source, MCMProgramOptions options) { | 154 | 203 | ||
MCMProgram p = new MCMProgram(this, source, options.toString()); | 155 | 204 | return new MCMMem(CL.clCreateImage2D(context, flags, format, width, height, 0, data, null)); | |
this.register(p); | 156 | 205 | } | |
return p; | 157 | 206 | ||
} | 158 | 207 | @Override | |
159 | 208 | protected void releaseImpl() { | ||
public MCMProgram createProgramFromFile(String file, String... options) { | 160 | 209 | CL.clReleaseContext(context); | |
MCMProgram p = new MCMProgram(this, MCMUtils.readFileAsString(file), options); | 161 | 210 | } | |
this.register(p); | 162 | 211 | ||
return p; | 163 | 212 | } | |
} | 164 | 213 | ||
165 | ||||
public MCMProgram createProgramFromFile(String file, MCMProgramOptions options) { | 166 | |||
MCMProgram p = new MCMProgram(this, MCMUtils.readFileAsString(file), options.toString()); | 167 | |||
this.register(p); | 168 | |||
return p; | 169 | |||
} | 170 | |||
171 | ||||
public MCMMemBuilder newBuffer() { | 172 | |||
return new MCMMemBuilder(this); | 173 | |||
} | 174 | |||
175 |
src/mcmas/plugins/axb/AXBPlugin.java
View file @
1d0884a
package mcmas.plugins.axb; | 1 | 1 | package mcmas.plugins.axb; | |
2 | 2 | |||
import java.util.Random; | 3 | |||
4 | 3 | import java.util.Random; | ||
import org.jocl.Pointer; | 5 | 4 | ||
import org.perf4j.StopWatch; | 6 | 5 | import org.jocl.Pointer; | |
import org.perf4j.slf4j.Slf4JStopWatch; | 7 | 6 | import org.perf4j.StopWatch; | |
import org.slf4j.Logger; | 8 | 7 | import org.perf4j.slf4j.Slf4JStopWatch; | |
import org.slf4j.LoggerFactory; | 9 | 8 | import org.slf4j.Logger; | |
10 | 9 | import org.slf4j.LoggerFactory; | ||
import mcmas.api.MCMASContext; | 11 | 10 | ||
import mcmas.api.MCMASPlugin; | 12 | |||
import mcmas.core.MCMCommandQueue; | 13 | 11 | import mcmas.api.MCMASContext; | |
import mcmas.core.MCMCommandQueueProperty; | 14 | 12 | import mcmas.api.MCMASPlugin; | |
import mcmas.core.MCMContext; | 15 | 13 | import mcmas.core.MCMCommandQueue; | |
import mcmas.core.MCMEvent; | 16 | 14 | import mcmas.core.MCMCommandQueueProperty; | |
import mcmas.core.MCMKernel; | 17 | 15 | import mcmas.core.MCMContext; | |
import mcmas.core.MCMMem; | 18 | 16 | import mcmas.core.MCMEvent; | |
import mcmas.core.MCMProgram; | 19 | 17 | import mcmas.core.MCMKernel; | |
import mcmas.core.MCMProgramOptions; | 20 | 18 | import mcmas.core.MCMMem; | |
import mcmas.core.MCMType; | 21 | 19 | import mcmas.core.MCMProgram; | |
import mcmas.core.MCMUtils; | 22 | 20 | import mcmas.core.MCMProgramOptions; | |
23 | 21 | import mcmas.core.MCMType; | ||
public class AXBPlugin extends MCMASPlugin<AXBPlugin> { | 24 | 22 | import mcmas.core.MCMUtils; | |
25 | 23 | |||
private final Logger logger = LoggerFactory.getLogger(AXBPlugin.class); | 26 | 24 | public class AXBPlugin extends MCMASPlugin<AXBPlugin> { | |
private final StopWatch watch = new Slf4JStopWatch().setNormalPriority(Slf4JStopWatch.DEBUG_LEVEL); | 27 | 25 | ||
private final StopWatch runWatch = new Slf4JStopWatch().setNormalPriority(Slf4JStopWatch.TRACE_LEVEL); | 28 | 26 | private final Logger logger = LoggerFactory.getLogger(AXBPlugin.class); | |
29 | 27 | private final StopWatch watch = new Slf4JStopWatch().setNormalPriority(Slf4JStopWatch.DEBUG_LEVEL); | ||
@Override | 30 | 28 | private final StopWatch runWatch = new Slf4JStopWatch().setNormalPriority(Slf4JStopWatch.TRACE_LEVEL); | |
public AXBPlugin newInstance(MCMASContext context) { | 31 | 29 | ||
return null; | 32 | 30 | @Override | |
} | 33 | 31 | public AXBPlugin newInstance(MCMASContext context) { | |
34 | 32 | return null; | ||
private static final String SOURCE = "kernels/plugins/axb.cl"; | 35 | 33 | } | |
36 | 34 | |||
private MCMKernel kernel = null; | 37 | 35 | private static final String SOURCE = "kernels/plugins/axb.cl"; | |
38 | 36 | |||
public AXBPlugin(MCMASContext context) { | 39 | 37 | private MCMKernel kernel = null; | |
super(context); | 40 | 38 | ||
} | 41 | 39 | public AXBPlugin(MCMASContext context) { | |
42 | 40 | super(context); | ||
public void transform(int[] vector, float a, float b) { | 43 | 41 | } | |
watch.start("axb_transform"); | 44 | 42 | ||
MCMMem vectorMem = getContext().getContext().newBuffer().Using(vector).b(); | 45 | 43 | public void transform(int[] vector, float a, float b) { | |
transform("int", vectorMem, Pointer.to(vector), vector.length, a, b); | 46 | 44 | watch.start("axb_transform"); | |
watch.stop(); | 47 | 45 | MCMMem vectorMem = getContext().getContext().newBuffer().Using(vector).b(); | |
} | 48 | 46 | transform("int", vectorMem, Pointer.to(vector), vector.length, a, b); | |
49 | 47 | watch.stop(); | ||
public void transform(float[] vector, float a, float b) { | 50 | 48 | } | |
watch.start("axb_transform"); | 51 | 49 | ||
MCMMem vectorMem = getContext().getContext().newBuffer().Size(vector.length, MCMType.FLOAT).b(); | 52 | 50 | public void transform(float[] vector, float a, float b) { | |
transform("float", vectorMem, Pointer.to(vector), vector.length, a, b); | 53 | 51 | watch.start("axb_transform"); | |
watch.stop(); | 54 | 52 | MCMMem vectorMem = getContext().getContext().newBuffer().Size(vector.length, MCMType.FLOAT).b(); | |
} | 55 | 53 | transform("float", vectorMem, Pointer.to(vector), vector.length, a, b); | |
56 | 54 | watch.stop(); | ||
private void transform(String type, MCMMem mem, Pointer pointer, int size, Object a, Object b) { | 57 | 55 | } | |
MCMProgramOptions options = new MCMProgramOptions(); | 58 | 56 | ||
options.define("TYPE", type); | 59 | 57 | private void transform(String type, MCMMem mem, Pointer pointer, int size, Object a, Object b) { | |
60 | 58 | MCMProgramOptions options = new MCMProgramOptions(); | ||
MCMContext c = getContext().getContext(); | 61 | 59 | options.define("TYPE", type); | |
MCMCommandQueue q = getContext().getQueue(); | 62 | 60 | ||
//OCLKernel kernel = null; | 63 | 61 | MCMContext c = getContext().getContext(); | |
MCMProgram program = null; | 64 | 62 | MCMCommandQueue q = getContext().getQueue(); | |
MCMEvent finished = null; | 65 | 63 | //OCLKernel kernel = null; | |
66 | 64 | MCMProgram program = null; | ||
try { | 67 | 65 | MCMEvent finished = null; | |
runWatch.start("axb_build"); | 68 | 66 | ||
if (kernel == null) { | 69 | 67 | try { | |
c = getContext().getContext(); | 70 | 68 | runWatch.start("axb_build"); | |
71 | 69 | if (kernel == null) { | ||
q = getContext().getQueue(); | 72 | 70 | c = getContext().getContext(); | |
program = c.createProgramFromFile(SOURCE, options); | 73 | 71 | ||
74 | 72 | q = getContext().getQueue(); | ||
kernel = program.createKernel("transform"); | 75 | 73 | program = c.createProgramFromFile(SOURCE, options); | |
} | 76 | 74 | ||
runWatch.stop(); | 77 | 75 | kernel = program.createKernel("transform"); | |
78 | 76 | } | ||
runWatch.start("axb_kernel"); | 79 | 77 | runWatch.stop(); | |
80 | 78 | |||
kernel.setArguments(mem, a, b); | 81 | 79 | runWatch.start("axb_kernel"); | |
finished = q.enqueue1DKernel(kernel, size); | 82 | 80 | ||
q.blockingReadBuffer(mem, pointer, 0, mem.getSize(), finished); | 83 | 81 | kernel.setArguments(mem, a, b); | |
84 | 82 | finished = q.enqueue1DKernel(kernel, size); | ||
runWatch.stop(); | 85 | 83 | q.blockingReadBuffer(mem, pointer, 0, mem.getSize(), finished); | |
86 | 84 | |||
logger.debug(MCMUtils.formatEventStats("axb_opencl", finished)); | 87 | 85 | runWatch.stop(); | |
} finally { | 88 | 86 | ||
finished.release(); | 89 | 87 | logger.debug(MCMUtils.formatEventStats("axb_opencl", finished)); | |
mem.release(); | 90 | 88 | } finally { | |
//kernel.release(); | 91 | 89 | finished.release(); | |
92 | 90 | mem.release(); | ||
if (program != null) { | 93 | 91 | //kernel.release(); | |
program.release(); | 94 | 92 | ||
} | 95 | 93 | if (program != null) { | |
} | 96 | 94 | program.release(); | |
} | 97 | 95 | } | |
98 | 96 | } | ||
public void clampedTransform(int[] vector, float a, int b, int min, int max) { | 99 | 97 | } | |
watch.start("axb_clamped_transform " + vector.length); | 100 | 98 | ||
MCMMem vectorMem = getMCMContext().newBuffer().Using(vector).b(); | 101 | 99 | public void clampedTransform(int[] vector, float a, int b, int min, int max) { | |
clampedTransform("int", vectorMem, Pointer.to(vector), vector.length, a, b, min, max); | 102 | 100 | watch.start("axb_clamped_transform " + vector.length); | |
watch.stop(); | 103 | 101 | MCMMem vectorMem = getMCMContext().newBuffer().Using(vector).b(); | |
} | 104 | 102 | clampedTransform("int", vectorMem, Pointer.to(vector), vector.length, a, b, min, max); | |
105 | 103 | watch.stop(); | ||
public void clampedTransform(float[] vector, float a, float b, float min, float max) { | 106 | 104 | } | |
watch.start("axb_clamped_transform"); | 107 | 105 | ||
MCMMem vectorMem = getMCMContext().newBuffer().Using(vector).b(); | 108 | 106 | public void clampedTransform(float[] vector, float a, float b, float min, float max) { | |
clampedTransform("float", vectorMem, Pointer.to(vector), vector.length, a, b, min, max); | 109 | 107 | watch.start("axb_clamped_transform"); | |
watch.stop(); | 110 | 108 | MCMMem vectorMem = getMCMContext().newBuffer().Using(vector).b(); | |
} | 111 | 109 | clampedTransform("float", vectorMem, Pointer.to(vector), vector.length, a, b, min, max); | |
112 | 110 | watch.stop(); | ||
private void clampedTransform(String type, MCMMem mem, Pointer pointer, int size, Object a, Object b, | 113 | 111 | } | |
Object min, Object max) { | 114 | 112 | ||
MCMProgramOptions options = new MCMProgramOptions(); | 115 | 113 | private void clampedTransform(String type, MCMMem mem, Pointer pointer, int size, Object a, Object b, | |
options.define("TYPE", type); | 116 | 114 | Object min, Object max) { | |
117 | 115 | MCMProgramOptions options = new MCMProgramOptions(); | ||
MCMContext c = getContext().getContext(); | 118 | 116 | options.define("TYPE", type); | |
MCMCommandQueue q = getContext().getQueue(); | 119 | 117 | ||
//OCLKernel kernel = null; | 120 | 118 | MCMContext c = getContext().getContext(); | |
MCMProgram program = null; | 121 | 119 | MCMCommandQueue q = getContext().getQueue(); | |
MCMEvent finished = null; | 122 | 120 | //OCLKernel kernel = null; | |
123 | 121 | MCMProgram program = null; | ||
try { | 124 | 122 | MCMEvent finished = null; | |
runWatch.start("axb_build"); | 125 | 123 | ||
if (kernel == null) { | 126 | 124 | try { | |
c = getContext().getContext(); | 127 | 125 | runWatch.start("axb_build"); | |
128 | 126 | if (kernel == null) { | ||
q = getContext().getQueue(); | 129 | 127 | c = getContext().getContext(); | |
program = c.createProgramFromFile(SOURCE, options); | 130 | 128 | ||
131 | 129 | q = getContext().getQueue(); | ||
kernel = program.createKernel("clamped_transform"); | 132 | 130 | program = c.createProgramFromFile(SOURCE, options); | |
} | 133 | 131 | ||
runWatch.stop(); | 134 | 132 | kernel = program.createKernel("clamped_transform"); | |
135 | 133 | } | ||
runWatch.start("axb_kernel"); | 136 | 134 | runWatch.stop(); | |
137 | 135 | |||
kernel.setArguments(mem, a, b, min, max); | 138 | 136 | runWatch.start("axb_kernel"); | |
finished = q.enqueue1DKernel(kernel, size); | 139 | 137 | ||
q.blockingReadBuffer(mem, pointer, 0, mem.getSize(), finished); | 140 | 138 | kernel.setArguments(mem, a, b, min, max); | |
141 | 139 | finished = q.enqueue1DKernel(kernel, size); | ||
runWatch.stop(); | 142 | 140 | q.blockingReadBuffer(mem, pointer, 0, mem.getSize(), finished); | |
143 | 141 | |||
logger.trace(MCMUtils.formatEventStats("opencl_transform", finished)); | 144 | 142 | runWatch.stop(); | |
} finally { | 145 | 143 |
src/mcmas/plugins/gridsearch/GridSearchPlugin.java
View file @
1d0884a
package mcmas.plugins.gridsearch; | 1 | 1 | package mcmas.plugins.gridsearch; | |
2 | 2 | |||
import java.util.Arrays; | 3 | 3 | import java.util.Arrays; | |
4 | 4 | |||
import org.jocl.Pointer; | 5 | 5 | import org.jocl.Pointer; | |
import org.perf4j.StopWatch; | 6 | 6 | import org.perf4j.StopWatch; | |
import org.perf4j.slf4j.Slf4JStopWatch; | 7 | 7 | import org.perf4j.slf4j.Slf4JStopWatch; | |
import org.slf4j.Logger; | 8 | 8 | import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | 9 | 9 | import org.slf4j.LoggerFactory; | |
10 | 10 | |||
import mcmas.api.MCMASContext; | 11 | |||
import mcmas.api.MCMASPlugin; | 12 | 11 | import mcmas.api.MCMASContext; | |
//import mcmas.core.MCMChrono; | 13 | 12 | import mcmas.api.MCMASPlugin; | |
import mcmas.core.MCMCommandQueue; | 14 | 13 | //import mcmas.core.MCMChrono; | |
import mcmas.core.MCMContext; | 15 | 14 | import mcmas.core.MCMCommandQueue; | |
import mcmas.core.MCMEvent; | 16 | 15 | import mcmas.core.MCMContext; | |
import mcmas.core.MCMKernel; | 17 | 16 | import mcmas.core.MCMEvent; | |
import mcmas.core.MCMMem; | 18 | 17 | import mcmas.core.MCMKernel; | |
import mcmas.core.MCMProgram; | 19 | 18 | import mcmas.core.MCMMem; | |
import mcmas.core.MCMProgramOptions; | 20 | 19 | import mcmas.core.MCMProgram; | |
import mcmas.core.MCMUtils; | 21 | 20 | import mcmas.core.MCMProgramOptions; | |
import mcmas.utils.IntMatrix; | 22 | 21 | import mcmas.core.MCMUtils; | |
23 | 22 | import mcmas.utils.IntMatrix; | ||
public class GridSearchPlugin extends MCMASPlugin<GridSearchPlugin> { | 24 | 23 | ||
25 | 24 | public class GridSearchPlugin extends MCMASPlugin<GridSearchPlugin> { | ||
public static final String KERNEL_SOURCE = "kernels/plugins/grid_search2.cl"; | 26 | 25 | ||
public static final String KERNEL_MAX = "grid_search_max"; | 27 | 26 | public static final String KERNEL_SOURCE = "kernels/plugins/grid_search2.cl"; | |
28 | 27 | public static final String KERNEL_MAX = "grid_search_max"; | ||
private MCMKernel kernel = null; | 29 | 28 | ||
30 | 29 | private MCMKernel kernel = null; | ||
private final static Logger logger = LoggerFactory.getLogger(GridSearchPlugin.class); | 31 | 30 | ||
private final StopWatch watch = new Slf4JStopWatch().setNormalPriority(Slf4JStopWatch.DEBUG_LEVEL); | 32 | 31 | private final static Logger logger = LoggerFactory.getLogger(GridSearchPlugin.class); | |
private final StopWatch runWatch = new Slf4JStopWatch().setNormalPriority(Slf4JStopWatch.TRACE_LEVEL); | 33 | 32 | private final StopWatch watch = new Slf4JStopWatch().setNormalPriority(Slf4JStopWatch.DEBUG_LEVEL); | |
34 | 33 | private final StopWatch runWatch = new Slf4JStopWatch().setNormalPriority(Slf4JStopWatch.TRACE_LEVEL); | ||
@Override | 35 | 34 | ||
public GridSearchPlugin newInstance(MCMASContext context) { | 36 | 35 | @Override | |
return new GridSearchPlugin(context); | 37 | 36 | public GridSearchPlugin newInstance(MCMASContext context) { | |
} | 38 | 37 | return new GridSearchPlugin(context); | |
39 | 38 | } | ||
public GridSearchPlugin(MCMASContext context) { | 40 | 39 | ||
super(context); | 41 | 40 | public GridSearchPlugin(MCMASContext context) { | |
} | 42 | 41 | super(context); | |
43 | 42 | } | ||
public void findMaxCell(IntMatrix grid, int radius, int[] xPositions, int[] yPositions, int[] xResults, int[] yResults) { | 44 | 43 | ||
watch.start("gridsearch_findmax"); | 45 | 44 | public void findMaxCell(IntMatrix grid, int radius, int[] xPositions, int[] yPositions, int[] xResults, int[] yResults) { | |
MCMContext c = getContext().getContext(); | 46 | 45 | watch.start("gridsearch_findmax"); | |
MCMCommandQueue q = getContext().getQueue(); | 47 | 46 | MCMContext c = getContext().getContext(); | |
48 | 47 | MCMCommandQueue q = getContext().getQueue(); | ||
//OCLProgram program = null; | 49 | 48 | ||
//OCLKernel kernel = null; | 50 | 49 | //OCLProgram program = null; | |
MCMMem gridMem = null; | 51 | 50 | //OCLKernel kernel = null; | |
MCMMem xPositionsMem = null; | 52 | 51 | MCMMem gridMem = null; | |
MCMMem yPositionsMem = null; | 53 | 52 | MCMMem xPositionsMem = null; | |
MCMMem xResultsMem = null; | 54 | 53 | MCMMem yPositionsMem = null; | |
MCMMem yResultsMem = null; | 55 | 54 | MCMMem xResultsMem = null; | |
56 | 55 | MCMMem yResultsMem = null; | ||
MCMProgramOptions options = new MCMProgramOptions(); | 57 | 56 | ||
options.define("WIDTH", grid.getWidth()); | 58 | 57 | MCMProgramOptions options = new MCMProgramOptions(); | |
options.define("HEIGHT", grid.getHeight()); | 59 | 58 | options.define("WIDTH", grid.getWidth()); | |
options.define("TYPE", "int"); | 60 | 59 | options.define("HEIGHT", grid.getHeight()); | |
61 | 60 | options.define("TYPE", "int"); | ||
//System.out.println(options); | 62 | 61 | ||
63 | 62 | //System.out.println(options); | ||
MCMProgram program = null; | 64 | 63 | ||
//OCLKernel kernel = null; | 65 | 64 | MCMProgram program = null; | |
MCMEvent finished = null; | 66 | 65 | //OCLKernel kernel = null; | |
67 | 66 | MCMEvent finished = null; | ||
try { | 68 | 67 | ||
runWatch.start("gridsearch_build"); | 69 | 68 | try { | |
if (kernel == null) { | 70 | 69 | runWatch.start("gridsearch_build"); | |
program = c.createProgramFromFile(KERNEL_SOURCE, options); | 71 | 70 | if (kernel == null) { | |
kernel = program.createKernel(KERNEL_MAX); | 72 | 71 | program = c.createProgramFromFile(KERNEL_SOURCE, options); | |
} | 73 | 72 | kernel = program.createKernel(KERNEL_MAX); | |
runWatch.stop(); | 74 | 73 | } | |
75 | 74 | runWatch.stop(); | ||
runWatch.start("gridsearch_kernel"); | 76 | 75 | ||
77 | 76 | runWatch.start("gridsearch_kernel"); | ||
gridMem = c.newBuffer().Using(grid.getStorage()).b(); | 78 | 77 | ||
xPositionsMem = c.newBuffer().Using(xPositions).b(); | 79 | 78 | gridMem = c.newBuffer().Using(grid.getStorage()).b(); | |
yPositionsMem = c.newBuffer().Using(yPositions).b(); | 80 | 79 | xPositionsMem = c.newBuffer().Using(xPositions).b(); | |
xResultsMem = c.newBuffer().Size(xPositionsMem.getSize()).b(); | 81 | 80 | yPositionsMem = c.newBuffer().Using(yPositions).b(); | |
yResultsMem = c.newBuffer().Size(yPositionsMem.getSize()).b(); | 82 | 81 | xResultsMem = c.newBuffer().Size(xPositionsMem.getSize()).b(); | |
83 | 82 | yResultsMem = c.newBuffer().Size(yPositionsMem.getSize()).b(); | ||
kernel.setArguments(gridMem, radius, xPositionsMem, yPositionsMem, xResultsMem, yResultsMem); | 84 | 83 | ||
85 | 84 | kernel.setArguments(gridMem, radius, xPositionsMem, yPositionsMem, xResultsMem, yResultsMem); | ||
finished = q.enqueue1DKernel(kernel, xPositions.length); | 86 | 85 | ||
87 | 86 | finished = q.enqueue1DKernel(kernel, xPositions.length); | ||
q.blockingReadBuffer(xResultsMem, Pointer.to(xResults), 0, xResultsMem.getSize(), finished); | 88 | 87 | ||
q.blockingReadBuffer(yResultsMem, Pointer.to(yResults), 0, yResultsMem.getSize(), finished); | 89 | 88 | q.blockingReadBuffer(xResultsMem, Pointer.to(xResults), 0, xResultsMem.getSize(), finished); | |
90 | 89 | q.blockingReadBuffer(yResultsMem, Pointer.to(yResults), 0, yResultsMem.getSize(), finished); | ||
runWatch.stop(); | 91 | 90 | ||
92 | 91 | runWatch.stop(); | ||
logger.debug(MCMUtils.formatEventStats("gridsearch_opencl", finished)); | 93 | 92 | ||
94 | 93 | logger.debug(MCMUtils.formatEventStats("gridsearch_opencl", finished)); | ||
watch.stop(); | 95 | 94 | ||
} catch (Exception e) { | 96 | 95 | watch.stop(); | |
System.err.println(e); | 97 | 96 | } catch (Exception e) { | |
e.printStackTrace(); | 98 | 97 | System.err.println(e); | |
} finally { | 99 | 98 | e.printStackTrace(); | |
gridMem.release(); | 100 | 99 | } finally { | |
xPositionsMem.release(); | 101 | 100 | gridMem.release(); | |
yPositionsMem.release(); | 102 | 101 | xPositionsMem.release(); | |
xResultsMem.release(); | 103 | 102 | yPositionsMem.release(); | |
yResultsMem.release(); | 104 | 103 | xResultsMem.release(); | |
finished.release(); | 105 | 104 | yResultsMem.release(); | |
//kernel.release(); | 106 | 105 | finished.release(); | |
if (program != null) { | 107 | 106 | //kernel.release(); | |
program.release(); | 108 | 107 | if (program != null) { | |
} | 109 | 108 | program.release(); | |
} | 110 | 109 | } | |
} | 111 | 110 | } | |
112 | 111 | } | ||
/* | 113 | 112 | ||
public void findNearestCellNot(float[] vector, int radius, float value, int[] xPositions, int[] yPositions, int[] xResults, int[] yResults) { | 114 | 113 | /* | |
OCLContext c = getContext().getContext(); | 115 | 114 | public void findNearestCellNot(float[] vector, int radius, float value, int[] xPositions, int[] yPositions, int[] xResults, int[] yResults) { | |
OCLCommandQueue q = getContext().getQueue(); | 116 | 115 | OCLContext c = getContext().getContext(); | |
117 | 116 | OCLCommandQueue q = getContext().getQueue(); | ||
OCLProgram program = null; | 118 | 117 | ||
OCLKernel kernel = null; | 119 | 118 | OCLProgram program = null; | |
OCLMem xPositionsMem = null; | 120 | 119 | OCLKernel kernel = null; | |
OCLMem yPositionsMem = null; | 121 | 120 | OCLMem xPositionsMem = null; | |
OCLMem xResultsMem = null; | 122 | 121 | OCLMem yPositionsMem = null; | |
OCLMem yResultsMem = null; | 123 | 122 | OCLMem xResultsMem = null; | |
124 | 123 | OCLMem yResultsMem = null; | ||
try { | 125 | 124 | ||
program = c.createProgramFromFile(SOURCE_NEAREST); | 126 | 125 | try { | |
kernel = program.createKernel("search_float_nearest"); | 127 | 126 | program = c.createProgramFromFile(SOURCE_NEAREST); | |
128 | 127 | kernel = program.createKernel("search_float_nearest"); | ||
xPositionsMem = c.newBuffer().Using(xPositions).b(); | 129 | 128 | ||
yPositionsMem = c.newBuffer().Using(yPositions).b(); | 130 | 129 | xPositionsMem = c.newBuffer().Using(xPositions).b(); | |
xResultsMem = c.newBuffer().Size(xResults.length, OCLType.FLOAT).b(); | 131 | 130 | yPositionsMem = c.newBuffer().Using(yPositions).b(); | |
yResultsMem = c.newBuffer().Size(yResults.length, OCLType.FLOAT).b(); | 132 | 131 | xResultsMem = c.newBuffer().Size(xResults.length, OCLType.FLOAT).b(); | |
133 | 132 | yResultsMem = c.newBuffer().Size(yResults.length, OCLType.FLOAT).b(); | ||
kernel.setArguments(vector, radius, value, xPositionsMem, yPositionsMem, xResultsMem, yResultsMem); | 134 | 133 | ||
OCLEvent finished = q.enqueue1DKernel(kernel, vector.length); | 135 | 134 | kernel.setArguments(vector, radius, value, xPositionsMem, yPositionsMem, xResultsMem, yResultsMem); | |
136 | 135 | OCLEvent finished = q.enqueue1DKernel(kernel, vector.length); | ||
OCLEvent r1 = q.enqueueReadBuffer(xResultsMem, Pointer.to(xResults), 0, xResultsMem.getSize(), finished); | 137 | 136 | ||
OCLEvent r2 = q.enqueueReadBuffer(yResultsMem, Pointer.to(yResults), 0, yResultsMem.getSize(), finished); | 138 | 137 | OCLEvent r1 = q.enqueueReadBuffer(xResultsMem, Pointer.to(xResults), 0, xResultsMem.getSize(), finished); | |
139 | 138 | OCLEvent r2 = q.enqueueReadBuffer(yResultsMem, Pointer.to(yResults), 0, yResultsMem.getSize(), finished); | ||
OCLEvent.waitFor(r1, r2); | 140 | 139 | ||
} finally { | 141 | 140 | OCLEvent.waitFor(r1, r2); | |
xPositionsMem.release(); | 142 | 141 | } finally { |
src/preypredator/PPRuntimeGPU.java
View file @
1d0884a
package preypredator; | 1 | 1 | package preypredator; | |
2 | 2 | |||
import mcmas.api.MCMASContext; | 3 | |||
import mcmas.core.MCMCommandQueueProperty; | 4 | 3 | import mcmas.api.MCMASContext; | |
import mcmas.plugins.axb.AXBPlugin; | 5 | 4 | import mcmas.core.MCMCommandQueueProperty; | |
import mcmas.plugins.gridsearch.GridSearchPlugin; | 6 | 5 | import mcmas.plugins.axb.AXBPlugin; | |
7 | 6 | import mcmas.plugins.gridsearch.GridSearchPlugin; | ||
8 | 7 | |||
public class PPRuntimeGPU implements PPRuntime { | 9 | 8 | ||
10 | 9 | public class PPRuntimeGPU implements PPRuntime { | ||
private final MCMASContext context; | 11 | 10 | ||
private final AXBPlugin axbPlugin; | 12 | 11 | private final MCMASContext context; | |
private final GridSearchPlugin searchPlugin; | 13 | 12 | private final AXBPlugin axbPlugin; | |
14 | 13 | private final GridSearchPlugin searchPlugin; | ||
public PPRuntimeGPU() { | 15 | 14 | ||
this.context = new MCMASContext(MCMCommandQueueProperty.ENABLE_PROFILING); | 16 | 15 | public PPRuntimeGPU() { | |
this.axbPlugin = new AXBPlugin(context); | 17 | 16 | this.context = new MCMASContext(MCMCommandQueueProperty.ENABLE_PROFILING); | |
this.searchPlugin = new GridSearchPlugin(context); | 18 | 17 | this.axbPlugin = new AXBPlugin(context); | |
} | 19 | 18 | this.searchPlugin = new GridSearchPlugin(context); | |
20 | 19 | } | ||
@Override | 21 | 20 | ||
public void growGrass(PPGrid grass, float factor, int growth, int min, int max) { | 22 | 21 | @Override | |
axbPlugin.clampedTransform(grass.getStorage(), factor, growth, min, max); | 23 | 22 | public void growGrass(PPGrid grass, float factor, int growth, int min, int max) { | |
} | 24 | 23 | axbPlugin.clampedTransform(grass.getStorage(), factor, growth, min, max); | |
25 | 24 | } | ||
@Override | 26 | 25 | ||
public void selectMaxTarget(PPGrid grid, int radius, int[] xPositions, | 27 | 26 | @Override | |
int[] yPositions, int[] newXPositions, | 28 | 27 | public void selectMaxTarget(PPGrid grid, int radius, int[] xPositions, | |
int[] newYPositions) { | 29 | 28 | int[] yPositions, int[] newXPositions, | |
searchPlugin.findMaxCell(grid, radius, xPositions, yPositions, newXPositions, newYPositions); | 30 | 29 | int[] newYPositions) { | |
} | 31 | 30 | searchPlugin.findMaxCell(grid, radius, xPositions, yPositions, newXPositions, newYPositions); | |
32 | 31 | } | ||
@Override | 33 | 32 |
src/preypredator/PreyPredator2.java
View file @
1d0884a
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; | |
15 | ||||
public class PreyPredator2 { | 16 | 15 | ||
17 | 16 | public class PreyPredator2 { | ||
private final static Logger logger = LoggerFactory.getLogger(PreyPredator2.class); | 18 | 17 | ||
private final StopWatch watch = new Slf4JStopWatch(); | 19 | 18 | private final static Logger logger = LoggerFactory.getLogger(PreyPredator2.class); | |
private final StopWatch runWatch = new Slf4JStopWatch(); | 20 | 19 | private final StopWatch watch = new Slf4JStopWatch(); | |
21 | 20 | private final StopWatch runWatch = new Slf4JStopWatch(); | ||
//public static final int WIDTH = 1000; | 22 | 21 | ||
//public static final int HEIGHT = 1000; | 23 | 22 | //public static final int WIDTH = 1000; | |
24 | 23 | //public static final int HEIGHT = 1000; | ||
public static final int GRASS_MIN = 0; | 25 | 24 | ||
public static final int GRASS_MAX = 100; | 26 | 25 | public static final int GRASS_MIN = 0; | |
public static final int GRASS_ORIG = 20; | 27 | 26 | public static final int GRASS_MAX = 100; | |
public static final float GRASS_FACT = 1.0f; | 28 | 27 | public static final int GRASS_ORIG = 20; | |
public static final int GRASS_GROWTH = 10; | 29 | 28 | public static final float GRASS_FACT = 1.0f; | |
30 | 29 | public static final int GRASS_GROWTH = 10; | ||
public static final int PREY_ORIG = 10; | 31 | 30 | ||
//public static final int PREY_RADIUS = 20; | 32 | 31 | public static final int PREY_ORIG = 10; | |
public static final int PREY_MAX = 50; | 33 | 32 | //public static final int PREY_RADIUS = 20; | |
34 | 33 | public static final int PREY_MAX = 50; | ||
public static final int PRED_ORIG = 10; | 35 | 34 | ||
//public static final int PRED_RADIUS = 20; | 36 | 35 | public static final int PRED_ORIG = 10; | |
public static final int PRED_MAX = 100; | 37 | 36 | //public static final int PRED_RADIUS = 20; | |
38 | 37 | public static final int PRED_MAX = 100; | ||
// Attributes | 39 | 38 | ||
40 | 39 | // Attributes | ||
private final PPGrid grass; | 41 | 40 | ||
private final PPGrid preys; | 42 | 41 | private final PPGrid grass; | |
private final PPGrid preds; | 43 | 42 | private final PPGrid preys; | |
44 | 43 | private final PPGrid preds; | ||
45 | 44 | |||
// Temporary buffers used in each step | 46 | 45 | ||
47 | 46 | // Temporary buffers used in each step | ||
private final IntArrayList preysX; | 48 | 47 | ||
private final IntArrayList preysY; | 49 | 48 | private final IntArrayList preysX; | |
private final IntArrayList predsX; | 50 | 49 | private final IntArrayList preysY; | |
private final IntArrayList predsY; | 51 | 50 | private final IntArrayList predsX; | |
52 | 51 | private final IntArrayList predsY; | ||
private final int nbPreds; | 53 | 52 | ||
private final int nbPreys; | 54 | 53 | private final int nbPreds; | |
private final int range; | 55 | 54 | private final int nbPreys; | |
private final PPRuntime runtime; | 56 | 55 | private final int range; | |
57 | 56 | private final PPRuntime runtime; | ||
//OCLChrono chrono = new OCLChrono("preypredator"); | 58 | 57 | ||
59 | 58 | //OCLChrono chrono = new OCLChrono("preypredator"); | ||
private final int size; | 60 | 59 | ||
61 | 60 | private final int size; | ||
public PreyPredator2(int size, int nbPreys, int nbPredators, int range, PPRuntime runtime) { | 62 | 61 | ||
this.grass = new PPGrid(size, size); | 63 | 62 | public PreyPredator2(int size, int nbPreys, int nbPredators, int range, PPRuntime runtime) { | |
this.preys = new PPGrid(size, size); | 64 | 63 | this.grass = new PPGrid(size, size); | |
this.preds = new PPGrid(size, size); | 65 | 64 | this.preys = new PPGrid(size, size); | |
66 | 65 | this.preds = new PPGrid(size, size); | ||
this.size = size; | 67 | 66 | ||
this.nbPreds = nbPredators; | 68 | 67 | this.size = size; | |
this.nbPreys = nbPreys; | 69 | 68 | this.nbPreds = nbPredators; | |
this.range = range; | 70 | 69 | this.nbPreys = nbPreys; | |
71 | 70 | this.range = range; | ||
this.preysX = new IntArrayList(); | 72 | 71 | ||
this.preysY = new IntArrayList(); | 73 | 72 | this.preysX = new IntArrayList(); | |
this.predsX = new IntArrayList(); | 74 | 73 | this.preysY = new IntArrayList(); | |
this.predsY = new IntArrayList(); | 75 | 74 | this.predsX = new IntArrayList(); | |
76 | 75 | this.predsY = new IntArrayList(); | ||
this.runtime = runtime; | 77 | 76 | ||
} | 78 | 77 | this.runtime = runtime; | |
79 | 78 | } | ||
public void setup() { | 80 | 79 | ||
watch.start("pp_setup"); | 81 | 80 | public void setup() { | |
initGrid(grass.getStorage(), (int) (grass.getStorage().length * 0.8), GRASS_ORIG); | 82 | 81 | watch.start("pp_setup"); | |
initGrid(preys.getStorage(), nbPreys, PREY_ORIG); | 83 | 82 | initGrid(grass.getStorage(), (int) (grass.getStorage().length * 0.8), GRASS_ORIG); | |
initGrid(preds.getStorage(), nbPreds, PRED_ORIG); | 84 | 83 | initGrid(preys.getStorage(), nbPreys, PREY_ORIG); | |
watch.stop(); | 85 | 84 | initGrid(preds.getStorage(), nbPreds, PRED_ORIG); | |
} | 86 | 85 | watch.stop(); | |
87 | 86 | } | ||
public void update_positions() { | 88 | 87 | ||
//chrono.start("update_positions"); | 89 | 88 | public void update_positions() { | |
90 | 89 | //chrono.start("update_positions"); | ||
preysX.clear(); | 91 | 90 | ||
preysY.clear(); | 92 | 91 | preysX.clear(); | |
predsX.clear(); | 93 | 92 | preysY.clear(); | |
predsY.clear(); | 94 | 93 | predsX.clear(); | |
95 | 94 | predsY.clear(); | ||
for (int j = 0; j < size; j++) { | 96 | 95 | ||
for (int i = 0; i < size; i++) { | 97 | 96 | for (int j = 0; j < size; j++) { | |
if (preys.get(i, j) > 0) { | 98 | 97 | for (int i = 0; i < size; i++) { | |
preysX.add(i); | 99 | 98 | if (preys.get(i, j) > 0) { | |
preysY.add(j); | 100 | 99 | preysX.add(i); | |
} | 101 | 100 | preysY.add(j); | |
102 | 101 | } | ||
if (preds.get(i, j) > 0) { | 103 | 102 | ||
predsX.add(i); | 104 | 103 | if (preds.get(i, j) > 0) { | |
predsY.add(j); | 105 | 104 | predsX.add(i); | |
} | 106 | 105 | predsY.add(j); | |
} | 107 | 106 | } | |
} | 108 | 107 | } | |
109 | 108 | } | ||
//System.out.println(chrono.stop()); | 110 | 109 | ||
} | 111 | 110 | //System.out.println(chrono.stop()); | |
112 | 111 | } | ||
public void step() { | 113 | 112 | ||
watch.start("pp_step"); | 114 | 113 | public void step() { | |
//OCLChrono stepChrono = new OCLChrono("step"); | 115 | 114 | watch.start("pp_step"); | |
//stepChrono.start(); | 116 | 115 | //OCLChrono stepChrono = new OCLChrono("step"); | |
117 | 116 | //stepChrono.start(); | ||
update_positions(); | 118 | 117 | ||
119 | 118 | update_positions(); | ||
runWatch.start("pp_grass"); | 120 | 119 | ||
runtime.growGrass(grass, GRASS_FACT, GRASS_GROWTH, GRASS_MIN, GRASS_MAX); | 121 | 120 | runWatch.start("pp_grass"); | |
runWatch.stop(); | 122 | 121 | runtime.growGrass(grass, GRASS_FACT, GRASS_GROWTH, GRASS_MIN, GRASS_MAX); | |
123 | 122 | runWatch.stop(); | ||
int[] newPreysX = new int[preysX.size()]; | 124 | 123 | ||
int[] newPreysY = new int[preysY.size()]; | 125 | 124 | int[] newPreysX = new int[preysX.size()]; | |
int[] newPredsX = new int[predsX.size()]; | 126 | 125 | int[] newPreysY = new int[preysY.size()]; | |
int[] newPredsY = new int[predsY.size()]; | 127 | 126 | int[] newPredsX = new int[predsX.size()]; | |
128 | 127 | int[] newPredsY = new int[predsY.size()]; | ||
runWatch.start("pp_move_preys"); | 129 | 128 | ||
if (preysX.size() > 0) { | 130 | 129 | runWatch.start("pp_move_preys"); | |
131 | 130 | if (preysX.size() > 0) { | ||
runtime.selectMaxTarget(grass, range, | 132 | 131 | ||
preysX.toIntArray(), preysY.toIntArray(), | 133 | 132 | runtime.selectMaxTarget(grass, range, | |
newPreysX, newPreysY); | 134 | 133 | preysX.toIntArray(), preysY.toIntArray(), | |
} | 135 | 134 | newPreysX, newPreysY); | |
runWatch.stop(); | 136 | 135 | } | |
137 | 136 | runWatch.stop(); | ||
/* | 138 | 137 | ||
for (int i = 0; i < preysX.size(); i++) { | 139 | 138 | /* | |
System.out.println("Prey: (" + preysX.get(i) + ", " + preysY.get(i) + ") => (" + | 140 | 139 | for (int i = 0; i < preysX.size(); i++) { | |
newPreysX[i] + ", " + newPreysY[i] + ")"); | 141 | 140 | System.out.println("Prey: (" + preysX.get(i) + ", " + preysY.get(i) + ") => (" + | |
}*/ | 142 | 141 | newPreysX[i] + ", " + newPreysY[i] + ")"); | |
143 | 142 | }*/ | ||
// PREYS | 144 | 143 | ||
145 | 144 | // PREYS | ||
for (int i = 0; i < preysX.size(); i++) { | 146 | 145 | ||
final int oldPos = preys.offset(preysX.get(i), preysY.get(i)); | 147 | 146 | for (int i = 0; i < preysX.size(); i++) { | |
final int newPos = preys.offset(newPreysX[i], newPreysY[i]); | 148 | 147 | final int oldPos = preys.offset(preysX.get(i), preysY.get(i)); | |
149 | 148 | final int newPos = preys.offset(newPreysX[i], newPreysY[i]); | ||
// Check an eventual conflict with another prey | 150 | 149 | ||
if (preys.get(newPos) > 0) { | 151 | 150 | // Check an eventual conflict with another prey | |
continue; | 152 | 151 | if (preys.get(newPos) > 0) { | |
} | 153 | 152 | continue; | |
154 | 153 | } | ||
// Movement | 155 | 154 | ||
preys.set(newPos, preys.get(oldPos)); | 156 | 155 | // Movement | |
preys.set(oldPos, 0); | 157 | 156 | preys.set(newPos, preys.get(oldPos)); | |
158 | 157 | preys.set(oldPos, 0); | ||
// Eating | 159 | 158 | ||
if (grass.get(newPos) >= 0) { | 160 | 159 | // Eating | |
preys.set(newPos, clamp(preys.get(newPos) + grass.get(newPos), 0, PREY_MAX)); | 161 | 160 | if (grass.get(newPos) >= 0) { | |
grass.set(newPos, 0); | 162 | 161 | preys.set(newPos, clamp(preys.get(newPos) + grass.get(newPos), 0, PREY_MAX)); | |
} | 163 | 162 | grass.set(newPos, 0); | |
164 | 163 | } | ||
// Reproduction | 165 | 164 | ||
if (preys.get(newPos) == PREY_MAX) { | 166 | 165 | // Reproduction | |
preys.set(oldPos, PREY_ORIG); | 167 | 166 | if (preys.get(newPos) == PREY_MAX) { | |
} | 168 | 167 | preys.set(oldPos, PREY_ORIG); | |
} | 169 | 168 | } | |
170 | 169 | } | ||
runWatch.start("pp_move_preds"); | 171 | 170 | ||
if (predsX.size() > 0) { | 172 | 171 | runWatch.start("pp_move_preds"); | |
int [] predsXbis = predsX.toIntArray(); | 173 | 172 | if (predsX.size() > 0) { | |
int [] predsYbis = predsY.toIntArray(); | 174 | 173 | int [] predsXbis = predsX.toIntArray(); | |
175 | 174 | int [] predsYbis = predsY.toIntArray(); | ||
runtime.selectMaxTarget(preys, (int) Math.round(range * 1.5), | 176 | 175 | ||
predsXbis, predsYbis, | 177 | 176 | runtime.selectMaxTarget(preys, (int) Math.round(range * 1.5), | |
newPredsX, newPredsY); | 178 | 177 | predsXbis, predsYbis, | |
} | 179 | 178 | newPredsX, newPredsY); | |
runWatch.stop(); | 180 | 179 | } | |
181 | 180 | runWatch.stop(); | ||
/*for (int i = 0; i < predsX.size(); i++) { | 182 | 181 | ||
System.out.println("Pred: (" + predsX.get(i) + ", " + predsY.get(i) + ") => (" + | 183 | 182 | /*for (int i = 0; i < predsX.size(); i++) { | |
newPredsX[i] + ", " + newPredsY[i] + ")"); | 184 | 183 | System.out.println("Pred: (" + predsX.get(i) + ", " + predsY.get(i) + ") => (" + | |
}*/ | 185 | 184 | newPredsX[i] + ", " + newPredsY[i] + ")"); | |
186 | 185 | }*/ | ||
// PREDATOR | 187 | 186 | ||
188 | 187 | // PREDATOR | ||
for (int i = 0; i < predsX.size(); i++) { | 189 | 188 | ||
final int pos = predsY.get(i) * size + predsX.get(i); | 190 | 189 | for (int i = 0; i < predsX.size(); i++) { | |
final int newPos = newPredsY[i] * size + newPredsX[i]; | 191 | 190 | final int pos = predsY.get(i) * size + predsX.get(i); | |
192 | 191 | final int newPos = newPredsY[i] * size + newPredsX[i]; | ||
// Check an eventual conflict with another predator | 193 | 192 | ||
if (preds.get(newPos) > 0) { | 194 | 193 | // Check an eventual conflict with another predator | |
continue; | 195 | 194 | if (preds.get(newPos) > 0) { | |
} | 196 | 195 | continue; | |
197 | 196 | } | ||
// Movement | 198 | 197 | ||
preds.set(newPos, preds.get(pos)); | 199 | 198 | // Movement | |
preds.set(pos, 0); | 200 | 199 | preds.set(newPos, preds.get(pos)); | |
201 | 200 | preds.set(pos, 0); | ||
// Eating | 202 | 201 | ||
if (preys.get(newPos) >= 0) { | 203 | 202 | // Eating | |
preds.set(newPos, clamp(preds.get(newPos) + preys.get(newPos), 0, PRED_MAX)); | 204 | 203 | if (preys.get(newPos) >= 0) { | |
preys.set(newPos, 0); | 205 | 204 | preds.set(newPos, clamp(preds.get(newPos) + preys.get(newPos), 0, PRED_MAX)); | |
} | 206 | 205 | preys.set(newPos, 0); | |
207 | 206 | } | ||
// Reproduction | 208 | 207 | ||
if (preds.get(newPos) == PRED_MAX) { | 209 | 208 | // Reproduction | |
preds.set(pos, PRED_ORIG); | 210 | 209 | if (preds.get(newPos) == PRED_MAX) { | |
} | 211 | 210 | preds.set(pos, PRED_ORIG); | |
} | 212 | 211 | } | |
213 | 212 | } | ||
/*System.out.println(grass); | 214 | 213 | ||
System.out.println(preys);*/ | 215 | 214 | /*System.out.println(grass); | |
//System.out.println(preds); | 216 | 215 | System.out.println(preys);*/ | |
//logger.info(stepChrono.stop().toString()); | 217 | 216 | //System.out.println(preds); | |
watch.stop(); | 218 | 217 | //logger.info(stepChrono.stop().toString()); | |
} | 219 | 218 | watch.stop(); | |
220 | 219 | } | ||
private void initGrid(int[] grid, int nbCells, int value) { | 221 | 220 | ||
int remainingCells = nbCells; | 222 | 221 | private void initGrid(int[] grid, int nbCells, int value) { | |
Random rng = new Random(); | 223 | 222 | int remainingCells = nbCells; | |
224 | 223 | Random rng = new Random(); | ||
while (remainingCells > 0) { | 225 | 224 | ||
final int pos = rng.nextInt(grid.length); | 226 | 225 | while (remainingCells > 0) { | |
227 | 226 | final int pos = rng.nextInt(grid.length); | ||
if (grid[pos] == 0) { | 228 | 227 | ||
grid[pos] = value; | 229 | 228 | if (grid[pos] == 0) { | |
remainingCells--; | 230 | 229 | grid[pos] = value; | |
} | 231 | 230 | remainingCells--; | |
} | 232 | 231 | } | |
} | 233 | 232 | } | |
234 | 233 | } | ||
// Helpers | 235 | 234 |
utils/mcmas/infos.sh
View file @
1d0884a
#!/bin/bash | 1 | 1 | #!/bin/bash | |
2 | 2 | |||
. "${BASH_SOURCE%/*}/common.sh" | 3 | 3 | . "${BASH_SOURCE%/*}/common.sh" | |
4 | 4 | |||
if [ $# -lt 1 ]; then | 5 | 5 | if [ $# -lt 1 ]; then | |
echo "Usage: $0 <directory>" | 6 | 6 | echo "Usage: $0 <directory>" | |
exit 1 | 7 | 7 | exit 1 | |
fi | 8 | 8 | fi | |
9 | 9 | |||
DEST_DIR="$1" | 10 | 10 | DEST_DIR="$1" | |
11 | 11 | |||
if [ ! -d $DEST_DIR ]; then | 12 | 12 | if [ ! -d $DEST_DIR ]; then | |
mkdir -p $DEST_DIR | 13 | 13 | mkdir -p $DEST_DIR | |
fi | 14 | 14 | fi | |
15 | 15 | |||
cat /etc/issue > $DEST_DIR/platform_issue | 16 | 16 | cat /etc/issue > $DEST_DIR/platform_issue |
utils/mcmas/list.sh
View file @
1d0884a
File was created | 1 | #!/bin/bash | ||
2 | ||||
3 | . "${BASH_SOURCE%/*}/common.sh" | |||
4 | ||||
5 | echo $JAVA | |||
6 | $JAVA mcmas.core.MCMUtils | |||
#!/bin/bash | 1 | 7 | ||
2 | ||||
. "${BASH_SOURCE%/*}/common.sh" | 3 | |||
4 |