diff --git a/core/src/main/java/matrixstudio/kernel/CLUtil.java b/core/src/main/java/matrixstudio/kernel/CLUtil.java index eb9cb77..dd02575 100644 --- a/core/src/main/java/matrixstudio/kernel/CLUtil.java +++ b/core/src/main/java/matrixstudio/kernel/CLUtil.java @@ -1,6 +1,8 @@ package matrixstudio.kernel; import matrixstudio.model.Device; +import matrixstudio.model.PlatformCL; + import org.jocl.CL; import org.jocl.Pointer; import org.jocl.cl_device_id; @@ -155,8 +157,48 @@ public static Device getDevice(cl_device_id device) { } } + /** + * Get list of platform + * @return + */ + + public static List getListPlatform(){ + List listPlatform = new ArrayList<>(); + + int numPlatforms[] = new int[1]; + clGetPlatformIDs(0, null, numPlatforms); + + // Obtain the platform IDs + cl_platform_id platforms[] = new cl_platform_id[numPlatforms[0]]; + clGetPlatformIDs(platforms.length, platforms, null); + + listPlatform.addAll(Arrays.asList(platforms)); + + return listPlatform; + + } + + /** + * Get platform by platformId + * @param platformId + * @return + */ + + public static PlatformCL getPlatform(cl_platform_id platformId) { + + PlatformCL res = new PlatformCL(platformId); + + return res; + } + public static void main(String[] args) { cl_device_id id = selectHardware(Device.CPU).get(0); System.out.println(getLong(id, CL.CL_DEVICE_MAX_WORK_GROUP_SIZE)); + + List list = getListPlatform(); + + for(int i = 0; i < list.size(); i++) { + System.out.println(list.get(i)); + } } } diff --git a/core/src/main/java/matrixstudio/kernel/Simulator.java b/core/src/main/java/matrixstudio/kernel/Simulator.java index 33ab7b1..9b47d5e 100644 --- a/core/src/main/java/matrixstudio/kernel/Simulator.java +++ b/core/src/main/java/matrixstudio/kernel/Simulator.java @@ -254,12 +254,28 @@ private void initMatrices() { } private void initCL() throws EvaluationException, ParseException { + final Model model = getModel(); + final Scheduler scheduler = model.getScheduler(); + long numBytes[] = new long[1]; - + // Creation of an OpenCL context on GPU log.log("Obtaining platform..."); - cl_platform_id platforms[] = new cl_platform_id[1]; - clGetPlatformIDs(platforms.length, platforms, null); + cl_platform_id platforms[]; + + //Condition if platform IDs is null or not + if(scheduler.getPlatform().getPlatformId() != null) { + platforms = new cl_platform_id[1]; + platforms[0] = scheduler.getPlatform().getPlatformId(); + + }else { + + platforms = new cl_platform_id[1]; + clGetPlatformIDs(platforms.length, platforms, null); + } + + + if(platforms[0]==null) { throw new CLException("No OpenCL plateform found. Impossible to compile the code."); } else { @@ -272,8 +288,6 @@ private void initCL() throws EvaluationException, ParseException { // Enable exceptions and subsequently get error checks CL.setExceptionsEnabled(true); - final Model model = getModel(); - final Scheduler scheduler = model.getScheduler(); // checks if device exist final List hardwareList = CLUtil.selectHardware(scheduler.getDevice()); diff --git a/core/src/main/java/matrixstudio/model/PlatformCL.java b/core/src/main/java/matrixstudio/model/PlatformCL.java new file mode 100644 index 0000000..8fbb6a4 --- /dev/null +++ b/core/src/main/java/matrixstudio/model/PlatformCL.java @@ -0,0 +1,79 @@ +package matrixstudio.model; + +import java.util.ArrayList; +import java.util.List; + +import org.jocl.cl_platform_id; + +import matrixstudio.kernel.CLUtil; + +/** + * Classe Platform + *

The Platform class is used to retrieve the platform id

+ * + * @author lesech + */ + +/** + * + */ + +public class PlatformCL { + + /** + * platform id + * + * @see PlatformCL#getPlatformId() + * @see PlatformCL#setPlatformId(cl_platform_id) + * @see PlatformCL#setPlatformId(int) + */ + private cl_platform_id platformId; + + /** + * Constructor of the Platform class with a variable of type cl_platform as parameter + * @param platformId + */ + + public PlatformCL(cl_platform_id platformId) { + this.platformId = platformId; + } + + /** + * + *

Default constructor of the platform class

+ */ + public PlatformCL() { + this.platformId = null; + } + + /** + * get platform id's + * @return + */ + public cl_platform_id getPlatformId() { + return this.platformId; + } + + /** + * set platform Id + * @param platformId + */ + + public void setPlatformId(cl_platform_id platformId) { + this.platformId = platformId; + } + + /** + * set platformId with Index + * @param index + */ + + public void setPlatformId(int index) { + List listPlatform = CLUtil.getListPlatform(); + cl_platform_id res = listPlatform.get(index); + + this.platformId = res; + + } + +} diff --git a/core/src/main/java/matrixstudio/model/Scheduler.java b/core/src/main/java/matrixstudio/model/Scheduler.java index 1add094..8b5e68f 100644 --- a/core/src/main/java/matrixstudio/model/Scheduler.java +++ b/core/src/main/java/matrixstudio/model/Scheduler.java @@ -21,6 +21,8 @@ public class Scheduler implements ModelObject, BoostObject { private final List taskList = new ArrayList(); private Device device = Device.ANY; + + private PlatformCL platform = new PlatformCL(); private int deviceOrder = 1; @@ -208,6 +210,7 @@ public void setDevice(Device newValue) { this.device= newValue; } } + /** *

Gets deviceOrder.

@@ -226,6 +229,32 @@ public void setDeviceOrder(int newValue) { } } + + /** + * set platform + * @param platform + */ + + public void setPlatform(PlatformCL platform) { + this.platform = platform; + } + + /** + * set platform with index + * @param index + */ + public void setPlatform(int index) { + this.platform.setPlatformId(index); + } + + /** + * get platform attribut + * @return platform + */ + public PlatformCL getPlatform() { + return this.platform; + } + public void writeToBoost(Boost boost) { boost.writeObject(model); BoostUtil.writeObjectCollection(boost, taskList); diff --git a/core/src/test/java/matrixstudio/model/ModelTest.java b/core/src/test/java/matrixstudio/model/ModelTest.java index e715e92..327f2c8 100644 --- a/core/src/test/java/matrixstudio/model/ModelTest.java +++ b/core/src/test/java/matrixstudio/model/ModelTest.java @@ -1,7 +1,12 @@ package matrixstudio.model; import java.io.File; +import java.util.List; + +import matrixstudio.kernel.CLUtil; import matrixstudio.kernel.Tools; + +import org.jocl.cl_platform_id; import org.junit.Assert; import org.junit.Test; @@ -25,6 +30,22 @@ public void test1() throws Exception { Assert.assertEquals(1, loaded.getScheduler().getTaskCount()); } + + @Test + public void testTaillePlatform() { + List listPlatform = CLUtil.getListPlatform(); + + Assert.assertEquals(1, listPlatform.size()); + } + + @Test + public void testInstanceof() { + List listPlatform = CLUtil.getListPlatform(); + + for(int i = 0; i < listPlatform.size(); i++) { + Assert.assertTrue(listPlatform.get(i) instanceof cl_platform_id); + } + } private Model createModel() { Model model = new Model();