r/OpenCL • u/jacksmithsm • Jan 18 '14
OpenCL vs OpenGL?
Can someone tell the what the differences are between openCL and openGL?
r/OpenCL • u/jacksmithsm • Jan 18 '14
Can someone tell the what the differences are between openCL and openGL?
r/OpenCL • u/psyked222 • Dec 17 '16
Hi,
I'm doing a quicksort (recursiv) with openCL on one thread and I got some issues. The openCL compilator will return an error if i try to compile my code for my intel cpu ("recursion detected" with openCL 2.0) but it compiles and works on my nvidia 950M(openCL 1.2) only for few recursion calls.
After some investigations, i've found that the "OUT_OF_MEMORY" error appends when my stack was bigger than 32Ko, so I've two questions for you expers ;)
First one : Why the hell can I use recursivity on openCL 1.2 devices and not on openCL 2.0 devices ? (when openCL 1.2 isn't supposed to support recursion).
Second one : The private memory can't be bigger than 32Ko (like the max size of my stack). So, is my stack stored in my private memory ? Or just in anothe location with the same space ?
r/OpenCL • u/[deleted] • Aug 07 '15
r/OpenCL • u/Eilai • Dec 04 '15
So I have something like this at the moment to test things, this isn't the intended code to run on the kernel, trying to see if it "works" before moving on to using work groups properly, and basically the kernel seems to run forever sometimes and also only sometimes my HD graphics driver for my Intel CPU (my GPU is an Nvidia 960) crashes.
#pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable
#pragma OPENCL EXTENSION cl_khr_local_int32_base_atomics : enable
__kernel void broadphase_kernel(__global widget *_widgets, __global manifold_cl *_contacts, int _WidgetSize, __global int* _contactsSize)
{
int gid = get_global_id(0);
//__kernel void generatePairsKernel__global
widget temp_widget_a_cl = _widgets[gid];
for (int j = 0; j < _WidgetSize; j++)
{
manifold_cl _m;
_m.isColliding_cl = false;
bVec3 temp;
temp.x = 1;
temp.y = 1;
temp.z = 1;
_m.normal_cl = temp;
_m.penetration_cl = 0;
_m.A_cl = gid;
_m.B_cl = gid;
_contacts[gid] = _m;
}
}r/OpenCL • u/ric96 • Nov 29 '16
r/OpenCL • u/sumit2222 • Nov 20 '16
hi all, i had install AMDAPPSDK-3.0 for my laptop with intel i5 3rd generation configuration. i have no GPU other than my intel's processors inbuilt graphics card. i had installed the SDK in the below way: ./AMD-APP-SDK-v3.0.130.136-GA-linux64.sh my .bashrc file has: export LD_LIBRARY_PATH=/home/roadeo/AMDAPPSDK-3.0/lib/x86_64/ export AMDAPPSDKROOT="/home/roadeo/AMDAPPSDK-3.0" export OPENCL_VENDOR_PATH="/home/roadeo/AMDAPPSDK-3.0/etc/OpenCL/vendors/"
When i run clinfo to check whether OPENCL is installed properly or not. But i get this error: terminate called after throwing an instance of 'cl::Error' what(): clGetPlatformIDs Aborted core dumped.
after googling i with frustration install fglrx using sudo apt-get. When i run clinfo i get a lot of details about opencl versions, vendor etc.. I don't know whether is it required or not.
What i m doing wrong kindly suggest.
r/OpenCL • u/[deleted] • Dec 10 '22
Why aren't all programs written in OpenCL?
r/OpenCL • u/streamcomputing • Jul 25 '13
r/OpenCL • u/Yungwavfile • Nov 21 '20
I recently installed davinci resolve but it said that I needed to install a OpenCL accelerated GPU card or update my OpenCL software driver. Is it possible for me to get OpenCL or do something so that I can run davinci without buying a new gpu. I dont really know anything about opencl btw
r/OpenCL • u/[deleted] • Jul 07 '18
Hi,
I've been playing around with OpenCL lately.
I've written a nice C++, OOP wrapper for the OpenCL C API (based on https://anteru.net/blog/2012/11/04/2016/index.html)
I've written some basic kernels for filling a matrix with constants, creating an identity matrix, adding 2 matrices and multiplying 2 matrices (naively).
I thought I'd see if the code I wrote was actually any faster than regular-old CPU-based C++ code and came to a surprising conclusion.
My results can be found here: https://pastebin.com/Y7ABDnRP
As you can see my CPU is anywhere from 342x to 15262x faster than my GPU.
The kernels being used are VERY simple (https://pastebin.com/0qQJtKV3).
All timing was measured using C++'s std::chrono::system_clock, around the complete operation (because, in the end, that's the time that matters).
I can't seem to think of a reason why OpenCL should be THIS MUCH slower.
Sure, My CPU has some SIMD instructions and faster access to RAM, but these results are a bit extreme to be attributed to that, aren't they?
Here's the C++ code that I used to do my tests: https://pastebin.com/kJPv9wib
Could someone give me a hint as to why my GPU code is so much slower?
P.S.: (In the results you can see, I actually forgot to create an m4 for the CPU, so m3 was first storing the result of an addition, and then the result of a multiplication. After I fixed this, I got SEGFAULT's for any size of the sizes > 500. For a size of 500 the CPU took anywhere from 704-1457µs to complete its operations, which is still orders of magnitude faster than OpenCL.)
P.P.S.: I didn't post the complete code because it's a lot code spread out across a lot of files. I don't want a complete and full analysis of every line of code, I just want some pointers/general principles that I missed that can explain this huge difference.
P.P.P.S.: All data transfers were done using mapped buffers.
Edit: I just checked, the AMD Radeon M265 has 6 (maximum) compute units running at 825MHz (maximum, both queried using clGetDeviceInfo())
r/OpenCL • u/rgbench • Oct 14 '12
r/OpenCL • u/-Xcrp • Aug 26 '24
im working on bitcoin puzzle mining project but my code throwing runtime error: OUT_OF_RESOURCES
everything looks fine:
pyopencl:
import
pyopencl
as
cl
from
hashlib
import sha256
import
base58
import
os
import
numpy
as
np
import
warnings
warnings
.filterwarnings("ignore",
category
=
UserWarning
,
module
='pyopencl')
def
get_kernel(
cl
):
return open(
cl
,"r").read()
def
privToWIF(
private_key
):
# Step 1: Add version byte (0x80 for mainnet)
versioned_key = '80' +
private_key
# Step 3: Double SHA-256 hash
first_sha = sha256(
bytes
.fromhex(versioned_key)).hexdigest()
second_sha = sha256(
bytes
.fromhex(first_sha)).hexdigest()
# Step 4: Add first 4 bytes of the second SHA-256 hash as checksum
checksum = second_sha[:8]
final_key = versioned_key + checksum
# Step 5: Encode in Base58Check
compressed_private_key =
base58
.b58encode(
bytes
.fromhex(final_key)).decode('utf-8')
return compressed_private_key
def
compress_private_key(
private_key_hex
):
# Step 1: Add version byte (0x80 for mainnet)
versioned_key = '80' +
private_key_hex
# Step 2: Add compression byte (0x01)
compressed_key = versioned_key + '01'
# Step 3: Double SHA-256 hash
try:
sha = sha256(
bytes
.fromhex(sha256(
bytes
.fromhex(compressed_key)).hexdigest())).hexdigest()
except
Exception
as e:
print(
private_key_hex
)
with open(
os
.path.join(
os
.path.expanduser("~"), "Desktop\\privkey.txt"),"a") as f:
f.write("\n" +
f
"Bozuk hex: {
private_key_hex
} \n Compressli Bozuk Hex: {compress_private_key(
private_key_hex
)}\n")
return compress_private_key(
private_key_hex
)
# Step 4: Add first 4 bytes of the second SHA-256 hash as checksum
checksum = sha[:8]
final_key = compressed_key + checksum
# Step 5: Encode in Base58Check
compressed_private_key =
base58
.b58encode(
bytes
.fromhex(final_key)).decode('utf-8')
return compressed_private_key
def
main():
global result_string
for platform in
cl
.get_platforms():
devices = platform.get_devices(
cl
.device_type.GPU)
for device in devices:
context =
cl
.Context([device])
queue =
cl
.CommandQueue(context)
sizeof_output = 4096 # 256 bit hex int length
result =
np
.zeros(sizeof_output,
dtype
=
np
.uint8)
result_buffer =
cl
.Buffer(context,
cl
.mem_flags.WRITE_ONLY, result.nbytes)
program =
cl
.
Program
(context, get_kernel("kernel.cl")).build()
kernel = program.main_src
kernel.set_args(result_buffer)
global_work_size = (sizeof_output,)
cl
.enqueue_nd_range_kernel(queue, kernel, global_work_size, None)
cl
.enqueue_copy(queue, result, result_buffer)
queue.finish()
result_string = ''.join(chr(x) for x in result)
print(result_string)
wif = privToWIF(result_string)
print("\n" +
f
"Private Key: {result_string}" + "\n" +
f
"WIF Private Key: {wif}"+"\n" +
f
"WIF COMPRESSED Private Key:{compress_private_key(result_string)}" + "\n")
if True:
with open(
os
.path.join(
os
.path.expanduser("~"), "Desktop\\privkey.txt"),"a") as f:
f.write("\n" +
f
"Private Key: {result_string}" + "\n" +
f
"WIF Private Key: {wif}"+"\n" +
f
"WIF COMPRESSED Private Key:{compress_private_key(result_string)}" + "\n")
if __name__ == "__main__":
os
.system("cls")
main()bigint library:
#include "bigint.clh"
int len(__global char *str)
{
int i = 0;
while (str[i] != '\0')
{
i++;
}
return i;
}
void int_to_bytes(__global uint *num, __global uchar *byte_array)
{
uint value = num[0];
for (int i = 0; i < 4; i++)
{
byte_array[3 - i] = (uchar)(value & 0xFF);
value >>= 8;
}
}
void bytes_to_int(__global uchar *byte_array, __global uint *result)
{
uint value = 0;
for (int i = 0; i < 4; i++)
{
value = (value << 8) | byte_array[i];
}
result[0] = value;
}
void concate_str(__global char *a, char *b, __global char *result)
{
int gid = get_global_id(0);
// Calculate the lengths of the input strings
int lena = 0;
while (a[lena] != '\0')
{
lena++;
}
int lenb = 0;
while (b[lenb] != '\0')
{
lenb++;
}
// Concatenate the strings
for (int i = 0; i < lena; i++)
{
result[i] = a[i];
}
for (int i = 0; i < lenb; i++)
{
result[lena + i] = b[i];
}
// Null-terminate the result string
result[lena + lenb] = '\0';
}
void concate_str_ng(__constant char *a, char *b, __global char *result)
{
int gid = get_global_id(0);
// Calculate the lengths of the input strings
int lena = 0;
while (a[lena] != '\0')
{
lena++;
}
int lenb = 0;
while (b[lenb] != '\0')
{
lenb++;
}
// Concatenate the strings
for (int i = 0; i < lena; i++)
{
result[i] = a[i];
}
for (int i = 0; i < lenb; i++)
{
result[lena + i] = b[i];
}
// Null-terminate the result string
result[lena + lenb] = '\0';
}
__constant uint k[64] = {
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2};
__constant uint initial_hash[8] = {0x6a09e667, 0xbb67ae85, 0x3c6ef372,
0xa54ff53a, 0x510e527f, 0x9b05688c,
0x1f83d9ab, 0x5be0cd19};
uint rotate_sha(uint x, uint n) { return (x >> n) | (x << (32 - n)); }
void sha256(__global uchar *in, __global uchar *out, uint length)
{
uint h[8];
for (int i = 0; i < 8; i++)
{
h[i] = initial_hash[i];
}
// Process each 512-bit chunk
for (int chunk = 0; chunk < length; chunk += 64)
{
uint w[64];
for (int i = 0; i < 16; i++)
{
w[i] = (in[chunk + 4 * i] << 24) | (in[chunk + 4 * i + 1] << 16) |
(in[chunk + 4 * i + 2] << 8) | in[chunk + 4 * i + 3];
}
for (int i = 16; i < 64; i++)
{
uint s0 = (rotate_sha(w[i - 15], 7) ^ rotate_sha(w[i - 15], 18) ^
(w[i - 15] >> 3));
uint s1 = (rotate_sha(w[i - 2], 17) ^ rotate_sha(w[i - 2], 19) ^
(w[i - 2] >> 10));
w[i] = w[i - 16] + s0 + w[i - 7] + s1;
}
uint a = h[0];
uint b = h[1];
uint c = h[2];
uint d = h[3];
uint e = h[4];
uint f = h[5];
uint g = h[6];
uint hh = h[7];
for (int i = 0; i < 64; i++)
{
uint S1 = (rotate_sha(e, 6) ^ rotate_sha(e, 11) ^ rotate_sha(e, 25));
uint ch = ((e & f) ^ (~e & g));
uint temp1 = hh + S1 + ch + k[i] + w[i];
uint S0 = (rotate_sha(a, 2) ^ rotate_sha(a, 13) ^ rotate_sha(a, 22));
uint maj = ((a & b) ^ (a & c) ^ (b & c));
uint temp2 = S0 + maj;
hh = g;
g = f;
f = e;
e = d + temp1;
d = c;
c = b;
b = a;
a = temp1 + temp2;
}
h[0] += a;
h[1] += b;
h[2] += c;
h[3] += d;
h[4] += e;
h[5] += f;
h[6] += g;
h[7] += hh;
}
for (int i = 0; i < 8; i++)
{
out[4 * i] = (h[i] >> 24) & 0xff;
out[4 * i + 1] = (h[i] >> 16) & 0xff;
out[4 * i + 2] = (h[i] >> 8) & 0xff;
out[4 * i + 3] = h[i] & 0xff;
}
}
__constant char base58_alphabet[] =
"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
void base58_encode(__global uchar *input, uint input_len,
__global char *output)
{
uint i, j;
uint carry;
const uint max_output_len = 256; // Adjust this size based on your needs
uchar temp[max_output_len];
// Initialize the temp array
for (i = 0; i < max_output_len; i++)
{
temp[i] = 0;
}
for (i = 0; i < input_len; i++)
{
carry = input[i];
for (j = max_output_len; j > 0; j--)
{
carry += (uint)(temp[j - 1]) << 8;
temp[j - 1] = carry % 58;
carry /= 58;
}
}
// Skip leading zeros in temp
i = 0;
while (i < max_output_len && temp[i] == 0)
{
i++;
}
// Translate the digits to the base58 alphabet
j = 0;
while (i < max_output_len)
{
output[j++] = base58_alphabet[temp[i++]];
}
for (i = 0; i < input_len && input[i] == 0; i++)
{
output[j++] = '1';
}
// Null-terminate the output string
output[j] = '\0';
}
void create_versioned_key(__global const char *privkey,
__global char *versioned)
{
// Define the prefix and suffix
const char prefix[] = "80";
const char suffix[] = "01";
// Calculate the lengths
uint prefix_len = 2; // Length of "80"
uint privkey_len = 0;
uint suffix_len = 2; // Length of "01"
// Calculate the length of the private key
while (privkey[privkey_len] != '\0')
{
privkey_len++;
}
// Construct the versioned key
uint i = 0;
// Add the prefix
for (i = 0; i < prefix_len; i++)
{
versioned[i] = prefix[i];
}
for (uint j = 0; j < privkey_len; j++, i++)
{
versioned[i] = privkey[j];
}
for (uint j = 0; j < suffix_len; j++, i++)
{
versioned[i] = suffix[j];
}
// Null-terminate the versioned key
versioned[i] = '\0';
}
void compress_private_key(__global char *privkey, __global char *out)
{
int id =
get_global_id(1) * get_global_size(0) + get_global_id(0); // 1D global id
// __global const char *versioned = "80" + privkey + "01";
__global char *versioned;
create_versioned_key(privkey, versioned);
__global uchar *versioned_b;
stringToByteArray(versioned, versioned_b, len(versioned));
__global uchar *firsh_sha;
sha256(versioned_b, firsh_sha, len(versioned));
__global uchar *second_sha;
sha256(firsh_sha, second_sha, sizeof(firsh_sha) / sizeof(firsh_sha[0]));
char checksum[8];
checksum[id % 8] = second_sha[id % 8];
__global char *final_key;
concate_str(versioned, checksum, final_key);
__global uchar *final_key_b;
stringToByteArray(final_key, final_key_b, len(final_key));
__global char *compressed_key;
base58_encode(final_key_b, len(final_key), compressed_key);
*out = *compressed_key;
}
void random_generator(__global const
BigInt
*start, __global const
BigInt
*end,
__global
BigInt
*output)
{
int gid = get_global_id(0);
// Seed based on global ID and current time
uint seed = gid + (uint)get_global_id(0) + (uint)get_global_size(0);
// Linear Congruential Generator (LCG) parameters
uint a = 1664525;
uint c = 1013904223;
uint m = 0xFFFFFFFF;
// Initialize state with seed
uint state = seed;
// Generate random number
state = (a * state + c) % m;
// Map the random number to the range [start, end]
BigInt range;
init_bigint_ng(&range);
// Calculate range = end - start
for (int i = 0; i < MAX_DIGITS; i++)
{
range.digits[i] = end[gid].digits[i] - start[gid].digits[i];
}
range.length = end[gid].length;
// Calculate output = start + (state % range)
for (int i = 0; i < MAX_DIGITS; i++)
{
output[gid].digits[i] = start[gid].digits[i] + (state % range.digits[i]);
}
output[gid].length = start[gid].length;
}
__constant uint K[5] = {0x00000000, 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xA953FD4E};
__constant uint KK[5] = {0x50A28BE6, 0x5C4DD124, 0x6D703EF3, 0x7A6D76E9, 0x00000000};
void ripemd160_hash(__global const uchar *input, __global uint *output, uint length)
{
uint h0 = 0x67452301;
uint h1 = 0xEFCDAB89;
uint h2 = 0x98BADCFE;
uint h3 = 0x10325476;
uint h4 = 0xC3D2E1F0;
uint A, B, C, D, E, AA, BB, CC, DD, EE, T;
uint X[16];
for (uint i = 0; i < length; i += 64)
{
for (uint j = 0; j < 16; j++)
{
X[j] = ((uint)input[i + 4 * j]) | (((uint)input[i + 4 * j + 1]) << 8) |
(((uint)input[i + 4 * j + 2]) << 16) |
(((uint)input[i + 4 * j + 3]) << 24);
}
A = AA = h0;
B = BB = h1;
C = CC = h2;
D = DD = h3;
E = EE = h4;
// Main loop
for (uint j = 0; j < 80; j++)
{
uint F = (B ^ C ^ D);
uint Kt = K[j / 16];
uint s = (j % 16);
T = A + F + X[s] + Kt;
A = E;
E = D;
D = (C << 10) | (C >> (32 - 10));
C = B;
B = (T << s) | (T >> (32 - s));
uint tempAA = AA;
uint tempBB = BB;
uint tempCC = CC;
uint tempDD = DD;
uint tempEE = EE;
F = (tempBB ^ (tempCC | ~tempDD));
Kt = KK[j / 16];
s = (j % 16);
T = tempAA + F + X[s] + Kt;
AA = tempEE;
EE = tempDD;
DD = (tempCC << 10) | (tempCC >> (32 - 10));
CC = tempBB;
BB = (T << s) | (T >> (32 - s));
}
T = h1 + C + DD;
h1 = h2 + D + EE;
h2 = h3 + E + AA;
h3 = h4 + A + BB;
h4 = h0 + B + CC;
h0 = T;
}
output[0] = h0;
output[1] = h1;
output[2] = h2;
output[3] = h3;
output[4] = h4;
}
const char *privkeyToAddr(__global char *privkey_c)
{
int id =
get_global_id(1) * get_global_size(0) + get_global_id(0); // 1D global id
__global BigInt *a;
string_to_bigint(privkey_c, a);
__global uchar *byte_array_a;
bigint_to_byte_array(a, byte_array_a);
int len_byte_array_a = sizeof(byte_array_a) / sizeof(byte_array_a[0]);
__global uchar *_x;
__global uchar *_y;
if (id < (len_byte_array_a / 2))
{
_x[id] = byte_array_a[id];
}
else if (id < len_byte_array_a)
{
_y[id] = byte_array_a[id];
}
__global BigInt *x;
__global BigInt *y;
byteArrayToBigInt(_x, len_byte_array_a / 2, x);
byteArrayToBigInt(_y, len_byte_array_a / 2, y);
__global BigInt *x_cube;
__global BigInt *seven;
__global BigInt *y_square;
__global BigInt *x_cube_p_seven;
__global BigInt *x_cube_p_seven_m_y_square;
__global BigInt *p;
__global BigInt *public_k;
power_bigint(x, 3, x_cube);
int_to_bigint(7, seven);
add_bigint(x_cube, seven, x_cube_p_seven);
power_bigint(y, 2, y_square);
subtract_bigint(x_cube_p_seven, y_square, x_cube_p_seven_m_y_square);
init_bigint_from_string("115792089237316195423570985008687907853269984665640564039457584007908834671663",
p);
bigint_mod(x_cube_p_seven_m_y_square, p, public_k);
__global uchar *public_k_b;
__global uchar *public_k_h;
__global uint *public_k_rh;
__global uchar *public_k_rh_b;
__global char *public_k_rs;
bigint_to_byte_array(public_k, public_k_b);
sha256(public_k_b, public_k_h, sizeof(public_k_b) / sizeof(public_k_b[0]));
ripemd160_hash(public_k_h, public_k_rh,
sizeof(public_k_h) / sizeof(public_k_h[0]));
int_to_bytes(public_k_rh, public_k_rh_b);
byteArrayToString(public_k_rh_b, public_k_rs,
sizeof(public_k_rh_b) / sizeof(public_k_rh_b[0]));
__global char *network_byte;
concate_str_ng("00", public_k_rs, network_byte);
__global uchar *network_byte_b;
stringToByteArray(network_byte, network_byte_b, len(network_byte));
__global uchar *sha1;
__global uchar *sha2;
sha256(network_byte_b, sha1,
sizeof(network_byte_b) / sizeof(network_byte_b[0]));
sha256(sha1, sha2, sizeof(sha1) / sizeof(sha1[0]));
char checksum[9];
__global char *sha2_hex;
byteArrayToHexString(sha2, sha2_hex, sizeof(sha2) / sizeof(sha2[0]));
if (id < 8)
checksum[id] = sha2_hex[id];
__global char *net_checksum;
concate_str(network_byte, checksum, net_checksum);
__global BigInt *net_checksum_B;
string_to_bigint(net_checksum, net_checksum_B);
__global uchar *net_checksum_b;
bigint_to_byte_array(net_checksum_B, net_checksum_b);
__global char *addr;
base58_encode(net_checksum_b,
sizeof(net_checksum_b) / sizeof(net_checksum_b[0]), addr);
return addr;
}
__global char *randomGenPrivKey(__global BigInt *start, __global BigInt *end,
__global char *wallet_addr)
{
__global BigInt *a;
__global char *b;
while (1)
{
random_generator(start, end, a);
bigint_to_hex_string(a, b);
while (len(b) < 64)
{
if (len(b) < 64)
{
*b = '0' + *b;
}
}
__global char *compressed_privkey;
compress_private_key(b, compressed_privkey);
if (wallet_addr == privkeyToAddr(compressed_privkey))
{
// return (*b);
return b; // return to host
}
}
}
// support biginteger chatgpt: would you write a big integer struct in opencl C
// after ask what you want
__kernel void main_src(__global uchar *result) // strlen : 64
{
__global BigInt *start;
__global BigInt *end;
__global char *Addr;
__global BigInt *base;
init_bigint_from_string("2", base);
power_bigint(base, 65, start);
power_bigint(base, 66, end);
__global char *result_real = randomGenPrivKey(start, end, Addr);
// int id = get_global_id(0);
int id =
get_global_id(1) * get_global_size(0) + get_global_id(0); // 1D global id
result[id] = result_real[id]; // change char by char for errors.
}
// DEFINITION OF: ALGORITHM
/*
OBJECTIVE: generate Random Big number and generate BTC addr from it
NEEDED: Big integer API, secp256k1, ripemd160, SHA256, True Random API
CORE NEEDED: Big Integer, True Random
*/finally, kernel file:
#include "bigint.clh"
int len(__global char *str)
{
int i = 0;
while (str[i] != '\0')
{
i++;
}
return i;
}
void int_to_bytes(__global uint *num, __global uchar *byte_array)
{
uint value = num[0];
for (int i = 0; i < 4; i++)
{
byte_array[3 - i] = (uchar)(value & 0xFF);
value >>= 8;
}
}
void bytes_to_int(__global uchar *byte_array, __global uint *result)
{
uint value = 0;
for (int i = 0; i < 4; i++)
{
value = (value << 8) | byte_array[i];
}
result[0] = value;
}
void concate_str(__global char *a, char *b, __global char *result)
{
int gid = get_global_id(0);
// Calculate the lengths of the input strings
int lena = 0;
while (a[lena] != '\0')
{
lena++;
}
int lenb = 0;
while (b[lenb] != '\0')
{
lenb++;
}
// Concatenate the strings
for (int i = 0; i < lena; i++)
{
result[i] = a[i];
}
for (int i = 0; i < lenb; i++)
{
result[lena + i] = b[i];
}
// Null-terminate the result string
result[lena + lenb] = '\0';
}
void concate_str_ng(__constant char *a, char *b, __global char *result)
{
int gid = get_global_id(0);
// Calculate the lengths of the input strings
int lena = 0;
while (a[lena] != '\0')
{
lena++;
}
int lenb = 0;
while (b[lenb] != '\0')
{
lenb++;
}
// Concatenate the strings
for (int i = 0; i < lena; i++)
{
result[i] = a[i];
}
for (int i = 0; i < lenb; i++)
{
result[lena + i] = b[i];
}
// Null-terminate the result string
result[lena + lenb] = '\0';
}
__constant uint k[64] = {
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2};
__constant uint initial_hash[8] = {0x6a09e667, 0xbb67ae85, 0x3c6ef372,
0xa54ff53a, 0x510e527f, 0x9b05688c,
0x1f83d9ab, 0x5be0cd19};
uint rotate_sha(uint x, uint n) { return (x >> n) | (x << (32 - n)); }
void sha256(__global uchar *in, __global uchar *out, uint length)
{
uint h[8];
for (int i = 0; i < 8; i++)
{
h[i] = initial_hash[i];
}
// Process each 512-bit chunk
for (int chunk = 0; chunk < length; chunk += 64)
{
uint w[64];
for (int i = 0; i < 16; i++)
{
w[i] = (in[chunk + 4 * i] << 24) | (in[chunk + 4 * i + 1] << 16) |
(in[chunk + 4 * i + 2] << 8) | in[chunk + 4 * i + 3];
}
for (int i = 16; i < 64; i++)
{
uint s0 = (rotate_sha(w[i - 15], 7) ^ rotate_sha(w[i - 15], 18) ^
(w[i - 15] >> 3));
uint s1 = (rotate_sha(w[i - 2], 17) ^ rotate_sha(w[i - 2], 19) ^
(w[i - 2] >> 10));
w[i] = w[i - 16] + s0 + w[i - 7] + s1;
}
uint a = h[0];
uint b = h[1];
uint c = h[2];
uint d = h[3];
uint e = h[4];
uint f = h[5];
uint g = h[6];
uint hh = h[7];
for (int i = 0; i < 64; i++)
{
uint S1 = (rotate_sha(e, 6) ^ rotate_sha(e, 11) ^ rotate_sha(e, 25));
uint ch = ((e & f) ^ (~e & g));
uint temp1 = hh + S1 + ch + k[i] + w[i];
uint S0 = (rotate_sha(a, 2) ^ rotate_sha(a, 13) ^ rotate_sha(a, 22));
uint maj = ((a & b) ^ (a & c) ^ (b & c));
uint temp2 = S0 + maj;
hh = g;
g = f;
f = e;
e = d + temp1;
d = c;
c = b;
b = a;
a = temp1 + temp2;
}
h[0] += a;
h[1] += b;
h[2] += c;
h[3] += d;
h[4] += e;
h[5] += f;
h[6] += g;
h[7] += hh;
}
for (int i = 0; i < 8; i++)
{
out[4 * i] = (h[i] >> 24) & 0xff;
out[4 * i + 1] = (h[i] >> 16) & 0xff;
out[4 * i + 2] = (h[i] >> 8) & 0xff;
out[4 * i + 3] = h[i] & 0xff;
}
}
__constant char base58_alphabet[] =
"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
void base58_encode(__global uchar *input, uint input_len,
__global char *output)
{
uint i, j;
uint carry;
const uint max_output_len = 256; // Adjust this size based on your needs
uchar temp[max_output_len];
// Initialize the temp array
for (i = 0; i < max_output_len; i++)
{
temp[i] = 0;
}
for (i = 0; i < input_len; i++)
{
carry = input[i];
for (j = max_output_len; j > 0; j--)
{
carry += (uint)(temp[j - 1]) << 8;
temp[j - 1] = carry % 58;
carry /= 58;
}
}
// Skip leading zeros in temp
i = 0;
while (i < max_output_len && temp[i] == 0)
{
i++;
}
// Translate the digits to the base58 alphabet
j = 0;
while (i < max_output_len)
{
output[j++] = base58_alphabet[temp[i++]];
}
for (i = 0; i < input_len && input[i] == 0; i++)
{
output[j++] = '1';
}
// Null-terminate the output string
output[j] = '\0';
}
void create_versioned_key(__global const char *privkey,
__global char *versioned)
{
// Define the prefix and suffix
const char prefix[] = "80";
const char suffix[] = "01";
// Calculate the lengths
uint prefix_len = 2; // Length of "80"
uint privkey_len = 0;
uint suffix_len = 2; // Length of "01"
// Calculate the length of the private key
while (privkey[privkey_len] != '\0')
{
privkey_len++;
}
// Construct the versioned key
uint i = 0;
// Add the prefix
for (i = 0; i < prefix_len; i++)
{
versioned[i] = prefix[i];
}
for (uint j = 0; j < privkey_len; j++, i++)
{
versioned[i] = privkey[j];
}
for (uint j = 0; j < suffix_len; j++, i++)
{
versioned[i] = suffix[j];
}
// Null-terminate the versioned key
versioned[i] = '\0';
}
void compress_private_key(__global char *privkey, __global char *out)
{
int id =
get_global_id(1) * get_global_size(0) + get_global_id(0); // 1D global id
// __global const char *versioned = "80" + privkey + "01";
__global char *versioned;
create_versioned_key(privkey, versioned);
__global uchar *versioned_b;
stringToByteArray(versioned, versioned_b, len(versioned));
__global uchar *firsh_sha;
sha256(versioned_b, firsh_sha, len(versioned));
__global uchar *second_sha;
sha256(firsh_sha, second_sha, sizeof(firsh_sha) / sizeof(firsh_sha[0]));
char checksum[8];
checksum[id % 8] = second_sha[id % 8];
__global char *final_key;
concate_str(versioned, checksum, final_key);
__global uchar *final_key_b;
stringToByteArray(final_key, final_key_b, len(final_key));
__global char *compressed_key;
base58_encode(final_key_b, len(final_key), compressed_key);
*out = *compressed_key;
}
void random_generator(__global const
BigInt
*start, __global const
BigInt
*end,
__global
BigInt
*output)
{
int gid = get_global_id(0);
// Seed based on global ID and current time
uint seed = gid + (uint)get_global_id(0) + (uint)get_global_size(0);
// Linear Congruential Generator (LCG) parameters
uint a = 1664525;
uint c = 1013904223;
uint m = 0xFFFFFFFF;
// Initialize state with seed
uint state = seed;
// Generate random number
state = (a * state + c) % m;
// Map the random number to the range [start, end]
BigInt range;
init_bigint_ng(&range);
// Calculate range = end - start
for (int i = 0; i < MAX_DIGITS; i++)
{
range.digits[i] = end[gid].digits[i] - start[gid].digits[i];
}
range.length = end[gid].length;
// Calculate output = start + (state % range)
for (int i = 0; i < MAX_DIGITS; i++)
{
output[gid].digits[i] = start[gid].digits[i] + (state % range.digits[i]);
}
output[gid].length = start[gid].length;
}
__constant uint K[5] = {0x00000000, 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xA953FD4E};
__constant uint KK[5] = {0x50A28BE6, 0x5C4DD124, 0x6D703EF3, 0x7A6D76E9, 0x00000000};
void ripemd160_hash(__global const uchar *input, __global uint *output, uint length)
{
uint h0 = 0x67452301;
uint h1 = 0xEFCDAB89;
uint h2 = 0x98BADCFE;
uint h3 = 0x10325476;
uint h4 = 0xC3D2E1F0;
uint A, B, C, D, E, AA, BB, CC, DD, EE, T;
uint X[16];
for (uint i = 0; i < length; i += 64)
{
for (uint j = 0; j < 16; j++)
{
X[j] = ((uint)input[i + 4 * j]) | (((uint)input[i + 4 * j + 1]) << 8) |
(((uint)input[i + 4 * j + 2]) << 16) |
(((uint)input[i + 4 * j + 3]) << 24);
}
A = AA = h0;
B = BB = h1;
C = CC = h2;
D = DD = h3;
E = EE = h4;
// Main loop
for (uint j = 0; j < 80; j++)
{
uint F = (B ^ C ^ D);
uint Kt = K[j / 16];
uint s = (j % 16);
T = A + F + X[s] + Kt;
A = E;
E = D;
D = (C << 10) | (C >> (32 - 10));
C = B;
B = (T << s) | (T >> (32 - s));
uint tempAA = AA;
uint tempBB = BB;
uint tempCC = CC;
uint tempDD = DD;
uint tempEE = EE;
F = (tempBB ^ (tempCC | ~tempDD));
Kt = KK[j / 16];
s = (j % 16);
T = tempAA + F + X[s] + Kt;
AA = tempEE;
EE = tempDD;
DD = (tempCC << 10) | (tempCC >> (32 - 10));
CC = tempBB;
BB = (T << s) | (T >> (32 - s));
}
T = h1 + C + DD;
h1 = h2 + D + EE;
h2 = h3 + E + AA;
h3 = h4 + A + BB;
h4 = h0 + B + CC;
h0 = T;
}
output[0] = h0;
output[1] = h1;
output[2] = h2;
output[3] = h3;
output[4] = h4;
}
const char *privkeyToAddr(__global char *privkey_c)
{
int id =
get_global_id(1) * get_global_size(0) + get_global_id(0); // 1D global id
__global BigInt *a;
string_to_bigint(privkey_c, a);
__global uchar *byte_array_a;
bigint_to_byte_array(a, byte_array_a);
int len_byte_array_a = sizeof(byte_array_a) / sizeof(byte_array_a[0]);
__global uchar *_x;
__global uchar *_y;
if (id < (len_byte_array_a / 2))
{
_x[id] = byte_array_a[id];
}
else if (id < len_byte_array_a)
{
_y[id] = byte_array_a[id];
}
__global BigInt *x;
__global BigInt *y;
byteArrayToBigInt(_x, len_byte_array_a / 2, x);
byteArrayToBigInt(_y, len_byte_array_a / 2, y);
__global BigInt *x_cube;
__global BigInt *seven;
__global BigInt *y_square;
__global BigInt *x_cube_p_seven;
__global BigInt *x_cube_p_seven_m_y_square;
__global BigInt *p;
__global BigInt *public_k;
power_bigint(x, 3, x_cube);
int_to_bigint(7, seven);
add_bigint(x_cube, seven, x_cube_p_seven);
power_bigint(y, 2, y_square);
subtract_bigint(x_cube_p_seven, y_square, x_cube_p_seven_m_y_square);
init_bigint_from_string("115792089237316195423570985008687907853269984665640564039457584007908834671663",
p);
bigint_mod(x_cube_p_seven_m_y_square, p, public_k);
__global uchar *public_k_b;
__global uchar *public_k_h;
__global uint *public_k_rh;
__global uchar *public_k_rh_b;
__global char *public_k_rs;
bigint_to_byte_array(public_k, public_k_b);
sha256(public_k_b, public_k_h, sizeof(public_k_b) / sizeof(public_k_b[0]));
ripemd160_hash(public_k_h, public_k_rh,
sizeof(public_k_h) / sizeof(public_k_h[0]));
int_to_bytes(public_k_rh, public_k_rh_b);
byteArrayToString(public_k_rh_b, public_k_rs,
sizeof(public_k_rh_b) / sizeof(public_k_rh_b[0]));
__global char *network_byte;
concate_str_ng("00", public_k_rs, network_byte);
__global uchar *network_byte_b;
stringToByteArray(network_byte, network_byte_b, len(network_byte));
__global uchar *sha1;
__global uchar *sha2;
sha256(network_byte_b, sha1,
sizeof(network_byte_b) / sizeof(network_byte_b[0]));
sha256(sha1, sha2, sizeof(sha1) / sizeof(sha1[0]));
char checksum[9];
__global char *sha2_hex;
byteArrayToHexString(sha2, sha2_hex, sizeof(sha2) / sizeof(sha2[0]));
if (id < 8)
checksum[id] = sha2_hex[id];
__global char *net_checksum;
concate_str(network_byte, checksum, net_checksum);
__global BigInt *net_checksum_B;
string_to_bigint(net_checksum, net_checksum_B);
__global uchar *net_checksum_b;
bigint_to_byte_array(net_checksum_B, net_checksum_b);
__global char *addr;
base58_encode(net_checksum_b,
sizeof(net_checksum_b) / sizeof(net_checksum_b[0]), addr);
return addr;
}
__global char *randomGenPrivKey(__global BigInt *start, __global BigInt *end,
__global char *wallet_addr)
{
__global BigInt *a;
__global char *b;
while (1)
{
random_generator(start, end, a);
bigint_to_hex_string(a, b);
while (len(b) < 64)
{
if (len(b) < 64)
{
*b = '0' + *b;
}
}
__global char *compressed_privkey;
compress_private_key(b, compressed_privkey);
if (wallet_addr == privkeyToAddr(compressed_privkey))
{
// return (*b);
return b; // return to host
}
}
}
// support biginteger chatgpt: would you write a big integer struct in opencl C
// after ask what you want
__kernel void main_src(__global uchar *result) // strlen : 64
{
__global BigInt *start;
__global BigInt *end;
__global char *Addr;
__global BigInt *base;
init_bigint_from_string("2", base);
power_bigint(base, 65, start);
power_bigint(base, 66, end);
__global char *result_real = randomGenPrivKey(start, end, Addr);
// int id = get_global_id(0);
int id =
get_global_id(1) * get_global_size(0) + get_global_id(0); // 1D global id
result[id] = result_real[id]; // change char by char for errors.
}
// DEFINITION OF: ALGORITHM
/*
OBJECTIVE: generate Random Big number and generate BTC addr from it
NEEDED: Big integer API, secp256k1, ripemd160, SHA256, True Random API
CORE NEEDED: Big Integer, True Random
*/this looks fine i think i couldn't manage the cores and memory fine but it just needs optimization i think please let me know if you manage to how to run this code without any errors
Thanks.
r/OpenCL • u/jsgui • Jul 31 '13
I'm wanting to get started writing some OpenCL for some relatively simple applications, but find it hard knowing where to start. I am experienced with OOP but a beginner with C and C++, and have been focusing a lot on node.js recently, and my goal includes creating language bindings between node.js and the C or C++ code that is used to call OpenCL.
It would be great to have others contribute other language bindings to this.
Essentially, the project will include quite a lot of boilerplate code, with explanations of what it does, and if necessary which hardware platforms it will work on.
A goal is to enable a beginner to OpenCL, like myself, to download some code, follow some simple instructions (not too many, it should be compile and run), and have some simple OpenCL code running in Windows, OS X or Linux within a couple of minutes. When that is running, the beginner can learn by doing, and hopefully progress to an intermediate before too long.
If any experienced OpenCL coders want to contribute their time into this open-source project then that would be really useful in making OpenCL more usable and better understood by a wider audience. Any advice about which existing code samples to use and how to get them to run would also be much appreciated.
r/OpenCL • u/lijicheng1006 • Jun 04 '17
I'm new to GPU and OpenCL. My GPU is Intel Iris Graphics 6100 1536 MB on Mac, CL_DEVICE_MAX_COMPUTE_UNITS is 48. Honestly I do not have a good understanding on GPU hardware level. So my question is: Assume we want to test the best speedup GPU could achieve. And because my GPU has 48 compute units, and since a work group is running on a single work unit defined by OpenCL, does it mean I could only use 1/48 of total GPU at most for a single task? Or this 48 is the max number of the computer units, that means at most 48 different work groups can run at a time. If I only assign a single task on GPU, the compute unit is actually 1(ideally suppose GPU wouldn't do anything else meanwhile except our task), but occupying all GPU resources. Also that means for a compute unit, its "size" is not fixed, correct? If we only do a single task, it's a big one, controlling all processing elements all by itself. If 2 task at the same time, then each using 1/2 GPU resources. If there are 48 work groups running all together, ideally each one is controlling 1/48 of total processing elements of GPU?
r/OpenCL • u/streamcomputing • Nov 02 '12
r/OpenCL • u/streamcomputing • Nov 02 '12
r/OpenCL • u/Forest_of_Crystals • Jul 31 '22
Hello,
ive been trying to figure out what is wrong as it cant find CL/opencl.lib
im using OCL SDK light and ${OCL_ROOT}\include in C/C++ general, ${OCL_ROOT}\lib\x86_64 in linker general, and opencl.lib in linker input.
im using an amd gpu too.
r/OpenCL • u/[deleted] • Jun 30 '20
Alright, so I wanted to make a function that would make it easier for me to pass arguments to the kernel, but it doesn't seem to do so? If I pass arguments regularly, like this:
kernel.setArgs(0, arg)
kernel.setArgs(1, arg2)
...It works fine. However, when I have a function like this:
template<typename ...Args>
void launchKernel(cl::NDRange offset, cl::NDRange end, Args... args)
{
std::vector<std::any> vec = { args... };
for (int i = 0; i < vec.size(); i++)
{
kernel.setArg(i, vec[i]);
}
//queue.enqueueNDRangeKernel(kernel, offset, end);
queue.enqueueTask(kernel);
}it passes nothing1 to the kernel, and as a result, I get back nothing. I am quite sure this is actually the problem because as I said, it works when I set args the other way and launch in the same way. I also think it probably has something to do with std::any. I have verified that the ages coming through are actually what they should be (buffers) by doing something like this:
std::cout << vec[i].type().name();Which prints cl::Buffer. What am I doing wrong?
1 By nothing, I mean null. When I read the buffer, I get back a buffer full of "\0"
r/OpenCL • u/streamcomputing • Dec 15 '13
r/OpenCL • u/olljoh • Mar 23 '16
Would it make sense to define a Kernel like a text parser?
Let Every atomic char or int that is sent to a kernel be a pointer to a noun or verb, that is defined by writing and compiling the Kernel. You make kernels that parse inputs. Of course the opencl Kernel instruction set is fully included in that definition and a sentence can define custom variables up to some limit. So the Kernel becomes remote controlled and can be fully self aware by parsing its own instruction set. If your Kernels library has a Definition missing you compile a new one that has it. A Library of 216 instructions makes a big flexible Kernel but its far from encyclopedic. So you may need different compiled volumes of The full encyclopedia.
A uint16 becomes a sentence of instructions and pointers. Sentences can have references to other sentences, like; continued-after-this-linebreak. A workgroup processes 1 sentence, one vec16 for wach workitem, waiting for its refferece to ne inserted like an alias.
A Kernel becomes a text parser that parses thousands of sentences in parallel. Or just a Single sentence on a More idle tick.
r/OpenCL • u/streamcomputing • Oct 04 '12