• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

bernardladenthin / BitcoinAddressFinder / #308

28 Apr 2025 10:01PM UTC coverage: 68.174% (-0.7%) from 68.832%
#308

push

bernardladenthin
Refactor OpenCL handling for device endianness and dynamic platform selection

22 of 76 new or added lines in 10 files covered. (28.95%)

1 existing line in 1 file now uncovered.

1221 of 1791 relevant lines covered (68.17%)

0.68 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/main/java/net/ladenthin/bitcoinaddressfinder/OpenCLContext.java
1
// @formatter:off
2
/**
3
 * Copyright 2020 Bernard Ladenthin bernard.ladenthin@gmail.com
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *    http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 */
18
// @formatter:on
19
package net.ladenthin.bitcoinaddressfinder;
20

21
import com.google.common.io.Resources;
22
import java.io.IOException;
23
import java.math.BigInteger;
24
import java.net.URL;
25
import java.nio.ByteBuffer;
26
import java.nio.ByteOrder;
27
import java.nio.charset.StandardCharsets;
28
import java.util.ArrayList;
29
import java.util.List;
30
import net.ladenthin.bitcoinaddressfinder.configuration.CProducerOpenCL;
31
import net.ladenthin.bitcoinaddressfinder.opencl.OpenCLBuilder;
32
import net.ladenthin.bitcoinaddressfinder.opencl.OpenCLDevice;
33
import net.ladenthin.bitcoinaddressfinder.opencl.OpenCLDeviceSelection;
34
import net.ladenthin.bitcoinaddressfinder.opencl.OpenCLPlatform;
35
import net.ladenthin.bitcoinaddressfinder.opencl.OpenCLPlatformSelector;
36
import static org.jocl.CL.clBuildProgram;
37
import static org.jocl.CL.clCreateCommandQueueWithProperties;
38
import static org.jocl.CL.clCreateContext;
39
import static org.jocl.CL.clCreateKernel;
40
import static org.jocl.CL.clCreateProgramWithSource;
41
import static org.jocl.CL.clReleaseCommandQueue;
42
import static org.jocl.CL.clReleaseContext;
43
import org.jocl.cl_command_queue;
44
import org.jocl.cl_context;
45
import org.jocl.cl_context_properties;
46
import org.jocl.cl_device_id;
47
import org.jocl.cl_kernel;
48
import org.jocl.cl_program;
49
import org.jocl.cl_queue_properties;
50
import org.jocl.CL;
51
import static org.jocl.CL.clReleaseKernel;
52
import static org.jocl.CL.clReleaseProgram;
53
import org.slf4j.Logger;
54
import org.slf4j.LoggerFactory;
55

