Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialising dim3 variables in CUDA, how does the "dim3 dimGrid(numBlocks);" syntax work?

I'm learning CUDA, and in plenty of example code I see block and grid dimensions being set like this:

dim3 dimGrid(numBlocks);
dim3 dimBlock(numThreadsPerBlock);
exampleKernel<<<dimGrid, dimBlock>>>(input);

I understand that a line like dim3 dimGrid(numBlocks); is initialising dimGrid, a variable of dim3 type, to have numBlocks as its x value - but I'm not sure how this works.

I would have just assumed it was normal C++ syntax, but for C++ I thought that the line has to be written like this:

dim3 dimGrid = dim3(numBlocks);

Otherwise you get "the most vexing parse". So I'm assuming that the interpretation of those lines as a variable assignment is special behaviour by CUDA's NVCC compiler, but I can't find anything that confirms this.

Am I right that this is what is happening, or is there something else I don't understand about how this works?

like image 821
Adam Goodwin Avatar asked Feb 16 '23 01:02

Adam Goodwin


1 Answers

This is normal C++ syntax, you can try it yourself with a minimal working example.

#include <iostream>

using namespace std;

class A {
    int _x;
public:
    A(int x) : _x(x) {}
    int x() const { return _x; }
};

int main() {
    A first(3);
    cout << first.x() << endl; // "3"
    return 0;
}

Hope this helps.

like image 55
pfac Avatar answered Feb 19 '23 03:02

pfac