Commit 0540e9b2 authored by Benedikt Zoennchen's avatar Benedikt Zoennchen
Browse files

fix memory management errors in CLLinkedCell.java

parent 280cdc18
......@@ -128,7 +128,7 @@ public final class CLInfo {
public static void checkCLError(int errcode) throws OpenCLException {
if (errcode != CL_SUCCESS) {
throw new OpenCLException(String.format("OpenCL error [0x%X]", errcode));
throw new OpenCLException(String.format("OpenCL error [0x%X, %d]", errcode, errcode));
}
}
......
package org.vadere.util.opencl;
import org.apache.commons.lang3.tuple.Pair;
import org.vadere.util.logging.Logger;
import org.jetbrains.annotations.NotNull;
import org.lwjgl.PointerBuffer;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.system.MemoryUtil;
import org.vadere.util.opencl.examples.InfoUtils;
import java.io.IOException;
import java.io.InputStream;
......@@ -15,6 +19,14 @@ import java.nio.channels.SeekableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import static org.lwjgl.opencl.CL10.CL_CONTEXT_PLATFORM;
import static org.lwjgl.opencl.CL10.CL_DEVICE_TYPE_ALL;
import static org.lwjgl.opencl.CL10.clGetDeviceIDs;
import static org.lwjgl.opencl.CL10.clGetPlatformIDs;
/**
* Utility-class without a state. This class offers method to interact with OpenCL e.g. memory management methods.
......@@ -23,6 +35,65 @@ import java.nio.file.Paths;
*/
public class CLUtils {
private static Logger log = Logger.getLogger(CLUtils.class);
public static List<Long> getSupportedPlatforms(@NotNull final MemoryStack stack, final int deviceType) {
List<Long> supportedPlatforms = new ArrayList<>(2);
IntBuffer pi = stack.mallocInt(1);
InfoUtils.checkCLError(clGetPlatformIDs(null, pi));
if (pi.get(0) == 0) {
throw new RuntimeException("No OpenCL platforms found.");
}
PointerBuffer platforms = stack.mallocPointer(pi.get(0));
InfoUtils.checkCLError(clGetPlatformIDs(platforms, (IntBuffer)null));
IntBuffer errcode_ret = stack.callocInt(1);
for (int p = 0; p < platforms.capacity(); p++) {
long platform = platforms.get(p);
InfoUtils.checkCLError(clGetDeviceIDs(platform, deviceType, null, pi));
PointerBuffer devices = stack.mallocPointer(pi.get(0));
InfoUtils.checkCLError(clGetDeviceIDs(platform, deviceType, devices, (IntBuffer)null));
if(devices.capacity() > 0) {
supportedPlatforms.add(platform);
}
}
return supportedPlatforms;
}
public static Optional<Pair<Long, Long>> getFirstSupportedPlatformAndDevice(@NotNull final MemoryStack stack, final int deviceType) {
IntBuffer pi = stack.mallocInt(1);
InfoUtils.checkCLError(clGetPlatformIDs(null, pi));
if (pi.get(0) == 0) {
throw new RuntimeException("No OpenCL platforms found.");
}
PointerBuffer platforms = stack.mallocPointer(pi.get(0));
InfoUtils.checkCLError(clGetPlatformIDs(platforms, (IntBuffer)null));
for (int p = 0; p < platforms.capacity(); p++) {
long platform = platforms.get(p);
if(InfoUtils.checkCLSuccess(clGetDeviceIDs(platform, deviceType, null, pi))) {
PointerBuffer devices = stack.mallocPointer(pi.get(0));
if(InfoUtils.checkCLSuccess(clGetDeviceIDs(platform, deviceType, devices, (IntBuffer)null))) {
if(devices.capacity() > 0) {
return Optional.of(Pair.of(platform, devices.get(0)));
}
}
}
}
return Optional.empty();
}
/**
* Reads the specified resource and returns the raw data as a ByteBuffer.
*
......
......@@ -87,10 +87,12 @@ public final class CLDemo {
//System.out.println("\tCL_KERNEL_PREFERRED_WORK_GROUP_SIZE_MULTIPLE = " + InfoUtils.getDeviceInfoPointer(device, CL_KERNEL_PREFERRED_WORK_GROUP_SIZE_MULTIPLE));
System.out.println("\tCL_DEVICE_MAX_WORK_ITEM_DIMENSIONS = " + InfoUtils.getDeviceInfoInt(device, CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS));
System.out.println("\tCL_DEVICE_MAX_WORK_GROUP_SIZE = " + InfoUtils.getDeviceInfoPointer(device, CL_DEVICE_MAX_WORK_GROUP_SIZE));
System.out.println("\tCL_DEVICE_LOCAL_MEM_SIZE = " + InfoUtils.getDeviceInfoLong(device, CL_DEVICE_LOCAL_MEM_SIZE));
System.out.println("\tCL_DEVICE_MAX_CLOCK_FREQUENCY = " + InfoUtils.getDeviceInfoInt(device, CL_DEVICE_MAX_CLOCK_FREQUENCY));
System.out.println("\tCL_DEVICE_ADDRESS_BITS = " + InfoUtils.getDeviceInfoInt(device, CL_DEVICE_ADDRESS_BITS));
System.out.println("\tCL_DEVICE_AVAILABLE = " + (InfoUtils.getDeviceInfoInt(device, CL_DEVICE_AVAILABLE) != 0));
System.out.println("\tCL_DEVICE_COMPILER_AVAILABLE = " + (InfoUtils.getDeviceInfoInt(device, CL_DEVICE_COMPILER_AVAILABLE) != 0));
System.out.println("\tCL_DEVICE_VENDOR_ID = " + InfoUtils.getDeviceInfoInt(device, CL_DEVICE_VENDOR_ID));
printDeviceInfo(device, "CL_DEVICE_NAME", CL_DEVICE_NAME);
printDeviceInfo(device, "CL_DEVICE_VENDOR", CL_DEVICE_VENDOR);
......
......@@ -125,8 +125,12 @@ public final class InfoUtils {
public static void checkCLError(int errcode) {
if (errcode != CL_SUCCESS) {
throw new RuntimeException(String.format("OpenCL error [0x%X]", errcode));
throw new RuntimeException(String.format("OpenCL error [0x%X, %d]", errcode, errcode));
}
}
public static boolean checkCLSuccess(int errcode) {
return (errcode == CL_SUCCESS);
}
}
\ No newline at end of file
......@@ -16,6 +16,8 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import static org.lwjgl.opencl.CL10.CL_DEVICE_TYPE_GPU;
import static org.lwjgl.opencl.CL10.CL_DEVICE_TYPE_CPU;
import static org.junit.Assert.assertEquals;
/**
......@@ -23,7 +25,7 @@ import static org.junit.Assert.assertEquals;
*/
public class TestCLLinkedList {
private static Logger logger = Logger.getLogger(TestConvolution.class);
private static Logger logger = Logger.getLogger(TestCLLinkedList.class);
private static Random random = new Random();
......@@ -120,14 +122,41 @@ public class TestCLLinkedList {
@Test
@Ignore
public void testGridCellSmall() throws IOException, OpenCLException {
public void testGridCellSmallOnCPU() throws IOException, OpenCLException {
testGridCellSmall(CL_DEVICE_TYPE_CPU);
}
@Test
@Ignore
public void testGridCellSmallOnGPU() throws IOException, OpenCLException {
testGridCellSmall(CL_DEVICE_TYPE_GPU);
}
@Test
@Ignore
public void testGridCellLargeOnCPU() throws IOException, OpenCLException {
testGridCellLarge(CL_DEVICE_TYPE_CPU);
}
@Test
@Ignore
public void testGridCellLargeOnGPU() throws IOException, OpenCLException {
testGridCellLarge(CL_DEVICE_TYPE_GPU);
}
private void testGridCellSmall(final int deviceType) throws IOException, OpenCLException {
int size = 8;
CLLinkedCell clUniformHashedGrid = new CLLinkedCell(size, new VRectangle(0, 0, 10, 10), 0.6);
CLLinkedCell clUniformHashedGrid = new CLLinkedCell(size, new VRectangle(0, 0, 10, 10), 0.6, deviceType);
ArrayList<VPoint> positions = new ArrayList<>();
for(int i = 0; i < size; i++) {
positions.add(new VPoint(random.nextFloat() * 10,random.nextFloat() * 10));
}
long ms = System.currentTimeMillis();
CLLinkedCell.LinkedCell gridCells = clUniformHashedGrid.calcLinkedCell(positions);
long runtime = System.currentTimeMillis() - ms;
logger.infof("testGridCellSmall required " + runtime + " [ms]");
int numberOfCells = clUniformHashedGrid.getGridSize()[0] * clUniformHashedGrid.getGridSize()[1];
int sum = 0;
for(int cell = 0; cell < numberOfCells; cell++) {
......@@ -146,16 +175,20 @@ public class TestCLLinkedList {
assertEquals(size, sum);
}
@Test
@Ignore
public void testGridCellLarge() throws IOException, OpenCLException {
private void testGridCellLarge(final int device) throws IOException, OpenCLException {
int size = 32768;
CLLinkedCell clUniformHashedGrid = new CLLinkedCell(size, new VRectangle(0, 0, 10, 10), 0.6);
//int size = 8192;
CLLinkedCell clUniformHashedGrid = new CLLinkedCell(size, new VRectangle(0, 0, 10, 10), 0.6, device);
ArrayList<VPoint> positions = new ArrayList<>();
for(int i = 0; i < size; i++) {
positions.add(new VPoint(random.nextFloat() * 10,random.nextFloat() * 10));
}
long ms = System.currentTimeMillis();
CLLinkedCell.LinkedCell gridCells = clUniformHashedGrid.calcLinkedCell(positions);
long runtime = System.currentTimeMillis() - ms;
logger.infof("testGridCellLarge required " + runtime + " [ms]");
equalPositions(gridCells);
int numberOfCells = clUniformHashedGrid.getGridSize()[0] * clUniformHashedGrid.getGridSize()[1];
int sum = 0;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment