Skip to content

Commit fd3ef4a

Browse files
Merge pull request ComputationalRadiationPhysics#2522 from sbastrakov/fix-PMaccConstexprCaptureMSVC
Add a workaround for MSVC bug with capturing constexpr
2 parents 65457a8 + cbd0dfa commit fd3ef4a

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

include/pmacc/fields/operations/AddExchangeToBorder.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ namespace operations
8585
// number of cells in a superCell
8686
constexpr uint32_t numCells = pmacc::math::CT::volume< SuperCellSize >::type::value;
8787
constexpr uint32_t numWorkers = T_numWorkers;
88-
constexpr int dim = T_Mapping::Dim;
88+
PMACC_CONSTEXPR_CAPTURE int dim = T_Mapping::Dim;
8989

9090
uint32_t const workerIdx = threadIdx.x;
9191

include/pmacc/fields/operations/CopyGuardToExchange.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ namespace operations
8282
// number of cells in a superCell
8383
constexpr uint32_t numCells = pmacc::math::CT::volume< SuperCellSize >::type::value;
8484
constexpr uint32_t numWorkers = T_numWorkers;
85-
constexpr int dim = T_Mapping::Dim;
85+
PMACC_CONSTEXPR_CAPTURE int dim = T_Mapping::Dim;
8686

8787
uint32_t const workerIdx = threadIdx.x;
8888

include/pmacc/particles/ParticlesBase.kernel

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ struct KernelFillGapsLastFrame
135135
uint32_t const
136136
)
137137
{
138-
lastFrame = pb.getLastFrame( DataSpace< dim >( superCellIdx ) );
138+
lastFrame = pb.getLastFrame( superCellIdx );
139139
counterGaps = 0;
140140
counterParticles = 0;
141141
srcGap = 0;
@@ -324,8 +324,8 @@ struct KernelFillGaps
324324
uint32_t const
325325
)
326326
{
327-
firstFrame = pb.getFirstFrame( DataSpace< dim >(superCellIdx) );
328-
lastFrame = pb.getLastFrame( DataSpace< dim >(superCellIdx) );
327+
firstFrame = pb.getFirstFrame( superCellIdx );
328+
lastFrame = pb.getLastFrame( superCellIdx );
329329
}
330330
);
331331

@@ -512,7 +512,7 @@ struct KernelShiftParticles
512512
using FrameType = typename ParBox::FrameType;
513513
using FramePtr = typename ParBox::FramePtr;
514514

515-
constexpr uint32_t dim = Mapping::Dim;
515+
PMACC_CONSTEXPR_CAPTURE uint32_t dim = Mapping::Dim;
516516
constexpr uint32_t frameSize = math::CT::volume< typename FrameType::SuperCellSize >::type::value;
517517
/* number exchanges in 2D=9 and in 3D=27 */
518518
constexpr uint32_t numExchanges = traits::NumberOfExchanges< dim >::value;
@@ -962,7 +962,7 @@ struct KernelCopyGuardToExchange
962962
using namespace particles::operations;
963963
using namespace mappings::threads;
964964

965-
constexpr uint32_t dim = T_Mapping::Dim;
965+
PMACC_CONSTEXPR_CAPTURE uint32_t dim = T_Mapping::Dim;
966966
constexpr uint32_t frameSize = math::CT::volume< typename T_ParBox::FrameType::SuperCellSize >::type::value;
967967
constexpr uint32_t numWorkers = T_numWorkers;
968968

@@ -1180,7 +1180,7 @@ struct KernelInsertParticles
11801180
using namespace particles::operations;
11811181
using namespace mappings::threads;
11821182

1183-
constexpr uint32_t dim = T_Mapping::Dim;
1183+
PMACC_CONSTEXPR_CAPTURE uint32_t dim = T_Mapping::Dim;
11841184
constexpr uint32_t frameSize = math::CT::volume< typename T_ParBox::FrameType::SuperCellSize >::type::value;
11851185
constexpr uint32_t numWorkers = T_numWorkers;
11861186

include/pmacc/types.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,4 +232,28 @@ enum AreaType
232232
#define __delete(var) if((var)) { delete (var); var=nullptr; }
233233
#define __deleteArray(var) if((var)) { delete[] (var); var=nullptr; }
234234

235+
/**
236+
* Visual Studio has a bug with constexpr variables being captured in lambdas as
237+
* non-constexpr variables, causing build errors. The issue has been verified
238+
* for versions 14.0 and 15.5 (latest at the moment) and is also reported in
239+
* https://stackoverflow.com/questions/28763375/using-lambda-captured-constexpr-value-as-an-array-dimension
240+
* and related issue
241+
* https://developercommunity.visualstudio.com/content/problem/1997/constexpr-not-implicitely-captured-in-lambdas.html
242+
*
243+
* As a workaround (until this is fixed in VS) add a new PMACC_CONSTEXPR_CAPTURE
244+
* macro for declaring constexpr variables that are captured in lambdas and have
245+
* to remain constexpr inside a lambda e.g., used as a template argument. Such
246+
* variables have to be declared with PMACC_CONSTEXPR_CAPTURE instead of
247+
* constexpr. The macro will be replaced with just constexpr for other compilers
248+
* and for Visual Studio with static constexpr, which makes it capture properly.
249+
*
250+
* Note that this macro is to be used only in very few cases, where not only a
251+
* constexpr is captured, but also it has to remain constexpr inside a lambda.
252+
*/
253+
#ifdef _MSC_VER
254+
# define PMACC_CONSTEXPR_CAPTURE static constexpr
255+
#else
256+
# define PMACC_CONSTEXPR_CAPTURE constexpr
257+
#endif
258+
235259
} //namespace pmacc

0 commit comments

Comments
 (0)