how to use std::copy for int** - c++

My2DClass::My2DClass(const int r, const int c, int mat[3][3]):m_r(r),m_c(c)
{
matrix = new int*[r];
for (int i = 0; i < r; i++)
matrix[i] = new int[c];
for (int i = 0; i < m_r; i++) {
for (int j = 0; j < m_c; j++) {
matrix[i][j] = mat[i][j];
}
}
//std::copy(&mat[0][0], &mat[0][0] + m_r * m_c, &matrix[0][0]);
}
How to use std::copy() for int**? The commented line throws an exception at runtime.

You are allocating an array of pointers to arrays, so a single call to std::copy() will not work. You would have to call std::copy() on every array individually. So really, only your inner-most loop can be replaced with std::copy(), eg:
My2DClass::My2DClass(const int r, const int c, int mat[3][3])
: m_r(r), m_c(c)
{
matrix = new int*[r];
for (int i = 0; i < r; i++)
matrix[i] = new int[c];
for (int i = 0; i < m_r; i++) {
std::copy(&mat[i][0], &mat[i][m_c], matrix[i]);
// or: std::copy_n(&mat[i][0], m_c, matrix[i]);
}
}
If you consolidate the 2 remaining loops into a single loop, eg:
My2DClass::My2DClass(const int r, const int c, int mat[3][3])
: m_r(r), m_c(c)
{
matrix = new int*[r];
for (int i = 0; i < r; ++i) {
matrix[i] = new int[c];
std::copy(&mat[i][0], &mat[i][c], matrix[i]);
// or: std::copy_n(&mat[i][0], c, matrix[i]);
}
}
Then you could replace that loop with std::for_each(), eg:
My2DClass::My2DClass(const int r, const int c, int mat[3][3])
: m_r(r), m_c(c)
{
matrix = new int*[r];
std::for_each(matrix, matrix + r,
[=](int* &arr){
arr = new int[c];
std::copy(&mat[i][0], &mat[i][c], arr);
// or: std::copy_n(&mat[i][0], c, arr);
}
);
}
Though, you really should avoid using new[] manually at all, consider using std::vector instead, which would greatly simplify management of your arrays, eg:
// std::vector<std::vector<int>> matrix;
My2DClass::My2DClass(const int r, const int c, int mat[3][3])
: m_r(r), m_c(c), matrix(r)
{
for(int i = 0; i < r; ++i) {
matrix[i].assign(&mat[i][0], &mat[i][c]);
}
}
Otherwise, consider flattening your matrix into a single 1-dimensional array instead. Especially since the input mat is a flat array in memory anyway. Then you can do a single std::copy() from one to the other, eg:
// int *matrix;
My2DClass::My2DClass(const int r, const int c, int mat[3][3])
: m_r(r), m_c(c), matrix(new int[r*c])
{
std::copy(&mat[0][0], &mat[r][c], matrix);
}
Or, using std::vector instead:
// std::vector<int> matrix;
My2DClass::My2DClass(const int r, const int c, int mat[3][3])
: m_r(r), m_c(c), matrix(&mat[0][0], &mat[r][c])
{
}
Either way, you can convert 2-dimensional indexes into 1-dimensional indexes using this formula:
(r * m_c) + c
For example:
int& My2DClass::operator()(int r, int c)
{
return matrix[(r * m_c) + c];
}

Related

CTPL C++ threadpooling datarace on accessing array's elements

