Skip to content

Scalar Parameters For Kernel

Hüseyin Tuğrul BÜYÜKIŞIK edited this page May 31, 2024 · 8 revisions

Scalar parameter is a read-only kernel parameter for gpu and has only 1-element.

From vector-add sample that adds a scalar to all elements of vector:

        const size_t n = 64;

        GPGPU::Computer computer(GPGPU::Computer::DEVICE_GPUS,0/*select only first gpu*/);
        computer.compile(
            R"(

            kernel void vecAdd(global int * A, const global int * B, const int scalar) 
            { 
                const int threadId=get_global_id(0); 
                A[threadId] = B[threadId] + scalar;
            }

            )", "vecAdd");

        auto scalar = computer.createScalarInput<int>("scalar"); // this is not an array on GPU. its a kernel scalar value.
        auto data1 =  computer.createArrayOutputAll<int>("A", n); // array output (copy all elements to host RAM, only for single-gpu)
        auto data2 =  computer.createArrayInput<int>("B", n); // array input (broadcast all elements from host RAM to all devices)


        auto kernelParams = data1.next(data2).next(scalar);
        
        for (int i = 0; i < 5; i++)
        {
            data2.access<int>(15)=i;
            scalar = 1000; // same as scalar.access<int>(0)=1000;
            computer.compute(kernelParams, "vecAdd", 0, 64 /* kernel threads */, 4 /* block threads */);                                
            std::cout << data1.access<int>(15)<<std::endl;
        }

output:

1000
1001
1002
1003
1004
Clone this wiki locally