Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:24:15

0001 /*
0002  * Base class for tests.
0003  *
0004  * Author: Marcel Rieger
0005  */
0006 
0007 #ifndef PHYSICSTOOLS_TENSORFLOW_TEST_TESTBASECUDA_H
0008 #define PHYSICSTOOLS_TENSORFLOW_TEST_TESTBASECUDA_H
0009 
0010 #include <boost/filesystem.hpp>
0011 #include <filesystem>
0012 #include <cppunit/extensions/HelperMacros.h>
0013 #include <stdexcept>
0014 #include "catch.hpp"
0015 
0016 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0017 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0018 #include "FWCore/ParameterSetReader/interface/ParameterSetReader.h"
0019 #include "FWCore/PluginManager/interface/PluginManager.h"
0020 #include "FWCore/PluginManager/interface/standard.h"
0021 #include "FWCore/ServiceRegistry/interface/Service.h"
0022 #include "FWCore/ServiceRegistry/interface/ServiceRegistry.h"
0023 #include "FWCore/ServiceRegistry/interface/ServiceToken.h"
0024 #include "FWCore/Utilities/interface/Exception.h"
0025 #include "FWCore/Utilities/interface/ResourceInformation.h"
0026 #include "HeterogeneousCore/CUDAServices/interface/CUDAInterface.h"
0027 #include "HeterogeneousCore/CUDAUtilities/interface/requireDevices.h"
0028 
0029 class testBaseCUDA : public CppUnit::TestFixture {
0030 public:
0031   std::string dataPath_;
0032 
0033   void setUp();
0034   void tearDown();
0035   std::string cmsswPath(std::string path);
0036 
0037   virtual void test() = 0;
0038 
0039   virtual std::string pyScript() const = 0;
0040 };
0041 
0042 void testBaseCUDA::setUp() {
0043   dataPath_ =
0044       cmsswPath("/test/" + std::string(std::getenv("SCRAM_ARCH")) + "/" + boost::filesystem::unique_path().string());
0045 
0046   // create the graph
0047   std::string testPath = cmsswPath("/src/PhysicsTools/TensorFlow/test");
0048   std::string cmd = "python3 -W ignore " + testPath + "/" + pyScript() + " " + dataPath_;
0049   std::array<char, 128> buffer;
0050   std::string result;
0051   std::shared_ptr<FILE> pipe(popen(cmd.c_str(), "r"), pclose);
0052   if (!pipe) {
0053     throw std::runtime_error("popen() failed!");
0054   }
0055   while (!feof(pipe.get())) {
0056     if (fgets(buffer.data(), 128, pipe.get()) != NULL) {
0057       result += buffer.data();
0058     }
0059   }
0060   std::cout << std::endl << result << std::endl;
0061 }
0062 
0063 void testBaseCUDA::tearDown() {
0064   if (std::filesystem::exists(dataPath_)) {
0065     std::filesystem::remove_all(dataPath_);
0066   }
0067 }
0068 
0069 std::string testBaseCUDA::cmsswPath(std::string path) {
0070   if (path.size() > 0 && path.substr(0, 1) != "/") {
0071     path = "/" + path;
0072   }
0073 
0074   std::string base = std::string(std::getenv("CMSSW_BASE"));
0075   std::string releaseBase = std::string(std::getenv("CMSSW_RELEASE_BASE"));
0076 
0077   return (std::filesystem::exists(base.c_str()) ? base : releaseBase) + path;
0078 }
0079 
0080 #endif  // PHYSICSTOOLS_TENSORFLOW_TEST_TESTBASECUDA_H