-
Notifications
You must be signed in to change notification settings - Fork 0
/
poisson_demo.cc
156 lines (131 loc) · 3.35 KB
/
poisson_demo.cc
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
@copyright Russell Standish 2000-2013
@author Russell Standish
This file is part of Classdesc
Open source licensed under the MIT license. See LICENSE for details.
*/
/* This is a simple demo of graphcode. */
extern void initr(unsigned long seed);
extern unsigned long ibase(void);
extern double drand(void);
// we need to include mpi.h before iostream
#ifdef MPI_SUPPORT
#include <mpi.h>
#endif
#include <math.h>
#include <iostream>
#define MAP vmap
#include "graphcode.h"
#include "graphcode.cd"
using namespace GRAPHCODE_NS;
using namespace std;
#include "poisson_demo.h"
#include "poisson_demo.cd"
#include <classdesc_epilogue.h>
/* coordinate to ID function */
struct makeID_t
{
int size;
makeID_t(int s): size(s) {}
GraphID_t operator()(int x, int y)
{ return Wrap(x,size) + size*Wrap(y,size);}
};
void print(objref& x)
{
std::cout << x.ID << " is connected to ";
for (Ptrlist::iterator p=x->begin(); p!=x->end(); p++)
std::cout << p->ID << " ";
std::cout << std::endl;
};
class von: public Graph
{
public:
void setup(int size);
void update();
void print();
};
void von::setup(int size)
{
unsigned xprocs=(unsigned)sqrt(double(nprocs()));
unsigned yprocs=nprocs()/xprocs;
int i, j;
makeID_t makeID(size);
objects[makeID(size-1,size-1)]; //optimised for vmaps
for(j=0; j<size; j++)
for(i=0; i<size; i++)
{
objref& o=objects[makeID(i,j)];
o.proc=(i*xprocs) / size + (j*yprocs)/size*xprocs;
if (o.proc==myid())
{
AddObject(cell(),o.ID);
o->push_back(objects[makeID(i-1,j)]);
o->push_back(objects[makeID(i+1,j)]);
o->push_back(objects[makeID(i,j-1)]);
o->push_back(objects[makeID(i,j+1)]);
}
}
rebuild_local_list();
Partition_Objects();
}
void cell::update(const cell& from)
{
double sum_nbr=0;
for (Ptrlist::iterator n=from.begin(); n!=from.end(); n++)
{
cell& nbr=dynamic_cast<cell&>(**n);
sum_nbr += nbr.my_value;
}
my_value = from.my_value + 0.1*(sum_nbr - from.size()*from.my_value);
}
void von::update()
{
Prepare_Neighbours(true); /* make a copy of neighbouring objects
onto the current thread */
omap back=objects; // backing map
for(iterator i=begin(); i!=end(); i++)
dynamic_cast<cell&>(**i).update( dynamic_cast<cell&>(*back[i->ID]) );
}
double error(Graph& pGraph, unsigned int size)
{
double retval=0.0;
for (Ptrlist::iterator i=pGraph.begin(); i!=pGraph.end(); i++)
retval += fabs(dynamic_cast<cell&>(**i).my_value-0.5);
return retval;
}
inline void swap(von*& x, von*& y) { von *t=x; x=y; y=t;}
int main(int argc, char** argv)
{
#ifdef MPI_SUPPORT
MPISPMD c(argc,argv);
// if (myid()==0) getchar();
// MPI_Barrier(MPI_COMM_WORLD);
#endif
if (argc<3)
{
printf("usage: %s gridsize niter\n",argv[0]);
return 1;
}
const int testsize=atoi(argv[1]);
const int niter=atoi(argv[2]);
/* initialise random number generator */
initr(0xdeadbeef);
for(int junk=0; junk<(1<<16); junk++)
ibase();
von g;
g.setup(testsize);
g.gather();
double starterror=error(g, testsize);
for(int t=0; t<niter; t++)
{
#ifndef SILENT
g.gather();
printf("On node %d: time is: %d, error is: %5.10f, updating...\n",
myid(), t, error(g, testsize));
#endif
g.update();
}
g.gather();
if (myid()==0 && error(g, testsize)/ starterror >0.2) return 1;
return 0;
}