Does accessing the same array's different elements create a data race?
I have a "Matrix" wrapper class for an array with matrix interface, and i wrote a parallel multiplication by a scalar function for it.
I use CTPL library for thread pools.
I know that writing from a thread into an array cell passed by reference is not a data race (please correct me if i'm wrong) so i decided to pass a cell from the array to the function so i can write multiplication result into the cell itself, not by passing the reference to an array and the index, so i can avoid a data race.
I ran the function 10k times and the results did not differ even once, but a sanitizer i use ("-fsanitize=thread -fPIE -pie -g" in Cmake flags) still alerts me of a data race on the line where i create the thread pool.
Is the sanitizer mistaken or am i really experiencing a data race somewhere?
Here are the pieces of code, relevant to the prolem:
Wrapper:
class Matrix {
protected:
int width;
int height;
double* matrix;
public:
Matrix(int m, int n);
Matrix(int m, int n, const std::vector<double>& values);
int get_width() {
return width;
}
int get_height() {
return height;
}
double get_element(int row_num, int col_num);
void set_element(int row_num, int col_num, double el);
double* get_cell_ref(int row_num, int col_num);
};
Method implementations:
Matrix::Matrix(int m, int n) {
assert(m > 0 && n > 0);
matrix = new double[m * n]{0};
width = n;
height = m;
}
Matrix::Matrix(int m, int n, const std::vector<double>& values) {
assert(m > 0 && n > 0 && values.size() == m * n);
matrix = new double[m * n];
width = n;
height = m;
for (int i = 0; i < m * n; ++i) {
matrix[i] = values[i];
}
}
double Matrix::get_element(int row_num, int col_num) {
assert(check_valid(row_num, col_num, get_width(), get_height()));
return matrix[col_num + get_width() * row_num];
}
void Matrix::set_element(int row_num, int col_num, double el) {
assert(check_valid(row_num, col_num, get_width(), get_height()));
matrix[col_num + row_num * get_width()] = el;
}
double* Matrix::get_cell_ref(int row_num, int col_num) {
int idx = col_num + get_width() * row_num;
return &matrix[idx];
}
The function that supposedly has a data race:
Matrix* scalar_multiply_parallel(Matrix* a, double mul, int threadN) {
auto* b = new Matrix(a->get_height(), a->get_width());
ctpl::thread_pool thr_pool(threadN);
std::vector<std::future<void>> futures(a->get_height() * a->get_width());
for (int i =0; i < a->get_height(); i++) {
for (int j =0; j < a->get_width(); j++) {
int idx = j + a->get_width() * i;
auto util = [&a, &b, i, j, mul](int) {
//b->set_element(i, j, a->get_element(i, j) * mul);
double *cell;
cell = b->get_cell_ref(i, j);
*cell = a->get_element(i, j) * mul;
};
futures[idx] = thr_pool.push(util);
}
}
for (auto& f: futures) {
f.get();
}
return b;
}

Cannot write in matrix <vector<vector<double>> object

I created a matrix class by using a vector< vector >
structure. When created, the matrix is filled with zeroes (probably not the best way to do it, I'm planning to change it).
The header of the class goes something like this:
class Matrix{
public:
/*Basic constructor, accepts matrix dimensions
Matrix(int nr, int nc);
/*return element i,j*/
double elem(int i, int j);
/*operator () overloading - same thing as previous method*/
double operator()(int i, int j);
private:
vector<vector<double> > Matrix_;
int nr_, nc_;
};
while the implementation is:
//CONSTRUCTOR
Matrix::Matrix(int nrows, int ncols)
{
nc_ = ncols;
nr_ = nrows;
/*creates rows*/
for (int i = 0; i < nrows; i++)
{
vector<double> row;
Matrix_.push_back(row);
}
/*Fills matrix with zeroes*/
for (int i = 0; i < nr_; i++)
{
for (int j = 0; j < nc_; j++)
{
Matrix_[i].push_back(0);
}
}
}
/*method returning i,j element of the matrix (I overloaded () to do the same)*/
double Matrix::elem(int i, int j)
{
return Matrix_[i][j];
}
/*operator () overloading*/
double Matrix::operator()(int i, int j)
{
return Matrix_[i][j];
}
Finally, in the main program I have:
Matrix m1(rows, cols);
for (int i=0; i<rows; i++)
{
for (int j=0; j<cols; j++)
{
m1(i,j) = i*j;
/*OR, using the other method*/
m1.elem(i,j) = i*j;
}
}
and the problem is that I am always returned the error:
matrix.cpp:55:27: error: lvalue required as left operand of assignment
m1.elem(i,j) = i*j;
no matter if I am using the method .elem() or the operator ().
So, I guess the problem is that I am not accessing the elements the proper way to change their values, but I don't understand why.
Any suggestion would be greatly appreciated, thanks!
In order to be able to modify a matrix element you need to return a reference to it:
double& Matrix::elem(int i, int j) {
return Matrix_[i][j];
}
and:
double& Matrix::operator()(int i, int j) {
return Matrix_[i][j];
}
You can also add these for const matrices:
double Matrix::elem(int i, int j) const {
return Matrix_[i][j];
}
and:
double Matrix::operator()(int i, int j) const {
return Matrix_[i][j];
}

How to call a dynamic matrix into a function?

I have declared a matrix this way:
double **MB;
MB = new double *[650000];
for (int count = 0; count < 650000; count++)
MB[count] = new double [2];
Now I want to call my matrix in a function that should modify it.
bool notindic (..., double MB [][2], ...) {}
and in the main:
notindic(..., MB, ...)
Now it gives me this error: *[Error] cannot convert 'double**' to 'double ()[2]' for argument '3' to 'bool notindic(std::string, std::string, double ()[2], int, int)'
How can I fix it?
Thank you.
Just pass array pointer as parameter
#include <iostream>
const int NX = 65;
const int NY = 2;
bool notindic(double** MB) {
for(int i = 0; i < NX; ++i) {
for(int j = 0; j < NY; ++j) {
MB[i][j] = i + j;
}
}
}
int main() {
double **MB = new double *[650000];
for (int count = 0; count < 650000; count++) {
MB[count] = new double [2];
}
notindic(MB);
for(int i = 0; i < NX; ++i) {
for(int j = 0; j < NY; ++j) {
std::cout << MB[i][j] << " ";
}
std::cout << std::endl;
}
}
Forget about all that pointer nonsense. It's error-prone, exception-unsafe, hard to write, hard to read, hard to maintain and may perform poorly. Represent your matrix as std::vector<double> and calculate the offsets accordingly.
bool notindic (std::vector<double> const& matrix, int m) {
// ...
auto const element = matrix[y * m + x];
// ...
}
auto const m = 650000;
auto const n = 2;
std::vector<double> my_matrix(m * n);
auto const result = notindic(my_matrix, m);
While you're at it, wrap it in a class like this:
template <class T>
class Matrix final
{
public:
Matrix(int m, int n) :
data(m * n),
m(m)
{
}
T& operator()(int x, int y)
{
return data[y * m + x];
}
T const& operator()(int x, int y) const
{
return data[y * m + x];
}
private:
std::vector<T> data;
int m;
};
bool notindic (Matrix<double> const& matrix) {
// ...
auto const element = matrix(x, y);
// ...
}
auto const m = 650000;
auto const n = 2;
Matrix<double> my_matrix(m, n);
auto const result = notindic(my_matrix);
Add additional member functions if you need them.

C++ 4d array memory deallocation is slow

My code has a 4D matrix in it for some math problem solving
int**** Sads = new int***[inputImage->HeightLines];
for (size_t i = 0; i < inputImage->HeightLines; i++)
{
Sads[i] = new int**[inputImage->WidthColumns];
for (size_t j = 0; j < inputImage->WidthColumns; j++)
{
Sads[i][j] = new int*[W_SIZE];
for (size_t k = 0; k < W_SIZE; k++)
{
Sads[i][j][k] = new int[W_SIZE];
}
}
}
//do something with Sads...
for (int i = 0; i < inputImage->HeightLines; i++)
{
int*** tempI = Sads[i];
for (int j = 0; j < inputImage->WidthColumns; j++)
{
int** tempJ = tempI[j];
for (int k = 0; k < W_SIZE; k++)
{
delete[] tempJ[k];
}
delete[] Sads[i][j];
}
delete[] Sads[i];
}
delete[] Sads;
The sizes are very large WidthColumns = 2018, HeightLines = 1332, W_SIZE =7, the memory allocation is very fast but the memory deallocation (delete) is very slow.
Is there a way to optimize it?
I tired openMP but it throws unrelated errors of missing DLL which are there... if I removed the #pragma omp parallel for everything works fine. but slow...
Using a pointer to a pointer to... is a bad idea because it will fragment your data a lot.
I would create a class ta manage the indices transform and use 1D array, it's a bit more complicated but it will be faster.
Anyway, a trick: nothing prevent you to build your int**** with pointers to a zone in memory that isn't sparse (1D array you preallocated) and then use it as a 4D array.
I'd probably be inclined to use a std::vector. Now memory allocation is taken care of for me (in one allocation/deallocation) and I get free copy/move semantics.
All I have to do is provide the offset calculations:
#include <vector>
#include <cstddef>
struct vector4
{
vector4(std::size_t lines, std::size_t columns)
: lines_(lines), columns_(columns)
, storage_(totalSize())
{}
auto totalSize() const -> std::size_t
{
return lines_ * columns_ * w_size * w_size;
}
int* at(std::size_t a)
{
return storage_.data() + (a * columns_ * w_size * w_size);
}
int* at(std::size_t a, std::size_t b)
{
return at(a) + (b * w_size * w_size);
}
int* at(std::size_t a, std::size_t b, std::size_t c)
{
return at(a, b) + (c * w_size);
}
int& at(std::size_t a, std::size_t b, std::size_t c, std::size_t d)
{
return *(at(a, b, c) + d);
}
private:
std::size_t lines_, columns_;
static constexpr std::size_t w_size = 32; // ?
std::vector<int> storage_;
};
int main()
{
auto v = vector4(20, 20);
v.at(3, 2, 5, 1) = 6;
// other things
// now let it go out of scope
}
The correct way to create, use, and delete a 4D array is this, using the closure of the statement group to delete the automatic variables.
{
const int H = 10;
const int I = 10;
const int J = 10;
const int K = 10;
int h = 0;
int i = 0;
int j = 0;
int k = 0;
int fourDimArray [H][I][J][K];
fourDimArray[h][i][j][k] = 0;
}
If you have a need to dynamically allocate, then use either STL's list or vector class or use something like this with perhaps inline methods to calculate the index of the 1D array from the 4D array indices if you need blazing speed.
int * fourDimArrayAsOneDim = new int[H*I*J*K];
fourDimArrayAsOneDim[indexFromIndices(h, i, j, k)] = 0;
delete [] fourDimArrayAsOneDim;

How to avoid returning pointers in a class

Assume I have a class A that has say 3 methods. So the first methods assigns some values to the first array and the rest of the methods in order modify what is computed by the previous method. Since I wanted to avoid designing the methods that return an array (pointer to local variable) I picked 3 data member and store the intermediate result in each of them. Please note that this simple code is used for illustration.
class A
{
public: // for now how the class members should be accessed isn't important
int * a, *b, *c;
A(int size)
{
a = new int [size];
b = new int [size];
c = new int [size];
}
void func_a()
{
int j = 1;
for int(i = 0; i < size; i++)
a[i] = j++; // assign different values
}
void func_b()
{
int k = 6;
for (int i = 0; i < size; i++)
b[i] = a[i] * (k++);
}
void func_c()
{
int p = 6;
for int (i = 0; i < size; i++)
c[i] = b[i] * (p++);
}
};
Clearly, if I have more methods I have to have more data members.
** I'd like to know how I can re-design the class (having methods that return some values and) at the same time, the class does not have the any of two issues (returning pointers and have many data member to store the intermediate values)
There are two possibilities. If you want each function to return a new array of values, you can write the following:
std::vector<int> func_a(std::vector<int> vec){
int j = 1;
for (auto& e : vec) {
e = j++;
}
return vec;
}
std::vector<int> func_b(std::vector<int> vec){
int j = 6;
for (auto& e : vec) {
e *= j++;
}
return vec;
}
std::vector<int> func_c(std::vector<int> vec){
//same as func_b
}
int main() {
std::vector<int> vec(10);
auto a=func_a(vec);
auto b=func_b(a);
auto c=func_c(b);
//or in one line
auto r = func_c(func_b(func_a(std::vector<int>(10))));
}
Or you can apply each function to the same vector:
void apply_func_a(std::vector<int>& vec){
int j = 1;
for (auto& e : vec) {
e = j++;
}
}
void apply_func_b(std::vector<int>& vec){
int j = 6;
for (auto& e : vec) {
e *= j++;
}
}
void apply_func_c(std::vector<int>& vec){
// same as apply_func_b
}
int main() {
std::vector<int> vec(10);
apply_func_a(vec);
apply_func_b(vec);
apply_func_c(vec);
}
I'm not a big fan of the third version (passing the input parameter as the output):
std::vector<int>& func_a(std::vector<int>& vec)
Most importantly, try to avoid C-style arrays and use std::vector or std::array, and don't use new, but std::make_unique and std::make_shared
I'm assuming you want to be able to modify a single array with no class-level attributes and without returning any pointers. Your above code can be modified to be a single function, but I've kept it as 3 to more closely match your code.
void func_a(int[] arr, int size){
for(int i = 0; i < size; i++)
arr[i] = i+1;
}
void func_b(int[] arr, int size){
int k = 6;
for(int i = 0; i < size; i++)
arr[i] *= (k+i);
}
//this function is exactly like func_b so it is really unnecessary
void func_c(int[] arr, int size){
int p = 6;
for(int i = 0; i < size; i++)
arr[i] *= (p+i);
}
But if you just want a single function:
void func(int[] arr, int size){
int j = 6;
for(int i = 0; i < size; i++)
arr[i] = (i+1) * (j+i) * (j+i);
}
This solution in other answers is better, if you are going to allocate memory then do it like this (and test it!) also if you are not using the default constructor and copy constructor then hide them, this will prevent calling them by accident
class A{
private:
A(const &A){}
A() {}//either define these or hide them as private
public:
int * a, *b, *c;
int size;
A(int sz) {
size = sz;
a = new int[size];
b = new int[size];
c = new int[size];
}
~A()
{
delete[]a;
delete[]b;
delete[]c;
}
//...
};