Skip to content

Commit

Permalink
Fix a segfalut error for a huge matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
ttadano committed Jan 7, 2018
1 parent c504950 commit e162e74
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
28 changes: 13 additions & 15 deletions alm/fitting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ void Fitting::fitmain()
<< N_new << std::endl << std::endl;

// memory->allocate(amat, M, N_new);
memory->allocate(amat_1D, N_new * M);
unsigned long NM = static_cast<long>(N_new) * static_cast<long>(M);
memory->allocate(amat_1D, NM);
memory->allocate(fsum, M);
memory->allocate(fsum_orig, M);

calc_matrix_elements_algebraic_constraint(M, N, N_new, nat, natmin, ndata_used,
nmulti, maxorder, u, f, amat_1D, fsum,
fsum_orig);
Expand Down Expand Up @@ -1068,6 +1068,7 @@ void Fitting::calc_matrix_elements(const int M,
++iparam;
}
}

}

memory->deallocate(ind);
Expand All @@ -1092,16 +1093,15 @@ void Fitting::calc_matrix_elements_algebraic_constraint(const int M,
double *bvec,
double *bvec_orig)
{
int i, j;
int irow;
int ncycle;
long i, j;
long irow;
long ncycle;

std::cout << " Calculation of matrix elements for direct fitting started ... ";

ncycle = ndata_fit * nmulti;
int natmin3 = 3 * natmin;


long natmin3 = 3 * static_cast<long>(natmin);

#ifdef _OPENMP
#pragma omp parallel for private(j)
#endif
Expand All @@ -1118,10 +1118,10 @@ void Fitting::calc_matrix_elements_algebraic_constraint(const int M,
#endif
{
int *ind;
int mm, order, iat, k;
int im, idata, iparam;
int ishift;
int iold, inew;
long mm, order, iat, k;
long im, idata, iparam;
long ishift;
long iold, inew;
double amat_tmp;
double **amat_orig;
double **amat_mod;
Expand Down Expand Up @@ -1230,15 +1230,13 @@ void Fitting::calc_matrix_elements_algebraic_constraint(const int M,
amat[natmin3 * ncycle * j + i + idata] = amat_mod[i][j];
}
}

}

memory->deallocate(ind);
memory->deallocate(amat_orig);
memory->deallocate(amat_mod);
}

std::cout << "done!" << std::endl << std::endl;
}


Expand Down
10 changes: 6 additions & 4 deletions alm/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ namespace ALM_NS

// allocator

template <typename T>
T* allocate(T *&arr, const unsigned int n1)
template <typename T, typename A>
T* allocate(T *&arr, const A n1)
{
try {
arr = new T [n1];
Expand Down Expand Up @@ -144,9 +144,11 @@ namespace ALM_NS

// memsize calculator

unsigned long memsize_in_MB(const int size_of_one, const unsigned int n1)

template <typename A>
unsigned long memsize_in_MB(const int size_of_one, const A n1)
{
unsigned long n = n1 * size_of_one;
unsigned long n = static_cast<unsigned long>(n1) * size_of_one;
return n / 1000000;
}

Expand Down

0 comments on commit e162e74

Please sign in to comment.