Skip to main content

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:

  1. Use Loop unrolling to compute in CUDA any size and any count of dimensions array
  2. Use Batching to compute any size array, even if it s big that can't be fitted in GPU memory

Requirements:

CUDA
Python

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.4.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

unrollcuda-0.0.4-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file unrollcuda-0.0.4.tar.gz.

File metadata

  • Download URL: unrollcuda-0.0.4.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

Hashes for unrollcuda-0.0.4.tar.gz
Algorithm Hash digest
SHA256 43c2a702618c824e44aacfee36679e45b3f205c032999c6579ea38be45ad0b1e
MD5 90e040ead7a63dd9061a36383d11351d
BLAKE2b-256 92fc9a820c9292f8af403c063d3ddbb5aa9391fa35a6a2b17a9aa77aa1f8aab3

See more details on using hashes here.

File details

Details for the file unrollcuda-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: unrollcuda-0.0.4-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

Hashes for unrollcuda-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 a43ac7855cd0a38dfdd67bb32f6469952fce12ac53776bb83eaf03c2d8450b7a
MD5 180a58d151dce6704b0cb6eaa9b8a802
BLAKE2b-256 ea5fc2b62994f565fbc504d620c02c0e8e3e1e5756ff677dc6e1be7170d606e5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page