1 package net.bmahe.genetics4j.gpu.spec.fitness.cldata;
2
3 import java.util.Objects;
4
5 import org.apache.commons.lang3.Validate;
6 import org.jocl.CL;
7 import org.jocl.Sizeof;
8 import org.jocl.cl_image_desc;
9 import org.jocl.cl_image_format;
10 import org.jocl.cl_mem;
11
12 import net.bmahe.genetics4j.gpu.spec.fitness.MultipleComputer;
13
14 public class ResultAllocators {
15
16 private ResultAllocators() {
17 }
18
19 public static ResultAllocator ofSize(final int type, final int size) {
20 Validate.isTrue(type > 0);
21 Validate.isTrue(size > 0);
22
23 return (openCLExecutionContext, kernelExecutionContext, generation, genotypes) -> {
24
25 final var clContext = openCLExecutionContext.clContext();
26
27 final cl_mem clMem = CL.clCreateBuffer(clContext, CL.CL_MEM_WRITE_ONLY, type * size, null, null);
28
29 return CLData.of(clMem, type, size);
30 };
31 }
32
33 public static ResultAllocator ofSizeFloat(final int size) {
34 Validate.isTrue(size > 0);
35
36 return ofSize(Sizeof.cl_float, size);
37 }
38
39 public static ResultAllocator ofSizeInt(final int size) {
40 Validate.isTrue(size > 0);
41
42 return ofSize(Sizeof.cl_int, size);
43 }
44
45 public static ResultAllocator ofMultiplePopulationSizeFloat(final MultipleComputer multipleComputer) {
46 Objects.requireNonNull(multipleComputer);
47
48 return (openCLExecutionContext, kernelExecutionContext, generation, genotypes) -> {
49
50 final var clContext = openCLExecutionContext.clContext();
51
52 final int multiple = multipleComputer
53 .compute(openCLExecutionContext, kernelExecutionContext, generation, genotypes);
54 final int length = genotypes.size() * multiple;
55
56 final cl_mem clMem = CL.clCreateBuffer(clContext, CL.CL_MEM_WRITE_ONLY, Sizeof.cl_float * length, null, null);
57
58 return CLData.of(clMem, Sizeof.cl_float, length);
59 };
60 }
61
62 public static ResultAllocator ofMultiplePopulationSizeFloat(final int multiple) {
63 Validate.isTrue(multiple > 0);
64
65 return ofMultiplePopulationSizeFloat(
66 (openCLExecutionContext, kernelExecutionContext, generation, genotypes) -> multiple);
67 }
68
69 public static ResultAllocator ofPopulationSizeFloat() {
70 return ofMultiplePopulationSizeFloat(1);
71 }
72
73 public static ResultAllocator ofImage(final int width, final int height, final int channelOrder,
74 final int dataType) {
75 Validate.isTrue(width > 0);
76 Validate.isTrue(height > 0);
77 Validate.isTrue(channelOrder > 0);
78 Validate.isTrue(dataType > 0);
79
80 return (openCLExecutionContext, kernelExecutionContext, generation, genotypes) -> {
81 final var clContext = openCLExecutionContext.clContext();
82
83 final cl_image_desc imageDesc = new cl_image_desc();
84 imageDesc.image_type = CL.CL_MEM_OBJECT_IMAGE2D;
85 imageDesc.image_width = width;
86 imageDesc.image_height = height;
87
88 cl_image_format imageFormat = new cl_image_format();
89 imageFormat.image_channel_order = channelOrder;
90 imageFormat.image_channel_data_type = dataType;
91
92 final cl_mem clImageMem = CL
93 .clCreateImage(clContext, CL.CL_MEM_WRITE_ONLY, imageFormat, imageDesc, null, null);
94 return CLData.of(clImageMem, Sizeof.cl_mem, 1);
95 };
96 }
97
98 public static ResultAllocator ofImage(final int width, final int height) {
99 return ofImage(width, height, CL.CL_RGBA, CL.CL_FLOAT);
100 }
101 }