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
|
#include <mpi.h>
#include <stdlib.h>
#include "one2many.h"
#include "memory.h"
#include <map>
/* ---------------------------------------------------------------------- */
One2Many::One2Many(MPI_Comm caller_comm)
{
comm = caller_comm;
MPI_Comm_rank(comm,&me);
MPI_Comm_size(comm,&nprocs);
memory = new Memory(comm);
hash = new std::map<int,int>();
}
/* ---------------------------------------------------------------------- */
One2Many::~One2Many()
{
delete memory;
delete hash;
}
/* ---------------------------------------------------------------------- */
void One2Many::setup(int nsrc_in, int ndest, int *id)
{
nsrc = nsrc_in;
// store my local IDs in hash
hash->clear();
for (int i = 0; i < ndest; i++)
hash->insert(std::pair<int,int> (id[i],i));
}
/* ---------------------------------------------------------------------- */
void One2Many::scatter(double *src, int n, double *dest)
{
int i,j,k,m;
// allocate src on procs that don't have it
int flag = 0;
if (src == NULL) {
src = (double *) memory->smalloc(n*nsrc*sizeof(double),"one2many:src");
flag = 1;
}
// broadcast src from 0 to other procs
MPI_Bcast(src,n*nsrc,MPI_DOUBLE,0,comm);
// each proc loops over entire src
// if I own the global ID, copy src values into dest
std::map<int,int>::iterator loc;
for (m = 1; m <= nsrc; m++) {
loc = hash->find(m);
if (loc == hash->end()) continue;
i = n*loc->second;
j = 3*(m-1);
if (n == 1) dest[i] = src[j];
else
for (k = 0; k < n; k++)
dest[i++] = src[j++];
}
// free locally allocated src
if (flag) memory->sfree(src);
}
|