56
public class OpenCLContext {
57

58
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
×
59
    
60
    public String[] getOpenCLPrograms() throws IOException {
61
        List<String> resourceNamesContent = getResourceNamesContent(getResourceNames());
×
62
        List<String> resourceNamesContentWithReplacements = new ArrayList<>();
×
63
        for (String content : resourceNamesContent) {
×
64
            String contentWithReplacements = content;
×
65
            contentWithReplacements = contentWithReplacements.replaceAll("#include.*", "");
×
66
            resourceNamesContentWithReplacements.add(contentWithReplacements);
×
67
        }
×
68
        String[] openClPrograms = resourceNamesContentWithReplacements.toArray(new String[0]);
×
69
        return openClPrograms;
×
70
    }
71
    
72
    private List<String> getResourceNames() {
73
        List<String> resourceNames = new ArrayList<>();
×
74
        resourceNames.add("inc_defines.h");
×
75
        resourceNames.add("copyfromhashcat/inc_vendor.h");
×
76
        resourceNames.add("copyfromhashcat/inc_types.h");
×
77
        resourceNames.add("copyfromhashcat/inc_platform.h");
×
78
        resourceNames.add("copyfromhashcat/inc_platform.cl");
×
79
        resourceNames.add("copyfromhashcat/inc_common.h");
×
80
        resourceNames.add("copyfromhashcat/inc_common.cl");
×
81

82
        resourceNames.add("copyfromhashcat/inc_ecc_secp256k1.h");
×
83
        resourceNames.add("copyfromhashcat/inc_ecc_secp256k1.cl");
×
84
        resourceNames.add("inc_ecc_secp256k1custom.cl");
×
85
        return resourceNames;
×
86
    }
87
    
88
    private final static String KERNEL_NAME = "generateKeysKernel_grid";
89
    private final static boolean EXCEPTIONS_ENABLED = true;
90
    
91
    private final CProducerOpenCL producerOpenCL;
92
    private final BitHelper bitHelper;
93

94
    private OpenCLDevice device;
95
    private cl_context context;
96
    private cl_command_queue commandQueue;
97
    private cl_program program;
98
    private cl_kernel kernel;
99
    private OpenClTask openClTask;
100
    private ByteBufferUtility byteBufferUtility;
101
    
102
    public OpenCLContext(CProducerOpenCL producerOpenCL, BitHelper bitHelper) {
×
103
        this.producerOpenCL = producerOpenCL;
×
104
        this.bitHelper = bitHelper;
×
105
        this.byteBufferUtility = new ByteBufferUtility(true);
×
106
    }
×
107
    
108
    public void init() throws IOException {
109
        
110
        // #################### general ####################
111
        
112
        // Enable exceptions and subsequently omit error checks in this sample
113
        CL.setExceptionsEnabled(EXCEPTIONS_ENABLED);
×
114
        
NEW
115
        List<OpenCLPlatform> platforms = new OpenCLBuilder().build();
×
116

NEW
117
        OpenCLDeviceSelection selection = OpenCLPlatformSelector.select(
×
118
            platforms,
119
            producerOpenCL.platformIndex,
120
            producerOpenCL.deviceType,
121
            producerOpenCL.deviceIndex
122
        );
123
        
NEW
124
        device = selection.getDevice();
×
NEW
125
        cl_context_properties contextProperties = selection.getContextProperties();
×
NEW
126
        cl_device_id[] cl_device_ids = new cl_device_id[]{device.device()};
×
127
        
128
        // Create a context for the selected device
129
        context = clCreateContext(contextProperties, 1, cl_device_ids, null, null, null);
×
130
        
131
        // Create a command-queue for the selected device
132
        cl_queue_properties properties = new cl_queue_properties();
×
NEW
133
        commandQueue = clCreateCommandQueueWithProperties(context, device.device(), properties, null);
×
134
        
135
        // #################### kernel specifix ####################
136
        
137
        String[] openCLPrograms = getOpenCLPrograms();
×
138
        // Create the program from the source code
139
        program = clCreateProgramWithSource(context, openCLPrograms.length, openCLPrograms, null, null);
×
140

141
        // Build the program
142
        clBuildProgram(program, 0, null, null, null, null);
×
143
        
144
        // Create the kernel
145
        kernel = clCreateKernel(program, KERNEL_NAME, null);
×
146
        
NEW
147
        openClTask = new OpenClTask(context, device.getByteOrder(), producerOpenCL, bitHelper, byteBufferUtility);
×
148
    }
×
149

150
    OpenClTask getOpenClTask() {
151
        return openClTask;
×
152
    }
153

154
    public void release() {
155
        openClTask.releaseCl();
×
156
        clReleaseKernel(kernel);
×
157
        clReleaseProgram(program);
×
158
        clReleaseCommandQueue(commandQueue);
×
159
        clReleaseContext(context);
×
160
    }
×
161

162
    public OpenCLGridResult createKeys(BigInteger privateKeyBase) {
163
        openClTask.setSrcPrivateKeyChunk(privateKeyBase);
×
164
        ByteBuffer dstByteBuffer = openClTask.executeKernel(kernel, commandQueue);
×
165

NEW
166
        OpenCLGridResult openCLGridResult = new OpenCLGridResult(privateKeyBase, device.getByteOrder(), bitHelper.convertBitsToSize(producerOpenCL.batchSizeInBits), dstByteBuffer);
×
167
        return openCLGridResult;
×
168
    }
169

170
    private static List<String> getResourceNamesContent(List<String> resourceNames) throws IOException {
171
        List<String> contents = new ArrayList<>();
×
172
        for (String resourceName : resourceNames) {
×
173
            URL url = Resources.getResource(resourceName);
×
174
            String content = Resources.toString(url, StandardCharsets.UTF_8);
×
175
            contents.add(content);
×
176
        }
×
177
        return contents;
×
178
    }
179
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc