Skip to content
This repository has been archived by the owner on Dec 9, 2024. It is now read-only.

Remove device initialization from library #385

Merged
merged 11 commits into from
Apr 23, 2024
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ OBJECTS=$(OBJECTS_CPU) $(OBJECTS_CUDA) $(OBJECTS_ROCM)

CXX = g++
CXXFLAGS = -g -O2 -Wall -fPIC -Wshadow -Woverloaded-virtual -Wno-unused-function -fno-var-tracking -std=c++17
INCLUDEFLAGS= -ISDL -I$(shell pwd) -Icode -Icode/core -I${ALPAKA_ROOT}/include -I/${BOOST_ROOT}/include $(shell rooutil-config --include) -I$(shell root-config --incdir)
INCLUDEFLAGS= -ISDL -I$(shell pwd) -Icode -Icode/core -I${ALPAKA_ROOT}/include -I/${BOOST_ROOT}/include $(shell rooutil-config --include) -I$(shell root-config --incdir) -I${CMSSW_BASE}/src
ifdef CMSSW_RELEASE_BASE
INCLUDEFLAGS:= ${INCLUDEFLAGS} -I${CMSSW_RELEASE_BASE}/src
endif
LDFLAGS = -g -O2 $(SDLLIB) -L${TRACKLOOPERDIR}/SDL $(shell rooutil-config --libs) $(shell root-config --libs)
LDFLAGS_CUDA= -L${CUDA_HOME}/lib64 -lcudart
LDFLAGS_ROCM= -L${ROCM_ROOT}/lib -lamdhip64
Expand Down
47 changes: 13 additions & 34 deletions SDL/Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include <alpaka/alpaka.hpp>

#include "HeterogeneousCore/AlpakaInterface/interface/config.h"

#ifdef CACHE_ALLOC
#include "HeterogeneousCore/AlpakaInterface/interface/CachedBufAlloc.h"
#endif
Expand All @@ -23,33 +25,19 @@ namespace SDL {
typedef float FPX;
#endif

using Idx = std::size_t;
using Dim = alpaka::DimInt<3u>;
using Dim1d = alpaka::DimInt<1u>;
using Vec = alpaka::Vec<Dim, Idx>;
using Vec1d = alpaka::Vec<Dim1d, Idx>;
#if defined(ALPAKA_ACC_GPU_CUDA_ENABLED) || defined(ALPAKA_ACC_GPU_HIP_ENABLED)
using QueueProperty = alpaka::NonBlocking;
#else
using QueueProperty = alpaka::Blocking;
#endif
using WorkDiv = alpaka::WorkDivMembers<Dim, Idx>;
using Idx = alpaka_common::Idx;
using Dim = alpaka_common::Dim3D;
using Dim1d = alpaka_common::Dim1D;
using Vec = alpaka_common::Vec3D;
using Vec1d = alpaka_common::Vec1D;
using WorkDiv = alpaka_common::WorkDiv3D;

Vec const elementsPerThread(Vec::all(static_cast<Idx>(1)));
using Acc = ALPAKA_ACCELERATOR_NAMESPACE::Acc3D;
using Dev = ALPAKA_ACCELERATOR_NAMESPACE::Device;
using DevHost = ALPAKA_ACCELERATOR_NAMESPACE::DevHost;
using QueueAcc = ALPAKA_ACCELERATOR_NAMESPACE::Queue;

// - AccGpuCudaRt
// - AccCpuThreads
// - AccCpuSerial
#ifdef ALPAKA_ACC_GPU_CUDA_ENABLED
using Acc = alpaka::AccGpuCudaRt<Dim, Idx>;
#elif ALPAKA_ACC_CPU_B_SEQ_T_THREADS_ENABLED
using Acc = alpaka::AccCpuThreads<Dim, Idx>;
#elif ALPAKA_ACC_CPU_B_SEQ_T_SEQ_ENABLED
using Acc = alpaka::AccCpuSerial<Dim, Idx>;
#elif ALPAKA_ACC_GPU_HIP_ENABLED
using Acc = alpaka::AccGpuHipRt<Dim, Idx>;
#endif
using Dev = alpaka::Dev<Acc>;
Vec const elementsPerThread(Vec::all(static_cast<Idx>(1)));

// Needed for files that are compiled by g++ to not throw an error.
// uint4 is defined only for CUDA, so we will have to revisit this soon when running on other backends.
Expand All @@ -62,15 +50,6 @@ namespace SDL {
};
#endif

auto const platformAcc = alpaka::Platform<Acc>{};
auto const platformHost = alpaka::Platform<alpaka::DevCpu>{};
auto const devHost = alpaka::getDevByIdx(platformHost, 0u);
#if defined ALPAKA_ACC_GPU_CUDA_ENABLED || defined ALPAKA_ACC_CPU_B_SEQ_T_THREADS_ENABLED || \
defined ALPAKA_ACC_CPU_B_SEQ_T_SEQ_ENABLED || defined ALPAKA_ACC_GPU_HIP_ENABLED
auto const devAcc = alpaka::getDevByIdx(platformAcc, 0u);
using QueueAcc = alpaka::Queue<Acc, QueueProperty>;
#endif

// Buffer type for allocations where auto type can't be used.
template <typename TDev, typename TData>
using Buf = alpaka::Buf<TDev, TData, Dim1d, Idx>;
Expand Down
34 changes: 18 additions & 16 deletions SDL/EndcapGeometry.cc
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
#include "EndcapGeometry.h"

SDL::EndcapGeometry<SDL::Dev>::EndcapGeometry(unsigned int sizef)
: geoMapDetId_buf(allocBufWrapper<unsigned int>(devAcc, sizef)),
geoMapPhi_buf(allocBufWrapper<float>(devAcc, sizef)) {}

SDL::EndcapGeometry<SDL::Dev>::EndcapGeometry(std::string filename, unsigned int sizef)
: geoMapDetId_buf(allocBufWrapper<unsigned int>(devAcc, sizef)),
geoMapPhi_buf(allocBufWrapper<float>(devAcc, sizef)) {
load(filename);
SDL::EndcapGeometry<SDL::Dev>::EndcapGeometry(SDL::Dev const& devAccIn, unsigned int sizef)
: geoMapDetId_buf(allocBufWrapper<unsigned int>(devAccIn, sizef)),
geoMapPhi_buf(allocBufWrapper<float>(devAccIn, sizef)) {}

SDL::EndcapGeometry<SDL::Dev>::EndcapGeometry(SDL::Dev const& devAccIn,
SDL::QueueAcc& queue,
std::string filename,
unsigned int sizef)
: geoMapDetId_buf(allocBufWrapper<unsigned int>(devAccIn, sizef)),
geoMapPhi_buf(allocBufWrapper<float>(devAccIn, sizef)) {
load(queue, filename);
}

void SDL::EndcapGeometry<SDL::Dev>::load(std::string filename) {
void SDL::EndcapGeometry<SDL::Dev>::load(SDL::QueueAcc& queue, std::string filename) {
dxdy_slope_.clear();
centroid_phis_.clear();

Expand All @@ -30,13 +33,11 @@ void SDL::EndcapGeometry<SDL::Dev>::load(std::string filename) {
}
}

fillGeoMapArraysExplicit();
fillGeoMapArraysExplicit(queue);
}

void SDL::EndcapGeometry<SDL::Dev>::fillGeoMapArraysExplicit() {
QueueAcc queue(devAcc);

int phi_size = centroid_phis_.size();
void SDL::EndcapGeometry<SDL::Dev>::fillGeoMapArraysExplicit(SDL::QueueAcc& queue) {
unsigned int phi_size = centroid_phis_.size();

// Temporary check for endcap initialization.
if (phi_size != endcap_size) {
Expand All @@ -47,6 +48,7 @@ void SDL::EndcapGeometry<SDL::Dev>::fillGeoMapArraysExplicit() {
}

// Allocate buffers on host
SDL::DevHost const& devHost = cms::alpakatools::host();
auto mapPhi_host_buf = allocBufWrapper<float>(devHost, phi_size);
auto mapDetId_host_buf = allocBufWrapper<unsigned int>(devHost, phi_size);

Expand All @@ -66,8 +68,8 @@ void SDL::EndcapGeometry<SDL::Dev>::fillGeoMapArraysExplicit() {
nEndCapMap = counter;

// Copy data from host to device buffers
alpaka::memcpy(queue, geoMapPhi_buf, mapPhi_host_buf, phi_size);
alpaka::memcpy(queue, geoMapDetId_buf, mapDetId_host_buf, phi_size);
alpaka::memcpy(queue, geoMapPhi_buf, mapPhi_host_buf);
alpaka::memcpy(queue, geoMapDetId_buf, mapDetId_host_buf);
alpaka::wait(queue);
}

Expand Down
11 changes: 6 additions & 5 deletions SDL/EndcapGeometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

#include "Constants.h"

#include "HeterogeneousCore/AlpakaInterface/interface/host.h"

namespace SDL {
template <typename TDev>
class EndcapGeometry {};
Expand All @@ -26,14 +28,13 @@ namespace SDL {

unsigned int nEndCapMap;

EndcapGeometry(unsigned int sizef = endcap_size);
EndcapGeometry(std::string filename, unsigned int sizef = endcap_size);
EndcapGeometry(Dev const& devAccIn, unsigned int sizef = endcap_size);
EndcapGeometry(Dev const& devAccIn, QueueAcc& queue, std::string filename, unsigned int sizef = endcap_size);
~EndcapGeometry() = default;

void load(std::string);
void load(QueueAcc& queue, std::string);

void fillGeoMapArraysExplicit();
void CreateGeoMapArraysExplicit();
void fillGeoMapArraysExplicit(QueueAcc& queue);
float getdxdy_slope(unsigned int detid);
};
} // namespace SDL
Expand Down
Loading