Skip to content

Commit d0f590d

Browse files
authored
Fix GPU buffer freeing (#180)
* Bump dependencies, update transmission benchmark * Bump ViennaRay * Free coverage buffer * Remove ViennaRay dev branch
1 parent 9139226 commit d0f590d

11 files changed

Lines changed: 141 additions & 61 deletions

File tree

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ include("cmake/cpm.cmake")
101101

102102
CPMAddPackage(
103103
NAME ViennaCore
104-
VERSION 1.6.3
104+
VERSION 1.7.0
105105
GIT_REPOSITORY "https://github.com/ViennaTools/ViennaCore"
106106
EXCLUDE_FROM_ALL ${VIENNAPS_BUILD_PYTHON}
107107
OPTIONS "VIENNACORE_USE_GPU ${VIENNAPS_USE_GPU}")
@@ -113,7 +113,7 @@ CPMAddPackage(
113113

114114
CPMFindPackage(
115115
NAME ViennaRay
116-
VERSION 3.8.0
116+
VERSION 3.8.2
117117
GIT_REPOSITORY "https://github.com/ViennaTools/ViennaRay"
118118
EXCLUDE_FROM_ALL ${VIENNAPS_BUILD_PYTHON}
119119
OPTIONS "VIENNARAY_USE_GPU ${VIENNAPS_USE_GPU}")

gpu/benchmark/transmission.cpp

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@
55
#include <raygTraceDisk.hpp>
66
#include <raygTraceTriangle.hpp>
77

8-
// #define useDiskMesh
8+
#define USE_DISK_MESH
9+
#ifdef USE_DISK_MESH
10+
#define FILE_POST "_disk"
11+
#else
12+
#define FILE_POST "_tri"
13+
#endif
914

1015
constexpr int D = 3;
1116
using NumericType = float;
12-
constexpr bool useGPU = true;
17+
constexpr bool useGPU = false;
1318

1419
template <class T> auto createCylinderLS(T gridDelta, T radius, T length) {
1520
double bounds[2 * D] = {-1.0, 1.0, -1.0, 1.0, 0.0, length + gridDelta};
@@ -104,12 +109,12 @@ auto createCylinderDiskMesh(NumericType gridDelta, NumericType radius,
104109
lsMesh->insertNextNode(Vec3Df{0.0f, 0.0f, 0.0f});
105110
materialIds.push_back(1);
106111
normals->push_back(Vec3Df{0.0f, 0.0f, 1.0f});
107-
radii.push_back(radius);
112+
radii.push_back(radius * 1.5f); // make sure rays hit the plane
108113

109114
lsMesh->insertNextNode(Vec3Df{0.0f, 0.0f, float(length)});
110115
materialIds.push_back(2);
111116
normals->push_back(Vec3Df{0.0f, 0.0f, -1.0f});
112-
radii.push_back(radius);
117+
radii.push_back(radius * 1.5f); // make sure rays hit the plane
113118

114119
// viennals::VTKWriter<float>(lsMesh, "cylinder.vtp").apply();
115120

@@ -209,16 +214,19 @@ std::array<NumericType, N> logSpace(NumericType start, NumericType end) {
209214
}
210215

211216
int main() {
212-
auto lengths = logSpace<20>(.01, 10000.0);
217+
auto lengths = logSpace<25>(.01, 10000.0);
213218
// std::array<NumericType, 1> lengths = {100.0};
214219
constexpr NumericType radius = 10.0;
215220
constexpr NumericType gridDelta = .5;
216221
constexpr int nRays = int(1e7);
217222
std::vector<unsigned> counts(4, 0);
218223

219224
if constexpr (useGPU) {
220-
viennacore::CudaBuffer customDataBuffer;
221-
customDataBuffer.allocUpload(counts);
225+
#ifdef USE_DISK_MESH
226+
viennacore::Logger::getInstance()
227+
.addError("No GPU disk mesh implementation")
228+
.print();
229+
#endif
222230

223231
auto context = viennacore::DeviceContext::createContext();
224232
viennaray::gpu::TraceTriangle<NumericType, D> tracer(context);
@@ -245,7 +253,10 @@ int main() {
245253
tracer.insertNextParticle(particle);
246254
tracer.prepareParticlePrograms();
247255

248-
std::ofstream logFile("transmission_log_tri_GPU.txt");
256+
std::string fileName = "transmission_log_GPU";
257+
fileName.append(FILE_POST);
258+
fileName.append(".txt");
259+
std::ofstream logFile(fileName);
249260
logFile << "LR\tTransmission\tReflection\tHitsToTransmission\tHitsToReflect"
250261
"ion\n";
251262

@@ -281,10 +292,9 @@ int main() {
281292
}
282293

283294
logFile.close();
284-
customDataBuffer.free();
285295
} else {
286296

287-
#ifdef useDiskMesh
297+
#ifdef USE_DISK_MESH
288298
viennaray::TraceDisk<NumericType, D> tracer;
289299
#else
290300
viennaray::TraceTriangle<NumericType, D> tracer;
@@ -302,14 +312,17 @@ int main() {
302312
auto particle = std::make_unique<TransmissionParticle>();
303313
tracer.setParticleType(particle);
304314

305-
std::ofstream logFile("transmission_log_disk_CPU.txt");
315+
std::string fileName = "transmission_log_CPU";
316+
fileName.append(FILE_POST);
317+
fileName.append(".txt");
318+
std::ofstream logFile(fileName);
306319
logFile << "LR\tTransmission\tReflection\tHitsToTransmission\tHitsToReflect"
307320
"ion\n";
308321

309322
for (auto length : lengths) {
310323
std::cout << "Length: " << length << std::endl;
311324
std::cout << "L/R: " << length / radius << std::endl;
312-
#ifdef useDiskMesh
325+
#ifdef USE_DISK_MESH
313326
auto [mesh, materialIds] =
314327
createCylinderDiskMesh(gridDelta, radius, length);
315328
#else
@@ -323,6 +336,13 @@ int main() {
323336

324337
tracer.apply();
325338

339+
// auto info = tracer.getRayTraceInfo();
340+
// std::cout << "Non geo hits: " << info.nonGeometryHits << std::endl;
341+
// std::cout << "Num rays: " << info.numRays << std::endl;
342+
// std::cout << "Boundary hits: " << info.boundaryHits << std::endl;
343+
// std::cout << "Reflections: " << info.reflections << std::endl;
344+
// std::cout << "Rays terminated: " << info.raysTerminated << std::endl;
345+
326346
auto &localData = tracer.getLocalData();
327347
counts[0] = static_cast<unsigned>(
328348
localData.getVectorData(0)[0]); // Transmission count

gpu/benchmark/transmission.ipynb

Lines changed: 74 additions & 27 deletions
Large diffs are not rendered by default.

gpu/include/psgPipelineParameters.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22

3-
#include <raygRNG.hpp>
3+
#include <vcRNG.hpp>
44

55
namespace viennaps::gpu::impl {
66
struct IonParams {
@@ -64,7 +64,7 @@ __forceinline__ __device__ void updateEnergy(viennaray::gpu::PerRayData *prd,
6464

6565
float newEnergy;
6666
do {
67-
const float u = viennaray::gpu::getNormalDistRand(&prd->RNGstate);
67+
const float u = viennacore::getNormalDistRand(&prd->RNGstate);
6868
newEnergy = prd->energy * (Eref_peak + sigma * u);
6969
} while (newEnergy < 0.f || newEnergy > prd->energy);
7070
prd->energy = newEnergy;
@@ -97,7 +97,7 @@ initNormalDistEnergy(viennaray::gpu::PerRayData *prd, const float mean,
9797
energy = mean;
9898
} else {
9999
do {
100-
const float u = viennaray::gpu::getNormalDistRand(&prd->RNGstate);
100+
const float u = viennacore::getNormalDistRand(&prd->RNGstate);
101101
energy = mean + sigma * u;
102102
} while (energy < 0.f);
103103
}

include/viennaps/process/psGPUDiskEngine.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,9 @@ class GPUDiskEngine final : public FluxEngine<NumericType, D> {
207207
context.diskMesh, context.getProcessName() + "_flux_" +
208208
std::to_string(iterations++) + ".vtp")
209209
.apply();
210-
211-
if (context.flags.useCoverages) {
212-
d_coverages.free();
213-
}
214210
}
211+
212+
d_coverages.free();
215213
this->timer_.finish();
216214

217215
return ProcessResult::SUCCESS;

include/viennaps/process/psGPULineEngine.hpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ class GPULineEngine final : public FluxEngine<NumericType, D> {
114114
elementKdTree_ = KDTreeType::New();
115115

116116
CreateSurfaceMesh<NumericType, float, D>(
117-
context.domain->getLevelSets().back(), surfaceMesh_, elementKdTree_,
118-
1e-12, context.rayTracingParams.minNodeDistanceFactor)
117+
context.domain->getSurface(), surfaceMesh_, elementKdTree_, 1e-12,
118+
context.rayTracingParams.minNodeDistanceFactor)
119119
.apply();
120120

121121
viennaray::LineMesh lineMesh(
@@ -258,11 +258,9 @@ class GPULineEngine final : public FluxEngine<NumericType, D> {
258258
context.diskMesh, context.getProcessName() + "_flux_" +
259259
std::to_string(iterations++) + ".vtp")
260260
.apply();
261-
262-
if (context.flags.useCoverages) {
263-
d_coverages.free();
264-
}
265261
}
262+
263+
d_coverages.free();
266264
this->timer_.finish();
267265

268266
return ProcessResult::SUCCESS;

include/viennaps/process/psGPUTriangleEngine.hpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ class GPUTriangleEngine final : public FluxEngine<NumericType, D> {
111111
elementKdTree_ = KDTreeType::New();
112112

113113
CreateSurfaceMesh<NumericType, float, D>(
114-
context.domain->getLevelSets().back(), surfaceMesh_, elementKdTree_,
115-
1e-12, context.rayTracingParams.minNodeDistanceFactor)
114+
context.domain->getSurface(), surfaceMesh_, elementKdTree_, 1e-12,
115+
context.rayTracingParams.minNodeDistanceFactor)
116116
.apply();
117117
assert(!surfaceMesh_->nodes.empty());
118118

@@ -248,11 +248,9 @@ class GPUTriangleEngine final : public FluxEngine<NumericType, D> {
248248
context.getProcessName() + "_flux_" +
249249
std::to_string(iterations++) + ".vtp")
250250
.apply();
251-
252-
if (context.flags.useCoverages) {
253-
d_coverages.free();
254-
}
255251
}
252+
253+
d_coverages.free();
256254
this->timer_.finish();
257255

258256
return ProcessResult::SUCCESS;
@@ -285,7 +283,7 @@ class GPUTriangleEngine final : public FluxEngine<NumericType, D> {
285283
const auto numPoints = rayTracer_.getNumberOfElements();
286284
assert(numRates > 0);
287285
assert(numPoints == surfaceMesh_->getElements<3>().size());
288-
auto valueBuffer = rayTracer_.getResults();
286+
auto &valueBuffer = rayTracer_.getResults();
289287
std::vector<float> tmpBuffer(numRates * numPoints);
290288
valueBuffer.download(tmpBuffer.data(), numPoints * numRates);
291289
auto particles = rayTracer_.getParticles();

include/viennaps/process/psProcess.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@ template <typename NumericType, int D> class Process {
193193

194194
// Factory method for creating flux engines
195195
std::unique_ptr<FluxEngine<NumericType, D>> createFluxEngine() {
196+
assert(fluxEngineType_ != FluxEngineType::AUTO &&
197+
"Flux engine type must be specified before creation.");
198+
Logger::getInstance()
199+
.addDebug("Creating flux engine of type: " + to_string(fluxEngineType_))
200+
.print();
196201
// Create CPU engine
197202
if (fluxEngineType_ == FluxEngineType::CPU_DISK) {
198203
return std::make_unique<CPUDiskEngine<NumericType, D>>();

python/pyWrapDimension.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,8 @@ template <int D> void bindApi(py::module &module) {
511511
.def("generateCellSet", &Domain<T, D>::generateCellSet,
512512
"Generate the cell set.")
513513
.def("getLevelSets", &Domain<T, D>::getLevelSets)
514+
.def("getSurface", &Domain<T, D>::getSurface,
515+
"Get the surface level set.")
514516
.def("getCellSet", &Domain<T, D>::getCellSet, "Get the cell set.")
515517
.def("getGrid", &Domain<T, D>::getGrid, "Get the grid")
516518
.def("getGridDelta", &Domain<T, D>::getGridDelta, "Get the grid delta.")

python/viennaps/d2/__init__.pyi

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,11 @@ class Domain:
549549
Get the domain setup.
550550
"""
551551

552+
def getSurface(self) -> viennals.d2.Domain:
553+
"""
554+
Get the surface level set.
555+
"""
556+
552557
def getSurfaceMesh(
553558
self,
554559
addInterfaces: bool = False,
@@ -1446,6 +1451,7 @@ class ToDiskMesh:
14461451
def __init__(self, domain: Domain, mesh: viennals._core.Mesh) -> None: ...
14471452
@typing.overload
14481453
def __init__(self) -> None: ...
1454+
def apply(self) -> None: ...
14491455
def setDomain(self, arg0: Domain) -> None:
14501456
"""
14511457
Set the domain in the mesh converter.

0 commit comments

Comments
 (0)