Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SofaOpenCL] Make it work with 20.06 #1361

Merged
merged 4 commits into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions applications/plugins/SofaOpenCL/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ project(SofaOpenCL)

set(SOFAOPENCL_VERSION 0.1)

find_package(OpenCL REQUIRED)
find_package(SofaGeneral REQUIRED)
sofa_find_package(SofaSphFluid QUIET)

set(HEADER_FILES
CPUSPHFluidForceField.h
CPUSPHFluidForceFieldWithOpenCL.h
Expand Down Expand Up @@ -92,17 +96,22 @@ else()
endif()


find_package(OpenCL REQUIRED)
find_package(SofaGeneral REQUIRED)

message(WARNING "SofaOpenCL: TODO: don't use absolute path (SOFA_SRC_DIR)")

include_directories(${OPENCL_INCLUDE_DIRS})
add_library(${PROJECT_NAME} SHARED ${HEADER_FILES} ${SOURCE_FILES} ${OTHER_FILES})
add_definitions("-DSOFA_SRC_DIR=\"${CMAKE_CURRENT_SOURCE_DIR}/../../..\"")

add_definitions("-DSOFA_BUILD_GPU_OPENCL")
target_link_libraries(${PROJECT_NAME} ${OPENCL_LIBRARIES} SofaHelper SofaEngine SofaSphFluid SofaUserInteraction csparse) # taucs taucs_mt system-taucs)
target_link_libraries(${PROJECT_NAME} ${OPENCL_LIBRARIES} SofaHelper SofaEngine SofaUserInteraction csparse) # taucs taucs_mt system-taucs)
if(SofaSphFluid_FOUND)
target_link_libraries(${PROJECT_NAME} SofaSphFluid) # taucs taucs_mt system-taucs)
endif()

## Install rules for the library and headers; CMake package configurations files
sofa_create_package(SofaOpenCL ${SOFAOPENCL_VERSION} SofaOpenCL SofaOpenCL)
sofa_generate_package(
NAME ${PROJECT_NAME}
TARGETS ${PROJECT_NAME}
VERSION ${SOFAOPENCL_VERSION}
INCLUDE_INSTALL_DIR ${PROJECT_NAME}
RELOCATABLE "plugins"
)
8 changes: 4 additions & 4 deletions applications/plugins/SofaOpenCL/OpenCLFixedConstraint.inl
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ template <>
void FixedConstraintInternalData<gpu::opencl::OpenCLVec3fTypes>::projectResponse(Main* m, VecDeriv& dx)
{
Data& data = *m->data;
if (m->f_fixAll.getValue())
if (m->d_fixAll.getValue())
FixedConstraintOpenCL3f_projectResponseContiguous(dx.size(), dx.deviceWrite());
else if (data.minIndex >= 0)
FixedConstraintOpenCL3f_projectResponseContiguous(data.maxIndex-data.minIndex+1, OpenCLMemoryManager<float>::deviceOffset(dx.deviceWrite(),3*data.minIndex));
Expand All @@ -205,7 +205,7 @@ template <>
void FixedConstraintInternalData<gpu::opencl::OpenCLVec3f1Types>::projectResponse(Main* m, VecDeriv& dx)
{
Data& data = *m->data;
if (m->f_fixAll.getValue())
if (m->d_fixAll.getValue())
FixedConstraintOpenCL3f1_projectResponseContiguous(dx.size(), dx.deviceWrite());
else if (data.minIndex >= 0)
FixedConstraintOpenCL3f1_projectResponseContiguous(data.maxIndex-data.minIndex+1, OpenCLMemoryManager<float>::deviceOffset(dx.deviceWrite(),4*data.minIndex));
Expand All @@ -217,7 +217,7 @@ template <>
void FixedConstraintInternalData<gpu::opencl::OpenCLVec3dTypes>::projectResponse(Main* m, VecDeriv& dx)
{
Data& data = *m->data;
if (m->f_fixAll.getValue())
if (m->d_fixAll.getValue())
FixedConstraintOpenCL3d_projectResponseContiguous(dx.size(), dx.deviceWrite());
else if (data.minIndex >= 0)
FixedConstraintOpenCL3d_projectResponseContiguous(data.maxIndex-data.minIndex+1, OpenCLMemoryManager<double>::deviceOffset(dx.deviceWrite(),3*data.minIndex));
Expand All @@ -229,7 +229,7 @@ template <>
void FixedConstraintInternalData<gpu::opencl::OpenCLVec3d1Types>::projectResponse(Main* m, VecDeriv& dx)
{
Data& data = *m->data;
if (m->f_fixAll.getValue())
if (m->d_fixAll.getValue())
FixedConstraintOpenCL3d1_projectResponseContiguous(dx.size(), dx.deviceWrite());
else if (data.minIndex >= 0)
FixedConstraintOpenCL3d1_projectResponseContiguous(data.maxIndex-data.minIndex+1, OpenCLMemoryManager<double>::deviceOffset(dx.deviceWrite(),4*data.minIndex));
Expand Down
8 changes: 4 additions & 4 deletions applications/plugins/SofaOpenCL/OpenCLIdentityMapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ template class IdentityMapping< OpenCLVec3fTypes, OpenCLVec3dTypes>;
template class IdentityMapping< OpenCLVec3dTypes, OpenCLVec3fTypes>;
template class IdentityMapping< OpenCLVec3dTypes, OpenCLVec3dTypes>;
template class IdentityMapping< OpenCLVec3dTypes, Vec3fTypes>;
template class IdentityMapping< OpenCLVec3Types, Vec3Types>;
template class IdentityMapping< OpenCLVec3dTypes, Vec3Types>;

template class IdentityMapping< Vec3fTypes, OpenCLVec3dTypes>;
template class IdentityMapping< Vec3Types, OpenCLVec3Types>;
template class IdentityMapping< Vec3Types, OpenCLVec3dTypes>;


template class IdentityMapping< OpenCLVec3d1Types, Vec3fTypes>;
Expand Down Expand Up @@ -108,10 +108,10 @@ int IdentityMappingOpenCLClass = core::RegisterObject("Supports GPU-side computa
.add< IdentityMapping< OpenCLVec3dTypes, OpenCLVec3fTypes> >()
.add< IdentityMapping< OpenCLVec3dTypes, OpenCLVec3dTypes> >()
.add< IdentityMapping< OpenCLVec3dTypes, Vec3fTypes> >()
.add< IdentityMapping< OpenCLVec3Types, Vec3Types> >()
.add< IdentityMapping< OpenCLVec3dTypes, Vec3Types> >()

.add< IdentityMapping< Vec3fTypes, OpenCLVec3dTypes> >()
.add< IdentityMapping< Vec3Types, OpenCLVec3Types> >()
.add< IdentityMapping< Vec3dTypes, OpenCLVec3Types> >()

.add< IdentityMapping< OpenCLVec3d1Types, Vec3fTypes> >()
.add< IdentityMapping< OpenCLVec3f1Types, OpenCLVec3f1Types> >()
Expand Down
12 changes: 6 additions & 6 deletions applications/plugins/SofaOpenCL/OpenCLSPHFluidForceField.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ class SPHFluidForceFieldInternalData< gpu::opencl::OpenCLVectorTypes<TCoord,TDer

void fillParams(Main* m, int kernelType, double kFactor=1.0, double bFactor=1.0)
{
Real h = m->particleRadius.getValue();
Real h = m->d_particleRadius.getValue();
params.h = h;
params.h2 = h*h;
params.stiffness = (Real)(kFactor*m->pressureStiffness.getValue());
params.mass = m->particleMass.getValue();
params.stiffness = (Real)(kFactor*m->d_pressureStiffness.getValue());
params.mass = m->d_particleMass.getValue();
params.mass2 = params.mass*params.mass;
params.density0 = m->density0.getValue();
params.viscosity = (Real)(bFactor*m->viscosity.getValue());
params.surfaceTension = (Real)(kFactor*m->surfaceTension.getValue());
params.density0 = m->d_density0.getValue();
params.viscosity = (Real)(bFactor*m->d_viscosity.getValue());
params.surfaceTension = (Real)(kFactor*m->d_surfaceTension.getValue());

if (kernelType == 1)
{
Expand Down
65 changes: 36 additions & 29 deletions applications/plugins/SofaOpenCL/OpenCLSPHFluidForceField.inl
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,16 @@ void SPHFluidForceFieldInternalData<gpu::opencl::OpenCLVec3fTypes>::Kernels_addD
template <>
void SPHFluidForceField<gpu::opencl::OpenCLVec3fTypes>::addForce(const core::MechanicalParams* /*mparams*/ /* PARAMS FIRST */, DataVecDeriv& d_f, const DataVecCoord& d_x, const DataVecDeriv& d_v)
{
if (grid == NULL) return;
if (m_grid == NULL) return;

VecDeriv& f = *d_f.beginEdit();
const VecCoord& x = d_x.getValue();
const VecDeriv& v = d_v.getValue();

grid->updateGrid(x);
data.fillParams(this, kernelType.getValue());
m_grid->updateGrid(x);
data.fillParams(this, d_kernelType.getValue());
f.resize(x.size());
Grid::Grid* g = grid->getGrid();
Grid::Grid* g = m_grid->getGrid();
data.pos4.recreate(x.size());
data.Kernels_computeDensity(
g->getNbCells(), g->getCellsVector().deviceRead(), g->getCellGhostVector().deviceRead(),
Expand All @@ -115,7 +115,7 @@ void SPHFluidForceField<gpu::opencl::OpenCLVec3fTypes>::addDForce(const core::Me
{
//?
return;
if (grid == NULL) return;
if (m_grid == NULL) return;

VecDeriv& df = *d_df.beginEdit();
const VecDeriv& dx = d_dx.getValue();
Expand All @@ -125,7 +125,7 @@ void SPHFluidForceField<gpu::opencl::OpenCLVec3fTypes>::addDForce(const core::Me
const VecDeriv& v = this->mstate->read(core::ConstVecDerivId::velocity())->getValue();
data.fillParams(this, mparams->kFactor(), mparams->bFactor());
df.resize(dx.size());
Grid::Grid* g = grid->getGrid();
Grid::Grid* g = m_grid->getGrid();
data.Kernels_addDForce(
g->getNbCells(), g->getCellsVector().deviceRead(), g->getCellGhostVector().deviceRead(),
df.deviceWrite(), data.pos4.deviceRead(), v.deviceRead(), dx.deviceRead());
Expand Down Expand Up @@ -155,16 +155,16 @@ void SPHFluidForceFieldInternalData<gpu::opencl::OpenCLVec3dTypes>::Kernels_addD
template <>
void SPHFluidForceField<gpu::opencl::OpenCLVec3dTypes>::addForce(const core::MechanicalParams* /*mparams*/ /* PARAMS FIRST */, DataVecDeriv& d_f, const DataVecCoord& d_x, const DataVecDeriv& d_v)
{
if (grid == NULL) return;
if (m_grid == NULL) return;

VecDeriv& f = *d_f.beginEdit();
const VecCoord& x = d_x.getValue();
const VecDeriv& v = d_v.getValue();

grid->updateGrid(x);
data.fillParams(this, kernelType.getValue());
m_grid->updateGrid(x);
data.fillParams(this, d_kernelType.getValue());
f.resize(x.size());
Grid::Grid* g = grid->getGrid();
Grid::Grid* g = m_grid->getGrid();
data.pos4.recreate(x.size());
data.Kernels_computeDensity(
g->getNbCells(), g->getCellsVector().deviceRead(), g->getCellGhostVector().deviceRead(),
Expand All @@ -179,14 +179,14 @@ void SPHFluidForceField<gpu::opencl::OpenCLVec3dTypes>::addForce(const core::Mec
template <>
void SPHFluidForceField<gpu::opencl::OpenCLVec3dTypes>::addDForce(const core::MechanicalParams* mparams /* PARAMS FIRST */, DataVecDeriv& d_df, const DataVecDeriv& d_dx)
{
if (grid == NULL) return;
if (m_grid == NULL) return;
VecDeriv& df = *d_df.beginEdit();
const VecDeriv& dx = d_dx.getValue();
//const VecCoord& x = this->mstate->read(core::ConstVecCoordId::position())->getValue();
const VecDeriv& v = this->mstate->read(core::ConstVecDerivId::velocity())->getValue();
data.fillParams(this, mparams->kFactor(), mparams->bFactor());
df.resize(dx.size());
Grid::Grid* g = grid->getGrid();
Grid::Grid* g = m_grid->getGrid();
data.Kernels_addDForce(
g->getNbCells(), g->getCellsVector().deviceRead(), g->getCellGhostVector().deviceRead(),
df.deviceWrite(), data.pos4.deviceRead(), v.deviceRead(), dx.deviceRead());
Expand All @@ -198,34 +198,41 @@ void SPHFluidForceField<gpu::opencl::OpenCLVec3fTypes>::draw(const sofa::core::v
{
if(!vparams->displayFlags().getShowForceFields())return;
//if (!getContext()->getShowForceFields()) return;
//if (grid != NULL)
// grid->draw(vparams);
//if (m_grid != NULL)
// m_grid->draw(vparams);
const VecCoord& x = this->mstate->read(core::ConstVecCoordId::position())->getValue();
const gpu::opencl::OpenCLVector<defaulttype::Vec4f> pos4 = this->data.pos4;
if (pos4.empty()) return;
glDisable(GL_LIGHTING);
glColor3f(0,1,1);
glDisable(GL_BLEND);
glDepthMask(1);
glPointSize(5);
glBegin(GL_POINTS);
for (unsigned int i=0; i<pos4.size(); i++)

vparams->drawTool()->saveLastState();
vparams->drawTool()->disableLighting();
vparams->drawTool()->disableBlending();
vparams->drawTool()->enableDepthTest();

std::vector<sofa::defaulttype::Vec4f> colorVector;
std::vector<sofa::defaulttype::Vector3> vertices;

for (unsigned int i = 0; i < m_particles.size(); i++)
{
float density = pos4[i][3];
float f = (float)(density / density0.getValue());
f = 1+10*(f-1);
Particle& Pi = m_particles[i];
float f = (float)(Pi.density / d_density0.getValue());
f = 1 + 10 * (f - 1);
if (f < 1)
{
glColor3f(0,1-f,f);
colorVector.push_back(sofa::defaulttype::Vec4f(0, 1 - f, f, 1));
}
else
{
glColor3f(f-1,0,2-f);
colorVector.push_back(sofa::defaulttype::Vec4f(f - 1, 0, 2 - f, 1));
}
helper::gl::glVertexT(x[i]);
vertices.push_back(sofa::defaulttype::Vector3(x[i]));
}
glEnd();
glPointSize(1);

vparams->drawTool()->drawPoints(vertices, 5, colorVector);
vertices.clear();
colorVector.clear();

vparams->drawTool()->restoreLastState();
}


Expand Down
7 changes: 5 additions & 2 deletions applications/plugins/SofaOpenCL/OpenCLUniformMass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ namespace opencl
{

int UniformMassOpenCLClass = core::RegisterObject("Supports GPU-side computations using OPENCL")
.add< component::mass::UniformMass<OpenCLVec3Types,double> >()
.add< component::mass::UniformMass<OpenCLVec3Types, float> >()
.add< component::mass::UniformMass<OpenCLVec3f1Types, float> >()
.add< component::mass::UniformMass<OpenCLRigid3fTypes, sofa::defaulttype::Rigid3fMass> >()
.add< component::mass::UniformMass<OpenCLVec3dTypes,double> >()
.add< component::mass::UniformMass<OpenCLVec3d1Types,double> >()
.add< component::mass::UniformMass<OpenCLRigid3Types,sofa::defaulttype::Rigid3Mass> >()
.add< component::mass::UniformMass<OpenCLRigid3dTypes,sofa::defaulttype::Rigid3Mass> >()

;

Expand Down
39 changes: 36 additions & 3 deletions applications/plugins/SofaOpenCL/OpenCLUniformMass.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,47 @@ namespace mass


// -- Mass interface
// float
template <>
void UniformMass<gpu::opencl::OpenCLVec3Types, SReal>::addMDx(const core::MechanicalParams* mparams /* PARAMS FIRST */, DataVecDeriv& f, const DataVecDeriv& dx, SReal factor);
void UniformMass<gpu::opencl::OpenCLVec3Types, float>::addMDx(const core::MechanicalParams* mparams /* PARAMS FIRST */, DataVecDeriv& f, const DataVecDeriv& dx, double factor);

template <>
void UniformMass<gpu::opencl::OpenCLVec3Types, SReal>::accFromF(const core::MechanicalParams* mparams /* PARAMS FIRST */, DataVecDeriv& a, const DataVecDeriv& f);
void UniformMass<gpu::opencl::OpenCLVec3Types, float>::accFromF(const core::MechanicalParams* mparams /* PARAMS FIRST */, DataVecDeriv& a, const DataVecDeriv& f);

template <>
void UniformMass<gpu::opencl::OpenCLVec3Types, SReal>::addForce(const core::MechanicalParams* mparams /* PARAMS FIRST */, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v);
void UniformMass<gpu::opencl::OpenCLVec3Types, float>::addForce(const core::MechanicalParams* mparams /* PARAMS FIRST */, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v);

//template <>
//bool UniformMass<gpu::opencl::OpenCLVec3Types, SReal>::addBBox(SReal* minBBox, SReal* maxBBox);

template <>
double UniformMass<gpu::opencl::OpenCLRigid3fTypes, sofa::defaulttype::Rigid3fMass>::getPotentialEnergy(const core::MechanicalParams* mparams /* PARAMS FIRST */, const DataVecCoord& x) const;

template <>
double UniformMass<gpu::opencl::OpenCLRigid3fTypes, sofa::defaulttype::Rigid3fMass>::getElementMass(unsigned int) const;

template <>
void UniformMass<gpu::opencl::OpenCLRigid3fTypes, sofa::defaulttype::Rigid3fMass>::draw(const sofa::core::visual::VisualParams* vparams);

template <>
void UniformMass<gpu::opencl::OpenCLVec3f1Types, float>::addMDx(const core::MechanicalParams* mparams /* PARAMS FIRST */, DataVecDeriv& f, const DataVecDeriv& dx, double factor);

template <>
void UniformMass<gpu::opencl::OpenCLVec3f1Types, float>::accFromF(const core::MechanicalParams* mparams /* PARAMS FIRST */, DataVecDeriv& a, const DataVecDeriv& f);

template <>
void UniformMass<gpu::opencl::OpenCLVec3f1Types, float>::addForce(const core::MechanicalParams* mparams /* PARAMS FIRST */, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v);


// double
template <>
void UniformMass<gpu::opencl::OpenCLVec3dTypes, SReal>::addMDx(const core::MechanicalParams* mparams /* PARAMS FIRST */, DataVecDeriv& f, const DataVecDeriv& dx, SReal factor);

template <>
void UniformMass<gpu::opencl::OpenCLVec3dTypes, SReal>::accFromF(const core::MechanicalParams* mparams /* PARAMS FIRST */, DataVecDeriv& a, const DataVecDeriv& f);

template <>
void UniformMass<gpu::opencl::OpenCLVec3dTypes, SReal>::addForce(const core::MechanicalParams* mparams /* PARAMS FIRST */, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v);

//template <>
//bool UniformMass<gpu::opencl::OpenCLVec3Types, SReal>::addBBox(SReal* minBBox, SReal* maxBBox);
Expand Down
Loading