Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor MemoryUtils #1806

Merged
merged 4 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions arcane/src/arcane/accelerator/core/ReduceMemoryImpl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

#include "arcane/utils/CheckedConvert.h"
#include "arcane/utils/PlatformUtils.h"
#include "arcane/utils/IMemoryRessourceMng.h"
#include "arcane/utils/MemoryUtils.h"

#include "arcane/accelerator/core/Runner.h"
#include "arcane/accelerator/core/Memory.h"
Expand All @@ -32,7 +32,7 @@
{
IMemoryAllocator* _getAllocator(eMemoryRessource r)
{
return platform::getDataMemoryRessourceMng()->getAllocator(r);
return MemoryUtils::getAllocator(r);

Check warning on line 35 in arcane/src/arcane/accelerator/core/ReduceMemoryImpl.cc

View check run for this annotation

Codecov / codecov/patch

arcane/src/arcane/accelerator/core/ReduceMemoryImpl.cc#L35

Added line #L35 was not covered by tests
}
} // namespace

Expand Down
6 changes: 3 additions & 3 deletions arcane/src/arcane/impl/VariableSynchronizer.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
//-----------------------------------------------------------------------------
// Copyright 2000-2023 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
// Copyright 2000-2024 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: Apache-2.0
//-----------------------------------------------------------------------------
Expand All @@ -17,8 +17,8 @@
#include "arcane/utils/NotSupportedException.h"
#include "arcane/utils/FatalErrorException.h"
#include "arcane/utils/ITraceMng.h"
#include "arcane/utils/IMemoryRessourceMng.h"
#include "arcane/utils/ValueConvert.h"
#include "arcane/utils/MemoryUtils.h"
#include "arcane/utils/internal/MemoryBuffer.h"

#include "arcane/core/VariableSynchronizerEventArgs.h"
Expand Down Expand Up @@ -282,7 +282,7 @@
// implémentations MPI (i.e: BXI) ne le supportent pas.
if (m_runner) {
buffer_copier->setRunQueue(internal_pm->defaultQueue());
allocator = platform::getDataMemoryRessourceMng()->getAllocator(eMemoryRessource::Device);
allocator = MemoryUtils::getAllocator(eMemoryRessource::Device);

Check warning on line 285 in arcane/src/arcane/impl/VariableSynchronizer.cc

View check run for this annotation

Codecov / codecov/patch

arcane/src/arcane/impl/VariableSynchronizer.cc#L285

Added line #L285 was not covered by tests
}

// Créé une instance de l'implémentation
Expand Down
10 changes: 5 additions & 5 deletions arcane/src/arcane/materials/MeshMaterialSynchronizeBuffer.cc
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
//-----------------------------------------------------------------------------
// Copyright 2000-2023 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
// Copyright 2000-2024 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: Apache-2.0
//-----------------------------------------------------------------------------
/*---------------------------------------------------------------------------*/
/* MeshMaterialSynchronizeBuffer.cc (C) 2000-2023 */
/* MeshMaterialSynchronizeBuffer.cc (C) 2000-2024 */
/* */
/* Gestion des buffers pour la synchronisation de variables matériaux. */
/*---------------------------------------------------------------------------*/
Expand All @@ -15,7 +15,7 @@

#include "arcane/utils/UniqueArray.h"
#include "arcane/utils/PlatformUtils.h"
#include "arcane/utils/IMemoryRessourceMng.h"
#include "arcane/utils/MemoryUtils.h"

/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
Expand Down Expand Up @@ -200,7 +200,7 @@ namespace impl
extern "C++" ARCANE_MATERIALS_EXPORT Ref<IMeshMaterialSynchronizeBuffer>
makeMultiBufferMeshMaterialSynchronizeBufferRef(eMemoryRessource memory_ressource)
{
auto* a = platform::getDataMemoryRessourceMng()->getAllocator(memory_ressource);
auto* a = MemoryUtils::getAllocator(memory_ressource);
auto* v = new MultiBufferMeshMaterialSynchronizeBuffer(a);
return makeRef<IMeshMaterialSynchronizeBuffer>(v);
}
Expand All @@ -214,7 +214,7 @@ makeMultiBufferMeshMaterialSynchronizeBufferRef()
extern "C++" ARCANE_MATERIALS_EXPORT Ref<IMeshMaterialSynchronizeBuffer>
makeOneBufferMeshMaterialSynchronizeBufferRef(eMemoryRessource memory_ressource)
{
auto* a = platform::getDataMemoryRessourceMng()->getAllocator(memory_ressource);
auto* a = MemoryUtils::getAllocator(memory_ressource);
auto* v = new OneBufferMeshMaterialSynchronizeBuffer(a);
return makeRef<IMeshMaterialSynchronizeBuffer>(v);
}
Expand Down
16 changes: 9 additions & 7 deletions arcane/src/arcane/materials/internal/IndexSelecter.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@
// SPDX-License-Identifier: Apache-2.0
//-----------------------------------------------------------------------------
/*---------------------------------------------------------------------------*/
/* IndexSelecter.h (C) 2000-2024 */
/* IndexSelecter.h (C) 2000-2024 */
/* */
/* Selection d'index avec API accélérateur */
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/

#include "arcane/accelerator/Filter.h"
#include "arcane/utils/IMemoryRessourceMng.h"
#include "arcane/utils/MemoryUtils.h"

#include "arcane/accelerator/core/Memory.h"
#include "arcane/accelerator/GenericFilterer.h"

/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/

namespace Arcane::Accelerator
{
Expand All @@ -38,8 +40,8 @@ class IndexSelecter
m_is_accelerator_policy = isAcceleratorPolicy(runqueue->executionPolicy());
m_memory_host = eMemoryRessource(m_is_accelerator_policy ? eMemoryRessource::HostPinned : eMemoryRessource::Host);
m_memory_device = eMemoryRessource(m_is_accelerator_policy ? eMemoryRessource::Device : eMemoryRessource::Host);
m_localid_select_device = UniqueArray<Int32>(platform::getDataMemoryRessourceMng()->getAllocator(m_memory_device));
m_localid_select_host = UniqueArray<Int32>(platform::getDataMemoryRessourceMng()->getAllocator(m_memory_host));
m_localid_select_device = UniqueArray<Int32>(MemoryUtils::getAllocator(m_memory_device));
m_localid_select_host = UniqueArray<Int32>(MemoryUtils::getAllocator(m_memory_host));
}

~IndexSelecter()
Expand Down
4 changes: 2 additions & 2 deletions arcane/src/arcane/parallel/thread/SharedMemoryMessageQueue.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "arcane/utils/ITraceMng.h"
#include "arcane/utils/ValueConvert.h"

#include "arcane/utils/internal/MemoryRessourceMng.h"
#include "arcane/utils/internal/MemoryResourceMng.h"

#include "arcane/parallel/thread/SharedMemoryMessageQueue.h"
#include "arcane/parallel/thread/IAsyncQueue.h"
Expand Down Expand Up @@ -73,7 +73,7 @@ copyFromSender(SharedMemoryMessageRequest* sender)
ARCANE_FATAL("Not enough memory for receiving message receive={0} send={1}",
receive_size,send_size);

MemoryRessourceMng::genericCopy(ConstMemoryView(send_span), MutableMemoryView(receive_span));
MemoryResourceMng::genericCopy(ConstMemoryView(send_span), MutableMemoryView(receive_span));
}

/*---------------------------------------------------------------------------*/
Expand Down
3 changes: 2 additions & 1 deletion arcane/src/arcane/tests/accelerator/NumArrayUnitTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "arcane/utils/PlatformUtils.h"
#include "arcane/utils/ValueChecker.h"
#include "arcane/utils/IMemoryRessourceMng.h"
#include "arcane/utils/MemoryUtils.h"

#include "arcane/core/BasicUnitTest.h"
#include "arcane/core/ServiceFactory.h"
Expand Down Expand Up @@ -608,7 +609,7 @@ _executeTest4(eMemoryRessource mem_kind)
constexpr int n1 = 1000;

constexpr double expected_sum1 = 999000.0;
IMemoryAllocator* allocator = platform::getDataMemoryRessourceMng()->getAllocator(mem_kind);
IMemoryAllocator* allocator = MemoryUtils::getAllocator(mem_kind);

{
SharedArray<double> t1;
Expand Down
16 changes: 8 additions & 8 deletions arcane/src/arcane/utils/IMemoryRessourceMng.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
// SPDX-License-Identifier: Apache-2.0
//-----------------------------------------------------------------------------
/*---------------------------------------------------------------------------*/
/* IMemoryRessourceMng.h (C) 2000-2024 */
/* IMemoryResourceMng.h (C) 2000-2024 */
/* */
/* Gestion des ressources mémoire pour les CPU et accélérateurs. */
/*---------------------------------------------------------------------------*/
#ifndef ARCANE_UTILS_IMEMORYRESSOURCEMNG_H
#define ARCANE_UTILS_IMEMORYRESSOURCEMNG_H
#ifndef ARCANE_UTILS_IMEMORYRESOURCEMNG_H
#define ARCANE_UTILS_IMEMORYRESOURCEMNG_H
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/

#include "arcane/utils/MemoryRessource.h"
#include "arcane/utils/UtilsTypes.h"

/*---------------------------------------------------------------------------*/
Expand All @@ -27,13 +26,14 @@ class IMemoryRessourceMngInternal;
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
/*!
* \internal
* \brief Gestion des ressources mémoire pour les CPU et accélérateurs.
*/
class ARCANE_UTILS_EXPORT IMemoryRessourceMng
class ARCANE_UTILS_EXPORT IMemoryResourceMng
{
public:

virtual ~IMemoryRessourceMng() = default;
virtual ~IMemoryResourceMng() = default;

public:

Expand All @@ -42,7 +42,7 @@ class ARCANE_UTILS_EXPORT IMemoryRessourceMng
*
* Lève une exception si aucun allocateur pour la ressource \a v existe.
*/
virtual IMemoryAllocator* getAllocator(eMemoryRessource r) = 0;
virtual IMemoryAllocator* getAllocator(eMemoryResource r) = 0;

/*!
* \brief Allocateur mémoire pour la ressource \a r.
Expand All @@ -51,7 +51,7 @@ class ARCANE_UTILS_EXPORT IMemoryRessourceMng
* exception si \a throw_if_not_found est vrai ou retourne \a nullptr
* si \a throw_if_not_found est faux.
*/
virtual IMemoryAllocator* getAllocator(eMemoryRessource r, bool throw_if_not_found) = 0;
virtual IMemoryAllocator* getAllocator(eMemoryResource r, bool throw_if_not_found) = 0;

public:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@
// SPDX-License-Identifier: Apache-2.0
//-----------------------------------------------------------------------------
/*---------------------------------------------------------------------------*/
/* MemoryRessourceMng.cc (C) 2000-2024 */
/* MemoryResourceMng.cc (C) 2000-2024 */
/* */
/* Gestion des ressources mémoire pour les CPU et accélérateurs. */
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/

#include "arcane/utils/internal/MemoryRessourceMng.h"
#include "arcane/utils/internal/MemoryResourceMng.h"

#include "arcane/utils/FatalErrorException.h"
#include "arcane/utils/PlatformUtils.h"
#include "arcane/utils/Array.h"
#include "arcane/utils/MemoryView.h"
#include "arcane/utils/MemoryAllocator.h"
#include "arcane/utils/internal/MemoryUtilsInternal.h"

/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
Expand All @@ -30,42 +31,18 @@

namespace
{
const char* _toName(eMemoryRessource r)
{
switch (r) {
case eMemoryRessource::Unknown:
return "Unknown";
case eMemoryRessource::Host:
return "Host";
case eMemoryRessource::HostPinned:
return "HostPinned";
case eMemoryRessource::Device:
return "Device";
case eMemoryRessource::UnifiedMemory:
return "UnifiedMemory";
}
return "Invalid";
}

inline bool _isHost(eMemoryRessource r)
{
// Si on sait pas, considère qu'on est accessible de puis l'hôte.
if (r == eMemoryRessource::Unknown)
if (r == eMemoryResource::Unknown)
return true;
if (r == eMemoryRessource::Host || r == eMemoryRessource::UnifiedMemory || r == eMemoryRessource::HostPinned)
if (r == eMemoryResource::Host || r == eMemoryResource::UnifiedMemory || r == eMemoryResource::HostPinned)
return true;
return false;
}

} // namespace

extern "C++" ARCANE_UTILS_EXPORT std::ostream&
operator<<(std::ostream& o, eMemoryRessource r)
{
o << _toName(r);
return o;
}

/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/

Expand All @@ -74,8 +51,8 @@
{
public:

void copy(ConstMemoryView from, eMemoryRessource from_mem,
MutableMemoryView to, eMemoryRessource to_mem,
void copy(ConstMemoryView from, eMemoryResource from_mem,
MutableMemoryView to, eMemoryResource to_mem,
[[maybe_unused]] const RunQueue* queue) override
{
// Sans support accélérateur, on peut juste faire un 'memcpy' si la mémoire
Expand All @@ -96,8 +73,8 @@
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/

MemoryRessourceMng::
MemoryRessourceMng()
MemoryResourceMng::
MemoryResourceMng()
: m_default_memory_copier(new DefaultHostMemoryCopier())
, m_copier(m_default_memory_copier.get())
{
Expand All @@ -111,22 +88,22 @@
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/

int MemoryRessourceMng::
_checkValidRessource(eMemoryRessource r)
int MemoryResourceMng::
_checkValidResource(eMemoryResource r)
{
int x = (int)r;
if (x <= 0 || x >= NB_MEMORY_RESSOURCE)
ARCANE_FATAL("Invalid value '{0}'. Valid range is '1' to '{1}'", x, NB_MEMORY_RESSOURCE - 1);
if (x <= 0 || x >= Arccore::ARCCORE_NB_MEMORY_RESOURCE)
ARCANE_FATAL("Invalid value '{0}'. Valid range is '1' to '{1}'", x, Arccore::ARCCORE_NB_MEMORY_RESOURCE - 1);

Check warning on line 96 in arcane/src/arcane/utils/MemoryResourceMng.cc

View check run for this annotation

Codecov / codecov/patch

arcane/src/arcane/utils/MemoryResourceMng.cc#L96

Added line #L96 was not covered by tests
return x;
}

/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/

IMemoryAllocator* MemoryRessourceMng::
getAllocator(eMemoryRessource r, bool throw_if_not_found)
IMemoryAllocator* MemoryResourceMng::
getAllocator(eMemoryResource r, bool throw_if_not_found)
{
int x = _checkValidRessource(r);
int x = _checkValidResource(r);
IMemoryAllocator* a = m_allocators[x];

// Si pas d'allocateur spécifique et qu'on n'est pas sur accélérateur,
Expand All @@ -148,28 +125,28 @@
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/

IMemoryAllocator* MemoryRessourceMng::
getAllocator(eMemoryRessource r)
IMemoryAllocator* MemoryResourceMng::
getAllocator(eMemoryResource r)
{
return getAllocator(r, true);
}

/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/

void MemoryRessourceMng::
setAllocator(eMemoryRessource r, IMemoryAllocator* allocator)
void MemoryResourceMng::
setAllocator(eMemoryResource r, IMemoryAllocator* allocator)
{
int x = _checkValidRessource(r);
int x = _checkValidResource(r);
m_allocators[x] = allocator;
}

/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/

void MemoryRessourceMng::
copy(ConstMemoryView from, eMemoryRessource from_mem,
MutableMemoryView to, eMemoryRessource to_mem, const RunQueue* queue)
void MemoryResourceMng::
copy(ConstMemoryView from, eMemoryResource from_mem,
MutableMemoryView to, eMemoryResource to_mem, const RunQueue* queue)
{
Int64 from_size = from.bytes().size();
Int64 to_size = to.bytes().size();
Expand All @@ -182,11 +159,11 @@
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/

void MemoryRessourceMng::
void MemoryResourceMng::
genericCopy(ConstMemoryView from, MutableMemoryView to)
{
IMemoryRessourceMng* mrm = platform::getDataMemoryRessourceMng();
eMemoryRessource mem_type = eMemoryRessource::Unknown;
IMemoryResourceMng* mrm = MemoryUtils::getDataMemoryResourceMng();
eMemoryResource mem_type = eMemoryResource::Unknown;
mrm->_internal()->copy(from, mem_type, to, mem_type, nullptr);
}

Expand Down
Loading
Loading