-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpoisson_demo.cc
More file actions
168 lines (140 loc) · 3.47 KB
/
poisson_demo.cc
File metadata and controls
168 lines (140 loc) · 3.47 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
/*
@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;
using namespace std;
#include "poisson_demo.h"
#include "poisson_demo.cd"
#include <classdesc_epilogue.h>
/* coordinate to ID function */
struct MakeId
{
int size;
MakeId(int s): size(s) {}
GraphId 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 (auto& j: *x)
std::cout << j.id() << " ";
std::cout << std::endl;
};
class Von: public Graph<Cell>
{
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 makeId(size);
for(j=0; j<size; j++)
for(i=0; i<size; i++)
{
ObjRef o=insertObject<>(makeId(i,j));
o.proc((i*xprocs) / size + (j*yprocs)/size*xprocs);
o->neighbours.push_back(makeId(i-1,j));
o->neighbours.push_back(makeId(i+1,j));
o->neighbours.push_back(makeId(i,j-1));
o->neighbours.push_back(makeId(i,j+1));
}
rebuildPtrLists();
}
void Cell::update(const Cell& from)
{
double sumNbr=0;
for (auto& n: from)
{
auto& nbr=*n->as<Cell>();
sumNbr += nbr.myValue;
}
myValue = from.myValue + 0.1*(sumNbr - from.size()*from.myValue);
}
void Von::update()
{
prepareNeighbours(true); /* make a copy of neighbouring objects
onto the current thread */
Graph from;
from.objects=objects.deepCopy();
from.rebuildPtrLists();
for(auto& i: *this)
i->as<Cell>()->update( *from.objects[i.id()]);
}
double localError(Graph<Cell>& pGraph, unsigned int size)
{
double retval=0.0;
for (auto& i: pGraph)
retval += fabs(i->as<Cell>()->myValue-0.5);
return retval;
}
double error(Graph<Cell>& pGraph, unsigned int size)
{
double retval=0.0;
for (auto& i: pGraph.objects)
retval += fabs(i->myValue-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);
#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);
// In this case, objects are created insitu, so neither of the
// following methods are needed. They are included just to exercise
// them for unit test purposes
g.partitionObjects();
g.distributeObjects();
g.gather();
double startError=error(g, testSize);
for(int t=0; t<nIter; t++)
{
#ifndef SILENT
printf("On node %d: time is: %d, error is: %5.10f, updating...\n",
myid(), t, localError(g, testSize));
#endif
g.update();
}
g.gather();
if (myid()==0)
{
cout << "Total error="<<error(g, testSize)<<endl;
if (error(g, testSize)/ startError >0.2) return 1;
}
return 0;
}