Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help to understand Calculate Tensors_single.cu #6

Open
Vishwatosh opened this issue Nov 11, 2019 · 2 comments
Open

Help to understand Calculate Tensors_single.cu #6

Vishwatosh opened this issue Nov 11, 2019 · 2 comments

Comments

@Vishwatosh
Copy link

Hello,
First of all, thank you for the nice MMGNLSE code.
I am new to CUDA and therefore, find it difficult to understand the calculation of the overlap integrals in the CUDA file.
I would appreciate if anyone can help me to explain the below lines from the Calculate Tensors_single.cu file:
if (full_thread_idx >= nmp4) {
return;
}

// Turn linear index into components
unsigned int midx1 = full_thread_idx % num_modes;
unsigned int midx2 = (full_thread_idx/num_modes) % num_modes;
unsigned int midx3 = (full_thread_idx/num_modes/num_modes) % num_modes;
unsigned int midx4 = (full_thread_idx/num_modes/num_modes/num_modes);

// Compute the sum
for (int i = 0; i < Nx; i++) {
    for (int j = 0; j < Nx; j++) {
        SR[full_thread_idx] += fields[midx1+i*num_modes+j*Nxnm]*fields[midx2+i*num_modes+j*Nxnm]*fields[midx3+i*num_modes+j*Nxnm]*fields[midx4+i*num_modes+j*Nxnm];
    }
}

// Normalize
SR[full_thread_idx] /= norms[midx1]*norms[midx2]*norms[midx3]*norms[midx4];

Thanks in advance!

@logangwright
Copy link
Contributor

The key things to understand here are the use of kernel functions and vectorized element indexing in CUDA, so that as much happens in parallel as possible (of course the calculation of the overlap integrals is very parallelizable). You probably noticed that the SR tensor is already flattened, but the funny indexing on the fields comes from the vectorized format.
https://devblogs.nvidia.com/even-easier-introduction-cuda/

Note that there should also be the option to calculate the tensors by brute force in Matlab if you prefer (see the Matlab script for calculating the tensors). If in doubt (or you want to check), this can also be used. If I were to write this today, I'd probably just use parfor in Matlab on that.

@Vishwatosh
Copy link
Author

Dear Wright,
Thank you very much for the nice explanation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants