Divide Array Elements by Scalar using CUDA C++

Divide Array Elements by Scalar using CUDA C++

In many high-performance computing tasks - such as data preprocessing, graphics, or scientific simulations - we often need to perform operations on arrays, such as division by a scalar value. When these arrays get large, performing such operations on the CPU can become a bottleneck. CUDA lets us use the parallel processing of GPUs. This tutorial explains how to divide array elements by scalar using CUDA C++.

We define a kernel called divide that takes a pointer to our data array, the number of elements, and the divisor. This kernel performs division of the array elements by a scalar.

In the main function, the array is initialized on the host, and memory for the array is allocated on the GPU using cudaMalloc. The array is copied to the device with cudaMemcpy, and the kernel is launched with a sufficient number of blocks and threads to ensure every element of the array is processed. After the kernel completes, the modified array is copied back to the host, printed, and the GPU memory is freed.

#include <iostream>
#include <vector>

__global__ void divide(float *data, const size_t n, const float divisor) {
    unsigned int i = blockDim.x * blockIdx.x + threadIdx.x;
    if (i < n) {
        data[i] /= divisor;
    }
}

int main() {
    std::vector<float> a = {
        0, 14, 28, 42, 56, 70, 84, 98, 112, 126, 140, 154, 168, 182, 196, 210, 224, 255
    };

    size_t bytes = a.size() * sizeof(float);
    float *da;
    cudaMalloc(&da, bytes);

    cudaMemcpy(da, a.data(), bytes, cudaMemcpyHostToDevice);

    size_t blockSize = 256;
    size_t numBlocks = (a.size() + blockSize - 1) / blockSize;

    divide<<< numBlocks, blockSize >>>(da, a.size(), 255);
    cudaMemcpy(a.data(), da, bytes, cudaMemcpyDeviceToHost);

    for (auto value: a) {
        std::cout << value << " ";
    }

    cudaFree(da);

    return 0;
}

Output:

0 0.054902 0.109804 0.164706 ... 0.768627 0.823529 0.878431 1

Leave a Comment

Cancel reply

Your email address will not be published.