View Javadoc
1   package net.bmahe.genetics4j.gpu.spec.fitness.cldata;
2   
3   import org.apache.commons.lang3.Validate;
4   import org.jocl.Sizeof;
5   
6   public class LocalMemoryAllocators {
7   
8   	private LocalMemoryAllocators() {
9   	}
10  
11  	public static LocalMemoryAllocator ofSize(final int type, final long size) {
12  		Validate.isTrue(type > 0);
13  		Validate.isTrue(size > 0);
14  
15  		return (openCLExecutionContext, kernelExecutionContext, generation, genotypes) -> type * size;
16  	}
17  
18  	public static LocalMemoryAllocator ofWorkGroupSize(final int type, final int multiple) {
19  		Validate.isTrue(multiple > 0);
20  
21  		return (openCLExecutionContext, kernelExecutionContext, generation, genotypes) -> {
22  
23  			final long[] workGroupSize = kernelExecutionContext.workGroupSize().get();
24  			long totalWorkGroupSize = 1;
25  			for (int i = 0; i < workGroupSize.length; i++) {
26  				totalWorkGroupSize *= workGroupSize[i];
27  			}
28  			return type * totalWorkGroupSize * multiple;
29  		};
30  	}
31  
32  	public static LocalMemoryAllocator ofWorkGroupSize(final int type) {
33  		return ofWorkGroupSize(type, 1);
34  	}
35  
36  	public static LocalMemoryAllocator ofMaxWorkGroupSize(final int type, final int multiple) {
37  		Validate.isTrue(multiple > 0);
38  
39  		return (openCLExecutionContext, kernelExecutionContext, generation, genotypes) -> {
40  
41  			final var device = openCLExecutionContext.device();
42  			return type * device.maxWorkGroupSize() * multiple;
43  		};
44  	}
45  
46  	public static LocalMemoryAllocator ofMaxWorkGroupSize(final int type) {
47  		return ofMaxWorkGroupSize(type, 1);
48  	}
49  
50  	public static LocalMemoryAllocator ofMaxWorkGroupSizeFloat(final int multiple) {
51  		return ofMaxWorkGroupSize(Sizeof.cl_float, multiple);
52  	}
53  
54  	public static LocalMemoryAllocator ofMaxWorkGroupSizeFloat() {
55  		return ofMaxWorkGroupSize(Sizeof.cl_float, 1);
56  	}
57  
58  	public static LocalMemoryAllocator ofSizeFloat(final long size) {
59  		Validate.isTrue(size > 0);
60  
61  		return ofSize(Sizeof.cl_float, size);
62  	}
63  
64  	public static LocalMemoryAllocator ofSizeInt(final long size) {
65  		Validate.isTrue(size > 0);
66  
67  		return ofSize(Sizeof.cl_int, size);
68  	}
69  }