Suppose I already have a C program that solves a specific computational problem. I want to convert that into a CUDA program.
What steps should I follow to do that?
For instance, can I think as follows?
Step 1. select which part you want to parallelize.
Step 2. determine if a GPU-friendly algorithm could be adapted.
E.g., in the case of matrix multiplication, we can use the sum of the outer product method.
Step 3. determine how much memory is needed in the GPU. Allocate that memory to the GPU.
Step 4. determine how many threads, blocks, and grids are needed.
E.g., Assuming that the matrix multiplication is implemented using this approach, the number of threads, blocks, and grids required would be:
- The number of threads per block can be determined based on the hardware limits of the GPU and the size of the tile being computed. A common choice is to use a block size of 16x16 or 32x32, depending on the GPU architecture.
- The number of blocks per grid can be determined based on the size of the matrices being multiplied and the block size. Specifically, the number of blocks required in each dimension can be computed as ceil(n / block_size), where n is the size of the matrix in that dimension. For example, if the block size is 16 and the size of the first matrix is m*n, then the number of blocks required in the x dimension would be ceil(n / 16), and the number of blocks required in the y dimension would be ceil(m / 16).
- The number of grids required would depend on the size of the matrices being multiplied and the number of blocks per grid. Specifically, the number of grids required in each dimension can be computed as ceil(matrix_size / (block_size * num_blocks)), where matrix_size is the size of the matrix in that dimension and num_blocks is the number of blocks in that dimension.
Step 5. ... ... ...
If yes, please give me a guideline.