I want to pass a two-dimensional array in C# as a parameter to a function from C++ DLL (It has to be C++ as I am using CUDA C++). I tried many things but failed to pass it directly using arrays or vectors. The only thing that I could do is to convert it to a one-dimensional array, passing it to the function along with its dimensions, then convert it back to a two-dimensional vector. Here is the C++ DLL code:
int Add(int* a, int m, int n)
{
int i, j, ans;
vector<vector<int>> A(m, vector<int>(n));
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
A[i][j] = a[i * n + j];
// Main calculations
return ans;
}
and this is the C# code that passes the array:
[DllImport("CUDALib.dll")]
static extern int Add(int[] a, int m, int n);
private void PassArray(int[,] A)
{
int i, j, m, n, ans;
int[] a;
m = A.GetLength(0);
n = A.GetLength(1);
a = new int[m * n];
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
a[i * n + j] = A[i, j];
ans = Add(a, m, n);
}
I there any faster, more efficient and direct way to do this?