Add helper functions for setting up Nbnxm gpu object in nblib
[alexxy/gromacs.git] / api / nblib / tests / nbnxmsetup.cpp
index a928ed4aa65d5df112e34bd905444bf59f94587d..369173467e5338b0037a43741eb187d40489477b 100644 (file)
  */
 #include <cmath>
 
-#include "gromacs/utility/arrayref.h"
+#include "gromacs/hardware/device_management.h"
 #include "gromacs/mdtypes/forcerec.h"
+#include "gromacs/mdtypes/interaction_const.h"
+#include "gromacs/mdtypes/simulation_workload.h"
 #include "gromacs/nbnxm/nbnxm.h"
 #include "gromacs/nbnxm/nbnxm_simd.h"
 #include "nblib/box.h"
 #include "nblib/nbnxmsetuphelpers.h"
 
 #include "testutils/testasserts.h"
+#include "testutils/test_hardware_environment.h"
 
 namespace nblib
 {
@@ -99,19 +102,20 @@ TEST(NbnxmSetupTest, canTranslateBenchmarkEnum4XM)
 
 TEST(NbnxmSetupTest, CheckKernelSetupThrowsAuto)
 {
-    EXPECT_ANY_THROW(checkKernelSetup(SimdKernels::SimdAuto));
+    EXPECT_ANY_THROW(checkKernelSetupSimd(SimdKernels::SimdAuto));
 }
 
 TEST(NbnxmSetupTest, CheckKernelSetupThrowsCount)
 {
-    EXPECT_ANY_THROW(checkKernelSetup(SimdKernels::Count));
+    EXPECT_ANY_THROW(checkKernelSetupSimd(SimdKernels::Count));
 }
 
 TEST(NbnxmSetupTest, canCreateKernelSetupPlain)
 {
     NBKernelOptions nbKernelOptions;
-    nbKernelOptions.nbnxmSimd      = SimdKernels::SimdNo;
-    Nbnxm::KernelSetup kernelSetup = createKernelSetupCPU(nbKernelOptions);
+    nbKernelOptions.nbnxmSimd = SimdKernels::SimdNo;
+    Nbnxm::KernelSetup kernelSetup =
+            createKernelSetupCPU(nbKernelOptions.nbnxmSimd, nbKernelOptions.useTabulatedEwaldCorr);
     EXPECT_EQ(kernelSetup.kernelType, Nbnxm::KernelType::Cpu4x4_PlainC);
     EXPECT_EQ(kernelSetup.ewaldExclusionType, Nbnxm::EwaldExclusionType::Table);
 }
@@ -123,7 +127,7 @@ TEST(NbnxmSetupTest, canCreateParticleInfoAllVdv)
     mask |= gmx::sc_atomInfo_HasVdw;
     mask |= gmx::sc_atomInfo_HasCharge;
     std::vector<int64_t> refParticles  = { mask, mask };
-    std::vector<int64_t> testParticles = createParticleInfoAllVdv(numParticles);
+    std::vector<int64_t> testParticles = createParticleInfoAllVdw(numParticles);
     EXPECT_EQ(refParticles, testParticles);
 }
 
@@ -154,7 +158,7 @@ TEST(NbnxmSetupTest, canCheckKernelSetup)
 #ifdef GMX_NBNXN_SIMD_2XNN
     nbKernelOptions.nbnxmSimd = SimdKernels::Simd2XMM;
 #endif
-    EXPECT_NO_THROW(checkKernelSetup(nbKernelOptions.nbnxmSimd));
+    EXPECT_NO_THROW(checkKernelSetupSimd(nbKernelOptions.nbnxmSimd));
 }
 
 // check if the user is allowed to ask for SimdKernels::Simd2XMM when NBLIB is not compiled with it
@@ -164,7 +168,7 @@ TEST(NbnxmSetupTest, cannotCreateKernelSetupCPU2XM)
     NBKernelOptions nbKernelOptions;
     nbKernelOptions.nbnxmSimd             = SimdKernels::Simd2XMM;
     nbKernelOptions.useTabulatedEwaldCorr = true;
-    EXPECT_ANY_THROW(createKernelSetupCPU(nbKernelOptions));
+    EXPECT_ANY_THROW(createKernelSetupCPU(nbKernelOptions.nbnxmSimd, nbKernelOptions.useTabulatedEwaldCorr));
 }
 #endif
 
@@ -175,7 +179,7 @@ TEST(NbnxmSetupTest, cannotCreateKernelSetupCPU4XM)
     NBKernelOptions nbKernelOptions;
     nbKernelOptions.nbnxmSimd             = SimdKernels::Simd4XM;
     nbKernelOptions.useTabulatedEwaldCorr = false;
-    EXPECT_ANY_THROW(createKernelSetupCPU(nbKernelOptions));
+    EXPECT_ANY_THROW(createKernelSetupCPU(nbKernelOptions.nbnxmSimd, nbKernelOptions.useTabulatedEwaldCorr));
 }
 #endif
 
@@ -189,6 +193,48 @@ TEST(NbnxmSetupTest, CanCreateNbnxmCPU)
     EXPECT_NO_THROW(createNbnxmCPU(numParticles, nbKernelOptions, numEnergyGroups, nonbondedParameters));
 }
 
+#if GMX_GPU_CUDA
+TEST(NbnxmSetupTest, canCreateKernelSetupGPU)
+{
+    NBKernelOptions    nbKernelOptions;
+    Nbnxm::KernelSetup kernelSetup = createKernelSetupGPU(nbKernelOptions.useTabulatedEwaldCorr);
+    EXPECT_EQ(kernelSetup.kernelType, Nbnxm::KernelType::Gpu8x8x8);
+    EXPECT_EQ(kernelSetup.ewaldExclusionType, Nbnxm::EwaldExclusionType::Analytical);
+}
+
+TEST(NbnxmSetupTest, CanCreateDeviceStreamManager)
+{
+    const auto& testDeviceList = gmx::test::getTestHardwareEnvironment()->getTestDeviceList();
+    for (const auto& testDevice : testDeviceList)
+    {
+        const DeviceInformation& deviceInfo = testDevice->deviceInfo();
+        setActiveDevice(deviceInfo);
+        gmx::SimulationWorkload simulationWork = createSimulationWorkloadGpu();
+        EXPECT_NO_THROW(createDeviceStreamManager(deviceInfo, simulationWork));
+    }
+}
+
+TEST(NbnxmSetupTest, CanCreateNbnxmGPU)
+{
+    const auto& testDeviceList = gmx::test::getTestHardwareEnvironment()->getTestDeviceList();
+    for (const auto& testDevice : testDeviceList)
+    {
+        const DeviceInformation& deviceInfo = testDevice->deviceInfo();
+        setActiveDevice(deviceInfo);
+        size_t                  numParticles = 1;
+        NBKernelOptions         nbKernelOptions;
+        std::vector<real>       nonbondedParameters = { 1, 1 };
+        gmx::SimulationWorkload simulationWork      = createSimulationWorkloadGpu();
+        interaction_const_t     interactionConst    = createInteractionConst(nbKernelOptions);
+        // set DeviceInformation and create the DeviceStreamManager
+        auto deviceStreamManager = createDeviceStreamManager(deviceInfo, simulationWork);
+        EXPECT_NO_THROW(createNbnxmGPU(
+                numParticles, nbKernelOptions, nonbondedParameters, interactionConst, *deviceStreamManager));
+    }
+}
+
+#endif
+
 } // namespace
 } // namespace test
 } // namespace nblib