File: many2one.cpp

package info (click to toggle)
lammps 0~20161109.git9806da6-7
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 248,460 kB
  • ctags: 80,635
  • sloc: cpp: 701,185; python: 33,420; fortran: 26,434; ansic: 11,340; sh: 6,108; perl: 4,104; makefile: 2,891; xml: 2,590; f90: 1,690; objc: 238; lisp: 169; tcl: 61; csh: 16; awk: 14
file content (103 lines) | stat: -rw-r--r-- 2,285 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "mpi.h"
#include "stdio.h"
#include "stdlib.h"
#include "many2one.h"
#include "memory.h"

/* ---------------------------------------------------------------------- */

Many2One::Many2One(MPI_Comm caller_comm)
{
  comm = caller_comm;
  MPI_Comm_rank(comm,&me);
  MPI_Comm_size(comm,&nprocs);

  memory = new Memory(comm);

  if (me == 0) {
    counts = new int[nprocs];
    multicounts = new int[nprocs];
    displs = new int[nprocs];
    multidispls = new int[nprocs];
  } else counts = multicounts = displs = multidispls = NULL;

  idall = NULL;
}

/* ---------------------------------------------------------------------- */

Many2One::~Many2One()
{
  delete memory;

  delete [] counts;
  delete [] multicounts;
  delete [] displs;
  delete [] multidispls;

  memory->sfree(idall);
}

/* ---------------------------------------------------------------------- */

void Many2One::setup(int nsrc_in, int *id, int ndest)
{
  nsrc = nsrc_in;
  MPI_Allreduce(&nsrc,&nall,1,MPI_INT,MPI_SUM,comm);
  MPI_Gather(&nsrc,1,MPI_INT,counts,1,MPI_INT,0,comm);

  if (me == 0) {
    displs[0] = 0;
    for (int i = 1; i < nprocs; i++)
      displs[i] = displs[i-1] + counts[i-1];
  }

  // gather IDs into idall

  idall = NULL;
  if (me == 0)
    idall = (int *) memory->smalloc(nall*sizeof(int),"many2one:idall");
  MPI_Gatherv(id,nsrc,MPI_INT,idall,counts,displs,MPI_INT,0,comm);
}

/* ---------------------------------------------------------------------- */

void Many2One::gather(double *src, int n, double *dest)
{
  int i,j,ii,jj,m;

  if (me == 0)
    for (int i = 0; i < nprocs; i++) {
      multicounts[i] = n*counts[i];
      multidispls[i] = n*displs[i];
    }

  // allgather src into desttmp

  double *desttmp = NULL;
  if (me == 0)
    desttmp = (double *) memory->smalloc(n*nall*sizeof(double),
					 "many2one:idsttmp");
  MPI_Gatherv(src,n*nsrc,MPI_DOUBLE,desttmp,multicounts,multidispls,
	      MPI_DOUBLE,0,comm);

  // use idall to move datums from desttmp to dest

  if (me == 0) {
    if (n == 1)
      for (i = 0; i < nall; i++) {
	j = idall[i] - 1;
	dest[j] = desttmp[i];
      }
    else
      for (i = 0; i < nall; i++) {
	j = idall[i] - 1;
	ii = n*i;
	jj = n*j;
	for (m = 0; m < n; m++)
	  dest[jj++] = desttmp[ii++];
      }
  }

  memory->sfree(desttmp);
}