0
$\begingroup$

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:

  1. 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.
  2. 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).
  3. 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.

$\endgroup$

1 Answer 1

2
$\begingroup$

Your steps in the example are correct but it is hard to say without much information.

If you are doing something akin to a linear equation approximator or eigenpair approximator, where most of the involved steps are matrix-vector multiplications, then the custom CUDA kernel steps you posted will work just fine. For example for a Lanczos or QR or whatever your project is. Because for example (I am not sure what C modules exist for sparse matrices) but most Hamiltonians are sparse and thus the kernel structure will differ.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.