Loop unrolling and batching for CUDA
Project description
unrollcuda
Loop unrolling and batching for CUDA
The core idea of this solution is to give a way to solve the following tasks:
- Use Loop unrolling to compute in CUDA any size and any count of dimensions array
- Use Batching to compute any size array, even if it s big that can't be fitted in GPU memory
Requirements:
Getting Started
Installation
pip install unrollcuda
Usage
Invert values in a multi-dimensional boolean array
invert.cu
__global__ void unroll(
bool *arr,
unsigned int *shape,
unsigned long long gpu_arr_size,
unsigned long long shape_total,
unsigned long long dimensions_count,
unsigned long long step,
unsigned char order,
unsigned long long batch_start
)
{
unsigned long long idx = threadIdx.x + blockIdx.x * blockDim.x;
unsigned long long idx_full;
unsigned int i = 0;
unsigned int *indices = new unsigned int[dimensions_count]; // array to hold the computed indices
unsigned long long tmp;
idx_full = i * step + idx;
while (idx_full < shape_total && idx_full < gpu_arr_size)
{
tmp = idx_full + batch_start; // add batch_start to account for the offset
// Compute the indices
for (unsigned int j = 0; j < dimensions_count; ++j)
{
unsigned int dimension = (order == 0) ? dimensions_count - j - 1 : j;
// Modulo by the dimension size
indices[dimension] = tmp % shape[dimension];
// Divide by the dimension size
tmp /= shape[dimension];
}
//printf("idx_full: %llu, idx: %llu, batch_start: %llu\n", idx_full, idx, batch_start);
for (unsigned int j = 0; j < dimensions_count; ++j)
{
// j is the dimension
// Your code ++
// Invert the value in arr
arr[idx_full] = !arr[idx_full];
// Your code --
break;
}
i += 1;
idx_full = i * step + idx;
}
// Free the memory
delete[] indices;
}
invert.py
import numpy as np
from unrollcuda import unrollcuda
def main():
dimensions = [3, 4]
shape = [int(size) for size in dimensions]
# random boolean values
arr = np.random.choice(
a=[False, True],
size=shape,
p=[0.5, 0.5],
)
with open('invert.cu', 'r') as f:
kernel_code = f.read()
uc = unrollcuda(kernel_code)
arr_new = uc.inference(arr)
# Prepare the test array
arr_test = arr.copy()
print('\nOriginal array:\n', arr_test)
# Convert all False values to True and vice versa
arr_test = np.logical_not(arr_test)
print('\nTest array:\n', arr_test)
# Check the result
result_check = np.array_equal(arr_new, arr_test)
print('\nResult check: ', result_check)
if __name__ == '__main__':
main()
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
unrollcuda-0.0.5.tar.gz
(5.3 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file unrollcuda-0.0.5.tar.gz.
File metadata
- Download URL: unrollcuda-0.0.5.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9f4676dc7d855f0568ffe443f75a8fdb3795749f7ef112646a0c439625135912
|
|
| MD5 |
e313329bf76dc00f16f5a98d3b8d3d60
|
|
| BLAKE2b-256 |
c3e87ab31ba59e9f1a26457a9e5a7d0792814e37a5fef87fb509bfc249e39597
|
File details
Details for the file unrollcuda-0.0.5-py3-none-any.whl.
File metadata
- Download URL: unrollcuda-0.0.5-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b582bb147587b65847f00e92dd0f29b90b4a2c91d2291bb4c3b3c978e452a2df
|
|
| MD5 |
0f643befa0b514b2e5b650b1b7c70748
|
|
| BLAKE2b-256 |
d035d16ecb969070b966f6501aaddf1708dd2ce8ff5cf08165a738a4a12d2928
|