Reputation: 2599
I need to write a program that computes values in a matrix, first sequentially, then in parallel using openCL.
It is the same thing as I already did in regular C (using MPI)
I want to make simple functions to initializeMatrix and printMatrix and the likes.
In C i used to do this very simply :
// Matrix initialization
void initMatrix(size_t M, size_t N, double (*matrix)[M][N][2])
{
int i, j;
for (j = 0; j < N; ++j)
{
for (i = 0; i < M; ++i)
{
(*matrix)[i][j][0] = (double)(( i * ( M - i - 1 ) ) * ( j * ( N - j - 1 ) ));
(*matrix)[i][j][1] = (*matrix)[i][j][0];
}
}
printf("Matrix has been initialized\n");
}
I saw this gets me errors in C++, as the compiler wants to know at COMPILE TIME the sizes of arrays (the M and N sizes are passed as arguments to program, therefore I can't know at compile time).
How do I do this in C++?
I am considering using Vectors, but I'm not sure if it's a good idea since I will have to use the OpenCL library
Upvotes: 0
Views: 135
Reputation: 56547
You can pass the array by reference/const reference via a template:
#include <iostream>
#include <cstddef> // for std::size_t
template <typename T, int M, int N, int P>
void f(T (&arr)[M][N][P]) // accepts 3-D arrays of arbitrary types
{
std::cout << "Size: " << M << " x " << N << " x " << P;
}
int main()
{
const std::size_t M = 2;
const std::size_t N = 3;
double arr[M][N][4];
f(arr);
}
Upvotes: 2