سؤال

What is the rule of thumb for using PetscMalloc2 (PetscMallocX) instead of PetscMalloc twice (X times)? Should the chunks have similar sizes, or is it always more efficient to allocate them together / at the same time? The manual says "Allocates 2 (X) chunks of memory both aligned to PETSC_MEMALIGN" for the definition, but it doesn't mean much to me as a newbie for PETSc and fine HPC issues. I had always assumed that compilers took care of such issues.

هل كانت مفيدة؟

المحلول

See http://www.mcs.anl.gov/petsc/petsc-current/include/petscsys.h.html#PetscMalloc2 , ligne 566 :

#if defined(PETSC_USE_DEBUG)
#define PetscMalloc2(m1,t1,r1,m2,t2,r2) (PetscMalloc((m1)*sizeof(t1),r1) || PetscMalloc((m2)*sizeof(t2),r2))
#else
#define PetscMalloc2(m1,t1,r1,m2,t2,r2) ((*(r2) = 0,PetscMalloc((m1)*sizeof(t1) (m2)*sizeof(t2)+(PETSC_MEMALIGN-1),r1)) || (*(r2) = (t2*)PetscAddrAlign(*(r1)+m1),0))
#endif

If you are in debug mode, PetscMalloc2 is equivalent to two PetscMalloc.

Otherwise PetscMalloc2 ensures that both buffers are one after another in memory, with a little space due to memory alignement. http://en.wikipedia.org/wiki/Data_structure_alignment Allocation through PetscMalloc be called only once, which would be better if you call this function a large number of times in your code. If you call it once in a while, it won't change much !

Bye,

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top