Here is an algorithm that does a base test of finding the adjacent diagonal neighbor and records the results using a 4x4 identity matrix. This does not include any use of OMP or parallel computing. However this is a generic class template of a MxN matrix that is simple enough to use. Instead of storing the contents in a vector of vectors; I've flattened out the data to a single 1D vector and the amount of memory is already preserved upon instantiation of the template. I'm using a function template to compare elements from within a matrix passing back the indexes (M,N) or (x,y) as well as if the result was true or false. I use a struct here to contain the relationship of the x-y indexes & the bool result. The heuristics of checking the neighbors avoids looking in the last column & last row of the matrix since there will not be any elements father to the right nor farther down: this can be seen within the main function. This may be of help to you to where you can try to apply the class, struct & function to the OMP library.
template<unsigned Col, unsigned Row>
class Matrix2D {
public:
const unsigned col_size = Col;
const unsigned row_size = Row;
const unsigned stride_ = col_size;
const unsigned matrix_size = col_size * row_size;
private:
std::vector<int> data_;
public:
Matrix2D() {
data_.resize( matrix_size );
}
void addElement( unsigned x, unsigned y, int val ) {
data_[(x * col_size + y)] = val;
}
/*int getElement( unsigned x, unsigned y ) {
int value = data_[(x * col_size + y)];
return value;
}*/
int getElement( unsigned idx ) {
return data_[idx];
}
};
struct Neighbor {
unsigned indexCol;
unsigned indexRow;
bool notSame;
};
template<unsigned Col, unsigned Row>
void compareMatrixDiagonals( Matrix2D<Col, Row>& mat, Neighbor& n, unsigned colIdx, unsigned rowIdx );
int main() {
Matrix2D<4, 4> mat4x4;
mat4x4.addElement( 0, 0, 1 );
mat4x4.addElement( 0, 1, 0 );
mat4x4.addElement( 0, 2, 0 );
mat4x4.addElement( 0, 3, 0 );
mat4x4.addElement( 1, 0, 0 );
mat4x4.addElement( 1, 1, 1 );
mat4x4.addElement( 1, 2, 0 );
mat4x4.addElement( 1, 3, 0 );
mat4x4.addElement( 2, 0, 0 );
mat4x4.addElement( 2, 1, 0 );
mat4x4.addElement( 2, 2, 1 );
mat4x4.addElement( 2, 3, 0 );
mat4x4.addElement( 3, 0, 0 );
mat4x4.addElement( 3, 1, 0 );
mat4x4.addElement( 3, 2, 0 );
mat4x4.addElement( 3, 3, 1 );
unsigned idx = 0;
for ( unsigned i = 0; i < mat4x4.matrix_size; i++ ) {
std::cout << mat4x4.getElement( i ) << " ";
idx++;
if ( idx == 4 ) {
std::cout << "\n";
idx = 0;
}
}
std::cout << "\n";
unsigned colIdx = 0;
unsigned rowIdx = 0;
std::vector<Neighbor> neighbors;
Neighbor n;
// If we are in the last col or row we can ignore
// (0,3),(1,3),(2,3),(3,3),(3,0),(3,1),(3,2), {*(3,3)* already excluded}
// This is with a 4x4 matrix: we can substitute and use LastCol - LastRow
// for any size MxN Matrix.
const unsigned LastCol = mat4x4.col_size - 1;
const unsigned LastRow = mat4x4.row_size - 1;
for ( unsigned i = 0; i < LastCol; i++ ) {
for ( unsigned j = 0; j < LastRow; j++ ) {
compareMatrixDiagonals( mat4x4, n, i, j );
neighbors.push_back( n );
}
}
for ( unsigned i = 0; i < neighbors.size(); i++ ) {
std::cout << "(" << neighbors[i].indexCol
<< "," << neighbors[i].indexRow
<< ") " << neighbors[i].notSame
<< "\n";
}
std::cout << "\nPress any key & enter to quit." << std::endl;
char c;
std::cin >> c;
return 0;
}
template<unsigned Col, unsigned Row>
void compareMatrixDiagonals( Matrix2D<Col, Row>& mat, Neighbor& N, unsigned colIdx, unsigned rowIdx ) {
unsigned firstIdx = (colIdx * mat.col_size + rowIdx);
unsigned nextIdx = ((colIdx + 1) * mat.col_size + (rowIdx + 1));
if ( mat.getElement( firstIdx ) != mat.getElement( nextIdx ) ) {
N.indexCol = colIdx;
N.indexRow = rowIdx;
N.notSame = true;
} else {
N.indexCol = colIdx;
N.indexRow = rowIdx;
N.notSame = false;
}
}