[lammps-users] MPI_Allreduce in library.cpp

Hello,

I am writing some very basic functions additional to those in
library.cpp. This one returns an array of the atom types. I want to
check my use of MPI_Allreduce.

void lammps_get_types(void *ptr, int *types)
{
  LAMMPS *lmp = (LAMMPS *) ptr;
  int natoms = static_cast<int> (lmp->atom->natoms);
  int *copy = new int[natoms];

  int *x = lmp->atom->type;
  int *tag = lmp->atom->tag;
  int nlocal = lmp->atom->nlocal;

  int id;
  for (int i = 0; i < nlocal; i++) {
    id = tag[i]-1;
    copy[id] = x[i];
  }

  MPI_Allreduce(copy,types,natoms,MPI_INT,MPI_SUM,lmp->world);
  delete [] copy;
}

I have never played with MPI before, and reading the synopsis:
int MPI_Allreduce ( void *sendbuf, void *recvbuf, int count,
                   MPI_Datatype datatype, MPI_Op op, MPI_Comm comm )

I don't quite understand the final 3 arguments, although I made an
educated guess as can be seen above.
Any hints as how to handle this now and in the future?
Thanks
Andy

Andy, I was similarly confused by such issues when I started playing with MPI. What I would recommend is:

  1. Find LAMMPS routines similar to the one you want to write - you’ve already done this - with a bit more work you can find out that MPI_Datatype datatype is usually something like MPI_INT (if you are sending an integer or integer array), MPI_Comm comm is usually “world” in LAMMPS, etc.

  2. Trial and error (and error, and error). I never found a good MPI tutorial suitable for beginners, though it wasn’t for lack of trying.

Best,
Rob

Andy, I was similarly confused by such issues when I started playing
with MPI. What I would recommend is:

1) Find LAMMPS routines similar to the one you want to write - you've
already done this - with a bit more work you can find out that
MPI_Datatype datatype is usually something like MPI_INT (if you are
sending an integer or integer array), MPI_Comm comm is usually "world"
in LAMMPS, etc.

2) Trial and error (and error, and error). I never found a good MPI
tutorial suitable for beginners, though it wasn't for lack of trying.

FWIW, there are *very* good self-study tutorials for MPI
(and related issues) in the CI-Tutor webpage at NCSA.

http://ci-tutor.ncsa.illinois.edu/

cheers,
   axel.

Thanks for the previous assistance, I now have a better grip on MPI
and have enrolled on the course on the web link you sent.

I have advanced a bit with the MPI, in particular using mpi4py, since
I am using lammps in python. I wrote a post on it just now.

I do have a small question concerning interfacing with lammps.
Firstly, in the function lammps_extract() in the file library.cpp, how
do you make sense of the name field? For example if I want to access
the automatically generated compute "thermo_pe" , what should I enter
as the name? Generally, how do you work out what the name should be
for computes?

Thanks
Andy-from-Tokyo-Ballard

The doc on lammps_extract:

/* ----------------------------------------------------------------------
   extract a pointer to an internal LAMMPS value or data structure
   category: 0 = general, 1 = atom, 2 = fix, 3 = compute
   id = ID of fix or compute (else not used, just pass NULL)
   name = desired quantity, e.g. x or dt
   returns a void pointer which the caller can cast to the desired data type
   returns a NULL if LAMMPS does not recognize the name
------------------------------------------------------------------------- */

ID is the fix (or compute ID), e.g. thermo_pe. Name is
the variable, e.g. scalar, that the compute must recognize as
something it will return to you.

Steve