Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
VariableUtils.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* VariableUtils.cc (C) 2000-2025 */
9/* */
10/* Various utility functions for variables. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
15
17#include "arcane/utils/IMemoryAllocator.h"
18
19#include "arcane/accelerator/core/RunQueue.h"
20#include "arcane/accelerator/core/Memory.h"
21
22#include "arcane/core/IData.h"
23#include "arcane/core/IVariable.h"
24#include "arcane/core/VariableRef.h"
25#include "arcane/core/internal/IDataInternal.h"
26#include "arcane/core/internal/IVariableInternal.h"
27#include "arcane/core/datatype/DataAllocationInfo.h"
29#include "arcane/core/materials/internal/IMeshMaterialVariableInternal.h"
30
31/*---------------------------------------------------------------------------*/
32/*---------------------------------------------------------------------------*/
33
34/*!
35 * \file VariableUtils.h
36 *
37 * \brief Utility functions for variables.
38 */
39
40/*---------------------------------------------------------------------------*/
41/*---------------------------------------------------------------------------*/
42
43namespace Arcane
44{
45using namespace Arcane::Accelerator;
46
47/*---------------------------------------------------------------------------*/
48/*---------------------------------------------------------------------------*/
49
50void VariableUtils::
51prefetchVariableAsync(IVariable* var, const RunQueue* queue_or_null)
52{
54 if (!queue_or_null)
55 return;
56 if (!var->isUsed())
57 return;
58 IData* d = var->data();
59 INumericDataInternal* nd = d->_commonInternal()->numericData();
60 if (!nd)
61 return;
62 // We only prefetch if the variable is in unified memory.
63 // This is only useful in this case and furthermore with CUDA it causes
64 // an error if the memory is not u
65 if (nd->memoryAllocator()->memoryResource() != eMemoryResource::UnifiedMemory)
66 return;
67 ConstMemoryView mem_view = nd->memoryView();
68 queue_or_null->prefetchMemory(MemoryPrefetchArgs(mem_view).addAsync());
69}
70
71/*---------------------------------------------------------------------------*/
72/*---------------------------------------------------------------------------*/
73
74void VariableUtils::
75prefetchVariableAsync(VariableRef& var, const RunQueue* queue_or_null)
76{
77 return prefetchVariableAsync(var.variable(), queue_or_null);
78}
79
80/*---------------------------------------------------------------------------*/
81/*---------------------------------------------------------------------------*/
82
83void VariableUtils::
84markVariableAsMostlyReadOnly(IVariable* var)
85{
87 var->setAllocationInfo(alloc_info);
88}
89
90/*---------------------------------------------------------------------------*/
91/*---------------------------------------------------------------------------*/
92
93void VariableUtils::
94markVariableAsMostlyReadOnly(VariableRef& var)
95{
97}
98
99/*---------------------------------------------------------------------------*/
100/*---------------------------------------------------------------------------*/
101
102void VariableUtils::
103markVariableAsMostlyReadOnly(::Arcane::Materials::MeshMaterialVariableRef& var)
104{
105 auto vars = var.materialVariable()->_internalApi()->variableReferenceList();
106 for (VariableRef* v : vars)
107 markVariableAsMostlyReadOnly(v->variable());
108}
109
110/*---------------------------------------------------------------------------*/
111/*---------------------------------------------------------------------------*/
112
113void VariableUtils::
114experimentalChangeAllocator(::Arcane::Materials::IMeshMaterialVariable* var,
116{
118 Arcane::Materials::IMeshMaterialVariableInternal* mat_var = var->_internalApi();
119 for (VariableRef* vref : mat_var->variableReferenceList())
120 vref->variable()->_internalApi()->changeAllocator(mem_opts);
121 var->globalVariable()->_internalApi()->changeAllocator(mem_opts);
122}
123
124/*---------------------------------------------------------------------------*/
125/*---------------------------------------------------------------------------*/
126
127void VariableUtils::
128experimentalChangeAllocator(IVariable* var, eMemoryRessource mem)
129{
131 var->_internalApi()->changeAllocator(mem_opts);
132}
133
134/*---------------------------------------------------------------------------*/
135/*---------------------------------------------------------------------------*/
136
137void VariableUtils::
138experimentalChangeAllocator(VariableRef& var, eMemoryRessource mem)
139{
140 experimentalChangeAllocator(var.variable(), mem);
141}
142
143/*---------------------------------------------------------------------------*/
144/*---------------------------------------------------------------------------*/
145
146} // namespace Arcane
147
148/*---------------------------------------------------------------------------*/
149/*---------------------------------------------------------------------------*/
#define ARCANE_CHECK_POINTER(ptr)
Macro returning the pointer ptr if it is not null or throwing an exception if it is null.
Utility functions for variables.
void markVariableAsMostlyReadOnly(IVariable *var)
Indicates that the variable is mostly read-only.
void prefetchVariableAsync(IVariable *var, const RunQueue *queue_or_null)
Prefetches the memory associated with the variable var.
Memory and allocator management functions.
void prefetchMemory(const MemoryPrefetchArgs &args) const
Performs a memory prefetch.
Definition RunQueue.cc:247
Constant view on a contiguous memory region containing fixed-size elements.
Information on data allocation.
Interface of a data item.
Definition IData.h:34
Interface of a variable.
Definition IVariable.h:40
virtual IData * data()=0
Data associated with the variable.
virtual bool isUsed() const =0
Usage state of the variable.
virtual void setAllocationInfo(const DataAllocationInfo &v)=0
Sets allocation information.
virtual IVariableInternal * _internalApi()=0
Internal Arcane API.
Interface of a material variable on a mesh.
virtual IVariable * globalVariable() const =0
Associated global variable on the mesh.
IMeshMaterialVariable * materialVariable() const
Associated material variable.
Reference to a variable.
Definition VariableRef.h:56
IVariable * variable() const
Associated variable.
MemoryAllocationOptions getAllocationOptions(eMemoryResource mem_resource)
Default allocation for the resource mem_resource.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
@ HostAndDeviceMostlyRead
Indicates that the data will be used both on the accelerator and on the CPU and will not be frequentl...
Arcane::eMemoryResource eMemoryRessource
Typedef for the historical Arcane version (with 2's').
@ UnifiedMemory
Allocates using unified memory.