Skip to content

Commit 6a422cc

Browse files
authored
Merge pull request #541 from shkodm/sampler_arb
Implemented sampler on Arbitrary grid and fixes for cartesian one
2 parents 7320b7b + a7f0364 commit 6a422cc

14 files changed

Lines changed: 147 additions & 27 deletions

src/ArbLattice.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ ArbLattice::ArbLattice(size_t num_snaps_, const UnitEnv& units_, const std::map<
2222
}
2323

2424
void ArbLattice::initialize(size_t num_snaps_, const std::map<std::string, int>& setting_zones, pugi::xml_node arb_node) {
25-
const int rank = mpitools::MPI_Rank(comm);
2625
sizes.snaps = num_snaps_;
2726
#ifdef ADJOINT
2827
sizes.snaps += 2; // Adjoint snaps are appended to the total snap allocation
@@ -927,6 +926,36 @@ void ArbLattice::resetAverage(){
927926
}
928927
}
929928

929+
void ArbLattice::getSample(int quant, unsigned int lid, real_t scale, real_t *buf) {
930+
setSnapIn(Snap);
931+
#ifdef ADJOINT
932+
setAdjSnapIn(aSnap);
933+
#endif
934+
launcher.sampleQuantity(quant, lid, buf, scale, data);
935+
}
936+
937+
938+
void ArbLattice::updateAllSamples(){
939+
const int rank = mpitools::MPI_Rank(comm);
940+
if (sample->size != 0) {
941+
for (size_t j = 0; j < sample->spoints.size(); j++) {
942+
if (rank == sample->spoints[j].rank) {
943+
for(const Model::Quantity& q : model->quantities) {
944+
if (sample->quant->in(q.name.c_str())){
945+
double v = sample->units->alt(q.unit.c_str());
946+
getSample(q.id, sample->spoints[j].lid, 1/v,
947+
&sample->gpu_buffer[sample->location[q.name.c_str()]+(data.iter - sample->startIter)*sample->size + sample->totalIter*j*sample->size]);
948+
}
949+
}
950+
}
951+
}
952+
}
953+
}
954+
955+
unsigned int ArbLattice::getCartesianCoordinateLid(vector_t point) const {
956+
return launcher.getCartesianCoordinateLid(point, data);
957+
}
958+
930959
ArbLattice::~ArbLattice()
931960
{
932961
RFI.Close();

src/ArbLattice.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,18 @@ class ArbLattice : public LatticeBase {
9696
virtual void setFlags(const std::vector<big_flag_t>& x) override;
9797
virtual void setField(const Model::Field& f, const std::vector<real_t>& x) override;
9898
virtual void setFieldAdjZero(const Model::Field& f) override;
99+
virtual void updateAllSamples() override;
99100

100101
const ArbVTUGeom& getVTUGeom() const { return vtu_geom; }
101102
Span<const flag_t> getNodeTypes() const { return {node_types_host.data(), node_types_host.size()}; } /// Get host view of node types (permuted)
102103
const ArbLatticeConnectivity& getConnectivity() const { return connect; }
103104
const std::vector<unsigned>& getLocalPermutation() const { return local_permutation; }
104105

106+
unsigned int getCartesianCoordinateLid(vector_t point) const;
107+
void getSample(int quant, unsigned int lid, real_t scale, real_t *buf);
108+
105109
void resetAverage();
106-
lbRegion getLocalBoundingBox() const override; /// Compute local bounding box, assuming the arbitrary lattice is a subset of a Cartesian lattice
110+
lbRegion getLocalBoundingBox() const override; /// Compute local bounding box, assuming the arbitrary lattice is a subset of a Cartesian lattice
107111

108112
protected:
109113
ArbLatticeLauncher launcher; /// Launcher responsible for running CUDA kernels on the lattice

src/ArbLatticeLauncher.h.Rt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ struct ArbLatticeLauncher {
2222

2323
void getQuantity(int quant, real_t* host_tab, real_t scale, const LatticeData& data) const;
2424

25+
unsigned int getCartesianCoordinateLid(const vector_t point, const LatticeData& data) const;
26+
27+
void sampleQuantity(int quant, unsigned int lid, real_t* host_tab, real_t scale, const LatticeData &data) const;
28+
2529
private:
2630
<?R for (q in rows(Quantities)) { ifdef(q$adjoint);
2731
if (q$adjoint) {
@@ -31,6 +35,8 @@ if (q$adjoint) {
3135
}
3236
?>
3337
void getQuantity<?%s q$name ?>(<?%s q$type ?>* tab, real_t scale, const LatticeData& data) const;
38+
39+
void getSample<?%s q$name ?>(<?%s q$type ?>* tab, real_t scale, const LatticeData& data, unsigned int lid) const;
3440
<?R }
3541
ifdef() ?>
3642
};

src/ArbLatticeLauncher.hpp.Rt

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ struct ArbLatticeExecutor : public LinearExecutor {
3434

3535
template <eOperationType I, eCalculateGlobals G, eStage S>
3636
void ArbLatticeLauncher::RunBorder(CudaStream_t stream, const LatticeData& data) const {
37-
const ArbLatticeExecutor<I, G, S> executor{{container.num_border_nodes}, container, data, 0};
37+
const ArbLatticeExecutor<I, G, S> executor{{container.num_border_nodes},
38+
container, data, 0};
3839
LaunchExecutorAsync(executor, stream);
3940
}
4041

@@ -99,6 +100,7 @@ struct GetQuantityArbExecutor<?%s q$name?> : public LinearExecutor {
99100
LatticeData data;
100101
<?%s q$type ?>* buf;
101102
real_t scale;
103+
unsigned int offset; // Starting offset for iteration space
102104

103105
CudaDeviceFunction void Execute() const {
104106
using LA = ArbLatticeAccess; <?R
@@ -109,7 +111,8 @@ if (q$adjoint) { ?>
109111
}?>
110112
const int i = threadID(CudaThread, CudaBlock, CudaNumberOfThreads);
111113
if (inRange(i)) {
112-
LA acc(i, container);
114+
const int index = i + offset;
115+
LA acc(index, container);
113116
N now(acc, data);
114117
acc.pop(now); <?R
115118
if (q$adjoint) { ?>
@@ -129,10 +132,68 @@ if (q$type == "vector_t") {
129132
};
130133

131134
void ArbLatticeLauncher::getQuantity<?%s q$name ?>(<?%s q$type ?>* tab, real_t scale, const LatticeData& data) const {
132-
const GetQuantityArbExecutor<?%s q$name?> executor{{container.num_border_nodes + container.num_interior_nodes}, container, data, tab, scale};
135+
const GetQuantityArbExecutor<?%s q$name?> executor{{container.num_border_nodes + container.num_interior_nodes}, container, data, tab, scale, 0};
136+
LaunchExecutor(executor);
137+
}
138+
139+
void ArbLatticeLauncher::getSample<?%s q$name ?>(<?%s q$type ?>* tab, real_t scale, const LatticeData& data, unsigned int lid) const {
140+
const GetQuantityArbExecutor<?%s q$name?> executor{{1}, container, data, tab, scale, lid};
133141
LaunchExecutor(executor);
134142
}
143+
135144
<?R }
136145
ifdef() ?>
137146

147+
struct CartesianCoordinateMapperArbExecutor: public LinearExecutor {
148+
ArbLatticeContainer container;
149+
LatticeData data;
150+
vector_t point; // cartesian point coordinates
151+
real_t epsilon2; // should be a small value (e.g. < 1e-10) to match with only one point
152+
unsigned int *matched_lid;
153+
154+
CudaDeviceFunction void Execute() const {
155+
using LA = ArbLatticeAccess;
156+
const int i = threadID(CudaThread, CudaBlock, CudaNumberOfThreads);
157+
if (inRange(i)) {
158+
LA acc(i, container);
159+
real_t X = acc.getX();
160+
real_t Y = acc.getY();
161+
real_t Z = acc.getZ();
162+
real_t distance = (X - point.x)*(X - point.x) + (Y - point.y)*(Y - point.y) + (Z - point.z)*(Z - point.z);
163+
if (distance < epsilon2) {
164+
*matched_lid= i;
165+
}
166+
}
167+
}
168+
};
169+
170+
171+
unsigned int ArbLatticeLauncher::getCartesianCoordinateLid(const vector_t point, const LatticeData& data) const {
172+
const auto gpu_lid = cudaMakeUnique<unsigned int>(1);
173+
const real_t tol2 = 1e-10;
174+
const CartesianCoordinateMapperArbExecutor executor{{container.num_border_nodes + container.num_interior_nodes}, container, data, point, tol2,
175+
gpu_lid.get()};
176+
LaunchExecutor(executor);
177+
178+
unsigned int matched_lid;
179+
CudaMemcpy(&matched_lid, gpu_lid.get(), sizeof(unsigned int), CudaMemcpyDeviceToHost);
180+
return matched_lid;
181+
}
182+
183+
void ArbLatticeLauncher::sampleQuantity(int quant, unsigned int lid, real_t* host_tab, real_t scale, const LatticeData &data) const {
184+
switch(quant) { <?R
185+
for (q in rows(Quantities)) { ifdef(q$adjoint);
186+
?>
187+
case <?%s q$Index ?>: {
188+
const auto gpu_tab = cudaMakeUnique<<?%s q$type ?>>(1);
189+
getSample<?%s q$name ?>(gpu_tab.get(), scale, data, lid);
190+
CudaMemcpy(host_tab, gpu_tab.get(), sizeof(<?%s q$type ?>), CudaMemcpyDeviceToHost);
191+
break;
192+
} <?R
193+
}
194+
ifdef();
195+
?>
196+
}
197+
}
198+
138199
#endif // ARBLATTICELAUNCHER_HPP

src/CartLattice.cpp.Rt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ CartLattice::CartLattice(CartConnectivity connect, int ns, const UnitEnv& units_
6363
{
6464
DEBUG_M;
6565
AllocContainer(launcher.container, getLocalRegion().nx, getLocalRegion().ny, getLocalRegion().nz);
66-
sample = std::make_unique<Sampler>(model.get(), units, connectivity.mpi_rank);
6766
Snaps = std::make_unique<FTabs[]>(num_snaps);
6867
setPosition(0.0,0.0,0.0);
6968
DEBUG_M;
@@ -721,8 +720,9 @@ void CartLattice::GetSample<?%s q$name ?>(const lbRegion& over, real_t scale,rea
721720
launcher.container.in = Snaps[Snap];
722721
<?R if (q$adjoint) { ?>
723722
launcher.container.adjin = aSnaps[aSnap]; <?R } ?>
724-
lbRegion small = getLocalRegion().intersect(over);
725-
launcher.SampleQuantity<?%s q$name ?>(small, (<?%s q$type ?>*)buf, scale, data);
723+
lbRegion local_reg = getLocalRegion();
724+
lbRegion small_local = over.shift(local_reg.dx, local_reg.dy, local_reg.dz);
725+
launcher.SampleQuantity<?%s q$name ?>(small_local, (<?%s q$type ?>*)buf, scale, data);
726726
}
727727
<?R } ;ifdef() ?>
728728
void CartLattice::updateAllSamples(){

src/CartLattice.h.Rt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include "LatticeBase.hpp"
99
#include "CartLatticeLauncher.h"
1010
#include "CartConnectivity.hpp"
11-
#include "Sampler.h"
1211
#include "Geometry.h"
1312

1413
#include <memory>
@@ -73,7 +72,6 @@ protected:
7372
#endif
7473

7574
public:
76-
std::unique_ptr<Sampler> sample; //initializing sampler with zero size
7775
real_t px, py, pz;
7876

7977
CartLattice (CartConnectivity connect, int ns, const UnitEnv& units_);
@@ -148,7 +146,7 @@ public:
148146
int getPar(const ParStruct& par_struct, double * wb);
149147
int setPar(const ParStruct& par_struct, double * w);
150148

151-
void updateAllSamples();
149+
virtual void updateAllSamples() override;
152150
void resetAverage();
153151
};
154152

src/CartLatticeLauncher.hpp.Rt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ public:
307307
GetQuantitySampleExecutor<?%s q$name ?>(Args&&... args) : GetQuantityExecutor<?%s q$name ?>Base(std::forward<Args>(args)...) {}
308308

309309
CudaHostFunction LaunchParams ComputeLaunchParams(dim3) const {
310-
return LaunchParams{dim3(small.nx, small.ny), dim3(1)};
310+
return LaunchParams{dim3(small.nx, small.ny, small.nz), dim3(1)};
311311
}
312312
};
313313

src/Handlers/cbSample.cpp

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ int cbSample::Init () {
1616
else {
1717
s.add_from_string("all",',');
1818
}
19-
const auto lattice = solver->getCartLattice();
2019
for (pugi::xml_node par = node.first_child(); par; par = par.next_sibling()) {
2120
if (strcmp(par.name(),"Point") == 0) {
2221
lbRegion loc;
@@ -32,33 +31,44 @@ int cbSample::Init () {
3231
if (attr) {
3332
loc.dz = solver->units.alt(attr.value());
3433
}
35-
loc = lattice->getLocalRegion().intersect(loc);
36-
if (loc.nx == 1) lattice->sample->addPoint(loc, solver->mpi_rank);
34+
loc = solver->lattice->getLocalBoundingBox().intersect(loc);
35+
36+
if (loc.nx == 1) {
37+
unsigned int lid = 0;
38+
auto variant = solver->getLatticeVariant();
39+
if (auto* lattice = std::get_if<Lattice<ArbLattice>*>(&variant)) {
40+
// cache lid for arbitrary lattice
41+
const real_t offset = 0.5;
42+
vector_t point{real_t(loc.dx) + offset, real_t(loc.dy) + offset, real_t(loc.dz) + offset};
43+
lid = (*lattice)->getCartesianCoordinateLid(point);
44+
}
45+
solver->lattice->sample->addPoint(loc, solver->mpi_rank, lid);
46+
}
47+
3748
} else {
3849
error("Uknown element in Sampler\n");
3950
return -1;
4051
}
4152
}
4253
filename = solver->outIterFile(nm, ".csv");
43-
lattice->sample->units = &solver->units;
44-
lattice->sample->mpi_rank = solver->mpi_rank;
45-
lattice->sample->Allocate(&s,startIter,everyIter);
46-
lattice->sample->initCSV(filename.c_str());
54+
solver->lattice->sample->units = &solver->units;
55+
solver->lattice->sample->mpi_rank = solver->mpi_rank;
56+
solver->lattice->sample->Allocate(&s,startIter,everyIter);
57+
solver->lattice->sample->initCSV(filename.c_str());
4758
return 0;
4859
}
4960

5061

5162
int cbSample::DoIt () {
5263
Callback::DoIt();
53-
const auto lattice = solver->getCartLattice();
54-
lattice->sample->writeHistory(solver->iter);
55-
lattice->sample->startIter = solver->iter;
64+
solver->lattice->sample->writeHistory(solver->iter);
65+
solver->lattice->sample->startIter = solver->iter;
5666
return 0;
5767
}
5868

5969

6070
int cbSample::Finish () {
61-
solver->getCartLattice()->sample->Finish();
71+
solver->lattice->sample->Finish();
6272
return Callback::Finish();
6373
}
6474

src/Lattice.hpp.Rt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,7 @@ struct Lattice : public LatticeType {
109109
CudaDeviceSynchronize();
110110
LatticeType::Snap = tab_out;
111111
LatticeType::MarkIteration();
112-
if constexpr(std::is_same_v<LatticeType, CartLattice>) /// TODO
113-
LatticeType::updateAllSamples();
112+
LatticeType::updateAllSamples();
114113
DEBUG_PROF_POP();
115114
}
116115

src/LatticeBase.cpp.Rt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ c_header();
55

66
#include "LatticeBase.hpp"
77
#include "utils.h"
8+
#include "Global.h"
89

910
LatticeBase::LatticeBase(int zonesettings, int zones, int num_snaps_, const UnitEnv& units_)
1011
: model(std::make_unique<Model_m>()), zSet(zonesettings, zones), num_snaps(num_snaps_), units(&units_) {
@@ -24,6 +25,8 @@ LatticeBase::LatticeBase(int zonesettings, int zones, int num_snaps_, const Unit
2425
data.ConstZoneSettings = zSet.gpuConst;
2526
std::fill_n(iSnaps.get(), maxSnaps, -1);
2627

28+
sample = std::make_unique<Sampler>(model.get(), units, D_MPI_RANK);
29+
2730
// Setting settings to default
2831
<?R for (v in rows(Settings)) {
2932
if (is.na(v$derived)) { ?>

0 commit comments

Comments
 (0)