Skip to content

Commit

Permalink
Merge pull request #70 from kjetil-lye/master
Browse files Browse the repository at this point in the history
Fixes for cuda for the dll writer.
  • Loading branch information
Kjetil Olsen Lye committed Jul 11, 2019
2 parents b648d3a + 1ecc237 commit 9b4bc89
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 9 deletions.
8 changes: 6 additions & 2 deletions alsfvm/include/alsfvm/cuda/CudaMemory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
Expand Down Expand Up @@ -71,6 +71,8 @@ class CudaMemory : public memory::Memory<T> {
virtual void copyFromHost(const T* bufferPointer, size_t bufferLength) override;




///
/// Adds the other memory area to this one
/// \param other the memory area to add from
Expand Down Expand Up @@ -178,6 +180,8 @@ class CudaMemory : public memory::Memory<T> {

std::shared_ptr<memory::Memory<T> > getHostMemory() override;

const std::shared_ptr<const memory::Memory<T> > getHostMemory() const override;

//! Computes the total variation, given here as
//!
//! \f[\sum_{i,j,k} \sqrt(\sum_{n=1}^d|u_{(i,j,k)}-u_{(i,j,k)-e_n}|^2)^p.\f]
Expand Down
8 changes: 6 additions & 2 deletions alsfvm/include/alsfvm/memory/HostMemory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
Expand Down Expand Up @@ -218,6 +218,10 @@ class HostMemory : public alsfvm::memory::Memory<T> {

virtual std::shared_ptr<Memory<T> > getHostMemory() override;

//! Copies the data to host if it is on GPU, otherwise makes a copy
//! Const version
virtual const std::shared_ptr<const Memory<T> > getHostMemory() const override;

//! Computes the total variation, given here as
//!
//! \f[\sum_{i,j,k} \sqrt(\sum_{n=1}^d|u_{(i,j,k)}-u_{(i,j,k)-e_n}|^2)^p.\f]
Expand Down
4 changes: 4 additions & 0 deletions alsfvm/include/alsfvm/memory/Memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,10 @@ class Memory : public MemoryBase,
//! Copies the data to host if it is on GPU, otherwise makes a copy
virtual std::shared_ptr<Memory<T> > getHostMemory() = 0;

//! Copies the data to host if it is on GPU, otherwise makes a copy
//! Const version
virtual const std::shared_ptr<const Memory<T> > getHostMemory() const = 0;

//! Computes the total variation, given here as
//!
//! \f[\sum_{i,j,k} \sqrt(\sum_{n=1}^d|u_{(i,j,k)}-u_{(i,j,k)-e_n}|^2)^p.\f]
Expand Down
11 changes: 11 additions & 0 deletions alsfvm/src/cuda/CudaMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,17 @@ std::shared_ptr<memory::Memory<T> > CudaMemory<T>::getHostMemory() {
return pointer;
}

template<class T>
const std::shared_ptr<const memory::Memory<T> > CudaMemory<T>::getHostMemory()
const {
std::shared_ptr<memory::Memory<T> > pointer(new memory::HostMemory<T>(this->nx,
this->ny, this->nz));

this->copyToHost(pointer->getPointer(), pointer->getSize());

return std::dynamic_pointer_cast<const memory::Memory<T>>(pointer);
}

template<class T>
real CudaMemory<T>::getTotalVariation(int p, const ivec3& start,
const ivec3& end) const {
Expand Down
4 changes: 3 additions & 1 deletion alsfvm/src/functional/TimeIntegrationFunctional.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ void TimeIntegrationFunctional::write(const volume::Volume& conservedVariables,

void TimeIntegrationFunctional::finalize(const grid::Grid& grid,
const simulator::TimestepInformation& timestepInformation) {
const ivec3 numberOfNodes = grid.getGlobalSize() / grid.getDimensions();
grid::Grid smallerGrid(grid.getOrigin(),
grid.getTop(),
functionalSize,
grid.getBoundaryConditions(),
grid.getGlobalPosition(),
grid.getGlobalSize());
numberOfNodes * functionalSize);

writer->write(*conservedVolume, smallerGrid, timestepInformation);
}

Expand Down
20 changes: 16 additions & 4 deletions alsfvm/src/io/DLLWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <boost/dll.hpp>
#include <boost/algorithm/string.hpp>
#include "alsutils/config.hpp"
#include "alsutils/cuda/get_current_gpu_id.hpp"

namespace alsfvm {
namespace io {
Expand Down Expand Up @@ -101,9 +102,7 @@ void DLLWriter::write(const volume::Volume& conservedVariables,
const grid::Grid& grid,
const simulator::TimestepInformation& timestepInformation) {

if (needsDataOnHost && !conservedVariables.getScalarMemoryArea(0)->isOnHost()) {

}

if (newTimestepFunction) {
newTimestepFunction(dllData, parametersStruct,
Expand All @@ -130,15 +129,28 @@ void DLLWriter::write(const volume::Volume& conservedVariables,


for (size_t var = 0; var < conservedVariables.getNumberOfVariables(); ++var) {
auto dataSmartPointer = conservedVariables.getScalarMemoryArea(var);

if (needsDataOnHost && !dataSmartPointer->isOnHost()) {
dataSmartPointer = dataSmartPointer->getHostMemory();

}

int gpuID = -1;

if (!dataSmartPointer->isOnHost()) {
gpuID = alsutils::cuda::getCurrentGPUId();
}

writeFunction(dllData, parametersStruct,
timestepInformation.getCurrentTime(),
conservedVariables.getName(var).c_str(),
conservedVariables.getScalarMemoryArea(var)->getPointer(),
dataSmartPointer->getPointer(),
nx, ny, nz,
ngx, ngy, ngz,
ax, ay, az,
bx, by, bz,
int(!conservedVariables.getScalarMemoryArea(var)->isOnHost()) - 1);
gpuID);
}

if (endTimestepFunction) {
Expand Down
6 changes: 6 additions & 0 deletions alsfvm/src/memory/HostMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,12 @@ std::shared_ptr<Memory<T> > HostMemory<T>::getHostMemory() {
return this->shared_from_this();
}

template<class T>
const std::shared_ptr<const Memory<T> > HostMemory<T>::getHostMemory() const {
return this->shared_from_this();

}

template<class T>
real HostMemory<T>::getTotalVariation(int p, const ivec3& start,
const ivec3& end) const {
Expand Down
21 changes: 21 additions & 0 deletions alsutils/include/alsutils/cuda/get_current_gpu_id.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once
#include "alsutils/config.hpp"
#ifdef ALSVINN_HAVE_CUDA
#include <cuda.h>
#include <cuda_runtime.h>
#include "alsutils/cuda/cuda_safe_call.hpp"

#endif
namespace alsutils {
namespace cuda {
//! Returns the current active GPU id, or -1 if cuda is disabled
inline int getCurrentGPUId() {
int deviceID = -1;
#ifdef ALSVINN_HAVE_CUDA
CUDA_SAFE_CALL(cudaGetDevice(&deviceID));
#endif

return deviceID;
}
}
}
43 changes: 43 additions & 0 deletions test/library_tests/src/fvm/functional/PowerTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* Copyright (c) 2019 ETH Zurich, Kjetil Olsen Lye
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <gtest/gtest.h>
#include "alsfvm/types.hpp"
#include "alsutils/math/FastPower.hpp"
#include "alsutils/math/PowPower.hpp"

TEST(PowerTest, TestPowPower) {
for (int p = 1; p < 10; ++p) {
// I would have tested for unlimited power here, but the senate would not
// approve.
ASSERT_EQ(alsutils::math::PowPower::power(2, p), std::pow(2, p));
}
}

TEST(PowerTest, TestFastPower) {
ASSERT_EQ(alsutils::math::FastPower<1>::power(2, 1), std::pow(2, 1));


ASSERT_EQ(alsutils::math::FastPower<2>::power(2, 2), std::pow(2, 2));


ASSERT_EQ(alsutils::math::FastPower<3>::power(2, 3), std::pow(2, 3));


ASSERT_EQ(alsutils::math::FastPower<4>::power(2, 4), std::pow(2, 4));


ASSERT_EQ(alsutils::math::FastPower<5>::power(2, 5), std::pow(2, 5));
}

0 comments on commit 9b4bc89

Please sign in to comment.