What is better practise in high-performance computing: passing a struct of data into a function or a set of variables?

三世轮回 提交于 2019-12-12 05:28:16

问题


Imagine that I have a struct that contains a set of variables that describe an object, which is a grid in my case. I was wondering, if I have a function that only uses a subset of the grid, whether there are any performance differences in the two variants of the computational_kernel functions below. The kernels are the same, except that the one in which the struct is passed has to extract the itot, jtot and ktot from the struct before heavy computations are done.

struct Grid
{
    int itot;
    int jtot;
    int ktot;

    int not_used_in_kernel1;
    int not_used_in_kernel2;
    int not_used_in_kernel3;
    int not_used_in_kernel4;
}
Grid grid;

// Code that initializes the grid values...

// Variant 1
computational_kernel(double* array1, double* array2,
                     const int itot, const int jtot, const int ktot);

// Variant 2
computational_kernel(double* array1, double* array2,
                     const Grid& grid);

回答1:


If computational_kernel is a function that does a lot of work internally and is invoked few times the difference between the two versions is infinitesimal. The second version has just the extra cost of dereferencing 3 values, than the rest is identical, and you presumably have to do such dereferencing anyway before invoking the first version.

I would definitely use the second form for compactness reasons: if you are defining object-oriented data structures then use them in such a fashion (better encapsulation).




回答2:


I think passing a struct is better for the code maintenence. if you add new fields to your grid, you will only have to change the function. But passing a set of variables you will have to change the function and every call to the function.




回答3:


I would say that passing a reference to a struct as in the 2nd variant would probably be more efficient performance-wise. On the 1st variant the caller will need to push the 3 int variables on the stack while on the 2nd variant all it has to push is a reference (a pointer) to the struct and do the stuff there. The performance impact is of course bigger if you had more than 3 variables to pass.



来源:https://stackoverflow.com/questions/28041181/what-is-better-practise-in-high-performance-computing-passing-a-struct-of-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!