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
import unrollcuda as uc


def main():
        
    dimensions = [3, 4, 7, 12]
    shape = [int(size) for size in dimensions]
    # random boolean values
    arr = np.random.choice(
        a=[False, True],
        size=shape,
        p=[0.5, 0.5],
        )
    print('Array shape: ', arr.shape)
    
    # Read the kernel code from the file
    with open('invert.cu', 'r') as f:
        kernel_code = f.read()
    # Define the unrollcuda instance
    ker = uc.kernel(kernel_code)
    # Call inference
    arr_new = ker.inference(arr)

    # Prepare the test array
    arr_test = arr.copy()
    # Convert all False values to True and vice versa
    arr_test = np.logical_not(arr_test)

    # Check the result
    result_check = np.array_equal(arr_new, arr_test)
    print('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.6.tar.gz (5.6 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.6-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: unrollcuda-0.0.6.tar.gz
  • Upload date:
  • Size: 5.6 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.6.tar.gz
Algorithm Hash digest
SHA256 a9695faa99efa6bb7f991f7d2d879f6fea421abbd2699e4745e18ccf7fad1d91
MD5 319fb9481e6093ec4335ef38cbbac250
BLAKE2b-256 2cd32c381c5cebeb70ce73746e283ef80ee5f18987cced193ea6316ccd15a2e9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: unrollcuda-0.0.6-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.6-py3-none-any.whl
Algorithm Hash digest
SHA256 c7c36b93631dedae431b7408e9ffeb724a9d888b6df12ab85eae6685f5e6d396
MD5 a1850d938ca17ed1d717117cdf946d03
BLAKE2b-256 1b75c60472f88e73cfd9ef3e9aabba4c5d557e07896e592e593cad8694806cbd

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