Finding area of sine wave using cuda

Parikshit Patil
2 min readDec 3, 2019

How to find area under function?

area of sine wave.

We are taking N i.e no of steps from 0 -> π . In in each step, we will find middle point of strip shown in above image. and find area of that single strip with following formula.

finding area of rectangular strip

Basics of cuda c

nVIdia GPU is called as Device and CPU from where code is invoked is called host. for running code on device, we need to transport/copy our data/variables to GPU Memory.

How to Send data back and forth?

for sending data/variables back and forth, we will use cuda api calls.

cudaError_t cudaMalloc ( void ** devPtr, size_t size )

for allocating space on GPU Memory. while

__host__ ​cudaError_t cudaMemcpy ( void* dst, const void* src, size_t count, cudaMemcpyKind kind )

used for copying data either from host to device (CPU->GPU) or device to host (GPU->CPU). That flow is dependent on forth parameter cudaMemcpyKind.

example of copying data from device to host

How to run code on GPU?

__global__ function will executed on GPU Device and can be called as

functionName<<<block, ThreadPerBlock>>>();

How to find area of sine wave?

We are taking N i.e no of steps 1000000. for each step, GPU compute area of strip and save in array on GPU memory. after all operations completed this array is copied to Host i.e. CPU memory Aka RAM. Then all array values added to final output. output must be 2 but some minute error, it will be around 1.999.

Working Code for finding area

How to run code and get output?

check that your PC/laptop has nVidia graphic card and whether it is supporting cuda. if yes, install cuda on your machine. Then save above code as main.cu and in same directory run commands

for compiling,

> nvcc main.cu

for running,

> ./a.out

for profiling,

> nvprof ./a.out

I started writing blog on github pages. check it you may find some useful content there.

--

--