我有一个并行循环调用函数“interpol”来填充局部变量“Velocity”,例如将“velField”乘以某些coofficient。:
void foo(){
//Shared pointer to the source
VectorField* velField = Source.getPointer();
#pragma omp parallel shared( velField )
{
//private variable Velocity
matrixdouble Velocity(1,3);
#pragma omp for schedule(auto)
for (unsigned i=0;i< NbNodes; ++i)
{
Velocity = interpol(*velField);
}
}
}
和
matrixdouble interpol(VectorField& velField ){
matrixdouble Result(1,3);
for (int i = 0; i < 1; i++)
for (int j = 0; j < 3; j++)
Result(i,j) = velField(i,j) * 0.25;
//here the program stops
return Result;
}
但是当您在“interpol”函数中返回“Result”时,程序总是会失败,并显示“double free or corruption(fasttop)”消息。有20个线程,它给18个相同的地址,它失败了,另外两个也是相同的。
: double free or corruption (fasttop): 0x0000000000ebcb20 ***
: double free or corruption (fasttop): 0x0000000000ebcb20 ***
: double free or corruption (fasttop): 0x0000000000ebca40 ***
: double free or corruption (fasttop): 0x0000000000ebca40 ***
: double free or corruption (fasttop): 0x0000000000ebca40 ***
: double free or corruption (fasttop): 0x0000000000ebca40 ***
: double free or corruption (fasttop): 0x0000000000ebca40 ***
: double free or corruption (fasttop): 0x0000000000ebca40 ***
...
所以我的问题是:函数“interpol”中的变量“Result”是否共享?
template<typename T> class Matrix
{
public:
// Constructors & Destructors.
inline Matrix();
inline Matrix(const Matrix<T> & original);
inline Matrix(const unsigned rows, const unsigned cols);
inline Matrix(const unsigned rows, const unsigned cols, const T val);
inline Matrix(const unsigned rows, const unsigned cols, T * pdata);
inline ~Matrix();
// Resize and reshape functions
inline void resize(const unsigned rows, const unsigned cols);
inline void reshape(const unsigned rows, const unsigned cols, const T & pad = 0);
void fill(const T val);
inline Matrix<T> & reference(const Matrix<T> & original);
inline Matrix<T> & operator=(const Matrix<T> & original);
Matrix<T> & operator+=(const Matrix<T> & m2);
Matrix<T> & operator-=(const Matrix<T> & m2);
Matrix<T> & operator*=(const double d);
inline Matrix<T> copy() const throw (std::runtime_error);
void apply(T (*f)(T));
Matrix<T> applyCopy(T (*f)(T)) const;
inline unsigned getRows() const;
inline unsigned getCols() const;
inline unsigned getSize() const;
inline T * getData() const;
inline void setName(std::string name);
inline std::string getName() const;
inline const T & operator()(const unsigned row, const unsigned col) const;
inline T & operator()(const unsigned row, const unsigned col);
inline int operator==(const Matrix<T> & right) const;
inline void invert() throw ();
inline void addColToCol(const unsigned col1, const Matrix<T> & m2, const unsigned col2);
inline void addVecToCol(const unsigned col1, const Vector<T> & v2);
inline void addVecToRow(const unsigned col1, const Vector<T> & v2);
inline void transpose();
Matrix<T> transposeCopy();
Vector<T> toVector() const throw (std::logic_error);
Vector<T> colToVector(const unsigned col) const;
Vector<T> rowToVector(const unsigned row) const;
void vectorToCol(const unsigned col, const Vector<T> & v);
void vectorToRow(const unsigned row, const Vector<T> & v);
inline void times(const Matrix<T> & m, const Vector<T> & v);
void AXPY(T alpha, const Matrix<T> & X) throw ();
void AXPY_ColToCol(const unsigned col1, const Matrix<T> & X, const unsigned col2,
T alpha) throw ();
void GEMM(T alpha, const Matrix<T> & A, const Matrix<T> & B, T beta) throw ();
void GEMM(T alpha, const DiagMatrix<T> & D, const Matrix<T> & B, T beta) throw ();
void SYMM(T alpha, const Matrix<T> & A, const Matrix<T> & B, T beta) throw ();
Vector<std::complex<T> > EigenValues() throw ();
Matrix<T> subMatrix(const unsigned row1, const unsigned row2, const unsigned col1,
const unsigned col2) const throw ();
private:
ReferenceMatrix<T> * data_;
};
和一个私有变量
template<typename T> class ReferenceMatrix
{
public:
ReferenceMatrix();
ReferenceMatrix(const unsigned rows, const unsigned cols);
ReferenceMatrix(const unsigned rows, const unsigned cols, T * pdata);
~ReferenceMatrix();
inline void resize(const unsigned rows, const unsigned cols);
inline void reshape(const unsigned rows, const unsigned cols, const T & pad);
inline ReferenceMatrix<T> * reference();
inline void dereference();
inline unsigned getRows() const;
inline unsigned getCols() const;
inline unsigned getSize() const;
inline T * getData() const;
inline void setName(std::string name);
inline std::string getName() const;
inline const T & operator()(const unsigned & row, const unsigned & col) const;
inline T & operator()(const unsigned & row, const unsigned & col);
void transpose();
private:
unsigned rows_, cols_, size_;
unsigned reference_count_;
bool user_mem_management_; // deallocation done by user
T * data_;
std::string name_;
};
参考矩阵的构造函数如下,这意味着它是动态分配的
template<typename T> ReferenceMatrix<T>::ReferenceMatrix(const unsigned rows,
const unsigned cols)
{
// Using namespace & using directives.
using namespace std;
// Create a new (rows by cols) Matrix.
if (((rows_ = rows) > 0) & ((cols_ = cols) > 0))
{
size_ = rows_ * cols_;
user_mem_management_ = false;
data_ = new T[size_];
reference_count_ = 1;
}
else
{
size_ = 0;
user_mem_management_ = false;
data_ = 0;
reference_count_ = 1;
}
name_ = "";
}