From 229c8916afb82594a060e1711d13034d52840536 Mon Sep 17 00:00:00 2001 From: Benjamin Worpitz Date: Fri, 26 Jun 2015 09:09:30 +0200 Subject: [PATCH] fix unconditional usage of getAvailableSlotsHost Currently the `getAvailableSlotsHost` function of a `CreationPolicy` is required irrespective of its availability. In the [source](https://github.com/ComputationalRadiationPhysics/mallocMC/blob/84c8c20309633c98f69be83153eb897ce2d83110/src/include/mallocMC/mallocMC_hostclass.hpp#L144-155) the `CreationPolicy::providesAvailableSlots::value` flag is checked in a runtime `if` so the function is required for all CreationPolicies even for `OldMalloc`. This has been changed to a compile time construct. --- src/include/mallocMC/mallocMC_hostclass.hpp | 63 ++++++++++++++------- 1 file changed, 43 insertions(+), 20 deletions(-) diff --git a/src/include/mallocMC/mallocMC_hostclass.hpp b/src/include/mallocMC/mallocMC_hostclass.hpp index 1c768305..f4d3b9f9 100644 --- a/src/include/mallocMC/mallocMC_hostclass.hpp +++ b/src/include/mallocMC/mallocMC_hostclass.hpp @@ -64,6 +64,42 @@ namespace mallocMC{ size_t size; }; + namespace detail{ + + /** + * @brief Template class to call getAvailableSlots[Host|Accelerator] if the CreationPolicy provides it. + * + * Returns 0 else. + * + * @tparam T_CreationPolicy The desired type of a CreationPolicy + * @tparam T_ProvidesAvailableSlotsHost If the CreationPolicy provides getAvailableSlotsHost + */ + template + struct GetAvailableSlotsIfAvail + { + template + MAMC_HOST MAMC_ACCELERATOR + static unsigned getAvailableSlots(size_t slotSize, T_Allocator &){ + return 0; + } + }; + template<> + struct GetAvailableSlotsIfAvail< + boost::mpl::bool_ > + { + template + MAMC_HOST MAMC_ACCELERATOR + static unsigned getAvailableSlots(size_t slotSize, T_Allocator & alloc){ +#ifdef __CUDA_ARCH__ + return alloc.getAvailableSlotsAccelerator(slotSize); +#else + return alloc.getAvailableSlotsHost(slotSize, alloc); +#endif + } + }; + + } + /** * @brief "HostClass" that combines all policies to a useful allocator * @@ -144,15 +180,15 @@ namespace mallocMC{ /* * This is a workaround for a bug with getAvailSlotsPoly: * Due to some problems with conditional compilation (possibly a CUDA bug), - * this host function must explicitly be used from inside a host + * getAvailableSlotsHost must explicitly be used from inside a host * function at least once. Doing it here guarantees that it is executed * and that this execution happens on the host. Usually, simply defining * this inside a host function (without actually executing it) would be * sufficient. However, due to the template nature of policy based * design, functions are only compiled if they are actually used. */ - if(CreationPolicy::providesAvailableSlots::value) - CreationPolicy::getAvailableSlotsHost(1024,*this); //actual slot size does not matter + detail::GetAvailableSlotsIfAvail > + ::getAvailableSlots(1024, *this); //actual slot size does not matter return h; } @@ -178,7 +214,10 @@ namespace mallocMC{ // polymorphism over the availability of getAvailableSlots MAMC_HOST MAMC_ACCELERATOR unsigned getAvailableSlots(size_t slotSize){ - return getAvailSlotsPoly(slotSize, boost::mpl::bool_()); + slotSize = AlignmentPolicy::applyPadding(slotSize); + + return detail::GetAvailableSlotsIfAvail > + ::getAvailableSlots(slotSize, *this); } MAMC_HOST @@ -188,22 +227,6 @@ namespace mallocMC{ return v; } - private: - MAMC_HOST MAMC_ACCELERATOR - unsigned getAvailSlotsPoly(size_t slotSize, boost::mpl::bool_){ - return 0; - } - - MAMC_HOST MAMC_ACCELERATOR - unsigned getAvailSlotsPoly(size_t slotSize, boost::mpl::bool_){ - slotSize = AlignmentPolicy::applyPadding(slotSize); -#ifdef __CUDA_ARCH__ - return CreationPolicy::getAvailableSlotsAccelerator(slotSize); -#else - return CreationPolicy::getAvailableSlotsHost(slotSize,*this); -#endif - } - }; } //namespace mallocMC